Package vg.modules.opener

Source Code of vg.modules.opener.GUIOpener

package vg.modules.opener;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.Observable;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileFilter;

import vg.core.AMenuItem;
import vg.core.VisualGraph;
import vg.core.event.UIEventOpenNewGraph;
import vg.core.exception.CoreException;
import vg.core.exception.EnumCriticalityException;
import vg.core.plugin.IPlugin;
import vg.core.plugin.PluginParameter;

public class GUIOpener implements IPlugin {
  private JMenuItem      jMenuItem;
  private PluginParameter parameter;
  private JFileChooser   fileChooser;
  private final static String DEF_LAST_OPEN_GRAPH_DIR = "GRAPH_OPENER_LastDir";
  //-------------------------------------------------------------------------
  public void install(final PluginParameter param) {
    this.parameter = param;
    //---------------------------------------
    this.jMenuItem = new JMenuItem("Open file");
    this.jMenuItem.setIcon(new ImageIcon("./data/resources/textures/openFile.png"));
    //---------------------------------------
    this.jMenuItem.addActionListener(new ActionListener() {   
      public void actionPerformed(ActionEvent e) {
        openGraph();
      }
    });
    this.jMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.CTRL_DOWN_MASK));
    //---------------------------------------
    this.parameter.userInterface.addMenuItem(new AMenuItem(this.jMenuItem) {
      public void update(Observable o, Object arg) {
      }
    }, "File");
        fileChooser = new JFileChooser(".");
    String lastDir = VisualGraph.config.getProperty(GUIOpener.DEF_LAST_OPEN_GRAPH_DIR);
    if(lastDir != null) {
      fileChooser.setCurrentDirectory(new File(lastDir));
    }
       
        for (final String ext : CommandOpener.getAvailableExtensions()) {             
          fileChooser.addChoosableFileFilter(new FileFilter() {
        public String getDescription() {       
          return ext + " file (*." + ext +")";
        }
        public boolean accept(File f) {
          if (f != null) {
            if (f.isDirectory())
              return true;
            return f.getName().toLowerCase().endsWith(ext);
          }
          return false;
        }
      });
        }
  }
  private void openGraph() {   
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        fileChooser.updateUI();
        Component parent = parameter.userInterface.getView();
        if (fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
          VisualGraph.log.printDebug("[" + this.getClass().getName() + ".openGraph] [PROCESS] Open file = "
              + fileChooser.getSelectedFile().getPath());
          VisualGraph.config.setProperty(GUIOpener.DEF_LAST_OPEN_GRAPH_DIR, fileChooser.getCurrentDirectory().getAbsolutePath());
          Thread thread = new Thread(new Runnable() {
            public void run() {
              try {
                final String fileName = fileChooser.getSelectedFile().getPath();
                int newGraphId = CommandOpener.openFile(fileName, parameter.model);
                VisualGraph.log.printDebug("[" + this.getClass().getName() + ".openGraph] [OK] Open file = "
                    + fileChooser.getSelectedFile().getPath());
                UIEventOpenNewGraph ong = new UIEventOpenNewGraph(newGraphId, parameter.model.getGraphName(newGraphId));
                parameter.userInterface.addEvent(ong);
              } catch (Throwable e) {
                VisualGraph.log.printException(e);
                VisualGraph.windowMessage.showExceptionMessage(new CoreException("Cannot open file.\nException : "
                    + e.getMessage(), EnumCriticalityException.WARNING));
              }
            }
          });
          thread.setDaemon(true)
          thread.start();   
        }
      }
    });
 
  }
}
TOP

Related Classes of vg.modules.opener.GUIOpener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.