fr.lip6.move.coloane.core.ui.actions.CurveAction.java Source code

Java tutorial

Introduction

Here is the source code for fr.lip6.move.coloane.core.ui.actions.CurveAction.java

Source

/**
 * Copyright (c) 2006-2010 MoVe - Laboratoire d'Informatique de Paris 6 (LIP6).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Jean-Baptiste VORON (LIP6) - Project Head / Initial contributor
 *   Clment DMOULINS (LIP6) - Project Manager
 *
 * Official contacts:
 *   coloane@lip6.fr
 *   http://coloane.lip6.fr
 */
package fr.lip6.move.coloane.core.ui.actions;

import fr.lip6.move.coloane.core.main.Coloane;
import fr.lip6.move.coloane.core.ui.commands.ArcChangeCurveCmd;
import fr.lip6.move.coloane.core.ui.editpart.ArcEditPart;
import fr.lip6.move.coloane.interfaces.model.IArc;

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

import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.ui.actions.SelectionAction;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IWorkbenchPart;

/**
 * Action that change the curvature status of an arc.<br>
 * The arc must be selected.
 *
 * @author Jean-Baptiste Voron
 */
public class CurveAction extends SelectionAction {

    /** Action ID */
    public static final String ID = "arc.curve"; //$NON-NLS-1$

    /**
     * Constructor
     * @param part The workbench where come from the action
     */
    public CurveAction(IWorkbenchPart part) {
        super(part);
        setLazyEnablementCalculation(false);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected final void init() {
        setId(ID);
        setText("Curve / Straight"); //$NON-NLS-1$
        setToolTipText("Curve or Straight arcs"); //$NON-NLS-1$

        // Image associated to the action
        ImageDescriptor icon = ImageDescriptor.createFromFile(Coloane.class, "/resources/icons/curve.png"); //$NON-NLS-1$
        if (icon != null) {
            setImageDescriptor(icon);
        }
        setEnabled(false);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected final boolean calculateEnabled() {
        List<IArc> selectedNodes = this.getSelectedNode();

        // Must check whether nodes are selected or not
        if (selectedNodes == null) {
            return false;
        }

        return (this.getSelectedNode().size() > 0);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final void run() {
        CompoundCommand commandsGroup = new CompoundCommand();
        List<IArc> selectedNodes = this.getSelectedNode();

        // Check whether the current selection is empty or not
        if (selectedNodes == null) {
            return;
        }

        for (IArc arc : selectedNodes) {
            commandsGroup.add(new ArcChangeCurveCmd(arc));
        }
        execute(commandsGroup);
    }

    /**
     * Get arcs from the current selection.
     * @return A list of {@link IArc} or <code>null</code> if the selection is empty
     */
    @SuppressWarnings("unchecked")
    private List<IArc> getSelectedNode() {
        List<IArc> selectedArcs = new ArrayList<IArc>();
        List<Object> objects = getSelectedObjects();

        // If the current selection is empty... return !
        if (objects.isEmpty()) {
            return null;
        }

        // All selected objects must be arcs
        for (Object object : objects) {
            if (!(object instanceof ArcEditPart)) {
                continue;
            }
            ArcEditPart part = (ArcEditPart) object;
            selectedArcs.add((IArc) part.getModel());
        }
        return selectedArcs;
    }
}