View Javadoc

1   // Copyright 2004-2005, FreeHEP.
2   package hep.wired.heprep.interaction;
3   
4   import java.awt.*;
5   import java.awt.event.*;
6   import java.awt.geom.*;
7   import java.util.*;
8   import java.util.List;
9   import javax.swing.*;
10  
11  import org.freehep.application.Application;
12  
13  import hep.wired.edit.Translate;
14  import hep.wired.services.RecordPlot;
15  import hep.wired.services.GraphicsPanel;
16  import hep.wired.feature.Translateable3D;
17  import hep.wired.heprep.services.Projection;
18  import hep.wired.interaction.AbstractInteractionHandler;
19  import hep.wired.heprep.image.WiredImage;
20  
21  /***
22   * Click mouse to set a location.
23   *
24   * @author Mark Donszelmann
25   * @version $Id: ClickToSetLocation.java 683 2005-03-14 02:25:00Z duns $
26   */
27  public class ClickToSetLocation extends AbstractInteractionHandler {
28  
29      private boolean showHairLines;
30      private int mouseX = -1;
31      private int mouseY = -1;
32      private int x = -1;
33      private int y = -1;
34      private boolean xIsFixed, yIsFixed;
35      private List/*<LocationListener>*/ listeners = new ArrayList();
36  
37      /***
38       * Create a click to set location handler.
39       */
40      public ClickToSetLocation() {
41          this(true);
42      }
43      
44      /***
45       * Create a click to set location handler.
46       */
47      public ClickToSetLocation(boolean showHairLines) {
48          super("Click to Set a Location");
49          this.showHairLines = showHairLines;
50          xIsFixed = false;
51          yIsFixed = false;
52      }
53  
54      /***
55       * Add a location listener, to be informed when the location changes.
56       */
57      public void addLocationListener(LocationListener listener) {
58          listeners.add(listener);
59      }
60  
61      /***
62       * Remove a location listener.
63       */
64      public void removeLocationListener(LocationListener listener) {
65          listeners.remove(listener);
66      }
67  
68      public Icon getIcon(int size) {
69          return WiredImage.getIcon("Translate3D%w", size);
70      }    
71  
72      public String getDescription() {
73          return "Click to select a point in two orthogonal planes.";
74      }
75  
76      public boolean isSupportedBy(GraphicsPanel panel) {
77          return false;
78  //        return (new Translate().isSupportedBy(panel)) && panel.supports(Translateable3D.class);
79      }
80  
81      public void changeCursor(RecordPlot plot, InputEvent event) {
82          plot.setCursor(WiredImage.getBestCursor("TranslateCursor3D%w", 32, 32));
83      }
84  
85      public void reset(RecordPlot plot, InputEvent event) {
86          x = mouseX;
87          y = mouseY;
88          xIsFixed = false;
89          yIsFixed = false;
90          defineShape(plot);
91          informListeners(plot);        
92          Application.getApplication().setStatusMessage(getDescription());
93      }
94  
95      public void mouseMoved(RecordPlot plot, MouseEvent event) {
96          modifyPoint(event);
97          defineShape(plot);
98          informListeners(plot);
99      }
100 
101     public void mouseEntered(RecordPlot plot, MouseEvent event) {
102         plot.requestFocusInWindow();
103         modifyPoint(event);
104         defineShape(plot);
105         informListeners(plot);
106     }
107 
108     public void mouseExited(RecordPlot plot, MouseEvent event) {
109         modifyPoint(event);
110         defineShape(plot);
111         informListeners(plot);
112     }
113 
114     public void mouseButton1Clicked(RecordPlot plot, MouseEvent event) {
115         modifyPoint(event);
116         xIsFixed = true;
117         yIsFixed = true;
118         defineShape(plot);
119         informListeners(plot);
120     }
121   
122     /***
123      * Sets (and fixes) the location of this handler.
124      */ 
125     public void setLocation(RecordPlot plot, int x, int y, boolean xIsFixed, boolean yIsFixed) {
126         this.x = x;
127         this.y = y;
128         this.xIsFixed = xIsFixed;
129         this.yIsFixed = yIsFixed;
130         defineShape(plot);
131 //        plot.repaint();
132     }  
133 
134     private void informListeners(RecordPlot plot) {
135         for (Iterator i=listeners.iterator(); i.hasNext(); ) {
136             ((LocationListener)i.next()).setLocation(plot, x, y, xIsFixed, yIsFixed);
137         }
138     }
139             
140     private void modifyPoint(MouseEvent event) {
141         int id = event.getID();
142         x = (xIsFixed) ? x : (id != MouseEvent.MOUSE_EXITED) ? event.getX() : -1;
143         y = (yIsFixed) ? y : (id != MouseEvent.MOUSE_EXITED) ? event.getY() : -1;
144         mouseX = (id != MouseEvent.MOUSE_EXITED) ? event.getX() : -1;
145         mouseY = (id != MouseEvent.MOUSE_EXITED) ? event.getY() : -1;
146     }
147     
148     private void defineShape(RecordPlot plot) {
149         float w = plot.getWidth();
150         float h = plot.getHeight();
151         GeneralPath p = new GeneralPath();
152         
153         // horizontal
154         if (y >= 0) {
155             p.moveTo(0, y);
156             p.lineTo(w, y);
157         }
158         
159         // vertical
160         if (x >= 0) {
161             p.moveTo(x, 0);
162             p.lineTo(x, h);
163         }
164         plot.drawShape(p);
165     }
166 
167     public String toString() {
168         return "Click to set a location";
169     }
170 }