001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * JUMP is Copyright (C) 2003 Vivid Solutions
006: *
007: * This program implements extensions to JUMP and is
008: * Copyright (C) Stefan Steiniger.
009: *
010: * This program is free software; you can redistribute it and/or
011: * modify it under the terms of the GNU General Public License
012: * as published by the Free Software Foundation; either version 2
013: * of the License, or (at your option) any later version.
014: *
015: * This program is distributed in the hope that it will be useful,
016: * but WITHOUT ANY WARRANTY; without even the implied warranty of
017: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
018: * GNU General Public License for more details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with this program; if not, write to the Free Software
022: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
023: *
024: * For more information, contact:
025: * Stefan Steiniger
026: * perriger@gmx.de
027: */
028: /*****************************************************
029: * created: original version by Vivid Solution
030: * last modified: 03.06.2005
031: * 01.10.2005 [scale now obtained from other class]
032: *
033: * description:
034: * calculates the actual scale and draws the text
035: * and a white rectangle around
036: *
037: * @author sstein
038: *****************************************************/package org.openjump.core.ui.plugin.view.helpclassescale;
039:
040: import java.awt.BasicStroke;
041: import java.awt.Color;
042: import java.awt.Font;
043: import java.awt.Graphics2D;
044: import java.awt.Stroke;
045: import java.awt.font.TextLayout;
046: import java.awt.geom.Rectangle2D;
047:
048: import org.openjump.core.ui.util.ScreenScale;
049:
050: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
051: import com.vividsolutions.jump.workbench.ui.LayerViewPanel;
052: import com.vividsolutions.jump.workbench.ui.renderer.SimpleRenderer;
053:
054: /**
055: * @description:
056: * calculates the actual scale and draws the text
057: * and a white rectangle around
058: *
059: * @author sstein
060: **/
061: public class ShowScaleRenderer extends SimpleRenderer {
062:
063: public final static String CONTENT_ID = "SCALE_SHOW";
064: /**
065: * Height of the increment boxes, in view-space units.
066: */
067: private final static int BAR_HEIGHT = 13;
068: private final static Color FILL1 = Color.WHITE;
069:
070: /**
071: * Distance from the right edge, in view-space units.
072: */
073: private final static int HORIZONTAL_MARGIN = 100;
074:
075: /**
076: * In view-space units; the actual increment may be a bit larger or smaller
077: * than this amount.
078: */
079: private final static Color LINE_COLOR = Color.GRAY;
080: private final static int TEXT_BOTTOM_MARGIN = 1;
081: private final static Color TEXT_COLOR = Color.black;
082:
083: /**
084: * Distance from the bottom edge, in view-space units.
085: */
086: private final int FONTSIZE = 12;
087: private final static int VERTICAL_MARGIN = 3;
088: private final static String ENABLED_KEY = "SCALE_SHOW_ENABLED";
089: private Font FONT = new Font("Dialog", Font.PLAIN, FONTSIZE);
090: private Stroke stroke = new BasicStroke();
091: private PlugInContext myPlugInContext = null;
092:
093: public ShowScaleRenderer(LayerViewPanel panel) {
094: super (CONTENT_ID, panel);
095: }
096:
097: protected void paint(Graphics2D g) {
098: if (!isEnabled(panel)) {
099: return;
100: }
101: //Override dashes set in GridRenderer [Jon Aquino]
102: g.setStroke(stroke);
103: double screenScale = ScreenScale.getHorizontalMapScale(panel
104: .getViewport());
105: paintScaleLabel(g, screenScale);
106: }
107:
108: private int barBottom() {
109: return panel.getHeight() - VERTICAL_MARGIN;
110: }
111:
112: private int barTop() {
113: return barBottom() - BAR_HEIGHT;
114: }
115:
116: private TextLayout createTextLayout(String text, Font font,
117: Graphics2D g) {
118: return new TextLayout(text, font, g.getFontRenderContext());
119: }
120:
121: private void paintScaleLabel(Graphics2D g, double scale) {
122:
123: Integer scaleD = new Integer((int) Math.floor(scale));
124: String text = "1 : " + scaleD.toString();
125: int length = text.length();
126:
127: //-- draw rectangle
128: Rectangle2D.Double shape = new Rectangle2D.Double(panel
129: .getWidth()
130: - (length + 13) * 3.6, barTop(),
131: (length + 12) * 3.6 - 3, barBottom() - barTop());
132: g.setColor(FILL1);
133: g.fill(shape);
134: g.setColor(LINE_COLOR);
135: g.draw(shape);
136:
137: //draw text
138: Font font = FONT;
139: g.setColor(TEXT_COLOR);
140:
141: int textBottomMargin = TEXT_BOTTOM_MARGIN;
142:
143: TextLayout layout = createTextLayout(text, font, g);
144: layout.draw(g,
145: (float) (panel.getWidth() - (length + 11) * 3.6),
146: (float) (barBottom() - textBottomMargin));
147:
148: }
149:
150: /*********** getters and setters ******************/
151:
152: /**
153: *
154: * @param panel
155: * @return
156: */
157: public static boolean isEnabled(LayerViewPanel panel) {
158: return panel.getBlackboard().get(ENABLED_KEY, false);
159: }
160:
161: public static void setEnabled(boolean enabled, LayerViewPanel panel) {
162: panel.getBlackboard().put(ENABLED_KEY, enabled);
163: }
164:
165: /**
166: * @param myPlugInContext The myPlugInContext to set.
167: */
168: public void setMyPlugInContext(PlugInContext myPlugInContext) {
169: this.myPlugInContext = myPlugInContext;
170: }
171: }
|