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