KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > tools > internal > JNIGeneratorAppUI


1 /*******************************************************************************
2  * Copyright (c) 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.tools.internal;
12
13 import java.io.*;
14 import java.lang.reflect.*;
15
16 import org.eclipse.swt.*;
17 import org.eclipse.swt.widgets.*;
18 import org.eclipse.swt.custom.*;
19 import org.eclipse.swt.graphics.*;
20 import org.eclipse.swt.layout.*;
21
22 public class JNIGeneratorAppUI {
23
24     Display display;
25     Shell shell;
26     
27     Composite actionsPanel;
28     Combo mainClassCb, outputDirCb;
29     Table classesLt, membersLt, paramsLt;
30     ProgressBar progressBar;
31     Label progressLabel;
32     FileDialog fileDialog;
33     
34     TableEditor paramTextEditor, memberTextEditor, classTextEditor;
35     FlagsEditor paramListEditor, memberListEditor, classListEditor;
36     Text paramEditorTx, memberEditorTx, classEditorTx;
37     List paramEditorLt, memberEditorLt, classEditorLt;
38     
39     static class FlagsEditor {
40         Table parent;
41         int column = -1;
42         TableItem item;
43         
44         public FlagsEditor(Table parent) {
45             this.parent = parent;
46         }
47         
48         public int getColumn() {
49             return column;
50         }
51         
52         public TableItem getItem() {
53             return item;
54         }
55         
56         public void setColumn(int column) {
57             this.column = column;
58         }
59         
60         public void setItem(TableItem item) {
61             this.item = item;
62         }
63     }
64     
65     JNIGeneratorApp app;
66
67     static final int CLASS_NAME_COLUMN = 0;
68     static final int CLASS_FLAGS_COLUMN = 1;
69     static final int CLASS_EXCLUDE_COLUMN = 2;
70     
71     static final int FIELD_NAME_COLUMN = 0;
72     static final int FIELD_FLAGS_COLUMN = 1;
73     static final int FIELD_CAST_COLUMN = 2;
74     static final int FIELD_ACCESSOR_COLUMN = 3;
75     static final int FIELD_EXCLUDE_COLUMN = 4;
76     
77     static final int METHOD_NAME_COLUMN = 0;
78     static final int METHOD_FLAGS_COLUMN = 1;
79     static final int METHOD_ACCESSOR_COLUMN = 2;
80     static final int METHOD_EXCLUDE_COLUMN = 3;
81     
82     static final int PARAM_INDEX_COLUMN = 0;
83     static final int PARAM_TYPE_COLUMN = 1;
84     static final int PARAM_FLAGS_COLUMN = 2;
85     static final int PARAM_CAST_COLUMN = 3;
86     
87 public JNIGeneratorAppUI() {
88     this (new JNIGeneratorApp());
89 }
90
91 public JNIGeneratorAppUI(JNIGeneratorApp app) {
92     this.app = app;
93 }
94
95 void cleanup() {
96     display.dispose();
97 }
98
99 void generateStructsHeader () {
100     StructsGenerator gen = new StructsGenerator(true);
101     gen.setMainClass(app.getMainClass());
102     gen.setMetaData(app.getMetaData());
103     gen.setClasses(getSelectedClasses());
104     gen.generate();
105 }
106
107 void generateStructs () {
108     StructsGenerator gen = new StructsGenerator(false);
109     gen.setMainClass(app.getMainClass());
110     gen.setMetaData(app.getMetaData());
111     gen.setClasses(getSelectedClasses());
112     gen.generate();
113 }
114
115 void generateSizeof () {
116     SizeofGenerator gen = new SizeofGenerator();
117     gen.setMainClass(app.getMainClass());
118     gen.setMetaData(app.getMetaData());
119     gen.setClasses(getSelectedClasses());
120     gen.generate();
121 }
122
123 void generateMetaData () {
124     MetaDataGenerator gen = new MetaDataGenerator();
125     gen.setMainClass(app.getMainClass());
126     gen.setMetaData(app.getMetaData());
127     Method[] methods = getSelectedMethods();
128     if (methods.length != 0) {
129         gen.generate(methods);
130     } else {
131         gen.setClasses(getSelectedClasses());
132         gen.generate();
133     }
134 }
135
136 void generateNatives () {
137     NativesGenerator gen = new NativesGenerator();
138     gen.setMainClass(app.getMainClass());
139     gen.setMetaData(app.getMetaData());
140     Method[] methods = getSelectedMethods();
141     if (methods.length != 0) {
142         gen.generate(methods);
143     } else {
144         gen.setClasses(getSelectedClasses());
145         gen.generate();
146     }
147 }
148
149 void generateAll() {
150     if (!updateOutputDir()) return;
151     Cursor cursor = display.getSystemCursor(SWT.CURSOR_WAIT);
152     shell.setCursor(cursor);
153     shell.setEnabled(false);
154     Control[] children = actionsPanel.getChildren();
155     for (int i = 0; i < children.length; i++) {
156         Control child = children[i];
157         if (child instanceof Button) child.setEnabled(false);
158     }
159     final boolean showProgress = true;
160     if (showProgress) {
161         progressLabel.setText("");
162         progressBar.setSelection(0);
163         progressLabel.setVisible(true);
164         progressBar.setVisible(true);
165     }
166     final boolean[] done = new boolean[1];
167     new Thread JavaDoc() {
168         public void run() {
169             try {
170                 app.generate(!showProgress ? null : new ProgressMonitor() {
171                     public void setTotal(final int total) {
172                         display.syncExec(new Runnable JavaDoc() {
173                             public void run() {
174                                 progressBar.setMaximum(total);
175                             }
176                         });
177                     }
178                     public void step() {
179                         display.syncExec(new Runnable JavaDoc() {
180                             public void run() {
181                                 progressBar.setSelection(progressBar.getSelection() + 1);
182                             }
183                         });
184                     }
185                     public void setMessage(final String JavaDoc message) {
186                         display.syncExec(new Runnable JavaDoc() {
187                             public void run() {
188                                 progressLabel.setText(message);
189                                 progressLabel.update();
190                             }
191                         });
192                     }
193                 });
194             } finally {
195                 done[0] = true;
196                 display.wake();
197             }
198         }
199     }.start();
200     while (!done[0]) {
201         if (!display.readAndDispatch()) display.sleep();
202     }
203     for (int i = 0; i < children.length; i++) {
204         Control child = children[i];
205         if (child instanceof Button) child.setEnabled(true);
206     }
207     if (showProgress) {
208         progressBar.setVisible(false);
209         progressLabel.setVisible(false);
210     }
211     shell.setEnabled(true);
212     shell.setCursor(null);
213 }
214
215 void generateConstants () {
216     ConstantsGenerator gen = new ConstantsGenerator();
217     gen.setMainClass(app.getMainClass());
218     gen.setMetaData(app.getMetaData());
219     Field[] fields = getSelectedFields();
220     if (fields.length != 0) {
221         gen.generate(fields);
222     } else {
223         gen.setClasses(getSelectedClasses());
224         gen.generate();
225     }
226 }
227
228 Class JavaDoc[] getSelectedClasses() {
229     TableItem[] items = classesLt.getSelection();
230     Class JavaDoc[] classes = new Class JavaDoc[items.length];
231     for (int i = 0; i < items.length; i++) {
232         TableItem item = items[i];
233         classes[i] = ((ClassData)item.getData()).getClazz();
234     }
235     return classes;
236 }
237
238 Method[] getSelectedMethods() {
239     TableItem[] selection = membersLt.getSelection();
240     Method[] methods = new Method[selection.length];
241     int count = 0;
242     for (int i = 0; i < selection.length; i++) {
243         TableItem item = selection [i];
244         Object JavaDoc data = item.getData();
245         if (data instanceof MethodData) {
246             Method method = ((MethodData)data).getMethod();
247             methods[count++] = method;
248         }
249     }
250     if (count != methods.length) {
251         Method[] result = new Method[count];
252         System.arraycopy(methods, 0, result, 0, count);
253         methods = result;
254     }
255     return methods;
256 }
257
258 Field[] getSelectedFields() {
259     TableItem[] selection = membersLt.getSelection();
260     Field[] fields = new Field[selection.length];
261     int count = 0;
262     for (int i = 0; i < selection.length; i++) {
263         TableItem item = selection [i];
264         Object JavaDoc data = item.getData();
265         if (data instanceof FieldData) {
266             Field field = ((FieldData)data).getField();
267             fields[count++] = field;
268         }
269     }
270     if (count != fields.length) {
271         Field[] result = new Field[count];
272         System.arraycopy(fields, 0, result, 0, count);
273         fields = result;
274     }
275     return fields;
276 }
277
278 public void open () {
279     display = new Display();
280     shell = new Shell(display);
281     shell.setText("JNI Generator");
282
283     GridData data;
284     GridLayout shellLayout = new GridLayout();
285     shellLayout.numColumns = 2;
286     shell.setLayout(shellLayout);
287     
288     Composite panel = new Composite(shell, SWT.NONE);
289     data = new GridData(GridData.FILL_BOTH);
290     panel.setLayoutData(data);
291     
292     GridLayout panelLayout = new GridLayout();
293     panelLayout.numColumns = 1;
294     panel.setLayout(panelLayout);
295     
296     Listener updateMainClassListener = new Listener() {
297         public void handleEvent(Event e) {
298             updateMainClass();
299             if (!updateOutputDir()) return;
300             updateClasses();
301             updateMembers();
302             updateParameters();
303         }
304     };
305     createMainClassPanel(panel, updateMainClassListener);
306     createClassesPanel(panel);
307     createMembersPanel(panel);
308     createParametersPanel(panel);
309     createActionButtons(shell);
310
311     Point preferredSize = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT);
312     shell.setSize(shell.getSize().x, preferredSize.y);
313     
314     updateMainClass();
315     updateClasses();
316     updateMembers();
317     updateParameters();
318 }
319
320 void createMainClassPanel(Composite panel, Listener updateListener) {
321     Label mainClassLb = new Label(panel, SWT.NONE);
322     mainClassLb.setText("&Main Class:");
323
324     GridData data;
325     mainClassCb = new Combo(panel, SWT.DROP_DOWN);
326     String JavaDoc mainClass = app.getMainClassName();
327     mainClassCb.setText(mainClass == null ? "" : mainClass);
328     data = new GridData(GridData.FILL_HORIZONTAL);
329     mainClassCb.setLayoutData(data);
330     mainClassCb.addListener(SWT.Selection, updateListener);
331     mainClassCb.addListener(SWT.DefaultSelection, updateListener);
332
333     Label outputDirLb = new Label(panel, SWT.NONE);
334     outputDirLb.setText("&Output Dir:");
335     
336     outputDirCb = new Combo(panel, SWT.DROP_DOWN);
337     String JavaDoc outputDir = app.getOutputDir();
338     outputDirCb.setText(outputDir == null ? "" : outputDir);
339     data = new GridData(GridData.FILL_HORIZONTAL);
340     outputDirCb.setLayoutData(data);
341     outputDirCb.addListener(SWT.Selection, updateListener);
342     outputDirCb.addListener(SWT.DefaultSelection, updateListener);
343
344     String JavaDoc mainClasses = app.getMetaData().getMetaData("swt_main_classes", null);
345     if (mainClasses != null) {
346         String JavaDoc[] list = ItemData.split(mainClasses, ",");
347         for (int i = 0; i < list.length; i += 2) {
348             mainClassCb.add(list[i].trim());
349             outputDirCb.add(list[i + 1].trim());
350         }
351     }
352 }
353
354 void createClassesPanel(Composite panel) {
355     Label classesLb = new Label(panel, SWT.NONE);
356     classesLb.setText("&Classes:");
357
358     GridData data;
359     classesLt = new Table(panel, SWT.CHECK | SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
360     data = new GridData(GridData.FILL_BOTH);
361     data.heightHint = classesLt.getItemHeight() * 6;
362     classesLt.setLayoutData(data);
363     classesLt.setHeaderVisible(true);
364     classesLt.addListener(SWT.Selection, new Listener() {
365         public void handleEvent(Event e) {
366             if (e.detail == SWT.CHECK) {
367                 updateGenerate((TableItem)e.item);
368             } else {
369                 updateMembers();
370                 updateParameters();
371             }
372         }
373     });
374     TableColumn column;
375     column = new TableColumn(classesLt, SWT.NONE, CLASS_NAME_COLUMN);
376     column.setText("Class");
377     column = new TableColumn(classesLt, SWT.NONE, CLASS_FLAGS_COLUMN);
378     column.setText("Flags");
379     /*
380     column = new TableColumn(classesLt, SWT.NONE, CLASS_EXCLUDE_COLUMN);
381     column.setText("Exclude");
382     */

383     
384     classTextEditor = new TableEditor(classesLt);
385     classTextEditor.grabHorizontal = true;
386     classEditorTx = new Text(classesLt, SWT.SINGLE);
387     classTextEditor.setEditor(classEditorTx);
388     Listener classTextListener = new Listener() {
389         public void handleEvent(Event e) {
390             if (e.type == SWT.Traverse) {
391                 switch (e.detail) {
392                     case SWT.TRAVERSE_ESCAPE:
393                         classTextEditor.setItem(null);
394                         break;
395                     default:
396                         return;
397                 }
398             }
399             classEditorTx.setVisible(false);
400             TableItem item = classTextEditor.getItem();
401             if (item == null) return;
402             int column = classTextEditor.getColumn();
403             ClassData classData = (ClassData)item.getData();
404             if (column == CLASS_EXCLUDE_COLUMN) {
405                 String JavaDoc text = classEditorTx.getText();
406                 classData.setExclude(text);
407                 item.setText(column, classData.getExclude());
408                 MetaData metaData = app.getMetaData();
409                 metaData.setMetaData(classData.getClazz(), classData);
410                 classesLt.getColumn(column).pack();
411             }
412         }
413     };
414     classEditorTx.addListener(SWT.DefaultSelection, classTextListener);
415     classEditorTx.addListener(SWT.FocusOut, classTextListener);
416     classEditorTx.addListener(SWT.Traverse, classTextListener);
417     
418     final Shell floater = new Shell(shell, SWT.NO_TRIM);
419     floater.setLayout(new FillLayout());
420     classListEditor = new FlagsEditor(classesLt);
421     classEditorLt = new List(floater, SWT.MULTI | SWT.BORDER);
422     classEditorLt.setItems(ClassData.getAllFlags());
423     floater.pack();
424     floater.addListener(SWT.Close, new Listener() {
425         public void handleEvent(Event e) {
426             classListEditor.setItem(null);
427             e.doit = false;
428             floater.setVisible(false);
429         }
430     });
431     Listener classesListListener = new Listener() {
432         public void handleEvent(Event e) {
433             if (e.type == SWT.Traverse) {
434                 switch (e.detail) {
435                     case SWT.TRAVERSE_RETURN:
436                         break;
437                     default:
438                         return;
439                 }
440             }
441             floater.setVisible(false);
442             TableItem item = classListEditor.getItem();
443             if (item == null) return;
444             int column = classListEditor.getColumn();
445             ClassData classData = (ClassData)item.getData();
446             if (column == CLASS_FLAGS_COLUMN) {
447                 String JavaDoc[] flags = classEditorLt.getSelection();
448                 classData.setFlags(flags);
449                 item.setText(column, getFlagsString(classData.getFlags()));
450                 item.setChecked(classData.getGenerate());
451                 MetaData metaData = app.getMetaData();
452                 metaData.setMetaData(classData.getClazz(), classData);
453                 classesLt.getColumn(column).pack();
454             }
455         }
456     };
457     classEditorLt.addListener(SWT.DefaultSelection, classesListListener);
458     classEditorLt.addListener(SWT.FocusOut, classesListListener);
459     classEditorLt.addListener(SWT.Traverse, classesListListener);
460
461     classesLt.addListener(SWT.MouseDown, new Listener() {
462         public void handleEvent(Event e) {
463             if (e.button != 1) return;
464             Point pt = new Point(e.x, e.y);
465             TableItem item = classesLt.getItem(pt);
466             if (item == null) return;
467             int column = -1;
468             for (int i = 0; i < classesLt.getColumnCount(); i++) {
469                 if (item.getBounds(i).contains(pt)) {
470                     column = i;
471                     break;
472                 }
473             }
474             if (column == -1) return;
475             ClassData data = (ClassData)item.getData();
476             if (column == CLASS_EXCLUDE_COLUMN) {
477                 classTextEditor.setColumn(column);
478                 classTextEditor.setItem(item);
479                 classEditorTx.setText(data.getExclude());
480                 classEditorTx.selectAll();
481                 classEditorTx.setVisible(true);
482                 classEditorTx.setFocus();
483             } else if (column == CLASS_FLAGS_COLUMN) {
484                 classListEditor.setColumn(column);
485                 classListEditor.setItem(item);
486                 classEditorLt.setSelection(data.getFlags());
487                 floater.setLocation(classesLt.toDisplay(e.x, e.y));
488                 floater.setVisible(true);
489                 classEditorLt.setFocus();
490             }
491         }
492     });
493 }
494
495 void createMembersPanel(Composite panel) {
496     Label membersLb = new Label(panel, SWT.NONE);
497     membersLb.setText("Mem&bers:");
498
499     GridData data;
500     membersLt = new Table(panel, SWT.CHECK | SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
501     data = new GridData(GridData.FILL_BOTH);
502     data.heightHint = membersLt.getItemHeight() * 6;
503     membersLt.setLayoutData(data);
504     membersLt.addListener(SWT.Selection, new Listener() {
505         public void handleEvent(Event e) {
506             if (e.detail == SWT.CHECK) {
507                 updateGenerate((TableItem)e.item);
508             } else {
509                 updateParameters();
510             }
511         }
512     });
513     
514     memberTextEditor = new TableEditor(membersLt);
515     memberTextEditor.grabHorizontal = true;
516     memberEditorTx = new Text(membersLt, SWT.SINGLE);
517     memberTextEditor.setEditor(memberEditorTx);
518     Listener memberTextListener = new Listener() {
519         public void handleEvent(Event e) {
520             if (e.type == SWT.Traverse) {
521                 switch (e.detail) {
522                     case SWT.TRAVERSE_ESCAPE:
523                         memberTextEditor.setItem(null);
524                         break;
525                     default:
526                         return;
527                 }
528             }
529             memberEditorTx.setVisible(false);
530             TableItem item = memberTextEditor.getItem();
531             if (item == null) return;
532             int column = memberTextEditor.getColumn();
533             ItemData memberData = (ItemData)item.getData();
534             String JavaDoc text = memberEditorTx.getText();
535             MetaData metaData = app.getMetaData();
536             if (memberData instanceof FieldData) {
537                 FieldData fieldData = (FieldData)memberData;
538                 switch (column) {
539                     case FIELD_CAST_COLUMN: {
540                         fieldData.setCast(text);
541                         item.setText(column, fieldData.getCast());
542                         break;
543                     }
544                     case FIELD_ACCESSOR_COLUMN: {
545                         fieldData.setAccessor(text);
546                         item.setText(column, fieldData.getAccessor());
547                         break;
548                     }
549                     case FIELD_EXCLUDE_COLUMN: {
550                         fieldData.setExclude(text);
551                         item.setText(column, fieldData.getExclude());
552                         break;
553                     }
554                 }
555                 metaData.setMetaData(fieldData.getField(), fieldData);
556                 membersLt.getColumn(column).pack();
557             } else if (memberData instanceof MethodData) {
558                 MethodData methodData = (MethodData)memberData;
559                 switch (column) {
560                     case METHOD_ACCESSOR_COLUMN: {
561                         methodData.setAccessor(text);
562                         item.setText(column, methodData.getAccessor());
563                         break;
564                     }
565                     case METHOD_EXCLUDE_COLUMN: {
566                         methodData.setExclude(text);
567                         item.setText(column, methodData.getExclude());
568                         break;
569                     }
570                 }
571                 metaData.setMetaData(methodData.getMethod(), methodData);
572                 membersLt.getColumn(column).pack();
573             }
574         }
575     };
576     memberEditorTx.addListener(SWT.DefaultSelection, memberTextListener);
577     memberEditorTx.addListener(SWT.FocusOut, memberTextListener);
578     memberEditorTx.addListener(SWT.Traverse, memberTextListener);
579     
580     final Shell floater = new Shell(shell, SWT.NO_TRIM);
581     floater.setLayout(new FillLayout());
582     memberListEditor = new FlagsEditor(membersLt);
583     memberEditorLt = new List(floater, SWT.MULTI | SWT.BORDER);
584     floater.addListener(SWT.Close, new Listener() {
585         public void handleEvent(Event e) {
586             memberListEditor.setItem(null);
587             e.doit = false;
588             floater.setVisible(false);
589         }
590     });
591     Listener memberListListener = new Listener() {
592         public void handleEvent(Event e) {
593             if (e.type == SWT.Traverse) {
594                 switch (e.detail) {
595                     case SWT.TRAVERSE_RETURN:
596                         break;
597                     default:
598                         return;
599                 }
600             }
601             floater.setVisible(false);
602             TableItem item = memberListEditor.getItem();
603             if (item == null) return;
604             int column = memberListEditor.getColumn();
605             ItemData data = (ItemData)item.getData();
606             String JavaDoc[] flags = memberEditorLt.getSelection();
607             data.setFlags(flags);
608             item.setText(column, getFlagsString(data.getFlags()));
609             item.setChecked(data.getGenerate());
610             MetaData metaData = app.getMetaData();
611             if (data instanceof FieldData) {
612                 FieldData fieldData = (FieldData)data;
613                 metaData.setMetaData(fieldData.getField(), fieldData);
614                 membersLt.getColumn(column).pack();
615             } else if (data instanceof MethodData) {
616                 MethodData methodData = (MethodData)data;
617                 metaData.setMetaData(methodData.getMethod(), methodData);
618                 membersLt.getColumn(column).pack();
619             }
620         }
621     };
622     memberEditorLt.addListener(SWT.DefaultSelection, memberListListener);
623     memberEditorLt.addListener(SWT.FocusOut, memberListListener);
624     memberEditorLt.addListener(SWT.Traverse, memberListListener);
625     
626     membersLt.addListener(SWT.MouseDown, new Listener() {
627         public void handleEvent(Event e) {
628             if (e.button != 1) return;
629             Point pt = new Point(e.x, e.y);
630             TableItem item = membersLt.getItem(pt);
631             if (item == null) return;
632             int column = -1;
633             for (int i = 0; i < membersLt.getColumnCount(); i++) {
634                 if (item.getBounds(i).contains(pt)) {
635                     column = i;
636                     break;
637                 }
638             }
639             if (column == -1) return;
640             ItemData itemData = (ItemData)item.getData();
641             if (itemData instanceof FieldData) {
642                 FieldData data = (FieldData)itemData;
643                 if (column == FIELD_CAST_COLUMN || column == FIELD_ACCESSOR_COLUMN || column == FIELD_EXCLUDE_COLUMN) {
644                     memberTextEditor.setColumn(column);
645                     memberTextEditor.setItem(item);
646                     String JavaDoc text = "";
647                     switch (column) {
648                         case FIELD_CAST_COLUMN: text = data.getCast(); break;
649                         case FIELD_ACCESSOR_COLUMN: text = data.getAccessor(); break;
650                         case FIELD_EXCLUDE_COLUMN: text = data.getExclude(); break;
651                     }
652                     memberEditorTx.setText(text);
653                     memberEditorTx.selectAll();
654                     memberEditorTx.setVisible(true);
655                     memberEditorTx.setFocus();
656                 } else if (column == FIELD_FLAGS_COLUMN) {
657                     memberListEditor.setColumn(column);
658                     memberListEditor.setItem(item);
659                     memberEditorLt.setItems(FieldData.getAllFlags());
660                     memberEditorLt.setSelection(data.getFlags());
661                     floater.setLocation(membersLt.toDisplay(e.x, e.y));
662                     floater.pack();
663                     floater.setVisible(true);
664                     memberEditorLt.setFocus();
665                 }
666             } else if (itemData instanceof MethodData) {
667                 MethodData data = (MethodData)itemData;
668                 if (column == METHOD_EXCLUDE_COLUMN || column == METHOD_ACCESSOR_COLUMN) {
669                     memberTextEditor.setColumn(column);
670                     memberTextEditor.setItem(item);
671                     String JavaDoc text = "";
672                     switch (column) {
673                         case METHOD_ACCESSOR_COLUMN: text = data.getAccessor(); break;
674                         case METHOD_EXCLUDE_COLUMN: text = data.getExclude(); break;
675                     }
676                     memberEditorTx.setText(text);
677                     memberEditorTx.selectAll();
678                     memberEditorTx.setVisible(true);
679                     memberEditorTx.setFocus();
680                 } else if (column == METHOD_FLAGS_COLUMN) {
681                     memberListEditor.setColumn(column);
682                     memberListEditor.setItem(item);
683                     memberEditorLt.setItems(MethodData.getAllFlags());
684                     memberEditorLt.setSelection(data.getFlags());
685                     floater.setLocation(membersLt.toDisplay(e.x, e.y));
686                     floater.pack();
687                     floater.setVisible(true);
688                     memberEditorLt.setFocus();
689                 }
690             }
691         }
692     });
693 }
694
695 void createParametersPanel(Composite panel) {
696     Label paramsLb = new Label(panel, SWT.NONE);
697     paramsLb.setText("&Parameters:");
698     
699     GridData data;
700     paramsLt = new Table(panel, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
701     data = new GridData(GridData.FILL_BOTH);
702     int itemHeight = paramsLt.getItemHeight();
703     data.heightHint = itemHeight * 6;
704     paramsLt.setLayoutData(data);
705     paramsLt.addListener(SWT.Selection, new Listener() {
706         public void handleEvent(Event e) {
707             if (e.detail == SWT.CHECK) {
708                 updateGenerate((TableItem)e.item);
709             }
710         }
711     });
712
713     TableColumn column;
714     column = new TableColumn(paramsLt, SWT.NONE, PARAM_INDEX_COLUMN);
715     column = new TableColumn(paramsLt, SWT.NONE, PARAM_TYPE_COLUMN);
716     column.setText("Type");
717     column = new TableColumn(paramsLt, SWT.NONE, PARAM_FLAGS_COLUMN);
718     column.setText("Flags");
719     column = new TableColumn(paramsLt, SWT.NONE, PARAM_CAST_COLUMN);
720     column.setText("Cast");
721     
722     paramTextEditor = new TableEditor(paramsLt);
723     paramTextEditor.grabHorizontal = true;
724     paramEditorTx = new Text(paramsLt, SWT.SINGLE);
725     paramTextEditor.setEditor(paramEditorTx);
726     Listener paramTextListener = new Listener() {
727         public void handleEvent(Event e) {
728             if (e.type == SWT.Traverse) {
729                 switch (e.detail) {
730                     case SWT.TRAVERSE_ESCAPE:
731                         paramTextEditor.setItem(null);
732                         break;
733                     default:
734                         return;
735                 }
736             }
737             paramEditorTx.setVisible(false);
738             TableItem item = paramTextEditor.getItem();
739             if (item == null) return;
740             int column = paramTextEditor.getColumn();
741             ParameterData paramData = (ParameterData)item.getData();
742             if (column == PARAM_CAST_COLUMN) {
743                 String JavaDoc text = paramEditorTx.getText();
744                 paramData.setCast(text);
745                 item.setText(column, paramData.getCast());
746                 MetaData metaData = app.getMetaData();
747                 metaData.setMetaData(paramData.getMethod(), paramData.getParameter(), paramData);
748                 paramsLt.getColumn(column).pack();
749             }
750         }
751     };
752     paramEditorTx.addListener(SWT.DefaultSelection, paramTextListener);
753     paramEditorTx.addListener(SWT.FocusOut, paramTextListener);
754     paramEditorTx.addListener(SWT.Traverse, paramTextListener);
755     
756     final Shell floater = new Shell(shell, SWT.NO_TRIM);
757     floater.setLayout(new FillLayout());
758     paramListEditor = new FlagsEditor(paramsLt);
759     paramEditorLt = new List(floater, SWT.MULTI | SWT.BORDER);
760     paramEditorLt.setItems(ParameterData.getAllFlags());
761     floater.pack();
762     floater.addListener(SWT.Close, new Listener() {
763         public void handleEvent(Event e) {
764             paramListEditor.setItem(null);
765             e.doit = false;
766             floater.setVisible(false);
767         }
768     });
769     Listener paramListListener = new Listener() {
770         public void handleEvent(Event e) {
771             if (e.type == SWT.Traverse) {
772                 switch (e.detail) {
773                     case SWT.TRAVERSE_RETURN:
774                         break;
775                     default:
776                         return;
777                 }
778             }
779             floater.setVisible(false);
780             TableItem item = paramListEditor.getItem();
781             if (item == null) return;
782             int column = paramListEditor.getColumn();
783             ParameterData paramData = (ParameterData)item.getData();
784             if (column == PARAM_FLAGS_COLUMN) {
785                 String JavaDoc[] flags = paramEditorLt.getSelection();
786                 paramData.setFlags(flags);
787                 item.setText(column, getFlagsString(paramData.getFlags()));
788                 MetaData metaData = app.getMetaData();
789                 metaData.setMetaData(paramData.getMethod(), paramData.getParameter(), paramData);
790                 paramsLt.getColumn(column).pack();
791             }
792         }
793     };
794     paramEditorLt.addListener(SWT.DefaultSelection, paramListListener);
795     paramEditorLt.addListener(SWT.FocusOut, paramListListener);
796     paramEditorLt.addListener(SWT.Traverse, paramListListener);
797
798     paramsLt.addListener(SWT.MouseDown, new Listener() {
799         public void handleEvent(Event e) {
800             if (e.button != 1) return;
801             Point pt = new Point(e.x, e.y);
802             TableItem item = paramsLt.getItem(pt);
803             if (item == null) return;
804             int column = -1;
805             for (int i = 0; i < paramsLt.getColumnCount(); i++) {
806                 if (item.getBounds(i).contains(pt)) {
807                     column = i;
808                     break;
809                 }
810             }
811             if (column == -1) return;
812             ParameterData data = (ParameterData)item.getData();
813             if (column == PARAM_CAST_COLUMN) {
814                 paramTextEditor.setColumn(column);
815                 paramTextEditor.setItem(item);
816                 paramEditorTx.setText(data.getCast());
817                 paramEditorTx.selectAll();
818                 paramEditorTx.setVisible(true);
819                 paramEditorTx.setFocus();
820             } else if (column == PARAM_FLAGS_COLUMN) {
821                 paramListEditor.setColumn(column);
822                 paramListEditor.setItem(item);
823                 paramEditorLt.setSelection(data.getFlags());
824                 floater.setLocation(paramsLt.toDisplay(e.x, e.y));
825                 floater.setVisible(true);
826                 paramEditorLt.setFocus();
827             }
828         }
829     });
830 }
831
832 Button createActionButton(Composite parent, String JavaDoc text, Listener listener) {
833     Button action = new Button(parent, SWT.PUSH);
834     action.setText(text);
835     GridData data = new GridData(GridData.FILL_HORIZONTAL);
836     action.setLayoutData(data);
837     action.addListener(SWT.Selection, listener);
838     return action;
839 }
840
841 void createActionButtons(Composite parent) {
842     actionsPanel = new Composite(parent, SWT.NONE);
843
844     GridData data = new GridData(GridData.FILL_VERTICAL);
845     actionsPanel.setLayoutData(data);
846         
847     GridLayout actionsLayout = new GridLayout();
848     actionsLayout.numColumns = 1;
849     actionsPanel.setLayout(actionsLayout);
850     
851     createActionButton(actionsPanel, "Generate &All", new Listener() {
852         public void handleEvent(Event e) {
853             generateAll();
854         }
855     });
856     
857     Label separator = new Label(actionsPanel, SWT.SEPARATOR | SWT.HORIZONTAL);
858     data = new GridData(GridData.FILL_HORIZONTAL);
859     separator.setLayoutData(data);
860     separator = new Label(actionsPanel, SWT.SEPARATOR | SWT.HORIZONTAL);
861     data = new GridData(GridData.FILL_HORIZONTAL);
862     separator.setLayoutData(data);
863     
864     createActionButton(actionsPanel, "Generate Structs &Header", new Listener() {
865         public void handleEvent(Event e) {
866             generateStructsHeader();
867         }
868     });
869     createActionButton(actionsPanel, "Generate &Structs", new Listener() {
870         public void handleEvent(Event e) {
871             generateStructs();
872         }
873     });
874     createActionButton(actionsPanel, "Generate &Natives", new Listener() {
875         public void handleEvent(Event e) {
876             generateNatives();
877         }
878     });
879     createActionButton(actionsPanel, "Generate Meta &Data", new Listener() {
880         public void handleEvent(Event e) {
881             generateMetaData();
882         }
883     });
884     createActionButton(actionsPanel, "Generate Cons&tants", new Listener() {
885         public void handleEvent(Event e) {
886             generateConstants();
887         }
888     });
889     createActionButton(actionsPanel, "Generate Si&zeof", new Listener() {
890         public void handleEvent(Event e) {
891             generateSizeof();
892         }
893     });
894
895     Composite filler = new Composite(actionsPanel, SWT.NONE);
896     filler.setLayoutData(new GridData(GridData.FILL_BOTH));
897     
898     progressLabel = new Label(actionsPanel, SWT.NONE);
899     data = new GridData(GridData.FILL_HORIZONTAL);
900     progressLabel.setLayoutData(data);
901     progressLabel.setVisible(false);
902     
903     progressBar = new ProgressBar(actionsPanel, SWT.NONE);
904     data = new GridData(GridData.FILL_HORIZONTAL);
905     progressBar.setLayoutData(data);
906     progressBar.setVisible(false);
907 }
908
909 public void run() {
910     shell.open();
911     while (!shell.isDisposed()) {
912         if (!display.readAndDispatch()) display.sleep ();
913     }
914     cleanup();
915 }
916
917 String JavaDoc getPackageString(String JavaDoc className) {
918     int dot = app.getMainClassName().lastIndexOf('.');
919     if (dot == -1) return "";
920     return app.getMainClassName().substring(0, dot);
921 }
922
923 String JavaDoc getClassString(Class JavaDoc clazz) {
924     String JavaDoc name = JNIGenerator.getTypeSignature3(clazz);
925     int index = name.lastIndexOf('.');
926     if (index == -1) return name;
927     return name.substring(index + 1, name.length());
928 }
929
930 String JavaDoc getFlagsString(String JavaDoc[] flags) {
931     if (flags.length == 0) return "";
932     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
933     for (int j = 0; j < flags.length; j++) {
934         String JavaDoc flag = flags[j];
935         if (buffer.length() != 0) buffer.append(", ");
936         buffer.append(flag);
937     }
938     return buffer.toString();
939 }
940
941 String JavaDoc getMethodString(Method method) {
942     String JavaDoc pkgName = getPackageString(method.getDeclaringClass().getName());
943     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
944     buffer.append(method.getName());
945     buffer.append("(");
946     Class JavaDoc[] params = method.getParameterTypes();
947     for (int i = 0; i < params.length; i++) {
948         Class JavaDoc param = params[i];
949         if (i != 0) buffer.append(",");
950         String JavaDoc string = JNIGenerator.getTypeSignature3(param);
951         if (string.startsWith(pkgName)) string = string.substring(pkgName.length() + 1);
952         buffer.append(string);
953     }
954     buffer.append(")");
955     return buffer.toString();
956 }
957
958 String JavaDoc getFieldString(Field field) {
959     return field.getName();
960 }
961
962 void updateClasses() {
963     classesLt.removeAll();
964     MetaData metaData = app.getMetaData();
965     Class JavaDoc[] classes = app.getClasses();
966     for (int i = 0; i < classes.length; i++) {
967         Class JavaDoc clazz = classes[i];
968         ClassData classData = metaData.getMetaData(clazz);
969         TableItem item = new TableItem(classesLt, SWT.NONE);
970         item.setData(classData);
971         item.setText(CLASS_NAME_COLUMN, getClassString(clazz));
972         item.setText(CLASS_FLAGS_COLUMN, getFlagsString(classData.getFlags()));
973         item.setChecked(classData.getGenerate());
974     }
975     TableColumn[] columns = classesLt.getColumns();
976     for (int i = 0; i < columns.length; i++) {
977         TableColumn column = columns[i];
978         column.pack();
979     }
980 }
981
982 void updateMembers() {
983     membersLt.removeAll();
984     MetaData metaData = app.getMetaData();
985     membersLt.setHeaderVisible(false);
986     TableColumn[] columns = membersLt.getColumns();
987     for (int i = 0; i < columns.length; i++) {
988         TableColumn column = columns[i];
989         column.dispose();
990     }
991     int[] indices = classesLt.getSelectionIndices();
992     if (indices.length != 1) return;
993     TableItem classItem = classesLt.getItem(indices[0]);
994     ClassData classData = (ClassData)classItem.getData();
995     Class JavaDoc clazz = classData.getClazz();
996     boolean hasNatives = false;
997     Method[] methods = clazz.getDeclaredMethods();
998     for (int i = 0; i < methods.length; i++) {
999         Method method = methods[i];
1000        int mods = method.getModifiers();
1001        if (hasNatives =((mods & Modifier.NATIVE) != 0)) break;
1002    }
1003    membersLt.setRedraw(false);
1004    if (hasNatives) {
1005        TableColumn column;
1006        column = new TableColumn(membersLt, SWT.NONE, METHOD_NAME_COLUMN);
1007        column.setText("Method");
1008        column = new TableColumn(membersLt, SWT.NONE, METHOD_FLAGS_COLUMN);
1009        column.setText("Flags");
1010        column = new TableColumn(membersLt, SWT.NONE, METHOD_ACCESSOR_COLUMN);
1011        column.setText("Accessor");
1012        /*
1013        column = new TableColumn(membersLt, SWT.NONE, METHOD_EXCLUDE_COLUMN);
1014        column.setText("Exclude");
1015        */

1016        JNIGenerator.sort(methods);
1017        for (int i = 0; i < methods.length; i++) {
1018            Method method = methods[i];
1019            if ((method.getModifiers() & Modifier.NATIVE) == 0) continue;
1020            MethodData methodData = metaData.getMetaData(method);
1021            TableItem item = new TableItem(membersLt, SWT.NONE);
1022            item.setData(methodData);
1023            item.setText(METHOD_NAME_COLUMN, getMethodString(method));
1024            item.setChecked(methodData.getGenerate());
1025            item.setText(METHOD_FLAGS_COLUMN, getFlagsString(methodData.getFlags()));
1026            item.setText(METHOD_ACCESSOR_COLUMN, methodData.getAccessor());
1027            /*
1028            item.setText(METHOD_EXCLUDE_COLUMN, methodData.getExclude());
1029            */

1030        }
1031    } else {
1032        TableColumn column;
1033        column = new TableColumn(membersLt, SWT.NONE, FIELD_NAME_COLUMN);
1034        column.setText("Field");
1035        column = new TableColumn(membersLt, SWT.NONE, FIELD_FLAGS_COLUMN);
1036        column.setText("Flags");
1037        column = new TableColumn(membersLt, SWT.NONE, FIELD_CAST_COLUMN);
1038        column.setText("Cast");
1039        column = new TableColumn(membersLt, SWT.NONE, FIELD_ACCESSOR_COLUMN);
1040        column.setText("Accessor");
1041        /*
1042        column = new TableColumn(membersLt, SWT.NONE, FIELD_EXCLUDE_COLUMN);
1043        column.setText("Exclude");
1044        */

1045        Field[] fields = clazz.getDeclaredFields();
1046        for (int i = 0; i < fields.length; i++) {
1047            Field field = fields[i];
1048            int mods = field.getModifiers();
1049            if (((mods & Modifier.PUBLIC) == 0) ||
1050                ((mods & Modifier.FINAL) != 0) ||
1051                ((mods & Modifier.STATIC) != 0)) continue;
1052            FieldData fieldData = metaData.getMetaData(field);
1053            TableItem item = new TableItem(membersLt, SWT.NONE);
1054            item.setData(fieldData);
1055            item.setText(FIELD_NAME_COLUMN, getFieldString(field));
1056            item.setChecked(fieldData.getGenerate());
1057            item.setText(FIELD_CAST_COLUMN, fieldData.getCast());
1058            item.setText(FIELD_FLAGS_COLUMN, getFlagsString(fieldData.getFlags()));
1059            item.setText(FIELD_ACCESSOR_COLUMN, fieldData.getAccessor());
1060            /*
1061            item.setText(FIELD_EXCLUDE_COLUMN, fieldData.getExclude());
1062            */

1063        }
1064    }
1065    columns = membersLt.getColumns();
1066    for (int i = 0; i < columns.length; i++) {
1067        TableColumn column = columns[i];
1068        column.pack();
1069    }
1070    membersLt.setHeaderVisible(true);
1071    membersLt.setRedraw(true);
1072}
1073
1074void updateParameters() {
1075    paramsLt.removeAll();
1076    MetaData metaData = app.getMetaData();
1077    int[] indices = membersLt.getSelectionIndices();
1078    if (indices.length != 1) {
1079        paramsLt.setHeaderVisible(false);
1080        return;
1081    }
1082    TableItem memberItem = membersLt.getItem(indices[0]);
1083    Object JavaDoc data = memberItem.getData();
1084    if (!(data instanceof MethodData)) return;
1085    paramsLt.setRedraw(false);
1086    MethodData methodData = (MethodData)memberItem.getData();
1087    Method method = methodData.getMethod();
1088    Class JavaDoc[] params = method.getParameterTypes();
1089    for (int i = 0; i < params.length; i++) {
1090        Class JavaDoc param = params[i];
1091        ParameterData paramData = metaData.getMetaData(method, i);
1092        TableItem item = new TableItem(paramsLt, SWT.NONE);
1093        item.setData(paramData);
1094        item.setText(PARAM_INDEX_COLUMN, String.valueOf(i));
1095        item.setText(PARAM_TYPE_COLUMN, getClassString(param));
1096        item.setText(PARAM_CAST_COLUMN, paramData.getCast());
1097        item.setText(PARAM_FLAGS_COLUMN, getFlagsString(paramData.getFlags()));
1098    }
1099    TableColumn[] columns = paramsLt.getColumns();
1100    for (int i = 0; i < columns.length; i++) {
1101        TableColumn column = columns[i];
1102        column.pack();
1103    }
1104    paramsLt.setRedraw(true);
1105    paramsLt.setHeaderVisible(true);
1106}
1107
1108void updateGenerate(TableItem item) {
1109    MetaData metaData = app.getMetaData();
1110    ItemData itemData = (ItemData)item.getData();
1111    itemData.setGenerate(item.getChecked());
1112    if (itemData instanceof ClassData) {
1113        ClassData data = (ClassData)itemData;
1114        metaData.setMetaData(data.getClazz(), data);
1115    } else if (itemData instanceof FieldData) {
1116        FieldData data = (FieldData)itemData;
1117        item.setText(FIELD_FLAGS_COLUMN, getFlagsString(data.getFlags()));
1118        metaData.setMetaData(data.getField(), data);
1119    } else if (itemData instanceof MethodData) {
1120        MethodData data = (MethodData)itemData;
1121        item.setText(METHOD_FLAGS_COLUMN, getFlagsString(data.getFlags()));
1122        metaData.setMetaData(data.getMethod(), data);
1123    } else if (itemData instanceof ParameterData) {
1124        ParameterData data = (ParameterData)itemData;
1125        item.setText(PARAM_FLAGS_COLUMN, getFlagsString(data.getFlags()));
1126        metaData.setMetaData(data.getMethod(), data.getParameter(), data);
1127    }
1128}
1129
1130boolean updateOutputDir() {
1131    String JavaDoc outputDirStr = outputDirCb.getText();
1132    File file = new File(outputDirStr);
1133    if (!file.exists()) {
1134        MessageBox dialog = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
1135        dialog.setMessage("Output directory does not exist.");
1136        dialog.open();
1137        return false;
1138    }
1139    if (!file.isDirectory()) {
1140        MessageBox dialog = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
1141        dialog.setMessage("Output directory is not a directory.");
1142        dialog.open();
1143        return false;
1144    }
1145    if (outputDirStr.length() > 0) {
1146        if (!outputDirStr.equals(app.getOutputDir())) {
1147            app.setOutputDir(outputDirStr);
1148        }
1149        if (outputDirCb.indexOf(outputDirStr) == -1) {
1150            outputDirCb.add(outputDirStr);
1151        }
1152    }
1153    return true;
1154}
1155
1156void updateMainClass() {
1157    String JavaDoc mainClassStr = mainClassCb.getText();
1158    if (mainClassStr.length() > 0) {
1159        if (!mainClassStr.equals(app.getMainClassName())) {
1160            app.setMainClassName(mainClassStr);
1161        }
1162        if (mainClassCb.indexOf(mainClassStr) == -1) {
1163            mainClassCb.add(mainClassStr);
1164        }
1165        if (app.getOutputDir() != null) {
1166            int index = outputDirCb.indexOf(app.getOutputDir());
1167            if (index != -1) outputDirCb.select(index);
1168        }
1169    }
1170}
1171
1172public static void main(String JavaDoc[] args) {
1173    JNIGeneratorApp gen = new JNIGeneratorApp ();
1174    if (args.length > 0) {
1175        gen.setMainClassName(args[0]);
1176        if (args.length > 1) gen.setOutputDir(args[1]);
1177    } else {
1178        gen.setMainClassName(JNIGeneratorApp.getDefaultMainClass());
1179    }
1180    JNIGeneratorAppUI ui = new JNIGeneratorAppUI(gen);
1181    ui.open();
1182    ui.run();
1183}
1184
1185}
1186
Popular Tags