Search in sources :

Example 1 with ProjectTree

use of net.sf.mzmine.desktop.impl.projecttree.ProjectTree 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 ProjectTree

use of net.sf.mzmine.desktop.impl.projecttree.ProjectTree 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)

Example 3 with ProjectTree

use of net.sf.mzmine.desktop.impl.projecttree.ProjectTree 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)

Aggregations

MainWindow (net.sf.mzmine.desktop.impl.MainWindow)3 ProjectTree (net.sf.mzmine.desktop.impl.projecttree.ProjectTree)3 MSDKRuntimeException (io.github.msdk.MSDKRuntimeException)2 ArrayList (java.util.ArrayList)2 Nonnull (javax.annotation.Nonnull)2 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)2 MZmineProjectImpl (net.sf.mzmine.project.impl.MZmineProjectImpl)2 PeakList (net.sf.mzmine.datamodel.PeakList)1 RawDataFile (net.sf.mzmine.datamodel.RawDataFile)1 PeakListTreeModel (net.sf.mzmine.desktop.impl.projecttree.PeakListTreeModel)1 RawDataTreeModel (net.sf.mzmine.desktop.impl.projecttree.RawDataTreeModel)1