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

Last change on this file since 5347 was 5347, checked in by mdewsnip, 21 years ago

Changed dictionary get()s to have the whole key.

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