KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsitconf > ui > ClassDialog


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.websvc.wsitconf.ui;
21
22 import java.awt.Dialog JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Set JavaDoc;
28 import org.netbeans.api.java.source.CompilationController;
29 import org.netbeans.api.java.source.JavaSource;
30 import org.netbeans.api.project.Project;
31 import org.netbeans.modules.websvc.wsitconf.util.AbstractTask;
32 import org.netbeans.modules.websvc.wsitconf.util.SourceUtils;
33 import org.openide.DialogDescriptor;
34 import org.openide.DialogDisplayer;
35 import org.openide.NotifyDescriptor;
36 import org.openide.filesystems.FileObject;
37 import org.openide.nodes.Node;
38 import org.openide.util.Exceptions;
39 import org.openide.util.NbBundle;
40
41 /**
42  *
43  * @author Martin Grebac
44  * Displays a Dialog for selecting classes that are in a project.
45  */

46 public class ClassDialog {
47     
48     private Dialog JavaDoc dialog;
49     private SelectClassPanel sPanel;
50     private SelectClassDialogDesc dlgDesc;
51     
52     /**
53      * Creates a new instance of ClassDialog
54      */

55     public ClassDialog(Project project, String JavaDoc extendingClass) {
56         sPanel = new SelectClassPanel(project);
57         dlgDesc = new SelectClassDialogDesc(sPanel, extendingClass);
58         dialog = DialogDisplayer.getDefault().createDialog(dlgDesc);
59     }
60     
61     public void show(){
62         dialog.setVisible(true);
63     }
64     
65     public boolean okButtonPressed(){
66         return dlgDesc.getValue() == DialogDescriptor.OK_OPTION;
67     }
68     
69     public Set JavaDoc<String JavaDoc> getSelectedClasses(){
70         Set JavaDoc<String JavaDoc> selectedClasses = new HashSet JavaDoc<String JavaDoc>();
71         Node[] nodes = sPanel.getSelectedNodes();
72         for(int i = 0; i < nodes.length; i++){
73             String JavaDoc name = getClassNameFromNode(nodes[i]);
74             selectedClasses.add(name);
75         }
76         return selectedClasses;
77     }
78
79     private String JavaDoc getClassNameFromNode(Node node) {
80         final String JavaDoc[] name = new String JavaDoc[1];
81
82         FileObject classElement = (FileObject) node.getLookup().lookup(FileObject.class);
83         JavaSource js = JavaSource.forFileObject(classElement);
84         try {
85             js.runUserActionTask(new AbstractTask<CompilationController>() {
86                  public void run(CompilationController controller) throws java.io.IOException JavaDoc {
87                      controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
88                      SourceUtils sourceUtils = SourceUtils.newInstance(controller);
89                      name[0] = sourceUtils.getTypeElement().getQualifiedName().toString();
90                  }
91              }, true);
92         } catch (IOException JavaDoc ex) {
93             Exceptions.printStackTrace(ex);
94         }
95         return name[0];
96     }
97     
98     class SelectClassDialogDesc extends DialogDescriptor{
99         Project project;
100         String JavaDoc extendingClass;
101         final SelectClassPanel sPanel;
102         
103         private Object JavaDoc[] closingOptionsWithoutOK = {DialogDescriptor.CANCEL_OPTION,
104         DialogDescriptor.CLOSED_OPTION};
105         private Object JavaDoc[] closingOptionsWithOK = {DialogDescriptor.CANCEL_OPTION,
106         DialogDescriptor.CLOSED_OPTION, DialogDescriptor.OK_OPTION};
107         
108         /**
109          * Creates a new instance of SelectClassDialogDesc
110          */

111         public SelectClassDialogDesc(SelectClassPanel sPanel, String JavaDoc extendingClass) {
112             super(sPanel, "Select Class"); //NOI18N
113
this.extendingClass = extendingClass;
114             this.sPanel = sPanel;
115             this.setButtonListener(new AddClassActionListener(sPanel));
116         }
117         
118         class AddClassActionListener implements ActionListener JavaDoc{
119             SelectClassPanel sPanel;
120             public AddClassActionListener(SelectClassPanel sPanel){
121                 this.sPanel = sPanel;
122             }
123             public void actionPerformed(ActionEvent JavaDoc evt){
124                 if(evt.getSource() == NotifyDescriptor.OK_OPTION){
125                     boolean accepted = true;
126                     String JavaDoc errMsg = null;
127                     Node[] selectedNodes = sPanel.getSelectedNodes();
128                     for(int i = 0; i < selectedNodes.length; i++){
129                         Node node = selectedNodes[i];
130                         FileObject classElement = (FileObject) node.getLookup().lookup(FileObject.class);
131                         JavaSource js = JavaSource.forFileObject(classElement);
132                         if (js == null) {
133                             errMsg = NbBundle.getMessage(ClassDialog.class, "TXT_NotJavaClass_msg"); //NOI18N
134
accepted = false;
135                             break;
136                         }
137                                                 
138                         if (!isWantedClass(js)) {
139                             errMsg = NbBundle.getMessage(ClassDialog.class, "TXT_NotWantedClass_msg", //NOI18N
140
classElement.getName(), extendingClass);
141                             accepted = false;
142                             break;
143                         }
144                     }
145                     if (!accepted) {
146                         NotifyDescriptor.Message notifyDescr =
147                                 new NotifyDescriptor.Message(errMsg,
148                                 NotifyDescriptor.ERROR_MESSAGE );
149                         DialogDisplayer.getDefault().notify(notifyDescr);
150                         SelectClassDialogDesc.this.setClosingOptions(closingOptionsWithoutOK);
151                     } else {
152                         // Everything was fine so allow OK
153
SelectClassDialogDesc.this.setClosingOptions(closingOptionsWithOK);
154                     }
155                 }
156             }
157         }
158
159         private boolean isWantedClass(JavaSource js) {
160             final Boolean JavaDoc[] subType = new Boolean JavaDoc[1];
161             subType[0] = false;
162             try {
163                 js.runUserActionTask(new AbstractTask<CompilationController>() {
164                      public void run(CompilationController controller) throws java.io.IOException JavaDoc {
165                          controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
166                          SourceUtils sourceUtils = SourceUtils.newInstance(controller);
167                          subType[0] = Boolean.valueOf(sourceUtils.isSubtype(extendingClass));
168                      }
169                  }, true);
170             } catch (IOException JavaDoc ex) {
171                 Exceptions.printStackTrace(ex);
172             }
173             return subType[0];
174         }
175     }
176 }
177
Popular Tags