KickJava   Java API By Example, From Geeks To Geeks.

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


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.RenameEntryDialog;
25 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin;
26 import org.apache.directory.ldapstudio.browser.core.internal.model.RootDSE;
27 import org.apache.directory.ldapstudio.browser.core.jobs.RenameEntryJob;
28 import org.apache.directory.ldapstudio.browser.core.model.IBookmark;
29 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
30 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
31 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
32 import org.apache.directory.ldapstudio.browser.core.model.RDN;
33 import org.eclipse.jface.dialogs.Dialog;
34 import org.eclipse.jface.dialogs.IInputValidator;
35 import org.eclipse.jface.dialogs.InputDialog;
36 import org.eclipse.jface.resource.ImageDescriptor;
37 import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds;
38
39
40 /**
41  * This Action renames Connections, Entries, Searches, or Bookmarks.
42  *
43  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
44  * @version $Rev$, $Date$
45  */

46 public class RenameAction extends BrowserAction
47 {
48     /**
49      * Creates a new instance of RenameAction.
50      *
51      */

52     public RenameAction()
53     {
54         super();
55     }
56
57
58     /**
59      * {@inheritDoc}
60      */

61     public String JavaDoc getText()
62     {
63
64         IConnection[] connections = getConnections();
65         IEntry[] entries = getEntries();
66         ISearch[] searches = getSearches();
67         IBookmark[] bookmarks = getBookmarks();
68
69         if ( connections.length == 1 && entries.length == 0 && searches.length == 0 && bookmarks.length == 0 )
70         {
71             return "Rename Connection...";
72         }
73         else if ( entries.length == 1 && connections.length == 0 && searches.length == 0 && bookmarks.length == 0 )
74         {
75             return "Rename Entry...";
76         }
77         else if ( searches.length == 1 && connections.length == 0 && entries.length == 0 && bookmarks.length == 0 )
78         {
79             return "Rename Search...";
80         }
81         else if ( bookmarks.length == 1 && connections.length == 0 && entries.length == 0 && searches.length == 0 )
82         {
83             return "Rename Bookmark...";
84         }
85         else
86         {
87             return "Rename";
88         }
89     }
90
91
92     /**
93      * {@inheritDoc}
94      */

95     public ImageDescriptor getImageDescriptor()
96     {
97         return null;
98     }
99
100
101     /**
102      * {@inheritDoc}
103      */

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

113     public void run()
114     {
115         IConnection[] connections = getConnections();
116         IEntry[] entries = getEntries();
117         ISearch[] searches = getSearches();
118         IBookmark[] bookmarks = getBookmarks();
119
120         if ( connections.length == 1 && entries.length == 0 && searches.length == 0 && bookmarks.length == 0 )
121         {
122             renameConnection( connections[0] );
123         }
124         else if ( entries.length == 1 && connections.length == 0 && searches.length == 0 && bookmarks.length == 0 )
125         {
126             renameEntry( entries[0] );
127         }
128         else if ( searches.length == 1 && connections.length == 0 && entries.length == 0 && bookmarks.length == 0 )
129         {
130             renameSearch( searches[0] );
131         }
132         else if ( bookmarks.length == 1 && connections.length == 0 && entries.length == 0 && searches.length == 0 )
133         {
134             renameBookmark( bookmarks[0] );
135         }
136     }
137
138
139     /**
140      * {@inheritDoc}
141      */

142     public boolean isEnabled()
143     {
144         try
145         {
146             IConnection[] connections = getConnections();
147             IEntry[] entries = getEntries();
148             ISearch[] searches = getSearches();
149             IBookmark[] bookmarks = getBookmarks();
150
151             return connections.length + entries.length + searches.length + bookmarks.length == 1;
152
153         }
154         catch ( Exception JavaDoc e )
155         {
156             return false;
157         }
158     }
159
160
161     /**
162      * Gets the Connections
163      *
164      * @return
165      * the Connections
166      */

167     protected IConnection[] getConnections()
168     {
169         if ( getSelectedConnections().length == 1 )
170         {
171             return getSelectedConnections();
172         }
173         else
174         {
175             return new IConnection[0];
176         }
177     }
178
179
180     /**
181      * Renames a Connection.
182      *
183      * @param connection
184      * the Connection to rename
185      */

186     protected void renameConnection( final IConnection connection )
187     {
188         IInputValidator validator = new IInputValidator()
189         {
190             public String JavaDoc isValid( String JavaDoc newName )
191             {
192                 if ( connection.getName().equals( newName ) )
193                     return null;
194                 else if ( BrowserCorePlugin.getDefault().getConnectionManager().getConnection( newName ) != null )
195                     return "A connection with this name already exists.";
196                 else
197                     return null;
198             }
199         };
200
201         InputDialog dialog = new InputDialog( getShell(), "Rename Connection", "New name:", connection.getName(),
202             validator );
203
204         dialog.open();
205         String JavaDoc newName = dialog.getValue();
206         if ( newName != null )
207         {
208             connection.setName( newName );
209         }
210     }
211
212
213     /**
214      * Gets the Entries
215      *
216      * @return
217      * the Entries
218      */

219     protected IEntry[] getEntries()
220     {
221
222         IEntry entry = null;
223
224         if ( getSelectedEntries().length == 1 )
225         {
226             entry = getSelectedEntries()[0];
227         }
228         else if ( getSelectedSearchResults().length == 1 )
229         {
230             entry = getSelectedSearchResults()[0].getEntry();
231         }
232         else if ( getSelectedValues().length == 1 && getSelectedValues()[0].isRdnPart() )
233         {
234             entry = getSelectedValues()[0].getAttribute().getEntry();
235         }
236
237         if ( entry != null && !( entry instanceof RootDSE ) )
238         {
239             return new IEntry[]
240                 { entry };
241         }
242         else
243         {
244             return new IEntry[0];
245         }
246     }
247
248
249     /**
250      * Renames an Entry.
251      *
252      * @param entry
253      * the Entry to rename
254      */

255     protected void renameEntry( final IEntry entry )
256     {
257         RenameEntryDialog renameDialog = new RenameEntryDialog( getShell(), entry );
258         if ( renameDialog.open() == Dialog.OK )
259         {
260             RDN newRdn = renameDialog.getRdn();
261             boolean deleteOldRdn = renameDialog.isDeleteOldRdn();
262             if ( newRdn != null && !newRdn.equals( entry.getRdn() ) )
263             {
264                 new RenameEntryJob( entry, newRdn, deleteOldRdn ).execute();
265             }
266         }
267     }
268
269
270     /**
271      * Get the Searches.
272      *
273      * @return
274      * the Searches
275      */

276     protected ISearch[] getSearches()
277     {
278         if ( getSelectedSearches().length == 1 )
279         {
280             return getSelectedSearches();
281         }
282         else
283         {
284             return new ISearch[0];
285         }
286     }
287
288
289     /**
290      * Renames a Search.
291      *
292      * @param search
293      * the Search to rename
294      */

295     protected void renameSearch( final ISearch search )
296     {
297         IInputValidator validator = new IInputValidator()
298         {
299             public String JavaDoc isValid( String JavaDoc newName )
300             {
301                 if ( search.getName().equals( newName ) )
302                     return null;
303                 else if ( search.getConnection().getSearchManager().getSearch( newName ) != null )
304                     return "A connection with this name already exists.";
305                 else
306                     return null;
307             }
308         };
309
310         InputDialog dialog = new InputDialog( getShell(), "Rename Search", "New name:", search.getName(), validator );
311
312         dialog.open();
313         String JavaDoc newName = dialog.getValue();
314         if ( newName != null )
315         {
316             search.setName( newName );
317         }
318     }
319
320
321     /**
322      * Get the Bookmarks
323      *
324      * @return
325      * the Bookmarks
326      */

327     protected IBookmark[] getBookmarks()
328     {
329         if ( getSelectedBookmarks().length == 1 )
330         {
331             return getSelectedBookmarks();
332         }
333         else
334         {
335             return new IBookmark[0];
336         }
337     }
338
339
340     /**
341      * Renames a Bookmark
342      *
343      * @param bookmark
344      * the Bookmark to rename
345      */

346     protected void renameBookmark( final IBookmark bookmark )
347     {
348         IInputValidator validator = new IInputValidator()
349         {
350             public String JavaDoc isValid( String JavaDoc newName )
351             {
352                 if ( bookmark.getName().equals( newName ) )
353                     return null;
354                 else if ( bookmark.getConnection().getBookmarkManager().getBookmark( newName ) != null )
355                     return "A bookmark with this name already exists.";
356                 else
357                     return null;
358             }
359         };
360
361         InputDialog dialog = new InputDialog( getShell(), "Rename Bookmark", "New name:", bookmark.getName(), validator );
362
363         dialog.open();
364         String JavaDoc newName = dialog.getValue();
365         if ( newName != null )
366         {
367             bookmark.setName( newName );
368         }
369     }
370 }
371
Popular Tags