⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 selection.java

📁 用Java开发的、实现类似Visio功能的应用程序源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
   	return null;
   }


   /**
    *   Get which shape in the selection that intersects with point pnt.
    * 
    *   @param  pnt A JFPoint used to test intersection.
    *
    *   @return A shape in the selection that intersects with the point.
    *
    */ 	
   public AbstractObject intersects(JFPoint pnt){
   	AbstractObject obj;
   	Iterator it=m_objectList.getList().iterator();
   	while (it!=null && it.hasNext()){
   		obj	=(AbstractObject)it.next();
   		if (obj!=null && obj instanceof AbstractShape){
   			AbstractShape shape	=(AbstractShape)obj;
   			if (shape.intersects(pnt))
   				return shape;
   		}
   	}
   	return null;
   }



  /**
    *   Drag current node and draw its relational lines.
    * 
    *   @param x, y A new position.
    *
    *   @param g	Graphics context to draw selections.
    *
    */ 	
  public void dragNode(double x, double y,Graphics g){
	AbstractShape drawObj	=getDraggingObj();
	if (drawObj==null) return;

  	Graphics2D g2	=(Graphics2D)g;
 	g2.setXORMode(Color.white);


 	//if dragged already, then re-draw it to eliminate the xor shape.
  	if (!m_lastPoint.equals(m_originalPoint,true)){
  		drawObj.moveNode(m_draggingNode,m_draggingNode.getXOffset(),m_draggingNode.getYOffset(),g);
  	}
	
	//draw a new xor shape of selection.  	
	//avoid drawing on the original position.
  	if (!m_originalPoint.equals(x,y,true)){
   	      drawObj.moveNode(m_draggingNode,x,y,g);
   	}

	//save the new position.
  	m_lastPoint.setX(x);
  	m_lastPoint.setY(y);
  }

  //current rotate position:
  JFPoint  		m_rotatePos		=new JFPoint();
  //zero angle start position
  JFPoint  		m_zeroAnglePos		=new JFPoint();
  GlobalSettings	m_settings		=GlobalSettings.getInstance();
  /**
   *  when rotate 
   */
  private JFPoint getRotatePosition(double x, double y){
  	m_rotatePos.setValue(x,y);
  	
  	if (m_settings.getRotateType()==RotateSetupDialog.ROTATE_ANYANYGLE)
  		return m_rotatePos;
  	
  	//a default zero angle position
  	m_zeroAnglePos.setValue(m_originalCenter.getX()+100,m_originalCenter.getY());
  	//current specified rotate angle, in degree
  	double rotateAngle	=m_settings.getRotateAngle();
  	if (rotateAngle<1)
  		rotateAngle	=1;


	//-----------get a rotated angle, then adjust it to meet the multiple of rotateAngle--------
	
	//original rotate position angle,in radian.
	double originalAngle	=Angle.getAngle(m_originalCenter,m_zeroAnglePos,m_originalPoint,true);
	//current rotate position angle,in radian
	double newAngle		=Angle.getAngle(m_originalCenter,m_zeroAnglePos,m_rotatePos,true);
	
	//angle rotated, in degree
	double angleRotated	=(newAngle - originalAngle)/Angle.PI * 180;

	
	//rotatation multiples to minimum rotateAngle
	int times		=(int)Math.abs(angleRotated/rotateAngle);
	if (times==0){
		m_rotatePos.setValue(m_originalPoint);
		return m_rotatePos;
	}
		
		
	//new angle adjusted, in degree
	double newAngleRotated	=times * rotateAngle;
	if (angleRotated<0)
		newAngleRotated	=-1 * newAngleRotated;

	
	//new angle adjusted, in radian
	newAngleRotated		=newAngleRotated * Angle.PI/ 180.0;


	//-----------to get a new angle--------
	
	//final new angle after plus the original angle, in radian
	double  finalNewAngle	=originalAngle + newAngleRotated;


	//-----------to calculate a new VIRTUAL position for current mouse--------
	
	//distance from center to current mouse position
	double radius		=m_originalCenter.distance(m_rotatePos);
	
	double xOffset		=radius * Math.cos(finalNewAngle);
	double yOffset		=radius * Math.sin(finalNewAngle);
	
	m_rotatePos.setValue(m_originalCenter.getX()+xOffset, m_originalCenter.getY()+yOffset);

	return m_rotatePos;  	
  }

  /**
    *   Rotate current objects by moving a rotating point.
    * 
    *   @param x,&nbsp;y A new position.
    *
    *   @param g	Graphics context to draw selections.
    *
    */ 	
  public void rotateObjects(double x, double y,Graphics g){
  	
  	//adjust the current rotate pos first
  	JFPoint rotatePos	=getRotatePosition(x,y);
  	x	=rotatePos.getX();
  	y	=rotatePos.getY();
  	
  	//then rotate the objects
  	Graphics2D g2	=(Graphics2D)g;
 	g2.setXORMode(Color.white);
	
 	//if rotated already, then re-draw it to eliminate the xor shape.
  	if (!m_lastPoint.equals(m_originalPoint)){
  		draw(g,true);
  	}
	
	//rotate all objects in selection
   	AbstractObject obj;
   	int i=0;
   	Iterator it=m_objectList.getList().iterator();
   	while (it!=null && it.hasNext()){
   		obj	=(AbstractObject)it.next();
   		if (obj!=null && obj instanceof AbstractShape){
   			AbstractShape shape	=(AbstractShape)obj;
   			if (!shape.isDisableMotion()){
   				shape.rotateNode(m_lastPoint,m_originalCenter,x,y,g);
   			}
   		}
   	}
   	draw(g,true);  	
	
	//save the new position.
  	m_lastPoint.setX(x);
  	m_lastPoint.setY(y);
  }
  
  /**
   * Align all the shapes in this list.
   */
  public void alignObjects(int ALIGNMENTTYPE){
  	
  	m_alignShapes.setList(m_objectList);
  	
  	switch (ALIGNMENTTYPE){
  	   	/** align left.*/
   		case AlignShapes.ALIGNMENTTYPE_LEFT:
   			m_alignShapes.alignToLeft();
   			break;

   		/** align right.*/
   		case AlignShapes.ALIGNMENTTYPE_RIGHT:
   			m_alignShapes.alignToRight();
   			break;

   		/** align top.*/
   		case AlignShapes.ALIGNMENTTYPE_TOP:
   			m_alignShapes.alignToTop();
   			break;

   		/** align bottom.*/
   		case AlignShapes.ALIGNMENTTYPE_BOTTOM:
   			m_alignShapes.alignToBottom();
   			break;

   		/** align center.*/
   		case AlignShapes.ALIGNMENTTYPE_CENTER:
   			m_alignShapes.alignToCenter();
   			break;

   		/** align justify horizontal.*/
   		case AlignShapes.ALIGNMENTTYPE_JUSTIFY_HORIZONTAL:
   			m_alignShapes.alignToHorizontalJustify();
   			break;

   		/** align justify vertical.*/
   		case AlignShapes.ALIGNMENTTYPE_JUSTIFY_VERTICAL:
   			m_alignShapes.alignToVerticalJustify();
   			break;

   		/** align horizontal proportional spacing.*/
   		case AlignShapes.ALIGNMENTTYPE_PROPORTIONAL_SPACING_HORIZONTAL:
   			m_alignShapes.alignToHorizontalProportionalSpacing();
   			break;

   		/** align vertical proportional spacing.*/
   		case AlignShapes.ALIGNMENTTYPE_PROPORTIONAL_SPACING_VERTICAL:
   			m_alignShapes.alignToVerticalProportionalSpacing();
   			break;

   	}    
   	
	m_drawCanvas.getDrawPage().clearAccessTimes();
	moveRelationalPorts();
  }  


  /**
    *   finish dragging current node and draw its relational lines.
    * 
    *   @param x,&nbsp;y A new position.
    *
    *   @param g	Graphics context to draw selections.
    *
    */ 	
  public void finishDragNode(double x, double y,Graphics g){
	AbstractShape drawObj	=getDraggingObj();
	if (drawObj==null) return;
	
	//draw a new xor shape of selection.  	
  	drawObj.finishMoveNode(m_draggingNode,x,y,null);
  }


  /**
    *   Move all the relational ports of this selection.
    *
    */ 	
  public void moveRelationalPorts(){
        ObjectList movedList    =new ObjectList();
        
        //firstly add rotatable object into movedList from selection.
        Iterator it     =m_objectList.getList().iterator();
        while (it!=null && it.hasNext()){
                AbstractShape drawObj        =(AbstractShape)it.next();
                //if (drawObj.isRotatable())
                //if (!drawObj.isOpenShape())
                       try{
                         movedList.add(drawObj);
                       }catch(Exception e){
                        }
        }

        
        //move all relational ports of this selection.
	it	=m_objectList.getList().iterator();
	while (it!=null && it.hasNext()){		
		AbstractShape thisShape	=(AbstractShape)it.next();	
		thisShape.moveRelationalPorts(movedList);
	}
        
        unboundBrokenPorts();
  }

   /**
    *   unbound broken ports
    */ 	
  private void unboundBrokenPorts(){

	Iterator it	=m_objectList.getList().iterator();
	while (it!=null && it.hasNext()){		
		AbstractShape thisShape	=(AbstractShape)it.next();	
		thisShape.unboundBrokenPorts();
	}

  }



  /**
    *   Modify label string of one object.
    *   @param c A container that used to place modifier objects.
    *   @param manager An opeation manager used to register the text's change.
    *   @return true if label changed, false otherwise.
    */ 	
  public boolean modifyLabel(java.awt.Container c,JFOperationManager manager){
	Object obj	=getFirstObj();
	if (obj==null || !(obj instanceof AbstractShape)) return false;

        //one object in select was disabled modifying properties
        List l	=new ArrayList();
        l.add(obj);
    	if (ShapeSettingDialog.getDisableModifyingProperties(l,true)){
 		JOptionPane.showMessageDialog(null, CADResource.getString("dialog.shapesetting.modificationdisabled"), CADResource.getString("sys.warn"), JOptionPane.ERROR_MESSAGE); 
    		return false;      
    	}
	
	//modify text	
	AbstractShape aShape	=(AbstractShape)obj;	        
        String oldText =aShape.getLabel().getText();
        String newText =TextDialog.getNewText(c,oldText); 
        aShape.getLabel().setText(newText);
        
        //register modifying operation
        manager.addModifyText(aShape,oldText,newText);
        
        /**replace the selection as current single object */
        try{
		m_objectList = new ObjectList();
		m_objectList.add(aShape);
	}catch(Exception e){
	}
	
        return !(oldText.equals(newText));
  }
 
   

 
   /**
    *   Convert this list to String 
    * 
    *   @return  An string represents the content of current list
    *
    */ 	
   public String toString(){
   	return m_objectList.toString();
   }



   /**
    *   Draw current object on graphic canvas.
    * 
    *   @param  g  A graphic canvas.	
    *   @param  isXorMode If is in xor mode now.
    *
    */ 	
   public void  draw(Graphics g,boolean isXorMode){
   	
  	
   	AbstractObject obj;
   	Iterator it=m_objectList.getList().iterator();
   	while (it!=null && it.hasNext()){
   		obj	=(AbstractObject)it.next();
   		if (obj!=null && obj instanceof AbstractShape){
   			((AbstractShape)obj).draw(g,isXorMode);
   		}
   	}
   }

   /**
    *   Draw picked state of current object on graphic canvas.
    * 
    *   @param g A graphic canvas.
    *   @param ifRotate If user's operation or other actions force objects to be rotated.
    *
    */  	
   public void drawPicked(Graphics g, boolean ifRotate){
   	AbstractObject obj;
   	Iterator it=m_objectList.getList().iterator();
   	while (it!=null && it.hasNext()){
   		obj	=(AbstractObject)it.next();
   		if (obj!=null && obj instanceof AbstractShape){
   			((AbstractShape)obj).drawPicked(g,ifRotate);
   		}
   	}
   }
                         
   
   /**
    *  if current selection has quadrant
    */
   public boolean hasQuadrant(){
	//if a quadrant is in the list, raise an 'cannot add quadrant to library' error        			
        ObjectList list	=getList();
        Iterator it	=list.getList().iterator();
        while (it!=null && it.hasNext()){
        	Object obj	=it.next();
        	if (obj instanceof com.jfimagine.jfgraph.shape.line.JFQuadrant){
        		return true;
        	}
        }  
        return false;
   }
 	
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -