01: /*
02: * Copyright (C) 2004 NNL Technology AB
03: * Visit www.infonode.net for information about InfoNode(R)
04: * products and how to contact NNL Technology AB.
05: *
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU General Public License
08: * as published by the Free Software Foundation; either version 2
09: * of the License, or (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU General Public License for more details.
15: *
16: * You should have received a copy of the GNU General Public License
17: * along with this program; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19: * MA 02111-1307, USA.
20: */
21:
22: // $Id: ViewMap.java,v 1.7 2005/02/16 11:28:14 jesper Exp $
23: package net.infonode.docking.util;
24:
25: import net.infonode.docking.View;
26:
27: import java.io.IOException;
28: import java.io.ObjectInputStream;
29: import java.io.ObjectOutputStream;
30:
31: /**
32: * A map of views that handles view serialization by assigning an integer id to each view.
33: * The id is unique for each view in the map. To guarantee serialization compatibility a view id must remain constant.
34: *
35: * @author $Author: jesper $
36: * @version $Revision: 1.7 $
37: */
38: public class ViewMap extends AbstractViewMap {
39: /**
40: * Constructor.
41: */
42: public ViewMap() {
43: }
44:
45: /**
46: * Utility constructor that creates a map with a number of views.
47: * A view gets it's index in the array as id.
48: *
49: * @param views the views to add to the map
50: */
51: public ViewMap(View[] views) {
52: for (int i = 0; i < views.length; i++)
53: addView(i, views[i]);
54: }
55:
56: /**
57: * Adds a view to the map.
58: *
59: * @param id the view id
60: * @param view the view
61: */
62: public void addView(int id, View view) {
63: addView(new Integer(id), view);
64: }
65:
66: /**
67: * Removes a view with a specific id from the map.
68: *
69: * @param id the view id
70: */
71: public void removeView(int id) {
72: removeView(new Integer(id));
73: }
74:
75: /**
76: * Returns the view with a specific id.
77: *
78: * @param id the view id
79: * @return the view with the id
80: */
81: public View getView(int id) {
82: return getView(new Integer(id));
83: }
84:
85: protected void writeViewId(Object id, ObjectOutputStream out)
86: throws IOException {
87: out.writeInt(((Integer) id).intValue());
88: }
89:
90: protected Object readViewId(ObjectInputStream in)
91: throws IOException {
92: return new Integer(in.readInt());
93: }
94:
95: }
|