Skip to main content
summaryrefslogtreecommitdiffstats
blob: e67662aa714325fa4acb811ec270858e9177ead2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*******************************************************************************
 * Copyright (c) 2004, 2016 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.ui.internal.intro.impl.model;

import org.eclipse.core.runtime.IRegistryChangeEvent;
import org.eclipse.core.runtime.PerformanceStats;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
import org.eclipse.ui.internal.intro.impl.IIntroConstants;
import org.eclipse.ui.internal.intro.impl.IntroPlugin;
import org.eclipse.ui.internal.intro.impl.Messages;
import org.eclipse.ui.internal.intro.impl.model.viewer.IntroModelContentProvider;
import org.eclipse.ui.internal.intro.impl.model.viewer.IntroModelLabelProvider;
import org.eclipse.ui.internal.intro.impl.util.ImageUtil;
import org.eclipse.ui.internal.intro.impl.util.Log;
import org.eclipse.ui.internal.intro.impl.util.Util;
import org.eclipse.ui.intro.IIntroPart;
import org.eclipse.ui.intro.config.CustomizableIntroPart;

/**
 * UI Implementation class that represents a Presentation Part. This class is
 * instantiated from plugin markup and so we need a default constructor,
 * including in subclasses. It has some base methods, including maintaining a
 * history of navigation locations. Also, dynamic awarness is honored here.
 *
 */
public abstract class AbstractIntroPartImplementation {

    // CustomizableIntroPart instance.
    private CustomizableIntroPart introPart = null;

    // IMemento for restoring state.
    private IMemento memento;

    protected History history = new History();

    // flag used to enable logging of perf data for full UI creation only once.
    // Since standbyStateChanged is called several times, flag is used in method
    // to filter out all subsequent calls.
    boolean logUIcreationTime = true;

    // Global actions
    protected Action backAction = new Action() {

        {
            setToolTipText(Messages.Browser_backwardButton_tooltip);
            setImageDescriptor(ImageUtil
                .createImageDescriptor("full/elcl16/backward_nav.gif")); //$NON-NLS-1$
            setDisabledImageDescriptor(ImageUtil
                .createImageDescriptor("full/dlcl16/backward_nav.gif")); //$NON-NLS-1$
        }

        @Override
		public void run() {
            navigateBackward();
        }
    };

    protected Action forwardAction = new Action() {

        {
            setToolTipText(Messages.Browser_forwardButton_tooltip);
            setImageDescriptor(ImageUtil
                .createImageDescriptor("full/elcl16/forward_nav.gif")); //$NON-NLS-1$
            setDisabledImageDescriptor(ImageUtil
                .createImageDescriptor("full/dlcl16/forward_nav.gif")); //$NON-NLS-1$
        }

        @Override
		public void run() {
            navigateForward();
        }
    };

    protected Action homeAction = new Action() {

        {
            setToolTipText(Messages.Browser_homeButton_tooltip);
            setImageDescriptor(ImageUtil
                .createImageDescriptor("full/elcl16/home_nav.gif")); //$NON-NLS-1$
            setDisabledImageDescriptor(ImageUtil
                .createImageDescriptor("full/dlcl16/home_nav.gif")); //$NON-NLS-1$
        }

        @Override
		public void run() {
            navigateHome();
        }
    };

    protected Action viewIntroModelAction = new Action() {

        {
            setToolTipText(Messages.IntroPart_showContentButton_tooltip);
            setImageDescriptor(ImageUtil
                .createImageDescriptor("contents_view.gif")); //$NON-NLS-1$
        }

        @Override
		public void run() {
            ElementTreeSelectionDialog treeViewer = new ElementTreeSelectionDialog(
                getIntroPart().getIntroSite().getShell(),
                new IntroModelLabelProvider(), new IntroModelContentProvider());
            treeViewer.setInput(getModel());
            treeViewer.open();
        }
    };

    /**
     * Creates the UI based on this implementation class. .
     *
     * @param parent
     */
    public abstract void createPartControl(Composite parent);

    /**
     * Called when the init method is called in the IIntroPart. Subclasses may
     * extend, for example to get the passed Memento. When extending, make sure
     * you include a call to super.
     *
     * @param introPart
     */
    public void init(IIntroPart introPart, IMemento memento) {
        // we know the class type to cast to.
        this.introPart = (CustomizableIntroPart) introPart;
        this.memento = memento;
    }

    /**
     * @return
     */
    public IntroModelRoot getModel() {
        return IntroPlugin.getDefault().getIntroModelRoot();
    }

    /**
     * @return Returns the introPart.
     */
    public CustomizableIntroPart getIntroPart() {
        return introPart;
    }


    /**
     * Updates the UI navigation history with either a real URL.
     *
     * @param location
     */
    public void updateHistory(String location) {
        history.updateHistory(location);
        updateNavigationActionsState();
    }

    /**
     * Updates the UI navigation history with a page ID.
     *
     * @param pageId
     */
    public void updateHistory(AbstractIntroPage page) {
        history.updateHistory(page);
        updateNavigationActionsState();
    }


    /**
     * Subclasses must implement to set the state of the navigation actions in
     * the toolbar.
     *
     */
    public abstract void setFocus();


    /**
     * Subclasses must implement to update the intro view actions when history
     * is updated.
     *
     */
    protected abstract void updateNavigationActionsState();



    public abstract boolean navigateBackward();

    public abstract boolean navigateForward();

    public abstract boolean navigateHome();


    /**
     * Called when the IntroPart is disposed. Subclasses should override to
     * dispose of resources. By default, this implementation does nothing.
     */
    public void dispose() {
        // no-op
    }


    /*
     * Add the Intro Model Viewer as an action to all implementations.
     */
    protected void addToolBarActions() {
        // Handle menus:
        IActionBars actionBars = getIntroPart().getIntroSite().getActionBars();
        IToolBarManager toolBarManager = actionBars.getToolBarManager();
        toolBarManager.add(viewIntroModelAction);
        toolBarManager.update(true);
        actionBars.updateActionBars();
    }

    /**
     * Called when the Intro changes state. This method should not be
     * subclassed. It adds performance logging calls. Subclasses must implement
     * doStandbyStateChanged instead.
     *
     * @param standby
     */
    public void standbyStateChanged(boolean standby, boolean isStandbyPartNeeded) {
        PerformanceStats setStandbyStateStats = null;
        long start = 0;
        if (Log.logPerformance) {
            if (logUIcreationTime && PerformanceStats.ENABLED) {
                PerformanceStats stats = PerformanceStats.getStats(
                    IIntroConstants.PERF_UI_ZOOM, IIntroConstants.INTRO);
                stats.endRun();
                Util
                    .logPerformanceMessage(
                        "(perf stats) time spent in UI code before content is displayed (standbyStateChanged event is fired) ", //$NON-NLS-1$
                        stats.getRunningTime());
                stats.reset();
            }

            // standby time.
            setStandbyStateStats = PerformanceStats.getStats(
                IIntroConstants.PERF_SET_STANDBY_STATE, IIntroConstants.INTRO);
            setStandbyStateStats.startRun();
            start = System.currentTimeMillis();
        }


        doStandbyStateChanged(standby, isStandbyPartNeeded);

        // now log performance
        if (Log.logPerformance) {
            if (PerformanceStats.ENABLED) {
                setStandbyStateStats.endRun();
                Util
                    .logPerformanceMessage(
                        "(perf stats) setting standby state (zooming, displaying content) took:", //$NON-NLS-1$
                        +setStandbyStateStats.getRunningTime());
                setStandbyStateStats.reset();
            } else
                Util
                    .logPerformanceTime(
                        "setting standby state (zooming, generating content, setText() ) took:", //$NON-NLS-1$
                        +start);

            if (logUIcreationTime) {
                if (PerformanceStats.ENABLED) {
                    PerformanceStats stats = PerformanceStats.getStats(
                        IIntroConstants.PERF_VIEW_CREATION_TIME,
                        IIntroConstants.INTRO);
                    stats.endRun();
                    Util
                        .logPerformanceMessage(
                            "END - (perf stats): creating CustomizableIntroPart view took:", //$NON-NLS-1$
                            +stats.getRunningTime());
                    stats.reset();
                } else
                    Util.logPerformanceTime(
                        "END: creating CustomizableIntroPart view took:", //$NON-NLS-1$
                        +IntroPlugin.getDefault().gettUICreationStartTime());


                // prevent further logging of UI creation time.
                logUIcreationTime = false;
            }

        }
    }



    /*
     * Subclasses must implement the actual logic for the method.
     */
    protected abstract void doStandbyStateChanged(boolean standby,
            boolean isStandbyPartNeeded);


    /**
     * Save the current state of the intro. Currently, we only store information
     * about the most recently visited intro page. In static case, the last HTML
     * page is remembered. In dynamic case, the last UI page or HTML page is
     * remembered.
     *
     * Note: This method saves the last visited intro page in a dynamic case.
     * Subclasses need to extend to get the desired behavior relavent to the
     * specific implementation. Broswer implementation needs to cache an http
     * web page, if it happens to be the last page visited.
     *
     * @param memento
     */
    public void saveState(IMemento memento) {
        saveCurrentPage(memento);
    }


    /**
     * This method saves the most recently visited dynamic intro page in the
     * memento. If a given implementation requires saving alternative
     * information (e.g., information about the most recently visited static
     * page) it should override this method.
     *
     * @param memento
     */
    protected void saveCurrentPage(IMemento memento) {
        IntroModelRoot model = getModel();

        if (memento == null || model == null)
            return;
        String currentPage = model.getCurrentPageId();
        if (currentPage != null && currentPage.length() > 0) {
            memento.putString(IIntroConstants.MEMENTO_CURRENT_PAGE_ATT,
                currentPage);
        }
    }


    /**
     * get the last page if it was stored in memento. This page is the last
     * visited intro page. It can be a intro page id, in the case of dynamic
     * intro. Or it can be an http in the case of static intro. It can also be
     * an http in the case of dynamic intro where the last visited page is a
     * url.
     */
    protected String getCachedCurrentPage() {
    	// Check to see if the start page has been overridden because
    	// content
    	String newContentPage = ExtensionMap.getInstance().getStartPage();
    	if (newContentPage != null) {
    		return newContentPage;
    	}
        IMemento memento = getMemento();
        if (memento == null) {
            String startPageId = getModel().getStartPageId();
            if (startPageId.length() > 0) {
			    return startPageId;
            } else {
            	return null;
            }
        }
        return memento.getString(IIntroConstants.MEMENTO_CURRENT_PAGE_ATT);
    }


    /**
     * @return Returns the memento passed on creation.
     */
    public IMemento getMemento() {
        return memento;
    }

    /**
     * Support dynamic awarness. Clear cached models first, then update UI by
     * delegating to implementation.
     *
     * @see org.eclipse.core.runtime.IRegistryChangeListener#registryChanged(org.eclipse.core.runtime.IRegistryChangeEvent)
     */
    public void registryChanged(IRegistryChangeEvent event) {
        history.clear();
        // give implementation a chance to react to change.
        handleRegistryChanged(event);
    }

    /*
     * Handle reacting to plugin registry changes. This method will only be
     * called when regitry changes pertaining to Intro extension points is
     * detected.
     */
    protected abstract void handleRegistryChanged(IRegistryChangeEvent event);


    public History getHistory() {
        return history;
    }


}

Back to the top