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