001: package desktop;
002:
003: import org.wings.*;
004: import org.wings.util.ComponentVisitor;
005:
006: import java.awt.event.ActionEvent;
007: import java.awt.event.ActionListener;
008: import java.util.prefs.Preferences;
009:
010: public class DesktopPane extends SDesktopPane {
011:
012: public static String FIRST_FREE_INDEX = "firstFreeIndex";
013: public static String WEIGHTX = "weightx";
014:
015: private Preferences pref;
016:
017: public static org.wings.util.SessionLocal<Integer> paneNo = new org.wings.util.SessionLocal<Integer>() {
018: @Override
019: protected Integer initialValue() {
020: return 0;
021: }
022: };
023:
024: public DesktopPane(String name) {
025: super ();
026: setName(name);
027:
028: pref = Preferences.userRoot().node("desktoppanes").node(
029: this .getName());
030:
031: setCG(new DpCG());
032: setVerticalAlignment(SConstants.TOP_ALIGN);
033: addNonFrameComponent(new DropLabel());
034:
035: setupContextMenu();
036: }
037:
038: public DesktopPane() {
039: super ();
040: setName("desktoppane" + paneNo.get().toString());
041: paneNo.set(paneNo.get() + 1);
042:
043: pref = Preferences.userRoot().node("desktoppanes").node(
044: this .getName());
045: pref.putInt(FIRST_FREE_INDEX, paneNo.get());
046:
047: setCG(new DpCG());
048: setVerticalAlignment(SConstants.TOP_ALIGN);
049: addNonFrameComponent(new DropLabel());
050:
051: setupContextMenu();
052: }
053:
054: private void setupContextMenu() {
055:
056: SPopupMenu popupMenu = new SPopupMenu();
057: SMenuItem menuItem;
058:
059: for (final DesktopTool tool : ToolRegistry.getToolRegistry()
060: .getRegisteredTools().values()) {
061: menuItem = new SMenuItem(tool.getText(), tool.getIcon());
062: menuItem.addActionListener(new ActionListener() {
063: public void actionPerformed(ActionEvent evt) {
064: addDesktopItem(tool.getItem());
065: }
066: });
067: popupMenu.add(menuItem);
068: }
069:
070: this .setComponentPopupMenu(popupMenu);
071: }
072:
073: public void addDesktopItem(DesktopTool tool) {
074: addDesktopItem(tool.getItem());
075: }
076:
077: public void addDesktopItem(DesktopItem contentItem) {
078: final InternalDesktopFrame newFrame = new InternalDesktopFrame();
079: contentItem.setContainer(newFrame);
080: Preferences p = Preferences.userRoot().node("desktopitems");
081: p.node((String) contentItem.getValue(DesktopItem.KEY)).put(
082: DesktopItem.DESKTOPPANE, this .getName());
083: p.node((String) contentItem.getValue(DesktopItem.KEY)).putInt(
084: DesktopItem.POSITION_ON_PANE,
085: getComponentList().size() - 1);
086:
087: this .add(newFrame, getComponentList().size() - 1);
088:
089: try {
090: this .invite(new ComponentVisitor() {
091: public void visit(SComponent c) { /* ign */
092: }
093:
094: public void visit(SContainer c) {
095: if (!(c instanceof SInternalFrame))
096: return;
097: SInternalFrame ff = (SInternalFrame) c;
098: if (ff != newFrame && ff.isMaximized()) {
099: ff.setMaximized(false);
100: // set _our_ frame maximized, then.
101: newFrame.setMaximized(true);
102: }
103: }
104: });
105:
106: pref.flush();
107: } catch (Exception e) {
108: System.err.println(e);
109: }
110:
111: }
112:
113: public void addExistingDesktopItem(DesktopItem contentItem) {
114: final InternalDesktopFrame newFrame = new InternalDesktopFrame();
115: contentItem.setContainer(newFrame);
116: this .add(newFrame, getComponentList().size() - 1);
117:
118: try {
119: this .invite(new ComponentVisitor() {
120: public void visit(SComponent c) { /* ign */
121: }
122:
123: public void visit(SContainer c) {
124: if (!(c instanceof SInternalFrame))
125: return;
126: SInternalFrame ff = (SInternalFrame) c;
127: if (ff != newFrame && ff.isMaximized()) {
128: ff.setMaximized(false);
129: // set _our_ frame maximized, then.
130: newFrame.setMaximized(true);
131: }
132: }
133: });
134:
135: pref.flush();
136: } catch (Exception e) {
137: System.err.println(e);
138: }
139: }
140:
141: public SComponent addNonFrameComponent(SComponent component) {
142: return addNonFrameComponent(component, null);
143: }
144:
145: public SComponent addNonFrameComponent(SComponent component,
146: Object constraints) {
147: if (component != null) {
148: // Sanity check: Component is only allowed to be inside one container
149: // equal to Swing (1:n tree, not n:m tree)
150: if (component.getParent() != null) {
151: if (component.getParent() == this ) {
152: // Forstall confusing ArrayOutOfBoundsException which would occur in the remove.
153: throw new IllegalArgumentException(
154: "Component must only added exactly "
155: + "once to exactly one container!");
156: } else {
157: // Try to silently fix the double-add by removing it from the old parent.
158: component.getParent().remove(component);
159: }
160: }
161:
162: if (constraints == null)
163: constraints = component.getName();
164:
165: getComponentList().add(component);
166: getConstraintList().add(constraints);
167: component.setParent(this );
168:
169: if (super .getLayout() != null)
170: super .getLayout().addComponent(component, constraints,
171: getComponentList().indexOf(component));
172:
173: component.addNotify();
174:
175: this .reload();
176: }
177: return component;
178: }
179:
180: public void removeAll() {
181: for (SComponent comp : this .getComponents()) {
182: if (comp instanceof SInternalFrame) {
183: ((SInternalFrame) comp).dispose();
184: remove(comp);
185: }
186:
187: getComponentList().remove(comp);
188: }
189: }
190: }
|