AddFileAction.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.gui.se.control;
017 
018 import java.awt.event.ActionEvent;
019 import java.io.File;
020 import java.io.IOException;
021 
022 import javax.swing.AbstractAction;
023 import javax.swing.JFileChooser;
024 import javax.swing.JOptionPane;
025 import javax.swing.filechooser.FileFilter;
026 
027 import org.qedeq.base.trace.Trace;
028 import org.qedeq.gui.se.pane.QedeqGuiConfig;
029 import org.qedeq.kernel.bo.KernelContext;
030 import org.qedeq.kernel.se.common.ModuleAddress;
031 
032 /**
033  * Load new module from local file.
034  *
035  @version $Revision: 1.7 $
036  @author  Michael Meyling
037  */
038 class AddFileAction extends AbstractAction {
039 
040     /** This class. */
041     private static final Class CLASS = AddFileAction.class;
042 
043     /** Controller. */
044     private final QedeqController controller;
045 
046     /** Start directory for file load. */
047     private File file = QedeqGuiConfig.getInstance().getFileBrowserStartDirecty();
048 
049     /**
050      * Constructor.
051      *
052      @param   controller  Controler access.
053      */
054     AddFileAction(final QedeqController controller) {
055         this.controller = controller;
056     }
057 
058     public void actionPerformed(final ActionEvent e) {
059         Trace.trace(CLASS, this, "actionPerformed", e);
060         JFileChooser chooser = new JFileChooser(file);
061         final FileFilter filter = new FileFilter() {
062             public boolean accept(final File f) {
063                 if (f.isDirectory() || f.getPath().endsWith(".xml")) {
064                     return true;
065                 }
066                 return false;
067             }
068 
069             // description of this filter
070             public String getDescription() {
071                 return "Qedeq Modules";
072             }
073         };
074 
075         chooser.setFileFilter(filter);
076         final int returnVal = chooser.showOpenDialog(controller.getMainFrame());
077 
078         if (returnVal != JFileChooser.APPROVE_OPTION) {
079             return;
080         }
081         final ModuleAddress address;
082 
083         try {
084             // remember parent directory
085             file = chooser.getSelectedFile().getParentFile();
086 
087             QedeqGuiConfig.getInstance().setFileBrowserStartDirecty(file);
088             address = KernelContext.getInstance().getModuleAddress(chooser.getSelectedFile());
089         catch (IOException ie) {
090             Trace.trace(CLASS, this, "actionPerformed""no correct URL", ie);
091             JOptionPane.showMessageDialog(
092                 controller.getMainFrame(),       // the parent that the dialog blocks
093                 "this is no valid QEDEQ file: " + chooser.getSelectedFile()
094                 "\n" + ie.getMessage()// message
095                 "Error",                         // title
096                 JOptionPane.ERROR_MESSAGE        // message type
097             );
098             return;
099         }
100 
101         controller.addToModuleHistory(address.getUrl());
102         final Thread thread = new Thread() {
103             public void run() {
104                 KernelContext.getInstance().loadModule(address);
105             }
106         };
107         thread.setDaemon(true);
108         thread.start();
109     }
110 
111 }