KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > menu > control > MakeSourceAction


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.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 JavaDoc;
37 import java.net.URL JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.GregorianCalendar JavaDoc;
42 import java.util.Calendar JavaDoc;
43 import java.io.File JavaDoc;
44 import java.io.FileOutputStream JavaDoc;
45 import java.io.PrintWriter JavaDoc;
46 import java.io.OutputStreamWriter JavaDoc;
47 import java.lang.reflect.Method JavaDoc;
48
49 import javax.swing.ImageIcon JavaDoc;
50 import javax.swing.JFileChooser JavaDoc;
51 import javax.swing.JOptionPane JavaDoc;
52 import javax.swing.KeyStroke JavaDoc;
53
54 /**
55  * An action to build a source file for a component.
56  */

57
58 public class MakeSourceAction extends AbstractAction implements
59   BindingController
60 {
61   String JavaDoc LS = System.getProperty("line.separator");
62   String JavaDoc FS = System.getProperty("file.separator");
63   String JavaDoc UD = System.getProperty("user.dir");
64   String JavaDoc author = System.getProperty("user.name");
65   private int includemap = 0;
66   private int nomap = 0;
67
68   public final static String JavaDoc CONFIGURATION_BINDING = "configuration";
69
70   public final static String JavaDoc SELECTION_BINDING = "selection";
71
72   private Configuration configuration;
73
74   private Selection selection;
75
76   static final String JavaDoc [] mois = {
77     "Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.",
78     "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."};
79
80   /**
81    * Constructs a new {@link MakeSourceAction} component.
82    */

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 JavaDoc url = getClass().getResource(
89       "/org/objectweb/fractal/gui/resources/source.gif");
90     putValue(SMALL_ICON, new ImageIcon JavaDoc(url));
91     setEnabled(true);
92   }
93
94   // -------------------------------------------------------------------------
95
// Implementation of the BindingController interface
96
// -------------------------------------------------------------------------
97

98   public String JavaDoc[] listFc () {
99     return new String JavaDoc[] { CONFIGURATION_BINDING, SELECTION_BINDING };
100   }
101
102   public Object JavaDoc lookupFc (final String JavaDoc 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 JavaDoc clientItfName,
113     final Object JavaDoc 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 JavaDoc 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   // -------------------------------------------------------------------------
132
// Implementation of the ActionListener interface
133
// -------------------------------------------------------------------------
134

135   public void actionPerformed (final ActionEvent JavaDoc e) {
136     Object JavaDoc 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       /*if ((c.getStatus() & Component.TYPE_MISSING) != 0) {
149         message ("Type for '"+c.getName()+"' missing");
150         return;
151       }*/

152
153       HashMap JavaDoc himport = new HashMap JavaDoc ();
154       HashMap JavaDoc himplem = new HashMap JavaDoc ();
155       HashMap JavaDoc hbind = new HashMap JavaDoc ();
156       StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
157
158       List JavaDoc cli = c.getClientInterfaces();
159       List JavaDoc 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.getName());
169
himport.put(itf.getMasterInterface().getSignature(), itf);
170         } else {
171 // himport.put(itf.getSignature(), itf.getName());
172
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 // if (!putimplem(c.getType(), himplem)) { return; }
183
// putimplem(c.getType(), himplem, true);
184

185       String JavaDoc suffixe = ".java";
186       String JavaDoc classname = c.getImplementation();
187       classname = classname.substring(classname.lastIndexOf('.')+1);
188       JFileChooser JavaDoc fileChooser = new JFileChooser JavaDoc();
189       fileChooser.setCurrentDirectory(new File JavaDoc(UD));
190       fileChooser.setSelectedFile (new File JavaDoc(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 JavaDoc f = fileChooser.getSelectedFile();
198
199       String JavaDoc filename = f.getName();
200       String JavaDoc 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 JavaDoc fos = null;
211       PrintWriter JavaDoc outf = null;
212       try {
213         fos = new FileOutputStream JavaDoc (pathname);
214         outf = new PrintWriter JavaDoc (new OutputStreamWriter JavaDoc (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 JavaDoc grcl = new GregorianCalendar JavaDoc ();
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 JavaDoc 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         //-----------------------------------------------------
238
// imports et declaration de la classe
239
//-----------------------------------------------------
240

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 JavaDoc it=himport.keySet().iterator(); it.hasNext(); ) {
257           String JavaDoc key = (String JavaDoc)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 JavaDoc it=himplem.keySet().iterator(); it.hasNext(); ) {
269           String JavaDoc key = (String JavaDoc)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         //-----------------------------------------------------
280
// public final static String
281
//-----------------------------------------------------
282

283         for (Iterator JavaDoc it=himport.keySet().iterator(); it.hasNext(); ) {
284           String JavaDoc key = (String JavaDoc)it.next();
285 // String value = (String)himport.get(key);
286
// if (value.compareTo("z") == 0) continue;
287
Object JavaDoc value = himport.get(key);
288           if (!(value instanceof ClientInterface)) continue;
289
290           String JavaDoc 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 JavaDoc it=hbind.keySet().iterator(); it.hasNext(); ) {
298           String JavaDoc key = (String JavaDoc)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         //-----------------------------------------------------
308
// Contructeur
309
//-----------------------------------------------------
310

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 JavaDoc it = hbind.keySet().iterator(); it.hasNext(); ) {
317           String JavaDoc key = (String JavaDoc)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         //-----------------------------------------------------
326
// BindingController
327
//-----------------------------------------------------
328

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           // -----------
338
// listFc
339
// -----------
340

341           String JavaDoc debut = "";
342           sb.append (" public String[] listFc () {"+LS);
343
344           // no map
345
if (includemap == 0) {
346             sb.append (" return new String[] { ");
347             if (hbind.size() > 1) sb.append(LS+" ");
348             for (Iterator JavaDoc it = hbind.keySet().iterator(); it.hasNext(); ) {
349               String JavaDoc key = (String JavaDoc)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 JavaDoc it = hbind.keySet().iterator(); it.hasNext(); ) {
360               String JavaDoc key = (String JavaDoc)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 JavaDoc it = hbind.keySet().iterator(); it.hasNext(); ) {
372               String JavaDoc key = (String JavaDoc)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 /* if (includemap > 1) */ {
381             int nb = 0;
382             for (Iterator JavaDoc it = hbind.keySet().iterator(); it.hasNext(); ) {
383               String JavaDoc key = (String JavaDoc)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 JavaDoc it = hbind.keySet().iterator(); it.hasNext(); ) {
408               String JavaDoc key = (String JavaDoc)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           // -----------
418
// lookupFc
419
// -----------
420

421           sb.append
422             (" public Object lookupFc (final String clientItfName) {"+LS);
423           debut = " ";
424           for (Iterator JavaDoc it = hbind.keySet().iterator(); it.hasNext(); ) {
425             String JavaDoc key = (String JavaDoc)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           // -----------
446
// bindFc
447
// -----------
448

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 JavaDoc it = hbind.keySet().iterator(); it.hasNext(); ) {
455             String JavaDoc key = (String JavaDoc)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           // -----------
476
// unbindFc
477
// -----------
478

479           sb.append (" public void unbindFc ("+LS);
480           sb.append (" final String clientItfName)"+LS);
481           sb.append (" {"+LS);
482           debut = " ";
483           for (Iterator JavaDoc it = hbind.keySet().iterator(); it.hasNext(); ) {
484             String JavaDoc key = (String JavaDoc)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         //-----------------------------------------------------
504
// Implementations (as far as possible)
505
//-----------------------------------------------------
506

507         ClassLoader JavaDoc cl = this.getClass().getClassLoader();
508
509         for (Iterator JavaDoc it=himplem.keySet().iterator(); it.hasNext(); ) {
510           String JavaDoc key = (String JavaDoc)it.next();
511           String JavaDoc value = (String JavaDoc)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 JavaDoc cla = cl.loadClass(value);
523
524             Method JavaDoc[] mtd = cla.getDeclaredMethods();
525             for (int i = 0; i < mtd.length; i++) {
526               Class JavaDoc[] 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 JavaDoc ex) {
552             sb.append (" // --- no class found !"+LS);
553           }
554         }
555
556
557         //-----------------------------------------------------
558
// end of file
559
//-----------------------------------------------------
560

561         outf.print(sb);
562         sb = null;
563         outf.println("}");
564         outf.close();
565       }
566       catch (Exception JavaDoc ex) { }
567
568     }
569   }
570
571   private String JavaDoc lastFieldOf (String JavaDoc 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 JavaDoc nkey, HashMap JavaDoc hm, boolean force)
578   {
579     String JavaDoc 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 JavaDoc motif) {
589     JOptionPane.showMessageDialog(null, motif, "Sorry ...",
590       JOptionPane.ERROR_MESSAGE);
591   }
592 }
593
Popular Tags