com.archimatetool.editor.diagram.actions.SelectAllAction.java Source code

Java tutorial

Introduction

Here is the source code for com.archimatetool.editor.diagram.actions.SelectAllAction.java

Source

/**
 * This program and the accompanying materials
 * are made available under the terms of the License
 * which accompanies this distribution in the file LICENSE.txt
 */
package com.archimatetool.editor.diagram.actions;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.actions.ActionFactory;

/**
 * SelectAllAction including connections
 * 
 * @author Phillip Beauvoir
 */
public class SelectAllAction extends Action {

    private IWorkbenchPart part;

    /**
     * Constructs a <code>SelectAllAction</code> and associates it with the
     * given part.
     * 
     * @param part The workbench part associated with this SelectAllAction
     */
    public SelectAllAction(IWorkbenchPart part) {
        this.part = part;
        setText(Messages.SelectAllAction_0);
        setToolTipText(Messages.SelectAllAction_1);
        setId(ActionFactory.SELECT_ALL.getId());
    }

    /**
     * Selects all edit parts and connections in the active workbench part.
     */
    @Override
    public void run() {
        GraphicalViewer viewer = (GraphicalViewer) part.getAdapter(GraphicalViewer.class);
        if (viewer != null) {
            viewer.setSelection(new StructuredSelection(getSelectableEditParts(viewer.getContents()).toArray()));
        }
    }

    /**
     * Retrieves edit parts which can be selected and their connections
     * 
     * @param editpart from which the edit parts are to be retrieved
     * @return list of selectable EditParts
     */
    List<GraphicalEditPart> getSelectableEditParts(EditPart editpart) {
        List<GraphicalEditPart> selected = new ArrayList<GraphicalEditPart>();

        for (Object child : editpart.getChildren()) {
            if (child instanceof GraphicalEditPart) {
                GraphicalEditPart childPart = (GraphicalEditPart) child;

                if (childPart.isSelectable()) {
                    selected.add(childPart);

                    // Add connections if selectable (only need to get source connections, not target)
                    for (Object o : childPart.getSourceConnections()) {
                        GraphicalEditPart connectionEditPart = (GraphicalEditPart) o;
                        if (connectionEditPart.isSelectable()) {
                            selected.add(connectionEditPart);
                        }
                    }

                    // Recurse into children
                    selected.addAll(getSelectableEditParts(childPart));
                }
            }
        }

        return selected;
    }
}