1 20 21 package org.apache.directory.ldapstudio.browser.ui.views.connection; 22 23 24 import java.util.ArrayList ; 25 import java.util.List ; 26 27 import org.apache.directory.ldapstudio.browser.common.dnd.ConnectionTransfer; 28 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin; 29 import org.apache.directory.ldapstudio.browser.core.ConnectionManager; 30 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 31 import org.eclipse.swt.dnd.DND; 32 import org.eclipse.swt.dnd.DropTarget; 33 import org.eclipse.swt.dnd.DropTargetEvent; 34 import org.eclipse.swt.dnd.DropTargetListener; 35 import org.eclipse.swt.widgets.Table; 36 import org.eclipse.swt.widgets.TableItem; 37 38 39 46 public class DropConnectionListener implements DropTargetListener 47 { 48 49 52 public DropConnectionListener() 53 { 54 } 55 56 57 63 public void dragEnter( DropTargetEvent event ) 64 { 65 if ( !ConnectionTransfer.getInstance().isSupportedType( event.currentDataType ) ) 66 { 67 event.detail = DND.DROP_NONE; 68 } 69 } 70 71 72 77 public void dragOperationChanged( DropTargetEvent event ) 78 { 79 dragOver( event ); 80 } 81 82 83 88 public void dragLeave( DropTargetEvent event ) 89 { 90 } 91 92 93 99 public void dragOver( DropTargetEvent event ) 100 { 101 boolean isOverSelection = false; 102 if ( event.detail == DND.DROP_MOVE || event.detail == DND.DROP_NONE ) 103 { 104 if ( ConnectionTransfer.getInstance().isSupportedType( event.currentDataType ) ) 105 { 106 if ( event.item != null && event.item.getData() instanceof IConnection ) 107 { 108 IConnection overConn = ( IConnection ) event.item.getData(); 109 if ( event.widget instanceof DropTarget ) 110 { 111 DropTarget dropTarget = ( DropTarget ) event.widget; 112 if ( dropTarget.getControl() instanceof Table ) 113 { 114 Table table = ( Table ) dropTarget.getControl(); 115 TableItem[] items = table.getSelection(); 116 List <IConnection> connectionList = new ArrayList <IConnection>(); 117 for ( int i = 0; i < items.length; i++ ) 118 { 119 if ( items[i].getData() instanceof IConnection ) 120 { 121 connectionList.add( ( IConnection ) items[i].getData() ); 122 } 123 } 124 if ( connectionList.contains( overConn ) ) 125 { 126 isOverSelection = true; 127 } 128 } 129 } 130 } 131 } 132 } 133 134 if ( !ConnectionTransfer.getInstance().isSupportedType( event.currentDataType ) ) 135 { 136 event.detail = DND.DROP_NONE; 137 } 138 else if ( event.item == null ) 139 { 140 event.detail = DND.DROP_NONE; 141 } 142 else if ( isOverSelection ) 143 { 144 event.detail = DND.DROP_NONE; 145 } 146 else if ( event.detail == DND.DROP_LINK ) 147 { 148 event.detail = DND.DROP_NONE; 149 } 150 else if ( event.detail == DND.DROP_NONE ) 151 { 152 event.detail = DND.DROP_DEFAULT; 153 } 154 } 155 156 157 162 public void dropAccept( DropTargetEvent event ) 163 { 164 } 165 166 167 173 public void drop( DropTargetEvent event ) 174 { 175 ConnectionManager connectionManager = BrowserCorePlugin.getDefault().getConnectionManager(); 176 177 try 178 { 179 if ( ConnectionTransfer.getInstance().isSupportedType( event.currentDataType ) ) 180 { 181 IConnection[] connections = ( IConnection[] ) event.data; 183 IConnection targetConnection = ( IConnection ) event.item.getData(); 184 185 if ( event.detail == DND.DROP_MOVE ) 186 { 187 boolean fromTop = connectionManager.indexOf( connections[0] ) < connectionManager 188 .indexOf( targetConnection ); 189 for ( int i = 0; i < connections.length; i++ ) 190 { 191 connectionManager.removeConnection( connections[i] ); 192 } 193 for ( int i = 0; i < connections.length; i++ ) 194 { 195 int index = connectionManager.indexOf( targetConnection ); 196 if ( fromTop ) 197 { 198 index++; 199 connectionManager.addConnection( index + i, connections[i] ); 200 } 201 else 202 { 203 connectionManager.addConnection( index, connections[i] ); 204 } 205 } 206 } 207 else if ( event.detail == DND.DROP_COPY ) 208 { 209 for ( int i = 0; i < connections.length; i++ ) 210 { 211 IConnection newConnection = ( IConnection ) connections[i].clone(); 212 int index = connectionManager.indexOf( targetConnection ); 213 connectionManager.addConnection( index + i + 1, newConnection ); 214 } 215 } 216 } 217 else 218 { 219 event.detail = DND.DROP_NONE; 220 } 221 } 222 catch ( Exception e ) 223 { 224 event.detail = DND.DROP_NONE; 225 e.printStackTrace(); 226 } 227 } 228 229 } 230 | Popular Tags |