[29] | 1 | /* |
---|
| 2 | * This program is free software; you can redistribute it and/or modify |
---|
| 3 | * it under the terms of the GNU General Public License as published by |
---|
| 4 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 5 | * (at your option) any later version. |
---|
| 6 | * |
---|
| 7 | * This program is distributed in the hope that it will be useful, |
---|
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 10 | * GNU General Public License for more details. |
---|
| 11 | * |
---|
| 12 | * You should have received a copy of the GNU General Public License |
---|
| 13 | * along with this program; if not, write to the Free Software |
---|
| 14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | /* |
---|
| 18 | * ConverterFileChooser.java |
---|
| 19 | * Copyright (C) 2006 University of Waikato, Hamilton, New Zealand |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | package weka.gui; |
---|
| 23 | |
---|
| 24 | import weka.core.Capabilities; |
---|
| 25 | import weka.core.Instances; |
---|
| 26 | import weka.core.converters.AbstractFileLoader; |
---|
| 27 | import weka.core.converters.AbstractFileSaver; |
---|
| 28 | import weka.core.converters.AbstractLoader; |
---|
| 29 | import weka.core.converters.AbstractSaver; |
---|
| 30 | import weka.core.converters.ConverterUtils; |
---|
| 31 | import weka.core.converters.FileSourcedConverter; |
---|
| 32 | |
---|
| 33 | import java.awt.BorderLayout; |
---|
| 34 | import java.awt.Component; |
---|
| 35 | import java.awt.event.ActionEvent; |
---|
| 36 | import java.awt.event.ActionListener; |
---|
| 37 | import java.beans.PropertyChangeEvent; |
---|
| 38 | import java.beans.PropertyChangeListener; |
---|
| 39 | import java.io.File; |
---|
| 40 | import java.util.Vector; |
---|
| 41 | |
---|
| 42 | import javax.swing.BorderFactory; |
---|
| 43 | import javax.swing.JButton; |
---|
| 44 | import javax.swing.JCheckBox; |
---|
| 45 | import javax.swing.JFileChooser; |
---|
| 46 | import javax.swing.JLabel; |
---|
| 47 | import javax.swing.JOptionPane; |
---|
| 48 | import javax.swing.JPanel; |
---|
| 49 | import javax.swing.filechooser.FileFilter; |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | * A specialized JFileChooser that lists all available file Loaders and Savers. |
---|
| 53 | * To list only savers that can handle the data that is about to be saved, one |
---|
| 54 | * can set a Capabilities filter. |
---|
| 55 | * |
---|
| 56 | * @author fracpete (fracpete at waikato dot ac dot nz) |
---|
| 57 | * @version $Revision: 4915 $ |
---|
| 58 | * @see #setCapabilitiesFilter(Capabilities) |
---|
| 59 | */ |
---|
| 60 | public class ConverterFileChooser |
---|
| 61 | extends JFileChooser { |
---|
| 62 | |
---|
| 63 | /** for serialization. */ |
---|
| 64 | private static final long serialVersionUID = -5373058011025481738L; |
---|
| 65 | |
---|
| 66 | /** unhandled type of dialog. */ |
---|
| 67 | public final static int UNHANDLED_DIALOG = 0; |
---|
| 68 | |
---|
| 69 | /** the loader dialog. */ |
---|
| 70 | public final static int LOADER_DIALOG = 1; |
---|
| 71 | |
---|
| 72 | /** the saver dialog. */ |
---|
| 73 | public final static int SAVER_DIALOG = 2; |
---|
| 74 | |
---|
| 75 | /** the file chooser itself. */ |
---|
| 76 | protected ConverterFileChooser m_Self; |
---|
| 77 | |
---|
| 78 | /** the file filters for the loaders. */ |
---|
| 79 | protected static Vector<ExtensionFileFilter> m_LoaderFileFilters; |
---|
| 80 | |
---|
| 81 | /** the file filters for the savers. */ |
---|
| 82 | protected static Vector<ExtensionFileFilter> m_SaverFileFilters; |
---|
| 83 | |
---|
| 84 | /** the type of dialog to display. */ |
---|
| 85 | protected int m_DialogType; |
---|
| 86 | |
---|
| 87 | /** the converter that was chosen by the user. */ |
---|
| 88 | protected Object m_CurrentConverter; |
---|
| 89 | |
---|
| 90 | /** the configure button. */ |
---|
| 91 | protected JButton m_ConfigureButton; |
---|
| 92 | |
---|
| 93 | /** the propertychangelistener. */ |
---|
| 94 | protected PropertyChangeListener m_Listener; |
---|
| 95 | |
---|
| 96 | /** the last filter that was used for opening/saving. */ |
---|
| 97 | protected FileFilter m_LastFilter; |
---|
| 98 | |
---|
| 99 | /** the Capabilities filter for the savers. */ |
---|
| 100 | protected Capabilities m_CapabilitiesFilter; |
---|
| 101 | |
---|
| 102 | /** whether to popup a dialog in case the file already exists (only save |
---|
| 103 | * dialog). */ |
---|
| 104 | protected boolean m_OverwriteWarning = true; |
---|
| 105 | |
---|
| 106 | /** whether the file to be opened must exist (only open dialog). */ |
---|
| 107 | protected boolean m_FileMustExist = true; |
---|
| 108 | |
---|
| 109 | /** the checkbox for bringing up the GenericObjectEditor. */ |
---|
| 110 | protected JCheckBox m_CheckBoxOptions; |
---|
| 111 | |
---|
| 112 | /** the note about the options dialog. */ |
---|
| 113 | protected JLabel m_LabelOptions; |
---|
| 114 | |
---|
| 115 | /** the GOE for displaying the options of a loader/saver. */ |
---|
| 116 | protected GenericObjectEditor m_Editor = null; |
---|
| 117 | |
---|
| 118 | /** whether the GOE was OKed or Canceled. */ |
---|
| 119 | protected int m_EditorResult; |
---|
| 120 | |
---|
| 121 | /** whether to display only core converters (hardcoded in ConverterUtils). |
---|
| 122 | * Necessary for RMI/Remote Experiments for instance. |
---|
| 123 | * |
---|
| 124 | * @see ConverterUtils#CORE_FILE_LOADERS |
---|
| 125 | * @see ConverterUtils#CORE_FILE_SAVERS */ |
---|
| 126 | protected boolean m_CoreConvertersOnly = false; |
---|
| 127 | |
---|
| 128 | static { |
---|
| 129 | initFilters(true, ConverterUtils.getFileLoaders()); |
---|
| 130 | initFilters(false, ConverterUtils.getFileSavers()); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | /** |
---|
| 134 | * onstructs a FileChooser pointing to the user's default directory. |
---|
| 135 | */ |
---|
| 136 | public ConverterFileChooser() { |
---|
| 137 | super(); |
---|
| 138 | initialize(); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | /** |
---|
| 142 | * Constructs a FileChooser using the given File as the path. |
---|
| 143 | * |
---|
| 144 | * @param currentDirectory the path to start in |
---|
| 145 | */ |
---|
| 146 | public ConverterFileChooser(File currentDirectory) { |
---|
| 147 | super(currentDirectory); |
---|
| 148 | initialize(); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | /** |
---|
| 152 | * Constructs a FileChooser using the given path. |
---|
| 153 | * |
---|
| 154 | * @param currentDirectory the path to start in |
---|
| 155 | */ |
---|
| 156 | public ConverterFileChooser(String currentDirectory) { |
---|
| 157 | super(currentDirectory); |
---|
| 158 | initialize(); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | /** |
---|
| 162 | * Further initializations. |
---|
| 163 | */ |
---|
| 164 | protected void initialize() { |
---|
| 165 | JPanel panel; |
---|
| 166 | JPanel panel2; |
---|
| 167 | |
---|
| 168 | m_Self = this; |
---|
| 169 | |
---|
| 170 | m_CheckBoxOptions = new JCheckBox("Invoke options dialog"); |
---|
| 171 | m_CheckBoxOptions.setMnemonic('I'); |
---|
| 172 | m_LabelOptions = new JLabel("<html><br>Note:<br><br>Some file formats offer additional<br>options which can be customized<br>when invoking the options dialog.</html>"); |
---|
| 173 | panel = new JPanel(new BorderLayout()); |
---|
| 174 | panel.add(m_CheckBoxOptions, BorderLayout.NORTH); |
---|
| 175 | panel2 = new JPanel(new BorderLayout()); |
---|
| 176 | panel2.add(m_LabelOptions, BorderLayout.NORTH); |
---|
| 177 | panel2.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
---|
| 178 | panel.add(panel2, BorderLayout.CENTER); |
---|
| 179 | setAccessory(panel); |
---|
| 180 | |
---|
| 181 | m_Editor = new GenericObjectEditor(false); |
---|
| 182 | ((GenericObjectEditor.GOEPanel) m_Editor.getCustomEditor()).addOkListener(new ActionListener() { |
---|
| 183 | public void actionPerformed(ActionEvent e) { |
---|
| 184 | m_EditorResult = JFileChooser.APPROVE_OPTION; |
---|
| 185 | m_CurrentConverter = m_Editor.getValue(); |
---|
| 186 | // thanks to serialization and transient readers/streams, we have |
---|
| 187 | // to set the file again to initialize the converter again |
---|
| 188 | try { |
---|
| 189 | ((FileSourcedConverter) m_CurrentConverter).setFile(((FileSourcedConverter) m_CurrentConverter).retrieveFile()); |
---|
| 190 | } |
---|
| 191 | catch (Exception ex) { |
---|
| 192 | // ignored |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | }); |
---|
| 196 | ((GenericObjectEditor.GOEPanel) m_Editor.getCustomEditor()).addCancelListener(new ActionListener() { |
---|
| 197 | public void actionPerformed(ActionEvent e) { |
---|
| 198 | m_EditorResult = JFileChooser.CANCEL_OPTION; |
---|
| 199 | } |
---|
| 200 | }); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | /** |
---|
| 204 | * filters out all non-core loaders if only those should be displayed. |
---|
| 205 | * |
---|
| 206 | * @param list the list of filters to check |
---|
| 207 | * @return the filtered list of filters |
---|
| 208 | * @see #m_CoreConvertersOnly |
---|
| 209 | */ |
---|
| 210 | protected Vector<ExtensionFileFilter> filterNonCoreLoaderFileFilters(Vector<ExtensionFileFilter> list) { |
---|
| 211 | Vector<ExtensionFileFilter> result; |
---|
| 212 | int i; |
---|
| 213 | ExtensionFileFilter filter; |
---|
| 214 | AbstractLoader loader; |
---|
| 215 | |
---|
| 216 | if (!getCoreConvertersOnly()) { |
---|
| 217 | result = list; |
---|
| 218 | } |
---|
| 219 | else { |
---|
| 220 | result = new Vector<ExtensionFileFilter>(); |
---|
| 221 | for (i = 0; i < list.size(); i++) { |
---|
| 222 | filter = list.get(i); |
---|
| 223 | loader = ConverterUtils.getLoaderForExtension(filter.getExtensions()[0]); |
---|
| 224 | if (ConverterUtils.isCoreFileLoader(loader.getClass().getName())) |
---|
| 225 | result.add(filter); |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | return result; |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | /** |
---|
| 233 | * filters out all non-core savers if only those should be displayed. |
---|
| 234 | * |
---|
| 235 | * @param list the list of filters to check |
---|
| 236 | * @return the filtered list of filters |
---|
| 237 | * @see #m_CoreConvertersOnly |
---|
| 238 | */ |
---|
| 239 | protected Vector<ExtensionFileFilter> filterNonCoreSaverFileFilters(Vector<ExtensionFileFilter> list) { |
---|
| 240 | Vector<ExtensionFileFilter> result; |
---|
| 241 | int i; |
---|
| 242 | ExtensionFileFilter filter; |
---|
| 243 | AbstractSaver saver; |
---|
| 244 | |
---|
| 245 | if (!getCoreConvertersOnly()) { |
---|
| 246 | result = list; |
---|
| 247 | } |
---|
| 248 | else { |
---|
| 249 | result = new Vector<ExtensionFileFilter>(); |
---|
| 250 | for (i = 0; i < list.size(); i++) { |
---|
| 251 | filter = list.get(i); |
---|
| 252 | saver = ConverterUtils.getSaverForExtension(filter.getExtensions()[0]); |
---|
| 253 | if (ConverterUtils.isCoreFileSaver(saver.getClass().getName())) |
---|
| 254 | result.add(filter); |
---|
| 255 | } |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | return result; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | /** |
---|
| 262 | * filters the list of file filters according to the currently set. |
---|
| 263 | * Capabilities |
---|
| 264 | * |
---|
| 265 | * @param list the filters to check |
---|
| 266 | * @return the filtered list of filters |
---|
| 267 | */ |
---|
| 268 | protected Vector<ExtensionFileFilter> filterSaverFileFilters(Vector<ExtensionFileFilter> list) { |
---|
| 269 | Vector<ExtensionFileFilter> result; |
---|
| 270 | int i; |
---|
| 271 | ExtensionFileFilter filter; |
---|
| 272 | AbstractSaver saver; |
---|
| 273 | |
---|
| 274 | if (m_CapabilitiesFilter == null) { |
---|
| 275 | result = list; |
---|
| 276 | } |
---|
| 277 | else { |
---|
| 278 | result = new Vector<ExtensionFileFilter>(); |
---|
| 279 | |
---|
| 280 | for (i = 0; i < list.size(); i++) { |
---|
| 281 | filter = list.get(i); |
---|
| 282 | saver = ConverterUtils.getSaverForExtension(filter.getExtensions()[0]); |
---|
| 283 | if (saver.getCapabilities().supports(m_CapabilitiesFilter)) |
---|
| 284 | result.add(filter); |
---|
| 285 | } |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | return result; |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | /** |
---|
| 292 | * initializes the ExtensionFileFilters. |
---|
| 293 | * |
---|
| 294 | * @param loader if true then the loader filter are initialized |
---|
| 295 | * @param classnames the classnames of the converters |
---|
| 296 | */ |
---|
| 297 | protected static void initFilters(boolean loader, Vector<String> classnames) { |
---|
| 298 | int i; |
---|
| 299 | int n; |
---|
| 300 | String classname; |
---|
| 301 | Class cls; |
---|
| 302 | String[] ext; |
---|
| 303 | String desc; |
---|
| 304 | FileSourcedConverter converter; |
---|
| 305 | ExtensionFileFilter filter; |
---|
| 306 | |
---|
| 307 | if (loader) |
---|
| 308 | m_LoaderFileFilters = new Vector<ExtensionFileFilter>(); |
---|
| 309 | else |
---|
| 310 | m_SaverFileFilters = new Vector<ExtensionFileFilter>(); |
---|
| 311 | |
---|
| 312 | for (i = 0; i < classnames.size(); i++) { |
---|
| 313 | classname = (String) classnames.get(i); |
---|
| 314 | |
---|
| 315 | // get data from converter |
---|
| 316 | try { |
---|
| 317 | cls = Class.forName(classname); |
---|
| 318 | converter = (FileSourcedConverter) cls.newInstance(); |
---|
| 319 | ext = converter.getFileExtensions(); |
---|
| 320 | desc = converter.getFileDescription(); |
---|
| 321 | } |
---|
| 322 | catch (Exception e) { |
---|
| 323 | cls = null; |
---|
| 324 | converter = null; |
---|
| 325 | ext = new String[0]; |
---|
| 326 | desc = ""; |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | if (converter == null) |
---|
| 330 | continue; |
---|
| 331 | |
---|
| 332 | // loader? |
---|
| 333 | if (loader) { |
---|
| 334 | for (n = 0; n < ext.length; n++) { |
---|
| 335 | filter = new ExtensionFileFilter(ext[n], desc + " (*" + ext[n] + ")"); |
---|
| 336 | m_LoaderFileFilters.add(filter); |
---|
| 337 | } |
---|
| 338 | } |
---|
| 339 | else { |
---|
| 340 | for (n = 0; n < ext.length; n++) { |
---|
| 341 | filter = new ExtensionFileFilter(ext[n], desc + " (*" + ext[n] + ")"); |
---|
| 342 | m_SaverFileFilters.add(filter); |
---|
| 343 | } |
---|
| 344 | } |
---|
| 345 | } |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | /** |
---|
| 349 | * initializes the GUI. |
---|
| 350 | * |
---|
| 351 | * @param dialogType the type of dialog to setup the GUI for |
---|
| 352 | */ |
---|
| 353 | protected void initGUI(int dialogType) { |
---|
| 354 | Vector<ExtensionFileFilter> list; |
---|
| 355 | int i; |
---|
| 356 | boolean acceptAll; |
---|
| 357 | |
---|
| 358 | // backup current state |
---|
| 359 | acceptAll = isAcceptAllFileFilterUsed(); |
---|
| 360 | |
---|
| 361 | // setup filters |
---|
| 362 | resetChoosableFileFilters(); |
---|
| 363 | setAcceptAllFileFilterUsed(acceptAll); |
---|
| 364 | if (dialogType == LOADER_DIALOG) |
---|
| 365 | list = filterNonCoreLoaderFileFilters(m_LoaderFileFilters); |
---|
| 366 | else |
---|
| 367 | list = filterSaverFileFilters(filterNonCoreSaverFileFilters(m_SaverFileFilters)); |
---|
| 368 | for (i = 0; i < list.size(); i++) { |
---|
| 369 | addChoosableFileFilter(list.get(i)); |
---|
| 370 | } |
---|
| 371 | if (list.size() > 0) { |
---|
| 372 | if ( (m_LastFilter == null) || (!list.contains(m_LastFilter)) ) |
---|
| 373 | setFileFilter(list.get(0)); |
---|
| 374 | else |
---|
| 375 | setFileFilter(m_LastFilter); |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | // listener |
---|
| 379 | if (m_Listener != null) |
---|
| 380 | removePropertyChangeListener(m_Listener); |
---|
| 381 | m_Listener = new PropertyChangeListener() { |
---|
| 382 | public void propertyChange(PropertyChangeEvent evt) { |
---|
| 383 | // filter changed |
---|
| 384 | if (evt.getPropertyName().equals(FILE_FILTER_CHANGED_PROPERTY)) { |
---|
| 385 | updateCurrentConverter(); |
---|
| 386 | } |
---|
| 387 | } |
---|
| 388 | }; |
---|
| 389 | addPropertyChangeListener(m_Listener); |
---|
| 390 | |
---|
| 391 | // initial setup |
---|
| 392 | if (dialogType == LOADER_DIALOG) { |
---|
| 393 | m_Editor.setClassType(AbstractFileLoader.class); |
---|
| 394 | m_Editor.setValue(new weka.core.converters.ArffLoader()); |
---|
| 395 | } |
---|
| 396 | else { |
---|
| 397 | m_Editor.setClassType(AbstractFileSaver.class); |
---|
| 398 | m_Editor.setValue(new weka.core.converters.ArffSaver()); |
---|
| 399 | } |
---|
| 400 | |
---|
| 401 | updateCurrentConverter(); |
---|
| 402 | } |
---|
| 403 | |
---|
| 404 | /** |
---|
| 405 | * sets the capabilities that the savers must have. use null if all should be |
---|
| 406 | * listed. |
---|
| 407 | * |
---|
| 408 | * @param value the minimum Capabilities the savers must have |
---|
| 409 | */ |
---|
| 410 | public void setCapabilitiesFilter(Capabilities value) { |
---|
| 411 | m_CapabilitiesFilter = (Capabilities) value.clone(); |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | /** |
---|
| 415 | * returns the capabilities filter for the savers, can be null if all are |
---|
| 416 | * listed. |
---|
| 417 | * |
---|
| 418 | * @return the minimum Capabilities the savers must have |
---|
| 419 | */ |
---|
| 420 | public Capabilities getCapabilitiesFilter() { |
---|
| 421 | if (m_CapabilitiesFilter != null) |
---|
| 422 | return (Capabilities) m_CapabilitiesFilter.clone(); |
---|
| 423 | else |
---|
| 424 | return null; |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | /** |
---|
| 428 | * Whether a warning is popped up if the file that is to be saved already |
---|
| 429 | * exists (only save dialog). |
---|
| 430 | * |
---|
| 431 | * @param value if true a warning will be popup |
---|
| 432 | */ |
---|
| 433 | public void setOverwriteWarning(boolean value) { |
---|
| 434 | m_OverwriteWarning = value; |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | /** |
---|
| 438 | * Returns whether a popup appears with a warning that the file already |
---|
| 439 | * exists (only save dialog). |
---|
| 440 | * |
---|
| 441 | * @return true if a warning pops up |
---|
| 442 | */ |
---|
| 443 | public boolean getOverwriteWarning() { |
---|
| 444 | return m_OverwriteWarning; |
---|
| 445 | } |
---|
| 446 | |
---|
| 447 | /** |
---|
| 448 | * Whether the selected file must exst (only open dialog). |
---|
| 449 | * |
---|
| 450 | * @param value if true the file must exist |
---|
| 451 | */ |
---|
| 452 | public void setFileMustExist(boolean value) { |
---|
| 453 | m_FileMustExist = value; |
---|
| 454 | } |
---|
| 455 | |
---|
| 456 | /** |
---|
| 457 | * Returns whether the selected file must exist (only open dialog). |
---|
| 458 | * |
---|
| 459 | * @return true if the file must exist |
---|
| 460 | */ |
---|
| 461 | public boolean getFileMustExist() { |
---|
| 462 | return m_FileMustExist; |
---|
| 463 | } |
---|
| 464 | |
---|
| 465 | /** |
---|
| 466 | * Whether to display only the hardocded core converters. Necessary for |
---|
| 467 | * RMI/Remote Experiments (dynamic class discovery doesn't work there!). |
---|
| 468 | * |
---|
| 469 | * @param value if true only the core converters will be displayed |
---|
| 470 | * @see #m_CoreConvertersOnly |
---|
| 471 | */ |
---|
| 472 | public void setCoreConvertersOnly(boolean value) { |
---|
| 473 | m_CoreConvertersOnly = value; |
---|
| 474 | } |
---|
| 475 | |
---|
| 476 | /** |
---|
| 477 | * Returns whether only the hardcoded core converters are displayed. |
---|
| 478 | * Necessary for RMI/REmote Experiments (dynamic class discovery doesn't |
---|
| 479 | * work there!). |
---|
| 480 | * |
---|
| 481 | * @return true if the file must exist |
---|
| 482 | * @see #m_CoreConvertersOnly |
---|
| 483 | */ |
---|
| 484 | public boolean getCoreConvertersOnly() { |
---|
| 485 | return m_CoreConvertersOnly; |
---|
| 486 | } |
---|
| 487 | |
---|
| 488 | /** |
---|
| 489 | * Pops a custom file chooser dialog with a custom approve button. Throws |
---|
| 490 | * an exception, if the dialog type is UNHANDLED_DIALOG. |
---|
| 491 | * |
---|
| 492 | * @param parent the parent of this dialog |
---|
| 493 | * @param approveButtonText the text for the OK button |
---|
| 494 | * @return the user's action |
---|
| 495 | */ |
---|
| 496 | public int showDialog(Component parent, String approveButtonText) { |
---|
| 497 | if (m_DialogType == UNHANDLED_DIALOG) |
---|
| 498 | throw new IllegalStateException("Either use showOpenDialog or showSaveDialog!"); |
---|
| 499 | else |
---|
| 500 | return super.showDialog(parent, approveButtonText); |
---|
| 501 | } |
---|
| 502 | |
---|
| 503 | /** |
---|
| 504 | * Pops up an "Open File" file chooser dialog. |
---|
| 505 | * |
---|
| 506 | * @param parent the parent of this file chooser |
---|
| 507 | * @return the result of the user's action |
---|
| 508 | */ |
---|
| 509 | public int showOpenDialog(Component parent) { |
---|
| 510 | m_DialogType = LOADER_DIALOG; |
---|
| 511 | m_CurrentConverter = null; |
---|
| 512 | |
---|
| 513 | initGUI(LOADER_DIALOG); |
---|
| 514 | |
---|
| 515 | int result = super.showOpenDialog(parent); |
---|
| 516 | |
---|
| 517 | m_DialogType = UNHANDLED_DIALOG; |
---|
| 518 | removePropertyChangeListener(m_Listener); |
---|
| 519 | |
---|
| 520 | // do we have to add the extension? |
---|
| 521 | if ( (result == APPROVE_OPTION) && (getSelectedFile().isFile()) ) { |
---|
| 522 | if (getFileFilter() instanceof ExtensionFileFilter) { |
---|
| 523 | String filename = getSelectedFile().getAbsolutePath(); |
---|
| 524 | String[] extensions = ((ExtensionFileFilter) getFileFilter()).getExtensions(); |
---|
| 525 | if (!filename.endsWith(extensions[0])) { |
---|
| 526 | filename += extensions[0]; |
---|
| 527 | setSelectedFile(new File(filename)); |
---|
| 528 | } |
---|
| 529 | } |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | // does file exist? |
---|
| 533 | if ( (result == APPROVE_OPTION) |
---|
| 534 | && (getFileMustExist()) |
---|
| 535 | && (getSelectedFile().isFile()) |
---|
| 536 | && (!getSelectedFile().exists()) ) { |
---|
| 537 | int retVal = JOptionPane.showConfirmDialog( |
---|
| 538 | parent, |
---|
| 539 | "The file '" |
---|
| 540 | + getSelectedFile() |
---|
| 541 | + "' does not exist - please select again!"); |
---|
| 542 | if (retVal == JOptionPane.OK_OPTION) |
---|
| 543 | result = showOpenDialog(parent); |
---|
| 544 | else |
---|
| 545 | result = CANCEL_OPTION; |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | if (result == APPROVE_OPTION) { |
---|
| 549 | m_LastFilter = getFileFilter(); |
---|
| 550 | configureCurrentConverter(LOADER_DIALOG); |
---|
| 551 | |
---|
| 552 | // bring up options dialog? |
---|
| 553 | if (m_CheckBoxOptions.isSelected()) { |
---|
| 554 | m_EditorResult = JFileChooser.CANCEL_OPTION; |
---|
| 555 | m_Editor.setValue(m_CurrentConverter); |
---|
| 556 | PropertyDialog pd; |
---|
| 557 | if (PropertyDialog.getParentDialog(this) != null) |
---|
| 558 | pd = new PropertyDialog(PropertyDialog.getParentDialog(this), m_Editor); |
---|
| 559 | else |
---|
| 560 | pd = new PropertyDialog(PropertyDialog.getParentFrame(this), m_Editor); |
---|
| 561 | pd.setVisible(true); |
---|
| 562 | result = m_EditorResult; |
---|
| 563 | } |
---|
| 564 | } |
---|
| 565 | |
---|
| 566 | return result; |
---|
| 567 | } |
---|
| 568 | |
---|
| 569 | /** |
---|
| 570 | * Pops up an "Save File" file chooser dialog. |
---|
| 571 | * |
---|
| 572 | * @param parent the parent of this file chooser |
---|
| 573 | * @return the result of the user's action |
---|
| 574 | */ |
---|
| 575 | public int showSaveDialog(Component parent) { |
---|
| 576 | m_DialogType = SAVER_DIALOG; |
---|
| 577 | m_CurrentConverter = null; |
---|
| 578 | |
---|
| 579 | initGUI(SAVER_DIALOG); |
---|
| 580 | |
---|
| 581 | boolean acceptAll = isAcceptAllFileFilterUsed(); |
---|
| 582 | |
---|
| 583 | // using "setAcceptAllFileFilterUsed" messes up the currently selected |
---|
| 584 | // file filter/file, hence backup/restore of currently selected |
---|
| 585 | // file filter/file |
---|
| 586 | FileFilter currentFilter = getFileFilter(); |
---|
| 587 | File currentFile = getSelectedFile(); |
---|
| 588 | setAcceptAllFileFilterUsed(false); |
---|
| 589 | setFileFilter(currentFilter); |
---|
| 590 | setSelectedFile(currentFile); |
---|
| 591 | |
---|
| 592 | int result = super.showSaveDialog(parent); |
---|
| 593 | |
---|
| 594 | // do we have to add the extension? |
---|
| 595 | if (result == APPROVE_OPTION) { |
---|
| 596 | if (getFileFilter() instanceof ExtensionFileFilter) { |
---|
| 597 | String filename = getSelectedFile().getAbsolutePath(); |
---|
| 598 | String[] extensions = ((ExtensionFileFilter) getFileFilter()).getExtensions(); |
---|
| 599 | if (!filename.endsWith(extensions[0])) { |
---|
| 600 | filename += extensions[0]; |
---|
| 601 | setSelectedFile(new File(filename)); |
---|
| 602 | } |
---|
| 603 | } |
---|
| 604 | } |
---|
| 605 | |
---|
| 606 | // using "setAcceptAllFileFilterUsed" messes up the currently selected |
---|
| 607 | // file filter/file, hence backup/restore of currently selected |
---|
| 608 | // file filter/file |
---|
| 609 | currentFilter = getFileFilter(); |
---|
| 610 | currentFile = getSelectedFile(); |
---|
| 611 | setAcceptAllFileFilterUsed(acceptAll); |
---|
| 612 | setFileFilter(currentFilter); |
---|
| 613 | setSelectedFile(currentFile); |
---|
| 614 | |
---|
| 615 | m_DialogType = UNHANDLED_DIALOG; |
---|
| 616 | removePropertyChangeListener(m_Listener); |
---|
| 617 | |
---|
| 618 | // overwrite the file? |
---|
| 619 | if ( (result == APPROVE_OPTION) |
---|
| 620 | && (getOverwriteWarning()) |
---|
| 621 | && (getSelectedFile().exists()) ) { |
---|
| 622 | int retVal = JOptionPane.showConfirmDialog( |
---|
| 623 | parent, |
---|
| 624 | "The file '" |
---|
| 625 | + getSelectedFile() |
---|
| 626 | + "' already exists - overwrite it?"); |
---|
| 627 | if (retVal == JOptionPane.OK_OPTION) |
---|
| 628 | result = APPROVE_OPTION; |
---|
| 629 | else if (retVal == JOptionPane.NO_OPTION) |
---|
| 630 | result = showSaveDialog(parent); |
---|
| 631 | else |
---|
| 632 | result = CANCEL_OPTION; |
---|
| 633 | } |
---|
| 634 | |
---|
| 635 | if (result == APPROVE_OPTION) { |
---|
| 636 | m_LastFilter = getFileFilter(); |
---|
| 637 | configureCurrentConverter(SAVER_DIALOG); |
---|
| 638 | |
---|
| 639 | // bring up options dialog? |
---|
| 640 | if (m_CheckBoxOptions.isSelected()) { |
---|
| 641 | m_EditorResult = JFileChooser.CANCEL_OPTION; |
---|
| 642 | m_Editor.setValue(m_CurrentConverter); |
---|
| 643 | PropertyDialog pd; |
---|
| 644 | if (PropertyDialog.getParentDialog(this) != null) |
---|
| 645 | pd = new PropertyDialog(PropertyDialog.getParentDialog(this), m_Editor); |
---|
| 646 | else |
---|
| 647 | pd = new PropertyDialog(PropertyDialog.getParentFrame(this), m_Editor); |
---|
| 648 | pd.setVisible(true); |
---|
| 649 | result = m_EditorResult; |
---|
| 650 | } |
---|
| 651 | } |
---|
| 652 | |
---|
| 653 | return result; |
---|
| 654 | } |
---|
| 655 | |
---|
| 656 | /** |
---|
| 657 | * returns the loader that was chosen by the user, can be null in case the |
---|
| 658 | * user aborted the dialog or the save dialog was shown. |
---|
| 659 | * |
---|
| 660 | * @return the chosen loader, if any |
---|
| 661 | */ |
---|
| 662 | public AbstractFileLoader getLoader() { |
---|
| 663 | configureCurrentConverter(LOADER_DIALOG); |
---|
| 664 | |
---|
| 665 | if (m_CurrentConverter instanceof AbstractFileSaver) |
---|
| 666 | return null; |
---|
| 667 | else |
---|
| 668 | return (AbstractFileLoader) m_CurrentConverter; |
---|
| 669 | } |
---|
| 670 | |
---|
| 671 | /** |
---|
| 672 | * returns the saver that was chosen by the user, can be null in case the |
---|
| 673 | * user aborted the dialog or the open dialog was shown. |
---|
| 674 | * |
---|
| 675 | * @return the chosen saver, if any |
---|
| 676 | */ |
---|
| 677 | public AbstractFileSaver getSaver() { |
---|
| 678 | configureCurrentConverter(SAVER_DIALOG); |
---|
| 679 | |
---|
| 680 | if (m_CurrentConverter instanceof AbstractFileLoader) |
---|
| 681 | return null; |
---|
| 682 | else |
---|
| 683 | return (AbstractFileSaver) m_CurrentConverter; |
---|
| 684 | } |
---|
| 685 | |
---|
| 686 | /** |
---|
| 687 | * sets the current converter according to the current filefilter. |
---|
| 688 | */ |
---|
| 689 | protected void updateCurrentConverter() { |
---|
| 690 | String[] extensions; |
---|
| 691 | Object newConverter; |
---|
| 692 | |
---|
| 693 | if (getFileFilter() == null) |
---|
| 694 | return; |
---|
| 695 | |
---|
| 696 | if (!isAcceptAllFileFilterUsed()) { |
---|
| 697 | // determine current converter |
---|
| 698 | extensions = ((ExtensionFileFilter) getFileFilter()).getExtensions(); |
---|
| 699 | if (m_DialogType == LOADER_DIALOG) |
---|
| 700 | newConverter = ConverterUtils.getLoaderForExtension(extensions[0]); |
---|
| 701 | else |
---|
| 702 | newConverter = ConverterUtils.getSaverForExtension(extensions[0]); |
---|
| 703 | |
---|
| 704 | try { |
---|
| 705 | if (m_CurrentConverter == null) { |
---|
| 706 | m_CurrentConverter = newConverter; |
---|
| 707 | } |
---|
| 708 | else { |
---|
| 709 | if (!m_CurrentConverter.getClass().equals(newConverter.getClass())) |
---|
| 710 | m_CurrentConverter = newConverter; |
---|
| 711 | } |
---|
| 712 | } |
---|
| 713 | catch (Exception e) { |
---|
| 714 | m_CurrentConverter = null; |
---|
| 715 | e.printStackTrace(); |
---|
| 716 | } |
---|
| 717 | } |
---|
| 718 | else { |
---|
| 719 | m_CurrentConverter = null; |
---|
| 720 | } |
---|
| 721 | } |
---|
| 722 | |
---|
| 723 | /** |
---|
| 724 | * configures the current converter. |
---|
| 725 | * |
---|
| 726 | * @param dialogType the type of dialog to configure for |
---|
| 727 | */ |
---|
| 728 | protected void configureCurrentConverter(int dialogType) { |
---|
| 729 | String filename; |
---|
| 730 | File currFile; |
---|
| 731 | |
---|
| 732 | if ( (getSelectedFile() == null) || (getSelectedFile().isDirectory()) ) |
---|
| 733 | return; |
---|
| 734 | |
---|
| 735 | filename = getSelectedFile().getAbsolutePath(); |
---|
| 736 | |
---|
| 737 | if (m_CurrentConverter == null) { |
---|
| 738 | if (dialogType == LOADER_DIALOG) |
---|
| 739 | m_CurrentConverter = ConverterUtils.getLoaderForFile(filename); |
---|
| 740 | else if (dialogType == SAVER_DIALOG) |
---|
| 741 | m_CurrentConverter = ConverterUtils.getSaverForFile(filename); |
---|
| 742 | else |
---|
| 743 | throw new IllegalStateException("Cannot determine loader/saver!"); |
---|
| 744 | |
---|
| 745 | // none found? |
---|
| 746 | if (m_CurrentConverter == null) |
---|
| 747 | return; |
---|
| 748 | } |
---|
| 749 | |
---|
| 750 | try { |
---|
| 751 | currFile = ((FileSourcedConverter) m_CurrentConverter).retrieveFile(); |
---|
| 752 | if ((currFile == null) || (!currFile.getAbsolutePath().equals(filename))) |
---|
| 753 | ((FileSourcedConverter) m_CurrentConverter).setFile(new File(filename)); |
---|
| 754 | } |
---|
| 755 | catch (Exception e) { |
---|
| 756 | e.printStackTrace(); |
---|
| 757 | } |
---|
| 758 | } |
---|
| 759 | |
---|
| 760 | /** |
---|
| 761 | * For testing the file chooser. |
---|
| 762 | * |
---|
| 763 | * @param args the commandline options - ignored |
---|
| 764 | * @throws Exception if something goes wrong with loading/saving |
---|
| 765 | */ |
---|
| 766 | public static void main(String[] args) throws Exception { |
---|
| 767 | ConverterFileChooser fc; |
---|
| 768 | int retVal; |
---|
| 769 | AbstractFileLoader loader; |
---|
| 770 | AbstractFileSaver saver; |
---|
| 771 | Instances data; |
---|
| 772 | |
---|
| 773 | fc = new ConverterFileChooser(); |
---|
| 774 | retVal = fc.showOpenDialog(null); |
---|
| 775 | |
---|
| 776 | // load file |
---|
| 777 | if (retVal == ConverterFileChooser.APPROVE_OPTION) { |
---|
| 778 | loader = fc.getLoader(); |
---|
| 779 | data = loader.getDataSet(); |
---|
| 780 | retVal = fc.showSaveDialog(null); |
---|
| 781 | |
---|
| 782 | // save file |
---|
| 783 | if (retVal == ConverterFileChooser.APPROVE_OPTION) { |
---|
| 784 | saver = fc.getSaver(); |
---|
| 785 | saver.setInstances(data); |
---|
| 786 | saver.writeBatch(); |
---|
| 787 | } |
---|
| 788 | else { |
---|
| 789 | System.out.println("Saving aborted!"); |
---|
| 790 | } |
---|
| 791 | } |
---|
| 792 | else { |
---|
| 793 | System.out.println("Loading aborted!"); |
---|
| 794 | } |
---|
| 795 | } |
---|
| 796 | } |
---|