📄 nodecreatecommand.java
字号:
/*******************************************************************************
* $Header: /cvsroot/EOS6/work_dir/niegy/com.primeton.studio.gef.ui/src/com/primeton/studio/gef/ui/commands/NodeCreateCommand.java,v 1.8 2006/12/30 02:45:04 niegy Exp $
* $Revision: 1.8 $
* $Date: 2006/12/30 02:45:04 $
*
*==============================================================================
*
* Copyright (c) 2001-2006 Primeton Technologies, Ltd.
* All rights reserved.
*
* Created on 2006-9-28
*******************************************************************************/
package com.primeton.studio.gef.ui.commands;
import java.util.Iterator;
import java.util.List;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.CreateRequest;
import com.primeton.studio.gef.core.Node;
import com.primeton.studio.gef.core.CoreFactory;
import com.primeton.studio.gef.core.Diagram;
import com.primeton.studio.gef.core.Location;
import com.primeton.studio.gef.core.ModelManager;
import com.primeton.studio.gef.core.NodeElement;
import com.primeton.studio.gef.core.NodeElementLabel;
import com.primeton.studio.gef.core.Note;
import com.primeton.studio.gef.core.Size;
import com.primeton.studio.gef.core.TNodeType;
import com.primeton.studio.gef.ui.Constants;
import com.primeton.studio.gef.ui.parts.ExtenderReader;
/**
* TODO此处填写 class 信息
*
* @author niegy (mailto:niegy@primeton.com)
*/
/*
* 修改历史
* $Log: NodeCreateCommand.java,v $
* Revision 1.8 2006/12/30 02:45:04 niegy
* 重构代码
*
* Revision 1.7 2006/12/12 08:26:39 niegy
* 重构代码,实现一般图元的编辑框
*
* Revision 1.6 2006/12/11 01:42:13 niegy
* 修改addAttriAndOper
*
* Revision 1.5 2006/12/08 07:04:32 niegy
* 重构代码
*
* Revision 1.4 2006/12/05 05:18:59 niegy
* 修改模型,增加连线的扩展点
*
* Revision 1.3 2006/11/22 07:07:38 niegy
* 增加table
*
* Revision 1.2 2006/11/18 12:13:53 niegy
* 增加容器模型
*
* Revision 1.1 2006/11/17 03:15:13 niegy
* create
*
*/
public class NodeCreateCommand extends Command {
/** The new shape. */
protected NodeElement newNode;
/** ShapeDiagram to add to. */
protected final Node parent;
/** A request to create a new Shape. */
private final CreateRequest request;
/** True, if newNode was added to parent. */
protected boolean nodeAdded;
private Rectangle rectangle;
private String pluginId;
private boolean isCreateNewNode = false;
private NodeElementLabel label = null;
/**
* Create a command that will add a new Shape to a ShapesDiagram.
* @param parent the ShapesDiagram that will hold the new element
* @param req a request to create a new Shape
* @throws IllegalArgumentException if any parameter is null, or the request
* does not provide a new Shape instance
*/
public NodeCreateCommand(Node parent, CreateRequest req, Rectangle rectangle) {
if (parent == null || req == null || !(req.getNewObject() instanceof NodeElement)) {
throw new IllegalArgumentException();
}
this.parent = parent;
this.request = req;
this.rectangle = rectangle;
setLabel("node creation");
}
public NodeCreateCommand(Node parent, CreateRequest req, Rectangle rectangle,String pluginId) {
this(parent,req,rectangle);
this.pluginId = pluginId;
}
public NodeCreateCommand(Node parent, CreateRequest req, Rectangle rectangle,String pluginId,boolean isCreateNewNode) {
this(parent,req,rectangle,pluginId);
this.isCreateNewNode = isCreateNewNode;
}
public NodeCreateCommand(String name,Node parent) {
super(name);
this.parent = parent;
this.request = null;
this.rectangle = null;
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#canUndo()
*/
public boolean canUndo() {
return nodeAdded;
}
public NodeElement getNode(){
if(newNode == null){
NodeElement nodeElement = (NodeElement)request.getType();
newNode = (NodeElement) request.getNewObject();
}
return newNode;
}
private void setSize(Size size){
if(newNode instanceof Note){
size.setHeight(Constants.NOTE_HEIGHT);
size.setWidth(Constants.NOTE_WIDTH);
return;
}
int type = newNode.getNodeType().getValue();
switch(type){
case TNodeType.COMMON:
size.setHeight(Constants.NODE_HEIGHT);
size.setWidth(Constants.NODE_WIDTH);
break;
case TNodeType.PARENT:
size.setHeight(Constants.PARENT_HEIGHT);
size.setWidth(Constants.PARENT_WIDTH);
break;
case TNodeType.CHILD:
size.setHeight(Constants.CHILD_HEIGHT);
size.setWidth(Constants.CHILD_WIDTH);
break;
case TNodeType.TABLE:
// size.setHeight(-1);
size.setWidth(-1);
break;
}
}
private void addAttriAndOper(){
if(newNode.getNodeType() == TNodeType.TABLE_LITERAL){
if(newNode.getNodes().size()==0){
NodeElement attriTemp = ExtenderReader.getInstance().getAtriNode(pluginId);
if(attriTemp != null){
NodeElement atriNodeclone = (NodeElement)EcoreUtil.copy(attriTemp);
newNode.getNodes().add(atriNodeclone);
}
NodeElement operTemp = ExtenderReader.getInstance().getOperNode(pluginId);
if(operTemp != null){
NodeElement operNodeclone = (NodeElement)EcoreUtil.copy(operTemp);
newNode.getNodes().add(operNodeclone);
}
}
}
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#execute()
*/
public void execute() {
iniAddInfo();
if(isCreateNewNode)
addAttriAndOper();
nodeAdded = parent.getNodes().add(newNode);
addNodeLabel();
}
private void addNodeLabel(){
int type = newNode.getNodeType().getValue();
switch(type){
case TNodeType.COMMON:
label = CoreFactory.eINSTANCE.createNodeElementLabel();
Location loc = newNode.getLocation();
Location newLoc = CoreFactory.eINSTANCE.createLocation();
newLoc.setX(loc.getX());
newLoc.setY(loc.getY()+40);
label.setLocation(newLoc);
label.setNodeType(TNodeType.LABEL_LITERAL);
label.setNode(newNode);
label.setId("label");
label.setName("label");
newNode.setNodeLabel(label);
parent.getNodes().add(label);
break;
}
}
private void delNodelabel(){
if(label != null){
newNode.setNodeLabel(null);
parent.getNodes().remove(label);
}
}
protected void iniAddInfo(){
// Obtain the new Shape instance from the request.
// This causes the factory stored in the request to create a new instance.
// The factory is supplied in the palette-tool-entry, see
// DataFlowEditorPaletteFactory#createComponentsGroup()
if(newNode == null)
newNode = (NodeElement) request.getNewObject();
// Get desired location and size from the request
Location loc = CoreFactory.eINSTANCE.createLocation();//ModelManager.getCoreFactory().createLocation();
Size size = CoreFactory.eINSTANCE.createSize();
if(request.getSize() != null) { // we'll use default size if it's null
size.setHeight(request.getSize().height);
size.setWidth(request.getSize().width);
}
else
setSize(size);
if(rectangle != null){
loc.setX(rectangle.x);
loc.setY(rectangle.y);
newNode.setLocation(loc);
newNode.setSize(size);
}
if(request.getType() instanceof NodeElement){
NodeElement nodeElement= (NodeElement)request.getType();
newNode.setDescription(nodeElement.getDescription());
if(newNode.getName().trim().equals(""))
newNode.setName(nodeElement.getName());
}
String ndoeid = getNodeId(newNode.getId());
String nameid = getNodeName(newNode.getName());
newNode.setId(ndoeid);
newNode.setName(nameid);
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#redo()
*/
public void redo() {
nodeAdded = parent.getNodes().add(newNode);
addNodeLabel();
}
protected String getNodeName(String nodeName){
for (int i = 0; i < 10; i++)
nodeName = nodeName.replaceAll(String.valueOf(i),"");
int i= 0;
String name = nodeName;
while(true){
boolean flag = false;
List list = null;
if(parent instanceof Diagram)
list = ((Diagram)parent).getNodes();
else if(parent instanceof NodeElement)
list = ((NodeElement)parent).getNodes();
for (Iterator iter = list.iterator(); iter.hasNext();) {
NodeElement element = (NodeElement) iter.next();
if(element.getName() == null)continue;
if(element.getName().equals(name)){
flag = true;
break;
}
}
i++;
if(!flag)
return name;
name = nodeName+i;
}
}
protected String getNodeId(String nodeName){
int i= 0;
String name = null;
while(true){
List list = null;
if(parent instanceof Diagram)
list = ((Diagram)parent).getNodes();
else if(parent instanceof NodeElement)
list = ((NodeElement)parent).getNodes();
boolean flag = false;
name = nodeName+i;
for (Iterator iter = list.iterator(); iter.hasNext();) {
NodeElement element = (NodeElement) iter.next();
if(element.getId().equals(name)){
flag = true;
break;
}
}
i++;
if(!flag)
return name;
}
}
/* (non-Javadoc)
* @see org.eclipse.gef.commands.Command#undo()
*/
public void undo() {
nodeAdded = parent.getNodes().remove(newNode);
delNodelabel();
}
public boolean canExecute() {
boolean canExecute = super.canExecute();
return canExecute;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -