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;
021:
022: import com.nwoods.jgo.JGoRectangle;
023: import java.awt.Color;
024: import java.awt.Font;
025: import java.awt.Image;
026: import java.awt.Insets;
027: import java.awt.Point;
028: import java.awt.Rectangle;
029: import java.util.logging.Level;
030: import java.util.logging.Logger;
031:
032: import javax.swing.ImageIcon;
033: import com.nwoods.jgo.JGoBrush;
034: import com.nwoods.jgo.JGoImage;
035: import com.nwoods.jgo.JGoObject;
036: import com.nwoods.jgo.JGoPen;
037: import org.netbeans.modules.soa.mapper.basicmapper.canvas.jgo.util.AccessibleArea;
038: import org.netbeans.modules.soa.mapper.basicmapper.canvas.jgo.util.JGoLabel;
039:
040: /**
041: * <p>
042: *
043: * Title: </p> BasicTitleBarUI <p>
044: *
045: * Description: </p> BasicTitleBarUI provides an implemation of visiual
046: * JGoObject of the methoid canvas node title bar.<p>
047: *
048: * Copyright: Copyright (c) 2002 </p> <p>
049: *
050: * Company: </p>
051: *
052: * @author Un Seng Leong
053: * @created December 4, 2002
054: * @version 1.0
055: */
056: public class BasicTitleBarUI extends AccessibleArea {
057: /**
058: * the log instance
059: */
060: private static final Logger LOGGER = Logger
061: .getLogger(BasicTitleBarUI.class.getName());
062:
063: /**
064: * the default expend image file
065: */
066: private static final String EXPENDED_IMAGE_PATH = "expended.gif";
067:
068: /**
069: * the default collapse image file
070: */
071: private static final String COLLAPSED_IMAGE_PATH = "collapsed.gif";
072:
073: /**
074: * the default title bar background brush
075: */
076: private static final JGoBrush TITLE_BRUSH = JGoBrush
077: .makeStockBrush(new Color(255, 255, 255, 0)); // transparent
078:
079: private static final Color COLOR_INNER_BORDER = new Color(186, 205,
080: 240); // baby blue
081:
082: /**
083: * the default title bar text color
084: */
085: private static final Color DEFAULT_TEXT_COLOR = Color.BLACK;
086:
087: /**
088: * the number of left margin
089: */
090: private int mLeftSpace = 2;
091:
092: /**
093: * the space between title and icon
094: */
095: private int mTitleIconAndTextGap = 2;
096:
097: /**
098: * the space between title and button
099: */
100: private int mTitleTextAndButtonsGap = 2;
101:
102: /**
103: * the top margin
104: */
105: private int mTopSpace = 1;
106:
107: /**
108: * the bottom margin
109: */
110: private int mBottomSpace = 1;
111:
112: /**
113: * the botton space
114: */
115: private int mButtonsGap = 2;
116:
117: /**
118: * the min width of this title bar
119: */
120: private int minWidth = 0;
121:
122: /**
123: * the min height of this title bar
124: */
125: private int minHeight = 0;
126:
127: /**
128: * default collapse icon
129: */
130: private static Image mDefaultCollapsedIcon;
131:
132: /**
133: * default expend icon
134: */
135: private static Image mDefaultExpendedIcon;
136:
137: /**
138: * the border rectangle
139: */
140: private JGoRectangle mBorderRect;
141:
142: /**
143: * the collapse JGoImage instance
144: */
145: private JGoImage mCollapsedJGoImage;
146:
147: /**
148: * the expand JGoImage instance
149: */
150: private JGoImage mExpandedJGoImage;
151:
152: /**
153: * the title image JGoImage instance
154: */
155: private JGoImage mTitleJGoImage;
156:
157: /**
158: * the title lable instance
159: */
160: private JGoLabel mTitleLabel;
161:
162: static {
163: try {
164: mDefaultExpendedIcon = new ImageIcon(BasicTitleBarUI.class
165: .getResource(EXPENDED_IMAGE_PATH)).getImage();
166: } catch (java.lang.Throwable t) {
167: LOGGER.log(Level.SEVERE,
168: "unable to load default expended icon", t);
169: }
170:
171: try {
172: mDefaultCollapsedIcon = new ImageIcon(BasicTitleBarUI.class
173: .getResource(COLLAPSED_IMAGE_PATH)).getImage();
174: } catch (java.lang.Throwable t) {
175: LOGGER.log(Level.SEVERE,
176: "unable to load default collapsed icon", t);
177: }
178: }
179:
180: /**
181: * Creates a new BasicTitleBarUI object.
182: */
183: public BasicTitleBarUI() {
184: this (null);
185: }
186:
187: /**
188: * Creates a new BasicTitleBarUI object, with the specified title text.
189: *
190: * @param title the title text of this title bar
191: */
192: public BasicTitleBarUI(String title) {
193: this (title, null);
194: }
195:
196: /**
197: * Creates a new BasicTitleBarUI object, with the specified title text and
198: * icon.
199: *
200: * @param title the title text of this title bar
201: * @param titleIcon the title icon of this title bar
202: */
203: public BasicTitleBarUI(String title, Image titleIcon) {
204: this (title, titleIcon, mDefaultExpendedIcon,
205: mDefaultCollapsedIcon);
206: }
207:
208: /**
209: * Creates a new BasicTitleBarUI object, with the specified title text and
210: * icon, and expand and collapse button icon.
211: *
212: * @param title the title text of this title bar
213: * @param titleIcon the title icon of this title bar
214: * @param expendedIcon the expended icon image
215: * @param collapsedIcon the collapse icon image
216: */
217: public BasicTitleBarUI(String title, Image titleIcon,
218: Image expendedIcon, Image collapsedIcon) {
219: mBorderRect = new JGoRectangle();
220: mTitleJGoImage = new JGoImage();
221: mExpandedJGoImage = new JGoImage();
222: mCollapsedJGoImage = new JGoImage();
223: mTitleLabel = new JGoLabel(title);
224: mTitleLabel.setSelectable(false);
225: mTitleLabel.setInsets(new Insets(0, 0, 0, 0));
226: mTitleLabel.setPen(null);
227: mTitleLabel.setTextColor(DEFAULT_TEXT_COLOR);
228:
229: setTitleIcon(titleIcon);
230: setExpendedIcon(expendedIcon);
231: setCollapseIcon(collapsedIcon);
232:
233: mBorderRect.setSelectable(false);
234: mTitleLabel.setSelectable(false);
235: mTitleJGoImage.setSelectable(false);
236: mExpandedJGoImage.setSelectable(false);
237: mCollapsedJGoImage.setSelectable(false);
238:
239: mBorderRect.setResizable(false);
240: mTitleLabel.setResizable(false);
241: mTitleJGoImage.setResizable(false);
242: mExpandedJGoImage.setResizable(false);
243: mCollapsedJGoImage.setResizable(false);
244:
245: mTitleJGoImage.setDraggable(false);
246: mTitleLabel.setDraggable(false);
247: mExpandedJGoImage.setDraggable(false);
248: mCollapsedJGoImage.setDraggable(false);
249: mBorderRect.setDraggable(false);
250:
251: this .setResizable(false);
252: this .setSelectable(false);
253: this .setDraggable(true);
254:
255: mBorderRect.setBrush(TITLE_BRUSH);
256: mBorderRect.setPen(JGoPen.makeStockPen(COLOR_INNER_BORDER));
257:
258: this .addObjectAtTail(mTitleLabel);
259: this .addObjectAtTail(mTitleJGoImage);
260: this .addObjectAtTail(mExpandedJGoImage);
261: this .addObjectAtTail(mCollapsedJGoImage);
262: this .addObjectAtHead(mBorderRect);
263:
264: expand();
265: ensureSize();
266: }
267:
268: /**
269: * Return the collapse button icon
270: *
271: * @return the collapse button icon
272: */
273: public Image getCollapseIcon() {
274: return mCollapsedJGoImage.getImage();
275: }
276:
277: /**
278: * Return the expanded button icon
279: *
280: * @return the expanded button icon
281: */
282: public Image getExpendedIcon() {
283: return mExpandedJGoImage.getImage();
284: }
285:
286: /**
287: * Return the font to use.
288: *
289: * @return the font to use.
290: */
291: public Font getFont() {
292: return mTitleLabel.getFont();
293: }
294:
295: /**
296: * Return the title text
297: *
298: * @return the title text
299: */
300: public String getTitle() {
301: return mTitleLabel.getText();
302: }
303:
304: /**
305: * Retrun The title icon.
306: *
307: * @return The title icon.
308: */
309: public Image getTitleIcon() {
310: return mTitleJGoImage.getImage();
311: }
312:
313: /**
314: * Return true if the point is within the button of expand or collapse
315: * button. false otherwise.
316: *
317: * @param point the point ot check
318: * @return true if the point is within the button of expand or
319: * collapse button. false otherwise.
320: */
321: public boolean isInButton(Point point) {
322: Rectangle imageRect = null;
323:
324: if (mCollapsedJGoImage.isVisible()) {
325: imageRect = new Rectangle(mCollapsedJGoImage.getLocation(),
326: mCollapsedJGoImage.getSize());
327: } else {
328: imageRect = new Rectangle(mExpandedJGoImage.getLocation(),
329: mExpandedJGoImage.getSize());
330: }
331: return imageRect.contains(point);
332: }
333:
334: /**
335: * Set the collapse icon image
336: *
337: * @param image the collapse icon image
338: */
339: public void setCollapseIcon(Image image) {
340: if (image == null) {
341: this .removeObject(mCollapsedJGoImage);
342: mCollapsedJGoImage = new JGoImage();
343: mCollapsedJGoImage.setSelectable(false);
344: mCollapsedJGoImage.setDraggable(false);
345: mCollapsedJGoImage.setResizable(false);
346: mCollapsedJGoImage.setSize(0, 0);
347: this .addObjectAtTail(mCollapsedJGoImage);
348: } else {
349: mCollapsedJGoImage.loadImage(image, true);
350: mCollapsedJGoImage.setSize(image.getWidth(null), image
351: .getHeight(null));
352: }
353: ensureSize();
354: }
355:
356: /**
357: * Sets the expand icon image.
358: *
359: * @param image the expand icon image.
360: */
361: public void setExpendedIcon(Image image) {
362: if (image == null) {
363: this .removeObject(mExpandedJGoImage);
364: mExpandedJGoImage = new JGoImage();
365: mExpandedJGoImage.setSelectable(false);
366: mExpandedJGoImage.setDraggable(false);
367: mExpandedJGoImage.setResizable(false);
368: mExpandedJGoImage.setSize(0, 0);
369: this .addObjectAtTail(mExpandedJGoImage);
370: } else {
371: mExpandedJGoImage.loadImage(image, true);
372: mExpandedJGoImage.setSize(image.getWidth(null), image
373: .getHeight(null));
374: }
375: ensureSize();
376: }
377:
378: /**
379: * Set the title text
380: *
381: * @param title the title text
382: */
383: public void setTitle(String title) {
384: mTitleLabel.setText(title);
385: ensureSize();
386: }
387:
388: /**
389: * Set the title icon.
390: *
391: * @param image the title icon.
392: */
393: public void setTitleIcon(Image image) {
394: if (image == null) {
395: this .removeObject(mTitleJGoImage);
396: mTitleJGoImage = new JGoImage();
397: mTitleJGoImage.setSelectable(false);
398: mTitleJGoImage.setDraggable(false);
399: mTitleJGoImage.setResizable(false);
400: mTitleJGoImage.setSize(0, 0);
401: this .addObjectAtTail(mTitleJGoImage);
402: } else {
403: mTitleJGoImage.loadImage(image, true);
404: mTitleJGoImage.setSize(image.getWidth(null), image
405: .getHeight(null));
406: }
407: ensureSize();
408: }
409:
410: /**
411: * Collapse the title bar
412: */
413: public void collapse() {
414: mCollapsedJGoImage.setVisible(true);
415: mExpandedJGoImage.setVisible(false);
416: }
417:
418: /**
419: * Expand the title bar
420: */
421: public void expand() {
422: mCollapsedJGoImage.setVisible(false);
423: mExpandedJGoImage.setVisible(true);
424: }
425:
426: /**
427: * Invokes when the size or location of this title bar changes.
428: *
429: * @param prevRect the pervious rectangle
430: */
431: public void geometryChange(Rectangle prevRect) {
432: // see if this is just a move and not a scale
433: if ((prevRect.width == getWidth())
434: && (prevRect.height == getHeight())) {
435: // let the default JGoArea implementation do the work
436: super .geometryChange(prevRect);
437: } else {
438: ensureSize();
439: }
440: }
441:
442: public int getMinimumWidth() {
443: // minimum width =
444: // left border and title icon gap +
445: // title icon width +
446: // title icon and text gap +
447: // title label width +
448: // title lable and button gap +
449: // button width (pick the wider one between expended and collapse image) +
450: // button and right border gap
451: return mLeftSpace
452: + mTitleJGoImage.getWidth()
453: + mTitleIconAndTextGap
454: + mTitleLabel.getWidth()
455: + mTitleTextAndButtonsGap
456: + Math.max(mExpandedJGoImage.getWidth(),
457: mCollapsedJGoImage.getWidth()) + mButtonsGap;
458: }
459:
460: public int getMinimumHeight() {
461: // minimum height =
462: // top border space +
463: // height of title icon or title label or expended icon or collapsed icon, the longer one +
464: // bottom border space
465: return mTopSpace
466: + Math.max(Math.max(mTitleLabel.getHeight(),
467: mTitleJGoImage.getHeight()), Math.max(
468: mExpandedJGoImage.getWidth(),
469: mCollapsedJGoImage.getWidth())) + mBottomSpace;
470: }
471:
472: /**
473: * Resize itself to the minimum size.
474: */
475: public void resizeToMinimum() {
476: this .setSize(getMinimumWidth(), getMinimumHeight());
477: }
478:
479: private void ensureSize() {
480: int miniWidth = getMinimumWidth();
481: if (getWidth() < miniWidth) {
482: // this will trigger the JGo geometryChange framework and
483: // call this method recursively
484: setWidth(miniWidth);
485: return;
486: }
487: int miniHeight = getMinimumHeight();
488: if (getHeight() < miniHeight) {
489: // this will trigger the JGo geometryChange framework and
490: // call this method recursively
491: setHeight(miniHeight);
492: return;
493: }
494: layoutChildren();
495: }
496:
497: /**
498: * Layout children of this title bar.
499: */
500: protected void layoutChildren() {
501: mTitleJGoImage.setLeft(getLeft() + mLeftSpace);
502: mTitleJGoImage.setTop(getTop() + mTopSpace);
503:
504: mTitleLabel.setSpotLocation(JGoObject.TopLeft, mTitleJGoImage,
505: JGoObject.TopRight);
506: mTitleLabel.setLeft(mTitleLabel.getLeft()
507: + mTitleIconAndTextGap);
508:
509: mExpandedJGoImage.setSpotLocation(JGoObject.TopRight, this ,
510: JGoObject.TopRight);
511: mExpandedJGoImage.setLeft(mExpandedJGoImage.getLeft()
512: - mButtonsGap);
513: mExpandedJGoImage.setTop(mExpandedJGoImage.getTop()
514: + mButtonsGap);
515:
516: mCollapsedJGoImage.setSpotLocation(JGoObject.TopLeft,
517: mExpandedJGoImage, JGoObject.TopLeft);
518:
519: mBorderRect.setBoundingRect(this .getBoundingRect());
520: }
521:
522: public void setTitleBarBackground(Color color) {
523: // mBorderRect.setBrush(JGoBrush.makeStockBrush(color));
524: }
525:
526: public void resetTitleBarBackground() {
527: // mBorderRect.setBrush(TITLE_BRUSH);
528: }
529:
530: public Color getTitleBarBackground() {
531: return mBorderRect.getBrush().getColor();
532: }
533: }
|