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.Insets;
045: import java.awt.Paint;
046: import java.awt.Point;
047: import java.awt.Rectangle;
048:
049: import org.netbeans.modules.sql.framework.ui.graph.IGraphInterface;
050:
051: import com.nwoods.jgo.JGoBrush;
052: import com.nwoods.jgo.JGoObject;
053: import com.nwoods.jgo.JGoPen;
054: import com.nwoods.jgo.JGoRectangle;
055:
056: /**
057: * @author Ritesh Adval
058: * @author Jonathan Giron
059: * @version $Revision$
060: */
061: public class BasicTableArea extends BasicCanvasArea implements
062: IGraphInterface {
063:
064: /**
065: * table area
066: */
067: protected TableArea tableArea;
068:
069: /**
070: * toolbar area
071: */
072: protected ToolBarArea toolBarArea;
073:
074: private JGoRectangle rect;
075:
076: private int titleToolBarGap = 2;
077: private int toolBarTableGap = 2;
078:
079: private boolean drawBoundingRect = false;
080:
081: /** Creates a new instance of SQLTableArea */
082: public BasicTableArea() {
083: super ();
084: if (drawBoundingRect) {
085: this .insets = new Insets(5, 5, 5, 5);
086:
087: rect = new JGoRectangle();
088: rect.setPen(JGoPen.makeStockPen(Color.lightGray));
089: rect.setBrush(JGoBrush.makeStockBrush(new Color(241, 249,
090: 253)));
091: rect.setResizable(false);
092: addObjectAtTail(rect);
093: }
094: }
095:
096: /**
097: * create a new instance of table
098: *
099: * @param nTitleArea titleArea
100: * @param toolBarArea toolBar Area
101: * @param nTableArea table area
102: */
103: public BasicTableArea(TitleArea nTitleArea,
104: ToolBarArea toolBarArea, TableArea nTableArea) {
105: this ();
106: this .titleArea = nTitleArea;
107: addObjectAtTail(titleArea);
108:
109: this .toolBarArea = toolBarArea;
110: addObjectAtTail(toolBarArea);
111:
112: this .tableArea = nTableArea;
113: addObjectAtTail(tableArea);
114:
115: }
116:
117: /**
118: * initialize the table
119: *
120: * @param loc location
121: * @param nTitleArea titleArea
122: * @param nTableArea nTableArea
123: */
124: public void initialize(Point loc, TitleArea nTitleArea,
125: TableArea nTableArea) {
126: this .titleArea = nTitleArea;
127: addObjectAtTail(titleArea);
128:
129: this .tableArea = nTableArea;
130: addObjectAtTail(tableArea);
131:
132: this .setBoundingRect(loc.x, loc.y, getMaximumWidth(),
133: getMaximumHeight());
134:
135: }
136:
137: /**
138: * set the title of the table
139: *
140: * @param title title
141: */
142: public void setTitle(String title) {
143: titleArea.setTitle(title);
144: }
145:
146: /**
147: * get the table area
148: *
149: * @return table area
150: */
151: public TableArea getTableArea() {
152: return tableArea;
153: }
154:
155: /**
156: * set the gap between title and toolbar
157: *
158: * @param gap gap between title and tool bar
159: */
160: public void setTitleToolBarGap(int gap) {
161: this .titleToolBarGap = gap;
162: }
163:
164: /**
165: * set the gap between toolbar and table
166: *
167: * @param gap gap
168: */
169: public void setToolBarTableGap(int gap) {
170: this .toolBarTableGap = gap;
171: }
172:
173: /**
174: * set the state
175: *
176: * @param sExpanded whether table is expanded
177: */
178: public void setExpanded(boolean sExpanded) {
179: tableArea.setExpanded(sExpanded);
180: //make this table resizeable only in expanded mode
181: this .setResizable(sExpanded);
182:
183: super .setExpanded(sExpanded);
184: }
185:
186: /**
187: * draw the bounding rectangle
188: *
189: * @param draw whether to draw a bounding rect
190: */
191: public void setDrawBoundingRect(boolean draw) {
192: drawBoundingRect = draw;
193: }
194:
195: /**
196: * Is a bounding rectangle is drawn over this area
197: *
198: * @return whether there is a bounding rectangle drawn
199: */
200: public boolean isDrawBoundingRect() {
201: return drawBoundingRect;
202: }
203:
204: /**
205: * get the maximum width
206: *
207: * @return maximum width
208: */
209: public int getMaximumWidth() {
210: int maxWidth = 0;
211:
212: //always take the width of TableArea..
213: if (tableArea != null) {
214: maxWidth = tableArea.getMaximumWidth();
215: }
216:
217: //TableTitleArea may be longer than TableArea due to title name
218: if (titleArea != null) {
219: maxWidth = Math.max(maxWidth, titleArea.getMaximumWidth());
220: }
221:
222: // Always account for horizontal insets.
223: maxWidth += getInsets().left + getInsets().right;
224: return maxWidth;
225: }
226:
227: /**
228: * get the minimum width
229: *
230: * @return minimum width
231: */
232: public int getMinimumWidth() {
233: // Default value.
234: int minWidth = 150;
235:
236: if (titleArea != null) {
237: minWidth = Math.max(minWidth, titleArea.getMinimumWidth());
238: }
239:
240: if (tableArea != null) {
241: minWidth = Math.max(minWidth, tableArea.getMinimumWidth());
242: }
243:
244: // Always adjust minimum width to account for horizontal insets.
245: minWidth += getInsets().left + getInsets().right;
246: return minWidth;
247: }
248:
249: /**
250: * get the maximum height
251: *
252: * @return maximum height
253: */
254: public int getMaximumHeight() {
255: int maxHeight = getInsets().top + getInsets().bottom;
256:
257: if (titleArea != null) {
258: maxHeight += titleArea.getMaximumHeight();
259: maxHeight += titleToolBarGap;
260: }
261: if (toolBarArea != null) {
262: maxHeight += toolBarArea.getMaximumHeight();
263: maxHeight += toolBarTableGap;
264: }
265: if (tableArea != null) {
266: maxHeight += tableArea.getMaximumHeight();
267: }
268:
269: return maxHeight;
270: }
271:
272: /**
273: * get the minimum height
274: *
275: * @return minimum height
276: */
277: public int getMinimumHeight() {
278: int minHeight = getInsets().top + getInsets().bottom;
279:
280: //take min height of title into account this much height
281: //we want to show always
282: if (titleArea != null) {
283: minHeight += titleArea.getMinimumHeight();
284: minHeight += titleToolBarGap;
285: }
286:
287: return minHeight;
288: }
289:
290: /**
291: * set the bounding rectangle. This also will constrain table to its maximum possible
292: * height.
293: *
294: * @param left left
295: * @param top top
296: * @param width width
297: * @param height height
298: */
299: public void setBoundingRect(int left, int top, int width, int height) {
300: //int maxWidth = this.getMaximumWidth();
301: int maxHeight = this .getMaximumHeight();
302: super .setBoundingRect(left, top, width, Math.min(height,
303: maxHeight));
304: }
305:
306: /**
307: * handle child geometry change. This handle change in TableArea. when a resize occurs
308: * table area sets its height to the height of all visible column. this avoids extra
309: * space in vertical scroll bar
310: *
311: * @param child child
312: * @param prevRect child prev rect
313: */
314: protected boolean geometryChangeChild(JGoObject child,
315: java.awt.Rectangle prevRect) {
316: if (child == tableArea && this .isExpandedState()) {
317: int height = tableArea.getVisibleRowTableHeights()
318: + toolBarTableGap;
319: if (titleArea != null) {
320: height += titleArea.getMaximumHeight();
321: }
322:
323: if (toolBarArea != null) {
324:
325: height += titleToolBarGap
326: + toolBarArea.getMaximumHeight();
327: }
328: if (this .getHeight() != height) {
329: setHeight(height);
330: } else {
331: layoutChildren();
332: }
333:
334: return true;
335: }
336:
337: return super .geometryChangeChild(child, prevRect);
338: }
339:
340: /**
341: * layout all the children of this table area
342: */
343: public void layoutChildren() {
344: if (drawBoundingRect) {
345: rect.setBoundingRect(this .getBoundingRect());
346: }
347:
348: Insets insets1 = getInsets();
349:
350: //get the bounding rectangle of this table area
351: int x = getLeft() + insets1.left;
352: int y = getTop() + insets1.top;
353: int width = getWidth() - insets1.left - insets1.right;
354: int height = getHeight() - insets1.top - insets1.bottom;
355:
356: titleArea.setBoundingRect(x, y, width, titleArea
357: .getMaximumHeight());
358:
359: if (toolBarArea != null) {
360: //if there is some height left allocate it to tool bar area
361: if (height - titleArea.getHeight() - titleToolBarGap > 0) {
362: toolBarArea.setVisible(true);
363: toolBarArea.setBoundingRect(x, y
364: + titleArea.getHeight() + titleToolBarGap,
365: width, toolBarArea.getMaximumHeight());
366:
367: } else {
368: toolBarArea.setVisible(false);
369: toolBarArea
370: .setBoundingRect(titleArea.getBoundingRect());
371: }
372:
373: if (tableArea != null) {
374: //if there is some height left allocate it to table area
375: if (height - toolBarArea.getHeight() - toolBarTableGap > 0) {
376: tableArea.setVisible(true);
377:
378: Rectangle tAreaRect = new Rectangle(x,
379: toolBarArea.getTop()
380: + toolBarArea.getHeight()
381: + toolBarTableGap, width, height
382: - titleArea.getHeight()
383: - titleToolBarGap
384: - toolBarArea.getHeight()
385: - toolBarTableGap);
386:
387: //if table area height did not change (this happens when table
388: // is first minimized and then maximized) then we still want
389: //to layout children one more time so that vertical scrollbar
390: //will not have extra space
391: if (tableArea.getBoundingRect() != tAreaRect) {
392: tableArea.setBoundingRect(tAreaRect);
393: } else {
394: tableArea.layoutChildren();
395: }
396:
397: return;
398: }
399: tableArea.setVisible(false);
400: tableArea.setBoundingRect(titleArea.getBoundingRect());
401: }
402:
403: } else { //end toolBar != null
404: if (tableArea != null) {
405: //if there is some height left allocate it to table area
406: if (height - titleArea.getHeight() - titleToolBarGap > 0) {
407: tableArea.setVisible(true);
408: Rectangle tAreaRect = new Rectangle(x, y
409: + titleArea.getHeight() + titleToolBarGap,
410: width, height - titleArea.getHeight()
411: - titleToolBarGap);
412:
413: //if table area height did not change (this happens when table
414: // is first minimized and then maximized) then we still want
415: //to layout children one more time so that vertical scrollbar
416: //will not have extra space
417:
418: if (tableArea.getBoundingRect() != tAreaRect) {
419: tableArea.setBoundingRect(tAreaRect);
420: } else {
421: tableArea.layoutChildren();
422: }
423: return;
424: }
425: tableArea.setVisible(false);
426: tableArea.setBoundingRect(titleArea.getBoundingRect());
427: }
428: }
429: }
430:
431: /**
432: * get the title area bounds
433: *
434: * @return title area bounds
435: */
436: public Rectangle getTitleAreaBounds() {
437: return titleArea.getBoundingRect();
438: }
439:
440: /**
441: * Sets brush for table title to the given instance.
442: *
443: * @param newBrush JGoBrush to use for title area
444: */
445: public void setTitleBrush(JGoBrush newBrush) {
446: if (this .titleArea != null) {
447: this .titleArea.setBrush(newBrush);
448: }
449: }
450:
451: /**
452: * update the vertical scrollbar
453: */
454: public void updateVerticalScrollBar() {
455: }
456:
457: public void setShowHeader(boolean show) {
458: this .tableArea.setShowHeader(show);
459: }
460:
461: /**
462: * Sets background color for this instance.
463: *
464: * @param c new background color
465: */
466: public void setBackgroundColor(Color c) {
467: if (this .tableArea != null) {
468: this .tableArea.setBackgroundColor(c);
469: }
470: }
471:
472: /**
473: * Sets background paint for this instance.
474: *
475: * @param p new Paint instance
476: */
477: public void setBackgroundPaint(Paint p) {
478: this.titleArea.setBackgroundPaint(p);
479: }
480: }
|