001: /*
002: * Stingray Software Objective Blend
003: * Copyright (C) 1996 Stingray Software, Inc.
004: * All Rights Reserved
005: *
006: * This source code is only intended as a supplement to
007: * the Stingray Objective Blend product. See the Objective
008: * Blend html help documentation for detailed information regarding
009: * using OB classes.
010: *
011: * Author : LingFeng Wang
012: * Description : OBBase.java
013: *
014: * CHANGELOG:
015: * 3/12/97 JDK1.1
016: */
017:
018: package ob.obbase;
019:
020: import java.awt.*;
021: import com.sun.portal.log.common.PortalLogger;
022: import java.awt.event.*;
023: import java.util.Vector;
024:
025: /**
026: * OBBase is a container class which provides common functionality for many
027: * of the Objective Blend controls. OBBase is designed to provide scrollbar
028: * functionality to navigate a control which needs to be displayed in a space
029: * which is too small to view it entirely. Thus, you can consider that OBBase
030: * presents a virtual view of a Panel component which subclasses OBBase.
031: */
032: public class OBBase extends Panel implements AdjustmentListener {
033:
034: protected Scrollbar vs;
035: protected Scrollbar hs;
036: protected int m_nVScrollbarWidth = 15;
037: protected int m_nHScrollbarHeight = 15;
038:
039: transient protected Image offscreen = null;
040:
041: protected int m_nTopRow = 0;
042: protected int m_nLeftCol = 0;
043:
044: protected boolean m_bAlwaysShowScrollbar = false;
045: protected boolean m_bHideHScrollbar = false;
046: protected boolean m_bHideVScrollbar = false;
047: protected boolean m_bDoubleBuffering = true;
048: transient protected String OSName;
049:
050: // this dialog is for evaluation purpose only. please comment it out.
051: // final static ob.obbase.EvalDlg evalDlg = new ob.obbase.EvalDlg();
052:
053: /**
054: * Construct a new OBBase object.
055: */
056: public OBBase() {
057: this (false);
058: setLayout(null);
059: }
060:
061: /**
062: * Construct a new OBBase object.
063: * @param bAlwaysShowScrollbar true if scrollbars should always be displayed, otherwise false
064: */
065: public OBBase(boolean bAlwaysShowScrollbar) {
066:
067: m_bAlwaysShowScrollbar = bAlwaysShowScrollbar;
068: vs = new Scrollbar(Scrollbar.VERTICAL);
069: hs = new Scrollbar(Scrollbar.HORIZONTAL);
070: vs.addAdjustmentListener(this );
071: hs.addAdjustmentListener(this );
072:
073: setLayout(null);
074: add(vs);
075: add(hs);
076: enableEvents(AWTEvent.FOCUS_EVENT_MASK);
077: enableEvents(AWTEvent.MOUSE_EVENT_MASK);
078: enableEvents(AWTEvent.KEY_EVENT_MASK);
079: enableEvents(AWTEvent.ADJUSTMENT_EVENT_MASK);
080: enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK);
081:
082: OSName = System.getProperty("os.name");
083: }
084:
085: /**
086: * getVScrollbar returns the vertical scrollbar.
087: * @return vertical scrollbar as type Scrollbar
088: */
089: public Scrollbar getVScrollbar() {
090: return vs;
091: }
092:
093: /**
094: * getHScrollbar returns the horizontal scrollbar.
095: * @return horizontal scrollbar as type Scrollbar
096: */
097: public Scrollbar getHScrollbar() {
098: return hs;
099: }
100:
101: /**
102: * getVScrollbarWidth returns the width of the vertical scrollbar.
103: * @return width of vertical scrollbar in pixels as type int.
104: */
105: public int getVScrollbarWidth() {
106: return m_nVScrollbarWidth;
107: }
108:
109: /**
110: * getHScrollbarHeight returns the height of the horizontal scrollbar.
111: * @return height of the horizontal scrollbar in pixels as type int
112: */
113: public int getHScrollbarHeight() {
114: return m_nHScrollbarHeight;
115: }
116:
117: /**
118: * setVScrollbarWidth changes the width of the vertical scrollbar to the specified value.
119: * @param width new width in pixels
120: */
121: public void setVScrollbarWidth(int width) {
122: if (width > 0) {
123: m_nVScrollbarWidth = width;
124: /* DEPRECATED
125: layout();
126: */
127: doLayout();
128: }
129: }
130:
131: /**
132: * setHScrollbarHeight changes the height of the horizontal scrollbar to the specified value.
133: * @param height new height in pixels
134: */
135: public void setHScrollbarHeight(int height) {
136: if (height > 0) {
137: m_nHScrollbarHeight = height;
138: /* DEPRECATED
139: layout();
140: */
141: doLayout();
142: }
143: }
144:
145: public void adjustmentValueChanged(AdjustmentEvent e) {
146:
147: if (e.getID() == AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED) {
148:
149: if (e.getSource() instanceof Scrollbar
150: && ((Scrollbar) e.getSource()).getOrientation() == Scrollbar.VERTICAL) {
151: int nValue = getVScrollbar().getValue();
152: //if (nValue!=m_nTopRow) {
153: if (onTopRowChange(nValue)) {
154: m_nTopRow = nValue;
155: onTopRowChanged(nValue);
156: }
157: //}
158: } else {
159: int nValue = getHScrollbar().getValue();
160: //if (nValue!=m_nLeftCol) {
161: if (onLeftColChange(nValue)) {
162: m_nLeftCol = nValue;
163: onLeftColChanged(nValue);
164: }
165: //}
166: }
167: }
168: }
169:
170: /* DEPRECATED
171: public void reshape(int x, int y, int w, int h) {
172: if (!(w>0 && h>0)) return;
173:
174: super.reshape(x, y, w, h);
175: updateScrollbar();
176: }
177: */
178:
179: /**
180: * setBounds reshapes the list box to the specified bounding box.
181: * @param x the x coordinate.
182: * @param y the y coordinate.
183: * @param w the width of the list box.
184: * @param h the height of the list box.
185: */
186: public void setBounds(int x, int y, int w, int h) {
187: if (!(w > 0 && h > 0))
188: return;
189:
190: /* DEPRECATED
191: super.reshape(x, y, w, h);
192: */
193: super .setBounds(x, y, w, h);
194: updateScrollbar();
195: }
196:
197: /* DEPRECATED
198: public synchronized void layout() {
199: Dimension d = size();
200: int w = d.width;
201: int h = d.height;
202:
203: Scrollbar vs = getVScrollbar();
204: Scrollbar hs = getHScrollbar();
205:
206: if (m_bAlwaysShowScrollbar||(getMaxTopRow()!=0 && !m_bHideVScrollbar))
207: hs.setBounds(getInsets().left, h-m_nHScrollbarHeight-getInsets().bottom, w-m_nVScrollbarWidth-getInsets().left-getInsets().right, m_nHScrollbarHeight);
208: else
209: hs.setBounds(getInsets().left, h-m_nHScrollbarHeight-getInsets().bottom, w-getInsets().left-getInsets().right, m_nHScrollbarHeight);
210:
211: if (m_bAlwaysShowScrollbar||(getMaxLeftCol()!=0 && !m_bHideHScrollbar))
212: vs.setBounds(w-m_nVScrollbarWidth-getInsets().right, getInsets().top, m_nVScrollbarWidth, h-m_nHScrollbarHeight-getInsets().top-getInsets().bottom);
213: else
214: vs.setBounds(w-m_nVScrollbarWidth-getInsets().right, getInsets().top, m_nVScrollbarWidth, h-getInsets().top-getInsets().bottom);
215:
216: if (m_bAlwaysShowScrollbar||(getMaxTopRow()!=0 && !m_bHideVScrollbar)) {
217: vs.show();
218: } else {
219: vs.hide();
220: }
221:
222: if (m_bAlwaysShowScrollbar||(getMaxLeftCol()!=0 && !m_bHideHScrollbar)) {
223: hs.show();
224: } else {
225: hs.hide();
226: }
227: }
228: */
229:
230: public synchronized void doLayout() {
231: /* DEPRECATED
232: Dimension d = size();
233: */
234: Dimension d = getSize();
235: int w = d.width;
236: int h = d.height;
237:
238: Scrollbar vs = getVScrollbar();
239: Scrollbar hs = getHScrollbar();
240:
241: if (m_bAlwaysShowScrollbar
242: || (getMaxTopRow() != 0 && !m_bHideVScrollbar))
243: hs.setBounds(getInsets().left, h - m_nHScrollbarHeight
244: - getInsets().bottom, w - m_nVScrollbarWidth
245: - getInsets().left - getInsets().right,
246: m_nHScrollbarHeight);
247: else
248: hs.setBounds(getInsets().left, h - m_nHScrollbarHeight
249: - getInsets().bottom, w - getInsets().left
250: - getInsets().right, m_nHScrollbarHeight);
251:
252: if (m_bAlwaysShowScrollbar
253: || (getMaxLeftCol() != 0 && !m_bHideHScrollbar))
254: vs.setBounds(w - m_nVScrollbarWidth - getInsets().right,
255: getInsets().top, m_nVScrollbarWidth, h
256: - m_nHScrollbarHeight - getInsets().top
257: - getInsets().bottom);
258: else
259: vs.setBounds(w - m_nVScrollbarWidth - getInsets().right,
260: getInsets().top, m_nVScrollbarWidth, h
261: - getInsets().top - getInsets().bottom);
262:
263: if (m_bAlwaysShowScrollbar
264: || (getMaxTopRow() != 0 && !m_bHideVScrollbar)) {
265: /* DEPRECATED
266: vs.show();
267: */
268: vs.setVisible(true);
269: } else {
270: /* DEPRECATED
271: vs.hide();
272: */
273: vs.setVisible(false);
274: }
275:
276: if (m_bAlwaysShowScrollbar
277: || (getMaxLeftCol() != 0 && !m_bHideHScrollbar)) {
278: /* DEPRECATED
279: hs.show();
280: */
281: hs.setVisible(true);
282: } else {
283: /* DEPRECATED
284: hs.hide();
285: */
286: hs.setVisible(false);
287: }
288: }
289:
290: /**
291: * update repaints the OBBase.
292: */
293:
294: public void paint(Graphics g) {
295: if (!isShowing())
296: return;
297: if (m_bDoubleBuffering && validateImage()) {
298: /* DEPRECATED
299: Dimension d = this.size();
300: */
301: Dimension d = this .getSize();
302: /* DEPRECATED
303: Rectangle r = g.getClipRect();
304: */
305: Rectangle r = g.getClipBounds();
306:
307: Graphics og = offscreen.getGraphics();
308:
309: if (r != null)
310: og.clipRect(r.x, r.y, r.width, r.height);
311: og.setFont(g.getFont());
312: og.setColor(getBackground());
313: og.fillRect(0, 0, d.width, d.height);
314: og.setColor(g.getColor());
315: draw(og);
316:
317: g.drawImage(offscreen, 0, 0, this );
318: og.dispose();
319: } else {
320: /* DEPRECATED
321: if (g!=null && g.getClipRect()!=null) draw(g);
322: */
323: if (g != null && g.getClipBounds() != null)
324: draw(g);
325: }
326: }
327:
328: /**
329: * draw paints the control. the derived class override this method to
330: * do painting. the OBBase will take care of double-buffering.
331: * @param g Graphics object
332: */
333: protected void draw(Graphics g) {
334: }
335:
336: /**
337: * validateImage is called every time when the paint method is called.
338: * this method checks if the off screen image is valid.
339: */
340: protected boolean validateImage() {
341:
342: try {
343: /* DEPRECATED
344: Dimension d = this.size();
345: */
346: Dimension d = this .getSize();
347:
348: if (offscreen == null
349: || offscreen.getWidth(this ) != d.width
350: || offscreen.getHeight(this ) != d.height) {
351: if (d.width > 0 && d.height > 0)
352: offscreen = this .createImage(d.width, d.height);
353: else
354: return false;
355: }
356:
357: return true;
358: } catch (Exception ex) {
359: return false;
360: }
361: }
362:
363: public void update(Graphics g, int x, int y, int w, int h) {
364: g.clipRect(x, y, w, h);
365: paint(g);
366: }
367:
368: public void update() {
369: /* DEPRECATED
370: Dimension d = this.size();
371: */
372: Dimension d = this .getSize();
373: int nHeight = d.height;
374: int nWidth = d.width;
375:
376: Graphics g = getGraphics();
377: if (g != null) {
378: update(g, 0, 0, nWidth, nHeight);
379: g.dispose();
380: }
381: }
382:
383: public void repaint() {
384: /* DEPRECATED
385: Dimension d = this.size();
386: */
387: Dimension d = this .getSize();
388: int nHeight = d.height;
389: int nWidth = d.width;
390:
391: Graphics g = getGraphics();
392: if (g != null) {
393: if (!m_bDoubleBuffering) {
394: g.setColor(getBackground());
395: g.fillRect(0, 0, d.width, d.height);
396: }
397: update(g, 0, 0, nWidth, nHeight);
398: g.dispose();
399: }
400: }
401:
402: public void update(Graphics g) {
403: if (m_bDoubleBuffering) {
404: paint(g);
405: } else {
406: super .update(g);
407: }
408: }
409:
410: /**
411: * onLeftColChange is called before the left column is changed.
412: * if this method returns false, the left column will not be changed.
413: * @Param nNewLeftCol the index of the new left column.
414: * @return true if left column should be changed, otherwise false.
415: */
416: protected boolean onLeftColChange(int nNewLeftCol) {
417: //nNewLeftCol;
418: return true;
419: }
420:
421: /**
422: * onTopRowChange is called before the top row is changed.
423: * if this method returns false, the top row will not be changed.
424: * @Param nNewTopRow the index of the new top row.
425: * @return true if top row should be changed, otherwise false
426: */
427: protected boolean onTopRowChange(int nNewTopRow) {
428: //nNewTopRow;
429: return true;
430: }
431:
432: /**
433: * onTopRowChanged is called after the top row has been changed.
434: * @param nNewTopRow the index of the new top row.
435: *
436: */
437: protected void onTopRowChanged(int nNewTopRow) {
438: repaint();
439: }
440:
441: /**
442: * onLeftColChanged is called after the left column has been changed.
443: * @param nNewLeftCol the index of the new left column.
444: */
445: protected void onLeftColChanged(int nNewLeftCol) {
446: repaint();
447: }
448:
449: /**
450: * getMaxTopRow is used to set the vertical scrollbar's maximum
451: * value.
452: */
453: protected int getMaxTopRow() {
454: return 0;
455: }
456:
457: /**
458: * getMaxLeftCol is used to set the horizontal scrollbar's maximum
459: * value.
460: */
461: protected int getMaxLeftCol() {
462: return 0;
463: }
464:
465: protected void updateScrollbar() {
466:
467: int nMaxTopRow = getMaxTopRow();
468: int nMaxLeftCol = getMaxLeftCol();
469:
470: Scrollbar vs = getVScrollbar();
471: Scrollbar hs = getHScrollbar();
472:
473: if (nMaxTopRow == 0) {
474: m_nTopRow = 0;
475: if (m_bAlwaysShowScrollbar) {
476: if (OSName.equals("Windows NT")
477: || OSName.equals("Windows 95"))
478: /* DEPRECATED
479: vs.disable();
480: */
481: vs.setEnabled(false);
482: }
483: } else {
484: if (!vs.isEnabled())
485: /* DEPRECATED
486: vs.enable();
487: */
488: vs.setEnabled(true);
489:
490: int nPage = nMaxTopRow / 10;
491: if (nPage < 5)
492: nPage = 5;
493:
494: vs.setValues(vs.getValue(), 1, 0, nMaxTopRow);
495: /* DEPRECATED
496: vs.setPageIncrement(nPage);
497: vs.setLineIncrement(1);
498: */
499: vs.setBlockIncrement(nPage);
500: vs.setUnitIncrement(1);
501: }
502:
503: if (nMaxLeftCol == 0) {
504: m_nLeftCol = 0;
505: if (m_bAlwaysShowScrollbar) {
506: if (OSName.equals("Windows NT")
507: || OSName.equals("Windows 95"))
508: /* DEPRECATED
509: vs.disable();
510: */
511: vs.setEnabled(false);
512: }
513: } else {
514: if (!hs.isEnabled())
515: /* DEPRECATED
516: hs.enable();
517: */
518: hs.setEnabled(true);
519:
520: int nPage = nMaxLeftCol / 10;
521: if (nPage < 5)
522: nPage = 5;
523:
524: hs.setValues(hs.getValue(), 1, 0, nMaxLeftCol);
525: /* DEPRECATED
526: hs.setPageIncrement(nPage);
527: hs.setLineIncrement(1);
528: */
529: hs.setBlockIncrement(nPage);
530: hs.setUnitIncrement(1);
531: }
532:
533: /* DEPRECATED
534: layout();
535: */
536: doLayout();
537: if (isShowing())
538: update();
539: }
540:
541: /**
542: * setAlwaysShowScrollbars sets the scrollbar displaying policy.
543: * @param bShowScrollbars if this is true, the scrollbars will be
544: * shown all the time. otherwise, the scrollbars will be shown if
545: * needed.
546: */
547: public void setAlwaysShowScrollbars(boolean bShowScrollbars) {
548: m_bAlwaysShowScrollbar = bShowScrollbars;
549: }
550:
551: /**
552: * getAlwaysShowScrollbars returns true if the corresponding flag has been
553: * seted to true.
554: */
555: public boolean getAlwaysShowScrollbars() {
556: return m_bAlwaysShowScrollbar;
557: }
558:
559: /**
560: * showHScrollbar shows the horizontal scrollbar.
561: */
562: public void showHScrollbar() {
563: m_bHideHScrollbar = false;
564: getHScrollbar().setVisible(true);
565: }
566:
567: /**
568: * showVScrollbar shows the vertical scrollbar.
569: */
570: public void showVScrollbar() {
571: m_bHideVScrollbar = false;
572: getVScrollbar().setVisible(true);
573: }
574:
575: /**
576: * hideHScrollbar hides the horizontal scrollbar.
577: */
578: public void hideHScrollbar() {
579: m_bHideHScrollbar = true;
580: getHScrollbar().setVisible(false);
581: }
582:
583: /**
584: * hideVScrollbar hides the vertical scrollbar.
585: */
586: public void hideVScrollbar() {
587: m_bHideVScrollbar = true;
588: getVScrollbar().setVisible(false);
589: }
590:
591: /**
592: * hideScrollbar hides the horizontal and vertical scrollbars.
593: */
594: public void hideScrollbar() {
595: m_bHideHScrollbar = true;
596: m_bHideVScrollbar = true;
597: hideVScrollbar();
598: hideHScrollbar();
599: }
600:
601: /**
602: * shows both scrollbars
603: */
604: public void showScrollbar() {
605: m_bHideHScrollbar = false;
606: m_bHideVScrollbar = false;
607: showVScrollbar();
608: showHScrollbar();
609: }
610:
611: /* DEPRECATED
612: public Dimension preferredSize() {
613: Dimension d = size();
614:
615: if (d.width>0&&d.height>0) return d;
616: else return minimumSize();
617: }
618: */
619:
620: /**
621: * getPreferredSize returns the preferred size.
622: * @return preferred size as type Dimension
623: */
624: public Dimension getPreferredSize() {
625: /* DEPRECATED
626: Dimension d = size();
627: */
628: Dimension d = getSize();
629:
630: if (d.width > 0 && d.height > 0)
631: return d;
632: /* DEPRECATED
633: else return minimumSize();
634: */
635: else
636: return getMinimumSize();
637: }
638:
639: /* DEPRECATED
640: public Dimension minimumSize() {
641: return new Dimension(100, 80);
642: }
643: */
644:
645: /**
646: * getMinimumSize returns the minimum size.
647: */
648: public Dimension getMinimumSize() {
649: return new Dimension(100, 80);
650: }
651:
652: /**
653: * sets double buffering mode on to reduce flicker
654: * @param bBuff true if double buffering enabled, otherwise false
655: */
656: public void setDoubleBuffering(boolean bBuff) {
657: m_bDoubleBuffering = bBuff;
658: }
659:
660: /**
661: * determines whether double buffering is used or not
662: * @return true if double buffering is used, otherwise false
663: */
664: public boolean isDoubleBuffering(boolean bBuff) {
665: return m_bDoubleBuffering;
666: }
667: }
|