KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > graph > model > BasicGraphModel


1 /***
2  * FractalGUI: a graphical tool to edit Fractal component configurations.
3  * Copyright (C) 2003 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: fractal@objectweb.org
20  *
21  * Authors: Eric Bruneton, Patrice Fauvel
22  */

23
24 package org.objectweb.fractal.gui.graph.model;
25
26 import org.objectweb.fractal.api.control.BindingController;
27
28 import org.objectweb.fractal.gui.model.Component;
29 import org.objectweb.fractal.gui.Constants;
30
31 import java.util.Map JavaDoc;
32 import java.util.WeakHashMap JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.awt.Color JavaDoc;
36
37 /**
38  * Basic implementation of the {@link GraphModel} interface.
39  */

40
41 public class BasicGraphModel implements GraphModel, BindingController {
42
43   /**
44    * A collection client interface bound to the {@link GraphModelListener
45    * listeners} of this model.
46    */

47
48   public final static String JavaDoc GRAPH_MODEL_LISTENERS_BINDING =
49     "graph-model-listeners";
50
51   /**
52    * The listeners client interface.
53    */

54
55   private Map JavaDoc listeners;
56
57   /**
58    * A map associating {@link Color}s to {@link Component}s.
59    */

60
61   private Map JavaDoc colors;
62
63   /**
64    * A map associating {@link Rect}s to {@link Component}s.
65    */

66
67   private Map JavaDoc positions;
68
69   /**
70    * Constructs a new {@link BasicGraphModel} component.
71    */

72
73   public BasicGraphModel () {
74     colors = new WeakHashMap JavaDoc();
75     positions = new WeakHashMap JavaDoc();
76     listeners = new HashMap JavaDoc();
77   }
78
79   // -------------------------------------------------------------------------
80
// Implementation of the BindingController interface
81
// -------------------------------------------------------------------------
82

83   public String JavaDoc[] listFc () {
84     return (String JavaDoc[])listeners.keySet().toArray(new String JavaDoc[listeners.size()]);
85   }
86
87   public Object JavaDoc lookupFc (final String JavaDoc clientItfName) {
88     if (clientItfName.startsWith(GRAPH_MODEL_LISTENERS_BINDING)) {
89       return listeners.get(clientItfName);
90     }
91     return null;
92   }
93
94   public void bindFc (
95     final String JavaDoc clientItfName,
96     final Object JavaDoc serverItf)
97   {
98     if (clientItfName.startsWith(GRAPH_MODEL_LISTENERS_BINDING)) {
99       listeners.put(clientItfName, serverItf);
100     }
101   }
102
103   public void unbindFc (final String JavaDoc clientItfName) {
104     if (clientItfName.startsWith(GRAPH_MODEL_LISTENERS_BINDING)) {
105       listeners.remove(clientItfName);
106     }
107   }
108
109   // -------------------------------------------------------------------------
110
// Implementation of the GraphModel interface
111
// -------------------------------------------------------------------------
112

113   public Color JavaDoc getComponentColor (Component component) {
114     Color JavaDoc c = (Color JavaDoc)colors.get(component);
115     if (c == null) {
116       c = Constants.COMPONENT_COLOR;
117       colors.put(component, c);
118     }
119     return c;
120   }
121
122   public void setComponentColor (Component component, Color JavaDoc color) {
123     Color JavaDoc oldColor = getComponentColor(component);
124     if (!color.equals(oldColor)) {
125       colors.put(component, color);
126       Iterator JavaDoc i = listeners.values().iterator();
127       while (i.hasNext()) {
128         GraphModelListener l = (GraphModelListener)i.next();
129         l.componentColorChanged(component, oldColor);
130       }
131     }
132   }
133
134   public Rect getComponentPosition (final Component component) {
135     Rect result = (Rect)positions.get(component);
136     if (result == null) {
137       result = new Rect(0.1, 0.1, 0.9, 0.9);
138       positions.put(component, result);
139     }
140     return result;
141   }
142
143   public void setComponentPosition (
144     final Component component,
145     final Rect position)
146   {
147     Rect oldPosition = getComponentPosition(component);
148     if (!position.equals(oldPosition)) {
149       positions.put(component, position);
150       Iterator JavaDoc i = listeners.values().iterator();
151       while (i.hasNext()) {
152         GraphModelListener l = (GraphModelListener)i.next();
153         l.componentPositionChanged(component, oldPosition);
154       }
155     }
156   }
157 }
158
Popular Tags