001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.applications.designstudio.components;
016:
017: import java.awt.Color;
018: import java.awt.Dimension;
019: import java.awt.Graphics;
020: import java.awt.Point;
021: import java.awt.Rectangle;
022: import java.awt.SystemColor;
023: import java.awt.event.MouseEvent;
024: import java.awt.event.MouseMotionListener;
025: import java.awt.geom.Point2D;
026: import java.beans.PropertyChangeEvent;
027: import java.beans.PropertyChangeListener;
028: import java.text.NumberFormat;
029:
030: import javax.swing.JComponent;
031: import javax.swing.UIManager;
032:
033: import org.jgraph.JGraph;
034:
035: public class Rule extends JComponent implements MouseMotionListener,
036: PropertyChangeListener {
037: public static final Color middleGray = new Color(170, 170, 170);
038: public static final NumberFormat nf = NumberFormat.getInstance();
039: public static final int INCH = 72;
040: public static final int HORIZONTAL = 0;
041: public static final int VERTICAL = 1;
042: public static final int SIZE = 15;
043: public static final int RM_PIXEL = 0;
044: public static final int RM_INCH = 1;
045: public static final int RM_METRIC = 2;
046:
047: public static String modeToStr(int pMode) {
048: switch (pMode) {
049: case RM_PIXEL:
050: return "pxl";
051: case RM_INCH:
052: return "in";
053: case RM_METRIC:
054: return "cm";
055: }
056: return "";
057: }
058:
059: private transient JGraph mGraph = null;
060: private double mIncrement = 0;
061: private double mUnits = 0;
062: private int mStep = 1;
063: private Point drag, mouse = new Point();
064: private int mOrientation;
065: private int mMode = RM_PIXEL;
066: private int mActiveOffset = 0;
067: private int mActiveLength = 0;
068:
069: public Rule(int o, boolean m, JGraph pGraph) {
070: mGraph = pGraph;
071: mOrientation = o;
072: mMode = RM_PIXEL;
073: setIncrementAndUnits();
074: nf.setMaximumFractionDigits(2);
075: mGraph.addMouseMotionListener(this );
076: mGraph.addPropertyChangeListener(this );
077: }
078:
079: private void setIncrementAndUnits() {
080: switch (mMode) {
081: case RM_PIXEL:
082: mUnits = mStep;
083: mUnits *= mGraph.getScale();
084: mIncrement = mUnits;
085: while (mIncrement < 10)
086: mIncrement *= 2;
087: break;
088: case RM_INCH:
089: mUnits = INCH * mStep;
090: mUnits *= mGraph.getScale();
091: mIncrement = mUnits / 2;
092: break;
093: case RM_METRIC:
094: mUnits = (int) (INCH / 2.54) * mStep; // 2.54 dots per centimeter
095: mUnits *= mGraph.getScale();
096: mIncrement = mUnits;
097: break;
098: default:
099: mIncrement = 0;
100: mUnits = 0;
101: break;
102: }
103: }
104:
105: public Dimension getPreferredSize() {
106: Dimension dim = mGraph.getPreferredSize();
107: if (mOrientation == HORIZONTAL)
108: dim.height = SIZE;
109: else
110: dim.width = SIZE;
111: return dim;
112: }
113:
114: // from MouseMotionListener
115: public void mouseMoved(MouseEvent e) {
116: if (drag != null) {
117: Point old = drag;
118: drag = null;
119: repaintStripe(old.x, old.y);
120: }
121: Point old = mouse;
122: mouse = e.getPoint();
123: repaintStripe(old.x, old.y);
124: repaintStripe(mouse.x, mouse.y);
125: }
126:
127: // from MouseMotionListener
128: public void mouseDragged(MouseEvent e) {
129: Point old = drag;
130: drag = e.getPoint();
131: if (old != null)
132: repaintStripe(old.x, old.y);
133: repaintStripe(drag.x, drag.y);
134: }
135:
136: // from PropertyChangeListener
137: public void propertyChange(PropertyChangeEvent event) {
138: String changeName = event.getPropertyName();
139: if (changeName.equals(JGraph.SCALE_PROPERTY))
140: repaint();
141: }
142:
143: public void repaintStripe(int x, int y) {
144: if (mOrientation == HORIZONTAL)
145: repaint(x, 0, 1, SIZE);
146: else
147: repaint(0, y, SIZE, 1);
148: }
149:
150: public void paintComponent(Graphics g) {
151: revalidate();
152: setIncrementAndUnits();
153: Rectangle drawHere = g.getClipBounds();
154:
155: // Fill clipping area with mGraph parent background.
156: if (mActiveLength > 0)
157: g.setColor(UIManager.getColor("control").darker());
158: else
159: g.setColor(new Color(255, 255, 225));
160: g.fillRect(drawHere.x, drawHere.y, drawHere.width,
161: drawHere.height);
162: Point2D p = mGraph.toScreen(new Point(mActiveOffset,
163: mActiveLength));
164:
165: g.setColor(UIManager.getColor("control").darker());
166: if (mOrientation == HORIZONTAL)
167: g.drawLine(drawHere.x, drawHere.y + drawHere.height - 1,
168: drawHere.x + drawHere.width, drawHere.y
169: + drawHere.height - 1);
170: else
171: g.drawLine(drawHere.x + drawHere.width - 1, drawHere.y,
172: drawHere.x + drawHere.width - 1, drawHere.y
173: + drawHere.height);
174:
175: //p.x = (int) (p.x * Rule.INCH/72);
176: //p.y = (int) (p.y * Rule.INCH/72);
177:
178: // draw "active region"
179: g.setColor(UIManager.getColor("control"));
180: if (mOrientation == HORIZONTAL)
181: g.fillRect((int) p.getX(), drawHere.y, (int) p.getY(),
182: drawHere.height);
183: else
184: g.fillRect(drawHere.x, (int) p.getX(), drawHere.width,
185: (int) p.getY());
186:
187: // Do the ruler labels in a small font that'buttonSelect black.
188: //g.setFont(new Font("SansSerif", Font.PLAIN, 8));
189: g.setFont(getFont());
190: g.setColor(SystemColor.darkGray);
191: //g.setColor(SystemColor.black);
192:
193: // Some vars we need.
194: double end = 0;
195: double start = 0;
196: int tickLength = 0;
197: String text = null;
198:
199: // Use clipping bounds to calculate first tick and last tick location.
200: if (mOrientation == HORIZONTAL) {
201: start = Math.floor(drawHere.x / mIncrement) * mIncrement;
202: end = Math.ceil((drawHere.x + drawHere.width) / mIncrement)
203: * mIncrement;
204: } else {
205: start = Math.floor(drawHere.y / mIncrement) * mIncrement;
206: end = Math
207: .ceil((drawHere.y + drawHere.height) / mIncrement)
208: * mIncrement;
209: }
210:
211: // Make a special case of 0 to display the number
212: // within the rule and draw a mUnits label.
213: if (start == 0) {
214: text = Integer.toString(0) + " " + modeToStr(mMode);
215: tickLength = 10;
216: if (mOrientation == HORIZONTAL) {
217: g.drawLine(0, SIZE - 1, 0, SIZE - tickLength - 1);
218: g.drawString(text, 2, 11);
219: } else {
220: g.drawLine(SIZE - 1, 0, SIZE - tickLength - 1, 0);
221: g.drawString(text, 1, 11);
222: }
223: text = null;
224: start = mIncrement;
225: }
226:
227: // ticks and labels
228: boolean label = false;
229: double lIncrement = mIncrement;
230: for (double i = start; i < end; i += lIncrement) {
231: if (mUnits == 0)
232: mUnits = 1;
233: tickLength = 10;
234: //VW make relative to scaling factor
235: text = nf.format(i / mUnits * mStep);
236: label = false;
237:
238: if (mOrientation == HORIZONTAL) {
239: g.drawLine((int) i, SIZE - 1, (int) i, SIZE
240: - tickLength - 1);
241: if (text != null)
242: g.drawString(text, (int) i + 2, 11);
243: } else {
244: g.drawLine(SIZE - 1, (int) i, SIZE - tickLength - 1,
245: (int) i);
246: if (text != null)
247: g.drawString(text, 0, (int) i + 9);
248: }
249: }
250:
251: // Draw Mouseposition
252: //if (mGraph.hasFocus()) {
253: g.setColor(Color.green);
254: if (mOrientation == HORIZONTAL)
255: g.drawLine(mouse.x, SIZE - 1, mouse.x, SIZE - tickLength
256: - 1);
257: else
258: g.drawLine(SIZE - 1, mouse.y, SIZE - tickLength - 1,
259: mouse.y);
260: if (drag != null)
261: if (mOrientation == HORIZONTAL)
262: g.drawLine(drag.x, SIZE - 1, drag.x, SIZE - tickLength
263: - 1);
264: else
265: g.drawLine(SIZE - 1, drag.y, SIZE - tickLength - 1,
266: drag.y);
267: //}
268: }
269:
270: /* Properties Accessors */
271:
272: /* Mode */
273:
274: public void setMode(int value) {
275: if (mMode != value) {
276: mMode = value;
277: setIncrementAndUnits();
278: repaint();
279: }
280: }
281:
282: public int getMode() {
283: return mMode;
284: }
285:
286: /* Increment */
287:
288: public double getIncrement() {
289: return mIncrement;
290: }
291:
292: /* Active Offset */
293:
294: public void setActiveOffset(int offset) {
295: mActiveOffset = offset;
296: }
297:
298: /* Active Length */
299:
300: public void setActiveLength(int length) {
301: mActiveLength = length;
302: }
303:
304: /* Grid Step */
305:
306: public int getGridStep() {
307: return mStep;
308: }
309:
310: public void setGridStep(int pValue) {
311: if (pValue != mStep) {
312: mStep = pValue;
313: setIncrementAndUnits();
314: invalidate();
315: }
316: }
317: }
|