001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.xslt.mapper.palette;
020:
021: import java.awt.datatransfer.DataFlavor;
022: import java.awt.datatransfer.UnsupportedFlavorException;
023: import java.io.IOException;
024: import org.netbeans.modules.soa.mapper.common.basicmapper.methoid.IMethoid;
025: import org.netbeans.modules.soa.ui.StubPaletteActions;
026: import org.netbeans.modules.xslt.mapper.methoid.Constants;
027: import org.netbeans.modules.xslt.mapper.methoid.MethoidLoader;
028: import org.netbeans.spi.palette.DragAndDropHandler;
029: import org.netbeans.spi.palette.PaletteController;
030: import org.netbeans.spi.palette.PaletteFactory;
031: import org.openide.ErrorManager;
032: import org.openide.filesystems.FileObject;
033: import org.openide.filesystems.Repository;
034: import org.openide.loaders.DataObject;
035: import org.openide.nodes.Node;
036: import org.openide.util.Lookup;
037: import org.openide.util.datatransfer.ExTransferable;
038:
039: /**
040: *
041: * @author nk160297
042: */
043: public final class XsltPaletteFactory {
044:
045: private XsltPaletteFactory() {
046: }
047:
048: /**
049: * Creates a new xslt palette.
050: * @return a new xslt palette
051: */
052: public static PaletteController getPalette() {
053: if (ourPalette == null) {
054: try {
055: ourPalette = PaletteFactory.createPalette(
056: Constants.XSLT_PALETTE_FOLDER,
057: new StubPaletteActions(), null,
058: new MyDnDHandler());
059: } catch (IOException e) {
060: e.printStackTrace();
061: }
062: }
063: return ourPalette;
064: }
065:
066: public static class MyDnDHandler extends DragAndDropHandler {
067: public void customize(ExTransferable t, Lookup item) {
068: try {
069: DataFlavor mapperFlavor = new DataFlavor(
070: DataFlavor.javaJVMLocalObjectMimeType);
071: t
072: .put(new MapperPaletteTransferable(
073: mapperFlavor, item));
074: } catch (ClassNotFoundException ex) {
075: ErrorManager.getDefault().notify(ex);
076: }
077: //
078: // t.addTransferListener(new TransferListener() {
079: // public void accepted(int action) {
080: // }
081: // public void ownershipLost() {
082: // }
083: // public void rejected() {
084: // }
085: // });
086: }
087: }
088:
089: public static class MapperPaletteTransferable extends
090: ExTransferable.Single {
091:
092: private Lookup myPaletteItemLookup;
093:
094: public MapperPaletteTransferable(DataFlavor flavor, Lookup item) {
095: super (flavor);
096: myPaletteItemLookup = item;
097: }
098:
099: protected Object getData() throws IOException,
100: UnsupportedFlavorException {
101: Node paletteItemNode = (Node) myPaletteItemLookup
102: .lookup(Node.class);
103: if (paletteItemNode != null) {
104: DataObject dataObject = (DataObject) paletteItemNode
105: .getCookie(DataObject.class);
106: if (dataObject != null) {
107: FileObject fo = dataObject.getPrimaryFile();
108: if (fo != null) {
109: String metainfoRef = (String) fo
110: .getAttribute(Constants.METAINFO_REF);
111: if (metainfoRef != null
112: && metainfoRef.length() != 0) {
113: FileObject metainfoFo = Repository
114: .getDefault()
115: .getDefaultFileSystem()
116: .findResource(metainfoRef);
117: if (metainfoFo != null) {
118: IMethoid methoid = MethoidLoader
119: .loadMethoid(metainfoFo);
120: return methoid;
121: }
122: }
123: }
124: }
125: }
126: return null;
127: }
128: }
129:
130: private static PaletteController ourPalette;
131: }
|