KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > views > connection > DropConnectionListener


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.browser.ui.views.connection;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
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 /**
40  * This class implements a {@link DropTargetListener} that is used to
41  * drag and drop connections withing the connections view.
42  *
43  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
44  * @version $Rev$, $Date$
45  */

46 public class DropConnectionListener implements DropTargetListener
47 {
48
49     /**
50      * Creates a new instance of DropConnectionListener.
51      */

52     public DropConnectionListener()
53     {
54     }
55
56
57     /**
58      * {@inheritDoc}
59      *
60      * This implementation checks if the event's data type is
61      * supported. If not supported dropping is rejected.
62      */

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     /**
73      * {@inheritDoc}
74      *
75      * This implementation just calls {@link #dragOver(DropTargetEvent)}.
76      */

77     public void dragOperationChanged( DropTargetEvent event )
78     {
79         dragOver( event );
80     }
81
82
83     /**
84      * {@inheritDoc}
85      *
86      * This implementation does nothing.
87      */

88     public void dragLeave( DropTargetEvent event )
89     {
90     }
91
92
93     /**
94      * {@inheritDoc}
95      *
96      * This implementation checks if the event's data type is
97      * supported. If not supported dropping is rejected.
98      */

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 JavaDoc<IConnection> connectionList = new ArrayList JavaDoc<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     /**
158      * {@inheritDoc}
159      *
160      * This implementation does nothing.
161      */

162     public void dropAccept( DropTargetEvent event )
163     {
164     }
165
166
167     /**
168      * {@inheritDoc}
169      *
170      * This implementation drops the dragged connection to
171      * the selected position.
172      */

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                 // get connection to handle
182
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 JavaDoc e )
223         {
224             event.detail = DND.DROP_NONE;
225             e.printStackTrace();
226         }
227     }
228
229 }
230
Popular Tags