Search in sources :

Example 1 with MainWindow

use of net.sf.mzmine.desktop.impl.MainWindow in project mzmine2 by mzmine.

the class SortDataFilesModule method runModule.

@Override
@Nonnull
public ExitCode runModule(@Nonnull MZmineProject project, @Nonnull ParameterSet parameters, @Nonnull Collection<Task> tasks) {
    List<RawDataFile> dataFiles = Arrays.asList(parameters.getParameter(SortDataFilesParameters.dataFiles).getValue().getMatchingRawDataFiles());
    RawDataTreeModel model = null;
    if (project instanceof MZmineProjectImpl) {
        model = ((MZmineProjectImpl) project).getRawDataTreeModel();
    } else if (MZmineCore.getDesktop() instanceof MainWindow) {
        ProjectTree tree = ((MainWindow) MZmineCore.getDesktop()).getMainPanel().getRawDataTree();
        model = (RawDataTreeModel) tree.getModel();
    }
    if (model == null)
        throw new MSDKRuntimeException("Cannot find raw data file tree model for sorting. Different MZmine project impl?");
    final DefaultMutableTreeNode rootNode = model.getRoot();
    // Get all tree nodes that represent selected data files, and remove
    // them from
    final ArrayList<DefaultMutableTreeNode> selectedNodes = new ArrayList<DefaultMutableTreeNode>();
    for (int row = 0; row < rootNode.getChildCount(); row++) {
        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) rootNode.getChildAt(row);
        Object selectedObject = selectedNode.getUserObject();
        if (dataFiles.contains(selectedObject)) {
            selectedNodes.add(selectedNode);
        }
    }
    // Get the index of the first selected item
    final ArrayList<Integer> positions = new ArrayList<Integer>();
    for (DefaultMutableTreeNode node : selectedNodes) {
        int nodeIndex = rootNode.getIndex(node);
        if (nodeIndex != -1)
            positions.add(nodeIndex);
    }
    if (positions.isEmpty())
        return ExitCode.ERROR;
    int insertPosition = Collections.min(positions);
    // Sort the data files by name
    Collections.sort(selectedNodes, new Comparator<DefaultMutableTreeNode>() {

        @Override
        public int compare(DefaultMutableTreeNode o1, DefaultMutableTreeNode o2) {
            return o1.getUserObject().toString().compareTo(o2.getUserObject().toString());
        }
    });
    // Reorder the nodes in the tree model
    for (DefaultMutableTreeNode node : selectedNodes) {
        model.removeNodeFromParent(node);
        model.insertNodeInto(node, rootNode, insertPosition);
        insertPosition++;
    }
    return ExitCode.OK;
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ArrayList(java.util.ArrayList) ProjectTree(net.sf.mzmine.desktop.impl.projecttree.ProjectTree) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) MainWindow(net.sf.mzmine.desktop.impl.MainWindow) MSDKRuntimeException(io.github.msdk.MSDKRuntimeException) RawDataTreeModel(net.sf.mzmine.desktop.impl.projecttree.RawDataTreeModel) MZmineProjectImpl(net.sf.mzmine.project.impl.MZmineProjectImpl) Nonnull(javax.annotation.Nonnull)

Example 2 with MainWindow

use of net.sf.mzmine.desktop.impl.MainWindow in project mzmine2 by mzmine.

the class MZmineCore method main.

/**
 * Main method
 */
@SuppressWarnings("unchecked")
public static void main(String[] args) {
    // In the beginning, set the default locale to English, to avoid
    // problems with conversion of numbers etc. (e.g. decimal separator may
    // be . or , depending on the locale)
    Locale.setDefault(new Locale("en", "US"));
    logger.info("Starting MZmine " + getMZmineVersion());
    // Remove old temporary files, if we find any
    TmpFileCleanup.removeOldTemporaryFiles();
    logger.fine("Loading core classes..");
    // create instance of configuration
    configuration = new MZmineConfigurationImpl();
    // create instances of core modules
    projectManager = new ProjectManagerImpl();
    taskController = new TaskControllerImpl();
    logger.fine("Initializing core classes..");
    projectManager.initModule();
    taskController.initModule();
    logger.fine("Loading modules");
    for (Class<?> moduleClass : MZmineModulesList.MODULES) {
        try {
            logger.finest("Loading module " + moduleClass.getName());
            // Create instance and init module
            MZmineModule moduleInstance = (MZmineModule) moduleClass.newInstance();
            // Add to the module list
            initializedModules.put(moduleClass, moduleInstance);
            // Create an instance of parameter set
            Class<? extends ParameterSet> parameterSetClass = moduleInstance.getParameterSetClass();
            ParameterSet parameterSetInstance = parameterSetClass.newInstance();
            // Add the parameter set to the configuration
            configuration.setModuleParameters((Class<MZmineModule>) moduleClass, parameterSetInstance);
        } catch (Throwable e) {
            logger.log(Level.SEVERE, "Could not load module " + moduleClass, e);
            e.printStackTrace();
            continue;
        }
    }
    // If we have no arguments, run in GUI mode, otherwise run in batch mode
    if (args.length == 0) {
        // Create the Swing GUI in the event-dispatching thread, as is
        // generally recommended
        Runnable desktopInit = new Runnable() {

            @Override
            public void run() {
                logger.fine("Initializing GUI");
                MainWindow mainWindow = new MainWindow();
                desktop = mainWindow;
                mainWindow.initModule();
                // Activate project - bind it to the desktop's project tree
                MZmineProjectImpl currentProject = (MZmineProjectImpl) projectManager.getCurrentProject();
                currentProject.activateProject();
                // add desktop menu icon
                for (Class<?> moduleClass : MZmineModulesList.MODULES) {
                    MZmineModule module = initializedModules.get(moduleClass);
                    if (module instanceof MZmineRunnableModule) {
                        mainWindow.getMainMenu().addMenuItemForModule((MZmineRunnableModule) module);
                    }
                }
            }
        };
        try {
            SwingUtilities.invokeAndWait(desktopInit);
        } catch (Exception e) {
            logger.log(Level.SEVERE, "Could not initialize GUI", e);
            e.printStackTrace();
            System.exit(1);
        }
    } else {
        desktop = new HeadLessDesktop();
    }
    // load configuration
    if (MZmineConfiguration.CONFIG_FILE.exists() && MZmineConfiguration.CONFIG_FILE.canRead()) {
        try {
            configuration.loadConfiguration(MZmineConfiguration.CONFIG_FILE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // if we have GUI, show it now
    if (desktop.getMainWindow() != null && !(desktop instanceof HeadLessDesktop)) {
        // update the size and position of the main window
        ParameterSet paramSet = configuration.getPreferences();
        WindowSettingsParameter settings = paramSet.getParameter(MZminePreferences.windowSetttings);
        settings.applySettingsToWindow(desktop.getMainWindow());
        // add last project menu items
        if (desktop instanceof MainWindow) {
            ((MainWindow) desktop).createLastUsedProjectsMenu(configuration.getLastProjects());
            // listen for changes
            configuration.getLastProjectsParameter().addFileListChangedListener(list -> {
                // new list of last used projects
                Desktop desk = getDesktop();
                if (desk instanceof MainWindow) {
                    ((MainWindow) desk).createLastUsedProjectsMenu(list);
                }
            });
        }
        // show the GUI
        logger.info("Showing main window");
        desktop.getMainWindow().setVisible(true);
        // show the welcome message
        desktop.setStatusBarText("Welcome to MZmine 2!");
        // Check for updated version
        NewVersionCheck NVC = new NewVersionCheck(CheckType.DESKTOP);
        Thread nvcThread = new Thread(NVC);
        nvcThread.setPriority(Thread.MIN_PRIORITY);
        nvcThread.start();
        // Tracker
        GoogleAnalyticsTracker GAT = new GoogleAnalyticsTracker("MZmine Loaded (GUI mode)", "/JAVA/Main/GUI");
        Thread gatThread = new Thread(GAT);
        gatThread.setPriority(Thread.MIN_PRIORITY);
        gatThread.start();
        // register shutdown hook only if we have GUI - we don't want to
        // save configuration on exit if we only run a batch
        ShutDownHook shutDownHook = new ShutDownHook();
        Runtime.getRuntime().addShutdownHook(shutDownHook);
    }
    // mode
    if (args.length > 0 && desktop instanceof HeadLessDesktop) {
        // Tracker
        GoogleAnalyticsTracker GAT = new GoogleAnalyticsTracker("MZmine Loaded (Headless mode)", "/JAVA/Main/GUI");
        Thread gatThread = new Thread(GAT);
        gatThread.setPriority(Thread.MIN_PRIORITY);
        gatThread.start();
        File batchFile = new File(args[0]);
        if ((!batchFile.exists()) || (!batchFile.canRead())) {
            logger.severe("Cannot read batch file " + batchFile);
            System.exit(1);
        }
        ExitCode exitCode = BatchModeModule.runBatch(projectManager.getCurrentProject(), batchFile);
        if (exitCode == ExitCode.OK)
            System.exit(0);
        else
            System.exit(1);
    }
}
Also used : Locale(java.util.Locale) ParameterSet(net.sf.mzmine.parameters.ParameterSet) MZmineRunnableModule(net.sf.mzmine.modules.MZmineRunnableModule) ExitCode(net.sf.mzmine.util.ExitCode) MZmineConfigurationImpl(net.sf.mzmine.main.impl.MZmineConfigurationImpl) TaskControllerImpl(net.sf.mzmine.taskcontrol.impl.TaskControllerImpl) IOException(java.io.IOException) ProjectManagerImpl(net.sf.mzmine.project.impl.ProjectManagerImpl) HeadLessDesktop(net.sf.mzmine.desktop.impl.HeadLessDesktop) WindowSettingsParameter(net.sf.mzmine.parameters.parametertypes.WindowSettingsParameter) HeadLessDesktop(net.sf.mzmine.desktop.impl.HeadLessDesktop) Desktop(net.sf.mzmine.desktop.Desktop) MainWindow(net.sf.mzmine.desktop.impl.MainWindow) File(java.io.File) MZmineModule(net.sf.mzmine.modules.MZmineModule) MZmineProjectImpl(net.sf.mzmine.project.impl.MZmineProjectImpl)

Example 3 with MainWindow

use of net.sf.mzmine.desktop.impl.MainWindow in project mzmine2 by mzmine.

the class ProjectSavingTask method run.

/**
 * @see java.lang.Runnable#run()
 */
@Override
public void run() {
    try {
        logger.info("Saving project to " + saveFile);
        setStatus(TaskStatus.PROCESSING);
        // Prepare a temporary ZIP file. We create this file in the same
        // directory as the final saveFile to avoid moving between
        // filesystems in the last stage (renameTo)
        File tempFile = File.createTempFile(saveFile.getName(), ".tmp", saveFile.getParentFile());
        tempFile.deleteOnExit();
        // Create a ZIP stream writing to the temporary file
        FileOutputStream tempStream = new FileOutputStream(tempFile);
        ZipOutputStream zipStream = new ZipOutputStream(tempStream);
        // Stage 1 - save version and configuration
        currentStage++;
        saveVersion(zipStream);
        saveConfiguration(zipStream);
        if (isCanceled()) {
            zipStream.close();
            tempFile.delete();
            return;
        }
        // Stage 2 - save RawDataFile objects
        currentStage++;
        saveRawDataFiles(zipStream);
        if (isCanceled()) {
            zipStream.close();
            tempFile.delete();
            return;
        }
        // Stage 3 - save PeakList objects
        currentStage++;
        savePeakLists(zipStream);
        if (isCanceled()) {
            zipStream.close();
            tempFile.delete();
            return;
        }
        // Stage 4 - save user parameters
        currentStage++;
        saveUserParameters(zipStream);
        if (isCanceled()) {
            zipStream.close();
            tempFile.delete();
            return;
        }
        // Stage 5 - finish and close the temporary ZIP file
        currentStage++;
        currentSavedObjectName = null;
        zipStream.close();
        // Final check for cancel
        if (isCanceled()) {
            tempFile.delete();
            return;
        }
        // Move the temporary ZIP file to the final location
        if (saveFile.exists() && !saveFile.delete()) {
            throw new IOException("Could not delete old file " + saveFile);
        }
        boolean renameOK = tempFile.renameTo(saveFile);
        if (!renameOK) {
            throw new IOException("Could not move the temporary file " + tempFile + " to the final location " + saveFile);
        }
        // Update the location of the project
        savedProject.setProjectFile(saveFile);
        // Update the window title to reflect the new name of the project
        if (MZmineCore.getDesktop() instanceof MainWindow) {
            MainWindow mainWindow = (MainWindow) MZmineCore.getDesktop();
            mainWindow.updateTitle();
        }
        logger.info("Finished saving the project to " + saveFile);
        setStatus(TaskStatus.FINISHED);
        // add to last loaded projects
        MZmineCore.getConfiguration().getLastProjectsParameter().addFile(saveFile);
    } catch (Throwable e) {
        e.printStackTrace();
        setStatus(TaskStatus.ERROR);
        if (currentSavedObjectName == null) {
            setErrorMessage("Failed saving the project: " + ExceptionUtils.exceptionToString(e));
        } else {
            setErrorMessage("Failed saving the project. Error while saving " + currentSavedObjectName + ": " + ExceptionUtils.exceptionToString(e));
        }
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) MainWindow(net.sf.mzmine.desktop.impl.MainWindow) IOException(java.io.IOException) RawDataFile(net.sf.mzmine.datamodel.RawDataFile) File(java.io.File)

Example 4 with MainWindow

use of net.sf.mzmine.desktop.impl.MainWindow in project mzmine2 by mzmine.

the class MZmineProjectImpl method activateProject.

public void activateProject() {
    // If running without GUI, just return
    if (!(MZmineCore.getDesktop() instanceof MainWindow))
        return;
    Runnable swingThreadCode = new Runnable() {

        public void run() {
            MainWindow mainWindow = (MainWindow) MZmineCore.getDesktop();
            // Update the name of the project in the window title
            mainWindow.updateTitle();
            ProjectTree peakListTree = mainWindow.getMainPanel().getPeakListTree();
            peakListTree.setModel(peakListTreeModel);
            ProjectTree rawDataTree = mainWindow.getMainPanel().getRawDataTree();
            rawDataTree.setModel(rawDataTreeModel);
        }
    };
    try {
        if (SwingUtilities.isEventDispatchThread())
            swingThreadCode.run();
        else
            SwingUtilities.invokeAndWait(swingThreadCode);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ProjectTree(net.sf.mzmine.desktop.impl.projecttree.ProjectTree) MainWindow(net.sf.mzmine.desktop.impl.MainWindow)

Example 5 with MainWindow

use of net.sf.mzmine.desktop.impl.MainWindow in project mzmine2 by mzmine.

the class SortPeakListsModule method runModule.

@Override
@Nonnull
public ExitCode runModule(@Nonnull MZmineProject project, @Nonnull ParameterSet parameters, @Nonnull Collection<Task> tasks) {
    List<PeakList> peakLists = Arrays.asList(parameters.getParameter(SortPeakListsParameters.peakLists).getValue().getMatchingPeakLists());
    PeakListTreeModel model = null;
    if (project instanceof MZmineProjectImpl) {
        model = ((MZmineProjectImpl) project).getPeakListTreeModel();
    } else if (MZmineCore.getDesktop() instanceof MainWindow) {
        ProjectTree tree = ((MainWindow) MZmineCore.getDesktop()).getMainPanel().getPeakListTree();
        model = (PeakListTreeModel) tree.getModel();
    }
    if (model == null)
        throw new MSDKRuntimeException("Cannot find feature list tree model for sorting. Different MZmine project impl?");
    final DefaultMutableTreeNode rootNode = model.getRoot();
    // Get all tree nodes that represent selected feature lists, and remove
    // them from
    final ArrayList<DefaultMutableTreeNode> selectedNodes = new ArrayList<DefaultMutableTreeNode>();
    for (int row = 0; row < rootNode.getChildCount(); row++) {
        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) rootNode.getChildAt(row);
        Object selectedObject = selectedNode.getUserObject();
        if (peakLists.contains(selectedObject)) {
            selectedNodes.add(selectedNode);
        }
    }
    // Get the index of the first selected item
    final ArrayList<Integer> positions = new ArrayList<Integer>();
    for (DefaultMutableTreeNode node : selectedNodes) {
        int nodeIndex = rootNode.getIndex(node);
        if (nodeIndex != -1)
            positions.add(nodeIndex);
    }
    if (positions.isEmpty())
        return ExitCode.ERROR;
    int insertPosition = Collections.min(positions);
    // Sort the feature lists by name
    Collections.sort(selectedNodes, new Comparator<DefaultMutableTreeNode>() {

        @Override
        public int compare(DefaultMutableTreeNode o1, DefaultMutableTreeNode o2) {
            return o1.getUserObject().toString().compareTo(o2.getUserObject().toString());
        }
    });
    // Reorder the nodes in the tree model
    for (DefaultMutableTreeNode node : selectedNodes) {
        model.removeNodeFromParent(node);
        model.insertNodeInto(node, rootNode, insertPosition);
        insertPosition++;
    }
    return ExitCode.OK;
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ArrayList(java.util.ArrayList) ProjectTree(net.sf.mzmine.desktop.impl.projecttree.ProjectTree) PeakListTreeModel(net.sf.mzmine.desktop.impl.projecttree.PeakListTreeModel) MainWindow(net.sf.mzmine.desktop.impl.MainWindow) MSDKRuntimeException(io.github.msdk.MSDKRuntimeException) PeakList(net.sf.mzmine.datamodel.PeakList) MZmineProjectImpl(net.sf.mzmine.project.impl.MZmineProjectImpl) Nonnull(javax.annotation.Nonnull)

Aggregations

MainWindow (net.sf.mzmine.desktop.impl.MainWindow)5 ProjectTree (net.sf.mzmine.desktop.impl.projecttree.ProjectTree)3 MZmineProjectImpl (net.sf.mzmine.project.impl.MZmineProjectImpl)3 MSDKRuntimeException (io.github.msdk.MSDKRuntimeException)2 File (java.io.File)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Nonnull (javax.annotation.Nonnull)2 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)2 RawDataFile (net.sf.mzmine.datamodel.RawDataFile)2 FileOutputStream (java.io.FileOutputStream)1 Locale (java.util.Locale)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 PeakList (net.sf.mzmine.datamodel.PeakList)1 Desktop (net.sf.mzmine.desktop.Desktop)1 HeadLessDesktop (net.sf.mzmine.desktop.impl.HeadLessDesktop)1 PeakListTreeModel (net.sf.mzmine.desktop.impl.projecttree.PeakListTreeModel)1 RawDataTreeModel (net.sf.mzmine.desktop.impl.projecttree.RawDataTreeModel)1 MZmineConfigurationImpl (net.sf.mzmine.main.impl.MZmineConfigurationImpl)1 MZmineModule (net.sf.mzmine.modules.MZmineModule)1