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 1997-2006 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: package org.netbeans.modules.apisupport.project.metainf;
042:
043: import java.util.StringTokenizer;
044: import javax.swing.JButton;
045: import javax.swing.event.DocumentEvent;
046: import javax.swing.event.DocumentListener;
047: import org.netbeans.api.java.project.JavaProjectConstants;
048: import org.netbeans.api.project.Project;
049: import org.netbeans.api.project.ProjectInformation;
050: import org.netbeans.api.project.SourceGroup;
051: import org.netbeans.api.project.Sources;
052: import org.netbeans.modules.apisupport.project.NbModuleProject;
053: import org.netbeans.modules.apisupport.project.spi.NbModuleProvider;
054: import org.netbeans.spi.java.project.support.ui.PackageView;
055: import org.openide.filesystems.FileObject;
056: import org.openide.loaders.DataObject;
057: import org.openide.nodes.Node;
058: import org.openide.nodes.NodeOperation;
059: import org.openide.util.NbBundle;
060: import org.openide.util.UserCancelException;
061:
062: /**
063: * Customizer for adding META-INF/services item
064: * @author pzajac
065: */
066: public class AddServiceDialog extends javax.swing.JPanel {
067:
068: /** selected class name
069: */
070: private Project project;
071:
072: /** Creates new form AddNodeDialog */
073: public AddServiceDialog(Project project) {
074: initComponents();
075: this .project = project;
076: okButton = new JButton();
077: cancelButton = new JButton();
078: org.openide.awt.Mnemonics.setLocalizedText(okButton, NbBundle
079: .getMessage(AddServiceDialog.class, "CTL_OK")); // NOI18N
080: org.openide.awt.Mnemonics.setLocalizedText(cancelButton,
081: NbBundle.getMessage(AddServiceDialog.class,
082: "CTL_CANCEL")); // NOI18N
083: okButton.setEnabled(false);
084: classField.getDocument().addDocumentListener(
085: new DocumentListener() {
086:
087: public void insertUpdate(DocumentEvent e) {
088: checkJavaFile();
089: }
090:
091: public void removeUpdate(DocumentEvent e) {
092: checkJavaFile();
093: }
094:
095: public void changedUpdate(DocumentEvent e) {
096: checkJavaFile();
097: }
098: });
099: }
100:
101: /** This method is called from within the constructor to
102: * initialize the form.
103: * WARNING: Do NOT modify this code. The content of this method is
104: * always regenerated by the Form Editor.
105: */
106: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
107: private void initComponents() {
108:
109: classField = new javax.swing.JTextField();
110: browseButton = new javax.swing.JButton();
111: jLabel1 = new javax.swing.JLabel();
112:
113: org.openide.awt.Mnemonics.setLocalizedText(browseButton,
114: org.openide.util.NbBundle.getMessage(
115: AddServiceDialog.class, "CTL_BROWSE")); // NOI18N
116: browseButton
117: .addActionListener(new java.awt.event.ActionListener() {
118: public void actionPerformed(
119: java.awt.event.ActionEvent evt) {
120: browseButtonActionPerformed(evt);
121: }
122: });
123:
124: jLabel1.setLabelFor(classField);
125: org.openide.awt.Mnemonics.setLocalizedText(jLabel1,
126: org.openide.util.NbBundle.getMessage(
127: AddServiceDialog.class, "LBL_CLASS_NAME")); // NOI18N
128:
129: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
130: this );
131: this .setLayout(layout);
132: layout
133: .setHorizontalGroup(layout
134: .createParallelGroup(
135: org.jdesktop.layout.GroupLayout.LEADING)
136: .add(
137: layout
138: .createSequentialGroup()
139: .addContainerGap()
140: .add(
141: layout
142: .createParallelGroup(
143: org.jdesktop.layout.GroupLayout.LEADING)
144: .add(
145: jLabel1,
146: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
147: 299,
148: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
149: .add(
150: layout
151: .createSequentialGroup()
152: .add(
153: classField,
154: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
155: 299,
156: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
157: .addPreferredGap(
158: org.jdesktop.layout.LayoutStyle.RELATED)
159: .add(
160: browseButton,
161: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
162: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
163: Short.MAX_VALUE)))
164: .addContainerGap()));
165: layout
166: .setVerticalGroup(layout
167: .createParallelGroup(
168: org.jdesktop.layout.GroupLayout.LEADING)
169: .add(
170: layout
171: .createSequentialGroup()
172: .addContainerGap()
173: .add(jLabel1)
174: .addPreferredGap(
175: org.jdesktop.layout.LayoutStyle.RELATED)
176: .add(
177: layout
178: .createParallelGroup(
179: org.jdesktop.layout.GroupLayout.BASELINE)
180: .add(
181: classField,
182: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
183: 25,
184: org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
185: .add(
186: browseButton))
187: .addContainerGap(
188: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
189: Short.MAX_VALUE)));
190:
191: classField
192: .getAccessibleContext()
193: .setAccessibleDescription(
194: org.openide.util.NbBundle
195: .getMessage(AddServiceDialog.class,
196: "AddServiceDialog.classField.AccessibleContext.accessibleDescription")); // NOI18N
197:
198: getAccessibleContext()
199: .setAccessibleDescription(
200: org.openide.util.NbBundle
201: .getMessage(AddServiceDialog.class,
202: "AddServiceDialog.AccessibleContext.accessibleDescription")); // NOI18N
203: }// </editor-fold>//GEN-END:initComponents
204:
205: private void browseButtonActionPerformed(
206: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButtonActionPerformed
207: Sources sources = project.getLookup().lookup(Sources.class);
208: SourceGroup groups[] = sources
209: .getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
210:
211: ProjectInformation inf = project.getLookup().lookup(
212: ProjectInformation.class);
213: if (groups.length > 0) {
214: Node srcNode = PackageView.createPackageView(groups[0]);
215: try {
216: Node node = NodeOperation.getDefault().select(
217: NbBundle.getMessage(AddServiceDialog.class,
218: "MSG_SelectAClass"),
219: inf.getDisplayName(), srcNode);
220: // try find a java file
221: DataObject dobj = null;
222: do {
223: dobj = node.getLookup().lookup(DataObject.class);
224: node = node.getParentNode();
225: } while (dobj == null && node != null);
226: if (dobj != null) {
227: FileObject fo = dobj.getPrimaryFile();
228: if (fo.getExt().equals("java")) { // NOI18N
229: String fileName = fo.getPath();
230: NbModuleProvider info = project.getLookup()
231: .lookup(NbModuleProvider.class);
232: String className = fileName.substring(info
233: .getSourceDirectory().getPath()
234: .length(), fileName.length()
235: - ".java".length()); // NOI18N
236: if (className.startsWith("/")) { // NOI18N
237: className = className.substring(1);
238: }
239: className = className.replace('/', '.');
240: classField.setText(className);
241: }
242:
243: }
244: checkJavaFile();
245: } catch (UserCancelException uc) {
246: // cancel button clicked
247: }
248:
249: }
250:
251: }//GEN-LAST:event_browseButtonActionPerformed
252:
253: private void checkJavaFile() {
254: String path = classField.getText();
255: FileObject root = ((NbModuleProject) project)
256: .getSourceDirectory();
257: boolean status = false;
258: // check if class already exists
259: // checking validity of inner classes is missing
260: StringTokenizer tokenizer = new StringTokenizer(path, "."); // NOI18N
261: while (tokenizer.hasMoreTokens()) {
262: String name = tokenizer.nextToken();
263: FileObject childFo = root.getFileObject(name);
264: if (childFo != null) {
265: if (childFo.isFolder()) {
266: root = childFo;
267: } else {
268: status = "java".equals(childFo.getExt()); // NOI18N
269: break;
270: }
271: } else {
272: status = root.getFileObject(name + ".java") != null; // NOI18N
273: break;
274: }
275: }
276: okButton.setEnabled(status);
277: }
278:
279: public String getClassName() {
280: return classField.getText();
281: }
282:
283: public JButton getCancelButton() {
284: return cancelButton;
285: }
286:
287: public JButton getOkButton() {
288: return okButton;
289: }
290:
291: private JButton okButton;
292: private JButton cancelButton;
293: // Variables declaration - do not modify//GEN-BEGIN:variables
294: private javax.swing.JButton browseButton;
295: private javax.swing.JTextField classField;
296: private javax.swing.JLabel jLabel1;
297: // End of variables declaration//GEN-END:variables
298:
299: }
|