Back to index

wims  3.65+svn20090927
MoverTool.java
Go to the documentation of this file.
00001 package rene.zirkel.tools;
00002 
00003 
00004 import java.awt.*;
00005 import java.awt.event.*;
00006 import java.util.*;
00007 
00008 import rene.gui.Global;
00009 import rene.util.*;
00010 import rene.zirkel.*;
00011 import rene.zirkel.objects.*;
00012 import rene.zirkel.constructors.*;
00013 
00018 public class MoverTool extends ObjectConstructor
00019 {      ConstructionObject P;
00020        boolean Selected=false;
00021        boolean ShowsValue,ShowsName;
00022        boolean Grab;
00023        boolean WasDrawable,ChangedDrawable;
00024        MyVector V=null;
00025        double OldX,OldY;
00026        Graphics ZCG;
00027 
00028        public void mousePressed (MouseEvent e, ZirkelCanvas zc)
00029        {      if (!Selected && V!=null) zc.clearSelected();
00030               if (e.isControlDown() && V==null)
00031                      // force a fixed angle or fixed circle to become drawable!
00032               {      P=zc.selectObject(e.getX(),e.getY());
00033                      ChangedDrawable=false;
00034                      if (P instanceof FixedCircleObject &&
00035                             ((FixedCircleObject)P).fixedByNumber())
00036                      {      WasDrawable=((FixedCircleObject)P).isDragable();
00037                             ChangedDrawable=true;
00038                             ((FixedCircleObject)P).setDragable(true);
00039                      }
00040                      else if (P instanceof FixedAngleObject &&
00041                             ((FixedAngleObject)P).fixedByNumber())
00042                      {      WasDrawable=((FixedAngleObject)P).isDragable();
00043                             ChangedDrawable=true;
00044                             ((FixedAngleObject)P).setDragable(true);
00045                      }
00046                      else P=null;
00047               }
00048               if (P==null) // no forced moveable fixed circle or angle
00049               {      if (V!=null) // try to select another point 
00050                             P=zc.selectMoveablePoint(e.getX(),e.getY());
00051                      else // try to select any moveable object 
00052                             P=zc.selectMoveableObject(e.getX(),e.getY());
00053               }
00054               if (P!=null && V!=null) 
00055                      // Check, if we have that point already:
00056               {      Enumeration en=V.elements();
00057                      while (en.hasMoreElements())
00058                      {      if (P==en.nextElement()) // point found
00059                             {      if (e.isShiftDown()) P=null; 
00060                                           // selected a point twice, but want still more points 
00061                                    else V.removeElement(P);
00062                                           // remove the point from the list and start dragging
00063                                    break;
00064                             }
00065                      }
00066               }                    
00067               if (P!=null) // point is selected for movement
00068               {      P.setSelected(true);
00069                      ShowsName=P.showName();
00070                      ShowsValue=P.showValue();
00071                      zc.setCursor(new Cursor(Cursor.MOVE_CURSOR));
00072               }
00073               else // point was chosen from a list of objects
00074               {      Selected=(zc.findSelectedObject()!=null);
00075               }
00076               // Highlight all selected points:
00077               if (V!=null)
00078               {      Enumeration en=V.elements();
00079                      while (en.hasMoreElements())
00080                      {      ((PointObject)en.nextElement()).setSelected(true);
00081                      }
00082               }
00083               if (P!=null)
00084               {      if (P instanceof PointObject)
00085                      {      if (e.isShiftDown()) // want more points!
00086                             {      if (V==null) V=new MyVector();
00087                                    if (P!=null) V.addElement(P);
00088                                    P=null;
00089                             }
00090                             else if (e.isControlDown())
00091                                    // show current construction while moving
00092                             {      zc.grab(true);
00093                                    Grab=true;
00094                             }
00095                             else
00096                                    // remember old point position 
00097                             {      OldX=((PointObject)P).getX(); OldY=((PointObject)P).getY();
00098                             }
00099                      }
00100                      else if (P instanceof MoveableObject)
00101                             // call startDrag method of the object
00102                      {      ((MoveableObject)P).startDrag(
00103                                    zc.x(e.getX()),zc.y(e.getY()));
00104                             // System.out.println("start dragging "+P.getName());
00105                             V=null;
00106                      }
00107                      if (ZCG!=null)
00108                      {      ZCG.dispose();
00109                             ZCG=null;
00110                      }
00111                      ZCG=zc.getGraphics();
00112               }
00113               zc.repaint();
00114               showStatus(zc);
00115        }
00116        
00117        public void mouseMoved (MouseEvent e, ZirkelCanvas zc, boolean simple)
00118        {      zc.indicateMoveableObjects(e.getX(),e.getY(),e.isControlDown());
00119        }
00120        
00121        boolean IntersectionBecameInvalid=false;
00122        
00130        public void mouseDragged (MouseEvent e, ZirkelCanvas zc)
00131        {      if (P==null) return;
00132               // System.out.println("dragging "+P.getName());
00133               zc.getConstruction().haveChanged();
00134               zc.getConstruction().dontAlternate(false);
00135               synchronized (zc)
00136               {      double oldx=0,oldy=0;
00137                      if ((Global.getParameter("restrictedmove",false) || e.isShiftDown()) 
00138                                    && P instanceof PointObject)
00139                      {      if (IntersectionBecameInvalid)
00140                             {      zc.getConstruction().dontAlternate(true);
00141                                    IntersectionBecameInvalid=false;
00142                             }
00143                             oldx=((PointObject)P).getX(); oldy=((PointObject)P).getY();
00144                      }
00145                      ((MoveableObject)P).dragTo(zc.x(e.getX()),zc.y(e.getY()));
00146                      if ((Global.getParameter("options.movename",false) && !P.isFixed())
00147                             ||
00148                             (Global.getParameter("options.movefixname",true) && P.isFixed()))
00149                      {      P.setShowValue(true);
00150                             P.setShowName(true);
00151                      }
00152                      P.updateText();
00153                      if (V!=null && (P instanceof PointObject))
00154                      {      double dx=((PointObject)P).getX()-OldX,dy=((PointObject)P).getY()-OldY;
00155                             Enumeration en=V.elements();
00156                             while (en.hasMoreElements())
00157                             {      PointObject p=(PointObject)en.nextElement();
00158                                    p.move(p.getX()+dx,p.getY()+dy);
00159                                    p.updateText();
00160                             }
00161                             OldX=((PointObject)P).getX(); OldY=((PointObject)P).getY();
00162                      }
00163                      if (P instanceof PointObject)
00164                      {      PointObject Po=(PointObject)P;
00165                             if (Po.dependsOnItselfOnly())
00166                             {      Po.dontUpdate(true);
00167                                    zc.dovalidate();
00168                                    Po.dontUpdate(false);
00169                             }
00170                      }
00171                      zc.validate();
00172                      if ((Global.getParameter("restrictedmove",false) || e.isShiftDown()) 
00173                                    && P instanceof PointObject 
00174                                    && zc.getConstruction().intersectionBecameInvalid())
00175                      {      ((PointObject)P).dragTo(oldx,oldy);
00176                             IntersectionBecameInvalid=true;
00177                             zc.validate();
00178                      }
00179               }
00180               if (ZCG==null) ZCG=zc.getGraphics();
00181               zc.paint(ZCG);
00182        }
00183        
00190        public void mouseReleased (MouseEvent e, ZirkelCanvas zc, boolean rightmouse)
00191        {      if (P==null) return;
00192               if (ZCG!=null)
00193               {      ZCG.dispose();
00194                      ZCG=null;
00195               }      
00196               zc.setCursor(Cursor.getDefaultCursor());
00197               P.setSelected(false);
00198               P.setShowValue(ShowsValue);
00199               P.setShowName(ShowsName);
00200               if (zc.showGrid() && !rightmouse && 
00201                             Global.getParameter("grid.leftsnap",false))
00202               {      if (P instanceof PointObject && V!=null)
00203                      {      double dx=((PointObject)P).getX(),dy=((PointObject)P).getY();
00204                             P.snap(zc);
00205                             dx=((PointObject)P).getX()-dx; 
00206                             dy=((PointObject)P).getY()-dy;
00207                             P.round();
00208                             P.updateText();
00209                             Enumeration en=V.elements();
00210                             while (en.hasMoreElements())
00211                             {      PointObject p=(PointObject)en.nextElement();
00212                                    p.move(p.getX()+dx,p.getY()+dy);
00213                                    p.updateText();
00214                             }
00215                      }
00216                      else
00217                      {      P.snap(zc);
00218                             P.round();
00219                             P.updateText();
00220                      }
00221               }
00222               zc.validate();
00223               if (Grab)
00224               {      zc.grab(false);
00225                      Grab=false;
00226               }
00227               if (ChangedDrawable)
00228               {      if (P instanceof FixedCircleObject)
00229                      {      ((FixedCircleObject)P).setDragable(WasDrawable);
00230                      }
00231                      else if (P instanceof FixedAngleObject)
00232                      {      ((FixedAngleObject)P).setDragable(WasDrawable);
00233                      }
00234               }
00235               zc.clearSelected();
00236               zc.repaint();
00237               P=null;
00238               V=null;
00239               Selected=false;
00240               showStatus(zc);
00241        }
00242        
00243        public void mouseReleased (MouseEvent e, ZirkelCanvas zc)
00244        {      mouseReleased(e,zc,false);
00245        }
00246        
00247        public void reset (ZirkelCanvas zc)
00248        {      V=null;
00249               P=null;
00250               zc.clearSelected();
00251               zc.repaint(); 
00252               Selected=false;
00253        }
00254        
00255        public void resetFirstTime (ZirkelCanvas zc)
00256        {      if (V!=null) V.removeAllElements();
00257               zc.clearSelected();
00258               zc.selectAllMoveableVisibleObjects();
00259               Graphics g=zc.getGraphics();
00260               if (g!=null)
00261               {      zc.paint(g);
00262                      g.dispose();
00263                      try
00264                      {      Thread.sleep(400);
00265                      }
00266                      catch (Exception e) {}
00267                      zc.clearSelected();
00268               }
00269               zc.repaint(); 
00270               Selected=false;
00271               P=null;
00272               V=null;
00273        }
00274        
00275        public void showStatus (ZirkelCanvas zc)
00276        {      if (P==null && !Selected) zc.showStatus(
00277                      Zirkel.name("message.move.select","Move: Select a point"));
00278               else if (Selected) 
00279                      zc.showStatus(
00280                      Zirkel.name("message.move.move","Move: Move the point"));
00281               else zc.showStatus(
00282                      Zirkel.name("message.move.move","Move: Move the point")+
00283                      " ("+P.getName()+")");
00284        }
00285        
00286        public boolean useSmartBoard ()
00287        {      return false;
00288        }
00289        
00290 }