001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.websvc.wsitconf.ui;
043:
044: import java.awt.Dialog;
045: import java.awt.event.ActionEvent;
046: import java.awt.event.ActionListener;
047: import java.io.IOException;
048: import java.util.HashSet;
049: import java.util.Set;
050: import org.netbeans.api.java.source.CompilationController;
051: import org.netbeans.api.java.source.JavaSource;
052: import org.netbeans.api.project.Project;
053: import org.netbeans.modules.websvc.wsitconf.util.AbstractTask;
054: import org.netbeans.modules.websvc.wsitconf.util.SourceUtils;
055: import org.openide.DialogDescriptor;
056: import org.openide.DialogDisplayer;
057: import org.openide.NotifyDescriptor;
058: import org.openide.filesystems.FileObject;
059: import org.openide.nodes.Node;
060: import org.openide.util.Exceptions;
061: import org.openide.util.NbBundle;
062:
063: /**
064: *
065: * @author Martin Grebac
066: * Displays a Dialog for selecting classes that are in a project.
067: */
068: public class ClassDialog {
069:
070: private Dialog dialog;
071: private SelectClassPanel sPanel;
072: private SelectClassDialogDesc dlgDesc;
073:
074: /**
075: * Creates a new instance of ClassDialog
076: */
077: public ClassDialog(Project project, String extendingClass) {
078: sPanel = new SelectClassPanel(project);
079: dlgDesc = new SelectClassDialogDesc(sPanel, extendingClass);
080: dialog = DialogDisplayer.getDefault().createDialog(dlgDesc);
081: }
082:
083: public void show() {
084: dialog.setVisible(true);
085: }
086:
087: public boolean okButtonPressed() {
088: return dlgDesc.getValue() == DialogDescriptor.OK_OPTION;
089: }
090:
091: public Set<String> getSelectedClasses() {
092: Set<String> selectedClasses = new HashSet<String>();
093: Node[] nodes = sPanel.getSelectedNodes();
094: for (int i = 0; i < nodes.length; i++) {
095: String name = getClassNameFromNode(nodes[i]);
096: selectedClasses.add(name);
097: }
098: return selectedClasses;
099: }
100:
101: private String getClassNameFromNode(Node node) {
102: final String[] name = new String[1];
103:
104: FileObject classElement = node.getLookup().lookup(
105: FileObject.class);
106: JavaSource js = JavaSource.forFileObject(classElement);
107: try {
108: js.runUserActionTask(
109: new AbstractTask<CompilationController>() {
110: public void run(CompilationController controller)
111: throws java.io.IOException {
112: controller
113: .toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
114: SourceUtils sourceUtils = SourceUtils
115: .newInstance(controller);
116: name[0] = sourceUtils.getTypeElement()
117: .getQualifiedName().toString();
118: }
119: }, true);
120: } catch (IOException ex) {
121: Exceptions.printStackTrace(ex);
122: }
123: return name[0];
124: }
125:
126: static class SelectClassDialogDesc extends DialogDescriptor {
127: String extendingClass;
128: final SelectClassPanel sPanel;
129:
130: private Object[] closingOptionsWithoutOK = {
131: DialogDescriptor.CANCEL_OPTION,
132: DialogDescriptor.CLOSED_OPTION };
133: private Object[] closingOptionsWithOK = {
134: DialogDescriptor.CANCEL_OPTION,
135: DialogDescriptor.CLOSED_OPTION,
136: DialogDescriptor.OK_OPTION };
137:
138: /**
139: * Creates a new instance of SelectClassDialogDesc
140: */
141: public SelectClassDialogDesc(SelectClassPanel sPanel,
142: String extendingClass) {
143: super (sPanel, "Select Class"); //NOI18N
144: this .extendingClass = extendingClass;
145: this .sPanel = sPanel;
146: this .setButtonListener(new AddClassActionListener(sPanel));
147: }
148:
149: class AddClassActionListener implements ActionListener {
150: SelectClassPanel sPanel;
151:
152: public AddClassActionListener(SelectClassPanel sPanel) {
153: this .sPanel = sPanel;
154: }
155:
156: public void actionPerformed(ActionEvent evt) {
157: if (evt.getSource() == NotifyDescriptor.OK_OPTION) {
158: boolean accepted = true;
159: String errMsg = null;
160: Node[] selectedNodes = sPanel.getSelectedNodes();
161: for (int i = 0; i < selectedNodes.length; i++) {
162: Node node = selectedNodes[i];
163: FileObject classElement = node.getLookup()
164: .lookup(FileObject.class);
165: JavaSource js = JavaSource
166: .forFileObject(classElement);
167: if (js == null) {
168: errMsg = NbBundle.getMessage(
169: ClassDialog.class,
170: "TXT_NotJavaClass_msg"); //NOI18N
171: accepted = false;
172: break;
173: }
174:
175: if (!isWantedClass(js)) {
176: errMsg = NbBundle.getMessage(
177: ClassDialog.class,
178: "TXT_NotWantedClass_msg", //NOI18N
179: classElement.getName(),
180: extendingClass);
181: accepted = false;
182: break;
183: }
184: }
185: if (!accepted) {
186: NotifyDescriptor.Message notifyDescr = new NotifyDescriptor.Message(
187: errMsg, NotifyDescriptor.ERROR_MESSAGE);
188: DialogDisplayer.getDefault().notifyLater(
189: notifyDescr);
190: SelectClassDialogDesc.this
191: .setClosingOptions(closingOptionsWithoutOK);
192: } else {
193: // Everything was fine so allow OK
194: SelectClassDialogDesc.this
195: .setClosingOptions(closingOptionsWithOK);
196: }
197: }
198: }
199: }
200:
201: private boolean isWantedClass(JavaSource js) {
202: final Boolean[] subType = new Boolean[1];
203: subType[0] = false;
204: try {
205: js.runUserActionTask(
206: new AbstractTask<CompilationController>() {
207: public void run(
208: CompilationController controller)
209: throws java.io.IOException {
210: controller
211: .toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
212: SourceUtils sourceUtils = SourceUtils
213: .newInstance(controller);
214: subType[0] = Boolean
215: .valueOf(sourceUtils
216: .isSubtype(extendingClass));
217: }
218: }, true);
219: } catch (IOException ex) {
220: Exceptions.printStackTrace(ex);
221: }
222: return subType[0];
223: }
224: }
225: }
|