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: AbstractViewMap.java,v 1.11 2005/02/16 11:28:14 jesper Exp $
023: package net.infonode.docking.util;
024:
025: import net.infonode.docking.View;
026: import net.infonode.docking.ViewSerializer;
027:
028: import javax.swing.*;
029: import java.io.IOException;
030: import java.io.ObjectInputStream;
031: import java.io.ObjectOutputStream;
032: import java.util.ArrayList;
033: import java.util.HashMap;
034: import java.util.Iterator;
035: import java.util.Map;
036:
037: /**
038: * Base class for view maps.
039: *
040: * @author $Author: jesper $
041: * @version $Revision: 1.11 $
042: * @since IDW 1.1.0
043: */
044: abstract public class AbstractViewMap implements ViewFactoryManager,
045: ViewSerializer {
046: private HashMap viewMap = new HashMap();
047: private ArrayList views = new ArrayList(20);
048:
049: abstract protected void writeViewId(Object id,
050: ObjectOutputStream out) throws IOException;
051:
052: abstract protected Object readViewId(ObjectInputStream in)
053: throws IOException;
054:
055: /**
056: * Returns the number of views in this map.
057: *
058: * @return the number of views in this map
059: */
060: public int getViewCount() {
061: return viewMap.size();
062: }
063:
064: /**
065: * Returns the view at a specific index.
066: * The view index is the same as the number of views in the map when the view was added to the map.
067: *
068: * @param index the view index
069: * @return the view at the index
070: */
071: public View getViewAtIndex(int index) {
072: return (View) views.get(index);
073: }
074:
075: public ViewFactory[] getViewFactories() {
076: ArrayList f = new ArrayList();
077:
078: for (int i = 0; i < views.size(); i++) {
079: final View view = (View) views.get(i);
080:
081: if (view.getRootWindow() == null)
082: f.add(new ViewFactory() {
083: public Icon getIcon() {
084: return view.getIcon();
085: }
086:
087: public String getTitle() {
088: return view.getTitle();
089: }
090:
091: public View createView() {
092: return view;
093: }
094: });
095: }
096:
097: return (ViewFactory[]) f.toArray(new ViewFactory[f.size()]);
098: }
099:
100: /**
101: * Returns true if this view map contains the view.
102: *
103: * @param view the view
104: * @return true if this view map contains the view
105: * @since IDW 1.3.0
106: */
107: public boolean contains(View view) {
108: return views.contains(view);
109: }
110:
111: public void writeView(View view, ObjectOutputStream out)
112: throws IOException {
113: for (Iterator it = viewMap.entrySet().iterator(); it.hasNext();) {
114: Map.Entry entry = (Map.Entry) it.next();
115:
116: if (entry.getValue() == view) {
117: writeViewId(entry.getKey(), out);
118: return;
119: }
120: }
121:
122: throw new IOException("Serialization of unknown view!");
123: }
124:
125: public View readView(ObjectInputStream in) throws IOException {
126: return (View) viewMap.get(readViewId(in));
127: }
128:
129: protected void addView(Object id, View view) {
130: Object oldView = viewMap.put(id, view);
131:
132: if (oldView != null)
133: views.remove(oldView);
134:
135: views.add(view);
136: }
137:
138: protected void removeView(Object id) {
139: Object view = viewMap.remove(id);
140:
141: if (view != null)
142: views.remove(view);
143: }
144:
145: protected View getView(Object id) {
146: return (View) viewMap.get(id);
147: }
148:
149: }
|