001: /*
002: * Copyright (C) 2004 Giuseppe MANNA
003: *
004: * This file is part of FreeReportBuilder
005: *
006: * FreeReportBuilder is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package it.frb;
023:
024: import java.awt.Color;
025:
026: public class Rule extends javax.swing.JComponent {
027: public static final int INCH = java.awt.Toolkit.getDefaultToolkit()
028: .getScreenResolution();
029: public static final int HORIZONTAL = 0;
030: public static final int VERTICAL = 1;
031: public static final int SIZE = 12;
032:
033: public int orientation;
034: public boolean isMetric;
035: private int increment;
036: private int units;
037: private javax.swing.JPanel panel;
038:
039: public Rule(int o, boolean m) {
040: this (null, o, m);
041: }
042:
043: public Rule(javax.swing.JPanel panel, int o, boolean m) {
044: this .panel = panel;
045: orientation = o;
046: isMetric = m;
047: setIncrementAndUnits();
048: }
049:
050: public void setIsMetric(boolean isMetric) {
051: this .isMetric = isMetric;
052: setIncrementAndUnits();
053: repaint();
054: }
055:
056: private void setIncrementAndUnits() {
057: if (isMetric) {
058: units = (int) ((double) INCH / (double) 2.54); // dots per centimeter
059: increment = units;
060: } else {
061: units = INCH;
062: increment = units / 2;
063: }
064: }
065:
066: public boolean isMetric() {
067: return this .isMetric;
068: }
069:
070: public int getIncrement() {
071: return increment;
072: }
073:
074: public void setPreferredHeight(int ph) {
075: setPreferredSize(new java.awt.Dimension(SIZE, ph));
076: }
077:
078: public void setPreferredWidth(int pw) {
079: setPreferredSize(new java.awt.Dimension(pw, SIZE));
080: }
081:
082: protected void paintComponent(java.awt.Graphics g) {
083:
084: java.awt.Rectangle drawHere;
085:
086: if (panel != null) {
087: drawHere = panel.getBounds();
088: drawHere = new java.awt.Rectangle(drawHere.x, drawHere.y,
089: 10000, 10000);
090: } else {
091: drawHere = g.getClipBounds();
092: }
093:
094: // Fill clipping area with dirty brown/orange.
095: //g.setColor(new Color(230, 163, 4));
096: g.setColor(new Color(204, 204, 204));
097: g.fillRect(drawHere.x, drawHere.y, drawHere.width,
098: drawHere.height);
099:
100: // Do the ruler labels in a small font that's black.
101: g
102: .setFont(new java.awt.Font("SansSerif",
103: java.awt.Font.PLAIN, 9));
104: g.setColor(Color.black);
105:
106: // Some vars we need.
107: int end = 0;
108: int start = 0;
109: int tickLength = 0;
110: String text = null;
111:
112: // Use clipping bounds to calculate first and last tick locations.
113: if (orientation == HORIZONTAL) {
114: start = (drawHere.x / increment) * increment;
115: end = (((drawHere.x + drawHere.width) / increment) + 1)
116: * increment;
117: } else {
118: start = (drawHere.y / increment) * increment;
119: end = (((drawHere.y + drawHere.height) / increment) + 1)
120: * increment;
121: }
122:
123: // Make a special case of 0 to display the number
124: // within the rule and draw a units label.
125: if (start == 0) {
126: text = Integer.toString(0) + (isMetric ? " cm" : " in");
127: tickLength = 10;
128: if (orientation == HORIZONTAL) {
129: g.drawLine(0, SIZE - 1, 0, SIZE - tickLength - 1);
130: g.drawString(text, 2, 7);
131: } else {
132: g.drawLine(SIZE - 1, 0, SIZE - tickLength - 1, 0);
133: g.drawString(text, 1, 10);
134: }
135: text = null;
136: start = increment;
137: }
138:
139: // ticks and labels
140: for (int i = start; i < end; i += increment) {
141: if (i % units == 0) {
142: tickLength = 2;
143: text = Integer.toString(i / units);
144: } else {
145: tickLength = 1;
146: text = null;
147: }
148:
149: if (tickLength != 0) {
150: if (orientation == HORIZONTAL) {
151: g.drawLine(i, SIZE - 1, i, SIZE - tickLength - 1);
152: if (text != null) {
153: if (text.equals("21")) {
154: g.setColor(Color.red);
155: text = "A4";
156: }
157:
158: if (text.equals("30")) {
159: g.setColor(Color.red);
160: text = "A3";
161: }
162: g.drawString(text, i - 3, 7);
163: g.setColor(Color.black);
164: }
165: } else {
166: g.drawLine(SIZE - 1, i, SIZE - tickLength - 1, i);
167: if (text != null)
168: g.drawString(text, 1, i + 3);
169: }
170: }
171: }
172: }
173: }
|