1 23 24 package org.objectweb.fractal.gui.repository.lib; 25 26 import java.awt.Color ; 27 import java.util.ArrayList ; 28 import java.util.List ; 29 import java.util.Map ; 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 createInterfaceType ( 50 String name, 51 String signature, 52 String role, 53 String contingency, 54 String cardinality, 55 Object context) throws Exception 56 { 57 return new String [] { 58 name, signature, role, contingency, cardinality 59 }; 60 } 61 62 public Object createComponentType ( 63 String name, 64 Object [] interfaceTypes, 65 Object context) throws Exception 66 { 67 return interfaceTypes; 68 } 69 70 public Object createComponent ( 71 Object type, 72 String name, 73 String definition, 74 Object controllerDesc, 75 Object contentDesc, 76 Object context) throws Exception 77 { 78 Factory f = (Factory)((Map )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 )controllerDesc); 87 if (contentDesc != null) { 88 c.setImplementation((String )contentDesc); 89 } 90 91 Object [] itfTypes = (Object [])type; 92 for (int i = 0; i < itfTypes.length; ++i) { 93 String [] itf = (String [])itfTypes[i]; 94 String itfName = itf[0]; 95 if (itfName.equals("attribute-controller")) { 96 continue; 97 } 98 String itfSignature = itf[1]; 99 String itfRole = itf[2]; 100 String itfContingency = itf[3]; 101 String 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 superComponent, 124 Object subComponent, 125 String name, 126 Object context) throws Exception 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 )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 client, 143 String clientItf, 144 Object server, 145 String serverItf, 146 Object context) throws Exception 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 cl = new ArrayList (c.getSlaveComponents()); 159 List sl = new ArrayList (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 ("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 cl = new ArrayList (c.getSlaveComponents()); 188 List sl = new ArrayList (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 ("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 cl = new ArrayList (c.getSlaveComponents()); 217 List sl = new ArrayList (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 ("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 interfaces, String 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 ("Internal error"); 269 } 270 271 public void startComponent (Object component, Object context) throws Exception { 272 } 274 275 public void setAttribute ( 276 Object component, 277 String attributeController, 278 String name, 279 String value, 280 Object context) throws Exception 281 { 282 Component c = (Component)component; 283 c.setAttributeController(attributeController); 284 c.setAttribute(name, value); 285 } 286 287 public void setCoordinates ( 288 Object parent, 289 Object component, 290 double x0, 291 double y0, 292 double x1, 293 double y1, 294 int color, 295 Object 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 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 )context).get("graph"); 313 if (g != null) { 314 g.setComponentPosition(c, new Rect(x0, y0, x1, y1)); 315 g.setComponentColor(c, new Color (color)); 316 } 317 } 318 } 319 | Popular Tags |