KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants;
28 import org.apache.directory.ldapstudio.browser.common.actions.BrowserAction;
29 import org.apache.directory.ldapstudio.browser.common.jobs.RunnableContextJobAdapter;
30 import org.apache.directory.ldapstudio.browser.core.jobs.InitializeAttributesJob;
31 import org.apache.directory.ldapstudio.browser.core.jobs.ReadEntryJob;
32 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
33 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult;
34 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin;
35 import org.eclipse.swt.dnd.Clipboard;
36 import org.eclipse.swt.dnd.TextTransfer;
37 import org.eclipse.swt.dnd.Transfer;
38 import org.eclipse.swt.widgets.Display;
39
40
41 /**
42  * This abstract class must be extended by each Action that <em>"Copies an Entry as..."</em>.
43  *
44  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
45  * @version $Rev$, $Date$
46  */

47 public abstract class CopyEntryAsAction extends BrowserAction
48 {
49     /**
50      * Returns DN only Mode.
51      */

52     public static final int MODE_DN_ONLY = 1;
53
54     /**
55      * Returns Attributes only Mode.
56      */

57     public static final int MODE_RETURNING_ATTRIBUTES_ONLY = 2;
58
59     /**
60      * Normal Mode
61      */

62     public static final int MODE_NORMAL = 3;
63
64     /**
65      * Includes Operational Attributes Mode.
66      */

67     public static final int MODE_INCLUDE_OPERATIONAL_ATTRIBUTES = 4;
68
69     protected int mode;
70
71     protected String JavaDoc type;
72
73     protected String JavaDoc appendix;
74
75
76     /**
77      * Creates a new instance of CopyEntryAsAction.
78      *
79      * @param type
80      * the type of the target
81      * @param mode
82      * the copy Mode
83      */

84     public CopyEntryAsAction( String JavaDoc type, int mode )
85     {
86         super();
87         this.type = type;
88         this.mode = mode;
89         if ( this.mode == MODE_DN_ONLY )
90         {
91             this.appendix = " (DN only)";
92         }
93         else if ( this.mode == MODE_RETURNING_ATTRIBUTES_ONLY )
94         {
95             this.appendix = " (returning attributes only)";
96         }
97         else if ( this.mode == MODE_INCLUDE_OPERATIONAL_ATTRIBUTES )
98         {
99             this.appendix = " (include operational attributes)";
100         }
101         else if ( this.mode == MODE_NORMAL )
102         {
103             this.appendix = " (all user attributes)";
104         }
105         else
106         {
107             appendix = "";
108         }
109     }
110
111
112     /**
113      * {@inheritDoc}
114      */

115     public String JavaDoc getText()
116     {
117         if ( getSelectedEntries().length + getSelectedSearchResults().length + getSelectedBookmarks().length > 0
118             && getSelectedSearches().length == 0 )
119         {
120             String JavaDoc text = ( getSelectedEntries().length + getSelectedSearchResults().length
121                 + getSelectedBookmarks().length > 1 ? "Copy Entries as " + type : "Copy Entry as " + type )
122                 + appendix;
123             return text;
124         }
125         else if ( getSelectedEntries().length + getSelectedSearchResults().length + getSelectedBookmarks().length == 0
126             && getSelectedSearches().length == 1 && getSelectedSearches()[0].getSearchResults() != null
127             && getSelectedSearches()[0].getSearchResults().length > 0 )
128         {
129             String JavaDoc text = ( getSelectedSearches()[0].getSearchResults().length > 1 ? "Copy Search Results as " + type
130                 : "Copy Search Result as " + type )
131                 + appendix;
132             return text;
133         }
134
135         return "Copy Entry as " + type + appendix;
136     }
137
138
139     /**
140      * {@inheritDoc}
141      */

142     public String JavaDoc getCommandId()
143     {
144         return null;
145     }
146
147
148     /**
149      * {@inheritDoc}
150      */

151     public void run()
152     {
153         // entries to copy
154
List JavaDoc<IEntry> entryList = new ArrayList JavaDoc<IEntry>();
155         for ( int i = 0; i < getSelectedEntries().length; i++ )
156         {
157             entryList.add( getSelectedEntries()[i] );
158         }
159         for ( int i = 0; i < getSelectedSearchResults().length; i++ )
160         {
161             entryList.add( getSelectedSearchResults()[i].getEntry() );
162         }
163         for ( int i = 0; i < getSelectedBookmarks().length; i++ )
164         {
165             IEntry entry = getSelectedBookmarks()[0].getConnection().getEntryFromCache(
166                 getSelectedBookmarks()[0].getDn() );
167             if ( entry == null )
168             {
169                 ReadEntryJob job = new ReadEntryJob( getSelectedBookmarks()[0].getConnection(),
170                     getSelectedBookmarks()[0].getDn() );
171                 RunnableContextJobAdapter.execute( job );
172                 entry = job.getReadEntry();
173             }
174             entryList.add( entry );
175         }
176         if ( getSelectedSearches().length == 1 )
177         {
178             ISearchResult[] results = getSelectedSearches()[0].getSearchResults();
179             for ( int k = 0; k < results.length; k++ )
180             {
181                 entryList.add( results[k].getEntry() );
182             }
183         }
184         IEntry[] entries = ( IEntry[] ) entryList.toArray( new IEntry[entryList.size()] );
185
186         // check uninitialized entries
187
List JavaDoc<IEntry> uninitializedEntryList = new ArrayList JavaDoc<IEntry>();
188         for ( int i = 0; entries != null && i < entries.length; i++ )
189         {
190             if ( !entries[i].isAttributesInitialized() )
191             {
192                 uninitializedEntryList.add( entries[i] );
193             }
194         }
195         if ( uninitializedEntryList.size() > 0
196             && ( this.mode == MODE_NORMAL || this.mode == MODE_INCLUDE_OPERATIONAL_ATTRIBUTES ) )
197         {
198             IEntry[] uninitializedEntries = ( IEntry[] ) uninitializedEntryList
199                 .toArray( new IEntry[uninitializedEntryList.size()] );
200
201             InitializeAttributesJob job = new InitializeAttributesJob( uninitializedEntries, false );
202             RunnableContextJobAdapter.execute( job );
203
204             // SyncInitializeEntryJob job = new
205
// SyncInitializeEntryJob(uninitializedEntries,
206
// InitializeEntryJob.INIT_ATTRIBUTES_MODE, null);
207
// job.execute();
208
}
209
210         // serialize
211
StringBuffer JavaDoc text = new StringBuffer JavaDoc();
212         serialializeEntries( entries, text );
213         copyToClipboard( text.toString() );
214     }
215
216
217     /**
218      * Serializes Entries.
219      *
220      * @param entries
221      * the Entries to serialize
222      * @param text
223      * the StringBuffer to serialize to
224      */

225     protected abstract void serialializeEntries( IEntry[] entries, StringBuffer JavaDoc text );
226
227
228     /**
229      * {@inheritDoc}
230      */

231     public boolean isEnabled()
232     {
233         boolean showOperational = BrowserUIPlugin.getDefault().getPreferenceStore().getBoolean(
234             BrowserCommonConstants.PREFERENCE_ENTRYEDITOR_SHOW_OPERATIONAL_ATTRIBUTES );
235
236         if ( getSelectedSearchResults().length > 0
237             && getSelectedEntries().length + getSelectedBookmarks().length + getSelectedSearches().length == 0 )
238         {
239             return ( this.mode == MODE_RETURNING_ATTRIBUTES_ONLY || this.mode == MODE_NORMAL
240                 || this.mode == MODE_DN_ONLY || ( this.mode == MODE_INCLUDE_OPERATIONAL_ATTRIBUTES && showOperational ) );
241         }
242         if ( getSelectedEntries().length + getSelectedSearchResults().length + getSelectedBookmarks().length > 0
243             && getSelectedSearches().length == 0 )
244         {
245             return ( this.mode == MODE_NORMAL || this.mode == MODE_DN_ONLY || ( this.mode == MODE_INCLUDE_OPERATIONAL_ATTRIBUTES && showOperational ) );
246         }
247         if ( getSelectedEntries().length + getSelectedSearchResults().length + getSelectedBookmarks().length == 0
248             && getSelectedSearches().length == 1 && getSelectedSearches()[0].getSearchResults() != null
249             && getSelectedSearches()[0].getSearchResults().length > 0 )
250         {
251             return ( this.mode != MODE_INCLUDE_OPERATIONAL_ATTRIBUTES || ( this.mode == MODE_INCLUDE_OPERATIONAL_ATTRIBUTES && showOperational ) );
252         }
253         return false;
254     }
255
256
257     /**
258      * Copies text to Clipboard
259      *
260      * @param text
261      * the Text to copy
262      */

263     protected void copyToClipboard( String JavaDoc text )
264     {
265         Clipboard clipboard = null;
266         try
267         {
268             clipboard = new Clipboard( Display.getCurrent() );
269             clipboard.setContents( new Object JavaDoc[]
270                 { text }, new Transfer[]
271                 { TextTransfer.getInstance() } );
272         }
273         finally
274         {
275             if ( clipboard != null )
276                 clipboard.dispose();
277         }
278     }
279 }
280
Popular Tags