KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > tool > codegen > eclipse > ui > JavaSourceSelectionPage


1 /*
2  * Copyright 2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package org.apache.axis.tool.codegen.eclipse.ui;
17
18 import java.io.File JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.net.URLClassLoader JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import org.apache.axis.tool.codegen.eclipse.plugin.CodegenWizardPlugin;
26 import org.eclipse.swt.SWT;
27 import org.eclipse.swt.events.ModifyEvent;
28 import org.eclipse.swt.events.ModifyListener;
29 import org.eclipse.swt.events.SelectionAdapter;
30 import org.eclipse.swt.events.SelectionEvent;
31 import org.eclipse.swt.events.SelectionListener;
32 import org.eclipse.swt.layout.GridData;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Button;
35 import org.eclipse.swt.widgets.Composite;
36 import org.eclipse.swt.widgets.DirectoryDialog;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.swt.widgets.Table;
39 import org.eclipse.swt.widgets.TableColumn;
40 import org.eclipse.swt.widgets.TableItem;
41 import org.eclipse.swt.widgets.Text;
42
43 public class JavaSourceSelectionPage extends AbstractWizardPage{
44
45    
46     private Text javaClassFileLocationBox;
47     private Text javaClassNameBox;
48     private Button searchDeclaredMethodsCheckBox;
49     
50     private Table table;
51     
52     private boolean dirty;
53
54     public JavaSourceSelectionPage() {
55         super("page4");
56     }
57
58     protected void initializeDefaultSettings() {
59         settings.put(JAVA_CLASS_NAME, "");
60         settings.put(JAVA_CLASS_LOCATION_NAME, "");
61     }
62
63     /*
64      * (non-Javadoc)
65      *
66      * @see org.apache.axis.tool.codegen.eclipse.ui.CodegenPage#getPageType()
67      */

68     public int getPageType() {
69         return JAVA_2_WSDL_TYPE;
70     }
71
72     /*
73      * (non-Javadoc)
74      *
75      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
76      */

77     public void createControl(Composite parent) {
78         Composite container = new Composite(parent, SWT.NULL);
79         GridLayout layout = new GridLayout();
80         container.setLayout(layout);
81         layout.numColumns = 3;
82         layout.verticalSpacing = 9;
83
84         GridData gd = new GridData(GridData.FILL_HORIZONTAL);
85
86         Label label = new Label(container, SWT.NULL);
87         label.setText(CodegenWizardPlugin
88                 .getResourceString("page4.javafilelocation.label"));
89
90         javaClassFileLocationBox = new Text(container, SWT.BORDER);
91         javaClassFileLocationBox.setLayoutData(gd);
92         javaClassFileLocationBox.setText(settings.get(JAVA_CLASS_LOCATION_NAME));
93         javaClassFileLocationBox.addModifyListener(new ModifyListener(){
94             public void modifyText(ModifyEvent e){
95                handleLocationTextChange();
96             }
97         });
98         
99
100         Button browseButton = new Button(container, SWT.PUSH);
101         browseButton.setText(CodegenWizardPlugin
102                 .getResourceString("general.browse"));
103         browseButton.addSelectionListener(new SelectionAdapter() {
104             public void widgetSelected(SelectionEvent e) {
105                 handleDirectoryBrowse();
106             }
107         });
108         
109         label = new Label(container, SWT.NULL);
110         label.setText(CodegenWizardPlugin
111                 .getResourceString("page4.classname.label"));
112         
113         gd = new GridData(GridData.FILL_HORIZONTAL);
114         javaClassNameBox = new Text(container,SWT.BORDER);
115         javaClassNameBox.setLayoutData(gd);
116         javaClassNameBox.setText(settings.get(JAVA_CLASS_NAME));
117         javaClassNameBox.addModifyListener(new ModifyListener(){
118             public void modifyText(ModifyEvent e){
119                 handleClassNameTextChange();
120              }
121          });
122         
123         Button searchButton = new Button(container, SWT.PUSH);
124         searchButton.setText(CodegenWizardPlugin
125                 .getResourceString("general.search"));
126         searchButton.addSelectionListener(new SelectionAdapter() {
127             public void widgetSelected(SelectionEvent e) {
128                 updateTable();
129             }
130         });
131         //searchButton.setEnabled(false);
132
gd = new GridData(GridData.FILL_HORIZONTAL);
133         gd.horizontalSpan = 3;
134         
135         searchDeclaredMethodsCheckBox = new Button(container,SWT.CHECK);
136         searchDeclaredMethodsCheckBox.setLayoutData(gd);
137         searchDeclaredMethodsCheckBox.setText("List Declared Methods Only");
138         searchDeclaredMethodsCheckBox.addSelectionListener(new SelectionListener(){
139             public void widgetSelected(SelectionEvent e){
140                 updateDirtyStatus(true);//dirty
141
}
142             public void widgetDefaultSelected(SelectionEvent e){}
143         });
144         
145         gd = new GridData(GridData.FILL_BOTH);
146         gd.horizontalSpan=3;
147         gd.verticalSpan=5;
148         table = new Table(container,SWT.SINGLE|SWT.FULL_SELECTION|SWT.CHECK);
149         table.setLinesVisible(true);
150         table.setHeaderVisible(true);
151         table.setLayoutData(gd);
152         declareColumn(table,20,"");
153         declareColumn(table,100,"Method Name");
154         declareColumn(table,100,"Return Type");
155         declareColumn(table,100,"Parameter Count");
156         table.setVisible(false);
157         
158         setPageComplete(false);
159         
160         if (restoredFromPreviousSettings){
161             handleLocationTextChange();
162             handleClassNameTextChange();
163         }
164         
165         setControl(container);
166
167     }
168
169     private void declareColumn(Table table, int width,String JavaDoc colName){
170         TableColumn column = new TableColumn(table,SWT.NONE);
171         column.setWidth(width);
172         column.setText(colName);
173     }
174     /**
175      * Pops up the file browse dialog box
176      *
177      */

178     private void handleDirectoryBrowse() {
179         DirectoryDialog fileDialog = new DirectoryDialog(this.getShell());
180         String JavaDoc dirName = fileDialog.open();
181         if (dirName != null) {
182             javaClassFileLocationBox.setText(dirName);
183         }
184
185     }
186     
187     private void handleLocationTextChange(){
188         String JavaDoc locationText = javaClassFileLocationBox.getText();
189         settings.put(JAVA_CLASS_LOCATION_NAME,locationText);
190         if (locationText==null || "".equals(locationText.trim())){
191             updateStatus("Class file location needs to be specified!");
192         }else{
193             updateStatus(null);
194         }
195     }
196     
197     private void handleClassNameTextChange(){
198         String JavaDoc className = javaClassNameBox.getText();
199         settings.put(JAVA_CLASS_NAME,className);
200         if (className==null || "".equals(className.trim())){
201             updateStatus("Fully qualified class name needs to be specified!");
202         }else if(className.endsWith(".")){
203             updateStatus("Class name is not properly terminated!");
204         }else{
205             updateStatus(null);
206         }
207     }
208     
209     public String JavaDoc getClassName(){
210         return javaClassNameBox.getText();
211     }
212     
213     public String JavaDoc getClassLocation(){
214         return javaClassFileLocationBox.getText();
215     }
216     
217     public Vector JavaDoc getSelectedMethods(){
218         Vector JavaDoc list = new Vector JavaDoc();
219         TableItem[] items = table.getItems();
220         int itemLength = items.length;
221         for(int i=0;i<itemLength;i++){
222            if(items[i].getChecked()){
223                list.add(items[i].getText(1));//get the selected method name only
224
}
225         }
226         return list;
227     }
228     
229     private void updateTable() {
230         //get a URL from the class file location
231
try {
232             String JavaDoc classFileLocation = this.getClassLocation();
233             URL JavaDoc classFileURL = new File JavaDoc(classFileLocation).toURL();
234             ClassLoader JavaDoc loader = new URLClassLoader JavaDoc(new URL JavaDoc[] { classFileURL });
235
236             Class JavaDoc clazz = loader.loadClass(getClassName());
237             Method JavaDoc[] methods = null;
238             
239             if (searchDeclaredMethodsCheckBox.getSelection()){
240                 methods = clazz.getDeclaredMethods();
241             }else{
242                 methods = clazz.getMethods();
243             }
244
245             int methodCount = methods.length;
246             if (methodCount > 0) {
247                 table.removeAll();
248                 TableItem[] items = new TableItem[methodCount]; // An item for each field
249
for (int i = 0 ; i < methodCount; i++){
250                    items[i] = new TableItem(table, SWT.NONE);
251                    items[i].setText(1,methods[i].getName());
252                    items[i].setText(2,methods[i].getReturnType().getName());
253                    items[i].setText(3,methods[i].getParameterTypes().length+"");
254                    items[i].setChecked(true);//check them all by default
255
}
256                 table.setVisible(true);
257                 
258                 //update the dirty variable
259
updateDirtyStatus(false);
260                updateStatus(null);
261             }
262
263         } catch (MalformedURLException JavaDoc e) {
264            updateStatus("Error : invalid location " +e.getMessage());
265         } catch (ClassNotFoundException JavaDoc e) {
266            updateStatus("Error : Class not found " + e.getMessage());
267         }
268     }
269     
270     private void updateDirtyStatus(boolean status){
271         dirty = status;
272         if (table.isVisible()){
273             table.setEnabled(!status);
274         }
275         setPageComplete(!status);
276     }
277 }
278
Popular Tags