001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.soa.mapper.basicmapper.canvas.jgo.util;
021:
022: import java.awt.Color;
023: import java.awt.Dimension;
024: import java.awt.Font;
025: import java.awt.Graphics;
026: import java.awt.GraphicsConfiguration;
027: import java.awt.GraphicsEnvironment;
028: import java.awt.Insets;
029: import java.awt.Point;
030: import java.awt.Rectangle;
031: import java.awt.image.VolatileImage;
032: import com.nwoods.jgo.JGoBrush;
033: import com.nwoods.jgo.JGoObject;
034: import com.nwoods.jgo.JGoPen;
035: import com.nwoods.jgo.JGoRectangle;
036: import com.nwoods.jgo.JGoText;
037: import com.nwoods.jgo.JGoView;
038: import javax.swing.JLabel;
039:
040: /**
041: * <p>
042: *
043: * Title: </p> <p>
044: *
045: * Description: </p> <p>
046: *
047: * Copyright: Copyright (c) 2002 </p> <p>
048: *
049: * Company: </p>
050: *
051: * @author Un Seng Leong
052: * @created December 4, 2002
053: * @version 1.0
054: */
055: public class JGoLabel extends AccessibleArea {
056:
057: public static final int CENTER = 0;
058: public static final int LEFT = 1;
059: public static final int RIGHT = 2;
060: public static final int TOP = 3;
061: public static final int BOTTOM = 4;
062:
063: static {
064: Font FONT_DEFAULT = new JLabel().getFont();
065: JGoText.setDefaultFontFaceName(FONT_DEFAULT.getFontName());
066: JGoText.setDefaultFontSize(FONT_DEFAULT.getSize());
067: }
068:
069: private int mVerticalAlignment = CENTER;
070: private int mHorizonalAligment = CENTER;
071: private Insets mBorderSpace = new Insets(0, 8, 0, 5);
072: private JGoRectangle mBoundRect;
073: private JGoText mTextLabel;
074:
075: /**
076: * this image holds the screen graphic to calculate the text width hight for this label.
077: */
078: private static VolatileImage mGraphicHolder = createGraphicImage();
079:
080: /**
081: * the maximum pixel length that our text can span
082: */
083: private static final int MAX_TEXT_WIDTH = 250;
084:
085: /**
086: * what gets catenated on to the end of a string that exceeds the MAX_TEXT_WIDTH
087: */
088: private static final String ELLIPSES = "...";
089:
090: /**
091: * Creates a new JGoLabel object.
092: */
093: public JGoLabel() {
094: super ();
095: initialize();
096: }
097:
098: /**
099: * Creates a new JGoLabel object.
100: *
101: * @param text DOCUMENT ME!
102: */
103: public JGoLabel(java.lang.String text) {
104: this ();
105: this .setText(text);
106: }
107:
108: public JGoBrush getBrush() {
109: return mBoundRect.getBrush();
110: }
111:
112: public Font getFont() {
113: return mTextLabel.getFont();
114: }
115:
116: public int getFontSize() {
117: return mTextLabel.getFontSize();
118: }
119:
120: public int getHorizonalAligment() {
121: return mHorizonalAligment;
122: }
123:
124: public Insets getInsets() {
125: return mBorderSpace;
126: }
127:
128: public JGoPen getPen() {
129: return mBoundRect.getPen();
130: }
131:
132: public String getText() {
133: return mTextLabel.getText();
134: }
135:
136: public Color getTextColor() {
137: return mTextLabel.getTextColor();
138: }
139:
140: public int getVerticalAligment() {
141: return mVerticalAlignment;
142: }
143:
144: public boolean isUnderline() {
145: return mTextLabel.isUnderline();
146: }
147:
148: public void setBrush(JGoBrush brush) {
149: mBoundRect.setBrush(brush);
150: }
151:
152: public void setBold(boolean isBold) {
153: mTextLabel.setBold(isBold);
154: }
155:
156: public void setItalic(boolean isItalic) {
157: mTextLabel.setItalic(isItalic);
158: }
159:
160: public void setFontSize(int fontSize) {
161: mTextLabel.setFontSize(fontSize);
162: ensureLabelSize();
163: }
164:
165: public void setHorizonalAligment(int horizonalAligment) {
166: if ((horizonalAligment != CENTER)
167: && (horizonalAligment != LEFT)
168: && (horizonalAligment != RIGHT)) {
169: throw new java.lang.IllegalArgumentException(
170: "Invalid horizonal aligment; either LEFT, CENTER, RIGHT");
171: }
172:
173: mHorizonalAligment = horizonalAligment;
174: layoutLabel();
175: }
176:
177: public void setInsets(Insets border) {
178: mBorderSpace = border;
179: ensureLabelSize();
180: }
181:
182: public void setPen(JGoPen pen) {
183: mBoundRect.setPen(pen);
184: }
185:
186: public void setText(String text) {
187: String restrictedText = restrictTextWidth(text);
188: mTextLabel.setText(restrictedText);
189: ensureLabelSize();
190: layoutChildren();
191: }
192:
193: private String restrictTextWidth(String text) {
194: if (mGraphicHolder.validate(getDefaultConfiguration()) == VolatileImage.IMAGE_INCOMPATIBLE) {
195: mGraphicHolder = createGraphicImage();
196: }
197: Graphics g = mGraphicHolder.getGraphics();
198: g.setFont(mTextLabel.getFont());
199: int desiredTextWidth = g.getFontMetrics().stringWidth(ELLIPSES);
200: StringBuffer buf = new StringBuffer();
201: char[] chars = text.toCharArray();
202: int charSize = 0;
203: for (int i = 0, size = chars.length; i < size; i++) {
204: charSize = g.getFontMetrics().stringWidth(
205: String.valueOf(chars[i]));
206: if (desiredTextWidth + charSize <= MAX_TEXT_WIDTH) {
207: buf.append(chars[i]);
208: desiredTextWidth += charSize;
209: } else {
210: buf.append(ELLIPSES);
211: break;
212: }
213: }
214: return buf.toString();
215: }
216:
217: public void setTextColor(Color textColor) {
218: mTextLabel.setTextColor(textColor);
219: }
220:
221: public void setUnderline(boolean underline) {
222: mTextLabel.setUnderline(underline);
223: }
224:
225: public void setVerticalAligment(int verticalAligment) {
226: if ((verticalAligment != CENTER) && (verticalAligment != TOP)
227: && (verticalAligment != BOTTOM)) {
228: throw new java.lang.IllegalArgumentException(
229: "Invalid vertical aligment; either TOP, CENTER, BOTTOM");
230: }
231:
232: mVerticalAlignment = verticalAligment;
233: layoutLabel();
234: }
235:
236: public void geometryChange(Rectangle prevRect) {
237: // see if this is just a move and not a scale
238: if ((prevRect.width == getWidth())
239: && (prevRect.height == getHeight())) {
240: // let the default JGoArea implementation do the work
241: super .geometryChange(prevRect);
242: } else {
243: ensureSize();
244: }
245: }
246:
247: public void doStartEditing(JGoView view, Point poc) {
248: mTextLabel.doStartEdit(view, poc);
249: }
250:
251: /**
252: * Return the minimum width of this label. It depends on the text width, and
253: * the border left and right space.
254: *
255: * @return return the minimum width of this label
256: */
257: public int getMinimumWidth() {
258: return mTextLabel.getWidth() + mBorderSpace.left
259: + mBorderSpace.right;
260: }
261:
262: /**
263: * Return the minimum height of this label. It depends on the text height,
264: * and border top and bottom space.
265: *
266: * @return The minimumHeight value
267: */
268: public int getMinimumHeight() {
269: return mTextLabel.getHeight() + mBorderSpace.top
270: + mBorderSpace.bottom;
271: }
272:
273: /**
274: * Resize itself to the minimum size.
275: */
276: public void resizeToMinimum() {
277: this .setSize(getMinimumWidth(), getMinimumHeight());
278: }
279:
280: // relocate the children due to the location change
281: /**
282: * Description of the Method
283: */
284: protected void layoutChildren() {
285: layoutLabel();
286: mBoundRect.setBoundingRect(getBoundingRect());
287: }
288:
289: /**
290: * Return the system default graphics configuration.
291: *
292: * @return GraphicsConfiguration the os system default graphics configuration
293: */
294: private static GraphicsConfiguration getDefaultConfiguration() {
295: return GraphicsEnvironment.getLocalGraphicsEnvironment()
296: .getDefaultScreenDevice().getDefaultConfiguration();
297: }
298:
299: /**
300: * Return the image for calculating the text width and height.
301: *
302: * @return VolatileImage the image is created by the default graphics configuration.
303: */
304: private static VolatileImage createGraphicImage() {
305: return getDefaultConfiguration().createCompatibleVolatileImage(
306: 1, 1);
307: }
308:
309: private void ensureLabelSize() {
310: if (mGraphicHolder.validate(getDefaultConfiguration()) == VolatileImage.IMAGE_INCOMPATIBLE) {
311: mGraphicHolder = createGraphicImage();
312: }
313:
314: Graphics g = mGraphicHolder.getGraphics();
315:
316: g.setFont(mTextLabel.getFont());
317:
318: int textWidth = g.getFontMetrics().stringWidth(
319: mTextLabel.getText());
320: int textHeight = g.getFontMetrics(mTextLabel.getFont())
321: .getHeight();
322:
323: if (textWidth != mTextLabel.getWidth()
324: || textHeight != mTextLabel.getHeight()) {
325: mTextLabel.setSize(new Dimension(textWidth, textHeight));
326: ensureSize();
327: }
328: }
329:
330: private void ensureSize() {
331:
332: // the width and height must be separated compare.
333: int requireWidth = this .getMinimumWidth();
334: if (getWidth() < requireWidth) {
335: // this will trigger the JGo geometryChange framework and
336: // call this method recursively
337: this .setWidth(requireWidth);
338: return;
339: }
340:
341: int requireHeight = this .getMinimumHeight();
342: if (getHeight() < requireHeight) {
343: // this will trigger the JGo geometryChange framework and
344: // call this method recursively
345: this .setHeight(requireHeight);
346: return;
347: }
348:
349: layoutChildren();
350: }
351:
352: private void initialize() {
353: mTextLabel = new JGoText();
354: mTextLabel.setSelectable(false);
355: mTextLabel.setDraggable(false);
356: mTextLabel.setAutoResize(false);
357: mTextLabel.setEditable(false);
358: mTextLabel.setResizable(false);
359: mTextLabel.setTransparent(true);
360: mTextLabel.setAlignment(JGoText.ALIGN_CENTER);
361:
362: mBoundRect = new JGoRectangle();
363: mBoundRect.setSelectable(false);
364: mBoundRect.setDraggable(false);
365: mBoundRect.setResizable(false);
366: mBoundRect.setPen(JGoPen.make(JGoPen.SOLID, 1, Color.BLACK));
367:
368: addObjectAtTail(mBoundRect);
369: addObjectAtTail(mTextLabel);
370: }
371:
372: private void layoutLabel() {
373: switch (mVerticalAlignment) {
374: case CENTER:
375:
376: switch (mHorizonalAligment) {
377: case CENTER:
378: mTextLabel.setSpotLocation(JGoObject.Center, this ,
379: JGoObject.Center);
380:
381: if (mBorderSpace.left > 0) {
382: int diff = mTextLabel.getLeft() - this .getLeft();
383:
384: if (diff < mBorderSpace.left) {
385: mTextLabel.setLeft(mTextLabel.getLeft()
386: + (mBorderSpace.left - diff));
387: }
388: }
389:
390: if (mBorderSpace.right > 0) {
391: int leftdiff = mTextLabel.getLeft()
392: - this .getLeft();
393: int rightdiff = this .getWidth()
394: - mTextLabel.getWidth() - leftdiff;
395:
396: if (rightdiff < mBorderSpace.right) {
397: mTextLabel.setLeft(mTextLabel.getLeft()
398: - (mBorderSpace.right - rightdiff));
399: }
400: }
401:
402: if (mBorderSpace.top > 0) {
403: int diff = mTextLabel.getTop() - this .getTop();
404:
405: if (diff < mBorderSpace.top) {
406: mTextLabel.setTop(mTextLabel.getTop()
407: + (mBorderSpace.top - diff));
408: }
409: }
410:
411: if (mBorderSpace.bottom > 0) {
412: int topdiff = mTextLabel.getTop() - this .getTop();
413: int bottomdiff = this .getHeight()
414: - mTextLabel.getHeight() - topdiff;
415:
416: if (bottomdiff < mBorderSpace.bottom) {
417: mTextLabel.setTop(mTextLabel.getTop()
418: - (mBorderSpace.bottom - bottomdiff));
419: }
420: }
421:
422: break;
423: case LEFT:
424: mTextLabel.setSpotLocation(JGoObject.LeftCenter, this ,
425: JGoObject.LeftCenter);
426:
427: if (mBorderSpace.left > 0) {
428: mTextLabel.setLeft(mTextLabel.getLeft()
429: + mBorderSpace.left);
430: }
431:
432: break;
433: case RIGHT:
434: mTextLabel.setSpotLocation(JGoObject.RightCenter, this ,
435: JGoObject.RightCenter);
436:
437: if (mBorderSpace.right > 0) {
438: mTextLabel.setLeft(mTextLabel.getLeft()
439: - mBorderSpace.left);
440: }
441:
442: break;
443: }
444:
445: break;
446: case TOP:
447:
448: switch (mHorizonalAligment) {
449: case CENTER:
450: mTextLabel.setSpotLocation(JGoObject.TopCenter, this ,
451: JGoObject.TopCenter);
452:
453: break;
454: case LEFT:
455: mTextLabel.setSpotLocation(JGoObject.TopLeft, this ,
456: JGoObject.TopLeft);
457:
458: if (mBorderSpace.left > 0) {
459: mTextLabel.setLeft(mTextLabel.getLeft()
460: + mBorderSpace.left);
461: }
462:
463: break;
464: case RIGHT:
465: mTextLabel.setSpotLocation(JGoObject.TopRight, this ,
466: JGoObject.TopRight);
467:
468: if (mBorderSpace.right > 0) {
469: mTextLabel.setLeft(mTextLabel.getLeft()
470: - mBorderSpace.left);
471: }
472:
473: break;
474: }
475:
476: if (mBorderSpace.top > 0) {
477: mTextLabel.setTop(mTextLabel.getTop()
478: + mBorderSpace.top);
479: }
480:
481: break;
482: case BOTTOM:
483:
484: switch (mHorizonalAligment) {
485: case CENTER:
486: mTextLabel.setSpotLocation(JGoObject.BottomCenter,
487: this , JGoObject.BottomCenter);
488:
489: break;
490: case LEFT:
491: mTextLabel.setSpotLocation(JGoObject.BottomLeft, this ,
492: JGoObject.BottomLeft);
493:
494: if (mBorderSpace.left > 0) {
495: mTextLabel.setLeft(mTextLabel.getLeft()
496: + mBorderSpace.left);
497: }
498:
499: break;
500: case RIGHT:
501: mTextLabel.setSpotLocation(JGoObject.BottomRight, this ,
502: JGoObject.BottomRight);
503:
504: if (mBorderSpace.right > 0) {
505: mTextLabel.setLeft(mTextLabel.getLeft()
506: - mBorderSpace.left);
507: }
508:
509: break;
510: }
511:
512: if (mBorderSpace.bottom > 0) {
513: mTextLabel.setTop(mTextLabel.getTop()
514: - mBorderSpace.bottom);
515: }
516: }
517: }
518: }
|