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:
042: package org.netbeans.modules.project.ui;
043:
044: import java.awt.Dialog;
045: import java.awt.event.ActionEvent;
046: import java.awt.event.ActionListener;
047: import java.io.File;
048: import java.io.IOException;
049: import java.util.ArrayList;
050: import javax.swing.JButton;
051: import javax.swing.event.DocumentListener;
052: import org.openide.DialogDescriptor;
053: import org.openide.DialogDisplayer;
054: import org.openide.filesystems.FileObject;
055: import org.openide.filesystems.FileUtil;
056: import org.openide.filesystems.Repository;
057: import org.openide.loaders.DataObject;
058: import org.openide.loaders.DataFolder;
059: import org.openide.loaders.DataObjectNotFoundException;
060: import org.openide.util.NbBundle;
061:
062: /**
063: *
064: * @author phrebejk
065: */
066: public class NoProjectNew extends javax.swing.JPanel implements
067: ActionListener, DocumentListener {
068:
069: public static final int TYPE_FILE = 0;
070: public static final int TYPE_FOLDER = 1;
071:
072: private static final String FILE_NAME = NbBundle.getMessage(
073: NoProjectNew.class, "LBL_NonProject_File_Name"); // NOI18N
074: private static final String FILE_TITLE = NbBundle.getMessage(
075: NoProjectNew.class, "LBL_NonProject_File_Title"); // NOI18N;
076: private static final String FILE_TEXT_FIELD_ACD = NbBundle
077: .getMessage(NoProjectNew.class,
078: "ACD_NonProject_File_TextField"); // NOI18N;
079: private static final String FILE_DIALOG_ACD = NbBundle.getMessage(
080: NoProjectNew.class, "ACD_NonProject_File_Dialog"); // NOI18N;
081: private static final String FOLDER_NAME = NbBundle.getMessage(
082: NoProjectNew.class, "LBL_NonProject_Folder_Name"); // NOI18N;
083: private static final String FOLDER_TITLE = NbBundle.getMessage(
084: NoProjectNew.class, "LBL_NonProject_Folder_Title"); // NOI18N;
085: private static final String FOLDER_TEXT_FIELD_ACD = NbBundle
086: .getMessage(NoProjectNew.class,
087: "ACD_NonProject_Folder_TextField"); // NOI18N;
088: private static final String FOLDER_DIALOG_ACD = NbBundle
089: .getMessage(NoProjectNew.class,
090: "ACD_NonProject_Folder_Dialog"); // NOI18N;
091:
092: public static final String COMMAND_OK = "OK";
093: public static final String COMMAND_CANCEL = "CANCEL";
094:
095: private static DataObject[] templates;
096:
097: private int type;
098: private DataFolder targetFolder;
099: private String result;
100: private JButton okOption;
101:
102: /** Creates new form BrowseFolders */
103: public NoProjectNew(int type, DataFolder targetFolder,
104: JButton okOption) {
105: initComponents();
106: nameTextField.getDocument().addDocumentListener(this );
107:
108: this .type = type;
109: this .targetFolder = targetFolder;
110: this .okOption = okOption;
111:
112: switch (type) {
113: case TYPE_FILE:
114: org.openide.awt.Mnemonics.setLocalizedText(nameLabel,
115: FILE_NAME);
116: nameTextField.getAccessibleContext()
117: .setAccessibleDescription(FILE_TEXT_FIELD_ACD);
118: getAccessibleContext().setAccessibleDescription(
119: FILE_DIALOG_ACD);
120: break;
121: case TYPE_FOLDER:
122: org.openide.awt.Mnemonics.setLocalizedText(nameLabel,
123: FOLDER_NAME);
124: nameTextField.getAccessibleContext()
125: .setAccessibleDescription(FOLDER_TEXT_FIELD_ACD);
126: getAccessibleContext().setAccessibleDescription(
127: FOLDER_DIALOG_ACD);
128: break;
129: }
130:
131: this .okOption.setEnabled(false);
132:
133: }
134:
135: /** This method is called from within the constructor to
136: * initialize the form.
137: * WARNING: Do NOT modify this code. The content of this method is
138: * always regenerated by the Form Editor.
139: */
140: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
141: private void initComponents() {
142: java.awt.GridBagConstraints gridBagConstraints;
143:
144: nameLabel = new javax.swing.JLabel();
145: nameTextField = new javax.swing.JTextField();
146:
147: setLayout(new java.awt.GridBagLayout());
148:
149: setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12,
150: 12, 12));
151: nameLabel.setLabelFor(nameTextField);
152: nameLabel.setText("Folders:");
153: gridBagConstraints = new java.awt.GridBagConstraints();
154: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
155: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
156: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 6);
157: add(nameLabel, gridBagConstraints);
158:
159: nameTextField.setColumns(25);
160: gridBagConstraints = new java.awt.GridBagConstraints();
161: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
162: gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
163: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
164: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
165: gridBagConstraints.weightx = 1.0;
166: add(nameTextField, gridBagConstraints);
167:
168: }
169:
170: // </editor-fold>//GEN-END:initComponents
171:
172: // Variables declaration - do not modify//GEN-BEGIN:variables
173: public javax.swing.JLabel nameLabel;
174: public javax.swing.JTextField nameTextField;
175:
176: // End of variables declaration//GEN-END:variables
177:
178: public static void showDialog(DataObject template,
179: DataFolder targetFolder) {
180:
181: int type;
182: if (template.getPrimaryFile().getName().equals("file")) {
183: type = TYPE_FILE;
184: } else {
185: type = TYPE_FOLDER;
186: }
187:
188: JButton options[] = new JButton[] {
189: new JButton(NbBundle.getMessage(NoProjectNew.class,
190: "LBL_NonProject_OK_Button")), // NOI18N
191: new JButton(NbBundle.getMessage(NoProjectNew.class,
192: "LBL_NonProject_Cancel_Button")), // NOI18N
193: };
194:
195: NoProjectNew npn = new NoProjectNew(type, targetFolder,
196: options[0]);
197:
198: options[0].setActionCommand(COMMAND_OK);
199: options[0].addActionListener(npn);
200: options[1].setActionCommand(COMMAND_CANCEL);
201: options[1].addActionListener(npn);
202:
203: DialogDescriptor dialogDescriptor = new DialogDescriptor(npn, // innerPane
204: type == TYPE_FILE ? FILE_TITLE : FOLDER_TITLE, // displayName
205: true, // modal
206: options, // options
207: options[0], // initial value
208: DialogDescriptor.BOTTOM_ALIGN, // options align
209: null, // helpCtx
210: null); // listener
211:
212: dialogDescriptor.setClosingOptions(new Object[] { options[0],
213: options[1] });
214:
215: Dialog dialog = DialogDisplayer.getDefault().createDialog(
216: dialogDescriptor);
217: dialog.setVisible(true);
218: npn.createFile();
219:
220: }
221:
222: public static DataObject[] getTemplates() {
223:
224: if (templates == null) {
225:
226: ArrayList<DataObject> tList = new ArrayList<DataObject>(2);
227: DataObject template;
228:
229: template = findTemplate("Templates/Other/file");
230: if (template != null) {
231: tList.add(template);
232: }
233:
234: template = findTemplate("Templates/Other/Folder");
235: if (template != null) {
236: tList.add(template);
237: }
238:
239: templates = new DataObject[tList.size()];
240: tList.toArray(templates);
241: }
242: return templates;
243: }
244:
245: // ActionListener implementation -------------------------------------------
246:
247: public void actionPerformed(ActionEvent e) {
248: result = COMMAND_OK.equals(e.getActionCommand()) ? getFileName()
249: : null;
250: }
251:
252: // Document listener implementation ----------------------------------------
253:
254: public void insertUpdate(javax.swing.event.DocumentEvent e) {
255: changedUpdate(e);
256: }
257:
258: public void removeUpdate(javax.swing.event.DocumentEvent e) {
259: changedUpdate(e);
260: }
261:
262: public void changedUpdate(javax.swing.event.DocumentEvent e) {
263:
264: String fileName = getFileName();
265:
266: if (fileName.length() == 0) {
267: okOption.setEnabled(false);
268: return;
269: }
270:
271: FileObject fo = targetFolder.getPrimaryFile().getFileObject(
272: fileName);
273: if (fo != null) {
274: okOption.setEnabled(false);
275: return;
276: }
277:
278: okOption.setEnabled(true);
279: }
280:
281: // Private methods ---------------------------------------------------------
282:
283: private static DataObject findTemplate(String name) {
284: FileObject tFo = Repository.getDefault().getDefaultFileSystem()
285: .findResource(name);
286: if (tFo == null) {
287: return null;
288: }
289: try {
290: return DataObject.find(tFo);
291: } catch (DataObjectNotFoundException e) {
292: return null;
293: }
294:
295: }
296:
297: private String getFileName() {
298: String name = nameTextField.getText().trim();
299: return name.replace(File.separatorChar, '/'); // NOI18N
300: }
301:
302: private void createFile() {
303: if (result != null) {
304:
305: if (!targetFolder.getPrimaryFile().canWrite()) {
306: return;
307: }
308:
309: DataObject dObj = null;
310:
311: try {
312: FileObject fo = type == TYPE_FILE ? FileUtil
313: .createData(targetFolder.getPrimaryFile(),
314: result) : FileUtil.createFolder(
315: targetFolder.getPrimaryFile(), result);
316: if (fo != null) {
317: dObj = DataObject.find(fo);
318: }
319: } catch (DataObjectNotFoundException e) {
320: // No data object no open
321: } catch (IOException e) {
322: // XXX
323: }
324:
325: if (result != null) {
326:
327: // handle new template in SystemFileSystem
328: DataObject rootDO = findTemplate("/Templates"); // NOI18N
329: if (rootDO != null && dObj != null) {
330: if (FileUtil.isParentOf(rootDO.getPrimaryFile(),
331: dObj.getPrimaryFile())) {
332: try {
333: dObj.setTemplate(true);
334: } catch (IOException e) {
335: // can ignore
336: }
337: }
338: }
339:
340: ProjectUtilities.openAndSelectNewObject(dObj);
341: }
342: }
343: }
344:
345: }
|