KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > actions > CopyValueAction


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.actions;
22
23
24 import java.util.Arrays JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.LinkedHashSet JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import org.apache.directory.ldapstudio.browser.common.actions.BrowserAction;
30 import org.apache.directory.ldapstudio.browser.common.actions.CopyAction;
31 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants;
32 import org.apache.directory.ldapstudio.browser.core.model.DN;
33 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
34 import org.apache.directory.ldapstudio.browser.core.model.IValue;
35 import org.apache.directory.ldapstudio.browser.core.utils.LdifUtils;
36 import org.apache.directory.ldapstudio.browser.core.utils.ModelConverter;
37 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants;
38 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin;
39
40 import org.eclipse.jface.resource.ImageDescriptor;
41 import org.eclipse.swt.dnd.TextTransfer;
42 import org.eclipse.swt.dnd.Transfer;
43
44
45 /**
46  * This Action copies the value of the selecte Entry to the Clipboard.
47  *
48  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
49  * @version $Rev$, $Date$
50  */

51 public class CopyValueAction extends BrowserAction
52 {
53     /**
54      * UTF8 Mode.
55      */

56     public static final int MODE_UTF8 = 1;
57
58     /**
59      * Base64 Mode.
60      */

61     public static final int MODE_BASE64 = 2;
62
63     /**
64      * Hexadecimal Mode.
65      */

66     public static final int MODE_HEX = 3;
67
68     /**
69      * LDIF Mode.
70      */

71     public static final int MODE_LDIF = 4;
72
73     private int mode;
74
75
76     /**
77      * Creates a new instance of CopyValueAction.
78      *
79      * @param mode
80      * the copy Mode
81      */

82     public CopyValueAction( int mode )
83     {
84         this.mode = mode;
85     }
86
87
88     /**
89      * {@inheritDoc}
90      */

91     public String JavaDoc getText()
92     {
93         if ( mode == MODE_UTF8 )
94         {
95             return getValueSet().size() > 1 ? "Copy Values (UTF-8)" : "Copy Value (UTF-8)";
96         }
97         else if ( mode == MODE_BASE64 )
98         {
99             return getValueSet().size() > 1 ? "Copy Values (BASE-64)" : "Copy Value (BASE-64)";
100         }
101         else if ( mode == MODE_HEX )
102         {
103             return getValueSet().size() > 1 ? "Copy Values (HEX)" : "Copy Value (HEX)";
104         }
105         else if ( mode == MODE_LDIF )
106         {
107             return getValueSet().size() > 1 ? "Copy Name-Value-Pairs as LDIF" : "Copy Name-Value-Pair as LDIF";
108         }
109         else
110         {
111             return "Copy Value";
112         }
113     }
114
115
116     /**
117      * {@inheritDoc}
118      */

119     public ImageDescriptor getImageDescriptor()
120     {
121         if ( mode == MODE_UTF8 )
122         {
123             return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_UTF8 );
124         }
125         else if ( mode == MODE_BASE64 )
126         {
127             return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_BASE64 );
128         }
129         else if ( mode == MODE_HEX )
130         {
131             return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_HEX );
132         }
133         else if ( mode == MODE_LDIF )
134         {
135             return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_LDIF );
136         }
137         else
138         {
139             return null;
140         }
141     }
142
143
144     /**
145      * {@inheritDoc}
146      */

147     public String JavaDoc getCommandId()
148     {
149         return null;
150     }
151
152
153     /**
154      * {@inheritDoc}
155      */

156     public boolean isEnabled()
157     {
158         return getValueSet().size() > 0 || getSelectedSearchResults().length > 0;
159     }
160
161
162     /**
163      * {@inheritDoc}
164      */

165     public void run()
166     {
167
168         StringBuffer JavaDoc text = new StringBuffer JavaDoc();
169         Set JavaDoc valueSet = getValueSet();
170         if ( !valueSet.isEmpty() )
171         {
172             for ( Iterator JavaDoc iterator = valueSet.iterator(); iterator.hasNext(); )
173             {
174                 IValue value = ( IValue ) iterator.next();
175
176                 if ( mode == MODE_UTF8 )
177                 {
178                     text.append( LdifUtils.utf8decode( value.getBinaryValue() ) );
179                     if ( iterator.hasNext() )
180                         text.append( BrowserCoreConstants.LINE_SEPARATOR );
181                 }
182                 else if ( mode == MODE_BASE64 )
183                 {
184                     text.append( LdifUtils.base64encode( value.getBinaryValue() ) );
185                     if ( iterator.hasNext() )
186                         text.append( BrowserCoreConstants.LINE_SEPARATOR );
187                 }
188                 else if ( mode == MODE_HEX )
189                 {
190                     text.append( LdifUtils.hexEncode( value.getBinaryValue() ) );
191                     if ( iterator.hasNext() )
192                         text.append( BrowserCoreConstants.LINE_SEPARATOR );
193                 }
194                 else if ( mode == MODE_LDIF )
195                 {
196                     text.append( ModelConverter.valueToLdifAttrValLine( value ).toFormattedString() );
197                 }
198
199             }
200         }
201         else if ( getSelectedSearchResults().length > 0 )
202         {
203             DN dn = getSelectedSearchResults()[0].getDn();
204
205             if ( mode == MODE_UTF8 )
206             {
207                 text.append( LdifUtils.utf8decode( dn.toString().getBytes() ) );
208             }
209             else if ( mode == MODE_BASE64 )
210             {
211                 text.append( LdifUtils.base64encode( dn.toString().getBytes() ) );
212             }
213             else if ( mode == MODE_HEX )
214             {
215                 text.append( LdifUtils.hexEncode( dn.toString().getBytes() ) );
216             }
217             else if ( mode == MODE_LDIF )
218             {
219                 text.append( ModelConverter.dnToLdifDnLine( dn ).toFormattedString() );
220             }
221         }
222
223         if ( text.length() > 0 )
224         {
225             CopyAction.copyToClipboard( new Object JavaDoc[]
226                 { text.toString() }, new Transfer[]
227                 { TextTransfer.getInstance() } );
228         }
229     }
230
231
232     /**
233      * Gets a Set containing all the Values
234      *
235      * @return
236      * a Set containing all the Values
237      */

238     protected Set JavaDoc getValueSet()
239     {
240         Set JavaDoc valueSet = new LinkedHashSet JavaDoc();
241         for ( int i = 0; i < getSelectedAttributeHierarchies().length; i++ )
242         {
243             for ( Iterator JavaDoc it = getSelectedAttributeHierarchies()[i].iterator(); it.hasNext(); )
244             {
245                 IAttribute att = ( IAttribute ) it.next();
246                 valueSet.addAll( Arrays.asList( att.getValues() ) );
247             }
248         }
249         for ( int i = 0; i < getSelectedAttributes().length; i++ )
250         {
251             valueSet.addAll( Arrays.asList( getSelectedAttributes()[i].getValues() ) );
252         }
253         valueSet.addAll( Arrays.asList( getSelectedValues() ) );
254         return valueSet;
255     }
256 }
257
Popular Tags