1 | /* |
---|
2 | * $Id: DragBox.java,v 1.2 2003/08/22 23:02:34 dwd Exp $ |
---|
3 | * |
---|
4 | * This software is provided by NOAA for full, free and open release. It is |
---|
5 | * understood by the recipient/user that NOAA assumes no liability for any |
---|
6 | * errors contained in the code. Although this software is released without |
---|
7 | * conditions or restrictions in its use, it is expected that appropriate |
---|
8 | * credit be given to its author and to the National Oceanic and Atmospheric |
---|
9 | * Administration should the software be included by the recipient as an |
---|
10 | * element in other product development. |
---|
11 | */ |
---|
12 | |
---|
13 | package gov.noaa.pmel.sgt.beans; |
---|
14 | |
---|
15 | import java.awt.Color; |
---|
16 | import java.awt.Rectangle; |
---|
17 | import java.awt.Point; |
---|
18 | import java.awt.Graphics; |
---|
19 | |
---|
20 | import gov.noaa.pmel.util.Point2D; |
---|
21 | import gov.noaa.pmel.util.Rectangle2D; |
---|
22 | |
---|
23 | /** |
---|
24 | * @author Donald Denbo |
---|
25 | * @version $Revision: 1.2 $, $Date: 2003/08/22 23:02:34 $ |
---|
26 | * @since 3.0 |
---|
27 | **/ |
---|
28 | abstract class DragBox implements DesignListener { |
---|
29 | public static final int UPPER_LEFT = 0; |
---|
30 | public static final int UPPER_RIGHT = 1; |
---|
31 | public static final int LOWER_LEFT = 2; |
---|
32 | public static final int LOWER_RIGHT = 3; |
---|
33 | public static final int CENTER = 4; |
---|
34 | |
---|
35 | protected static int handleSize_ = 8; |
---|
36 | protected Rectangle[] handles_ = new Rectangle[5]; |
---|
37 | protected int selectedHandle_ = -1; |
---|
38 | protected boolean selected_ = false; |
---|
39 | protected Color selectedColor_ = Color.red; |
---|
40 | protected Color unSelectedColor_ = Color.darkGray; |
---|
41 | protected Color color_ = unSelectedColor_; |
---|
42 | protected PanelHolder pHolder_; |
---|
43 | |
---|
44 | public DragBox(PanelHolder ph) { |
---|
45 | pHolder_ = ph; |
---|
46 | } |
---|
47 | |
---|
48 | public void setSelected(boolean sel) { |
---|
49 | selected_ = sel; |
---|
50 | if(selected_) { |
---|
51 | color_ = selectedColor_; |
---|
52 | } else { |
---|
53 | color_ = unSelectedColor_; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | public boolean isSelected() { |
---|
58 | return selected_; |
---|
59 | } |
---|
60 | |
---|
61 | public boolean handlesContain(Point pt) { |
---|
62 | for(int i=0; i < handles_.length; i++) { |
---|
63 | if(handles_[i].contains(pt)) { |
---|
64 | selectedHandle_ = i; |
---|
65 | return true; |
---|
66 | } |
---|
67 | } |
---|
68 | selectedHandle_ = -1; |
---|
69 | return false; |
---|
70 | } |
---|
71 | |
---|
72 | protected void computeHandles() { |
---|
73 | Rectangle bounds = getBounds(); |
---|
74 | handles_[UPPER_LEFT].setBounds(bounds.x, |
---|
75 | bounds.y, |
---|
76 | handleSize_, handleSize_); |
---|
77 | handles_[UPPER_RIGHT].setBounds(bounds.x+bounds.width-handleSize_, |
---|
78 | bounds.y, |
---|
79 | handleSize_, handleSize_); |
---|
80 | handles_[LOWER_LEFT].setBounds(bounds.x, |
---|
81 | bounds.y+bounds.height-handleSize_, |
---|
82 | handleSize_, handleSize_); |
---|
83 | handles_[LOWER_RIGHT].setBounds(bounds.x+bounds.width-handleSize_, |
---|
84 | bounds.y+bounds.height-handleSize_, |
---|
85 | handleSize_, handleSize_); |
---|
86 | handles_[CENTER].setBounds(bounds.x+(bounds.width-handleSize_)/2, |
---|
87 | bounds.y+(bounds.height-handleSize_)/2, |
---|
88 | handleSize_, handleSize_); |
---|
89 | } |
---|
90 | |
---|
91 | public void mouseOperation(int op, int dx, int dy) { |
---|
92 | if(op == -1) return; |
---|
93 | Point pt; |
---|
94 | Rectangle rect; |
---|
95 | if(op == DragBox.CENTER) { |
---|
96 | pt = getLocation(); |
---|
97 | pt.x += dx; |
---|
98 | pt.y += dy; |
---|
99 | setLocation(pt); |
---|
100 | } else { |
---|
101 | rect = getBounds(); |
---|
102 | int x2 = rect.x + rect.width; |
---|
103 | int y2 = rect.y + rect.height; |
---|
104 | switch (op) { |
---|
105 | case DragBox.UPPER_LEFT: |
---|
106 | rect.x += dx; |
---|
107 | rect.y += dy; |
---|
108 | break; |
---|
109 | case DragBox.UPPER_RIGHT: |
---|
110 | x2 += dx; |
---|
111 | rect.y += dy; |
---|
112 | break; |
---|
113 | case DragBox.LOWER_LEFT: |
---|
114 | rect.x += dx; |
---|
115 | y2 += dy; |
---|
116 | break; |
---|
117 | case DragBox.LOWER_RIGHT: |
---|
118 | x2 += dx; |
---|
119 | y2 += dy; |
---|
120 | } |
---|
121 | rect.width = x2 - rect.x; |
---|
122 | rect.height = y2 - rect.y; |
---|
123 | setBounds(rect); |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | public boolean contains(Point pt) { |
---|
128 | return getBounds().contains(pt); |
---|
129 | } |
---|
130 | |
---|
131 | public int getSelectedHandle() { |
---|
132 | return selectedHandle_; |
---|
133 | } |
---|
134 | |
---|
135 | Point2D.Double toLocation(Point pd) { |
---|
136 | return new Point2D.Double(toXLocation(pd.x), |
---|
137 | toYLocation(pd.y)); |
---|
138 | } |
---|
139 | |
---|
140 | Point toLocation(Point2D.Double pp) { |
---|
141 | return new Point(toXLocation(pp.x), |
---|
142 | toYLocation(pp.y)); |
---|
143 | } |
---|
144 | |
---|
145 | Rectangle2D.Double toRectangle(Rectangle rd) { |
---|
146 | return new Rectangle2D.Double(toXLocation(rd.x), |
---|
147 | toYLocation(rd.y + rd.height), |
---|
148 | transform(rd.width), |
---|
149 | transform(rd.height)); |
---|
150 | } |
---|
151 | |
---|
152 | Rectangle toRectangle(Rectangle2D.Double rp) { |
---|
153 | int h = transform(rp.height); |
---|
154 | return new Rectangle(toXLocation(rp.x), |
---|
155 | toYLocation(rp.y) - h, |
---|
156 | transform(rp.width), |
---|
157 | h); |
---|
158 | } |
---|
159 | |
---|
160 | double toXLocation(int xd) { |
---|
161 | return (xd - pHolder_.getBounds().x)/pHolder_.getPanelModel().getDpi(); |
---|
162 | } |
---|
163 | |
---|
164 | double toYLocation(int yd) { |
---|
165 | return (pHolder_.getBounds().height - yd + |
---|
166 | pHolder_.getBounds().y)/pHolder_.getPanelModel().getDpi(); |
---|
167 | } |
---|
168 | |
---|
169 | int toXLocation(double xp) { |
---|
170 | return (int)(xp*pHolder_.getPanelModel().getDpi()+0.5f) + |
---|
171 | pHolder_.getBounds().x; |
---|
172 | } |
---|
173 | |
---|
174 | int toYLocation(double yp) { |
---|
175 | return pHolder_.getBounds().height - (int)(yp*pHolder_.getPanelModel().getDpi()+0.5f) + |
---|
176 | pHolder_.getBounds().y; |
---|
177 | } |
---|
178 | |
---|
179 | double transform(int dev) { |
---|
180 | return dev/pHolder_.getPanelModel().getDpi(); |
---|
181 | } |
---|
182 | |
---|
183 | int transform(double phy) { |
---|
184 | return (int)(phy*pHolder_.getPanelModel().getDpi()+0.5f); |
---|
185 | } |
---|
186 | |
---|
187 | abstract public void draw(Graphics g); |
---|
188 | abstract public void update(String message); |
---|
189 | abstract public void setId(String id); |
---|
190 | abstract public String getId(); |
---|
191 | abstract public void setBounds(Rectangle bounds); |
---|
192 | abstract public Rectangle getBounds(); |
---|
193 | abstract public void setLocation(Point pt); |
---|
194 | abstract public Point getLocation(); |
---|
195 | } |
---|