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-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: * AddResourceOverwriteDialog.java
043: *
044: * Created on June 2, 2004, 8:08 PM
045: */
046:
047: package org.netbeans.modules.visualweb.project.jsf.api;
048:
049: import java.awt.Dialog;
050: import java.io.File;
051: import javax.swing.JButton;
052: import javax.swing.JRadioButton;
053: import org.openide.DialogDescriptor;
054: import org.openide.DialogDisplayer;
055: import org.openide.NotifyDescriptor;
056: import org.openide.filesystems.FileUtil;
057: import org.openide.util.NbBundle;
058:
059: /**
060: *
061: * @author dey
062: */
063: public class AddResourceOverwriteDialog extends javax.swing.JPanel {
064: private File target = null;
065: private File newTarget = null;
066: private File fileReturn = null;
067:
068: /** Mode flag which indicates that the dialog will ask whether to overwrite or
069: * create new when there is a file import conflict */
070: public static final int CONFLICT_ASK = 0;
071:
072: /** Mode flag which indicates that the dialog will ask whether to overwrite or
073: * create new when there is a file import conflict, AND in addition ask if
074: * the user wants to always create or always replace (a "Yes To All" mechanism).
075: * Use this only when you'return creating multiple resources.
076: *
077: * When using this be aware that once the user has used Always/Never, the mode
078: * will change, so you should set it back to CONFLICT_ASK when you're done
079: * with the batch operation.
080: */
081: public static final int CONFLICT_ASK_MANY = 1;
082:
083: /** Mode flag which indicates that the user always wants to replace, so take
084: * this answer as a given without asking the user.
085: */
086: public static final int CONFLICT_OVERWRITE = 2;
087:
088: /** Mode flag which indicates that the user always wants to use the existing
089: * resource, so take this answer as a given without asking the user
090: */
091: public static final int CONFLICT_USE_EXISTING = 3;
092:
093: /** Mode flag which indicates that the user always wants to create a new file
094: * rather than replace, so take this answer as a given without asking the user.
095: */
096: public static final int CONFLICT_CREATE_NEW = 4;
097:
098: /** <p>
099: * Operation for the AddResourceOverwriteDialog which determines whether
100: * the dialog will show "Always/Never" buttons, or if the dialog will even
101: * be shown at all (when the answer is already set in the flag). The default
102: * value is CONFLICT_ASK.
103: * </p>
104: * <p>
105: * Use this mode when you're going to be adding lots of resources
106: * in batch; if so you'll want to set the mode to "CONFLICT_ASK_MANY" originally
107: * and when you'return done, set it back to "CONFLICT_ASK". Once the user
108: * has answered for example "Always" in the dialog, the mode is changed to
109: * a particular answer which causes the dialog to just bail out immediately
110: * with getFile() returning the desired answer without user intervention.
111: * </p>
112: */
113: private static int mode = CONFLICT_ASK;
114:
115: /** Creates new form AddResourceOverwriteDialog */
116: public AddResourceOverwriteDialog(File target) {
117: this .target = target;
118: initComponents();
119: initComponents2();
120: }
121:
122: public static int getMode() {
123: return mode;
124: }
125:
126: public static void setMode(int value) {
127: mode = value;
128: }
129:
130: public void showDialog() {
131: if (mode == CONFLICT_OVERWRITE || mode == CONFLICT_USE_EXISTING) {
132: // PENDING: how does client distinguish between these two cases?
133: fileReturn = target;
134: return;
135: } else if (mode == CONFLICT_CREATE_NEW) {
136: fileReturn = newTarget;
137: return;
138: }
139:
140: DialogDescriptor desc = new DialogDescriptor(this , NbBundle
141: .getMessage(AddResourceOverwriteDialog.class,
142: "LBL_ResourceNameConflict"));
143: desc.setMessageType(NotifyDescriptor.WARNING_MESSAGE);
144: desc.setOptionType(NotifyDescriptor.OK_CANCEL_OPTION);
145: JButton alwaysButton = null;
146: if (mode == CONFLICT_ASK_MANY) {
147: alwaysButton = new JButton(NbBundle.getMessage(
148: AddResourceOverwriteDialog.class, "LBL_Always"));
149: desc.setAdditionalOptions(new Object[] { alwaysButton });
150: }
151: Dialog dlg = DialogDisplayer.getDefault().createDialog(desc);
152: dlg.show();
153: Object value = desc.getValue();
154: if (value != NotifyDescriptor.OK_OPTION
155: && value != alwaysButton) {
156: fileReturn = null;
157: return;
158: }
159: if (renameButton.isSelected()) {
160: fileReturn = newTarget;
161: if (value == alwaysButton) {
162: mode = CONFLICT_CREATE_NEW;
163: }
164: } else if (overwriteButton.isSelected()) {
165: boolean wasDeleted = target.delete();
166: fileReturn = target;
167: if (value == alwaysButton) {
168: mode = CONFLICT_OVERWRITE;
169: }
170: } else if (useExistingButton.isSelected()) {
171: fileReturn = target;
172: if (value == alwaysButton) {
173: mode = CONFLICT_USE_EXISTING;
174: }
175: }
176: }
177:
178: public File getFile() {
179: return fileReturn;
180: }
181:
182: private void initComponents2() {
183: newTarget = findUniqueFile(target);
184: label.setText(NbBundle.getMessage(
185: AddResourceOverwriteDialog.class, "LBL_AddResourceMsg",
186: target.getName()));
187: renameButton.setText(NbBundle.getMessage(
188: AddResourceOverwriteDialog.class,
189: "LBL_AddResourceRename", newTarget.getName()));
190: overwriteButton.setText(NbBundle.getMessage(
191: AddResourceOverwriteDialog.class,
192: "LBL_AddResourceOverwrite"));
193: useExistingButton.setText(NbBundle.getMessage(
194: AddResourceOverwriteDialog.class,
195: "LBL_AddResourceUseExisting"));
196: renameButton.setSelected(false);
197: overwriteButton.setSelected(false);
198: useExistingButton.setSelected(true);
199: }
200:
201: private File findUniqueFile(File old) {
202: int count = 1;
203: String fileName = old.getName();
204: String extension = FileUtil.getExtension(fileName);
205: String baseName = fileName.substring(0, fileName
206: .lastIndexOf(extension) - 1);
207: String testName = fileName;
208: File dir = old.getParentFile();
209: File newFile = null;
210: while (newFile == null) {
211: newFile = new File(dir, testName);
212: if (!newFile.exists())
213: return newFile;
214: newFile = null;
215: testName = baseName + "_" + count++ + "." + extension; // NOI18N
216: }
217: return newFile;
218: }
219:
220: /** This method is called from within the constructor to
221: * initialize the form.
222: * WARNING: Do NOT modify this code. The content of this method is
223: * always regenerated by the Form Editor.
224: */
225: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
226: private void initComponents() {
227: java.awt.GridBagConstraints gridBagConstraints;
228:
229: buttonGroup1 = new javax.swing.ButtonGroup();
230: jPanel1 = new javax.swing.JPanel();
231: useExistingButton = new javax.swing.JRadioButton();
232: renameButton = new javax.swing.JRadioButton();
233: overwriteButton = new javax.swing.JRadioButton();
234: label = new javax.swing.JLabel();
235:
236: setLayout(new java.awt.GridBagLayout());
237:
238: jPanel1.setLayout(new java.awt.GridLayout(0, 1));
239:
240: buttonGroup1.add(useExistingButton);
241: useExistingButton.setSelected(true);
242: useExistingButton
243: .setText("Use existing file instead of the selected file");
244: jPanel1.add(useExistingButton);
245:
246: buttonGroup1.add(renameButton);
247: renameButton.setText("Rename existing file to <>");
248: jPanel1.add(renameButton);
249:
250: buttonGroup1.add(overwriteButton);
251: overwriteButton
252: .setText("Overwrite existing file with the selected file");
253: jPanel1.add(overwriteButton);
254:
255: gridBagConstraints = new java.awt.GridBagConstraints();
256: gridBagConstraints.gridx = 0;
257: gridBagConstraints.gridy = 1;
258: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
259: gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
260: add(jPanel1, gridBagConstraints);
261:
262: label
263: .setText("The file <filename> already exists in the Resources folder. ");
264: gridBagConstraints = new java.awt.GridBagConstraints();
265: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
266: gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
267: add(label, gridBagConstraints);
268:
269: }
270:
271: // </editor-fold>//GEN-END:initComponents
272:
273: // Variables declaration - do not modify//GEN-BEGIN:variables
274: private javax.swing.ButtonGroup buttonGroup1;
275: private javax.swing.JPanel jPanel1;
276: private javax.swing.JLabel label;
277: private javax.swing.JRadioButton overwriteButton;
278: private javax.swing.JRadioButton renameButton;
279: private javax.swing.JRadioButton useExistingButton;
280: // End of variables declaration//GEN-END:variables
281:
282: }
|