001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: WindowItem.java,v 1.18 2007/01/28 21:25:10 jesper Exp $
023: package net.infonode.docking.model;
024:
025: import net.infonode.docking.DockingWindow;
026: import net.infonode.docking.internal.ReadContext;
027: import net.infonode.docking.internal.WriteContext;
028: import net.infonode.docking.properties.DockingWindowProperties;
029: import net.infonode.properties.propertymap.PropertyMap;
030: import net.infonode.properties.propertymap.PropertyMapUtil;
031: import net.infonode.util.Direction;
032:
033: import java.io.IOException;
034: import java.io.ObjectInputStream;
035: import java.io.ObjectOutputStream;
036: import java.lang.ref.WeakReference;
037: import java.util.ArrayList;
038:
039: /**
040: * @author $Author: jesper $
041: * @version $Revision: 1.18 $
042: */
043: abstract public class WindowItem {
044: public static final DockingWindowProperties emptyProperties = new DockingWindowProperties();
045:
046: abstract protected DockingWindow createWindow(
047: ViewReader viewReader, ArrayList childWindows);
048:
049: abstract public WindowItem copy();
050:
051: private WindowItem parent;
052: private WeakReference connectedWindow = new WeakReference(null);
053: private ArrayList windows = new ArrayList();
054: private DockingWindowProperties dockingWindowProperties;
055: private DockingWindowProperties parentProperties = emptyProperties;
056: private Direction lastMinimizedDirection;
057:
058: protected WindowItem() {
059: dockingWindowProperties = new DockingWindowProperties(
060: emptyProperties);
061: }
062:
063: protected WindowItem(WindowItem windowItem) {
064: dockingWindowProperties = new DockingWindowProperties(
065: windowItem.getDockingWindowProperties().getMap().copy(
066: true, true));
067: dockingWindowProperties.getMap().replaceSuperMap(
068: windowItem.getParentDockingWindowProperties().getMap(),
069: emptyProperties.getMap());
070: lastMinimizedDirection = windowItem.getLastMinimizedDirection();
071: }
072:
073: public boolean isRestoreWindow() {
074: return parent != null && parent.isRestoreWindow();
075: }
076:
077: public void addWindow(WindowItem item) {
078: addWindow(item, windows.size());
079: }
080:
081: public void addWindow(WindowItem item, int index) {
082: index = index == -1 ? windows.size() : index;
083:
084: if (item.parent == this ) {
085: int currentIndex = windows.indexOf(item);
086:
087: if (currentIndex != index) {
088: windows.remove(currentIndex);
089: windows.add(currentIndex < index ? index - 1 : index,
090: item);
091: }
092: } else {
093: item.setParent(this );
094: windows.add(index, item);
095: }
096: }
097:
098: public void removeWindow(WindowItem item) {
099: if (windows.remove(item))
100: item.parent = null;
101: }
102:
103: public void removeWindowRefs(DockingWindow window) {
104: if (connectedWindow.get() == window)
105: connectedWindow = new WeakReference(null);
106:
107: for (int i = 0; i < getWindowCount(); i++)
108: getWindow(i).removeWindowRefs(window);
109: }
110:
111: public void replaceWith(WindowItem item) {
112: if (item == this || parent == null)
113: return;
114:
115: item.setParent(parent);
116: int index = parent.windows.indexOf(this );
117: parent.windows.set(index, item);
118: parent = null;
119: }
120:
121: public int getWindowIndex(WindowItem item) {
122: return windows.indexOf(item);
123: }
124:
125: private void setParent(WindowItem parent) {
126: if (this .parent == parent)
127: return;
128:
129: if (this .parent != null)
130: this .parent.removeWindow(this );
131:
132: this .parent = parent;
133: }
134:
135: public final int getWindowCount() {
136: return windows.size();
137: }
138:
139: public final WindowItem getWindow(int index) {
140: return (WindowItem) windows.get(index);
141: }
142:
143: public WindowItem getParent() {
144: return parent;
145: }
146:
147: public void setConnectedWindow(DockingWindow window) {
148: connectedWindow = new WeakReference(window);
149: }
150:
151: public DockingWindow getConnectedWindow() {
152: return (DockingWindow) connectedWindow.get();
153: }
154:
155: public RootWindowItem getRootItem() {
156: return parent == null ? null : parent.getRootItem();
157: }
158:
159: public DockingWindow getVisibleDockingWindow() {
160: DockingWindow window = getConnectedWindow();
161:
162: if (window != null && window.getRootWindow() != null
163: && !window.isMinimized() && !window.isUndocked())
164: return window;
165:
166: for (int i = 0; i < getWindowCount(); i++) {
167: WindowItem item = getWindow(i);
168: window = item.getVisibleDockingWindow();
169:
170: if (window != null)
171: return window;
172: }
173:
174: return null;
175: }
176:
177: public DockingWindow getInsideDockingWindow() {
178: if (getParent() == null)
179: return null;
180:
181: DockingWindow dockingWindow = getParent().getConnectedWindow();
182:
183: if (dockingWindow != null)
184: return dockingWindow;
185:
186: return getParent().getInsideDockingWindow();
187: }
188:
189: public void removeAll() {
190: while (getWindowCount() > 0)
191: removeWindow(getWindow(0));
192: }
193:
194: public boolean cleanUp() {
195: for (int i = getWindowCount() - 1; i >= 0; i--) {
196: if (getWindow(i).cleanUp())
197: windows.remove(i);
198: }
199:
200: return getWindowCount() == 0 && getConnectedWindow() == null;
201: }
202:
203: public DockingWindow getFirstChildWindow() {
204: for (int i = 0; i < getWindowCount(); i++) {
205: DockingWindow window = getWindow(i).getFirstWindow();
206:
207: if (window != null)
208: return window;
209: }
210:
211: return null;
212: }
213:
214: public DockingWindow getFirstWindow() {
215: DockingWindow window = getConnectedWindow();
216: return window != null ? window : getFirstChildWindow();
217: }
218:
219: public WindowItem getChildWindowContaining(WindowItem windowItem) {
220: while (windowItem.getParent() != this ) {
221: windowItem = windowItem.getParent();
222:
223: if (windowItem == null)
224: return null;
225: }
226:
227: return windowItem;
228: }
229:
230: public boolean hasAncestor(WindowItem ancestor) {
231: return this == ancestor
232: || (parent != null && parent.hasAncestor(ancestor));
233: }
234:
235: public WindowItem getTopItem() {
236: return parent == null ? this : parent.getTopItem();
237: }
238:
239: public DockingWindowProperties getDockingWindowProperties() {
240: if (dockingWindowProperties == null) {
241: dockingWindowProperties = new DockingWindowProperties(
242: emptyProperties);
243: parentProperties = emptyProperties;
244: }
245:
246: return dockingWindowProperties;
247: }
248:
249: public DockingWindowProperties getParentDockingWindowProperties() {
250: return parentProperties == null ? emptyProperties
251: : parentProperties;
252: }
253:
254: public void setParentDockingWindowProperties(
255: DockingWindowProperties parentProperties) {
256: dockingWindowProperties.getMap().replaceSuperMap(
257: this .parentProperties.getMap(),
258: parentProperties.getMap());
259: this .parentProperties = parentProperties;
260: }
261:
262: public Direction getLastMinimizedDirection() {
263: return lastMinimizedDirection;
264: }
265:
266: public void setLastMinimizedDirection(
267: Direction lastMinimizedDirection) {
268: this .lastMinimizedDirection = lastMinimizedDirection;
269: }
270:
271: public void writeSettings(ObjectOutputStream out,
272: WriteContext context) throws IOException {
273: out.writeInt(getLastMinimizedDirection() == null ? -1
274: : getLastMinimizedDirection().getValue());
275:
276: if (context.getWritePropertiesEnabled()) {
277: dockingWindowProperties.getMap().write(out, true);
278: getPropertyObject().write(out, true);
279: }
280: }
281:
282: public void readSettings(ObjectInputStream in, ReadContext context)
283: throws IOException {
284: if (context.getVersion() > 1) {
285: int dir = in.readInt();
286: setLastMinimizedDirection(dir == -1 ? null : Direction
287: .getDirections()[dir]);
288: }
289:
290: if (context.isPropertyValuesAvailable()) {
291: if (context.getReadPropertiesEnabled()) {
292: dockingWindowProperties.getMap().read(in);
293: getPropertyObject().read(in);
294: } else {
295: PropertyMapUtil.skipMap(in);
296: PropertyMapUtil.skipMap(in);
297: }
298: }
299: }
300:
301: public void write(ObjectOutputStream out, WriteContext context,
302: ViewWriter viewWriter) throws IOException {
303: out.writeInt(getWindowCount());
304:
305: for (int i = 0; i < getWindowCount(); i++)
306: getWindow(i).write(out, context, viewWriter);
307:
308: DockingWindow window = getConnectedWindow();
309: writeSettings(out, context);
310: boolean b = window != null && !window.isMinimized()
311: && !window.isUndocked()
312: && window.getRootWindow() != null;
313: out.writeBoolean(window != null && !window.isMinimized()
314: && !window.isUndocked()
315: && window.getRootWindow() != null);
316: }
317:
318: public DockingWindow read(ObjectInputStream in,
319: ReadContext context, ViewReader viewReader)
320: throws IOException {
321: ArrayList childWindows = readChildWindows(in, context,
322: viewReader);
323: readSettings(in, context);
324: return in.readBoolean() ? createWindow(viewReader, childWindows)
325: : childWindows.size() > 0 ? (DockingWindow) childWindows
326: .get(0)
327: : null;
328: }
329:
330: public ArrayList readChildWindows(ObjectInputStream in,
331: ReadContext context, ViewReader viewReader)
332: throws IOException {
333: int count = in.readInt();
334: removeAll();
335: ArrayList childWindows = new ArrayList();
336:
337: for (int i = 0; i < count; i++) {
338: WindowItem childItem = WindowItemDecoder.decodeWindowItem(
339: in, context, viewReader);
340: addWindow(childItem);
341: DockingWindow cw = childItem.read(in, context, viewReader);
342:
343: if (cw != null)
344: childWindows.add(cw);
345: }
346:
347: return childWindows;
348: }
349:
350: protected PropertyMap getPropertyObject() {
351: return null;
352: }
353:
354: public String toString() {
355: StringBuffer s = new StringBuffer();
356: DockingWindow dw = getConnectedWindow();
357: s.append(dw + ":\n");
358:
359: for (int i = 0; i < windows.size(); i++)
360: s.append(" " + windows.get(i).toString());
361:
362: return s.toString();
363: }
364:
365: public void clearWindows() {
366: removeAll();
367: }
368:
369: }
|