Search in sources :

Example 1 with MainFrame

use of org.concord.energy3d.gui.MainFrame in project energy3d by concord-consortium.

the class FileChooser method showDialog.

public File showDialog(final String dotExtension, final FileFilter filter, final boolean isSaveDialog) {
    if (Config.isMac() && filter != null) {
        fileDialog.setMode(isSaveDialog ? FileDialog.SAVE : FileDialog.LOAD);
        fileDialog.setTitle(isSaveDialog ? "Save" : "Open");
        fileDialog.setFilenameFilter(new FilenameFilter() {

            @Override
            public boolean accept(final File dir, final String name) {
                return name.toLowerCase().endsWith(dotExtension);
            }
        });
        if (isSaveDialog && dotExtension.equals(".ng3") && Scene.getURL() != null) {
            fileDialog.setFile(Scene.getURL().getFile());
        } else {
            fileDialog.setFile(null);
        }
        fileDialog.setVisible(true);
        final String filename = fileDialog.getFile();
        if (filename == null) {
            System.out.println("cancelled.");
            return null;
        } else {
            final String filenameFull = fileDialog.getDirectory() + filename + (filename.toLowerCase().endsWith(dotExtension) ? "" : dotExtension);
            final File file = new File(filenameFull);
            Preferences.userNodeForPackage(MainApplication.class).put("dir", fileDialog.getDirectory());
            return file;
        }
    } else {
        fileChooser.setFileSelectionMode(filter == null ? JFileChooser.DIRECTORIES_ONLY : JFileChooser.FILES_ONLY);
        fileChooser.resetChoosableFileFilters();
        if (filter != null) {
            fileChooser.addChoosableFileFilter(filter);
            fileChooser.setFileFilter(filter);
        }
        if (isSaveDialog && dotExtension.equals(".ng3") && Scene.getURL() != null) {
            fileChooser.setSelectedFile(new File(Scene.getURL().getFile()));
        } else {
            fileChooser.setSelectedFile(new File(""));
        }
        while (true) {
            final MainFrame parent = MainFrame.getInstance();
            if (isSaveDialog) {
                if (fileChooser.showSaveDialog(parent) == JFileChooser.CANCEL_OPTION) {
                    return null;
                }
            } else if (fileChooser.showOpenDialog(parent) == JFileChooser.CANCEL_OPTION) {
                return null;
            }
            File file = fileChooser.getSelectedFile();
            if (!file.toString().toLowerCase().endsWith(dotExtension)) {
                file = new File(file.getParentFile(), Util.getFileName(file.toString()) + dotExtension);
            }
            if (!isSaveDialog || !file.exists() || JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(parent, "File \"" + file.getName() + "\" already exists. Overwrite?", "Save File", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE)) {
                Preferences.userNodeForPackage(MainApplication.class).put("dir", fileChooser.getCurrentDirectory().toString());
                return file;
            }
        }
    }
}
Also used : FilenameFilter(java.io.FilenameFilter) File(java.io.File) MainFrame(org.concord.energy3d.gui.MainFrame) MainApplication(org.concord.energy3d.MainApplication)

Example 2 with MainFrame

use of org.concord.energy3d.gui.MainFrame in project energy3d by concord-consortium.

the class MainApplication method main.

public static void main(final String[] args) {
    // testRegex();
    System.out.println("Initiating...");
    final long t = System.nanoTime();
    // checkSingleInstance(MainApplication.class, args);
    // startDeadlockDetectionThread();
    agents = new ArrayList<Agent>();
    // TODO: temporary test code below
    // agents.add(new ConformanceChecker("Conformance Checker"));
    // agents.add(new EventMinerSheet2("Event Miner Sheet 2"));
    // agents.add(new EventMinerSheet3("Event Miner Sheet 3"));
    final File testFile = new File(System.getProperty("user.dir"), "test.txt");
    // can't use File.canWrite() to check if we can write a file to this folder. So we have to walk extra miles as follows.
    try {
        testFile.createNewFile();
        testFile.delete();
    } catch (final Throwable e) {
        appDirectoryWritable = false;
    }
    System.setProperty("jogl.gljpanel.noglsl", "true");
    if (System.getProperty("os.name").startsWith("Mac")) {
        System.setProperty("apple.laf.useScreenMenuBar", "true");
        System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Energy3D");
    }
    Config.setWebStart(System.getProperty("javawebstart.version", null) != null);
    if (!Config.isWebStart()) {
        System.setProperty("jogamp.gluegen.UseTempJarCache", "false");
    }
    final boolean isJarOrEclipse = !Config.isWebStart() && !System.getProperty("java.library.path").contains("jogl");
    if (isJarOrEclipse) {
        setupLibraryPath();
    }
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (final Exception e) {
        e.printStackTrace();
    }
    initializing = true;
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            // this calls Swing GUI
            final SceneManager sceneManager = SceneManager.getInstance();
            // this calls Swing GUI
            Scene.getInstance();
            sceneManagerThread = new Thread(sceneManager, "Energy3D Main Application");
            sceneManagerThread.start();
            final MainFrame mainFrame = MainFrame.getInstance();
            mainFrame.updateTitleBar();
            mainFrame.setVisible(true);
            new Thread("Energy3D Open File") {

                @Override
                public void run() {
                    try {
                        if (Config.isMac()) {
                            Thread.sleep(200);
                        }
                        if (isMacOpeningFile) {
                            return;
                        }
                        // somehow newFile() must be called to set up the scene before we can correctly load the content when an NG3 file is double-clicked without an open instance
                        if (Scene.getURL() == null) {
                            Scene.newFile();
                        }
                        if (Config.isWebStart()) {
                            if (args.length > 1 && !args[args.length - 1].startsWith("-")) {
                                mainFrame.open(args[args.length - 1]);
                            }
                        } else {
                            if (args.length > 0) {
                                mainFrame.open(args[0]);
                            }
                        }
                    } catch (final Exception e) {
                        e.printStackTrace();
                    } finally {
                        initializing = false;
                    }
                }
            }.start();
            Updater.download();
        }
    });
    /* initialize data logging */
    addShutdownHook(new Runnable() {

        @Override
        public void run() {
            TimeSeriesLogger.getInstance().close();
        }
    });
    TimeSeriesLogger.getInstance().start();
    SnapshotLogger.getInstance().start(20);
    System.out.println("Initiatialization phase 2 done.");
    System.out.println("Time = " + (System.nanoTime() - t) / 1000000000.0);
}
Also used : Agent(org.concord.energy3d.agents.Agent) SceneManager(org.concord.energy3d.scene.SceneManager) File(java.io.File) MainFrame(org.concord.energy3d.gui.MainFrame)

Aggregations

File (java.io.File)2 MainFrame (org.concord.energy3d.gui.MainFrame)2 FilenameFilter (java.io.FilenameFilter)1 MainApplication (org.concord.energy3d.MainApplication)1 Agent (org.concord.energy3d.agents.Agent)1 SceneManager (org.concord.energy3d.scene.SceneManager)1