001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: ElementStyle.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.gui.old;
009:
010: import java.awt.*;
011:
012: import com.uwyn.rife.config.Config;
013: import com.uwyn.rife.gui.Rife;
014: import com.uwyn.rife.swing.JDialogSystemError;
015: import com.uwyn.rife.tools.ExceptionUtils;
016: import java.awt.font.FontRenderContext;
017: import java.awt.font.LineMetrics;
018: import java.awt.geom.AffineTransform;
019: import java.awt.geom.Rectangle2D;
020: import java.awt.image.AffineTransformOp;
021: import java.awt.image.BufferedImage;
022: import java.awt.image.BufferedImageOp;
023: import java.awt.image.RasterFormatException;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.InputStreamReader;
027: import java.io.UnsupportedEncodingException;
028:
029: public class ElementStyle {
030: public static final String MAX_PROPERTY_STRING = "MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM";
031: public static final int MAX_PROPERTY_STRING_LENGTH = MAX_PROPERTY_STRING
032: .length();
033:
034: private BufferedImage mParamImage = null;
035: private BufferedImageOp mParamImageOp = null;
036: private Graphics2D mParamGraphics = null;
037:
038: private Font mPlainFont = null;
039: private Font mBoldFont = null;
040:
041: public FontRenderContext mFontRenderContext = null;
042:
043: public Color mBodyBackgroundColor = null;
044: public Color mBodyBackgroundColorSelected = null;
045: public Color mElementBorderColor = null;
046: public Color mElementBorderColorSelected = null;
047: public Color mTitleBorderColor = null;
048: public Color mTitleTextColor = null;
049: public Color mExitTextColor = null;
050: public Color mParamBorderColor = null;
051: public Color mParamTextColor = null;
052: public Font mTitleFont = null;
053: public LineMetrics mTitleFontLineMetrics = null;
054: public Font mParamFont = null;
055: public LineMetrics mParamFontLineMetrics = null;
056: public Font mExitFont = null;
057: public LineMetrics mExitFontLineMetrics = null;
058: public float mElementBorderWidth = 0;
059: public float mTitleMarginWidth = 0;
060: public float mTitleMarginHeight = 0;
061: public float mExitMarginWidth = 0;
062: public float mExitMarginHeight = 0;
063: public float mParamMarginWidth = 0;
064: public float mParamMarginHeight = 0;
065: public float mParamLineWidth = 0;
066: public float mParamLineLength = 0;
067: public float mParamLineDashedWidth = 0;
068: public BasicStroke mParamLineDashedStroke = null;
069:
070: public ElementStyle(float scaleFactor) {
071: getBaseFonts();
072: setColors();
073: calculateStyle(scaleFactor);
074: }
075:
076: public static void setRenderingHints(Graphics2D g2d,
077: float scaleFactor) {
078: g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
079: RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
080: if (scaleFactor < 1) {
081: g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
082: RenderingHints.VALUE_ANTIALIAS_ON);
083: g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
084: RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
085: } else {
086: g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
087: RenderingHints.VALUE_ANTIALIAS_OFF);
088: g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
089: RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
090: }
091: g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
092: RenderingHints.VALUE_STROKE_PURE);
093: g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
094: RenderingHints.VALUE_RENDER_SPEED);
095: }
096:
097: private void getBaseFonts() {
098: mPlainFont = loadFont("/fonts/trebuchet.ttf");
099: mBoldFont = loadFont("/fonts/trebuchetbold.ttf");
100: }
101:
102: private Font loadFont(String path) {
103: InputStream input_stream = null;
104: InputStreamReader input_stream_reader = null;
105: Font font = null;
106:
107: input_stream = this .getClass().getResourceAsStream(path);
108:
109: if (input_stream != null) {
110: try {
111: input_stream_reader = new InputStreamReader(
112: input_stream, "ISO8859_1");
113:
114: if (input_stream_reader != null) {
115: try {
116: font = Font.createFont(Font.TRUETYPE_FONT,
117: input_stream);
118: } catch (IOException e) {
119: (new JDialogSystemError(
120: Rife.getMainFrame(),
121: "ElementStyle.loadFont() : IO error while creating the font from resource at '"
122: + path
123: + "' : "
124: + ExceptionUtils
125: .getExceptionStackTrace(e)))
126: .setVisible(true);
127: } catch (FontFormatException e) {
128: (new JDialogSystemError(
129: Rife.getMainFrame(),
130: "ElementStyle.loadFont() : Font format error while creating the font from resource at '"
131: + path
132: + "' : "
133: + ExceptionUtils
134: .getExceptionStackTrace(e)))
135: .setVisible(true);
136: }
137: } else {
138: (new JDialogSystemError(
139: Rife.getMainFrame(),
140: "ElementStyle.loadFont() : Couldn't create the inputstream reader for the font resource at '"
141: + path + "'.")).setVisible(true);
142: }
143: } catch (UnsupportedEncodingException e) {
144: (new JDialogSystemError(
145: Rife.getMainFrame(),
146: "ElementStyle.loadFont() : Error while creating the inputstream reader for the font resource at '"
147: + path
148: + "' : "
149: + ExceptionUtils
150: .getExceptionStackTrace(e)))
151: .setVisible(true);
152: }
153: } else {
154: (new JDialogSystemError(Rife.getMainFrame(),
155: "ElementStyle.loadFont() : Couldn't open the font resource at '"
156: + path + "'.")).setVisible(true);
157: }
158:
159: return font;
160: }
161:
162: public void drawParameterText(Graphics2D g2d, String text,
163: Color backgroundColor, int x, int y) {
164: mParamGraphics.setBackground(backgroundColor);
165: Rectangle2D text_size = mParamFont.getStringBounds(text,
166: mFontRenderContext);
167: int baseline = (int) (mParamFontLineMetrics.getAscent());
168: mParamGraphics.setClip(0, 0, (int) text_size.getWidth(),
169: mParamImage.getHeight());
170: mParamGraphics.clearRect(0, 0, (int) text_size.getWidth(),
171: mParamImage.getHeight());
172: mParamGraphics.drawString(text, 0, baseline);
173: Shape previous_clip = g2d.getClip();
174: g2d.clipRect(x - baseline, y - (int) text_size.getWidth(), x
175: - baseline + mParamImage.getHeight(), (int) text_size
176: .getWidth());
177: try {
178: g2d.drawImage(mParamImage, mParamImageOp, x, y);
179: } catch (RasterFormatException e) {
180: // do nothing
181: }
182: g2d.setClip(previous_clip);
183: }
184:
185: private void setColors() {
186: mBodyBackgroundColor = new Color(220, 255, 200);
187: mBodyBackgroundColorSelected = new Color(255, 255, 200);
188: mElementBorderColor = new Color(0, 0, 0);
189: mElementBorderColorSelected = new Color(0, 0, 0);
190: mTitleTextColor = new Color(0, 0, 0);
191: mTitleBorderColor = new Color(0, 0, 0);
192: mExitTextColor = new Color(0, 0, 0);
193: mParamTextColor = new Color(0, 0, 0);
194: mParamBorderColor = new Color(0, 0, 0);
195: }
196:
197: public void calculateStyle(float scaleFactor) {
198: mFontRenderContext = new FontRenderContext(null, false, false);
199:
200: mTitleFont = mBoldFont.deriveFont(Font.PLAIN,
201: (int) (13 * scaleFactor));
202: mTitleFontLineMetrics = mTitleFont.getLineMetrics(
203: MAX_PROPERTY_STRING, mFontRenderContext);
204: mParamFont = mPlainFont.deriveFont(Font.PLAIN,
205: (int) (11 * scaleFactor));
206: mParamFontLineMetrics = mParamFont.getLineMetrics(
207: MAX_PROPERTY_STRING, mFontRenderContext);
208: mExitFont = mPlainFont.deriveFont(Font.PLAIN,
209: (int) (11 * scaleFactor));
210: mExitFontLineMetrics = mExitFont.getLineMetrics(
211: MAX_PROPERTY_STRING, mFontRenderContext);
212: mElementBorderWidth = Math.round(scaleFactor);
213: mTitleMarginWidth = Math.round(5 * scaleFactor);
214: mTitleMarginHeight = Math.round(3 * scaleFactor);
215: mExitMarginWidth = Math.round(2 * scaleFactor);
216: mExitMarginHeight = Math.round(2 * scaleFactor);
217:
218: mParamMarginWidth = Math.round(scaleFactor);
219: mParamMarginHeight = Math.round(5 * scaleFactor);
220: mParamLineWidth = Math.round(3 * scaleFactor);
221: mParamLineLength = Math.round(Config.getRepInstance().getInt(
222: "GRID_SIZE")
223: * 2 * scaleFactor);
224: mParamLineDashedWidth = Math.round(scaleFactor);
225: mParamLineDashedStroke = new BasicStroke(mParamLineDashedWidth,
226: BasicStroke.CAP_SQUARE, BasicStroke.JOIN_BEVEL, 0f,
227: new float[] { 3f * scaleFactor }, 0f);
228:
229: Rectangle2D work_image_size = mParamFont.getStringBounds(
230: MAX_PROPERTY_STRING, mFontRenderContext);
231: mParamImage = new BufferedImage((int) work_image_size
232: .getWidth(), (int) work_image_size.getHeight(),
233: BufferedImage.TYPE_INT_RGB);
234: mParamGraphics = mParamImage.createGraphics();
235: setRenderingHints(mParamGraphics, scaleFactor);
236: mParamGraphics.setColor(mParamTextColor);
237: mParamGraphics.setFont(mParamFont);
238: AffineTransform image_op_transform = AffineTransform
239: .getRotateInstance(Math.toRadians(-90));
240: image_op_transform.concatenate(AffineTransform
241: .getTranslateInstance(0, -1
242: * (mParamFontLineMetrics.getAscent())));
243: mParamImageOp = new AffineTransformOp(image_op_transform,
244: mParamGraphics.getRenderingHints());
245: }
246: }
|