1:
42:
43: package ;
44:
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63:
64: import ;
65:
66:
71: public abstract class AbstractTabbedUI extends JComponent {
72:
73:
74: public static final String JMENUBAR_PROPERTY = "jMenuBar";
75:
76:
77: public static final String GLOBAL_MENU_PROPERTY = "globalMenu";
78:
79:
82: protected class ExitAction extends AbstractAction {
83:
84:
88: public ExitAction() {
89: putValue(NAME, "Exit");
90: }
91:
92:
97: public void actionPerformed(final ActionEvent e) {
98: attempExit();
99: }
100:
101: }
102:
103:
106: private class TabChangeHandler implements ChangeListener {
107:
108:
109: private final JTabbedPane pane;
110:
111:
116: public TabChangeHandler(final JTabbedPane pane) {
117: this.pane = pane;
118: }
119:
120:
125: public void stateChanged(final ChangeEvent e) {
126: setSelectedEditor(this.pane.getSelectedIndex());
127: }
128: }
129:
130:
133: private class TabEnableChangeListener implements PropertyChangeListener {
134:
135:
138: public TabEnableChangeListener() {
139: }
140:
141:
147: public void propertyChange(final PropertyChangeEvent evt) {
148: if (evt.getPropertyName().equals("enabled") == false) {
149: Log.debug ("PropertyName");
150: return;
151: }
152: if (evt.getSource() instanceof RootEditor == false) {
153: Log.debug ("Source");
154: return;
155: }
156: final RootEditor editor = (RootEditor) evt.getSource();
157: updateRootEditorEnabled(editor);
158: }
159: }
160:
161:
162: private ArrayList rootEditors;
163:
164: private JTabbedPane tabbedPane;
165:
166: private int selectedRootEditor;
167:
168: private JComponent currentToolbar;
169:
170: private JPanel toolbarContainer;
171:
172: private Action closeAction;
173:
174: private JMenuBar jMenuBar;
175:
176: private boolean globalMenu;
177:
178:
181: public AbstractTabbedUI() {
182: this.selectedRootEditor = -1;
183:
184: this.toolbarContainer = new JPanel();
185: this.toolbarContainer.setLayout(new BorderLayout());
186:
187: this.tabbedPane = new JTabbedPane(SwingConstants.BOTTOM);
188: this.tabbedPane.addChangeListener(new TabChangeHandler(this.tabbedPane));
189:
190: this.rootEditors = new ArrayList();
191:
192: setLayout(new BorderLayout());
193: add(this.toolbarContainer, BorderLayout.NORTH);
194: add(this.tabbedPane, BorderLayout.CENTER);
195:
196: this.closeAction = createCloseAction();
197: }
198:
199:
204: protected JTabbedPane getTabbedPane() {
205: return this.tabbedPane;
206: }
207:
208:
218: public boolean isGlobalMenu() {
219: return this.globalMenu;
220: }
221:
222:
227: public void setGlobalMenu(final boolean globalMenu) {
228: this.globalMenu = globalMenu;
229: if (isGlobalMenu()) {
230: setJMenuBar(updateGlobalMenubar());
231: }
232: else {
233: if (getRootEditorCount () > 0) {
234: setJMenuBar(createEditorMenubar(getRootEditor(getSelectedEditor())));
235: }
236: }
237: }
238:
239:
244: public JMenuBar getJMenuBar() {
245: return this.jMenuBar;
246: }
247:
248:
253: protected void setJMenuBar(final JMenuBar menuBar) {
254: final JMenuBar oldMenuBar = this.jMenuBar;
255: this.jMenuBar = menuBar;
256: firePropertyChange(JMENUBAR_PROPERTY, oldMenuBar, menuBar);
257: }
258:
259:
264: protected Action createCloseAction() {
265: return new ExitAction();
266: }
267:
268:
273: public Action getCloseAction() {
274: return this.closeAction;
275: }
276:
277:
282: protected abstract JMenu[] getPrefixMenus();
283:
284:
289: protected abstract JMenu[] getPostfixMenus();
290:
291:
297: private void addMenus(final JMenuBar menuBar, final JMenu[] customMenus) {
298: for (int i = 0; i < customMenus.length; i++) {
299: menuBar.add(customMenus[i]);
300: }
301: }
302:
303:
307: private JMenuBar updateGlobalMenubar () {
308: JMenuBar menuBar = getJMenuBar();
309: if (menuBar == null) {
310: menuBar = new JMenuBar();
311: }
312: else {
313: menuBar.removeAll();
314: }
315:
316: addMenus(menuBar, getPrefixMenus());
317: for (int i = 0; i < this.rootEditors.size(); i++)
318: {
319: final RootEditor editor = (RootEditor) this.rootEditors.get(i);
320: addMenus(menuBar, editor.getMenus());
321: }
322: addMenus(menuBar, getPostfixMenus());
323: return menuBar;
324: }
325:
326:
332: private JMenuBar createEditorMenubar(final RootEditor root) {
333:
334: JMenuBar menuBar = getJMenuBar();
335: if (menuBar == null) {
336: menuBar = new JMenuBar();
337: }
338: else {
339: menuBar.removeAll();
340: }
341:
342: addMenus(menuBar, getPrefixMenus());
343: if (isGlobalMenu())
344: {
345: for (int i = 0; i < this.rootEditors.size(); i++)
346: {
347: final RootEditor editor = (RootEditor) this.rootEditors.get(i);
348: addMenus(menuBar, editor.getMenus());
349: }
350: }
351: else
352: {
353: addMenus(menuBar, root.getMenus());
354: }
355: addMenus(menuBar, getPostfixMenus());
356: return menuBar;
357: }
358:
359:
364: public void addRootEditor(final RootEditor rootPanel) {
365: this.rootEditors.add(rootPanel);
366: this.tabbedPane.add(rootPanel.getEditorName(), rootPanel.getMainPanel());
367: rootPanel.addPropertyChangeListener("enabled", new TabEnableChangeListener());
368: updateRootEditorEnabled(rootPanel);
369: if (getRootEditorCount () == 1) {
370: setSelectedEditor(0);
371: }
372: else if (isGlobalMenu()) {
373: setJMenuBar(updateGlobalMenubar());
374: }
375: }
376:
377:
382: public int getRootEditorCount () {
383: return this.rootEditors.size();
384: }
385:
386:
393: public RootEditor getRootEditor(final int pos) {
394: return (RootEditor) this.rootEditors.get(pos);
395: }
396:
397:
402: public int getSelectedEditor() {
403: return this.selectedRootEditor;
404: }
405:
406:
411: public void setSelectedEditor(final int selectedEditor) {
412: final int oldEditor = this.selectedRootEditor;
413: if (oldEditor == selectedEditor) {
414:
415: return;
416: }
417: this.selectedRootEditor = selectedEditor;
418:
419:
420:
421:
422: for (int i = 0; i < this.rootEditors.size(); i++) {
423: final boolean shouldBeActive = (i == selectedEditor);
424: final RootEditor container =
425: (RootEditor) this.rootEditors.get(i);
426: if (container.isActive() && (shouldBeActive == false)) {
427: container.setActive(false);
428: }
429: }
430:
431: if (this.currentToolbar != null) {
432: closeToolbar();
433: this.toolbarContainer.removeAll();
434: this.currentToolbar = null;
435: }
436:
437: for (int i = 0; i < this.rootEditors.size(); i++) {
438: final boolean shouldBeActive = (i == selectedEditor);
439: final RootEditor container =
440: (RootEditor) this.rootEditors.get(i);
441: if ((container.isActive() == false) && (shouldBeActive == true)) {
442: container.setActive(true);
443: setJMenuBar(createEditorMenubar(container));
444: this.currentToolbar = container.getToolbar();
445: if (this.currentToolbar != null) {
446: this.toolbarContainer.add
447: (this.currentToolbar, BorderLayout.CENTER);
448: this.toolbarContainer.setVisible(true);
449: this.currentToolbar.setVisible(true);
450: }
451: else {
452: this.toolbarContainer.setVisible(false);
453: }
454:
455: this.getJMenuBar().repaint();
456: }
457: }
458: }
459:
460:
463: private void closeToolbar() {
464: if (this.currentToolbar != null) {
465: if (this.currentToolbar.getParent() != this.toolbarContainer) {
466:
467:
468: final Window w = SwingUtilities.windowForComponent(this.currentToolbar);
469: if (w != null) {
470: w.setVisible(false);
471: w.dispose();
472: }
473: }
474: this.currentToolbar.setVisible(false);
475: }
476: }
477:
478:
481: protected abstract void attempExit();
482:
483:
488: protected void updateRootEditorEnabled(final RootEditor editor) {
489:
490: final boolean enabled = editor.isEnabled();
491: for (int i = 0; i < this.tabbedPane.getTabCount(); i++) {
492: final Component tab = this.tabbedPane.getComponentAt(i);
493: if (tab == editor.getMainPanel()) {
494: this.tabbedPane.setEnabledAt(i, enabled);
495: return;
496: }
497: }
498: }
499: }