1 23 24 package org.objectweb.fractal.gui.menu.control; 25 26 import org.objectweb.fractal.api.control.BindingController; 27 28 import org.objectweb.fractal.gui.model.Component; 29 import org.objectweb.fractal.gui.model.Configuration; 30 import org.objectweb.fractal.gui.model.ClientInterface; 31 import org.objectweb.fractal.gui.model.ServerInterface; 32 import org.objectweb.fractal.gui.selection.model.Selection; 33 import org.objectweb.fractal.swing.AbstractAction; 34 import org.objectweb.fractal.gui.menu.control.SimpleFileFilter; 35 36 import java.awt.event.ActionEvent ; 37 import java.net.URL ; 38 import java.util.List ; 39 import java.util.HashMap ; 40 import java.util.Iterator ; 41 import java.util.GregorianCalendar ; 42 import java.util.Calendar ; 43 import java.io.File ; 44 import java.io.FileOutputStream ; 45 import java.io.PrintWriter ; 46 import java.io.OutputStreamWriter ; 47 import java.lang.reflect.Method ; 48 49 import javax.swing.ImageIcon ; 50 import javax.swing.JFileChooser ; 51 import javax.swing.JOptionPane ; 52 import javax.swing.KeyStroke ; 53 54 57 58 public class MakeSourceAction extends AbstractAction implements 59 BindingController 60 { 61 String LS = System.getProperty("line.separator"); 62 String FS = System.getProperty("file.separator"); 63 String UD = System.getProperty("user.dir"); 64 String author = System.getProperty("user.name"); 65 private int includemap = 0; 66 private int nomap = 0; 67 68 public final static String CONFIGURATION_BINDING = "configuration"; 69 70 public final static String SELECTION_BINDING = "selection"; 71 72 private Configuration configuration; 73 74 private Selection selection; 75 76 static final String [] mois = { 77 "Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.", 78 "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."}; 79 80 83 84 public MakeSourceAction () { 85 putValue(NAME, "Make source"); 86 putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("alt M")); 87 putValue(SHORT_DESCRIPTION, "Make source"); 88 URL url = getClass().getResource( 89 "/org/objectweb/fractal/gui/resources/source.gif"); 90 putValue(SMALL_ICON, new ImageIcon (url)); 91 setEnabled(true); 92 } 93 94 98 public String [] listFc () { 99 return new String [] { CONFIGURATION_BINDING, SELECTION_BINDING }; 100 } 101 102 public Object lookupFc (final String clientItfName) { 103 if (CONFIGURATION_BINDING.equals(clientItfName)) { 104 return configuration; 105 } else if (SELECTION_BINDING.equals(clientItfName)) { 106 return selection; 107 } 108 return null; 109 } 110 111 public void bindFc ( 112 final String clientItfName, 113 final Object serverItf) 114 { 115 if (CONFIGURATION_BINDING.equals(clientItfName)) { 116 configuration = (Configuration)serverItf; 117 } else if (SELECTION_BINDING.equals(clientItfName)) { 118 selection = (Selection)serverItf; 119 } 120 } 121 122 public void unbindFc (final String clientItfName) { 123 if (CONFIGURATION_BINDING.equals(clientItfName)) { 124 configuration = null; 125 } else if (SELECTION_BINDING.equals(clientItfName)) { 126 selection = null; 127 } 128 } 129 130 131 135 public void actionPerformed (final ActionEvent e) { 136 Object o = selection.getSelection(); 137 if (o instanceof Component) { 138 Component c = (Component)o; 139 140 if (c.isComposite()) { 141 message ("'"+c.getName()+"' is a composite"); 142 return; 143 } 144 if (c.getStatus() == Component.OK) { 145 message ("Implementation of '"+c.getName()+"' is already a valid class "); 146 return; 147 } 148 152 153 HashMap himport = new HashMap (); 154 HashMap himplem = new HashMap (); 155 HashMap hbind = new HashMap (); 156 StringBuffer sb = new StringBuffer (); 157 158 List cli = c.getClientInterfaces(); 159 List ser = c.getServerInterfaces(); 160 for (int j = 0; j < cli.size(); ++j) { 161 ClientInterface itf = (ClientInterface)cli.get(j); 162 if (itf.isCollection()) { 163 includemap++; 164 } else { 165 nomap++; 166 } 167 if (itf.getMasterInterface() != null) { 168 himport.put(itf.getMasterInterface().getSignature(), itf); 170 } else { 171 himport.put(itf.getSignature(), itf); 173 } 174 } 175 for (int j = 0; j < ser.size(); ++j) { 176 ServerInterface itf = (ServerInterface)ser.get(j); 177 himport.put(itf.getSignature(), "z"); 178 if (!putimplem (itf.getSignature(), himplem, false)) { 179 return; 180 } 181 } 182 185 String suffixe = ".java"; 186 String classname = c.getImplementation(); 187 classname = classname.substring(classname.lastIndexOf('.')+1); 188 JFileChooser fileChooser = new JFileChooser (); 189 fileChooser.setCurrentDirectory(new File (UD)); 190 fileChooser.setSelectedFile (new File (classname+suffixe)); 191 192 fileChooser.addChoosableFileFilter( 193 new SimpleFileFilter("java", "Java source files")); 194 if (fileChooser.showSaveDialog(null) != JFileChooser.APPROVE_OPTION) { 195 return; 196 } 197 File f = fileChooser.getSelectedFile(); 198 199 String filename = f.getName(); 200 String pathname = f.getPath(); 201 if (filename.endsWith(suffixe)) { 202 classname = filename.substring(0, filename.lastIndexOf(suffixe)); 203 } 204 else { 205 classname = filename; 206 filename = filename.concat(suffixe); 207 pathname = pathname.concat(suffixe); 208 } 209 210 FileOutputStream fos = null; 211 PrintWriter outf = null; 212 try { 213 fos = new FileOutputStream (pathname); 214 outf = new PrintWriter (new OutputStreamWriter (fos)); 215 216 outf.println 217 ("/***"+LS+" * This skeleton has been generated automacally " 218 +"by FractalGUI"); 219 outf.println (" * You can complete it as you like"); 220 outf.println (" *"); 221 outf.println (" * Author: "+author); 222 GregorianCalendar grcl = new GregorianCalendar (); 223 outf.println (" * Date: "+mois[grcl.get(Calendar.MONTH)]+", " 224 +grcl.get(Calendar.DAY_OF_MONTH)+"th "+grcl.get(Calendar.YEAR) 225 +" at "+grcl.get(Calendar.HOUR_OF_DAY)+"H"+grcl.get(Calendar.MINUTE)); 226 outf.println (" *"); 227 outf.println (" */"+LS); 228 229 230 String impl = c.getImplementation(); 231 if (impl.lastIndexOf('.') > 1) { 232 outf.println 233 ("package "+impl.substring(0, impl.lastIndexOf('.'))+";"+LS); 234 impl = impl.substring(0, impl.lastIndexOf('.')); 235 } 236 237 241 if (cli.size() > 0) { 242 outf.println 243 ("import org.objectweb.fractal.api.control.BindingController;"); 244 himplem.put("BindingController", "z"); 245 } 246 if (c.getAttributeController().length() > 0) { 247 outf.println 248 ("import "+c.getAttributeController()+";"); 249 himplem.put(c.getAttributeController(), "z"); 250 } 251 if (includemap > 0) { 252 outf.println ("import java.util.Map;"); 253 outf.println ("import java.util.HashMap;"); 254 } 255 256 for (Iterator it=himport.keySet().iterator(); it.hasNext(); ) { 257 String key = (String )it.next(); 258 if (key.lastIndexOf('.') > 1) { 259 if (impl.equals(key.substring(0, key.lastIndexOf('.')))) continue; 260 } 261 sb.append("import "+key+";"+LS); 262 } 263 264 sb.append (LS+LS); 265 sb.append ("public class "+classname); 266 sb.append (" implements"+LS); 267 268 for (Iterator it=himplem.keySet().iterator(); it.hasNext(); ) { 269 String key = (String )it.next(); 270 sb.append (" "+key); 271 if (it.hasNext()) { 272 sb.append (","+LS); 273 } else { 274 sb.append (LS); 275 } 276 } 277 sb.append ("{"+LS+LS); 278 279 283 for (Iterator it=himport.keySet().iterator(); it.hasNext(); ) { 284 String key = (String )it.next(); 285 Object value = himport.get(key); 288 if (!(value instanceof ClientInterface)) continue; 289 290 String mkey = key.substring(key.lastIndexOf('.')+1); 291 sb.append(" public final static String " 292 +mkey.toUpperCase()+"_BINDING = \"" 293 +mkey.toLowerCase()+"\";"+LS+LS); 294 hbind.put(mkey, value); 295 } 296 297 for (Iterator it=hbind.keySet().iterator(); it.hasNext(); ) { 298 String key = (String )it.next(); 299 ClientInterface clif = (ClientInterface)hbind.get(key); 300 if (clif.isCollection()) { 301 sb.append (" private Map "+key.toLowerCase()+";"+LS+LS); 302 } else { 303 sb.append (" private "+key+" "+key.toLowerCase()+";"+LS+LS); 304 } 305 } 306 307 311 sb.append (" /**"+LS); 312 sb.append (" * Constructs a new "+classname+LS); 313 sb.append (" */ "+LS); 314 sb.append (" public "+classname); 315 sb.append (" () {"+LS); 316 for (Iterator it = hbind.keySet().iterator(); it.hasNext(); ) { 317 String key = (String )it.next(); 318 ClientInterface clif = (ClientInterface)hbind.get(key); 319 if (clif.isCollection()) { 320 sb.append (" "+key.toLowerCase()+" = new HashMap();"+LS); 321 } 322 } 323 sb.append (" // ..."+LS+" }"+LS+LS); 324 325 329 if (cli.size() > 0) { 330 sb.append 331 (" // -----------------------------------------------------"+LS); 332 sb.append 333 (" // Implementation of the BindingController interface"+LS); 334 sb.append 335 (" // -----------------------------------------------------"+LS+LS); 336 337 341 String debut = ""; 342 sb.append (" public String[] listFc () {"+LS); 343 344 if (includemap == 0) { 346 sb.append (" return new String[] { "); 347 if (hbind.size() > 1) sb.append(LS+" "); 348 for (Iterator it = hbind.keySet().iterator(); it.hasNext(); ) { 349 String key = (String )it.next(); 350 sb.append (debut+key.toUpperCase()+"_BINDING"); 351 if (it.hasNext()) { 352 sb.append (","+LS); 353 debut = " "; 354 } else sb.append (" };"+LS); 355 } 356 } 357 358 else if (includemap == 1) { 359 for (Iterator it = hbind.keySet().iterator(); it.hasNext(); ) { 360 String key = (String )it.next(); 361 ClientInterface clif = (ClientInterface)hbind.get(key); 362 if (clif.isCollection()) { 363 sb.append (" int size = "+key.toLowerCase()+".size ();"+LS); 364 sb.append (" String[] names = new String[size+"+nomap+"];"+LS); 365 sb.append (" "+key.toLowerCase() 366 +".keySet().toArray(names);"+LS); 367 break; 368 } 369 } 370 int nb = 0; 371 for (Iterator it = hbind.keySet().iterator(); it.hasNext(); ) { 372 String key = (String )it.next(); 373 ClientInterface clif = (ClientInterface)hbind.get(key); 374 if (clif.isCollection()) continue; 375 sb.append(" names[size+"+nb+"] = "+key.toUpperCase()+"_BINDING;"+LS); 376 nb++; 377 } 378 } 379 380 else { 381 int nb = 0; 382 for (Iterator it = hbind.keySet().iterator(); it.hasNext(); ) { 383 String key = (String )it.next(); 384 ClientInterface clif = (ClientInterface)hbind.get(key); 385 if (clif.isCollection()) { 386 sb.append (" int sz"+nb+" = "+key.toLowerCase()+".size ();"+LS); 387 sb.append (" String [] names" 388 +nb+" = new String[sz"+nb+"];"+LS); 389 sb.append (" "+key.toLowerCase() 390 +".keySet().toArray(names"+nb+");"+LS+LS); 391 nb++; 392 } 393 } 394 sb.append (" int size = "); 395 for (int i = 0; i < nb; i++) { 396 if (i > 0) sb.append(" + sz"+i); 397 else sb.append("sz"+i); 398 } 399 sb.append(" + "+nomap+";"+LS); 400 sb.append(" String [] names = new String[size];"+LS); 401 sb.append(" int d = 0;"+LS); 402 for (int i = 0; i < nb; i++) { 403 sb.append (" for (int i = 0; i < sz"+i+"; i++) {"+LS); 404 sb.append (" names[d++] = names"+i+"[i];"+LS); 405 sb.append (" }"+LS); 406 } 407 for (Iterator it = hbind.keySet().iterator(); it.hasNext(); ) { 408 String key = (String )it.next(); 409 ClientInterface clif = (ClientInterface)hbind.get(key); 410 if (clif.isCollection()) continue; 411 sb.append(" names[d++] = "+key.toUpperCase()+"_BINDING;"+LS); 412 nb++; 413 } 414 } 415 sb.append (" return names;"+LS+" }"+LS+LS); 416 417 421 sb.append 422 (" public Object lookupFc (final String clientItfName) {"+LS); 423 debut = " "; 424 for (Iterator it = hbind.keySet().iterator(); it.hasNext(); ) { 425 String key = (String )it.next(); 426 ClientInterface clif = (ClientInterface)hbind.get(key); 427 if (clif.isCollection()) { 428 sb.append (debut+"if (clientItfName.startsWith (" 429 +key.toUpperCase()+"_BINDING)) {"+LS); 430 sb.append (" return "+key.toLowerCase()+ 431 ".get(clientItfName);"+LS); 432 } else { 433 sb.append (debut+"if ("+key.toUpperCase() 434 +"_BINDING.equals(clientItfName)) {"+LS); 435 sb.append (" return "+key.toLowerCase()+";"+LS); 436 } 437 if (it.hasNext()) { 438 sb.append (" }"); debut = " else "; 439 } 440 } 441 sb.append (" }"+LS); 442 sb.append (" return null;"+LS); 443 sb.append (" }"+LS+LS); 444 445 449 sb.append (" public void bindFc ("+LS); 450 sb.append (" final String clientItfName,"+LS); 451 sb.append (" final Object serverItf)"+LS); 452 sb.append (" {"+LS); 453 debut = " "; 454 for (Iterator it = hbind.keySet().iterator(); it.hasNext(); ) { 455 String key = (String )it.next(); 456 ClientInterface clif = (ClientInterface)hbind.get(key); 457 if (clif.isCollection()) { 458 sb.append (debut+"if (clientItfName.startsWith (" 459 +key.toUpperCase()+"_BINDING)) {"+LS); 460 sb.append (" "+key.toLowerCase()+".put(clientItfName, " 461 +"serverItf);"+LS); 462 } else { 463 sb.append (debut+"if ("+key.toUpperCase() 464 +"_BINDING.equals(clientItfName)) {"+LS); 465 sb.append (" "+key.toLowerCase()+" = " 466 +"("+key+")serverItf;"+LS); 467 } 468 if (it.hasNext()) { 469 sb.append (" }"); debut = " else "; 470 } 471 } 472 sb.append (" }"+LS); 473 sb.append (" }"+LS+LS); 474 475 479 sb.append (" public void unbindFc ("+LS); 480 sb.append (" final String clientItfName)"+LS); 481 sb.append (" {"+LS); 482 debut = " "; 483 for (Iterator it = hbind.keySet().iterator(); it.hasNext(); ) { 484 String key = (String )it.next(); 485 ClientInterface clif = (ClientInterface)hbind.get(key); 486 if (clif.isCollection()) { 487 sb.append (debut+"if (clientItfName.startsWith (" 488 +key.toUpperCase()+"_BINDING)) {"+LS); 489 sb.append (" "+key.toLowerCase()+".remove(clientItfName);"+LS); 490 } else { 491 sb.append (debut+"if ("+key.toUpperCase() 492 +"_BINDING.equals(clientItfName)) {"+LS); 493 sb.append (" "+key.toLowerCase()+" = null;"+LS); 494 } 495 if (it.hasNext()) { 496 sb.append (" }"); debut = " else "; 497 } 498 } 499 sb.append (" }"+LS); 500 sb.append (" }"+LS+LS); 501 } 502 503 507 ClassLoader cl = this.getClass().getClassLoader(); 508 509 for (Iterator it=himplem.keySet().iterator(); it.hasNext(); ) { 510 String key = (String )it.next(); 511 String value = (String )himplem.get(key); 512 if (value.compareTo("z") == 0) continue; 513 514 sb.append 515 (" // -----------------------------------------------------"+LS); 516 sb.append 517 (" // Implementation of the "+key+" interface"+LS); 518 sb.append 519 (" // -----------------------------------------------------"+LS+LS); 520 521 try { 522 Class cla = cl.loadClass(value); 523 524 Method [] mtd = cla.getDeclaredMethods(); 525 for (int i = 0; i < mtd.length; i++) { 526 Class [] param = mtd[i].getParameterTypes(); 527 sb.append (" public " 528 +lastFieldOf(mtd[i].getReturnType().getName()) 529 +" "+mtd[i].getName()+" ("); 530 531 if (param.length > 0) { 532 for (int j=0; j < param.length; j++) { 533 if (j > 0) sb.append (" "); 534 sb.append (lastFieldOf(param[j].getName())+" p"+j); 535 if (j < param.length-1) { 536 sb.append(","+LS); 537 } 538 if (param[j].toString().startsWith("class ")) { 539 if (himport.get(param[j].getName()) == null) { 540 himport.put(param[j], "z"); 541 outf.println("import "+param[j].getName()+";"); 542 } 543 } 544 } 545 } 546 sb.append (") {"+LS); 547 sb.append (" // add your own code here"+LS); 548 sb.append (" }"+LS+LS); 549 } 550 551 } catch (Exception ex) { 552 sb.append (" // --- no class found !"+LS); 553 } 554 } 555 556 557 561 outf.print(sb); 562 sb = null; 563 outf.println("}"); 564 outf.close(); 565 } 566 catch (Exception ex) { } 567 568 } 569 } 570 571 private String lastFieldOf (String st) { 572 int ind = st.lastIndexOf('.'); 573 if (ind < 0) return st; 574 else return st.substring(ind+1); 575 } 576 577 private boolean putimplem (String nkey, HashMap hm, boolean force) 578 { 579 String key = nkey.substring(nkey.lastIndexOf('.')+1); 580 if ((hm.containsKey(key)) && !force) { 581 message ("'"+key+" is ambiguous !"); 582 return false; 583 } 584 hm.put(key, nkey); 585 return true; 586 } 587 588 private void message (String motif) { 589 JOptionPane.showMessageDialog(null, motif, "Sorry ...", 590 JOptionPane.ERROR_MESSAGE); 591 } 592 } 593 | Popular Tags |