/******************************************************************************* * Copyright 2017 Capital One Services, LLC and Bitwise, Inc. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *******************************************************************************/ package hydrograph.ui.graph.action; import java.util.List; import org.eclipse.gef.EditPart; import org.eclipse.gef.commands.Command; import org.eclipse.gef.ui.actions.SelectionAction; import org.eclipse.jface.action.IAction; import org.eclipse.ui.ISharedImages; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.actions.ActionFactory; import hydrograph.ui.graph.command.ComponentCutCommand; import hydrograph.ui.graph.controller.CommentBoxEditPart; import hydrograph.ui.graph.controller.ComponentEditPart; import hydrograph.ui.graph.model.CommentBox; import hydrograph.ui.graph.model.Component; /** * The Class CutAction. * * @author Bitwise */ public class CutAction extends SelectionAction{ PasteAction pasteAction; /** * Instantiates a new cut action. * * @param part * the part * @param action * the action */ public CutAction(IWorkbenchPart part, IAction action) { super(part); this.pasteAction = (PasteAction) action; setLazyEnablementCalculation(true); } @Override protected void init() { super.init(); ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages(); setText("Cut"); setId(ActionFactory.CUT.getId()); setHoverImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_CUT)); setImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_CUT)); setDisabledImageDescriptor(sharedImages.getImageDescriptor(ISharedImages.IMG_TOOL_CUT_DISABLED)); setEnabled(false); } private Command createCutCommand(List<Object> selectedObjects) { ComponentCutCommand cutCommand =new ComponentCutCommand(); if (selectedObjects == null || selectedObjects.isEmpty()) { return null; } Object node = null; boolean enabled=false; for(Object obj:selectedObjects) { if(obj instanceof ComponentEditPart || obj instanceof CommentBoxEditPart) { enabled=true; break; } } if(enabled) { for(Object obj:selectedObjects) { if (obj instanceof ComponentEditPart) { node = (Component) ((EditPart)obj).getModel(); cutCommand.addElement(node); } else if(obj instanceof CommentBoxEditPart){ node = (CommentBox) ((EditPart)obj).getModel(); cutCommand.addElement(node); } } return cutCommand; } else return null; } @Override protected boolean calculateEnabled() { Command cmd = createCutCommand(getSelectedObjects()); if (cmd == null){ ContributionItemManager.CUT.setEnable(false); return false; }else{ ContributionItemManager.CUT.setEnable(true); return true; } } @Override public void run() { execute(createCutCommand(getSelectedObjects())); pasteAction.setPasteCounter(1); pasteAction.update(); } }