📄 selection.java
字号:
/**
* $Id:Selection.java $
*
* Copyright 2004 ~ 2005 JingFei International Cooperation LTD. All rights reserved. *
*/
package com.jfimagine.jfdraw.draw;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.awt.RenderingHints;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import com.jfimagine.jfgraph.shape.base.ObjectList;
import com.jfimagine.jfgraph.shape.base.AbstractObject;
import com.jfimagine.jfgraph.shape.base.AbstractShape;
import com.jfimagine.jfgraph.shape.base.Node;
import com.jfimagine.jfgraph.shape.base.Port;
import com.jfimagine.jfgraph.shape.base.Label;
import com.jfimagine.jfgraph.shape.decorate.Arrow;
import com.jfimagine.jfgraph.shape.decorate.LineFormat;
import com.jfimagine.jfgraph.shape.decorate.FillFormat;
import com.jfimagine.jfgraph.shape.decorate.FontFormat;
import com.jfimagine.jfdraw.gui.dialog.ShapeSettingDialog;
import com.jfimagine.jfdraw.gui.resource.CADResource;
import com.jfimagine.jfgraph.geom.Angle;
import com.jfimagine.jfgraph.geom.JFPoint;
import com.jfimagine.jfgraph.geom.Rect;
import com.jfimagine.jfdraw.gui.dialog.TextDialog;
import com.jfimagine.jfdraw.gui.DrawCanvas;
import com.jfimagine.jfgraph.shape.action.JFOperationManager;
import com.jfimagine.jfgraph.shape.decorate.GridFormat;
import com.jfimagine.jfdraw.gui.GlobalSettings;
import com.jfimagine.jfdraw.gui.dialog.RotateSetupDialog;
/**
* Selection class. Used as a collection if user 'picked-up' some shapes.
*
* @author CookieMaker
*
* @version $Revision: 1.00 $
*/
public class Selection {
/**
* An object List to encapsulate shape objects.
*/
private ObjectList m_objectList = new ObjectList();
/**
* Original center of all objects selected.
*/
private JFPoint m_originalCenter = new JFPoint();
/**
* Original point that mouse clicked.
*/
private JFPoint m_originalPoint = new JFPoint();
/**
* Last point that mouse dragged.
*/
private JFPoint m_lastPoint = new JFPoint();
/** current dragging node of current shape.*/
private Node m_draggingNode;
/** current moveing label of current shape.*/
private Label m_moveLabel;
/** When dragging node of current shape, if a relational port is also dragged.
*/
private boolean m_draggingPortAlso=false;
/**
* An AlignShapes object used to align shapes in selection.
*/
private AlignShapes m_alignShapes;
/** A Draw canvas reference*/
private DrawCanvas m_drawCanvas;
/**
* Constructor for ObjectList
*/
public Selection(DrawCanvas drawCanvas){
m_drawCanvas =drawCanvas;
m_alignShapes =new AlignShapes(drawCanvas.getOperationManager());
}
/**
* Get an objectList encapsulates shape Objects
*
* @return An object list.
*
*/
public ObjectList getList(){
return m_objectList;
}
/**
* If this selection has already contains such an object.
*
* @param obj A new AbstractObject.
*
* @return If obj is already inside the selection, then return true,otherwise false.
*
*/
public boolean contains(AbstractObject obj){
return m_objectList.contains(obj);
}
/**
* Add a new object at the end of the list.
*
* @param obj A new AbstractObject.
*
* @return The index the object located in the list.
*
*/
public int add(AbstractObject obj){
try{
if (obj==null)
return -1;
if (obj instanceof ObjectList){
return add(((ObjectList)obj).getList());
}
if (!contains(obj))
return m_objectList.add(obj);
else
return -1;
}catch(Exception e){
return -1;
}
}
/**
* Add an object list at the end of the list.
*
* @param objList A new AbstractObject list.
*
* @return The number of added.
*
*/
public int add(List objList){
int num=0;
try{
Iterator it =objList.iterator();
while (it!=null && it.hasNext()){
AbstractObject obj =(AbstractObject)it.next();
if (!contains(obj)){
m_objectList.add(obj);
num++;
}
}
return num;
}catch(Exception e){
return -1;
}
}
/**
* Remove an object in object list.
*
* @param obj An object to be removed.
*
*/
public void remove(AbstractObject obj){
try{
if (obj!=null)
m_objectList.removeByObjectId(obj.getObjectId());
}catch(Exception e){
}
}
/**
* Clear all objects in list.
*
*/
public void clear(){
try{
m_objectList.clear();
m_draggingNode =null;
m_moveLabel =null;
}catch(Exception e){
}
}
/**
* Get the number of AbstractObject in list.
*
* @return The number of AbstractObject.
*
*/
public int size(){
return m_objectList.size();
}
/**
* Get the bounds of this selection.
*
* @return The bounds rectangle of current selection.
*
*/
public Rect getBounds(){
return getBounds(false);
}
/**
* Get the bounds of this selection.
* @param avoidUnrotatable To avoid caculating the bounds of unrotatable objects.
* @return The bounds rectangle of current selection.
*
*/
public Rect getBounds(boolean avoidUnrotatable){
double minx=10000,miny=10000,maxx=0,maxy=0;
AbstractObject obj;
Iterator it=m_objectList.getList().iterator();
while (it!=null && it.hasNext()){
obj =(AbstractObject)it.next();
if (obj!=null && obj instanceof AbstractShape){
AbstractShape shapeObj =(AbstractShape)obj;
if (avoidUnrotatable && !shapeObj.isRotatable())
continue;
Rect bounds =(shapeObj).getBounds();
minx =Math.min(minx,bounds.getX());
miny =Math.min(miny,bounds.getY());
maxx =Math.max(maxx,bounds.getX()+bounds.getWidth());
maxy =Math.max(maxy,bounds.getY()+bounds.getHeight());
}
}
if (minx>maxx) minx=maxx;
if (miny>maxy) miny=maxy;
return new Rect(minx,miny,maxx-minx,maxy-miny);
}
/**
* Get original point.
*
* @return An original point.
*
*/
public JFPoint getOriginalPoint(){
return m_originalPoint;
}
/**
* Get original center.
*
* @return An original center point.
*
*/
public JFPoint getOriginalCenter(){
return m_originalCenter;
}
/**
* Set original center point by currently selected objects.
*
*/
public void setOriginalPoint(){
m_originalCenter =getBounds(true).getCenter();
}
/**
* Set original point.
*
* @param pnt A new original point.
*
*/
public void setOriginalPoint(JFPoint pnt){
m_originalPoint.setValue(pnt);
m_lastPoint.setValue(pnt);
setOriginalPoint();
}
/**
* Get last point.
*
* @return An last point.
*
*/
public JFPoint getLastPoint(){
return m_lastPoint;
}
/**
* Set last point.
*
* @param pnt A new last point.
*
*/
public void setLastPoint(JFPoint pnt){
m_lastPoint.setValue(pnt);
}
/** get current dragging node of current shape.
* @return Current dragging node.
*/
public Node getDraggingNode(){
return m_draggingNode;
}
/** set current dragging node of current shape.
* @param draggingNode A new dragging node of current shape.
*/
public void setDraggingNode(Node draggingNode){
m_draggingNode =draggingNode;
if (draggingNode==null)
m_draggingPortAlso =false;
else{
Port port =pickPort(draggingNode.getXOffset(),draggingNode.getYOffset());
if (port!=null)
m_draggingPortAlso =true;
else
m_draggingPortAlso =false;
}
}
/** get current moving label of current shape.
* @return Current moving label.
*/
public Label getMoveLabel(){
return m_moveLabel;
}
/** set current moving label of current shape.
* @param moveLabel Current moving label.
*/
public void setMoveLabel(Label moveLabel){
if (moveLabel==null || !moveLabel.getMovable())
m_moveLabel=null;
else
m_moveLabel =moveLabel;
}
/** When dragging node of current shape, if a relational port is also dragged.
*/
public boolean isDraggingPortAlso(){
return m_draggingPortAlso;
}
/**
* Add a new port to this selection.
* @param x,y Current picking position.
*
*/
public Port addPort(double x,double y){
AbstractObject obj;
Iterator it=m_objectList.getList().iterator();
while (it!=null && it.hasNext()){
obj =(AbstractObject)it.next();
if (obj!=null && obj instanceof AbstractShape){
Port port =((AbstractShape)obj).addPort(x,y);
if (port!=null)
return port;
}
}
return null;
}
/**
* Pick a port that is on the selection, according to the current x,y position
* @param x,y Current picking position.
*
*/
public Port pickPort(double x,double y){
AbstractObject obj;
Iterator it=m_objectList.getList().iterator();
while (it!=null && it.hasNext()){
obj =(AbstractObject)it.next();
if (obj!=null && obj instanceof AbstractShape){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -