KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > actions > PasteAction


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.common.actions;
22
23
24 import org.apache.directory.ldapstudio.browser.common.dialogs.ScopeDialog;
25 import org.apache.directory.ldapstudio.browser.common.dnd.ConnectionTransfer;
26 import org.apache.directory.ldapstudio.browser.common.dnd.EntryTransfer;
27 import org.apache.directory.ldapstudio.browser.common.dnd.ValuesTransfer;
28 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin;
29 import org.apache.directory.ldapstudio.browser.core.jobs.CopyEntriesJob;
30 import org.apache.directory.ldapstudio.browser.core.jobs.CreateValuesJob;
31 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
32 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
33 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
34 import org.apache.directory.ldapstudio.browser.core.model.IValue;
35 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContentRecord;
36
37 import org.eclipse.jface.resource.ImageDescriptor;
38 import org.eclipse.swt.dnd.Clipboard;
39 import org.eclipse.swt.dnd.Transfer;
40 import org.eclipse.swt.widgets.Display;
41 import org.eclipse.ui.ISharedImages;
42 import org.eclipse.ui.PlatformUI;
43 import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds;
44
45
46 /**
47  * This class implements the Paste Action.
48  *
49  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
50  * @version $Rev$, $Date$
51  */

52 public class PasteAction extends BrowserAction
53 {
54     /**
55      * Creates a new instance of PasteAction.
56      */

57     public PasteAction()
58     {
59         super();
60     }
61
62
63     /**
64      * {@inheritDoc}
65      */

66     public String JavaDoc getText()
67     {
68         // connection
69
IConnection[] connections = getConnectionsToPaste();
70         if ( connections != null )
71         {
72             return connections.length > 1 ? "Paste Connections" : "Paste Connection";
73         }
74
75         // entry
76
IEntry[] entries = getEntriesToPaste();
77         if ( entries != null )
78         {
79             return entries.length > 1 ? "Paste Entries" : "Paste Entry";
80         }
81
82         // value
83
IValue[] values = getValuesToPaste();
84         if ( values != null )
85         {
86             return values.length > 1 ? "Paste Values" : "Paste Value";
87         }
88
89         return "Paste";
90     }
91
92
93     /**
94      * {@inheritDoc}
95      */

96     public ImageDescriptor getImageDescriptor()
97     {
98         return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor( ISharedImages.IMG_TOOL_PASTE );
99     }
100
101
102     /**
103      * {@inheritDoc}
104      */

105     public String JavaDoc getCommandId()
106     {
107         return IWorkbenchActionDefinitionIds.PASTE;
108     }
109
110
111     /**
112      * {@inheritDoc}
113      */

114     public boolean isEnabled()
115     {
116
117         // connection
118
if ( getConnectionsToPaste() != null )
119         {
120             return true;
121         }
122
123         // entry
124
else if ( getEntriesToPaste() != null )
125         {
126             return true;
127         }
128
129         // value
130
else if ( getValuesToPaste() != null )
131         {
132             return true;
133         }
134
135         return false;
136     }
137
138
139     /**
140      * {@inheritDoc}
141      */

142     public void run()
143     {
144         // connection
145
IConnection[] connections = getConnectionsToPaste();
146         if ( connections != null )
147         {
148             for ( int i = 0; i < connections.length; i++ )
149             {
150                 IConnection newConnection = ( IConnection ) connections[i].clone();
151                 BrowserCorePlugin.getDefault().getConnectionManager().addConnection( newConnection );
152             }
153             return;
154         }
155
156         // entry
157
IEntry[] entries = getEntriesToPaste();
158         if ( entries != null )
159         {
160             this.pasteEntries( getSelectedEntries()[0], entries );
161             return;
162         }
163
164         // value
165
IValue[] values = getValuesToPaste();
166         if ( values != null )
167         {
168             this.pasteValues( values );
169             return;
170         }
171
172     }
173
174
175     /**
176      * Pastes the given entries
177      *
178      * @param parent
179      * the parent Entry
180      * @param entriesToPaste
181      * the Entries to paste
182      */

183     private void pasteEntries( final IEntry parent, final IEntry[] entriesToPaste )
184     {
185
186         int scope = ISearch.SCOPE_OBJECT;
187         boolean askForScope = false;
188         for ( int i = 0; i < entriesToPaste.length; i++ )
189         {
190             if ( entriesToPaste[i].hasChildren() )
191             {
192                 askForScope = true;
193                 break;
194             }
195         }
196         if ( askForScope )
197         {
198             ScopeDialog scopeDialog = new ScopeDialog( Display.getDefault().getActiveShell(), "Select Copy Depth",
199                 entriesToPaste.length > 1 );
200             scopeDialog.open();
201             scope = scopeDialog.getScope();
202         }
203
204         new CopyEntriesJob( parent, entriesToPaste, scope ).execute();
205     }
206
207
208     /**
209      * Paste Values
210      *
211      * @param values
212      * the Values to paste
213      */

214     private void pasteValues( IValue[] values )
215     {
216         IEntry entry = null;
217         if ( getSelectedAttributes().length > 0 )
218         {
219             entry = getSelectedAttributes()[0].getEntry();
220         }
221         else if ( getSelectedValues().length > 0 )
222         {
223             entry = getSelectedValues()[0].getAttribute().getEntry();
224         }
225         else if ( getSelectedEntries().length == 1 )
226         {
227             entry = getSelectedEntries()[0];
228         }
229         else if ( getSelectedSearchResults().length == 1 )
230         {
231             entry = getSelectedSearchResults()[0].getEntry();
232         }
233         else if ( getSelectedBookmarks().length == 1 )
234         {
235             entry = getSelectedBookmarks()[0].getEntry();
236         }
237
238         if ( entry != null )
239         {
240             String JavaDoc[] attributeNames = new String JavaDoc[values.length];
241             Object JavaDoc[] rawValues = new Object JavaDoc[values.length];
242             for ( int v = 0; v < values.length; v++ )
243             {
244                 attributeNames[v] = values[v].getAttribute().getDescription();
245                 rawValues[v] = values[v].getRawValue();
246             }
247             new CreateValuesJob( entry, attributeNames, rawValues ).execute();
248         }
249     }
250
251
252     /**
253      * Conditions: - a connection is selected - there are connections in
254      * clipboard
255      *
256      * @return
257      */

258     private IConnection[] getConnectionsToPaste()
259     {
260         if ( getSelectedBookmarks().length + getSelectedEntries().length + getSelectedSearchResults().length
261             + getSelectedSearches().length + getSelectedAttributes().length + getSelectedValues().length == 0
262             && getSelectedConnections().length > 0 )
263         {
264
265             Object JavaDoc content = this.getFromClipboard( ConnectionTransfer.getInstance() );
266             if ( content != null && content instanceof IConnection[] )
267             {
268                 IConnection[] connections = ( IConnection[] ) content;
269                 return connections;
270             }
271         }
272
273         return null;
274     }
275
276
277     /**
278      * Conditions: - an entry is selected - there are entries in clipboard
279      *
280      * @return
281      */

282     private IEntry[] getEntriesToPaste()
283     {
284         if ( getSelectedBookmarks().length + getSelectedSearchResults().length + getSelectedSearches().length
285             + getSelectedConnections().length + getSelectedAttributes().length + getSelectedValues().length == 0
286             && getSelectedEntries().length == 1 )
287         {
288
289             Object JavaDoc content = this.getFromClipboard( EntryTransfer.getInstance() );
290             if ( content != null && content instanceof IEntry[] )
291             {
292                 IEntry[] entries = ( IEntry[] ) content;
293                 return entries;
294             }
295         }
296
297         return null;
298     }
299
300
301     /**
302      * Conditions: - an attribute or value is selected - there are values in
303      * clipboard
304      *
305      * @return
306      */

307     private IValue[] getValuesToPaste()
308     {
309         if ( ( getSelectedEntries().length + getSelectedSearchResults().length + getSelectedBookmarks().length
310             + getSelectedSearches().length + getSelectedConnections().length == 0 && ( getSelectedAttributes().length
311             + getSelectedValues().length > 0 ) )
312             || ( getSelectedAttributes().length + getSelectedValues().length + getSelectedSearchResults().length
313                 + getSelectedBookmarks().length + getSelectedSearches().length + getSelectedConnections().length == 0 && ( getSelectedEntries().length == 1 ) )
314             || ( getSelectedAttributes().length + getSelectedValues().length + getSelectedEntries().length
315                 + getSelectedSearchResults().length + getSelectedSearches().length + getSelectedConnections().length == 0 && ( getSelectedBookmarks().length == 1 ) )
316             || ( getSelectedAttributes().length + getSelectedValues().length + getSelectedEntries().length
317                 + getSelectedBookmarks().length + getSelectedSearches().length + getSelectedConnections().length == 0 && ( getSelectedSearchResults().length == 1 ) )
318
319         )
320         {
321
322             Object JavaDoc content = this.getFromClipboard( ValuesTransfer.getInstance() );
323             if ( content != null && content instanceof IValue[] )
324             {
325                 IValue[] values = ( IValue[] ) content;
326                 return values;
327             }
328         }
329
330         return null;
331     }
332
333
334     /**
335      * Retrieve the data of the specified type currently available on the system clipboard.
336      *
337      * @param dataType
338      * the transfer agent for the type of data being requested
339      * @return
340      * the data obtained from the clipboard or null if no data of this type is available
341      */

342     protected Object JavaDoc getFromClipboard( Transfer dataType )
343     {
344         Clipboard clipboard = null;
345         try
346         {
347             clipboard = new Clipboard( Display.getCurrent() );
348             return clipboard.getContents( dataType );
349         }
350         finally
351         {
352             if ( clipboard != null )
353                 clipboard.dispose();
354         }
355     }
356 }
357
Popular Tags