KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > model > AbstractIntroPartImplementation


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.intro.impl.model;
13
14 import org.eclipse.core.runtime.IRegistryChangeEvent;
15 import org.eclipse.core.runtime.PerformanceStats;
16 import org.eclipse.jface.action.Action;
17 import org.eclipse.jface.action.IToolBarManager;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.ui.IActionBars;
20 import org.eclipse.ui.IMemento;
21 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
22 import org.eclipse.ui.internal.intro.impl.IIntroConstants;
23 import org.eclipse.ui.internal.intro.impl.IntroPlugin;
24 import org.eclipse.ui.internal.intro.impl.Messages;
25 import org.eclipse.ui.internal.intro.impl.model.viewer.IntroModelContentProvider;
26 import org.eclipse.ui.internal.intro.impl.model.viewer.IntroModelLabelProvider;
27 import org.eclipse.ui.internal.intro.impl.util.ImageUtil;
28 import org.eclipse.ui.internal.intro.impl.util.Log;
29 import org.eclipse.ui.internal.intro.impl.util.Util;
30 import org.eclipse.ui.intro.IIntroPart;
31 import org.eclipse.ui.intro.config.CustomizableIntroPart;
32
33 /**
34  * UI Implementation class that represents a Presentation Part. This class is
35  * instantiated from plugin markup and so we need a default constructor,
36  * including in subclasses. It has some base methods, including maintaining a
37  * history of navigation locations. Also, dynamic awarness is honored here.
38  *
39  */

40 public abstract class AbstractIntroPartImplementation {
41
42     // CustomizableIntroPart instance.
43
private CustomizableIntroPart introPart = null;
44
45     // IMemento for restoring state.
46
private IMemento memento;
47
48     protected History history = new History();
49
50     // flag used to enable logging of perf data for full UI creation only once.
51
// Since standbyStateChanged is called several times, flag is used in method
52
// to filter out all subsequent calls.
53
boolean logUIcreationTime = true;
54
55     // Global actions
56
protected Action backAction = new Action() {
57
58         {
59             setToolTipText(Messages.Browser_backwardButton_tooltip);
60             setImageDescriptor(ImageUtil
61                 .createImageDescriptor("full/elcl16/backward_nav.gif")); //$NON-NLS-1$
62
setDisabledImageDescriptor(ImageUtil
63                 .createImageDescriptor("full/dlcl16/backward_nav.gif")); //$NON-NLS-1$
64
}
65
66         public void run() {
67             navigateBackward();
68         }
69     };
70
71     protected Action forwardAction = new Action() {
72
73         {
74             setToolTipText(Messages.Browser_forwardButton_tooltip);
75             setImageDescriptor(ImageUtil
76                 .createImageDescriptor("full/elcl16/forward_nav.gif")); //$NON-NLS-1$
77
setDisabledImageDescriptor(ImageUtil
78                 .createImageDescriptor("full/dlcl16/forward_nav.gif")); //$NON-NLS-1$
79
}
80
81         public void run() {
82             navigateForward();
83         }
84     };
85
86     protected Action homeAction = new Action() {
87
88         {
89             setToolTipText(Messages.Browser_homeButton_tooltip);
90             setImageDescriptor(ImageUtil
91                 .createImageDescriptor("full/elcl16/home_nav.gif")); //$NON-NLS-1$
92
setDisabledImageDescriptor(ImageUtil
93                 .createImageDescriptor("full/dlcl16/home_nav.gif")); //$NON-NLS-1$
94
}
95
96         public void run() {
97             navigateHome();
98         }
99     };
100
101     protected Action viewIntroModelAction = new Action() {
102
103         {
104             setToolTipText(Messages.IntroPart_showContentButton_tooltip);
105             setImageDescriptor(ImageUtil
106                 .createImageDescriptor("contents_view.gif")); //$NON-NLS-1$
107
}
108
109         public void run() {
110             ElementTreeSelectionDialog treeViewer = new ElementTreeSelectionDialog(
111                 getIntroPart().getIntroSite().getShell(),
112                 new IntroModelLabelProvider(), new IntroModelContentProvider());
113             treeViewer.setInput(getModel());
114             treeViewer.open();
115         }
116     };
117
118     /**
119      * Creates the UI based on this implementation class. .
120      *
121      * @param parent
122      */

123     public abstract void createPartControl(Composite parent);
124
125     /**
126      * Called when the init method is called in the IIntroPart. Subclasses may
127      * extend, for example to get the passed Memento. When extending, make sure
128      * you include a call to super.
129      *
130      * @param introPart
131      */

132     public void init(IIntroPart introPart, IMemento memento) {
133         // we know the class type to cast to.
134
this.introPart = (CustomizableIntroPart) introPart;
135         this.memento = memento;
136     }
137
138     /**
139      * @return
140      */

141     public IntroModelRoot getModel() {
142         return IntroPlugin.getDefault().getIntroModelRoot();
143     }
144
145     /**
146      * @return Returns the introPart.
147      */

148     public CustomizableIntroPart getIntroPart() {
149         return introPart;
150     }
151
152
153     /**
154      * Updates the UI navigation history with either a real URL.
155      *
156      * @param location
157      */

158     public void updateHistory(String JavaDoc location) {
159         history.updateHistory(location);
160         updateNavigationActionsState();
161     }
162
163     /**
164      * Updates the UI navigation history with a page ID.
165      *
166      * @param pageId
167      */

168     public void updateHistory(AbstractIntroPage page) {
169         history.updateHistory(page);
170         updateNavigationActionsState();
171     }
172
173
174     /**
175      * Subclasses must implement to set the state of the navigation actions in
176      * the toolbar.
177      *
178      */

179     public abstract void setFocus();
180
181
182     /**
183      * Subclasses must implement to update the intro view actions when history
184      * is updated.
185      *
186      */

187     protected abstract void updateNavigationActionsState();
188
189
190
191     public abstract boolean navigateBackward();
192
193     public abstract boolean navigateForward();
194
195     public abstract boolean navigateHome();
196
197
198     /**
199      * Called when the IntroPart is disposed. Subclasses should override to
200      * dispose of resources. By default, this implementation does nothing.
201      */

202     public void dispose() {
203         // no-op
204
}
205
206
207     /*
208      * Add the Intro Model Viewer as an action to all implementations.
209      */

210     protected void addToolBarActions() {
211         // Handle menus:
212
IActionBars actionBars = getIntroPart().getIntroSite().getActionBars();
213         IToolBarManager toolBarManager = actionBars.getToolBarManager();
214         toolBarManager.add(viewIntroModelAction);
215         toolBarManager.update(true);
216         actionBars.updateActionBars();
217     }
218
219     /**
220      * Called when the Intro changes state. This method should not be
221      * subclassed. It adds performance logging calls. Subclasses must implement
222      * doStandbyStateChanged instead.
223      *
224      * @param standby
225      */

226     public void standbyStateChanged(boolean standby, boolean isStandbyPartNeeded) {
227         PerformanceStats setStandbyStateStats = null;
228         long start = 0;
229         if (Log.logPerformance) {
230             if (logUIcreationTime && PerformanceStats.ENABLED) {
231                 PerformanceStats stats = PerformanceStats.getStats(
232                     IIntroConstants.PERF_UI_ZOOM, IIntroConstants.INTRO);
233                 stats.endRun();
234                 Util
235                     .logPerformanceMessage(
236                         "(perf stats) time spent in UI code before content is displayed (standbyStateChanged event is fired) ", //$NON-NLS-1$
237
stats.getRunningTime());
238                 stats.reset();
239             }
240
241             // standby time.
242
setStandbyStateStats = PerformanceStats.getStats(
243                 IIntroConstants.PERF_SET_STANDBY_STATE, IIntroConstants.INTRO);
244             setStandbyStateStats.startRun();
245             start = System.currentTimeMillis();
246         }
247
248
249         doStandbyStateChanged(standby, isStandbyPartNeeded);
250
251         // now log performance
252
if (Log.logPerformance) {
253             if (PerformanceStats.ENABLED) {
254                 setStandbyStateStats.endRun();
255                 Util
256                     .logPerformanceMessage(
257                         "(perf stats) setting standby state (zooming, displaying content) took:", //$NON-NLS-1$
258
+setStandbyStateStats.getRunningTime());
259                 setStandbyStateStats.reset();
260             } else
261                 Util
262                     .logPerformanceTime(
263                         "setting standby state (zooming, generating content, setText() ) took:", //$NON-NLS-1$
264
+start);
265
266             if (logUIcreationTime) {
267                 if (PerformanceStats.ENABLED) {
268                     PerformanceStats stats = PerformanceStats.getStats(
269                         IIntroConstants.PERF_VIEW_CREATION_TIME,
270                         IIntroConstants.INTRO);
271                     stats.endRun();
272                     Util
273                         .logPerformanceMessage(
274                             "END - (perf stats): creating CustomizableIntroPart view took:", //$NON-NLS-1$
275
+stats.getRunningTime());
276                     stats.reset();
277                 } else
278                     Util.logPerformanceTime(
279                         "END: creating CustomizableIntroPart view took:", //$NON-NLS-1$
280
+IntroPlugin.getDefault().gettUICreationStartTime());
281
282
283                 // prevent further logging of UI creation time.
284
logUIcreationTime = false;
285             }
286
287         }
288     }
289
290
291
292     /*
293      * Subclasses must implement the actual logic for the method.
294      */

295     protected abstract void doStandbyStateChanged(boolean standby,
296             boolean isStandbyPartNeeded);
297
298
299     /**
300      * Save the current state of the intro. Currently, we only store information
301      * about the most recently visited intro page. In static case, the last HTML
302      * page is remembered. In dynamic case, the last UI page or HTML page is
303      * remembered.
304      *
305      * Note: This method saves the last visited intro page in a dynamic case.
306      * Subclasses need to extend to get the desired behavior relavent to the
307      * specific implementation. Broswer implementation needs to cache an http
308      * web page, if it happens to be the last page visited.
309      *
310      * @param memento
311      */

312     public void saveState(IMemento memento) {
313         saveCurrentPage(memento);
314     }
315
316
317     /**
318      * This method saves the most recently visited dynamic intro page in the
319      * memento. If a given implementation requires saving alternative
320      * information (e.g., information about the most recently visited static
321      * page) it should override this method.
322      *
323      * @param memento
324      */

325     protected void saveCurrentPage(IMemento memento) {
326         IntroModelRoot model = getModel();
327
328         if (memento == null || model == null)
329             return;
330         String JavaDoc currentPage = model.getCurrentPageId();
331         if (currentPage != null && currentPage.length() > 0) {
332             memento.putString(IIntroConstants.MEMENTO_CURRENT_PAGE_ATT,
333                 currentPage);
334         }
335     }
336
337
338     /**
339      * get the last page if it was stored in memento. This page is the last
340      * visited intro page. It can be a intro page id, in the case of dynamic
341      * intro. Or it can be an http in the case of static intro. It can also be
342      * an http in the case of dynamic intro where the last visited page is a
343      * url.
344      */

345     protected String JavaDoc getCachedCurrentPage() {
346         // Check to see if the start page has been overriden because
347
// content
348
String JavaDoc newContentPage = ExtensionMap.getInstance().getStartPage();
349         if (newContentPage != null) {
350             return newContentPage;
351         }
352         IMemento memento = getMemento();
353         if (memento == null)
354             return null;
355         return memento.getString(IIntroConstants.MEMENTO_CURRENT_PAGE_ATT);
356     }
357
358
359     /**
360      * @return Returns the memento passed on creation.
361      */

362     public IMemento getMemento() {
363         return memento;
364     }
365
366     /**
367      * Support dynamic awarness. Clear cached models first, then update UI by
368      * delegating to implementation.
369      *
370      * @see org.eclipse.core.runtime.IRegistryChangeListener#registryChanged(org.eclipse.core.runtime.IRegistryChangeEvent)
371      */

372     public void registryChanged(IRegistryChangeEvent event) {
373         history.clear();
374         // give implementation a chance to react to change.
375
handleRegistryChanged(event);
376     }
377
378     /*
379      * Handle reacting to plugin registry changes. This method will only be
380      * called when regitry changes pertaining to Intro extension points is
381      * detected.
382      */

383     protected abstract void handleRegistryChanged(IRegistryChangeEvent event);
384
385
386     public History getHistory() {
387         return history;
388     }
389
390
391 }
392
Popular Tags