KickJava   Java API By Example, From Geeks To Geeks.

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


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.LinkedHashSet JavaDoc;
25
26 import org.apache.directory.ldapstudio.browser.common.actions.BrowserAction;
27 import org.apache.directory.ldapstudio.browser.common.dialogs.MoveEntriesDialog;
28 import org.apache.directory.ldapstudio.browser.core.internal.model.RootDSE;
29 import org.apache.directory.ldapstudio.browser.core.jobs.MoveEntriesJob;
30 import org.apache.directory.ldapstudio.browser.core.model.DN;
31 import org.apache.directory.ldapstudio.browser.core.model.IBookmark;
32 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
33 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
34 import org.eclipse.jface.dialogs.Dialog;
35 import org.eclipse.jface.resource.ImageDescriptor;
36 import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds;
37
38
39 /**
40  * This Action moves Entries from on node of the Tree to another.
41  *
42  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
43  * @version $Rev$, $Date$
44  */

45 public class MoveAction extends BrowserAction
46 {
47     /**
48      * Creates a new instance of MoveAction.
49      */

50     public MoveAction()
51     {
52         super();
53     }
54
55
56     /**
57      * {@inheritDoc}
58      */

59     public String JavaDoc getText()
60     {
61
62         IEntry[] entries = getEntries();
63         ISearch[] searches = getSearches();
64         IBookmark[] bookmarks = getBookmarks();
65
66         if ( entries.length > 0 && searches.length == 0 && bookmarks.length == 0 )
67         {
68             return entries.length == 1 ? "Move Entry..." : "Move Entries...";
69         }
70         // else if(searches.length > 0 && entries.length==0 &&
71
// bookmarks.length==0) {
72
// return entries.length == 1 ? "Move Search..." : "Move Searches...";
73
// }
74
// else if(bookmarks.length > 0 && entries.length==0 &&
75
// searches.length==0) {
76
// return entries.length == 1 ? "Move Bookmark..." : "Move
77
// Bookmarks...";
78
// }
79
else
80         {
81             return "Move...";
82         }
83     }
84
85
86     /**
87      * {@inheritDoc}
88      */

89     public ImageDescriptor getImageDescriptor()
90     {
91         return null;
92     }
93
94
95     /**
96      * {@inheritDoc}
97      */

98     public String JavaDoc getCommandId()
99     {
100         return IWorkbenchActionDefinitionIds.MOVE;
101     }
102
103
104     /**
105      * {@inheritDoc}
106      */

107     public void run()
108     {
109
110         IEntry[] entries = getEntries();
111         ISearch[] searches = getSearches();
112         IBookmark[] bookmarks = getBookmarks();
113
114         if ( entries.length > 0 && searches.length == 0 && bookmarks.length == 0 )
115         {
116             moveEntries( entries );
117         }
118         else if ( searches.length > 0 && entries.length == 0 && bookmarks.length == 0 )
119         {
120             // tbd
121
}
122         else if ( bookmarks.length > 0 && entries.length == 0 && searches.length == 0 )
123         {
124             // tbd
125
}
126     }
127
128
129     /**
130      * {@inheritDoc}
131      */

132     public boolean isEnabled()
133     {
134
135         try
136         {
137             IEntry[] entries = getEntries();
138             ISearch[] searches = getSearches();
139             IBookmark[] bookmarks = getBookmarks();
140
141             return entries.length > 0 && searches.length == 0 && bookmarks.length == 0;
142
143         }
144         catch ( Exception JavaDoc e )
145         {
146             return false;
147         }
148     }
149
150
151     /**
152      * Gets the Entries.
153      *
154      * @return
155      * the Entries
156      */

157     protected IEntry[] getEntries()
158     {
159         if ( getSelectedConnections().length + getSelectedBookmarks().length + getSelectedSearches().length
160             + getSelectedAttributes().length + getSelectedValues().length == 0
161             && getSelectedEntries().length + getSelectedSearchResults().length > 0 )
162         {
163             LinkedHashSet JavaDoc<IEntry> entriesSet = new LinkedHashSet JavaDoc<IEntry>();
164             for ( int i = 0; i < getSelectedEntries().length; i++ )
165             {
166                 entriesSet.add( getSelectedEntries()[i] );
167             }
168             for ( int i = 0; i < this.getSelectedSearchResults().length; i++ )
169             {
170                 entriesSet.add( this.getSelectedSearchResults()[i].getEntry() );
171             }
172             IEntry[] entries = ( IEntry[] ) entriesSet.toArray( new IEntry[entriesSet.size()] );
173             for ( int i = 0; i < entries.length; i++ )
174             {
175                 if ( entries[i] == null || entries[i] instanceof RootDSE )
176                 {
177                     return new IEntry[0];
178                 }
179             }
180             return entries;
181         }
182         else
183         {
184             return new IEntry[0];
185         }
186     }
187
188
189     /**
190      * Opens a Move Entries Dialog and lauches the Move Entries Jobs.
191      *
192      * @param entries
193      * the entries to move
194      */

195     protected void moveEntries( final IEntry[] entries )
196     {
197         MoveEntriesDialog moveDialog = new MoveEntriesDialog( getShell(), entries );
198         if ( moveDialog.open() == Dialog.OK )
199         {
200             DN newParentDn = moveDialog.getParentDn();
201             if ( newParentDn != null /* && !newRdn.equals(entry.getRdn()) */)
202             {
203                 IEntry newParentEntry = entries[0].getConnection().getEntryFromCache( newParentDn );
204                 if ( newParentEntry != null )
205                 {
206                     new MoveEntriesJob( entries, newParentEntry ).execute();
207                 }
208             }
209         }
210     }
211
212
213     /**
214      * Gets the searches.
215      *
216      * @return
217      * the searches
218      */

219     protected ISearch[] getSearches()
220     {
221         if ( getSelectedSearches().length == 1 )
222         {
223             return getSelectedSearches();
224         }
225         else
226         {
227             return new ISearch[0];
228         }
229     }
230
231
232     /**
233      * Gets the Bookmarks
234      *
235      * @return
236      * the Bookmarks
237      */

238     protected IBookmark[] getBookmarks()
239     {
240         if ( getSelectedBookmarks().length == 1 )
241         {
242             return getSelectedBookmarks();
243         }
244         else
245         {
246             return new IBookmark[0];
247         }
248     }
249 }
250
Popular Tags