source: trunk/gli/src/org/greenstone/gatherer/gui/FileAssociationDialog.java@ 9101

Last change on this file since 9101 was 8243, checked in by mdewsnip, 20 years ago

Removed all occurrences of classes explicitly importing other classes in the same package.

  • Property svn:keywords set to Author Date Id Revision
File size: 17.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.gui;
38
39import java.awt.*;
40import java.awt.event.*;
41import java.io.File;
42import java.util.*;
43import javax.swing.*;
44import javax.swing.event.*;
45import javax.swing.filechooser.*;
46import javax.swing.table.*;
47import org.greenstone.gatherer.Configuration;
48import org.greenstone.gatherer.Dictionary;
49import org.greenstone.gatherer.Gatherer;
50import org.greenstone.gatherer.file.FileAssociationManager;
51import org.greenstone.gatherer.util.TableUtils;
52import org.greenstone.gatherer.util.Utility;
53
54/** The file association allows the entry of new file associations and the modification of existing ones.
55 * @author John Thompson, Greenstone Digital Library, University of Waikato
56 * @version 2.3
57 */
58public class FileAssociationDialog
59 extends ModalDialog {
60
61 /** The default size for the dialog. */
62 static final private Dimension SIZE = new Dimension(600, 450);
63
64 private boolean ignore = false;
65 private FileAssociationDialog self;
66 private FileAssociationManager file_association_manager;
67 private JButton add_button;
68 private JButton browse_button;
69 private JButton close_button;
70 private JButton remove_button;
71 private JButton replace_button;
72 private JTable existing_associations_table;
73 private JTextField command_field;
74 private JTextField extension_field;
75
76 /** Create a new file association dialog.
77 * @param file_association_manager A reference to the <strong>FileAssociationManager</strong> so we can determine what extensions are already associated.
78 */
79 public FileAssociationDialog(FileAssociationManager file_association_manager) {
80 super(Gatherer.g_man);
81 this.file_association_manager = file_association_manager;
82 this.self = this;
83
84 // Creation
85 setModal(true);
86 setSize(SIZE);
87 setJMenuBar(new SimpleMenuBar("fileassociations"));
88
89 JPanel content_pane = (JPanel) getContentPane();
90 content_pane.setBackground(Configuration.getColor("coloring.collection_heading_background", false));
91
92 JTextArea instructions_area = new JTextArea();
93 instructions_area.setEditable(false);
94 instructions_area.setLineWrap(true);
95 instructions_area.setRows(5);
96 instructions_area.setWrapStyleWord(true);
97 Dictionary.setText(instructions_area, "FileAssociationDialog.Instructions");
98
99 JPanel control_pane = new JPanel();
100 JLabel existing_associations_label = new JLabel();
101 existing_associations_table = new JTable(file_association_manager);
102 existing_associations_table.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
103 existing_associations_table.setColumnSelectionAllowed(false);
104 existing_associations_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
105 JPanel lower_pane = new JPanel();
106 JPanel details_pane = new JPanel();
107
108 JPanel extension_pane = new JPanel();
109 JLabel extension_label = new JLabel();
110 extension_field = new NonWhitespaceField();
111
112 JLabel command_label = new JLabel();
113 JPanel command_pane = new JPanel();
114 command_field = new JTextField();
115 browse_button = new GLIButton();
116 browse_button.setEnabled(!Utility.isMac());
117 browse_button.setMnemonic(KeyEvent.VK_B);
118
119 JPanel button_pane = new JPanel();
120
121 add_button = new GLIButton();
122 add_button.setEnabled(false);
123 add_button.setMnemonic(KeyEvent.VK_A);
124
125 replace_button = new GLIButton();
126 replace_button.setEnabled(false);
127 replace_button.setMnemonic(KeyEvent.VK_P);
128
129 remove_button = new GLIButton();
130 remove_button.setEnabled(false);
131 remove_button.setMnemonic(KeyEvent.VK_R);
132
133 close_button = new GLIButton();
134 close_button.setMnemonic(KeyEvent.VK_C);
135
136 // Connection
137 add_button.addActionListener(new AddButtonListener());
138 browse_button.addActionListener(new BrowseButtonListener());
139 close_button.addActionListener(new CloseButtonListener());
140 remove_button.addActionListener(new RemoveButtonListener());
141 replace_button.addActionListener(new ReplaceButtonListener());
142 CommandOrExtensionFieldListener coefl = new CommandOrExtensionFieldListener();
143 command_field.getDocument().addDocumentListener(coefl);
144 extension_field.getDocument().addDocumentListener(coefl);
145 coefl = null;
146 existing_associations_table.getSelectionModel().addListSelectionListener(new ExistingAssociationsTableListener());
147 Dictionary.setBoth(add_button, "FileAssociationDialog.Add", "FileAssociationDialog.Add_Tooltip");
148 if(Utility.isMac()) {
149 Dictionary.setBoth(browse_button, "FileAssociationDialog.Browse", "FileAssociationDialog.Browse_Tooltip_Mac");
150 }
151 else {
152 Dictionary.setBoth(browse_button, "FileAssociationDialog.Browse", "FileAssociationDialog.Browse_Tooltip");
153 }
154 Dictionary.setBoth(close_button, "FileAssociationDialog.Close", "FileAssociationDialog.Close_Tooltip");
155 Dictionary.setBoth(remove_button, "FileAssociationDialog.Remove", "FileAssociationDialog.Remove_Tooltip");
156 Dictionary.setBoth(replace_button, "FileAssociationDialog.Replace", "FileAssociationDialog.Replace_Tooltip");
157 Dictionary.setText(command_label, "FileAssociationDialog.Command");
158 Dictionary.setText(extension_label, "FileAssociationDialog.Extension");
159 Dictionary.setText(existing_associations_label, "FileAssociationDialog.Existing_Associations");
160 Dictionary.setText(this, "FileAssociationDialog.Title");
161
162 // Layout
163 extension_pane.setBorder(BorderFactory.createEmptyBorder(2,0,2,0));
164 extension_pane.setLayout(new BorderLayout(5,0));
165 extension_pane.add(extension_label, BorderLayout.WEST);
166 extension_pane.add(extension_field, BorderLayout.CENTER);
167
168 command_pane.setBorder(BorderFactory.createEmptyBorder(2,0,2,0));
169 command_pane.setLayout(new BorderLayout());
170 command_pane.add(command_field, BorderLayout.CENTER);
171 command_pane.add(browse_button, BorderLayout.EAST);
172
173 details_pane.setBorder(BorderFactory.createTitledBorder(Dictionary.get("FileAssociationDialog.Details")));
174 details_pane.setLayout(new GridLayout(3,1));
175 details_pane.add(extension_pane);
176 details_pane.add(command_label);
177 details_pane.add(command_pane);
178
179 lower_pane.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));
180 button_pane.setLayout(new GridLayout(2,3));
181 button_pane.add(add_button);
182 button_pane.add(replace_button);
183 button_pane.add(remove_button);
184 button_pane.add(new JPanel());
185 button_pane.add(new JPanel());
186 button_pane.add(close_button);
187
188 lower_pane.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));
189 lower_pane.setLayout(new BorderLayout());
190 lower_pane.add(details_pane, BorderLayout.CENTER);
191 lower_pane.add(button_pane, BorderLayout.SOUTH);
192
193 control_pane.setBorder(BorderFactory.createEmptyBorder(2,0,0,0));
194 control_pane.setLayout(new BorderLayout());
195 control_pane.add(existing_associations_label, BorderLayout.NORTH);
196 control_pane.add(new JScrollPane(existing_associations_table), BorderLayout.CENTER);
197 control_pane.add(lower_pane, BorderLayout.SOUTH);
198
199 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
200 content_pane.setLayout(new BorderLayout());
201 content_pane.add(new JScrollPane(instructions_area), BorderLayout.NORTH);
202 content_pane.add(control_pane, BorderLayout.CENTER);
203
204 Rectangle screen = Gatherer.g_man.getBounds(null);
205 setLocation((int)(screen.getX() + (screen.getWidth() - SIZE.width) / 2), (int)(screen.getY() + (screen.getHeight() - SIZE.height) / 2));
206 screen = null;
207 }
208
209 public void destroy() {
210 // Disconnect
211 // Clean up
212 file_association_manager = null;
213 self = null;
214 }
215
216 /** Redisplay the dialog, ensuring that the details (or lack thereof) for a certain extension is apparent. In fact if an extension is provided force the user to add it or cancel.
217 * @param new_extension The extension code as a <strong>String</strong>.
218 */
219 public String display(String new_extension) {
220 // Clear the current selection
221 existing_associations_table.clearSelection();
222 if(new_extension != null) {
223 extension_field.setText(new_extension);
224 }
225 setVisible(true);
226 if(new_extension != null) {
227 return file_association_manager.getCommandString(new_extension);
228 }
229 return null;
230 }
231
232 protected void processWindowEvent(WindowEvent event) {
233 if(event.getID() == WindowEvent.WINDOW_ACTIVATED) {
234 TableUtils.fixColumnToPreferredWidth(existing_associations_table, 0);
235 }
236 else {
237 super.processWindowEvent(event);
238 }
239 }
240
241 private class AddButtonListener
242 implements ActionListener {
243
244 public void actionPerformed(ActionEvent event) {
245 String extension_str = extension_field.getText();
246 file_association_manager.setCommand(extension_str, command_field.getText());
247 // Select the appropriate entry in the table
248 int index = -1;
249 for(int i = 0; index == -1 && i < file_association_manager.size(); i++) {
250 if(file_association_manager.getExtension(i).equals(extension_str)) {
251 index = i;
252 }
253 }
254 if(index > -1) {
255 existing_associations_table.setRowSelectionInterval(index, index);
256 }
257 // And update buttons
258 add_button.setEnabled(false);
259 remove_button.setEnabled(true);
260 }
261 }
262
263 /** Batch filter shows only files ending in bat. Based on ImageFilter.java which is a 1.4 example used by FileChooserDemo2.java. */
264 private class BatchFileFilter
265 extends FileFilter {
266
267 /** Accept all .exe files
268 * @param file a File
269 * @return true is this file should be shown, false otherwise
270 */
271 public boolean accept(File file) {
272 return file.getName().toLowerCase().endsWith(".bat");
273 }
274
275 /** The description of this filter
276 * @return a String
277 */
278 public String getDescription() {
279 return Dictionary.get("FileAssociationDialog.Batch_File");
280 }
281 }
282
283 /** 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. */
284 private class BrowseButtonListener
285 implements ActionListener {
286 /** Open up a simple JFileChooser when the user clicks the button.
287 * @param event An <strong>ActionEvent</strong> containing information about the event.
288 */
289 public void actionPerformed(ActionEvent event) {
290 JFileChooser chooser = new JFileChooser(new File(Utility.BASE_DIR));
291 OpenCollectionDialog.disableRename(chooser);
292 chooser.setDialogTitle(Dictionary.get("FileAssociationDialog.Browse_Title"));
293 chooser.setFileFilter(new BatchFileFilter());
294 chooser.setFileFilter(new CoreObjectModelFileFilter());
295 chooser.setFileFilter(new ExecutableFileFilter());
296 chooser.setAcceptAllFileFilterUsed(true);
297 if(chooser.showOpenDialog(Gatherer.g_man) == JFileChooser.APPROVE_OPTION) {
298 command_field.setText(chooser.getSelectedFile().getAbsolutePath());
299 }
300 }
301 }
302
303 private class CloseButtonListener
304 implements ActionListener {
305 public void actionPerformed(ActionEvent event) {
306 existing_associations_table.clearSelection();
307 add_button.setEnabled(false);
308 remove_button.setEnabled(false);
309 replace_button.setEnabled(false);
310 self.dispose();
311 }
312 }
313
314 /** Command filter shows only files ending in com. Based on ImageFilter.java which is a 1.4 example used by FileChooserDemo2.java. */
315 private class CoreObjectModelFileFilter
316 extends FileFilter {
317
318 /** Accept all .exe files
319 * @param file a File
320 * @return true is this file should be shown, false otherwise
321 */
322 public boolean accept(File file) {
323 return file.getName().toLowerCase().endsWith(".com");
324 }
325
326 /** The description of this filter
327 * @return a String
328 */
329 public String getDescription() {
330 return Dictionary.get("FileAssociationDialog.Command_File");
331 }
332 }
333
334 private class CommandOrExtensionFieldListener
335 implements DocumentListener {
336 /** Gives notification that an attribute or set of attributes changed. */
337 public void changedUpdate(DocumentEvent e) {
338 changed();
339 }
340 /** Gives notification that there was an insert into the document. */
341 public void insertUpdate(DocumentEvent e) {
342 changed();
343 }
344 /** Gives notification that a portion of the document has been removed. */
345 public void removeUpdate(DocumentEvent e) {
346 changed();
347 }
348
349 private void changed() {
350 ignore = true;
351 String extension_str = extension_field.getText();
352 String command_str = command_field.getText();
353 // Determine if there is currently an association selected
354 int selected_index = -1;
355 if((selected_index = existing_associations_table.getSelectionModel().getMinSelectionIndex()) != -1) {
356 String current_extension_str = file_association_manager.getExtension(selected_index);
357 String current_command_str = file_association_manager.getCommandString(current_extension_str);
358 // What buttons are enabled depends a lot on the file extension, taken to be the 'key'
359 if(extension_str.equals(current_extension_str)) {
360 // If the extensions are the same then remove is enabled
361 remove_button.setEnabled(true);
362 // But if the command is different, then enable replace
363 if(current_command_str.length() == 0) {
364 add_button.setEnabled(true);
365 replace_button.setEnabled(false);
366 }
367 else if(!command_str.equals(current_command_str)) {
368 add_button.setEnabled(false);
369 replace_button.setEnabled(true);
370 }
371 else {
372 add_button.setEnabled(false);
373 replace_button.setEnabled(false);
374 }
375 }
376 // We can add it, but we first clear the selection
377 else {
378 existing_associations_table.clearSelection();
379 add_button.setEnabled(true);
380 remove_button.setEnabled(false);
381 replace_button.setEnabled(false);
382 }
383 }
384 // If not, we must first test if the extension is in use
385 else {
386 int index = -1;
387 for(int i = 0; index == -1 && i < file_association_manager.size(); i++) {
388 String existing_extension_str = file_association_manager.getExtension(i);
389 if(existing_extension_str.equals(extension_str)) {
390 index = i;
391 }
392 }
393 if(index != -1) {
394 // Selection that index
395 existing_associations_table.setRowSelectionInterval(index, index);
396 // Set replace and remove enabled
397 add_button.setEnabled(false);
398 remove_button.setEnabled(true);
399 replace_button.setEnabled(true);
400 }
401 // Otherwise if there is some text in both extension and command, enable add only
402 else if(extension_str.length() > 0 && command_str.length() > 0) {
403 add_button.setEnabled(true);
404 remove_button.setEnabled(false);
405 replace_button.setEnabled(false);
406 }
407 // Otherwise disable everything
408 else {
409 add_button.setEnabled(false);
410 remove_button.setEnabled(false);
411 replace_button.setEnabled(false);
412 }
413 }
414 ignore = false;
415 }
416 }
417
418 /** Executable filter shows only files ending in exe. Based on ImageFilter.java which is a 1.4 example used by FileChooserDemo2.java. */
419 private class ExecutableFileFilter
420 extends FileFilter {
421
422 /** Accept all .exe files
423 * @param file a File
424 * @return true is this file should be shown, false otherwise
425 */
426 public boolean accept(File file) {
427 return file.getName().toLowerCase().endsWith(".exe");
428 }
429
430 /** The description of this filter
431 * @return a String
432 */
433 public String getDescription() {
434 return Dictionary.get("FileAssociationDialog.Executable_File");
435 }
436 }
437
438 private class ExistingAssociationsTableListener
439 implements ListSelectionListener {
440
441 public void valueChanged(ListSelectionEvent event) {
442 if(!event.getValueIsAdjusting() && !ignore) {
443 int selected_index = -1;
444 if((selected_index = existing_associations_table.getSelectionModel().getMinSelectionIndex()) != -1) {
445 // Propagate the details of the selection down into the fields
446 String extension_str = file_association_manager.getExtension(selected_index);
447 extension_field.setText(extension_str);
448 command_field.setText(file_association_manager.getCommandString(extension_str));
449 extension_str = null;
450 remove_button.setEnabled(true);
451 }
452 else {
453 // Clear the fields
454 extension_field.setText("");
455 command_field.setText("");
456 remove_button.setEnabled(false);
457 }
458 add_button.setEnabled(false);
459 replace_button.setEnabled(false);
460 }
461 }
462 }
463
464 private class RemoveButtonListener
465 implements ActionListener {
466 public void actionPerformed(ActionEvent event) {
467 file_association_manager.setCommand(extension_field.getText(), null);
468 // And update buttons
469 add_button.setEnabled(true);
470 remove_button.setEnabled(false);
471 }
472 }
473
474 private class ReplaceButtonListener
475 implements ActionListener {
476 public void actionPerformed(ActionEvent event) {
477 file_association_manager.setCommand(extension_field.getText(), command_field.getText());
478 // And update buttons
479 replace_button.setEnabled(false);
480 }
481 }
482}
483
484
485
486
487
488
Note: See TracBrowser for help on using the repository browser.