001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.ideaplugin.frames;
020:
021: import org.apache.axis2.tools.component.WizardPanel;
022: import org.apache.axis2.tools.component.WizardComponents;
023: import org.apache.axis2.tools.wizardframe.CodegenFrame;
024: import org.apache.ideaplugin.bean.ArchiveBean;
025: import org.apache.ideaplugin.bean.OperationObj;
026: import org.apache.ideaplugin.bean.ServiceObj;
027: import org.apache.ideaplugin.bean.ClassFileFilter;
028: import org.apache.ideaplugin.frames.table.ArchiveTableModel;
029:
030: import javax.swing.*;
031: import javax.swing.border.EmptyBorder;
032: import javax.swing.table.TableModel;
033: import java.util.HashMap;
034: import java.util.ArrayList;
035: import java.util.Iterator;
036: import java.awt.*;
037: import java.awt.event.ActionListener;
038: import java.awt.event.ActionEvent;
039: import java.lang.reflect.Method;
040: import java.net.URL;
041: import java.net.URLClassLoader;
042: import java.net.MalformedURLException;
043: import java.io.File;
044:
045: public class ServiceXMLGenerationPage extends WizardPanel {
046:
047: private JTextField txtServiceName;
048: private JTextField txtClassName;
049: private JCheckBox chkBoxSearchMethod;
050: private JButton btnLoad;
051: private JButton btnBrowse;
052: private JTable table;
053: private JLabel lblTable;
054: private JScrollPane scrollPane;
055: private HashMap operations;
056: private String fileName;
057: private int count = 1;
058: private ArrayList servicelsit;
059: private String sgXMl;
060: private final JFileChooser fileChooser = new JFileChooser();
061: private ArchiveBean archiveBean;
062:
063: /**
064: * Constructor
065: * @param wizardComponents
066: */
067: public ServiceXMLGenerationPage(WizardComponents wizardComponents,
068: ArchiveBean archiveBean) {
069: super (wizardComponents,
070: "Axis2 Idea Plugin Service Archiver Creator Wizards");
071: setPanelTopTitle("Service Archiver");
072: setPanelBottomTitle("Service XML Generation");
073: this .archiveBean = archiveBean;
074: init();
075: }
076:
077: private void init() {
078:
079: txtServiceName = new JTextField();
080: txtClassName = new JTextField();
081:
082: btnBrowse = new JButton("Browse");
083: btnLoad = new JButton("Load");
084:
085: chkBoxSearchMethod = new JCheckBox(
086: "Search declared method only", true);
087:
088: lblTable = new JLabel(
089: "Mark operation you do not want to publish ");
090: operations = new HashMap();
091: ArchiveTableModel myModel = new ArchiveTableModel(operations);
092: table = new JTable(myModel);
093: table.setOpaque(true);
094: table.setBackground(getBackground());
095: table.setShowGrid(true);
096: table.setSize(getPreferredSize());
097:
098: scrollPane = new JScrollPane(table);
099: scrollPane.setBorder(new EmptyBorder(0, 0, 0, 0));
100: scrollPane.setSize(table.getSize());
101: scrollPane.setOpaque(true);
102: scrollPane.setBackground(getBackground());
103: scrollPane.getViewport().setBackground(getBackground());
104: scrollPane.setViewportBorder(new EmptyBorder(0, 0, 0, 0));
105:
106: setBackButtonEnabled(true);
107: setNextButtonEnabled(false);
108: setFinishButtonEnabled(false);
109: setPageComplete(false);
110: this .setLayout(new GridBagLayout());
111:
112: this
113: .add(new JLabel("Class Name"), new GridBagConstraints(
114: 0, 0, 1, 1, 0.1, 0.0,
115: GridBagConstraints.NORTHWEST,
116: GridBagConstraints.NONE,
117: new Insets(5, 10, 0, 0), 0, 0));
118:
119: this .add(txtClassName, new GridBagConstraints(1, 0, 1, 1, 1.0,
120: 0.0, GridBagConstraints.NORTHWEST,
121: GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 0),
122: 0, 0));
123: this .add(btnBrowse, new GridBagConstraints(2, 0, 1, 1, 0.1,
124: 0.0, GridBagConstraints.NORTH, GridBagConstraints.NONE,
125: new Insets(5, 1, 1, 1), 0, 0));
126: btnBrowse.addActionListener(new ActionListener() {
127: public void actionPerformed(ActionEvent e) {
128: browseClassFile();
129: update();
130: }
131: });
132: this .add(btnLoad, new GridBagConstraints(3, 0, 1, 1, 0.1, 0.0,
133: GridBagConstraints.NORTH, GridBagConstraints.NONE,
134: new Insets(5, 1, 1, 10), 0, 0));
135:
136: btnLoad.addActionListener(new ActionListener() {
137: public void actionPerformed(ActionEvent e) {
138: updateTable();
139: update();
140: }
141: });
142: this
143: .add(new JLabel("Service Name"),
144: new GridBagConstraints(0, 1, 1, 1, 0.1, 0.0,
145: GridBagConstraints.NORTHWEST,
146: GridBagConstraints.NONE, new Insets(5,
147: 10, 0, 0), 0, 0));
148: this .add(txtServiceName, new GridBagConstraints(1, 1, 1, 1,
149: 1.0, 0.0, GridBagConstraints.NORTHWEST,
150: GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 0),
151: 0, 0));
152:
153: this .add(chkBoxSearchMethod, new GridBagConstraints(0, 2,
154: GridBagConstraints.RELATIVE, 1, 0.0, 0.0,
155: GridBagConstraints.NORTHWEST,
156: GridBagConstraints.HORIZONTAL,
157: new Insets(5, 10, 0, 10), 0, 0));
158: chkBoxSearchMethod.addActionListener(new ActionListener() {
159: public void actionPerformed(ActionEvent e) {
160: updateTable();
161: update();
162: }
163: });
164: this .add(lblTable, new GridBagConstraints(0, 3,
165: GridBagConstraints.RELATIVE, 1, 0.0, 0.0,
166: GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
167: new Insets(5, 10, 0, 10), 0, 0));
168:
169: this .add(scrollPane, new GridBagConstraints(0, 4,
170: GridBagConstraints.REMAINDER, 1, 1.0, 1.0,
171: GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,
172: new Insets(5, 10, 10, 10), 0, 0));
173:
174: setPageComplete(true);
175: }
176:
177: public void back() {
178: switchPanel(CodegenFrame.PANEL_FOURTH_C);
179: }
180:
181: public void next() {
182: setNextButtonEnabled(false);
183: checkautoGeneration();
184: switchPanel(CodegenFrame.PANEL_OPTION_C);
185: ((ServiceXMLEditPage) getWizardComponents().getWizardPanel(
186: CodegenFrame.PANEL_OPTION_C))
187: .setDescription(archiveBean.getServiceXML());
188: ((ServiceXMLEditPage) getWizardComponents().getWizardPanel(
189: CodegenFrame.PANEL_OPTION_C)).setDefaultEnabled();
190: }
191:
192: public void update() {
193:
194: }
195:
196: public int getPageType() {
197: return WizardPanel.SERVICE_ARCHIVE_TYPE;
198: }
199:
200: private void updateTable() {
201: //get a URL from the class file location
202: try {
203: String classFileLocation = archiveBean.getClassLoc()
204: .getPath();
205: URL classFileURL = new File(classFileLocation).toURL();
206:
207: ArrayList listofURLs = new ArrayList();
208: listofURLs.add(classFileURL);
209:
210: //get the libraries from the lib page and load it
211: ArrayList libList = archiveBean.getLibs();
212: String[] libFileList = new String[libList.size()];
213: for (int i = 0; i < libList.size(); i++) {
214: libFileList[i] = (String) libList.get(i);
215: }
216:
217: if (libFileList != null) {
218: int count = libFileList.length;
219: for (int i = 0; i < count; i++) {
220: listofURLs.add(new File(libFileList[i]).toURL());
221: }
222: }
223: txtServiceName.setText("MyService" + count);
224: ClassLoader loader = new URLClassLoader((URL[]) listofURLs
225: .toArray(new URL[listofURLs.size()]));
226: Class clazz = Class.forName(fileName, true, loader);
227: Method[] methods = null;
228: if (chkBoxSearchMethod.isSelected()) {
229: methods = clazz.getDeclaredMethods();
230: } else {
231: methods = clazz.getMethods();
232: }
233:
234: int methodCount = methods.length;
235: if (methodCount > 0) {
236: try {
237: table.removeAll();
238: table.setVisible(true);
239: operations.clear();
240: } catch (Exception e1) {
241: }
242: for (int i = 0; i < methodCount; i++) {
243: Method method = methods[i];
244: OperationObj operationobj = new OperationObj(method
245: .getName(), method.getReturnType()
246: .toString(), new Integer(method
247: .getParameterTypes().length), new Boolean(
248: true));
249: operations.put(method.getName(), operationobj);
250: }
251:
252: ArchiveTableModel myModel = new ArchiveTableModel(
253: operations);
254: table.setModel(myModel);
255: scrollPane.repaint();
256: this .repaint();
257: setNextButtonEnabled(true);
258: }
259:
260: } catch (MalformedURLException e) {
261: setNextButtonEnabled(false);
262: JOptionPane.showMessageDialog(btnLoad,
263: "The specified file is not a valid java class",
264: "Error!", JOptionPane.ERROR_MESSAGE);
265: } catch (ClassNotFoundException e) {
266: setNextButtonEnabled(false);
267: JOptionPane.showMessageDialog(btnLoad,
268: "The specified file is not a valid java class",
269: "Error!", JOptionPane.ERROR_MESSAGE);
270: }
271: }
272:
273: private void browseClassFile() {
274: fileChooser.setFileFilter(new ClassFileFilter());
275: fileChooser.setCurrentDirectory(archiveBean.getClassLoc());
276: int returnVal = fileChooser.showOpenDialog(this );
277: if (returnVal == JFileChooser.APPROVE_OPTION) {
278: File tempfile = fileChooser.getSelectedFile();
279: String newFile = tempfile.getPath();
280: int index = newFile.indexOf(archiveBean.getClassLoc()
281: .getAbsolutePath().trim());
282: if (index >= 0) {
283: int lastindex = archiveBean.getClassLoc()
284: .getAbsolutePath().trim().length();
285: newFile = newFile.substring(lastindex + 1);
286: char ch = File.separatorChar;
287: char newch = '.';
288: int cindex = newFile.indexOf(ch);
289: while (cindex >= 0) {
290: newFile = newFile.replace(ch, newch);
291: cindex = newFile.indexOf(ch);
292: }
293: fileName = newFile;
294: int classIndex = fileName.lastIndexOf(".");
295: fileName = fileName.substring(0, classIndex);
296: txtClassName.setText(fileName);
297: }
298: }
299: }
300:
301: private void serviceGroupProcess() {
302:
303: ArrayList ops = new ArrayList();
304: Iterator opitr = operations.values().iterator();
305: while (opitr.hasNext()) {
306: OperationObj operationObj = (OperationObj) opitr.next();
307: if (operationObj.getSelect().booleanValue()) {
308: ops.add(operationObj.getOpName());
309: }
310: }
311:
312: ServiceObj service = new ServiceObj(txtServiceName.getText(),
313: fileName, ops);
314: archiveBean.addToServicelsit(service);
315: if (!archiveBean.isSingleService()) {
316: int valu = JOptionPane.showConfirmDialog(this ,
317: "Do you want to add an another service to group",
318: "Service Archive", JOptionPane.YES_NO_OPTION);
319: if (valu == 0) {
320: fileName = "";
321: txtClassName.setText("");
322: txtServiceName.setText("");
323: operations.clear();
324: setNextButtonEnabled(false);
325: switchPanel(CodegenFrame.PANEL_FIRST_C);
326: count++;
327: this .repaint();
328: } else {
329: servicelsit = archiveBean.getServicelsit();
330: System.out.println(servicelsit.size());
331: sgXMl = "<serviceGroup>\n";
332: for (int i = 0; i < servicelsit.size(); i++) {
333: ServiceObj serviceObj = (ServiceObj) servicelsit
334: .get(i);
335: sgXMl = sgXMl + serviceObj.toString();
336: }
337: sgXMl = sgXMl + "</serviceGroup>";
338: archiveBean.setServiceXML(sgXMl);
339: sgXMl = "";
340: switchPanel(CodegenFrame.PANEL_OPTION_C);
341: }
342: } else {
343: servicelsit = archiveBean.getServicelsit();
344: sgXMl = "<serviceGroup>\n";
345: for (int i = 0; i < servicelsit.size(); i++) {
346: ServiceObj serviceObj = (ServiceObj) servicelsit.get(i);
347: sgXMl = sgXMl + serviceObj.toString();
348: }
349: sgXMl = sgXMl + "</serviceGroup>";
350: archiveBean.setServiceXML(sgXMl);
351: sgXMl = "";
352: switchPanel(CodegenFrame.PANEL_OPTION_C);
353: }
354: }
355:
356: private void checkautoGeneration() {
357: ArrayList ops = new ArrayList();
358: Iterator opitr = operations.values().iterator();
359: while (opitr.hasNext()) {
360: OperationObj operationObj = (OperationObj) opitr.next();
361: if (operationObj.getSelect().booleanValue()) {
362: ops.add(operationObj.getOpName());
363: }
364: }
365: txtServiceName.setText(txtServiceName.getText());
366: ServiceObj service = new ServiceObj(txtServiceName.getText(),
367: fileName, ops);
368: archiveBean.addToServicelsit(service);
369: servicelsit = archiveBean.getServicelsit();
370: sgXMl = "<serviceGroup>";
371: for (int i = 0; i < servicelsit.size(); i++) {
372: ServiceObj serviceObj = (ServiceObj) servicelsit.get(i);
373: sgXMl = sgXMl + serviceObj.toString();
374: }
375: sgXMl = sgXMl + "</serviceGroup>";
376: archiveBean.setServiceXML(sgXMl);
377: sgXMl = "";
378: }
379: }
|