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

Last change on this file since 8141 was 6318, checked in by jmt12, 20 years ago

Changed JButtons for GLIButtons, which know whether they should paint their background depending on what platform they are run on, and finished keyboard shortcuts

  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 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;
44import org.greenstone.gatherer.gui.GLIButton;
45
46/** Provides a prompt allowing the user some choice in whether a collection saves. */
47public class SaveCollectionBox
48 extends JDialog
49 implements ActionListener{
50 /** What option the user has choosen. */
51 private int result = 0;
52 /** Button to cancel prompt, no save. */
53 private JButton cancel = null;
54 /** Button for no save. */
55 private JButton no = null;
56 /** Button to save. */
57 private JButton yes = null;
58 /** A reference to ourselves so our inner classes can dispose of us. */
59 private SaveCollectionBox myself = null;
60 /** Result value if the user has choosen cancel. */
61 static final public int SAVE_CANCEL = 0;
62 /** Result value if the user has choosen no. */
63 static final public int SAVE_NO = 1;
64 /** Result value if the user has choosen yes. */
65 static final public int SAVE_YES = 2;
66 /** Construtor. */
67 public SaveCollectionBox() {
68 super(Gatherer.g_man);
69 this.myself = this;
70 result = SAVE_CANCEL;
71 // Dialog setup
72 this.setModal(true);
73 this.setSize(360,100);
74 Dictionary.setText(this, "SaveCollectionBox.Title");
75 }
76
77 /** 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. */
78 public void actionPerformed(ActionEvent event) {
79 if(event.getSource() == yes) {
80 result = SAVE_YES;
81 this.dispose();
82 } else if(event.getSource() == no) {
83 result = SAVE_NO;
84 this.dispose();
85 } else if(event.getSource() == cancel) {
86 result = SAVE_CANCEL;
87 this.dispose();
88 }
89 }
90
91 /** Destructor. */
92 public void destroy() {
93 cancel = null;
94 no.removeActionListener(this);
95 no = null;
96 yes.removeActionListener(this);
97 yes = null;
98 myself = null;
99 rootPane = null;
100 }
101
102 /** Displays the prompt by first building the controls, then waits for a user selection.
103 * @param name The name of the current collection as a <strong>String</strong>.
104 */
105 public int getUserOption(String name) {
106 JPanel content_pane = (JPanel) this.getContentPane();
107 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
108 content_pane.setLayout(new GridLayout(2,1));
109
110 String args[] = new String[1];
111 args[0] = name;
112 JLabel save_label = new JLabel();
113 Dictionary.setText(save_label, "SaveCollectionBox.Label", args);
114 content_pane.add(save_label);
115 JPanel button_pane = new JPanel(new GridLayout(1,3));
116 content_pane.add(button_pane);
117 // 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.
118 yes = new GLIButton();
119 yes.addActionListener(this);
120 KeyListenerImpl key_listener = new KeyListenerImpl();
121 yes.addKeyListener(key_listener);
122 yes.setMnemonic(KeyEvent.VK_Y);
123 Dictionary.setBoth(yes, "General.Yes", "General.Yes_Tooltip");
124 button_pane.add(yes);
125 no = new GLIButton();
126 no.addActionListener(this);
127 no.addKeyListener(key_listener);
128 no.setMnemonic(KeyEvent.VK_N);
129 Dictionary.setBoth(no, "General.No", "General.No_Tooltip");
130 button_pane.add(no);
131 cancel = new GLIButton();
132 cancel.addActionListener(this);
133 cancel.addKeyListener(key_listener);
134 cancel.setMnemonic(KeyEvent.VK_C);
135 Dictionary.setBoth(cancel, "General.Cancel", "General.Cancel_Tooltip");
136 button_pane.add(cancel);
137
138 // Center on screen.
139 Dimension dlgSize = getSize();
140 Dimension frmSize = Gatherer.g_man.getSize();
141 Point loc = Gatherer.g_man.getLocation();
142 setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
143 show();
144
145 // Deallocate things we allocated here.
146 yes.removeKeyListener(key_listener);
147 no.removeKeyListener(key_listener);
148 cancel.removeKeyListener(key_listener);
149 key_listener = null;
150 content_pane = null;
151 args = null;
152 save_label = null;
153 button_pane = null;
154 dlgSize = null;
155 frmSize = null;
156 loc = null;
157 return result;
158 }
159
160 /** 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. */
161 private class KeyListenerImpl
162 extends KeyAdapter {
163 /** 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).
164 * @param event A <strong>KeyEvent</strong> containing details about the key release event.
165 */
166 public void keyReleased(KeyEvent event) {
167 if(event.getKeyCode() == KeyEvent.VK_Y) {
168 yes.doClick(10);
169 }
170 else if(event.getKeyCode() == KeyEvent.VK_N) {
171 no.doClick(10);
172 }
173 else if(event.getKeyCode() == KeyEvent.VK_C) {
174 cancel.doClick(10);
175 }
176 }
177 }
178}
Note: See TracBrowser for help on using the repository browser.