KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
29 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants;
30 import org.apache.directory.ldapstudio.browser.core.jobs.InitializeAttributesJob;
31 import org.apache.directory.ldapstudio.browser.core.jobs.InitializeChildrenJob;
32 import org.apache.directory.ldapstudio.browser.core.jobs.SearchJob;
33 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
34 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
35 import org.eclipse.jface.resource.ImageDescriptor;
36
37
38 /**
39  * This Action refreshes the selected item.
40  *
41  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
42  * @version $Rev$, $Date$
43  */

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

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

58     public String JavaDoc getText()
59     {
60         IEntry[] entries = getEntries();
61         ISearch[] searches = getSearches();
62         IEntry entryInput = getEntryInput();
63         ISearch searchInput = getSearchInput();
64
65         if ( entries.length > 0 && searches.length == 0 && entryInput == null && searchInput == null )
66         {
67             return "Reload Attributes and Children";
68         }
69         else if ( searches.length > 0 && entries.length == 0 && entryInput == null && searchInput == null )
70         {
71             boolean searchAgain = true;
72             for ( int i = 0; i < searches.length; i++ )
73             {
74                 if ( searches[i].getSearchResults() == null )
75                 {
76                     searchAgain = false;
77                     break;
78                 }
79             }
80             if ( searchAgain )
81             {
82                 return "Search Again";
83             }
84             else
85             {
86                 return searches.length == 1 ? "Perform Search" : "Perform Searches";
87             }
88         }
89         else if ( entryInput != null && searches.length == 0 && entries.length == 0 && searchInput == null )
90         {
91             return "Reload Attributes";
92         }
93         else if ( searchInput != null && searches.length == 0 && entryInput == null )
94         {
95             return searchInput.getSearchResults() == null ? "Perform Search" : "Search Again";
96         }
97         else
98         {
99             return "Refresh";
100         }
101     }
102
103
104     /**
105      * {@inheritDoc}
106      */

107     public ImageDescriptor getImageDescriptor()
108     {
109         return BrowserCommonActivator.getDefault().getImageDescriptor( BrowserCommonConstants.IMG_REFRESH );
110     }
111
112
113     /**
114      * {@inheritDoc}
115      */

116     public String JavaDoc getCommandId()
117     {
118         return "org.eclipse.ui.file.refresh";
119     }
120
121
122     /**
123      * {@inheritDoc}
124      */

125     public void run()
126     {
127         IEntry[] entries = getEntries();
128         ISearch[] searches = getSearches();
129         IEntry entryInput = getEntryInput();
130         ISearch searchInput = getSearchInput();
131         boolean soa = BrowserCommonActivator.getDefault().getPreferenceStore().getBoolean(
132             BrowserCommonConstants.PREFERENCE_ENTRYEDITOR_SHOW_OPERATIONAL_ATTRIBUTES );
133
134         if ( entries.length > 0 )
135         {
136             new InitializeAttributesJob( entries, soa ).execute();
137             new InitializeChildrenJob( entries ).execute();
138         }
139         if ( searches.length > 0 )
140         {
141             new SearchJob( searches ).execute();
142         }
143
144         if ( entryInput != null )
145         {
146             new InitializeAttributesJob( new IEntry[]
147                 { entryInput }, soa ).execute();
148         }
149         if ( searchInput != null )
150         {
151             new SearchJob( new ISearch[]
152                 { searchInput } ).execute();
153         }
154     }
155
156
157     /**
158      * {@inheritDoc}
159      */

160     public boolean isEnabled()
161     {
162         IEntry[] entries = getEntries();
163         ISearch[] searches = getSearches();
164         IEntry entryInput = getEntryInput();
165         ISearch searchInput = getSearchInput();
166
167         return entries.length > 0 || searches.length > 0 || entryInput != null || searchInput != null;
168     }
169
170
171     /**
172      * Gets the Entries
173      *
174      * @return
175      * the entries
176      */

177     protected IEntry[] getEntries()
178     {
179         List JavaDoc<IEntry> entriesList = new ArrayList JavaDoc<IEntry>();
180         entriesList.addAll( Arrays.asList( getSelectedEntries() ) );
181         for ( int i = 0; i < getSelectedSearchResults().length; i++ )
182         {
183             entriesList.add( getSelectedSearchResults()[i].getEntry() );
184         }
185         for ( int i = 0; i < getSelectedBookmarks().length; i++ )
186         {
187             entriesList.add( getSelectedBookmarks()[i].getEntry() );
188         }
189         return ( IEntry[] ) entriesList.toArray( new IEntry[entriesList.size()] );
190     }
191
192
193     /**
194      * Gets the Searches.
195      *
196      * @return
197      * the Searches
198      */

199     protected ISearch[] getSearches()
200     {
201         return getSelectedSearches();
202     }
203
204
205     /**
206      * Gets the Entry Input.
207      *
208      * @return
209      * the Entry Input
210      */

211     private IEntry getEntryInput()
212     {
213         if ( getInput() != null && getInput() instanceof IEntry )
214         {
215             return ( IEntry ) getInput();
216         }
217         else
218         {
219             return null;
220         }
221     }
222
223
224     /**
225      * Gets the Search Input.
226      *
227      * @return
228      * the Search Input
229      */

230     private ISearch getSearchInput()
231     {
232         if ( getInput() != null && getInput() instanceof ISearch )
233         {
234             return ( ISearch ) getInput();
235         }
236         else
237         {
238             return null;
239         }
240     }
241 }
242
Popular Tags