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

Last change on this file since 12710 was 12710, checked in by kjdon, 18 years ago

removed existing associations label

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