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

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

Changed calls to the Dictionary.

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