1 23 24 package org.objectweb.fractal.gui.model; 25 26 import java.util.HashSet ; 27 import java.util.List ; 28 import java.util.ArrayList ; 29 import java.util.Collections ; 30 import java.util.Set ; 31 32 import javax.swing.JOptionPane ; 33 34 37 38 public abstract class BasicInterface implements Interface { 39 40 43 44 protected BasicComponent owner; 45 46 49 50 private long status; 51 52 55 56 String name; 57 58 61 62 private String signature; 63 64 67 68 private boolean isInternal; 69 70 73 74 private boolean isOptional; 75 76 79 80 private boolean isCollection; 81 82 86 87 protected Interface masterCollectionItf; 88 89 94 95 protected List slaveCollectionItfs; 96 97 100 101 protected Interface complementaryItf; 102 103 108 109 BasicInterface (final BasicComponent owner) { 110 this.owner = owner; 111 status = NAME_MISSING | SIGNATURE_MISSING; 112 name = ""; 113 signature = ""; 114 isInternal = false; 115 slaveCollectionItfs = new ArrayList (); 116 } 117 118 123 124 BasicInterface (final Interface externalItf) { 125 isInternal = true; 126 complementaryItf = externalItf; 127 } 128 129 135 136 BasicInterface (final Interface masterCollectionItf, final int ignored) { 137 this.isCollection = true; 138 this.masterCollectionItf = masterCollectionItf; 139 } 140 141 public Component getOwner () { 142 return isInternal 143 ? complementaryItf.getOwner() 144 : masterCollectionItf != null 145 ? masterCollectionItf.getOwner() 146 : owner; 147 } 148 149 public long getStatus () { 150 return isInternal 151 ? complementaryItf.getStatus() 152 : masterCollectionItf != null 153 ? masterCollectionItf.getStatus() 154 : status; 155 } 156 157 public void setStatus (final long status) { 158 this.status = status; 159 } 160 161 public String getName () { 162 if (isInternal) { 163 return complementaryItf.getName(); 164 } else if (masterCollectionItf != null) { 165 if (name == null) { 166 List l = masterCollectionItf.getSlaveCollectionInterfaces(); 167 Set s = new HashSet (); 168 for (int i = 0; i < l.size(); ++i) { 169 if (l.get(i) != this) { 170 s.add(((Interface)l.get(i)).getName()); 171 } 172 } 173 int i = 0; 174 do { 175 name = Integer.toString(100 + (i++)).substring(1); 176 } while (s.contains(masterCollectionItf.getName() + name)); 177 } 178 return masterCollectionItf.getName() + name; 179 184 } else { 185 return name; 186 } 187 } 188 189 public void setName (final String name) { 190 if (name == null) { 191 throw new IllegalArgumentException (); 192 } 193 if (isInternal) { 194 complementaryItf.setName(name); 195 } else if (masterCollectionItf != null) { 196 masterCollectionItf.setName(name); 197 } else { 198 String oldName = this.name; 199 if (!name.equals(oldName)) { 200 List vetoableListeners = owner.getOwner().getVetoableListeners(); 201 for (int i = 0; i < vetoableListeners.size(); ++i) { 202 VetoableConfigurationListener l = 203 (VetoableConfigurationListener)vetoableListeners.get(i); 204 l.canChangeInterfaceName(this); 205 } 206 this.name = name; 207 List listeners = owner.getOwner().getListeners(); 208 for (int i = 0; i < listeners.size(); ++i) { 209 ConfigurationListener l = (ConfigurationListener)listeners.get(i); 210 l.interfaceNameChanged(this, oldName); 211 } 212 } 213 } 214 } 215 216 public String getSignature () { 217 return isInternal 218 ? complementaryItf.getSignature() 219 : masterCollectionItf != null 220 ? masterCollectionItf.getSignature() 221 : signature; 222 } 223 224 public void setSignature (final String signature) { 225 if (signature == null) { 226 throw new IllegalArgumentException (); 227 } 228 if (isInternal) { 229 complementaryItf.setSignature(signature); 230 } else if (masterCollectionItf != null) { 231 masterCollectionItf.setSignature(signature); 232 } else { 233 String oldSignature = this.signature; 234 if (!signature.equals(oldSignature)) { 235 List vetoableListeners = owner.getOwner().getVetoableListeners(); 236 for (int i = 0; i < vetoableListeners.size(); ++i) { 237 VetoableConfigurationListener l = 238 (VetoableConfigurationListener)vetoableListeners.get(i); 239 l.canChangeInterfaceSignature(this); 240 } 241 this.signature = signature; 242 List listeners = owner.getOwner().getListeners(); 243 for (int i = 0; i < listeners.size(); ++i) { 244 ConfigurationListener l = (ConfigurationListener)listeners.get(i); 245 l.interfaceSignatureChanged(this, oldSignature); 246 } 247 } 248 } 249 } 250 251 public boolean isInternal () { 252 return isInternal; 253 } 254 255 public boolean isOptional () { 256 return isInternal 257 ? complementaryItf.isOptional() 258 : masterCollectionItf != null 259 ? masterCollectionItf.isOptional() 260 : isOptional; 261 } 262 263 public void setIsOptional (final boolean isOptional) { 264 if (isInternal) { 265 complementaryItf.setIsOptional(isOptional); 266 } else if (masterCollectionItf != null) { 267 masterCollectionItf.setIsOptional(isOptional); 268 } else { 269 boolean oldIsOptional = this.isOptional; 270 if (isOptional != oldIsOptional) { 271 List vetoableListeners = owner.getOwner().getVetoableListeners(); 272 for (int i = 0; i < vetoableListeners.size(); ++i) { 273 VetoableConfigurationListener l = 274 (VetoableConfigurationListener)vetoableListeners.get(i); 275 l.canChangeInterfaceContingency(this); 276 } 277 this.isOptional = isOptional; 278 List listeners = owner.getOwner().getListeners(); 279 for (int i = 0; i < listeners.size(); ++i) { 280 ConfigurationListener l = (ConfigurationListener)listeners.get(i); 281 l.interfaceContingencyChanged(this, oldIsOptional); 282 } 283 } 284 } 285 } 286 287 public boolean isCollection () { 288 return isInternal ? complementaryItf.isCollection() : isCollection; 289 } 290 291 292 public void setIsCollection (final boolean isCollection) { 294 boolean faut_raler = false; 296 if (isCollection) { 297 if (this instanceof ClientInterface) { 298 ClientInterface citf = (ClientInterface)this; 299 if (citf.getBinding() != null) { 300 faut_raler = true; 301 } 302 } 303 else if (this instanceof ServerInterface) { 304 ServerInterface sitf = (ServerInterface)this; 305 if (sitf.getBindings().size() > 0) { 306 faut_raler = true; 307 } 308 } 309 } 310 if (faut_raler) { 311 JOptionPane.showMessageDialog (null, 312 "Cannot set this interface as a collection interface."+ 313 "because, it's already bound to another.", "Error", 314 JOptionPane.ERROR_MESSAGE); 315 return; 316 } 317 318 if (isInternal) { 319 complementaryItf.setIsCollection(isCollection); 320 } else { 321 boolean oldIsCollection = this.isCollection; 322 if (isCollection != oldIsCollection) { 323 if (masterCollectionItf != null || 324 getSlaveCollectionInterfaces().size() > 0) 325 { 326 throw new IllegalOperationException( 327 "Cannot change the cardinality of a root collection interface. " + 328 "You must first remove all the interfaces of the collection"); 329 } 330 List vetoableListeners = owner.getOwner().getVetoableListeners(); 331 for (int i = 0; i < vetoableListeners.size(); ++i) { 332 VetoableConfigurationListener l = 333 (VetoableConfigurationListener)vetoableListeners.get(i); 334 l.canChangeInterfaceCardinality(this); 335 } 336 this.isCollection = isCollection; 337 List listeners = owner.getOwner().getListeners(); 338 for (int i = 0; i < listeners.size(); ++i) { 339 ConfigurationListener l = (ConfigurationListener)listeners.get(i); 340 l.interfaceCardinalityChanged(this, oldIsCollection); 341 } 342 } 343 } 344 } 345 346 public Interface getMasterCollectionInterface () { 347 return masterCollectionItf; 348 } 349 350 public List getSlaveCollectionInterfaces () { 351 return isInternal 352 ? complementaryItf.getSlaveCollectionInterfaces() 353 : masterCollectionItf != null 354 ? Collections.EMPTY_LIST 355 : Collections.unmodifiableList(slaveCollectionItfs); 356 } 357 358 364 365 void addSlaveCollectionInterface (final Interface slaveItf) { 366 if (slaveCollectionItfs == null) { 367 throw new RuntimeException ("Internal error"); 368 } 369 if (!slaveCollectionItfs.contains(slaveItf)) { 370 slaveCollectionItfs.add(slaveItf); 371 } 372 } 373 374 380 381 void removeSlaveCollectionInterface (final Interface slaveItf) { 382 if (slaveCollectionItfs == null) { 383 throw new RuntimeException ("Internal error"); 384 } 385 slaveCollectionItfs.remove(slaveItf); 386 } 387 388 public Interface getComplementaryInterface () { 389 return complementaryItf; 390 } 391 392 public Interface getMasterInterface () { 393 return null; 394 } 395 } 396 | Popular Tags |