001: /*
002: * Copyright Javelin Software, All rights reserved.
003: */
004:
005: package com.javelin.swinglets;
006:
007: /**
008: * A STabbedPane is a tab panel.
009: *
010: * @author Robin Sharp
011: */
012:
013: import java.awt.*;
014: import java.io.*;
015: import java.util.*;
016:
017: import com.javelin.swinglets.event.*;
018: import com.javelin.swinglets.tabbed.*;
019: import com.javelin.swinglets.plaf.*;
020:
021: public class STabbedPane extends SContainer {
022: /**
023: * Construct a STabbedPane with TOP tabPlacement.
024: */
025: public STabbedPane() {
026: this (SConstants.TOP);
027: }
028:
029: /**
030: * Construct an empty TabbedPane with the specified tab placement. <br>
031: * One of SConstants.TOP, SConstants.BOTTOM, SConstants.LEFT, or SConstants.RIGHT.
032: */
033: public STabbedPane(int tabPlacement) {
034: setTabPlacement(tabPlacement);
035: pages = new Vector(1);
036: }
037:
038: /**
039: * Returns the name of the L&F class that renders this component.
040: */
041: public Class getUIClass() {
042: return STabbedPane.class;
043: }
044:
045: /**
046: * Get the placement of the tabs.
047: */
048: public int getTabPlacement() {
049: return tabPlacement;
050: }
051:
052: /**
053: * Sets the placement of tabs. <br>
054: * One of SConstants.TOP, SConstants.BOTTOM, SConstants.LEFT, or SConstants.RIGHT.
055: */
056: public STabbedPane setTabPlacement(int tabPlacement) {
057: this .tabPlacement = tabPlacement;
058: return this ;
059: }
060:
061: /**
062: * Get the selected index.
063: */
064: public int getSelectedIndex() {
065: return selectedIndex;
066: }
067:
068: /**
069: * Set the selected index.
070: */
071: public STabbedPane setSelectedIndex(int selectedIndex) {
072: firePropertyChange("selectedIndex", this .selectedIndex,
073: this .selectedIndex = selectedIndex);
074: return this ;
075: }
076:
077: /**
078: * Get the currently selected component.
079: */
080: public SComponent getSelectedComponent() {
081: int index = getSelectedIndex();
082: if (index == -1)
083: return null;
084: return getComponentAt(index);
085: }
086:
087: /**
088: * Sets the selected component for this tabbedpane.
089: */
090: public STabbedPane setSelectedComponent(SComponent component) {
091: int index = indexOfComponent(component);
092: if (index != -1) {
093: setSelectedIndex(index);
094: }
095: return this ;
096: }
097:
098: /**
099: * Inserts a tab at index.
100: */
101: public void insertTab(String title, SIcon icon,
102: SComponent component, String tip, int index) {
103: super .add(component); //add to the container
104:
105: SPage page = new SPage(title != null ? title : "", icon,
106: component, tip);
107:
108: if (selectedIndex < 0) {
109: selectedIndex = 0;
110: }
111:
112: if (index >= pages.size()) {
113: pages.addElement(page);
114: } else {
115: pages.insertElementAt(page, index);
116: }
117: }
118:
119: /**
120: * Adds a tab.
121: */
122: public void addTab(String title, SIcon icon, SComponent component,
123: String tip) {
124: insertTab(title, icon, component, tip, pages.size());
125: }
126:
127: /**
128: * Adds tab.
129: */
130: public void addTab(String title, SIcon icon, SComponent component) {
131: insertTab(title, icon, component, null, pages.size());
132: }
133:
134: /**
135: * Adds a tab.
136: */
137: public void addTab(String title, SComponent component) {
138: insertTab(title, null, component, null, pages.size());
139: }
140:
141: /**
142: * Adds a tab.
143: */
144: public SContainer add(SComponent component) {
145: addTab(component.getName(), component);
146: return this ;
147: }
148:
149: /**
150: * Adds a tab.
151: */
152: public SContainer add(String title, SComponent component) {
153: addTab(title, component);
154: return this ;
155: }
156:
157: /**
158: * Adds a tab.
159: */
160: public SContainer add(SComponent component, int index) {
161: insertTab(component.getName(), null, component, null, index);
162: return this ;
163: }
164:
165: /**
166: * Removes the tab.
167: */
168: public void removeTabAt(int index) {
169: if (getSelectedIndex() > index) {
170: setSelectedIndex(getSelectedIndex() - 1);
171: }
172:
173: super .remove(((SPage) pages.elementAt(index)).getComponent());
174:
175: pages.removeElementAt(index);
176: }
177:
178: /**
179: * Remove the tab with the specified component.
180: */
181: public void remove(SComponent component) {
182: int index = indexOfComponent(component);
183: if (index >= 0) {
184: removeTabAt(index);
185: }
186: }
187:
188: /**
189: * Remove all the tabs.
190: */
191: public void removeAll() {
192: setSelectedIndex(-1);
193: pages.removeAllElements();
194:
195: super .removeAll();
196: }
197:
198: /**
199: * Get the number of tabs.
200: */
201: public int getTabCount() {
202: return pages.size();
203: }
204:
205: /**
206: * Get the tab title at index.
207: */
208: public String getTitleAt(int index) {
209: return ((SPage) pages.elementAt(index)).getTitle();
210: }
211:
212: /**
213: * Set the title at index
214: */
215: public STabbedPane setTitleAt(int index, String title) {
216: ((SPage) pages.elementAt(index)).setTitle(title);
217: return this ;
218: }
219:
220: /**
221: * Get the tab icon at index.
222: */
223: public SIcon getIconAt(int index) {
224: return ((SPage) pages.elementAt(index)).getIcon();
225: }
226:
227: /**
228: * Set the icon at index.
229: */
230: public STabbedPane setIconAt(int index, SIcon icon) {
231: ((SPage) pages.elementAt(index)).setIcon(icon);
232: return this ;
233: }
234:
235: /**
236: * Get the tab background color at index.
237: */
238: public SColor getBackgroundAt(int index) {
239: return ((SPage) pages.elementAt(index)).getBackground();
240: }
241:
242: /**
243: * Set the background color at index.
244: */
245: public STabbedPane setBackgroundAt(int index, SColor background) {
246: ((SPage) pages.elementAt(index)).setBackground(background);
247: return this ;
248: }
249:
250: /**
251: * Returns the tab foreground color at index.
252: */
253: public SColor getForegroundAt(int index) {
254: return ((SPage) pages.elementAt(index)).getForeground();
255: }
256:
257: /**
258: * Set the foreground color at index.
259: */
260: public STabbedPane setForegroundAt(int index, SColor foreground) {
261: ((SPage) pages.elementAt(index)).setForeground(foreground);
262: return this ;
263: }
264:
265: /**
266: * Check whether or not the tab at index is enabled.
267: */
268: public boolean isEnabledAt(int index) {
269: return ((SPage) pages.elementAt(index)).isEnabled();
270: }
271:
272: /**
273: * Set whether tab at index is enabled.
274: */
275: public STabbedPane setEnabledAt(int index, boolean enabled) {
276: ((SPage) pages.elementAt(index)).setEnabled(enabled);
277: return this ;
278: }
279:
280: /**
281: * Get the component at index.
282: */
283: public SComponent getComponentAt(int index) {
284: return ((SPage) pages.elementAt(index)).getComponent();
285: }
286:
287: /**
288: * Set the component at index.
289: */
290: public STabbedPane setComponentAt(int index, SComponent component) {
291: ((SPage) pages.elementAt(index)).setComponent(component);
292: return this ;
293: }
294:
295: /**
296: * Returns the first tab with the title.
297: */
298: public int indexOfTab(String title) {
299: for (int index = 0; index < getTabCount(); index++) {
300: if (getTitleAt(index).equals(title == null ? "" : title)) {
301: return index;
302: }
303: }
304: return -1;
305: }
306:
307: /**
308: * Returns the first tab with the icon.
309: */
310: public int indexOfTab(SIcon icon) {
311: for (int index = 0; index < getTabCount(); index++) {
312: if (getIconAt(index).equals(icon)) {
313: return index;
314: }
315: }
316: return -1;
317: }
318:
319: /**
320: * Returns the first tab for the specified component.
321: */
322: public int indexOfComponent(SComponent component) {
323: for (int index = 0; index < getTabCount(); index++) {
324: if (getComponentAt(index).equals(component)) {
325: return index;
326: }
327: }
328: return -1;
329: }
330:
331: /**
332: * Returns true if the Menubar's border should be painted.
333: */
334: public boolean isBorderPainted() {
335: return borderPainted;
336: }
337:
338: /**
339: * Sets whether the border should be painted.
340: */
341: public STabbedPane setBorderPainted(boolean borderPainted) {
342: firePropertyChange("borderPainted", this .borderPainted,
343: this .borderPainted = borderPainted);
344: return this ;
345: }
346:
347: /**
348: * Set Tabbed cell renderer for the current look and feel.
349: */
350: public STabbedPane setTabbedCellRenderer(
351: STabbedCellRenderer tabbedCellRenderer) {
352: tabbedCellRenderers.put(getLookAndFeel().getClass().getName(),
353: tabbedCellRenderer);
354:
355: return this ;
356: }
357:
358: /**
359: * Set Tabbed cell renderer for each look and feel.
360: */
361: public STabbedPane setTabbedCellRenderer(String lookAndFeel,
362: STabbedCellRenderer tabbedCellRenderer) {
363: tabbedCellRenderers.put(lookAndFeel, tabbedCellRenderer);
364:
365: return this ;
366: }
367:
368: /**
369: * Get Tabbed cell renderer, for the current look and feel.
370: */
371: public STabbedCellRenderer getTabbedCellRenderer() {
372: return getTabbedCellRenderer(getLookAndFeel().getClass()
373: .getName());
374: }
375:
376: /**
377: * Get Tabbed cell renderer, or null.
378: * <P>
379: * If a renderer has not been installed for the look and feel, it is.
380: * picked up from the LookAndFeel as SLookAndFeel.TABLE_DEFAULT_CELL_RENDERER.
381: */
382: public STabbedCellRenderer getTabbedCellRenderer(String lookAndFeel) {
383: STabbedCellRenderer renderer = (STabbedCellRenderer) tabbedCellRenderers
384: .get(lookAndFeel);
385:
386: if (renderer == null) {
387: Class clazz = getLookAndFeel().getUIDefaults()
388: .getTabbedCellRenderer(
389: SLookAndFeel.TABBED_DEFAULT_CELL_RENDERER);
390:
391: //System.out.println( lookAndFeel + " " + clazz.getName() );
392:
393: if (clazz != null) {
394: try {
395: renderer = (STabbedCellRenderer) clazz
396: .newInstance();
397: } catch (Exception e) {
398: throw new IllegalArgumentException(
399: "Cannot load cell renderer for class "
400: + clazz.getName());
401: }
402: }
403:
404: if (renderer != null) {
405: tabbedCellRenderers.put(lookAndFeel, renderer);
406: }
407: }
408:
409: return renderer;
410: }
411:
412: // EVENT ///////////////////////////////////////////////////////////////////////////////
413:
414: /**
415: * Processes events occurring on this component.
416: */
417: protected void processEvent(AWTEvent event) {
418: if (event instanceof FormEvent) {
419: processFormEvent((FormEvent) event);
420: }
421: }
422:
423: /**
424: * Process the FormEvent to compute which tab has been
425: * selected.
426: */
427: protected void processFormEvent(FormEvent event) {
428: //Get the name of the node that was pressed
429: for (Enumeration names = event.getParameterNames(); names
430: .hasMoreElements();) {
431: String name = (String) names.nextElement();
432: if (Character.isDigit(name.charAt(0))) {
433: int index = name.indexOf('.');
434: if (index >= 0) {
435: setSelectedIndex(Integer.parseInt(name.substring(0,
436: index)));
437: } else {
438: setSelectedIndex(Integer.parseInt(name));
439: }
440: }
441: }
442: }
443:
444: // PRIVATE //////////////////////////////////////////////////////////
445:
446: protected boolean borderPainted;
447: protected Vector pages = new Vector();
448: protected int tabPlacement;
449: protected int selectedIndex = -1;
450:
451: protected Hashtable tabbedCellRenderers = new Hashtable();
452:
453: public class SPage {
454: public SPage(String title, SIcon icon, SComponent component,
455: String tip) {
456: this .title = title;
457: this .icon = icon;
458: this .component = component;
459: this .tip = tip;
460: }
461:
462: public String getTitle() {
463: return title;
464: }
465:
466: public void setTitle(String title) {
467: this .title = title;
468: }
469:
470: public SIcon getIcon() {
471: return icon;
472: }
473:
474: public void setIcon(SIcon icon) {
475: this .icon = icon;
476: }
477:
478: public SComponent getComponent() {
479: return component;
480: }
481:
482: public void setComponent(SComponent component) {
483: this .component = component;
484: }
485:
486: public String getTip() {
487: return tip;
488: }
489:
490: public void setTip(String tip) {
491: this .tip = tip;
492: }
493:
494: public SColor getForeground() {
495: return foreground;
496: }
497:
498: public void setForeground(SColor foreground) {
499: this .foreground = foreground;
500: }
501:
502: public SColor getBackground() {
503: return background;
504: }
505:
506: public void setBackground(SColor background) {
507: this .background = background;
508: }
509:
510: public boolean isEnabled() {
511: return enabled;
512: }
513:
514: public void setEnabled(boolean enabled) {
515: this .enabled = enabled;
516: }
517:
518: // PRIVATE ////////////////////////////////////////////
519:
520: protected String title;
521: protected SIcon icon;
522: protected SComponent component;
523: protected String tip;
524:
525: protected SColor foreground;
526: protected SColor background;
527:
528: protected boolean enabled = true;
529:
530: }
531: }
|