1 | package org.greenstone.gatherer.gui;
|
---|
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 java.io.*;
|
---|
41 | import java.util.*;
|
---|
42 | import javax.swing.*;
|
---|
43 | import org.greenstone.gatherer.Gatherer;
|
---|
44 | import org.greenstone.gatherer.file.FileAssociationManager;
|
---|
45 | import org.greenstone.gatherer.gui.GComboBox;
|
---|
46 | import org.greenstone.gatherer.util.Utility;
|
---|
47 | import org.outerj.pollo.util.*;
|
---|
48 | import org.greenstone.gatherer.gui.SimpleMenuBar;
|
---|
49 | import org.greenstone.gatherer.gui.ModalDialog;
|
---|
50 |
|
---|
51 | /** The file association allows the entry of new file associations and the modification of existing ones. This is done via a list of known extensions, and two fields to fill out relevant details.
|
---|
52 | * @author John Thompson, Greenstone Digital Library, University of Waikato
|
---|
53 | * @version 2.3
|
---|
54 | */
|
---|
55 | public class FileAssociationDialog
|
---|
56 | extends ModalDialog {
|
---|
57 | /** <i>true</i> iff this addition action been cancelled by the user. */
|
---|
58 | private boolean cancelled = false;
|
---|
59 | /** <i>true</i> iff we should ignore any current changes to the extension list. */
|
---|
60 | private boolean ignore = false;
|
---|
61 | /** <i>true</i> iff the extension has not changed recently, but the command has. */
|
---|
62 | private boolean save_required = false;
|
---|
63 | /** A reference to ourselves so that our inner classes can dispose of us. */
|
---|
64 | private FileAssociationDialog self;
|
---|
65 | /** A reference to the manager in charge of storing all the file association information. */
|
---|
66 | private FileAssociationManager manager;
|
---|
67 | /** The button used to open a file browser for simplier executable selection. */
|
---|
68 | private JButton browse;
|
---|
69 | /** The button used to cancel this dialog without commiting any changes. */
|
---|
70 | private JButton cancel;
|
---|
71 | /** The button used to hide this dialog after updating or adding file associations as necessary. */
|
---|
72 | private JButton ok;
|
---|
73 | /** The combobox used to select what extension you wish to change. */
|
---|
74 | private GComboBox extension;
|
---|
75 | /** A field for entering the command string. */
|
---|
76 | private JTextField command;
|
---|
77 | /** The extension last chosen from the combobox. This way we can determine if the extension has been modified by the user. */
|
---|
78 | private String previous_extension;
|
---|
79 | /** The default size of a label. */
|
---|
80 | static final private Dimension LABEL_SIZE = new Dimension(150, 25);
|
---|
81 | /** The default size for the dialog. */
|
---|
82 | static final private Dimension SIZE = new Dimension(600, 175);
|
---|
83 | /** Create a new file association dialog.
|
---|
84 | * @param manager A reference to the <strong>FileAssociationManager</strong> so we can determine what extensions are already associated.
|
---|
85 | * @see org.greenstone.gatherer.Configuration
|
---|
86 | * @see org.greenstone.gatherer.gui.Coloring
|
---|
87 | */
|
---|
88 | public FileAssociationDialog(FileAssociationManager manager) {
|
---|
89 | super(Gatherer.g_man);
|
---|
90 | this.manager = manager;
|
---|
91 | this.self = this;
|
---|
92 |
|
---|
93 | // Creation
|
---|
94 | setModal(true);
|
---|
95 | setSize(SIZE);
|
---|
96 | setTitle(get("Title"));
|
---|
97 | setJMenuBar(new SimpleMenuBar("0"));// need to find an appropriate help page
|
---|
98 | JPanel content_pane = (JPanel) getContentPane();
|
---|
99 | content_pane.setBackground(Gatherer.config.getColor("coloring.collection_heading_background", false));
|
---|
100 | JPanel extension_pane = new JPanel();
|
---|
101 | extension_pane.setOpaque(false);
|
---|
102 | JLabel extension_label = new JLabel(get("Extension"));
|
---|
103 | extension_label.setPreferredSize(LABEL_SIZE);
|
---|
104 | extension = new GComboBox();
|
---|
105 | extension.setEditable(true);
|
---|
106 | extension.setPreferredSize(LABEL_SIZE);
|
---|
107 | for(Iterator extensions = manager.keySet().iterator(); extensions.hasNext(); ) {
|
---|
108 | extension.add((String)extensions.next());
|
---|
109 | }
|
---|
110 | JPanel command_pane = new JPanel();
|
---|
111 | command_pane.setOpaque(false);
|
---|
112 | JLabel command_label = new JLabel(get("Command"));
|
---|
113 | command_label.setPreferredSize(LABEL_SIZE);
|
---|
114 | JPanel inner_pane = new JPanel();
|
---|
115 | inner_pane.setOpaque(false);
|
---|
116 | command = new JTextField();
|
---|
117 | command.setBackground(Gatherer.config.getColor("coloring.collection_tree_background", false));
|
---|
118 | command.setSelectionColor(Gatherer.config.getColor("coloring.collection_selection_background", false));
|
---|
119 | command.setSelectedTextColor(Gatherer.config.getColor("coloring.collection_selection_foreground", false));
|
---|
120 | if(extension.count() > 0) {
|
---|
121 | command.setText(manager.getCommandImmediately((String)extension.get(0)));
|
---|
122 | }
|
---|
123 | browse = new JButton(get("Browse"));
|
---|
124 | browse.setBackground(Gatherer.config.getColor("coloring.button_background", false));
|
---|
125 | browse.setPreferredSize(LABEL_SIZE);
|
---|
126 | JPanel button_pane = new JPanel();
|
---|
127 | button_pane.setOpaque(false);
|
---|
128 | ok = new JButton(get("General.OK"));
|
---|
129 | ok.setBackground(Gatherer.config.getColor("coloring.button_background", false));
|
---|
130 | cancel = new JButton(get("General.Cancel"));
|
---|
131 | cancel.setBackground(Gatherer.config.getColor("coloring.button_background", false));
|
---|
132 | // Connection
|
---|
133 | browse.addActionListener(new BrowseListener());
|
---|
134 | cancel.addActionListener(new CancelListener());
|
---|
135 | ok.addActionListener(new OKListener());
|
---|
136 | extension.addItemListener(new ExtensionListener());
|
---|
137 | command.addKeyListener(new CommandListener());
|
---|
138 | // Layout
|
---|
139 | extension_pane.setLayout(new BorderLayout());
|
---|
140 | extension_pane.add(extension_label, BorderLayout.WEST);
|
---|
141 | extension_pane.add(extension, BorderLayout.EAST);
|
---|
142 |
|
---|
143 | inner_pane.setLayout(new BorderLayout());
|
---|
144 | inner_pane.add(command, BorderLayout.CENTER);
|
---|
145 | inner_pane.add(browse, BorderLayout.EAST);
|
---|
146 |
|
---|
147 | command_pane.setBorder(BorderFactory.createEmptyBorder(5,0,5,0));
|
---|
148 | command_pane.setLayout(new GridLayout(2,1));
|
---|
149 | command_pane.add(command_label);
|
---|
150 | command_pane.add(inner_pane);
|
---|
151 |
|
---|
152 | button_pane.setLayout(new GridLayout(1,2,2,2));
|
---|
153 | button_pane.add(ok);
|
---|
154 | button_pane.add(cancel);
|
---|
155 |
|
---|
156 | content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
157 | content_pane.setLayout(new BorderLayout());
|
---|
158 | content_pane.add(extension_pane, BorderLayout.NORTH);
|
---|
159 | content_pane.add(command_pane, BorderLayout.CENTER);
|
---|
160 | content_pane.add(button_pane, BorderLayout.SOUTH);
|
---|
161 |
|
---|
162 | Dimension screen_size = Gatherer.config.screen_size;
|
---|
163 | setLocation((screen_size.width - SIZE.width) / 2, (screen_size.height - SIZE.height) / 2);
|
---|
164 | }
|
---|
165 | /** Destructor to ensure dialog gets garbage collected. */
|
---|
166 | public void destroy() {
|
---|
167 | self = null;
|
---|
168 | manager = null;
|
---|
169 | browse = null;
|
---|
170 | cancel = null;
|
---|
171 | ok = null;
|
---|
172 | extension = null;
|
---|
173 | command = null;
|
---|
174 | previous_extension = null;
|
---|
175 | }
|
---|
176 | /** Redisplay the dialog, ensuring that the details (or lack thereof) for a certain extension is apparent.
|
---|
177 | * @param new_extension The extension code as a <strong>String</strong>.
|
---|
178 | */
|
---|
179 | public String display(String new_extension) {
|
---|
180 | if(new_extension != null) {
|
---|
181 | int index = extension.add(new_extension);
|
---|
182 | extension.setSelectedIndex(index);
|
---|
183 | }
|
---|
184 | else {
|
---|
185 |
|
---|
186 | }
|
---|
187 | setVisible(true);
|
---|
188 | if(save_required) {
|
---|
189 | manager.setCommand((String)extension.getSelectedItem(), (String)command.getText());
|
---|
190 | save_required = false;
|
---|
191 | }
|
---|
192 | if(cancelled) {
|
---|
193 | return null;
|
---|
194 | }
|
---|
195 | return command.getText();
|
---|
196 | }
|
---|
197 |
|
---|
198 | private String get(String key) {
|
---|
199 | return get(key, null);
|
---|
200 | }
|
---|
201 |
|
---|
202 | private String get(String key, String args[]) {
|
---|
203 | if(key.indexOf(".") == -1) {
|
---|
204 | key = "FileAssociationDialog." + key;
|
---|
205 | }
|
---|
206 | return Gatherer.dictionary.get(key, args);
|
---|
207 | }
|
---|
208 |
|
---|
209 | /** Whenever the user clicks the browse button, we should open up a file browser to allow them to select an executable file from somewhere in the file system. */
|
---|
210 | private class BrowseListener
|
---|
211 | implements ActionListener {
|
---|
212 | /** Open up a simple JFileChooser when the user clicks the button.
|
---|
213 | * @param event An <strong>ActionEvent</strong> containing information about the event.
|
---|
214 | */
|
---|
215 | public void actionPerformed(ActionEvent event) {
|
---|
216 | JFileChooser chooser = new JFileChooser(new File(Utility.BASE_DIR));
|
---|
217 | chooser.setDialogTitle(get("Browse_Title"));
|
---|
218 | chooser.setFileFilter(new ExtensionFileFilter(".bat", get("Batch_File")));
|
---|
219 | chooser.setFileFilter(new ExtensionFileFilter(".com", get("Command_File")));
|
---|
220 | chooser.setFileFilter(new ExtensionFileFilter(".exe", get("Executable_File")));
|
---|
221 | chooser.setAcceptAllFileFilterUsed(true);
|
---|
222 | int return_val = chooser.showOpenDialog(null);
|
---|
223 | if(return_val == JFileChooser.APPROVE_OPTION) {
|
---|
224 | command.setText(chooser.getSelectedFile().getAbsolutePath());
|
---|
225 | save_required = true;
|
---|
226 | }
|
---|
227 | }
|
---|
228 | }
|
---|
229 | /** Listens for actions apon the cancel button, and if detected disposes of the dialog without saving changes. */
|
---|
230 | private class CancelListener
|
---|
231 | implements ActionListener {
|
---|
232 | /** Listens for actions apon the cancel button, and if detected disposes of the dialog without saving changes.
|
---|
233 | * @param event An <strong>ActionEvent</strong> containing further information about the action.
|
---|
234 | */
|
---|
235 | public void actionPerformed(ActionEvent event) {
|
---|
236 | cancelled = true;
|
---|
237 | save_required = false;
|
---|
238 | self.dispose();
|
---|
239 | }
|
---|
240 | }
|
---|
241 | /** If any changes occur to the command field, mark it as needing saving before we can change extensions, or dispose of this dialog (other than cancelling. */
|
---|
242 | private class CommandListener
|
---|
243 | extends KeyAdapter {
|
---|
244 | /** Whenever we detect a new character has been typed, note that we need to update this association in the manager.
|
---|
245 | * @param event A <strong>KeyEvent</strong> containing information about the key typed event.
|
---|
246 | */
|
---|
247 | public void keyTyped(KeyEvent event) {
|
---|
248 | save_required = true;
|
---|
249 | }
|
---|
250 | }
|
---|
251 | /** Whenever the user selects an extension, we should fill out the command field with whatever value has been previously entered for the command (if any). */
|
---|
252 | private class ExtensionListener
|
---|
253 | implements ItemListener {
|
---|
254 | /** This method is called whenever the selection in the extension list changes, so that we can save any old details, then load up the information for the new selection.
|
---|
255 | * @param event An <strong>ItemEvent</strong> encompassing everything you ever wanted to know about the list selectio, but were afraid to ask.
|
---|
256 | * @see org.greenstone.gatherer.file.FileAssociationManager
|
---|
257 | */
|
---|
258 | public void itemStateChanged(ItemEvent event) {
|
---|
259 | String ext = (String) extension.getSelectedItem();
|
---|
260 | if(!ignore && ext != null && ext.length() > 0) {
|
---|
261 | ignore = true;
|
---|
262 | // Save the previous extension if necessary
|
---|
263 | if(save_required && previous_extension != null) {
|
---|
264 | manager.setCommand(previous_extension, command.getText());
|
---|
265 | save_required = false;
|
---|
266 | }
|
---|
267 | previous_extension = manager.getCommandImmediately((String)extension.getSelectedItem());
|
---|
268 | if(previous_extension != null) {
|
---|
269 | command.setText(previous_extension);
|
---|
270 | }
|
---|
271 | else {
|
---|
272 | command.setText("");
|
---|
273 | }
|
---|
274 | ignore = false;
|
---|
275 | }
|
---|
276 | }
|
---|
277 | }
|
---|
278 | /** Listen for clicks on the ok button, and dispose when detected. */
|
---|
279 | private class OKListener
|
---|
280 | implements ActionListener {
|
---|
281 | /** When a click on OK is detected, save the current information, then dispose.
|
---|
282 | * @param event An <strong>ActionEvent</strong> with details about the button press.
|
---|
283 | */
|
---|
284 | public void actionPerformed(ActionEvent event) {
|
---|
285 | if(save_required) {
|
---|
286 | manager.setCommand(extension.getSelectedItem().toString(), command.getText());
|
---|
287 | save_required = false;
|
---|
288 | }
|
---|
289 | self.dispose();
|
---|
290 | }
|
---|
291 | }
|
---|
292 | }
|
---|