1 19 20 package org.netbeans.modules.websvc.wsitconf.ui; 21 22 import java.awt.Dialog ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.io.IOException ; 26 import java.util.HashSet ; 27 import java.util.Set ; 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 46 public class ClassDialog { 47 48 private Dialog dialog; 49 private SelectClassPanel sPanel; 50 private SelectClassDialogDesc dlgDesc; 51 52 55 public ClassDialog(Project project, String 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 <String > getSelectedClasses(){ 70 Set <String > selectedClasses = new HashSet <String >(); 71 Node[] nodes = sPanel.getSelectedNodes(); 72 for(int i = 0; i < nodes.length; i++){ 73 String name = getClassNameFromNode(nodes[i]); 74 selectedClasses.add(name); 75 } 76 return selectedClasses; 77 } 78 79 private String getClassNameFromNode(Node node) { 80 final String [] name = new String [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 { 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 ex) { 93 Exceptions.printStackTrace(ex); 94 } 95 return name[0]; 96 } 97 98 class SelectClassDialogDesc extends DialogDescriptor{ 99 Project project; 100 String extendingClass; 101 final SelectClassPanel sPanel; 102 103 private Object [] closingOptionsWithoutOK = {DialogDescriptor.CANCEL_OPTION, 104 DialogDescriptor.CLOSED_OPTION}; 105 private Object [] closingOptionsWithOK = {DialogDescriptor.CANCEL_OPTION, 106 DialogDescriptor.CLOSED_OPTION, DialogDescriptor.OK_OPTION}; 107 108 111 public SelectClassDialogDesc(SelectClassPanel sPanel, String extendingClass) { 112 super(sPanel, "Select Class"); this.extendingClass = extendingClass; 114 this.sPanel = sPanel; 115 this.setButtonListener(new AddClassActionListener(sPanel)); 116 } 117 118 class AddClassActionListener implements ActionListener { 119 SelectClassPanel sPanel; 120 public AddClassActionListener(SelectClassPanel sPanel){ 121 this.sPanel = sPanel; 122 } 123 public void actionPerformed(ActionEvent evt){ 124 if(evt.getSource() == NotifyDescriptor.OK_OPTION){ 125 boolean accepted = true; 126 String 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"); accepted = false; 135 break; 136 } 137 138 if (!isWantedClass(js)) { 139 errMsg = NbBundle.getMessage(ClassDialog.class, "TXT_NotWantedClass_msg", 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 SelectClassDialogDesc.this.setClosingOptions(closingOptionsWithOK); 154 } 155 } 156 } 157 } 158 159 private boolean isWantedClass(JavaSource js) { 160 final Boolean [] subType = new Boolean [1]; 161 subType[0] = false; 162 try { 163 js.runUserActionTask(new AbstractTask<CompilationController>() { 164 public void run(CompilationController controller) throws java.io.IOException { 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 ex) { 171 Exceptions.printStackTrace(ex); 172 } 173 return subType[0]; 174 } 175 } 176 } 177 | Popular Tags |