KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Arrays JavaDoc;
25 import java.util.LinkedHashSet JavaDoc;
26
27 import org.apache.directory.ldapstudio.browser.common.actions.proxy.BrowserActionProxy;
28 import org.apache.directory.ldapstudio.browser.common.dnd.ConnectionTransfer;
29 import org.apache.directory.ldapstudio.browser.common.dnd.EntryTransfer;
30 import org.apache.directory.ldapstudio.browser.common.dnd.ValuesTransfer;
31 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants;
32 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
33 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
34 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
35 import org.apache.directory.ldapstudio.browser.core.model.IValue;
36 import org.apache.directory.ldapstudio.browser.core.utils.LdifUtils;
37
38 import org.eclipse.jface.resource.ImageDescriptor;
39 import org.eclipse.swt.dnd.Clipboard;
40 import org.eclipse.swt.dnd.TextTransfer;
41 import org.eclipse.swt.dnd.Transfer;
42 import org.eclipse.swt.widgets.Display;
43 import org.eclipse.ui.ISharedImages;
44 import org.eclipse.ui.PlatformUI;
45 import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds;
46
47
48 /**
49  * This class implements the Copy Action
50  *
51  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
52  * @version $Rev$, $Date$
53  */

54 public class CopyAction extends BrowserAction
55 {
56     protected BrowserActionProxy pasteActionProxy;
57
58
59     /**
60      * Creates a new instance of CopyAction.
61      *
62      * @param pasteActionProxy
63      * the associated Paste Action
64      */

65     public CopyAction( BrowserActionProxy pasteActionProxy )
66     {
67         super();
68         this.pasteActionProxy = pasteActionProxy;
69     }
70
71
72     /**
73      * {@inheritDoc}
74      */

75     public String JavaDoc getText()
76     {
77
78         // connection
79
IConnection[] connections = getConnections();
80         if ( connections != null )
81         {
82             return connections.length > 1 ? "Copy Connections" : "Copy Connection";
83         }
84
85         // entry/searchresult/bookmark
86
IEntry[] entries = getEntries();
87         if ( entries != null )
88         {
89             return entries.length > 1 ? "Copy Entries / DNs" : "Copy Entry / DN";
90         }
91
92         // values
93
IValue[] values = getValues();
94         if ( values != null )
95         {
96             return values.length > 1 ? "Copy Values" : "Copy Value";
97         }
98
99         return "Copy";
100     }
101
102
103     /**
104      * {@inheritDoc}
105      */

106     public ImageDescriptor getImageDescriptor()
107     {
108         return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor( ISharedImages.IMG_TOOL_COPY );
109     }
110
111
112     /**
113      * {@inheritDoc}
114      */

115     public String JavaDoc getCommandId()
116     {
117         return IWorkbenchActionDefinitionIds.COPY;
118     }
119
120
121     /**
122      * {@inheritDoc}
123      */

124     public void run()
125     {
126         IConnection[] connections = getConnections();
127         IEntry[] entries = getEntries();
128         IValue[] values = getValues();
129
130         // connection
131
if ( connections != null )
132         {
133             copyToClipboard( new Object JavaDoc[]
134                 { connections }, new Transfer[]
135                 { ConnectionTransfer.getInstance() } );
136         }
137
138         // entry/searchresult/bookmark
139
else if ( entries != null )
140         {
141             StringBuffer JavaDoc text = new StringBuffer JavaDoc();
142             for ( int i = 0; i < entries.length; i++ )
143             {
144                 text.append( entries[i].getDn().toString() );
145                 if ( i + 1 < entries.length )
146                 {
147                     text.append( BrowserCoreConstants.LINE_SEPARATOR );
148                 }
149             }
150             copyToClipboard( new Object JavaDoc[]
151                 { entries, text.toString() }, new Transfer[]
152                 { EntryTransfer.getInstance(), TextTransfer.getInstance() } );
153         }
154
155         // values
156
else if ( values != null )
157         {
158
159             // LdifAttrValLine[] lines = new LdifAttrValLine[values.length];
160
StringBuffer JavaDoc text = new StringBuffer JavaDoc();
161
162             for ( int i = 0; i < values.length; i++ )
163             {
164
165                 // lines[i] = ModelConverter.valueToLdifAttrValLine(values[i]);
166

167                 if ( values[i].isString() )
168                 {
169                     text.append( values[i].getStringValue() );
170                 }
171                 else if ( values[i].isBinary() )
172                 {
173                     text.append( LdifUtils.base64encode( values[i].getBinaryValue() ) );
174                 }
175                 if ( i + 1 < values.length )
176                 {
177                     text.append( BrowserCoreConstants.LINE_SEPARATOR );
178                 }
179             }
180
181             copyToClipboard( new Object JavaDoc[]
182                 { values, text.toString() }, new Transfer[]
183                 { ValuesTransfer.getInstance(), TextTransfer.getInstance() } );
184         }
185
186         // update paste action
187
if ( this.pasteActionProxy != null )
188         {
189             this.pasteActionProxy.updateAction();
190         }
191     }
192
193
194     /**
195      * Copies data to Clipboard
196      *
197      * @param data
198      * the data to be set in the clipboard
199      * @param dataTypes
200      * the transfer agents that will convert the data to its platform specific format;
201      * each entry in the data array must have a corresponding dataType
202      */

203     public static void copyToClipboard( Object JavaDoc[] data, Transfer[] dataTypes )
204     {
205         Clipboard clipboard = null;
206         try
207         {
208             clipboard = new Clipboard( Display.getCurrent() );
209             clipboard.setContents( data, dataTypes );
210         }
211         finally
212         {
213             if ( clipboard != null )
214                 clipboard.dispose();
215         }
216     }
217
218
219     /**
220      * {@inheritDoc}
221      */

222     public boolean isEnabled()
223     {
224
225         // connection
226
if ( getConnections() != null )
227         {
228             return true;
229         }
230
231         // entry/searchresult/bookmark
232
else if ( getEntries() != null )
233         {
234             return true;
235         }
236
237         // values
238
else if ( getValues() != null )
239         {
240             return true;
241         }
242
243         else
244         {
245             return false;
246         }
247     }
248
249
250     /**
251      * Get the Connections
252      *
253      * @return
254      * the Connections
255      */

256     private IConnection[] getConnections()
257     {
258
259         if ( getSelectedEntries().length + getSelectedSearchResults().length + getSelectedBookmarks().length
260             + getSelectedSearches().length + getSelectedAttributeHierarchies().length + getSelectedAttributes().length
261             + getSelectedValues().length == 0
262             && getSelectedConnections().length > 0 )
263         {
264             return getSelectedConnections();
265         }
266         else
267         {
268             return null;
269         }
270     }
271
272
273     /**
274      * Get the Entries
275      *
276      * @return
277      * the Entries
278      */

279     private IEntry[] getEntries()
280     {
281         if ( getSelectedConnections().length + getSelectedSearches().length + getSelectedAttributeHierarchies().length
282             + getSelectedAttributes().length + getSelectedValues().length == 0
283             && getSelectedEntries().length + getSelectedSearchResults().length + getSelectedBookmarks().length > 0 )
284         {
285
286             LinkedHashSet JavaDoc entriesSet = new LinkedHashSet JavaDoc();
287             for ( int i = 0; i < getSelectedEntries().length; i++ )
288             {
289                 entriesSet.add( getSelectedEntries()[i] );
290             }
291             for ( int i = 0; i < this.getSelectedSearchResults().length; i++ )
292             {
293                 entriesSet.add( this.getSelectedSearchResults()[i].getEntry() );
294             }
295             for ( int i = 0; i < this.getSelectedBookmarks().length; i++ )
296             {
297                 entriesSet.add( this.getSelectedBookmarks()[i].getEntry() );
298             }
299             return ( IEntry[] ) entriesSet.toArray( new IEntry[entriesSet.size()] );
300         }
301         else
302         {
303             return null;
304         }
305     }
306
307
308     /**
309      * Get the Values
310      *
311      * @return
312      * the Values
313      */

314     private IValue[] getValues()
315     {
316         if ( getSelectedConnections().length + getSelectedBookmarks().length + getSelectedEntries().length
317             + getSelectedSearches().length == 0
318             && getSelectedAttributeHierarchies().length + getSelectedAttributes().length + getSelectedValues().length > 0 )
319         {
320
321             LinkedHashSet JavaDoc valuesSet = new LinkedHashSet JavaDoc();
322             for ( int i = 0; i < this.getSelectedAttributeHierarchies().length; i++ )
323             {
324                 IAttribute[] attributes = getSelectedAttributeHierarchies()[i].getAttributes();
325                 for ( int k = 0; k < attributes.length; k++ )
326                 {
327                     valuesSet.addAll( Arrays.asList( attributes[k].getValues() ) );
328                 }
329             }
330             for ( int i = 0; i < this.getSelectedAttributes().length; i++ )
331             {
332                 valuesSet.addAll( Arrays.asList( this.getSelectedAttributes()[i].getValues() ) );
333             }
334             for ( int i = 0; i < this.getSelectedValues().length; i++ )
335             {
336                 valuesSet.add( this.getSelectedValues()[i] );
337             }
338             return ( IValue[] ) valuesSet.toArray( new IValue[valuesSet.size()] );
339         }
340         else
341         {
342             return null;
343         }
344     }
345 }
346
Popular Tags