001: /*
002: * Project: AMODA - Abstract Modeled Application
003: * Class: de.gulden.framework.amoda.environment.gui.GUIWorkspace
004: * Version: snapshot-beautyj-1.1
005: *
006: * Date: 2004-09-29
007: *
008: * This is a snapshot version of the AMODA 0.2 development branch,
009: * it is not released as a seperate version.
010: * For AMODA, see http://amoda.berlios.de/.
011: *
012: * This is licensed under the GNU Lesser General Public License (LGPL)
013: * and comes with NO WARRANTY.
014: *
015: * Author: Jens Gulden
016: * Email: amoda@jensgulden.de
017: */
018:
019: package de.gulden.framework.amoda.environment.gui;
020:
021: import de.gulden.framework.amoda.environment.commandline.*;
022: import de.gulden.framework.amoda.generic.core.GenericWorkspaceAbstract;
023: import de.gulden.framework.amoda.model.document.*;
024: import java.awt.*;
025: import java.awt.event.*;
026: import java.awt.event.ItemListener;
027: import java.util.*;
028: import java.util.List;
029: import javax.swing.*;
030: import javax.swing.event.*;
031: import javax.swing.event.InternalFrameListener;
032:
033: /**
034: * Class GUIWorkspace.
035: *
036: * @author Jens Gulden
037: * @version snapshot-beautyj-1.1
038: */
039: public class GUIWorkspace extends GenericWorkspaceAbstract implements
040: ItemListener, InternalFrameListener {
041:
042: // ------------------------------------------------------------------------
043: // --- fields ---
044: // ------------------------------------------------------------------------
045:
046: protected Hashtable viewsByFrames = new Hashtable();
047:
048: protected Hashtable framesByViews = new Hashtable();
049:
050: protected List recentActiveViews = new ArrayList();
051:
052: protected ButtonGroup menuButtonGroup = new ButtonGroup();
053:
054: protected Hashtable viewsByMenus = new Hashtable();
055:
056: protected Hashtable menusByViews = new Hashtable();
057:
058: protected Point newFrameLocation = new Point(30, 30);
059:
060: protected Hashtable titleNrsCollectionsByDocuments = new Hashtable();
061:
062: protected Hashtable titleNrsByFrames = new Hashtable();
063:
064: // ------------------------------------------------------------------------
065: // --- methods ---
066: // ------------------------------------------------------------------------
067:
068: public DocumentView getActiveView() {
069: JInternalFrame frame = getDesktopPane().getSelectedFrame();
070: if (frame != null) {
071: return (DocumentView) viewsByFrames.get(frame);
072: } else {
073: return null;
074: }
075: }
076:
077: public void setActiveView(DocumentView view) {
078: if (view != null) {
079: // may not actually set the view active, see PropertyVetoException below
080: JInternalFrame frame = (JInternalFrame) framesByViews
081: .get(view);
082: JRadioButtonMenuItem menu = (JRadioButtonMenuItem) menusByViews
083: .get(view);
084: try {
085: frame.setSelected(true);
086: recentActiveViews.remove(view);
087: recentActiveViews.add(0, view);
088: menu.setSelected(true);
089: } catch (java.beans.PropertyVetoException pve) {
090: //nop
091: }
092: }
093: }
094:
095: public void addView(DocumentView view) {
096: super .addView(view);
097: GUIApplicationEnvironment environment = ((GUIApplicationEnvironment) getEnvironment());
098: Collection titleNrs = (Collection) titleNrsCollectionsByDocuments
099: .get(view.getDocument());
100: if (titleNrs == null) { // first view of this document
101: titleNrs = new ArrayList();
102: titleNrsCollectionsByDocuments.put(view.getDocument(),
103: titleNrs);
104: }
105: int titleNr = 0;
106: Integer titleNrInteger;
107: do {
108: titleNrInteger = new Integer(++titleNr);
109: } while (titleNrs.contains(titleNrInteger));
110: titleNrs.add(titleNrInteger);
111: String title = view.getDocument().getTitle() + " [" + titleNr
112: + "]";
113: JInternalFrame frame = new JInternalFrame(title, true, true,
114: true, true);
115: titleNrsByFrames.put(frame, titleNrInteger);
116: frame
117: .setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);
118: frame.addInternalFrameListener(this );
119: JComponent component = view.createGUIEditor();
120: frame.getContentPane().add(component);
121: viewsByFrames.put(frame, view);
122: framesByViews.put(view, frame);
123: frame.pack();
124: frame.setLocation(newFrameLocation);
125: newFrameLocation.x += 30;
126: newFrameLocation.y += 30;
127: if (newFrameLocation.x > 150) {
128: newFrameLocation.x = 30;
129: newFrameLocation.y = 30;
130: }
131: getDesktopPane().add(frame);
132: JRadioButtonMenuItem menu = new JRadioButtonMenuItem(title);
133: menu.setFont(environment
134: .getFont(GUIApplicationEnvironment.FONT_MENU));
135: menuButtonGroup.add(menu);
136: menu.addItemListener(this );
137: viewsByMenus.put(menu, view);
138: menusByViews.put(view, menu);
139: if (menusByViews.size() == 1) {
140: environment.getWindowMenu().addSeparator();
141: }
142: environment.getWindowMenu().add(menu);
143: frame.setVisible(true);
144: setActiveView(view);
145: }
146:
147: public void removeView(DocumentView view) {
148: super .removeView(view);
149: JInternalFrame frame = (JInternalFrame) framesByViews.get(view);
150: JRadioButtonMenuItem menu = (JRadioButtonMenuItem) menusByViews
151: .get(view);
152: frame.dispose();
153: getDesktopPane().remove(frame);
154: viewsByFrames.remove(frame);
155: framesByViews.remove(view);
156: ((GUIApplicationEnvironment) getEnvironment()).getWindowMenu()
157: .remove(menu);
158: viewsByMenus.remove(menu);
159: menusByViews.remove(view);
160: menuButtonGroup.remove(menu);
161: Object titleNr = titleNrsByFrames.remove(frame);
162: Collection titleNrs = (Collection) titleNrsCollectionsByDocuments
163: .get(view.getDocument());
164: titleNrs.remove(titleNr);
165: if (titleNrs.isEmpty()) {
166: titleNrsCollectionsByDocuments.remove(view.getDocument());
167: }
168:
169: recentActiveViews.remove(view);
170: if (!recentActiveViews.isEmpty()) {
171: setActiveView((DocumentView) recentActiveViews.get(0));
172: } else if (!viewsByFrames.isEmpty()) {
173: setActiveView((DocumentView) viewsByFrames.values()
174: .iterator().next());
175: } else {
176: setActiveView(null);
177: }
178: }
179:
180: public void internalFrameActivated(InternalFrameEvent e) {
181: DocumentView view = (DocumentView) viewsByFrames.get(e
182: .getInternalFrame());
183: setActiveView(view);
184: }
185:
186: public void internalFrameClosed(InternalFrameEvent e) {
187: //nop
188: }
189:
190: public void internalFrameClosing(InternalFrameEvent e) {
191: JInternalFrame frame = e.getInternalFrame();
192: DocumentView view = (DocumentView) viewsByFrames.get(frame);
193: removeView(view);
194: }
195:
196: public void internalFrameDeactivated(InternalFrameEvent e) {
197: //nop
198: }
199:
200: public void internalFrameDeiconified(InternalFrameEvent e) {
201: //nop
202: }
203:
204: public void internalFrameIconified(InternalFrameEvent e) {
205: //nop
206: }
207:
208: public void internalFrameOpened(InternalFrameEvent e) {
209: //nop
210: }
211:
212: public void itemStateChanged(ItemEvent e) {
213: if (e.getStateChange() == ItemEvent.SELECTED) {
214: JRadioButtonMenuItem menu = (JRadioButtonMenuItem) e
215: .getItem();
216: DocumentView view = (DocumentView) viewsByMenus.get(menu);
217: setActiveView(view);
218: }
219: }
220:
221: public JComponent getGUIEditor(DocumentView view) {
222: JInternalFrame frame = (JInternalFrame) framesByViews.get(view);
223: if (frame != null) {
224: return (JComponent) frame.getContentPane().getComponent(0);
225: } else {
226: return null;
227: }
228: }
229:
230: public void updateWindowTitles() {
231: for (Iterator it = getAllViews().iterator(); it.hasNext();) {
232: de.gulden.framework.amoda.model.document.DocumentView view = (DocumentView) it
233: .next();
234: de.gulden.framework.amoda.model.document.Document document = view
235: .getDocument();
236: javax.swing.JInternalFrame frame = (javax.swing.JInternalFrame) framesByViews
237: .get(view);
238: Integer nr = (Integer) titleNrsByFrames.get(frame);
239: frame.setTitle(document.getTitle() + " [" + nr.toString()
240: + "]");
241: }
242: }
243:
244: protected JDesktopPane getDesktopPane() {
245: return ((de.gulden.framework.amoda.environment.gui.component.GUIFrame) (((GUIApplicationEnvironment) getEnvironment())
246: .getFrame())).getDesktopPane();
247: }
248:
249: } // end GUIWorkspace
|