source: trunk/gli/src/org/greenstone/gatherer/collection/SaveCollectionBox.java@ 6224

Last change on this file since 6224 was 5581, checked in by mdewsnip, 21 years ago

Many formatting, structural and code improvements.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer.collection;
38
39import java.awt.*;
40import java.awt.event.*;
41import javax.swing.*;
42import org.greenstone.gatherer.Dictionary;
43import org.greenstone.gatherer.Gatherer;
44
45/** Provides a prompt allowing the user some choice in whether a collection saves. */
46public class SaveCollectionBox
47 extends JDialog
48 implements ActionListener{
49 /** What option the user has choosen. */
50 private int result = 0;
51 /** Button to cancel prompt, no save. */
52 private JButton cancel = null;
53 /** Button for no save. */
54 private JButton no = null;
55 /** Button to save. */
56 private JButton yes = null;
57 /** A reference to ourselves so our inner classes can dispose of us. */
58 private SaveCollectionBox myself = null;
59 /** Result value if the user has choosen cancel. */
60 static final public int SAVE_CANCEL = 0;
61 /** Result value if the user has choosen no. */
62 static final public int SAVE_NO = 1;
63 /** Result value if the user has choosen yes. */
64 static final public int SAVE_YES = 2;
65 /** Construtor. */
66 public SaveCollectionBox() {
67 super(Gatherer.g_man);
68 this.myself = this;
69 result = SAVE_CANCEL;
70 // Dialog setup
71 this.setModal(true);
72 this.setSize(360,100);
73 Dictionary.setText(this, "SaveCollectionBox.Title");
74 }
75
76 /** Any implementation of <i>ActionListener</i> must include this method so that we can be informed when an action has occured. In this case we see what button the users clicked, set the appropriate result then dispose of the dialog. */
77 public void actionPerformed(ActionEvent event) {
78 if(event.getSource() == yes) {
79 result = SAVE_YES;
80 this.dispose();
81 } else if(event.getSource() == no) {
82 result = SAVE_NO;
83 this.dispose();
84 } else if(event.getSource() == cancel) {
85 result = SAVE_CANCEL;
86 this.dispose();
87 }
88 }
89
90 /** Destructor. */
91 public void destroy() {
92 cancel = null;
93 no.removeActionListener(this);
94 no = null;
95 yes.removeActionListener(this);
96 yes = null;
97 myself = null;
98 rootPane = null;
99 }
100
101 /** Displays the prompt by first building the controls, then waits for a user selection.
102 * @param name The name of the current collection as a <strong>String</strong>.
103 */
104 public int getUserOption(String name) {
105 JPanel content_pane = (JPanel) this.getContentPane();
106 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
107 content_pane.setLayout(new GridLayout(2,1));
108
109 String args[] = new String[1];
110 args[0] = name;
111 JLabel save_label = new JLabel();
112 Dictionary.setText(save_label, "SaveCollectionBox.Label", args);
113 content_pane.add(save_label);
114 JPanel button_pane = new JPanel(new GridLayout(1,3));
115 content_pane.add(button_pane);
116 // We add both mnemonics and key listener so that the 'y' of yes is underlined, but pressing just [Y] (rather than [CTL]-[Y]) performs a systematic click.
117 yes = new JButton();
118 yes.addActionListener(this);
119 KeyListenerImpl key_listener = new KeyListenerImpl();
120 yes.addKeyListener(key_listener);
121 yes.setMnemonic(KeyEvent.VK_Y);
122 Dictionary.setBoth(yes, "General.Yes", "General.Yes_Tooltip");
123 button_pane.add(yes);
124 no = new JButton();
125 no.addActionListener(this);
126 no.addKeyListener(key_listener);
127 no.setMnemonic(KeyEvent.VK_N);
128 Dictionary.setBoth(no, "General.No", "General.No_Tooltip");
129 button_pane.add(no);
130 cancel = new JButton();
131 cancel.addActionListener(this);
132 cancel.addKeyListener(key_listener);
133 cancel.setMnemonic(KeyEvent.VK_C);
134 Dictionary.setBoth(cancel, "General.Cancel", "General.Cancel_Tooltip");
135 button_pane.add(cancel);
136
137 // Center on screen.
138 Dimension dlgSize = getSize();
139 Dimension frmSize = Gatherer.g_man.getSize();
140 Point loc = Gatherer.g_man.getLocation();
141 setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
142 show();
143
144 // Deallocate things we allocated here.
145 yes.removeKeyListener(key_listener);
146 no.removeKeyListener(key_listener);
147 cancel.removeKeyListener(key_listener);
148 key_listener = null;
149 content_pane = null;
150 args = null;
151 save_label = null;
152 button_pane = null;
153 dlgSize = null;
154 frmSize = null;
155 loc = null;
156 return result;
157 }
158
159 /** Listens for key presses when the focus is on one of the buttons. Note that the key pressed needn't be the one associated with the button in focus. */
160 private class KeyListenerImpl
161 extends KeyAdapter {
162 /** Any extension of KeyAdapter can override this method so that we can be informed whenever a key is released (ie after a keyTyped event). In this case we map the key press to an appropriate click on one of the buttons (not necessarily the one the key release was detected on).
163 * @param event A <strong>KeyEvent</strong> containing details about the key release event.
164 */
165 public void keyReleased(KeyEvent event) {
166 if(event.getKeyCode() == KeyEvent.VK_Y) {
167 yes.doClick(10);
168 }
169 else if(event.getKeyCode() == KeyEvent.VK_N) {
170 no.doClick(10);
171 }
172 else if(event.getKeyCode() == KeyEvent.VK_C) {
173 cancel.doClick(10);
174 }
175 }
176 }
177}
Note: See TracBrowser for help on using the repository browser.