001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.sql.framework.ui.graph.impl;
042:
043: import java.awt.Color;
044: import java.awt.Cursor;
045: import java.awt.Dimension;
046: import java.awt.Insets;
047: import java.awt.Paint;
048: import java.awt.Point;
049: import java.awt.Rectangle;
050: import java.net.URL;
051:
052: import javax.swing.Icon;
053: import javax.swing.ImageIcon;
054:
055: import com.nwoods.jgo.JGoArea;
056: import com.nwoods.jgo.JGoBrush;
057: import com.nwoods.jgo.JGoDocumentChangedEdit;
058: import com.nwoods.jgo.JGoDocumentEvent;
059: import com.nwoods.jgo.JGoImage;
060: import com.nwoods.jgo.JGoObject;
061: import com.nwoods.jgo.JGoPen;
062: import com.nwoods.jgo.JGoRectangle;
063: import com.nwoods.jgo.JGoView;
064:
065: /**
066: * @author Ritesh Adval
067: * @version $Revision$
068: */
069: public class TitleArea extends CanvasArea {
070:
071: /**
072: * expansion state of this area
073: */
074: public static final int EXPANDED = 0;
075:
076: /**
077: * collapse state of this area
078: */
079: public static final int COLLAPSED = 1;
080:
081: //Events hints
082: /**
083: * a CHANGED JGoDocumentEvent or JGoViewEvent hint, by default sent to the Parent of
084: * the TableTitleArea. the new value sent event will be either
085: *
086: * @see com.nwoods.jgo.examples.sql.table.TableTitleArea#EXPANDED or
087: * @see com.nwoods.jgo.examples.sql.table.TableTitleArea#COLLAPSED toggle between
088: * expansion and collapse state
089: */
090: public static final int EXPANSION_STATE_CHANGED = JGoDocumentEvent.LAST + 50001;
091:
092: private static final Color DEFAULT_BG_COLOR = new Color(254, 253,
093: 235);
094:
095: private int state = EXPANDED;
096:
097: private boolean showExpImage = true;
098:
099: private BasicText title;
100: private JGoImage titleImg;
101: private ImageArea expandedImg;
102: private ImageArea collapsedImg;
103: private JGoRectangle rect;
104:
105: private URL expandedImgUrl = getClass()
106: .getResource(
107: "/org/netbeans/modules/sql/framework/ui/resources/images/expanded.gif");
108: private URL collapsedImgUrl = getClass()
109: .getResource(
110: "/org/netbeans/modules/sql/framework/ui/resources/images/collapsed.gif");
111: private URL titleImgUrl = getClass()
112: .getResource(
113: "/org/netbeans/modules/sql/framework/ui/resources/images/Table32.gif");
114:
115: // some constants for arranging various children
116: private int titleIconAndTextGap = 2;
117: private int textAndExpandedImgGap = 2;
118: //private int textAndCheckBoxGap = 2;
119: private int textTopGap = 0;
120: private int minWidth = 0;
121:
122: /**
123: * Creates a new instance of TableTitleArea
124: *
125: * @param titleStr title string
126: */
127: public TitleArea(String titleStr) {
128: this .setSelectable(false);
129: this .setResizable(false);
130:
131: // add the bounding display rectangle
132: rect = new InsetsRectangle();
133: rect.setPen(JGoPen.makeStockPen(Color.WHITE));
134: rect.setBrush(JGoBrush.makeStockBrush(DEFAULT_BG_COLOR));
135: rect.setSelectable(false);
136: rect.setResizable(false);
137: addObjectAtHead(rect);
138:
139: // add text of title
140: title = new BasicText(titleStr);
141: title.setEditable(false);
142: title.setSelectable(false);
143: title.setResizable(false);
144: title.setBold(true);
145: title.setTransparent(true);
146: addObjectAtTail(title);
147:
148: // add title image
149: titleImg = new JGoImage();
150:
151: titleImg.loadImage(titleImgUrl, true);
152: titleImg.setSize(titleImg.getImage().getWidth(null), titleImg
153: .getImage().getHeight(null));
154: titleImg.setSelectable(false);
155: titleImg.setResizable(false);
156:
157: addObjectAtTail(titleImg);
158:
159: // add expansion image
160: expandedImg = new ImageArea(expandedImgUrl);
161:
162: addObjectAtTail(expandedImg);
163:
164: // add collapse image
165: collapsedImg = new ImageArea(collapsedImgUrl);
166: collapsedImg.setVisible(false);
167: addObjectAtTail(collapsedImg);
168:
169: this .insets = new Insets(3, 3, 3, 3);
170: }
171:
172: /**
173: * set the title image
174: *
175: * @param icon icon
176: */
177: public void setTitleImage(Icon icon) {
178: ImageIcon imgIcon = (ImageIcon) icon;
179: titleImg.loadImage(imgIcon.getImage(), false);
180: titleImg.setSize(imgIcon.getImage().getWidth(null), imgIcon
181: .getImage().getHeight(null));
182:
183: // Force recalculation of minimum width.
184: minWidth = 0;
185: getMinimumWidth();
186: this .update();
187: }
188:
189: /**
190: * set the expanded image
191: *
192: * @param icon icon
193: */
194: public void setExpandedImage(Icon icon) {
195: expandedImg.setIcon(icon);
196:
197: minWidth = 0;
198: getMinimumWidth();
199: update();
200: }
201:
202: /**
203: * set the flag so that this text are will calculate for ...
204: *
205: * @param show whether a calculation for ... is to be done
206: */
207: public void setShowDot(boolean show) {
208: title.setShowDot(show);
209: }
210:
211: /**
212: * set the gap between text and expanded or collapsed images
213: *
214: * @param gap the gap
215: */
216: public void setTextAndExpandedImgGap(int gap) {
217: textAndExpandedImgGap = gap;
218:
219: minWidth = 0;
220: getMinimumWidth();
221: update();
222: }
223:
224: /**
225: * set the collapsed image
226: *
227: * @param icon icon
228: */
229: public void setCollapsedImage(Icon icon) {
230: collapsedImg.setIcon(icon);
231:
232: minWidth = 0;
233: getMinimumWidth();
234: update();
235: }
236:
237: /**
238: * initialize the title area
239: *
240: * @param loc location
241: */
242: public void initialize(Point loc) {
243: this .setBoundingRect(loc, new Dimension(getMaximumWidth(),
244: getMaximumHeight()));
245: }
246:
247: /**
248: * get the maximum width
249: *
250: * @return maximum width
251: */
252: public int getMaximumWidth() {
253: int maxWidth = 0;
254: if (title != null) {
255: maxWidth += title.getMaximumWidth();
256: maxWidth += titleIconAndTextGap;
257: }
258:
259: if (titleImg != null) {
260: maxWidth += titleImg.getWidth();
261: }
262:
263: if (getState() == EXPANDED) {
264: if (expandedImg != null) {
265: maxWidth += expandedImg.getWidth();
266: maxWidth += textAndExpandedImgGap;
267: }
268: } else {
269: if (collapsedImg != null) {
270: maxWidth += collapsedImg.getWidth();
271: maxWidth += textAndExpandedImgGap;
272: }
273: }
274:
275: maxWidth += getInsets().left + getInsets().right;
276:
277: return maxWidth;
278: }
279:
280: /**
281: * minimum width will be width of title image plus width of title plus width of expand
282: * image
283: *
284: * @return minimum width
285: */
286: public int getMinimumWidth() {
287: //calculate min width only at once
288: if (minWidth != 0) {
289: return minWidth;
290: }
291:
292: minWidth = getMaximumWidth();
293: return minWidth;
294: }
295:
296: /**
297: * get the maximum height
298: *
299: * @return maximum height
300: */
301: public int getMaximumHeight() {
302: int maxHeight = 0;
303:
304: if (title != null) {
305: maxHeight = title.getHeight();
306: }
307:
308: if (titleImg != null && maxHeight < titleImg.getHeight()) {
309: maxHeight = titleImg.getHeight();
310: }
311:
312: if (getState() == EXPANDED) {
313: if (expandedImg != null
314: && maxHeight < expandedImg.getHeight()) {
315: maxHeight = expandedImg.getHeight();
316: }
317: } else {
318: if (collapsedImg != null
319: && maxHeight < collapsedImg.getHeight()) {
320: maxHeight = collapsedImg.getHeight();
321: }
322: }
323:
324: maxHeight += getInsets().top + getInsets().bottom;
325:
326: return maxHeight;
327: }
328:
329: /**
330: * get minimum height. This will be equal to maximum height
331: *
332: * @return minimum height
333: */
334: public int getMinimumHeight() {
335: return getMaximumHeight();
336: }
337:
338: /**
339: * set the text of the title
340: *
341: * @param text text
342: */
343: public void setTitle(String text) {
344: title.setOriginalText(text);
345:
346: minWidth = 0;
347: getMinimumWidth();
348: update();
349: }
350:
351: /**
352: * toggle the state
353: *
354: * @param point point
355: * @return boolean
356: */
357: public boolean toggleState(Point point) {
358: boolean returnFlag = false;
359:
360: Rectangle imageRect = null;
361: if (collapsedImg.isVisible()) {
362: imageRect = new Rectangle(collapsedImg.getLocation(),
363: collapsedImg.getSize());
364: } else {
365: imageRect = new Rectangle(expandedImg.getLocation(),
366: expandedImg.getSize());
367: }
368:
369: //if user clicked on expansion or collapse images
370: if (imageRect.contains(point)) {
371: if (getState() == EXPANDED) {
372: setState(COLLAPSED);
373: } else {
374: setState(EXPANDED);
375: }
376: returnFlag = true;
377: }
378:
379: return returnFlag;
380: }
381:
382: /**
383: * get the state
384: *
385: * @return state
386: */
387: public int getState() {
388: return state;
389: }
390:
391: /**
392: * set the state
393: *
394: * @param newState newState
395: */
396: public void setState(int newState) {
397: if (this .state == newState) {
398: return;
399: }
400:
401: int oldState = this .state;
402: this .state = newState;
403:
404: if (!showExpImage) {
405: return;
406: }
407:
408: //change the image
409: if (newState == EXPANDED) {
410: expandedImg.setVisible(true);
411: collapsedImg.setVisible(false);
412: } else {
413: collapsedImg.setVisible(true);
414: expandedImg.setVisible(false);
415: }
416:
417: //fire CHANGED JGoDocumentEvent or JGoViewEvent hint
418: update(EXPANSION_STATE_CHANGED, oldState, null);
419:
420: //by default notify parent of this area
421: JGoArea parent = this .getParent();
422: if (parent != null) {
423: parent.update(EXPANSION_STATE_CHANGED, oldState, null);
424: }
425: }
426:
427: /**
428: * copy new value for redo
429: *
430: * @param e JGoDocumentChangedEdit
431: */
432: public void copyNewValueForRedo(JGoDocumentChangedEdit e) {
433: if (e.getFlags() == EXPANSION_STATE_CHANGED) {
434: } else {
435: super .copyNewValueForRedo(e);
436: }
437: }
438:
439: /**
440: * Change the cursor at the port
441: *
442: * @param flags
443: */
444: public boolean doUncapturedMouseMove(int flags, Point dc, Point vc,
445: JGoView view) {
446: if (getLayer() != null && getLayer().isModifiable()) {
447: view.setCursor(Cursor
448: .getPredefinedCursor(Cursor.MOVE_CURSOR));
449: return true;
450: }
451: return false;
452: }
453:
454: /**
455: * layout the children of this cell area
456: */
457: public void layoutChildren() {
458: Rectangle rectangle = this .getBoundingRect();
459: rect.setBoundingRect(rectangle);
460:
461: Insets insets1 = getInsets();
462:
463: int x = this .getLeft() + insets1.left;
464: int y = this .getTop() + insets1.top;
465: int width = this .getWidth() - insets1.left - insets1.right;
466: int height = this .getHeight() - insets1.top - insets1.bottom;
467:
468: // arrange title image
469: titleImg.setBoundingRect(x, y, titleImg.getWidth(), titleImg
470: .getHeight());
471:
472: // arrange title
473: int nonTitleWidth = titleImg.getWidth() + titleIconAndTextGap;
474:
475: if (getState() == EXPANDED) {
476: if (expandedImg != null) {
477: nonTitleWidth += expandedImg.getWidth();
478: nonTitleWidth += textAndExpandedImgGap;
479: }
480: } else {
481: if (collapsedImg != null) {
482: nonTitleWidth += collapsedImg.getWidth();
483: nonTitleWidth += textAndExpandedImgGap;
484: }
485: }
486:
487: title.setBoundingRect(x + titleImg.getWidth()
488: + titleIconAndTextGap, y + textTopGap, width
489: - nonTitleWidth, height);
490:
491: if (expandedImg != null && collapsedImg != null) {
492: //arrange expansion image
493: expandedImg.setLocation(x + width - expandedImg.getWidth(),
494: y);
495:
496: //arrange collapse image
497: collapsedImg.setSpotLocation(JGoObject.TopLeft,
498: expandedImg, JGoObject.TopLeft);
499: }
500: }
501:
502: class ImageArea extends CanvasArea {
503: JGoImage image;
504:
505: public ImageArea(URL imgUrl) {
506: super ();
507: this .setSelectable(true);
508: this .setResizable(false);
509: this .setPickableBackground(false);
510:
511: image = new JGoImage();
512: image.loadImage(imgUrl, true);
513: image.setSize(image.getImage().getWidth(null), image
514: .getImage().getHeight(null));
515: image.setSelectable(false);
516: image.setResizable(false);
517: addObjectAtTail(image);
518:
519: }
520:
521: public void setIcon(Icon icon) {
522: ImageIcon imgIcon = (ImageIcon) icon;
523: image.loadImage(imgIcon.getImage(), false);
524: image.setSize(imgIcon.getImage().getWidth(null), imgIcon
525: .getImage().getHeight(null));
526:
527: }
528:
529: /**
530: * redirect selction to a differnt object
531: *
532: * @return object
533: */
534: public JGoObject redirectSelection() {
535: //temporary change the selection color to white
536: //JGoView.setDefaultPrimarySelectionColor(Color.RED);
537: return this ;
538: }
539:
540: /**
541: * called when this object is selected
542: */
543: public void gainedSelection() {
544: //now change the seletion color back
545: //JGoView.setDefaultPrimarySelectionColor(JGoView.getDefaultSecondarySelectionColor());
546: }
547:
548: /**
549: * layout the children of this cell area
550: */
551: public void layoutChildren() {
552: Rectangle rectangle = this .getBoundingRect();
553: image.setBoundingRect(rectangle);
554: }
555:
556: /**
557: * handle mouse click and do expansion or collapse
558: *
559: * @param modifiers mouse modifers
560: * @param dc document point
561: * @param vc view point
562: * @param view view
563: * @return boolean
564: */
565: public boolean doMouseClick(int modifiers, Point dc, Point vc,
566: JGoView view) {
567: return ((TitleArea) this .getParent()).toggleState(dc);
568: }
569:
570: /**
571: * Change the cursor at the port
572: *
573: * @param flags
574: */
575: public boolean doUncapturedMouseMove(int flags, Point dc,
576: Point vc, JGoView view) {
577: //System.out.println("image bounding rect " + this.getBoundingRect() + "dc "+
578: // dc + "vc " + vc);
579: if (getLayer() != null && getLayer().isModifiable()) {
580: view.setCursor(Cursor
581: .getPredefinedCursor(Cursor.DEFAULT_CURSOR));
582: return true;
583: }
584: return false;
585: }
586: }
587:
588: public void showExpansionImage(boolean show) {
589: this .showExpImage = show;
590: expandedImg.setVisible(show);
591: collapsedImg.setVisible(show);
592: this .layoutChildren();
593: }
594:
595: public void setBackgroundColor(Color c) {
596: if (this .rect != null) {
597: this .rect.setBrush(JGoBrush.makeStockBrush(c));
598: }
599: }
600:
601: public void setBackgroundPaint(Paint p) {
602: if (this .rect != null) {
603: this .rect.setBrush(new JGoBrush(p));
604: }
605: }
606:
607: public void setPen(JGoPen newPen) {
608: rect.setPen(newPen);
609: }
610:
611: public JGoPen getPen() {
612: return rect.getPen();
613: }
614:
615: public void setBrush(JGoBrush newBrush) {
616: rect.setBrush(newBrush);
617: }
618: }
|