KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > repository > lib > FractalGUIBackend


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.repository.lib;
25
26 import java.awt.Color JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.objectweb.fractal.adl.attributes.AttributeBuilder;
32 import org.objectweb.fractal.adl.bindings.BindingBuilder;
33 import org.objectweb.fractal.adl.components.ComponentBuilder;
34 import org.objectweb.fractal.adl.coordinates.CoordinatesBuilder;
35 import org.objectweb.fractal.adl.implementations.ImplementationBuilder;
36 import org.objectweb.fractal.adl.types.TypeBuilder;
37 import org.objectweb.fractal.gui.graph.model.GraphModel;
38 import org.objectweb.fractal.gui.graph.model.Rect;
39 import org.objectweb.fractal.gui.model.ClientInterface;
40 import org.objectweb.fractal.gui.model.Component;
41 import org.objectweb.fractal.gui.model.Factory;
42 import org.objectweb.fractal.gui.model.Interface;
43 import org.objectweb.fractal.gui.model.ServerInterface;
44
45 public class FractalGUIBackend implements
46   TypeBuilder, ComponentBuilder, ImplementationBuilder, BindingBuilder, AttributeBuilder, CoordinatesBuilder
47 {
48
49   public Object JavaDoc createInterfaceType (
50     String JavaDoc name,
51     String JavaDoc signature,
52     String JavaDoc role,
53     String JavaDoc contingency,
54     String JavaDoc cardinality,
55     Object JavaDoc context) throws Exception JavaDoc
56   {
57     return new String JavaDoc[] {
58       name, signature, role, contingency, cardinality
59     };
60   }
61   
62   public Object JavaDoc createComponentType (
63     String JavaDoc name,
64     Object JavaDoc[] interfaceTypes,
65     Object JavaDoc context) throws Exception JavaDoc
66   {
67     return interfaceTypes;
68   }
69
70   public Object JavaDoc createComponent (
71     Object JavaDoc type,
72     String JavaDoc name,
73     String JavaDoc definition,
74     Object JavaDoc controllerDesc,
75     Object JavaDoc contentDesc,
76     Object JavaDoc context) throws Exception JavaDoc
77   {
78     Factory f = (Factory)((Map JavaDoc)context).get("factory");
79     Component c = f.createComponent();
80     c.setName(name);
81     if (definition != null &&
82         definition.indexOf(',') == -1 && definition.indexOf('/') == -1)
83     {
84       c.setType(definition);
85     }
86     c.setComponentControllerDescriptor((String JavaDoc)controllerDesc);
87     if (contentDesc != null) {
88       c.setImplementation((String JavaDoc)contentDesc);
89     }
90     
91     Object JavaDoc[] itfTypes = (Object JavaDoc[])type;
92     for (int i = 0; i < itfTypes.length; ++i) {
93       String JavaDoc[] itf = (String JavaDoc[])itfTypes[i];
94       String JavaDoc itfName = itf[0];
95       if (itfName.equals("attribute-controller")) {
96         continue;
97       }
98       String JavaDoc itfSignature = itf[1];
99       String JavaDoc itfRole = itf[2];
100       String JavaDoc itfContingency = itf[3];
101       String JavaDoc itfCardinality = itf[4];
102       if (itfRole.equals("server")) {
103         ServerInterface sitf = f.createServerInterface(c);
104         sitf.setName(itfName);
105         sitf.setSignature(itfSignature);
106         sitf.setIsOptional("optional".equals(itfContingency));
107         sitf.setIsCollection("collection".equals(itfCardinality));
108         c.addServerInterface(sitf);
109       } else {
110         ClientInterface citf = f.createClientInterface(c);
111         citf.setName(itfName);
112         citf.setSignature(itfSignature);
113         citf.setIsOptional("optional".equals(itfContingency));
114         citf.setIsCollection("collection".equals(itfCardinality));
115         c.addClientInterface(citf);
116       }
117     }
118     
119     return c;
120   }
121   
122   public void addComponent (
123     Object JavaDoc superComponent,
124     Object JavaDoc subComponent,
125     String JavaDoc name,
126     Object JavaDoc context) throws Exception JavaDoc
127   {
128     Component sc = (Component)superComponent;
129     Component c = (Component)subComponent;
130     if (c.getParent() == null) {
131       sc.addSubComponent(c);
132     } else {
133       Factory f = (Factory)((Map JavaDoc)context).get("factory");
134       Component d = f.createComponent(c);
135       d.setName(c.getName());
136       sc.addSubComponent(d);
137     }
138   }
139   
140   public void bindComponent (
141     int type,
142     Object JavaDoc client,
143     String JavaDoc clientItf,
144     Object JavaDoc server,
145     String JavaDoc serverItf,
146     Object JavaDoc context) throws Exception JavaDoc
147   {
148     Component c = (Component)client;
149     Component s = (Component)server;
150     if (type == NORMAL_BINDING) {
151       if (c.getParent() != s.getParent()) {
152         if (c.getMasterComponent() != null) {
153           c = c.getMasterComponent();
154         }
155         if (s.getMasterComponent() != null) {
156           s = s.getMasterComponent();
157         }
158         List JavaDoc cl = new ArrayList JavaDoc(c.getSlaveComponents());
159         List JavaDoc sl = new ArrayList JavaDoc(s.getSlaveComponents());
160         cl.add(c);
161         sl.add(s);
162         c = null;
163         s = null;
164         for (int i = 0; i < cl.size(); ++i) {
165           for (int j = 0; j < sl.size(); ++j) {
166             Component ci = (Component)cl.get(i);
167             Component sj = (Component)sl.get(j);
168             if (ci.getParent() == sj.getParent()) {
169               c = ci;
170               s = sj;
171               break;
172             }
173           }
174         }
175         if (c == null || s == null) {
176           throw new RuntimeException JavaDoc("Internal error");
177         }
178       }
179     } else if (type == EXPORT_BINDING) {
180       if (c != s.getParent()) {
181         if (c.getMasterComponent() != null) {
182           c = c.getMasterComponent();
183         }
184         if (s.getMasterComponent() != null) {
185           s = s.getMasterComponent();
186         }
187         List JavaDoc cl = new ArrayList JavaDoc(c.getSlaveComponents());
188         List JavaDoc sl = new ArrayList JavaDoc(s.getSlaveComponents());
189         cl.add(c);
190         sl.add(s);
191         c = null;
192         s = null;
193         for (int i = 0; i < cl.size(); ++i) {
194           for (int j = 0; j < sl.size(); ++j) {
195             Component ci = (Component)cl.get(i);
196             Component sj = (Component)sl.get(j);
197             if (ci == sj.getParent()) {
198               c = ci;
199               s = sj;
200               break;
201             }
202           }
203         }
204         if (c == null || s == null) {
205           throw new RuntimeException JavaDoc("Internal error");
206         }
207       }
208     } else {
209       if (c.getParent() != s) {
210         if (c.getMasterComponent() != null) {
211           c = c.getMasterComponent();
212         }
213         if (s.getMasterComponent() != null) {
214           s = s.getMasterComponent();
215         }
216         List JavaDoc cl = new ArrayList JavaDoc(c.getSlaveComponents());
217         List JavaDoc sl = new ArrayList JavaDoc(s.getSlaveComponents());
218         cl.add(c);
219         sl.add(s);
220         c = null;
221         s = null;
222         for (int i = 0; i < cl.size(); ++i) {
223           for (int j = 0; j < sl.size(); ++j) {
224             Component ci = (Component)cl.get(i);
225             Component sj = (Component)sl.get(j);
226             if (ci.getParent() == sj) {
227               c = ci;
228               s = sj;
229               break;
230             }
231           }
232         }
233         if (c == null || s == null) {
234           throw new RuntimeException JavaDoc("Internal error");
235         }
236       }
237     }
238     
239     Interface citf, sitf;
240     if (type == NORMAL_BINDING) {
241       citf = getInterface(c.getClientInterfaces(), clientItf);
242       sitf = s.getServerInterface(serverItf);
243     } else if (type == EXPORT_BINDING) {
244       citf = getInterface(c.getServerInterfaces(), clientItf).getComplementaryInterface();
245       sitf = s.getServerInterface(serverItf);
246     } else {
247       citf = getInterface(c.getClientInterfaces(), clientItf);
248       sitf = s.getClientInterface(serverItf).getComplementaryInterface();
249     }
250     
251     c.bind((ClientInterface)citf, clientItf.substring(citf.getName().length()), (ServerInterface)sitf);
252   }
253
254   private static Interface getInterface (List JavaDoc interfaces, String JavaDoc name) {
255     for (int i = 0; i < interfaces.size(); ++i) {
256       Interface itf = (Interface)interfaces.get(i);
257       if (itf.isCollection()) {
258         if (itf.getMasterCollectionInterface() != null) {
259           itf = itf.getMasterCollectionInterface();
260         }
261         if (name.startsWith(itf.getName())) {
262           return itf;
263         }
264       } else if (name.equals(itf.getName())) {
265         return itf;
266       }
267     }
268     throw new RuntimeException JavaDoc("Internal error");
269   }
270   
271   public void startComponent (Object JavaDoc component, Object JavaDoc context) throws Exception JavaDoc {
272     // does nothing
273
}
274
275   public void setAttribute (
276     Object JavaDoc component,
277     String JavaDoc attributeController,
278     String JavaDoc name,
279     String JavaDoc value,
280     Object JavaDoc context) throws Exception JavaDoc
281   {
282     Component c = (Component)component;
283     c.setAttributeController(attributeController);
284     c.setAttribute(name, value);
285   }
286   
287   public void setCoordinates (
288     Object JavaDoc parent,
289     Object JavaDoc component,
290     double x0,
291     double y0,
292     double x1,
293     double y1,
294     int color,
295     Object JavaDoc context)
296   {
297     Component c = (Component)component;
298     if (c.getParent() != parent) {
299       if (c.getMasterComponent() != null) {
300         c = c.getMasterComponent();
301       }
302       if (c.getParent() != parent) {
303         List JavaDoc cl = c.getSlaveComponents();
304         for (int i = 0; i < cl.size(); ++i) {
305           c = (Component)cl.get(i);
306           if (c.getParent() == parent) {
307             break;
308           }
309         }
310       }
311     }
312     GraphModel g = (GraphModel)((Map JavaDoc)context).get("graph");
313     if (g != null) {
314       g.setComponentPosition(c, new Rect(x0, y0, x1, y1));
315       g.setComponentColor(c, new Color JavaDoc(color));
316     }
317   }
318 }
319
Popular Tags