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: * CodeClipPaletteActions.java
043: *
044: * Created on August 8, 2006, 5:35 PM
045: *
046: * Adds additional actions to the Netbeans Palette Actions.
047: *
048: * @author Joelle Lam <joelle.lam@sun.com>
049: * @date 08/20/2006
050: */
051:
052: package org.netbeans.modules.visualweb.palette.api;
053:
054: import org.netbeans.modules.visualweb.palette.codeclips.CodeClipItemNode;
055: import org.netbeans.modules.visualweb.palette.codeclips.CodeClipUtilities;
056: import java.awt.event.ActionEvent;
057: import java.io.File;
058: import javax.swing.AbstractAction;
059: import javax.swing.Action;
060: import javax.swing.text.JTextComponent;
061: import org.netbeans.spi.palette.PaletteActions;
062: import org.openide.actions.NewAction;
063: import org.openide.actions.RenameAction;
064: import org.openide.loaders.DataFolder;
065: import org.openide.text.CloneableEditor;
066: import org.openide.util.Lookup;
067: import org.openide.util.NbBundle;
068: import org.openide.util.actions.SystemAction;
069:
070: public class CodeClipPaletteActions extends PaletteActions {
071: private String paletteFolderName;
072: private CloneableEditor cloneableEditor;
073:
074: /*
075: * Creates a new instance of FormPaletteProvider
076: * @param paletteFolderName string that contains the palette folder name
077: */
078: public CodeClipPaletteActions(String paletteFolderName,
079: CloneableEditor cloneableEditor) {
080: this .paletteFolderName = paletteFolderName;
081: this .cloneableEditor = cloneableEditor;
082: }
083:
084: public Action[] getImportActions() {
085: return new Action[] {
086: //new ResetPaletteAction(paletteFolderName)
087: };
088: }
089:
090: public Action[] getCustomCategoryActions(Lookup category) {
091: return new Action[] { new CreateCodeClipAction(category)
092: // I can not use the NewAction because I would need to overwrite getTypes in the Category class written by Netbeans.
093: // ((NewAction)SystemAction.get(NewAction.class))
094: };
095: }
096:
097: public Action[] getCustomItemActions(Lookup item) {
098: return new Action[] {
099: SystemAction.get(RenameAction.class)
100: .createContextAwareInstance(item),
101: new EditCodeClipAction(item)
102: // ((EditAction)SystemAction.get(EditAction.class)).createContextAwareInstance(item)
103: };
104: }
105:
106: public Action[] getCustomPaletteActions() {
107: return new Action[] { SystemAction.get(NewAction.class) };
108: }
109:
110: /*
111: * Called when user double clicks. Double click a palette item should
112: * insert a new item.
113: *
114: * @param item The lookup for the item clicked.
115: * @return action The action to be called when an item is double clicked. In this case, insert action.
116: */
117: public Action getPreferredAction(Lookup item) {
118: return new CodeClipPaletteInsertAction(item, cloneableEditor);
119: }
120:
121: /** No longer necessary since the Palette API now provides this in the manager.
122: * Reset Palette
123: private static class ResetPaletteAction extends AbstractAction {
124: String folderName;
125:
126: ResetPaletteAction(String folderName) {
127: super( NbBundle.getMessage(CodeClipPaletteActions.class, "RESET"));
128: this.folderName = folderName;
129: }
130:
131: public void actionPerformed(ActionEvent e) {
132: String msg;
133: FileSystem fs = Repository.getDefault().getDefaultFileSystem();
134: FileObject paletteFileObject = fs.findResource( folderName );
135: FileObject paletteParent = paletteFileObject.getParent();
136:
137: String nbUserDir = System.getProperty("netbeans.user");
138: String separator = new File(nbUserDir).separator;
139: File userPaletteFolder = new File(nbUserDir + separator + "config" + separator + paletteFileObject.getPath());
140:
141: if ( !userPaletteFolder.isDirectory() ){
142: msg = NbBundle.getMessage(CodeClipPaletteActions.class, "MSG_NoUserModifications");
143: DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(msg, NotifyDescriptor.ERROR_MESSAGE));
144: return;
145: }
146:
147: msg = NbBundle.getMessage(CodeClipPaletteActions.class, "MSG_OkayToDelete");
148: // Object option = DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(msg, NotifyDescriptor.OK_CANCEL_OPTION));
149:
150: NotifyDescriptor d =new NotifyDescriptor.Confirmation(msg, NotifyDescriptor.OK_CANCEL_OPTION);
151:
152: if (DialogDisplayer.getDefault().notify(d) != NotifyDescriptor.OK_OPTION ){
153: return;
154: }
155:
156: boolean success = deleteDir(userPaletteFolder);
157: if( !success ) {
158: msg = NbBundle.getMessage(CodeClipPaletteActions.class, "MSG_ErrorUnableToDeleteUserPaletteFolder");
159: DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(msg, NotifyDescriptor.ERROR_MESSAGE));
160: return;
161: }
162: //It is insufficient to refresh paletteFileObject since itself has been removed from the user dir.
163: paletteParent.refresh();
164:
165: }
166: }
167: */
168:
169: // Deletes all files and subdirectories under dir.
170: // Returns true if all deletions were successful.
171: // If a deletion fails, the method stops attempting to delete and returns false.
172: public static boolean deleteDir(File dir) {
173: if (dir.isDirectory()) {
174: String[] children = dir.list();
175: for (int i = 0; i < children.length; i++) {
176: boolean success = deleteDir(new File(dir, children[i]));
177: if (!success) {
178: return false;
179: }
180: }
181: }
182:
183: // The directory is now empty so delete it
184: return dir.delete();
185: }
186:
187: /**
188: * Create a code clip
189: */
190: private static class CreateCodeClipAction extends AbstractAction {
191: Lookup category;
192:
193: CreateCodeClipAction(Lookup category) {
194: super (NbBundle.getMessage(CodeClipPaletteActions.class,
195: "ADD"));
196: this .category = category;
197: }
198:
199: public void actionPerformed(ActionEvent e) {
200: DataFolder dataFolder = (DataFolder) category
201: .lookup(DataFolder.class);
202: CodeClipUtilities.createCodeClip(dataFolder);
203: }
204:
205: }
206:
207: private static class EditCodeClipAction extends AbstractAction {
208: Lookup item;
209:
210: EditCodeClipAction(Lookup item) {
211: super (NbBundle.getMessage(CodeClipPaletteActions.class,
212: "EDIT"));
213: this .item = item;
214: }
215:
216: public void actionPerformed(ActionEvent e) {
217: CodeClipItemNode ccNode = (CodeClipItemNode) item
218: .lookup(CodeClipItemNode.class);
219: ccNode.edit();
220: return;
221:
222: }
223: }
224:
225: /**
226: * Inserts a palette item
227: */
228: private static class CodeClipPaletteInsertAction extends
229: AbstractAction {
230:
231: Lookup item;
232: CloneableEditor cloneableEditor;
233:
234: CodeClipPaletteInsertAction(Lookup item,
235: CloneableEditor cloneableEditor) {
236: this .item = item;
237: this .cloneableEditor = cloneableEditor;
238: }
239:
240: public void actionPerformed(ActionEvent e) {
241: CodeClipItemNode ccNode = (CodeClipItemNode) item
242: .lookup(CodeClipItemNode.class);
243: ccNode.drop((JTextComponent) cloneableEditor
244: .getEditorPane());
245:
246: }
247: }
248:
249: }
|