|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.form.palette; import java.awt.Image; import java.text.MessageFormat; import org.openide.*; import org.openide.actions.*; import org.openide.filesystems.FileObject; import org.openide.loaders.*; import org.openide.nodes.*; import org.openide.util.HelpCtx; import org.openide.util.Utilities; import org.openide.util.actions.SystemAction; import org.openide.util.datatransfer.NewType; /** * The root node representing the Component Palette content. * @author Ian Formanek, Tomas Pavek */ public final class PaletteNode extends FilterNode { /** Icon resources */ private static final String paletteIcon16Res = "org/netbeans/modules/form/resources/palette.gif"; // NOI18N private static final String paletteIcon32Res = "org/netbeans/modules/form/resources/palette32.gif"; // NOI18N private static final String categoryIcon16Res = "org/netbeans/modules/form/resources/paletteCategory.gif"; // NOI18N private static final String categoryIcon32Res = "org/netbeans/modules/form/resources/paletteCategory32.gif"; // NOI18N static final String CAT_NAME = "categoryName"; // NOI18N static final String CAT_HIDDEN = "hidden_category"; // NOI18N static final Node.PropertySet[] NO_PROPERTIES = new Node.PropertySet[0]; private static SystemAction[] staticActions; private static PaletteNode paletteNodeInstance; // singleton instance // -------- /** Public constructor - to be instantiated from layer */ public PaletteNode() { this(PaletteUtils.getPaletteDataFolder().getNodeDelegate()); } private PaletteNode(Node originalNode) { super(originalNode, new Children(originalNode)); setDisplayName(CPManager.getBundle().getString("CTL_Component_palette")); // NOI18N } static PaletteNode getPaletteNode() { if (paletteNodeInstance == null) paletteNodeInstance = new PaletteNode(); return paletteNodeInstance; } // -------- public Image getIcon(int type) { return Utilities.loadImage(type == java.beans.BeanInfo.ICON_COLOR_32x32 || type == java.beans.BeanInfo.ICON_MONO_32x32 ? paletteIcon32Res : paletteIcon16Res); } public Image getOpenedIcon(int type) { return getIcon(type); } public NewType[] getNewTypes() { return new NewType[] { new NewCategory() }; } public SystemAction[] getActions() { if (staticActions == null) staticActions = new SystemAction [] { SystemAction.get(FileSystemAction.class), null, SystemAction.get(ReorderAction.class), null, // PasteAction yes or not? // SystemAction.get(PasteAction.class), // null, SystemAction.get(NewAction.class), }; return staticActions; } public Node.PropertySet[] getPropertySets() { return NO_PROPERTIES; } public HelpCtx getHelpCtx() { return new HelpCtx("gui.options.component-palette"); // NOI18N } // ------------ void createNewCategory() throws java.io.IOException { java.util.ResourceBundle bundle = CPManager.getBundle(); NotifyDescriptor.InputLine input = new NotifyDescriptor.InputLine( bundle.getString("CTL_NewCategoryName"), // NOI18N bundle.getString("CTL_NewCategoryTitle")); // NOI18N input.setInputText(bundle.getString("CTL_NewCategoryValue")); // NOI18N while (DialogDisplayer.getDefault().notify(input) == NotifyDescriptor.OK_OPTION) { String categoryName = input.getInputText(); if (checkCategoryName(categoryName, null)) { String folderName = convertCategoryToFolderName(categoryName, null); FileObject folder = PaletteUtils.getPaletteFolder().createFolder(folderName); if (!folderName.equals(categoryName)) folder.setAttribute(CAT_NAME, categoryName); break; } } } /** Checks category name if it is valid and if there's already not * a category with the same name. * @param name name to be checked * @param namedNode node which name is checked or null if it doesn't exist yet * @return true if the name is OK */ private static boolean checkCategoryName(String name, Node namedNode) { boolean invalid = false; if (name == null || "".equals(name)) // NOI18N invalid = true; else // name should not start with . or contain only spaces for (int i=0, n=name.length(); i < n; i++) { char ch = name.charAt(i); if (ch == '.' || (ch == ' ' && i+1 == n)) { invalid = true; break; } else if (ch != ' ') break; } if (invalid) { DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message(MessageFormat.format( CPManager.getBundle().getString("ERR_InvalidName"), // NOI18N new Object[] { name }), NotifyDescriptor.INFORMATION_MESSAGE)); return false; } Node[] nodes = getPaletteNode().getChildren().getNodes(); for (int i=0; i < nodes.length; i++) if (name.equals(nodes[i].getName()) && nodes[i] != namedNode) { DialogDisplayer.getDefault().notify( new NotifyDescriptor.Message(MessageFormat.format( CPManager.getBundle().getString("FMT_CategoryExists"), // NOI18N new Object[] { name }), NotifyDescriptor.INFORMATION_MESSAGE)); return false; } return true; } /** Converts category name to name that can be used as name of folder * for the category (restricted even to package name). */ private static String convertCategoryToFolderName(String name, String currentName) { if (name == null || "".equals(name)) // NOI18N return null; int i; int n = name.length(); StringBuffer nameBuff = new StringBuffer(n); char ch = name.charAt(0); if (Character.isJavaIdentifierStart(ch)) { nameBuff.append(ch); i = 1; } else { nameBuff.append('_'); i = 0; } while (i < n) { ch = name.charAt(i); if (Character.isJavaIdentifierPart(ch)) nameBuff.append(ch); i++; } String fName = nameBuff.toString(); if ("_".equals(fName)) // NOI18N fName = "Category"; // NOI18N if (fName.equals(currentName)) return fName; // having the base name, make sure it is not used yet FileObject paletteFO = PaletteUtils.getPaletteFolder(); String freeName = null; boolean nameOK = false; for (i=0; !nameOK; i++) { freeName = i > 0 ? fName + "_" + i : fName; // NOI18N if (Utilities.isWindows()) { nameOK = true; java.util.Enumeration en = paletteFO.getChildren(false); while (en.hasMoreElements()) { FileObject fo = (FileObject)en.nextElement(); String fn = fo.getName(); String fe = fo.getExt(); // case-insensitive on Windows if ((fe == null || "".equals(fe)) && fn.equalsIgnoreCase(freeName)) { // NOI18N nameOK = false; break; } } } else nameOK = paletteFO.getFileObject(freeName) == null; } return freeName; } // -------------- /** Children for the PaletteNode. Creates PaletteCategoryNode instances * as filter subnodes. */ private static class Children extends FilterNode.Children { public Children(Node original) { super(original); } protected Node copyNode(Node node) { DataFolder df = (DataFolder) node.getCookie(DataFolder.class); if (df != null) return new PaletteCategoryNode(df); return node.cloneNode(); } } // ------- private static class PaletteCategoryNode extends FilterNode { private static SystemAction[] staticActions; private DataFolder folder; // -------- PaletteCategoryNode(DataFolder folder) { super(folder.getNodeDelegate()); this.folder = folder; Object catName = folder.getPrimaryFile().getAttribute(CAT_NAME); if (catName instanceof String) setName((String)catName, false); } public boolean equals(Object o) { // this is needed so Index.indexOf works properly if (!(o instanceof Node)) return false; DataObject do1 = (DataObject) getCookie(DataObject.class); DataObject do2 = (DataObject) ((Node)o).getCookie(DataObject.class); return do1.equals(do2); } // ------- public void setName(String name) { setName(name, true); } public void setName(String name, boolean rename) { if (rename) { if (!checkCategoryName(name, this)) return; // invalid name try { FileObject fo = folder.getPrimaryFile(); String folderName = convertCategoryToFolderName(name, fo.getName()); fo.setAttribute(CAT_NAME, null); folder.rename(folderName); if (!folderName.equals(name)) fo.setAttribute(CAT_NAME, name); } catch (java.io.IOException ex) { RuntimeException e = new IllegalArgumentException(); org.openide.ErrorManager.getDefault().annotate(e, ex); throw e; } } super.setName(name); } public String getDisplayName () { // just add "(Hidden)" suffix if the category is hidden String name = super.getDisplayName(); if (Boolean.TRUE.equals(folder.getPrimaryFile().getAttribute(CAT_HIDDEN))) name = MessageFormat.format( CPManager.getBundle().getString("FMT_HiddenCategory"), // NOI18N new Object[] { name }); return name; } public Image getIcon(int type) { return Utilities.loadImage(type == java.beans.BeanInfo.ICON_COLOR_32x32 || type == java.beans.BeanInfo.ICON_MONO_32x32 ? categoryIcon32Res : categoryIcon16Res); } public Image getOpenedIcon(int type) { return getIcon(type); } public SystemAction[] getActions() { if (staticActions == null) staticActions = new SystemAction [] { SystemAction.get(FileSystemAction.class), null, SystemAction.get(MoveUpAction.class), SystemAction.get(MoveDownAction.class), SystemAction.get(ReorderAction.class), null, SystemAction.get(PasteAction.class), null, SystemAction.get(DeleteAction.class), SystemAction.get(RenameAction.class), }; return staticActions; } public Node.PropertySet[] getPropertySets() { return NO_PROPERTIES; } public HelpCtx getHelpCtx() { return new HelpCtx("gui.options.component-palette"); // NOI18N } } // ------- /** * New type for creation of new palette category. */ private final class NewCategory extends NewType { public String getName() { return CPManager.getBundle().getString("CTL_NewCategory"); // NOI18N } public HelpCtx getHelpCtx() { return new HelpCtx(NewCategory.class); } public void create() throws java.io.IOException { PaletteNode.this.createNewCategory(); } } } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.