source: trunk/gli/src/org/greenstone/gatherer/msm/ExportMDSPrompt.java@ 4399

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

Fixed tabbing.

  • Property svn:keywords set to Author Date Id Revision
File size: 10.2 KB
Line 
1package org.greenstone.gatherer.msm;
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 javax.swing.*;
42import javax.swing.filechooser.*;
43import org.greenstone.gatherer.Gatherer;
44import org.greenstone.gatherer.msm.MetadataSet;
45import org.greenstone.gatherer.msm.MetadataSetManager;
46import org.greenstone.gatherer.util.Utility;
47/** A GUI component for allowing the user to export a metadata set, based on certain conditions, and to a certain file (not necessarily a .mds file).
48 * @author John Thompson, Greenstone Digital Library, University of Waikato
49 * @version 2.3
50 */
51final public class ExportMDSPrompt
52 extends JDialog
53 implements ActionListener, KeyListener {
54 /** Is this an export prompt or an import one? */
55 private boolean export;
56 /** The file we wish to export the metadata set to. */
57 private File file = null;
58 /** The action the user has chosen from the dialog (either -1, if cancelled, or EXPORT). */
59 private int action = -1;
60 /** The button used to browse the local file system. */
61 private JButton browse_button = null;
62 /** Used to cancel the dialog. */
63 private JButton cancel_button = null;
64 /** Used to initiate the export, then dispose of the dialog. */
65 private JButton export_button = null;
66 /** The metadata sets available for export. */
67 private JComboBox sets = null;
68 /** The destination file name as a string. */
69 private JTextField file_name = null;
70 /** Ths button that, if selected, signifies you wish to export the metadata set with all values. */
71 private JToggleButton all_values = null;
72 /** The button that, if selected, signifies you wish to export the metadata set without any values (ie no mdv files). */
73 private JToggleButton no_values = null;
74 /** The button that, if selected, signifies you wish to export the metadata set with only those values that are subject nodes in the hierarchy. */
75 private JToggleButton structure_only = null;
76 /** A reference to the metadata set manager. */
77 private MetadataSetManager manager;
78 /** The default size for this dialog window. */
79 final static public Dimension EXPORT_SIZE = new Dimension(500,220);
80 /** The default size for this dialog window. */
81 final static public Dimension IMPORT_SIZE = new Dimension(500,120);
82 /** The default export action (there are several depending on how much information you wish to export). */
83 final static public int EXPORT = 0;
84 /** Constructor.
85 * @param manager A reference to the <strong>MetadataSetManager</strong>.
86 * @see org.greenstone.gatherer.Configuration
87 * @see org.greenstone.gatherer.util.Utility
88 */
89 public ExportMDSPrompt(MetadataSetManager manager, boolean export) {
90 super();
91 this.export = export;
92 // Creation
93 setModal(true);
94 if(export) {
95 setSize(EXPORT_SIZE);
96 setTitle(get("Export_Title"));
97 }
98 else {
99 setSize(IMPORT_SIZE);
100 setTitle(get("Import_Title"));
101 }
102 JPanel content_pane = (JPanel) getContentPane();
103 JPanel control_pane = new JPanel();
104 JLabel set_label = new JLabel(get("Export_Set"));
105 sets = new JComboBox(manager.getSets());
106 JLabel condition_label = new JLabel(get("Export_Conditions"));
107 JPanel condition_pane = new JPanel();
108 ButtonGroup condition_group = new ButtonGroup();
109 all_values = new JToggleButton(get("Export_All_Values"));
110 condition_group.add(all_values);
111 no_values = new JToggleButton(get("Export_No_Values"));
112 condition_group.add(no_values);
113 structure_only = new JToggleButton(get("Export_Subjects_Only"));
114 condition_group.add(structure_only);
115 all_values.setSelected(true);
116 JLabel file_label = new JLabel(get("Export_File"));
117 JPanel file_pane = new JPanel();
118 file_name = new JTextField(Utility.METADATA_DIR);
119 browse_button = new JButton(get("General.Browse"));
120 JPanel button_pane = new JPanel();
121 if(export) {
122 export_button = new JButton(get("File_Export"));
123 export_button.setEnabled(false);
124 }
125 else {
126 export_button = new JButton(get("File_Import"));
127 export_button.setEnabled(true);
128 }
129 cancel_button = new JButton(get("General.Cancel"));
130 // Listeners
131 browse_button.addActionListener(this);
132 cancel_button.addActionListener(this);
133 export_button.addActionListener(this);
134 file_name.addKeyListener(this);
135 // Layout
136 condition_pane.setLayout(new GridLayout(1,3));
137 condition_pane.add(all_values);
138 condition_pane.add(structure_only);
139 condition_pane.add(no_values);
140
141 file_pane.setLayout(new BorderLayout());
142 file_pane.add(file_name, BorderLayout.CENTER);
143 file_pane.add(browse_button, BorderLayout.EAST);
144
145 if(export) {
146 control_pane.setLayout(new GridLayout(6, 1));
147 control_pane.add(set_label);
148 control_pane.add(sets);
149 control_pane.add(condition_label);
150 control_pane.add(condition_pane);
151 control_pane.add(file_label);
152 control_pane.add(file_pane);
153 }
154 else {
155 control_pane.setLayout(new GridLayout(2,1));
156 control_pane.add(condition_label);
157 control_pane.add(condition_pane);
158 }
159
160 button_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
161 button_pane.setLayout(new GridLayout(1,2));
162 button_pane.add(export_button);
163 button_pane.add(cancel_button);
164
165 content_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
166 content_pane.setLayout(new BorderLayout());
167 content_pane.add(control_pane, BorderLayout.CENTER);
168 content_pane.add(button_pane, BorderLayout.SOUTH);
169 // Display
170 Dimension screen_size = Gatherer.config.screen_size;
171 setLocation((screen_size.width - getSize().width) / 2, (screen_size.height - getSize().height) / 2);
172 }
173 /** Whenever one of the buttons in the dialog is actioned this method is called to trigger the appropriate effects.
174 * @param event An <strong>ActionEvent</strong> containing information about the action performed.
175 * @see org.greenstone.gatherer.Gatherer
176 * @see org.greenstone.gatherer.msm.MDSFileFilter
177 * @see org.greenstone.gatherer.util.Utility
178 */
179 public void actionPerformed(ActionEvent event) {
180 if(event.getSource() == export_button) {
181 action = EXPORT;
182 dispose();
183 }
184 else if(event.getSource() == cancel_button) {
185 action = -1;
186 dispose();
187 }
188 else {
189 JFileChooser chooser = new JFileChooser(new File(Utility.METADATA_DIR));
190 javax.swing.filechooser.FileFilter filter = new MDSFileFilter();
191 chooser.setApproveButtonText(get("General.OK"));
192 chooser.setFileFilter(filter);
193 int returnVal = chooser.showSaveDialog(Gatherer.g_man);
194 if(returnVal == JFileChooser.APPROVE_OPTION) {
195 file = chooser.getSelectedFile();
196 if(file.getName().indexOf(".") == -1) {
197 file = new File(file.getName() + ".mds");
198 }
199 file_name.setText(file.getAbsolutePath());
200 export_button.setEnabled(true);
201 }
202 }
203 }
204 /** Show the prompt and get the user input.
205 * @return An <i>int</i> specifying what action the user has choosen.
206 */
207 public int display() {
208 show();
209 return action;
210 }
211 /** Get the current value of condition.
212 * @return The value as an <i>int</i>.
213 * @see org.greenstone.gatherer.msm.MetadataSet
214 */
215 public int getSelectedCondition() {
216 if(all_values.isSelected()) {
217 return MetadataSet.ALL_VALUES;
218 }
219 else if(no_values.isSelected()) {
220 return MetadataSet.NO_VALUES;
221 }
222 else {
223 return MetadataSet.SUBJECTS_ONLY;
224 }
225 }
226 /** Get the current value of file.
227 * @return The value as a <strong>File</strong>.
228 */
229 public File getSelectedFile() {
230 return file;
231 }
232 /** Get the current value of set.
233 * @return The value as a <strong>MetadataSet</strong>.
234 */
235 public MetadataSet getSelectedSet() {
236 return (MetadataSet) sets.getSelectedItem();
237 }
238 /** Any implementation of KeyListener must include this method so that we can be informed when a key has been pressed. In this case we ignore it.
239 * @param event A <strong>KeyEvent</strong> containing information about the key pressed.
240 */
241 public void keyPressed(KeyEvent event) {
242 }
243 /** Any implementation of KeyListener must include this method so that we can be informed once a key has been released. This is the earliest the VK code becomes stable and usable, so we will check if the file named in file_name can be written to and if so enable the export button.
244 * @param event A <strong>KeyEvent</strong> containing information about the key typed.
245 */
246 public void keyReleased(KeyEvent event) {
247 String pos_file = file_name.getText();
248 if(pos_file.indexOf(".") != -1) {
249 file = new File(file_name.getText());
250 if(file.canWrite() || !file.exists()) {
251 export_button.setEnabled(true);
252 }
253 else if(export) {
254 export_button.setEnabled(false);
255 }
256 }
257 }
258 /** Any implementation of KeyListener must include this method so that we can be informed when a key has been typed. In this case we ignore it.
259 * @param event A <strong>KeyEvent</strong> containing information about the key typed.
260 */
261 public void keyTyped(KeyEvent event) {
262 }
263
264 private String get(String key) {
265 return get(key, null);
266 }
267 private String get(String key, String[] args) {
268 if(key.indexOf(".") == -1) {
269 key = "MSMPrompt." + key;
270 }
271 return Gatherer.dictionary.get(key, args);
272 }
273}
Note: See TracBrowser for help on using the repository browser.