KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > metainf > AddServiceDialog


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 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.apisupport.project.metainf;
20
21 import org.netbeans.api.java.project.JavaProjectConstants;
22 import org.netbeans.api.project.ProjectInformation;
23 import org.netbeans.api.project.SourceGroup;
24 import org.netbeans.api.project.Sources;
25 import org.netbeans.modules.apisupport.project.NbModuleProject;
26 import org.netbeans.spi.java.project.support.ui.PackageView;
27 import org.openide.filesystems.FileObject;
28 import org.openide.loaders.DataObject;
29 import org.openide.nodes.Node;
30 import org.openide.nodes.NodeOperation;
31 import org.openide.util.NbBundle;
32 import org.openide.util.UserCancelException;
33
34 /**
35  * Customizer for adding META-INF/services item
36  * @author pzajac
37  */

38 public class AddServiceDialog extends javax.swing.JPanel JavaDoc {
39     
40     /** selected class name
41      */

42     private NbModuleProject project;
43     /** Creates new form AddNodeDialog */
44     public AddServiceDialog(NbModuleProject project) {
45         initComponents();
46         this.project = project;
47     }
48     
49     /** This method is called from within the constructor to
50      * initialize the form.
51      * WARNING: Do NOT modify this code. The content of this method is
52      * always regenerated by the Form Editor.
53      */

54     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
55
private void initComponents() {
56         classField = new javax.swing.JTextField JavaDoc();
57         browseButton = new javax.swing.JButton JavaDoc();
58         jLabel1 = new javax.swing.JLabel JavaDoc();
59
60         org.openide.awt.Mnemonics.setLocalizedText(browseButton, org.openide.util.NbBundle.getMessage(AddServiceDialog.class, "CTL_BROWSE")); // NOI18N
61
browseButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
62             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
63                 browseButtonActionPerformed(evt);
64             }
65         });
66
67         jLabel1.setLabelFor(classField);
68         org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(AddServiceDialog.class, "LBL_CLASS_NAME")); // NOI18N
69

70         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
71         this.setLayout(layout);
72         layout.setHorizontalGroup(
73             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
74             .add(layout.createSequentialGroup()
75                 .addContainerGap()
76                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
77                     .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 299, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
78                     .add(layout.createSequentialGroup()
79                         .add(classField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 299, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
80                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
81                         .add(browseButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)))
82                 .addContainerGap())
83         );
84         layout.setVerticalGroup(
85             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
86             .add(layout.createSequentialGroup()
87                 .addContainerGap()
88                 .add(jLabel1)
89                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
90                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
91                     .add(classField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
92                     .add(browseButton))
93                 .addContainerGap())
94         );
95     }// </editor-fold>//GEN-END:initComponents
96

97     private void browseButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_browseButtonActionPerformed
98
Sources sources = (Sources) project.getLookup().lookup(Sources.class);
99         SourceGroup groups[] = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
100         
101         ProjectInformation inf = (ProjectInformation) project.getLookup().lookup(ProjectInformation.class);
102         if (groups.length > 0) {
103             Node srcNode = PackageView.createPackageView(groups[0]);
104             try {
105                 Node node = NodeOperation.getDefault().select(NbBundle.getMessage(AddServiceDialog.class, "MSG_SelectAClass"),
106                         inf.getDisplayName(),
107                         srcNode );
108                 // try find a java file
109
DataObject dobj = null;
110                 do {
111                     dobj = (DataObject) node.getLookup().lookup(DataObject.class);
112                     node = node.getParentNode();
113                 } while (dobj == null && node != null);
114                 if (dobj != null) {
115                     FileObject fo = dobj.getPrimaryFile();
116                     if (fo.getExt().equals("java")) { // NOI18N
117
String JavaDoc fileName = fo.getPath();
118                         String JavaDoc className = fileName.substring(project.getSourceDirectory().getPath().length(),
119                                 fileName.length() - ".java".length()); // NOI18N
120
if (className.startsWith("/")) { // NOI18N
121
className = className.substring(1);
122                         }
123                         className = className.replace('/','.');
124                         classField.setText(className);
125                     }
126                     
127                 }
128             } catch (UserCancelException uc) {
129                 // cancel button clicked
130
}
131             
132         }
133         
134     }//GEN-LAST:event_browseButtonActionPerformed
135

136     public String JavaDoc getClassName() {
137         return classField.getText();
138     }
139     
140     // Variables declaration - do not modify//GEN-BEGIN:variables
141
private javax.swing.JButton JavaDoc browseButton;
142     private javax.swing.JTextField JavaDoc classField;
143     private javax.swing.JLabel JavaDoc jLabel1;
144     // End of variables declaration//GEN-END:variables
145

146 }
147
Popular Tags