~ubuntu-branches/ubuntu/trusty/netbeans/trusty

« back to all changes in this revision

Viewing changes to apisupport/project/src/org/netbeans/modules/apisupport/project/ui/wizard/BasicConfVisualPanel.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.apisupport.project.ui.wizard;
 
43
 
 
44
import java.io.File;
 
45
import java.io.IOException;
 
46
import javax.swing.event.DocumentEvent;
 
47
import javax.swing.event.DocumentListener;
 
48
import org.netbeans.api.project.Project;
 
49
import org.netbeans.api.project.ProjectManager;
 
50
import org.netbeans.api.project.ProjectUtils;
 
51
import org.netbeans.modules.apisupport.project.Util;
 
52
import org.netbeans.modules.apisupport.project.ui.UIUtil;
 
53
import org.netbeans.modules.apisupport.project.ui.customizer.SuiteUtils;
 
54
import org.openide.ErrorManager;
 
55
import org.openide.filesystems.FileObject;
 
56
import org.openide.filesystems.FileUtil;
 
57
import org.openide.util.NbBundle;
 
58
 
 
59
/**
 
60
 * Second UI panel of <code>NewNbModuleWizardIterator</code> for
 
61
 * <em>standalone</em> module creating mode. Allow user to enter basic
 
62
 * configuration:
 
63
 *
 
64
 * <ul>
 
65
 *  <li>Code Name Base</li>
 
66
 *  <li>Module Display Name</li>
 
67
 *  <li>Localizing Bundle</li>
 
68
 *  <li>XML Layer</li>
 
69
 * </ul>
 
70
 *
 
71
 * @author Martin Krauskopf
 
72
 */
 
73
final class BasicConfVisualPanel extends BasicVisualPanel.NewTemplatePanel {
 
74
    
 
75
    static final String EXAMPLE_BASE_NAME = "org.yourorghere."; // NOI18N
 
76
    
 
77
    private boolean wasLayerUpdated;
 
78
    private boolean wasBundleUpdated;
 
79
    
 
80
    private boolean listenersAttached;
 
81
    private final DocumentListener cnbDL;
 
82
    private final DocumentListener layerDL;
 
83
    private final DocumentListener bundleDL;
 
84
    
 
85
    public BasicConfVisualPanel(final NewModuleProjectData data) {
 
86
        super(data);
 
87
        initComponents();
 
88
        initAccessibility();
 
89
        cnbDL = new UIUtil.DocumentAdapter() {
 
90
            public void insertUpdate(DocumentEvent e) { checkCodeNameBase(); }
 
91
        };
 
92
        if (isLibraryWizard()) {
 
93
            // for library modules, don't generate any layer.
 
94
            layer.setVisible(false);
 
95
            layerValue.setVisible(false);
 
96
            layerDL = null;
 
97
        } else {
 
98
            layerDL = new UIUtil.DocumentAdapter() {
 
99
                public void insertUpdate(DocumentEvent e) { wasLayerUpdated = true; checkLayer(); }
 
100
            };
 
101
        }
 
102
        bundleDL = new UIUtil.DocumentAdapter() {
 
103
            public void insertUpdate(DocumentEvent e) { wasBundleUpdated = true; checkBundle(); }
 
104
        };
 
105
    }
 
106
    
 
107
    private void initAccessibility() {
 
108
        this.getAccessibleContext().setAccessibleDescription(getMessage("ACS_BasicConfVisualPanel"));
 
109
        bundleValue.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_BundleValue"));
 
110
        codeNameBaseValue.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_CodeNameBaseValue"));
 
111
        displayNameValue.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_DisplayNameValue"));
 
112
        layerValue.getAccessibleContext().setAccessibleDescription(getMessage("ACS_CTL_LayerValue"));
 
113
    }
 
114
    
 
115
    private void checkCodeNameBase() {
 
116
        if (!Util.isValidJavaFQN(getCodeNameBaseValue())) {
 
117
            setError(getMessage("MSG_InvalidCNB"));
 
118
        } else if (getData().isSuiteComponent() && cnbIsAlreadyInSuite(getData().getSuiteRoot(), getCodeNameBaseValue())) {
 
119
            setError(NbBundle.getMessage(BasicConfVisualPanel.class, "MSG_ComponentWithSuchCNBAlreadyInSuite",
 
120
                    getCodeNameBaseValue()));
 
121
        } else {
 
122
            markValid();
 
123
            // update layer and bundle from the cnb
 
124
            String dotName = getCodeNameBaseValue();
 
125
            String slashName = dotName.replace('.', '/');
 
126
            if (!wasBundleUpdated) {
 
127
                bundleValue.setText(slashName + "/Bundle.properties"); // NOI18N
 
128
                wasBundleUpdated = false;
 
129
            }
 
130
            if (!wasLayerUpdated && !isLibraryWizard()) {
 
131
                layerValue.setText(slashName + "/layer.xml"); // NOI18N
 
132
                wasLayerUpdated = false;
 
133
            }
 
134
        }
 
135
    }
 
136
    
 
137
    private void checkBundle() {
 
138
        checkEntry(getBundleValue(), "bundle", ".properties"); // NOI18N
 
139
    }
 
140
    
 
141
    private void checkLayer() {
 
142
        checkEntry(getLayerValue(), "layer", ".xml"); // NOI18N
 
143
    }
 
144
    
 
145
    /** Used for Layer and Bundle entries. */
 
146
    private void checkEntry(String path, String resName, String extension) {
 
147
        if (path.length() == 0) {
 
148
            setError(NbBundle.getMessage(BasicConfVisualPanel.class, "BasicConfVisualPanel_err_" + resName + "_empty"));
 
149
            return;
 
150
        }
 
151
        if (path.indexOf('/') == -1) {
 
152
            setError(NbBundle.getMessage(BasicConfVisualPanel.class, "BasicConfVisualPanel_err_" + resName + "_def_pkg"));
 
153
            return;
 
154
        }
 
155
        if (!path.endsWith(extension)) {
 
156
            setError(NbBundle.getMessage(BasicConfVisualPanel.class, "BasicConfVisualPanel_err_" + resName + "_ext", extension));
 
157
            return;
 
158
        }
 
159
        markValid();
 
160
    }
 
161
    
 
162
    void refreshData() {
 
163
        String cnb = getData().getCodeNameBase();
 
164
        codeNameBaseValue.setText(cnb);
 
165
        if (cnb.startsWith(EXAMPLE_BASE_NAME)) {
 
166
            codeNameBaseValue.select(0, EXAMPLE_BASE_NAME.length() - 1);
 
167
        }
 
168
        String dn = getData().getProjectDisplayName();
 
169
        displayNameValue.setText(dn);
 
170
        checkCodeNameBase();
 
171
    }
 
172
    
 
173
    /** Stores collected data into model. */
 
174
    void storeData() {
 
175
        // change will be fired -> update data
 
176
        getData().setCodeNameBase(getCodeNameBaseValue());
 
177
        getData().setProjectDisplayName(displayNameValue.getText());
 
178
        getData().setBundle(getBundleValue());
 
179
        if (!isLibraryWizard()) {
 
180
            getData().setLayer(getLayerValue());
 
181
        }
 
182
    }
 
183
    
 
184
    private String getCodeNameBaseValue() {
 
185
        return codeNameBaseValue.getText().trim();
 
186
    }
 
187
    
 
188
    private String getBundleValue() {
 
189
        return bundleValue.getText().trim();
 
190
    }
 
191
    
 
192
    private String getLayerValue() {
 
193
        return layerValue.getText().trim();
 
194
    }
 
195
    
 
196
    private boolean cnbIsAlreadyInSuite(String suiteDir, String cnb) {
 
197
        boolean result = false;
 
198
        FileObject suiteDirFO = FileUtil.toFileObject(new File(suiteDir));
 
199
        try {
 
200
            Project suite = ProjectManager.getDefault().findProject(suiteDirFO);
 
201
            for (Project p : SuiteUtils.getSubProjects(suite)) {
 
202
                if (ProjectUtils.getInformation(p).getName().equals(cnb)) {
 
203
                    result = true;
 
204
                    break;
 
205
                }
 
206
            }
 
207
        } catch (IOException e) {
 
208
            Util.err.notify(ErrorManager.INFORMATIONAL, e);
 
209
        }
 
210
        return result;
 
211
    }
 
212
    
 
213
    public @Override void addNotify() {
 
214
        super.addNotify();
 
215
        attachDocumentListeners();
 
216
    }
 
217
    
 
218
    public @Override void removeNotify() {
 
219
        // prevent checking when the panel is not "active"
 
220
        removeDocumentListeners();
 
221
        super.removeNotify();
 
222
    }
 
223
    
 
224
    private void attachDocumentListeners() {
 
225
        if (!listenersAttached) {
 
226
            codeNameBaseValue.getDocument().addDocumentListener(cnbDL);
 
227
            bundleValue.getDocument().addDocumentListener(bundleDL);
 
228
            if (!isLibraryWizard()) {
 
229
                layerValue.getDocument().addDocumentListener(layerDL);
 
230
            }
 
231
            listenersAttached = true;
 
232
        }
 
233
    }
 
234
    
 
235
    private void removeDocumentListeners() {
 
236
        if (listenersAttached) {
 
237
            codeNameBaseValue.getDocument().removeDocumentListener(cnbDL);
 
238
            bundleValue.getDocument().removeDocumentListener(bundleDL);
 
239
            if (!isLibraryWizard()) {
 
240
                layerValue.getDocument().removeDocumentListener(layerDL);
 
241
            }
 
242
            listenersAttached = false;
 
243
        }
 
244
    }
 
245
    
 
246
    private static String getMessage(String key) {
 
247
        return NbBundle.getMessage(BasicConfVisualPanel.class, key);
 
248
    }
 
249
    
 
250
    /** This method is called from within the constructor to
 
251
     * initialize the form.
 
252
     * WARNING: Do NOT modify this code. The content of this method is
 
253
     * always regenerated by the Form Editor.
 
254
     */
 
255
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
 
256
    private void initComponents() {
 
257
        java.awt.GridBagConstraints gridBagConstraints;
 
258
 
 
259
        confPanel = new javax.swing.JPanel();
 
260
        codeNameBase = new javax.swing.JLabel();
 
261
        displayName = new javax.swing.JLabel();
 
262
        bundle = new javax.swing.JLabel();
 
263
        layer = new javax.swing.JLabel();
 
264
        codeNameBaseValue = new javax.swing.JTextField();
 
265
        displayNameValue = new javax.swing.JTextField();
 
266
        bundleValue = new javax.swing.JTextField();
 
267
        layerValue = new javax.swing.JTextField();
 
268
        filler = new javax.swing.JLabel();
 
269
 
 
270
        setLayout(new java.awt.GridBagLayout());
 
271
 
 
272
        confPanel.setLayout(new java.awt.GridBagLayout());
 
273
 
 
274
        codeNameBase.setLabelFor(codeNameBaseValue);
 
275
        org.openide.awt.Mnemonics.setLocalizedText(codeNameBase, org.openide.util.NbBundle.getMessage(BasicConfVisualPanel.class, "LBL_CodeNameBase"));
 
276
        gridBagConstraints = new java.awt.GridBagConstraints();
 
277
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
 
278
        gridBagConstraints.insets = new java.awt.Insets(1, 0, 6, 12);
 
279
        confPanel.add(codeNameBase, gridBagConstraints);
 
280
 
 
281
        displayName.setLabelFor(displayNameValue);
 
282
        org.openide.awt.Mnemonics.setLocalizedText(displayName, org.openide.util.NbBundle.getMessage(BasicConfVisualPanel.class, "LBL_ModuleDisplayName"));
 
283
        gridBagConstraints = new java.awt.GridBagConstraints();
 
284
        gridBagConstraints.gridx = 0;
 
285
        gridBagConstraints.gridy = 1;
 
286
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
 
287
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 12);
 
288
        confPanel.add(displayName, gridBagConstraints);
 
289
 
 
290
        bundle.setLabelFor(bundleValue);
 
291
        org.openide.awt.Mnemonics.setLocalizedText(bundle, org.openide.util.NbBundle.getMessage(BasicConfVisualPanel.class, "LBL_LocalizingBundle"));
 
292
        gridBagConstraints = new java.awt.GridBagConstraints();
 
293
        gridBagConstraints.gridx = 0;
 
294
        gridBagConstraints.gridy = 2;
 
295
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
 
296
        gridBagConstraints.insets = new java.awt.Insets(18, 0, 0, 12);
 
297
        confPanel.add(bundle, gridBagConstraints);
 
298
 
 
299
        layer.setLabelFor(layerValue);
 
300
        org.openide.awt.Mnemonics.setLocalizedText(layer, org.openide.util.NbBundle.getMessage(BasicConfVisualPanel.class, "LBL_XMLLayer"));
 
301
        gridBagConstraints = new java.awt.GridBagConstraints();
 
302
        gridBagConstraints.gridx = 0;
 
303
        gridBagConstraints.gridy = 3;
 
304
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
 
305
        gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 12);
 
306
        confPanel.add(layer, gridBagConstraints);
 
307
 
 
308
        gridBagConstraints = new java.awt.GridBagConstraints();
 
309
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
 
310
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
 
311
        gridBagConstraints.weightx = 1.0;
 
312
        gridBagConstraints.insets = new java.awt.Insets(1, 0, 6, 0);
 
313
        confPanel.add(codeNameBaseValue, gridBagConstraints);
 
314
 
 
315
        gridBagConstraints = new java.awt.GridBagConstraints();
 
316
        gridBagConstraints.gridx = 1;
 
317
        gridBagConstraints.gridy = 1;
 
318
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
 
319
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
 
320
        confPanel.add(displayNameValue, gridBagConstraints);
 
321
 
 
322
        gridBagConstraints = new java.awt.GridBagConstraints();
 
323
        gridBagConstraints.gridx = 1;
 
324
        gridBagConstraints.gridy = 2;
 
325
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
 
326
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
 
327
        gridBagConstraints.insets = new java.awt.Insets(18, 0, 0, 0);
 
328
        confPanel.add(bundleValue, gridBagConstraints);
 
329
 
 
330
        gridBagConstraints = new java.awt.GridBagConstraints();
 
331
        gridBagConstraints.gridx = 1;
 
332
        gridBagConstraints.gridy = 3;
 
333
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
 
334
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
 
335
        gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 0);
 
336
        confPanel.add(layerValue, gridBagConstraints);
 
337
 
 
338
        gridBagConstraints = new java.awt.GridBagConstraints();
 
339
        gridBagConstraints.gridx = 0;
 
340
        gridBagConstraints.gridy = 4;
 
341
        gridBagConstraints.gridwidth = 2;
 
342
        gridBagConstraints.weighty = 1.0;
 
343
        confPanel.add(filler, gridBagConstraints);
 
344
 
 
345
        gridBagConstraints = new java.awt.GridBagConstraints();
 
346
        gridBagConstraints.gridx = 0;
 
347
        gridBagConstraints.gridy = 0;
 
348
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
 
349
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
 
350
        gridBagConstraints.weightx = 1.0;
 
351
        gridBagConstraints.weighty = 1.0;
 
352
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 0);
 
353
        add(confPanel, gridBagConstraints);
 
354
 
 
355
    }
 
356
    // </editor-fold>//GEN-END:initComponents
 
357
    
 
358
    // Variables declaration - do not modify//GEN-BEGIN:variables
 
359
    private javax.swing.JLabel bundle;
 
360
    private javax.swing.JTextField bundleValue;
 
361
    private javax.swing.JLabel codeNameBase;
 
362
    private javax.swing.JTextField codeNameBaseValue;
 
363
    private javax.swing.JPanel confPanel;
 
364
    private javax.swing.JLabel displayName;
 
365
    private javax.swing.JTextField displayNameValue;
 
366
    private javax.swing.JLabel filler;
 
367
    private javax.swing.JLabel layer;
 
368
    private javax.swing.JTextField layerValue;
 
369
    // End of variables declaration//GEN-END:variables
 
370
    
 
371
}