1 19 20 21 package org.netbeans.spi.palette; 22 import java.awt.datatransfer.DataFlavor ; 23 import java.awt.datatransfer.Transferable ; 24 import java.awt.datatransfer.UnsupportedFlavorException ; 25 import java.io.IOException ; 26 import org.netbeans.modules.palette.DefaultModel; 27 import org.openide.ErrorManager; 28 import org.openide.nodes.Index; 29 import org.openide.nodes.Node; 30 import org.openide.util.Lookup; 31 import org.openide.util.datatransfer.ExTransferable; 32 import org.openide.util.datatransfer.PasteType; 33 44 public abstract class DragAndDropHandler { 45 46 private static DragAndDropHandler defaultHandler; 47 48 static DragAndDropHandler getDefault() { 49 if( null == defaultHandler ) 50 defaultHandler = new DefaultDragAndDropHandler(); 51 return defaultHandler; 52 } 53 54 62 public abstract void customize( ExTransferable t, Lookup item ); 63 64 71 public boolean canDrop( Lookup targetCategory, DataFlavor [] flavors, int dndAction ) { 72 for( int i=0; i<flavors.length; i++ ) { 73 if( PaletteController.ITEM_DATA_FLAVOR.equals( flavors[i] ) ) { 74 return true; 75 } 76 } 77 return false; 78 } 79 80 90 public boolean doDrop( Lookup targetCategory, Transferable item, int dndAction, int dropIndex ) { 91 Node categoryNode = (Node)targetCategory.lookup( Node.class ); 92 try { 93 if( item.isDataFlavorSupported( PaletteController.ITEM_DATA_FLAVOR ) ) { 95 Lookup itemLookup = (Lookup)item.getTransferData( PaletteController.ITEM_DATA_FLAVOR ); 96 if( null != itemLookup ) { 97 Node itemNode = (Node)itemLookup.lookup( Node.class ); 98 if( null != itemNode ) { 99 Index order = (Index)categoryNode.getCookie( Index.class ); 100 if( null != order && order.indexOf( itemNode ) >= 0 ) { 101 return moveItem( targetCategory, itemLookup, dropIndex ); 104 } 105 } 106 } 107 } 108 PasteType paste = categoryNode.getDropType( item, dndAction, dropIndex ); 109 if( null != paste ) { 110 Node[] itemsBefore = categoryNode.getChildren().getNodes( DefaultModel.canBlock() ); 111 paste.paste(); 112 Node[] itemsAfter = categoryNode.getChildren().getNodes( DefaultModel.canBlock() ); 113 114 if( itemsAfter.length == itemsBefore.length+1 ) { 115 int currentIndex = -1; 116 Node newItem = null; 117 for( int i=itemsAfter.length-1; i>=0; i-- ) { 118 newItem = itemsAfter[i]; 119 currentIndex = i; 120 for( int j=0; j<itemsBefore.length; j++ ) { 121 if( newItem.equals( itemsBefore[j] ) ) { 122 newItem = null; 123 break; 124 } 125 } 126 if( null != newItem ) { 127 break; 128 } 129 } 130 if( null != newItem && dropIndex >= 0 ) { 131 if( currentIndex < dropIndex ) 132 dropIndex++; 133 moveItem( targetCategory, newItem.getLookup(), dropIndex ); 134 } 135 } 136 return true; 137 } 138 } catch( IOException ioE ) { 139 ErrorManager.getDefault().notify( ErrorManager.INFORMATIONAL, ioE ); 140 } catch( UnsupportedFlavorException e ) { 141 ErrorManager.getDefault().notify( ErrorManager.INFORMATIONAL, e ); 142 } 143 return false; 144 } 145 146 155 private boolean moveItem( Lookup category, Lookup itemToMove, int moveToIndex ) { 156 Node categoryNode = (Node)category.lookup( Node.class ); 157 if( null == categoryNode ) 158 return false; 159 Node itemNode = (Node)itemToMove.lookup( Node.class ); 160 if( null == itemNode ) 161 return false; 162 163 Index order = (Index)categoryNode.getCookie( Index.class ); 164 if( null == order ) { 165 return false; 166 } 167 168 int sourceIndex = order.indexOf( itemNode ); 169 if( sourceIndex < moveToIndex ) { 170 moveToIndex--; 171 } 172 order.move( sourceIndex, moveToIndex ); 173 return true; 174 } 175 176 180 public boolean canReorderCategories( Lookup paletteRoot ) { 181 Node rootNode = (Node)paletteRoot.lookup( Node.class ); 182 if( null != rootNode ) { 183 return null != rootNode.getCookie( Index.class ); 184 } 185 return false; 186 } 187 188 196 public boolean moveCategory( Lookup category, int moveToIndex ) { 197 Node categoryNode = (Node)category.lookup( Node.class ); 198 if( null == categoryNode ) 199 return false; 200 Node rootNode = categoryNode.getParentNode(); 201 if( null == rootNode ) 202 return false; 203 204 Index order = (Index)rootNode.getCookie( Index.class ); 205 if( null == order ) { 206 return false; 207 } 208 209 int sourceIndex = order.indexOf( categoryNode ); 210 if( sourceIndex < moveToIndex ) { 211 moveToIndex--; 212 } 213 order.move( sourceIndex, moveToIndex ); 214 return true; 215 } 216 217 private static final class DefaultDragAndDropHandler extends DragAndDropHandler { 218 public void customize(ExTransferable t, Lookup item) { 219 } 221 } 222 } 223 | Popular Tags |