建立org.netbeans.shapesample.palette包,並在其中船艦一下類:
/*
* Category.java
*
* Created on September 21, 2006, 9:00 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*
* To understand this class, see http://platform.netbeans.org/tutorials/nbm-nodesapi3.html
*/
package org.netbeans.shapesample.palette;
/**
*
* @author Geertjan Wielenga
*/
public class Category {
private String name;
/** Creates a new instance of Category */
public Category() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/*
* CategoryChildren.java
*
* Created on September 21, 2006, 9:00 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*
* To understand this class, see http://platform.netbeans.org/tutorials/nbm-nodesapi3.html
*/
package org.netbeans.shapesample.palette;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
/**
*
* @author Geertjan Wielenga
*/
public class CategoryChildren extends Children.Keys {
private String[] Categories = new String[]{
"Shapes"};
public CategoryChildren() {
}
protected Node[] createNodes(Object key) {
Category obj = (Category) key;
return new Node[] { new CategoryNode(obj) };
}
protected void addNotify() {
super.addNotify();
Category[] objs = new Category[Categories.length];
for (int i = 0; i < objs.length; i++) {
Category cat = new Category();
cat.setName(Categories[i]);
objs[i] = cat;
}
setKeys(objs);
}
}
/*
* CategoryNode.java
*
* Created on September 21, 2006, 9:02 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*
* To understand this class, see http://platform.netbeans.org/tutorials/nbm-nodesapi3.html
*/
package org.netbeans.shapesample.palette;
import org.openide.nodes.AbstractNode;
import org.openide.util.lookup.Lookups;
/**
*
* @author Geertjan Wielenga
*/
public class CategoryNode extends AbstractNode {
/** Creates a new instance of CategoryNode */
public CategoryNode( Category category ) {
super( new ShapeChildren(category), Lookups.singleton(category) );
setDisplayName(category.getName());
}
}
/*
* PaletteSupport.java
*
* Created on September 25, 2006, 2:22 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*
* To understand this class, see http://platform.netbeans.org/tutorials/nbm-nodesapi3.html
*/
package org.netbeans.shapesample.palette;
import java.awt.Image;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.beans.BeanInfo;
import java.io.IOException;
import javax.swing.Action;
import org.netbeans.spi.palette.DragAndDropHandler;
import org.netbeans.spi.palette.PaletteActions;
import org.netbeans.spi.palette.PaletteController;
import org.netbeans.spi.palette.PaletteFactory;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Node;
import org.openide.util.Lookup;
import org.openide.util.datatransfer.ExTransferable;
/**
*
* @author dave
*/
public class PaletteSupport {
public static PaletteController createPalette() {
AbstractNode paletteRoot = new AbstractNode(new CategoryChildren());
paletteRoot.setName("Palette Root");
return PaletteFactory.createPalette( paletteRoot, new MyActions(), null, new MyDnDHandler() );
}
private static class MyActions extends PaletteActions {
public Action[] getImportActions() {
return null;
}
public Action[] getCustomPaletteActions() {
return null;
}
public Action[] getCustomCategoryActions(Lookup lookup) {
return null;
}
public Action[] getCustomItemActions(Lookup lookup) {
return null;
}
public Action getPreferredAction(Lookup lookup) {
return null;
}
}
private static class MyDnDHandler extends DragAndDropHandler {
public void customize(ExTransferable exTransferable, Lookup lookup) {
Node node = lookup.lookup(Node.class);
final Image image = (Image) node.getIcon(BeanInfo.ICON_COLOR_16x16);
exTransferable.put(new ExTransferable.Single (DataFlavor.imageFlavor) {
protected Object getData() throws IOException, UnsupportedFlavorException {
return image;
}
});
}
}
}
/*
* Shape.java
*
* Created on September 21, 2006, 9:09 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*
* To understand this class, see http://platform.netbeans.org/tutorials/nbm-nodesapi3.html
*/
package org.netbeans.shapesample.palette;
/**
*
* @author Geertjan Wielenga
*/
public class Shape {
private Integer number;
private String category;
private String title;
private String image;
/** Creates a new instance of Instrument */
public Shape() {
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
/*
* ShapeChildren.java
*
* Created on September 21, 2006, 9:10 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*
* To understand this class, see http://platform.netbeans.org/tutorials/nbm-nodesapi3.html
*/
package org.netbeans.shapesample.palette;
import java.util.ArrayList;
import org.openide.nodes.Index;
import org.openide.nodes.Node;
/**
*
* @author Geertjan Wielenga
*/
public class ShapeChildren extends Index.ArrayChildren {
private Category category;
private String[][] items = new String[][]{
{"0", "Shapes", "org/netbeans/shapesample/palette/image1.png"},
{"1", "Shapes", "org/netbeans/shapesample/palette/image2.png"},
{"2", "Shapes", "org/netbeans/shapesample/palette/image3.png"},
};
public ShapeChildren(Category Category) {
this.category = Category;
}
protected java.util.List<Node> initCollection() {
ArrayList childrenNodes = new ArrayList( items.length );
for( int i=0; i<items.length; i++ ) {
if( category.getName().equals( items[i][1] ) ) {
Shape item = new Shape();
item.setNumber(new Integer(items[i][0]));
item.setCategory(items[i][1]);
item.setImage(items[i][2]);
childrenNodes.add( new ShapeNode( item ) );
}
}
return childrenNodes;
}
}
/*
* This is just a exercise to hoe to use JTable.
*/
package org.netbeans.shapesample.palette;
import java.awt.Image;
/**
*
* @author Vanessa <liyuan.li at Yunnan University>
* @version 1.0.0.0
*/
public class MyNode {
private Image image;
public MyNode(Image image) {
this.image = image;
}
public Image getImage() {
return image;
}
}
/*
* ShapeNode.java
*
* Created on September 21, 2006, 9:18 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*
* To understand this class, see http://platform.netbeans.org/tutorials/nbm-nodesapi3.html
*/
package org.netbeans.shapesample.palette;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.util.lookup.Lookups;
/**
*
* @author Geertjan Wielenga
*/
public class ShapeNode extends AbstractNode {
private Shape shape;
/** Creates a new instance of InstrumentNode */
public ShapeNode(Shape key) {
super(Children.LEAF, Lookups.fixed( new Object[] {key} ) );
this.shape = key;
setIconBaseWithExtension(key.getImage());
}
}