Skip to main content
summaryrefslogtreecommitdiffstats
blob: d8cbf591b67a079b047528ea3cea49bbf243bc8e (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
/*******************************************************************************
 * Copyright (c) 2001, 2007 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
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/
package org.eclipse.wst.sse.ui.views.contentoutline;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.TransferDragSourceListener;
import org.eclipse.jface.util.TransferDropTargetListener;
import org.eclipse.jface.viewers.IContentProvider;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.wst.sse.ui.internal.SSEUIMessages;
import org.eclipse.wst.sse.ui.internal.SSEUIPlugin;
import org.eclipse.wst.sse.ui.internal.contentoutline.PropertyChangeUpdateAction;
import org.eclipse.wst.sse.ui.internal.contentoutline.PropertyChangeUpdateActionContributionItem;
import org.eclipse.wst.sse.ui.internal.editor.EditorPluginImageHelper;
import org.eclipse.wst.sse.ui.internal.editor.EditorPluginImages;

/**
 * Basic Configuration class for Outline Pages
 * 
 * @since 1.0
 */
public abstract class ContentOutlineConfiguration {
	/**
	 * Add a collapse action to help with navigation.
	 */
	private class CollapseTreeAction extends Action {
		private TreeViewer fTreeViewer = null;

		public CollapseTreeAction(TreeViewer viewer) {
			super(SSEUIMessages.ContentOutlineConfiguration_0, AS_PUSH_BUTTON); //$NON-NLS-1$
			setImageDescriptor(COLLAPSE_E);
			setDisabledImageDescriptor(COLLAPSE_D);
			setToolTipText(getText());
			fTreeViewer = viewer;
		}

		public void run() {
			super.run();
			fTreeViewer.collapseAll();
		}
	}

	/**
	 * Toggles whether incoming selection notification affects us
	 */
	private class ToggleLinkAction extends PropertyChangeUpdateAction {
		public ToggleLinkAction(IPreferenceStore store, String preference) {
			super(SSEUIMessages.ContentOutlineConfiguration_1, store, preference, true); //$NON-NLS-1$
			setToolTipText(getText());
			setDisabledImageDescriptor(SYNCED_D);
			setImageDescriptor(SYNCED_E);
			update();
		}

		public void update() {
			super.update();
			setLinkWithEditor(isChecked());
		}
	}

	ImageDescriptor COLLAPSE_D = EditorPluginImageHelper.getInstance().getImageDescriptor(EditorPluginImages.IMG_DLCL_COLLAPSEALL);
	ImageDescriptor COLLAPSE_E = EditorPluginImageHelper.getInstance().getImageDescriptor(EditorPluginImages.IMG_ELCL_COLLAPSEALL);

	private boolean fIsLinkWithEditor = false;

	private ILabelProvider fLabelProvider;
	private IContributionItem[] fMenuContributions = null;
	private IContributionItem[] fToolbarContributions = null;
	private final String OUTLINE_LINK_PREF = "outline-link-editor"; //$NON-NLS-1$
	ImageDescriptor SYNCED_D = EditorPluginImageHelper.getInstance().getImageDescriptor(EditorPluginImages.IMG_DLCL_SYNCED);
	ImageDescriptor SYNCED_E = EditorPluginImageHelper.getInstance().getImageDescriptor(EditorPluginImages.IMG_ELCL_SYNCED);

	/**
	 * Create new instance of ContentOutlineConfiguration
	 */
	public ContentOutlineConfiguration() {
		// Must have empty constructor to createExecutableExtension
		super();
	}

	/**
	 * Creates the contributions for the view's local menu. Subclasses should
	 * merge their contributions with these.
	 * 
	 * @param viewer
	 *            the TreeViewer associated with this configuration
	 * @return menu contributions
	 */
	protected IContributionItem[] createMenuContributions(TreeViewer viewer) {
		IContributionItem toggleLinkItem = new PropertyChangeUpdateActionContributionItem(new ToggleLinkAction(getPreferenceStore(), OUTLINE_LINK_PREF));
		IContributionItem[] items = new IContributionItem[]{toggleLinkItem};
		return items;
	}

	/**
	 * Creates the toolbar contributions. Subclasses should merge their
	 * contributions with these.
	 * 
	 * @param viewer
	 *            the TreeViewer associated with this configuration
	 * @return toolbar contributions
	 */
	protected IContributionItem[] createToolbarContributions(TreeViewer viewer) {
		IContributionItem collapseAllItem = new ActionContributionItem(new CollapseTreeAction(viewer));
		IContributionItem[] items = new IContributionItem[]{collapseAllItem};
		return items;
	}

	/**
	 * Returns the ContentProvider to use with the given viewer.
	 * 
	 * @param viewer
	 *            the TreeViewer associated with this configuration
	 * @return the IContentProvider to use with this viewer
	 */
	public abstract IContentProvider getContentProvider(TreeViewer viewer);

	/**
	 * Returns an array of KeyListeners to attach to the given viewer's
	 * control or null.
	 * 
	 * @param viewer
	 *            the TreeViewer associated with this configuration
	 * @return an array of KeyListeners to attach to the TreeViewer's Control,
	 *         or null. The listeners should adhere to the KeyEvent.doit field
	 *         to ensure proper behaviors. Ordering of the event notifications
	 *         is dependent on the Control in the TreeViewer.
	 */
	public KeyListener[] getKeyListeners(TreeViewer viewer) {
		return null;
	}

	/**
	 * Returns the LabelProvider for the items within the given viewer.
	 * 
	 * @param viewer
	 *            the TreeViewer associated with this configuration
	 * @return the ILabelProvider for items within the viewer
	 */
	public ILabelProvider getLabelProvider(TreeViewer viewer) {
		if (fLabelProvider == null)
			fLabelProvider = new LabelProvider();
		return fLabelProvider;
	}

	/**
	 * Returns the menu contribution items for the local menu in the outline.
	 * 
	 * @param viewer
	 *            the TreeViewer associated with this configuration
	 * @return IContributionItem[] for the local menu
	 */
	public final IContributionItem[] getMenuContributions(TreeViewer viewer) {
		if (fMenuContributions == null) {
			fMenuContributions = createMenuContributions(viewer);
		}
		return fMenuContributions;
	}

	/**
	 * Returns the menu listener to notify when the given viewer's context
	 * menu is about to be shown or null.
	 * 
	 * @param viewer
	 *            the TreeViewer associated with this configuration
	 * @return the IMenuListener to notify when the viewer's context menu is
	 *         about to be shown, or null
	 */
	public IMenuListener getMenuListener(TreeViewer viewer) {
		return null;
	}

	/**
	 * Returns the PreferenceStore to use for this configuration.
	 * 
	 * @return the preference store in which to remember preferences (such as
	 *         the link-with-editor toggle state)
	 */
	protected IPreferenceStore getPreferenceStore() {
		return SSEUIPlugin.getInstance().getPreferenceStore();
	}

	/**
	 * Returns the (filtered) selection from the given selection.
	 * 
	 * @param selection
	 *            model selection
	 * @param viewer
	 *            the TreeViewer associated with this configuration
	 * @return The (filtered) selection from this event. Uses include mapping
	 *         model selection onto elements provided by the content provider.
	 *         Should only return elements that will be shown in the Tree
	 *         Control.
	 */
	public ISelection getSelection(TreeViewer viewer, ISelection selection) {
		return selection;
	}

	/**
	 * @since 2.0
	 * @param treeViewer
	 * @return a label provider providing the status line contents
	 */
	public ILabelProvider getStatusLineLabelProvider(TreeViewer treeViewer) {
		return null;
	}

	/**
	 * Returns contribution items for the local toolbar in the outline.
	 * 
	 * @param viewer
	 *            the TreeViewer associated with this configuration
	 * @return IContributionItem[] for the local toolbar
	 */
	public final IContributionItem[] getToolbarContributions(TreeViewer viewer) {
		if (fToolbarContributions == null) {
			fToolbarContributions = createToolbarContributions(viewer);
		}
		return fToolbarContributions;
	}

	/**
	 * Adopted since you can't easily removeDragSupport from
	 * StructuredViewers.
	 * 
	 * @param treeViewer
	 *            the TreeViewer associated with this configuration
	 * @return an array of TransferDragSourceListeners
	 */
	public TransferDragSourceListener[] getTransferDragSourceListeners(TreeViewer treeViewer) {
		return new TransferDragSourceListener[0];
	}

	/**
	 * Adopted since you can't easily removeDropSupport from
	 * StructuredViewers.
	 * 
	 * @param treeViewer
	 *            the TreeViewer associated with this configuration
	 * @return an array of TransferDropTargetListeners
	 */
	public TransferDropTargetListener[] getTransferDropTargetListeners(TreeViewer treeViewer) {
		return new TransferDropTargetListener[0];
	}

	/**
	 * Returns true if node selection changes affect selection in the
	 * TreeViewer.
	 * 
	 * @param treeViewer
	 *            the TreeViewer associated with this configuration
	 * @return true if outline is currently linked to selection in editor,
	 *         false otherwise
	 */
	public boolean isLinkedWithEditor(TreeViewer treeViewer) {
		return fIsLinkWithEditor;
	}

	/**
	 * Sets whether or not outline view should be linked with selection in
	 * editor.
	 * 
	 * @param isLinkWithEditor
	 *            The isLinkWithEditor to set.
	 */
	void setLinkWithEditor(boolean isLinkWithEditor) {
		fIsLinkWithEditor = isLinkWithEditor;
	}

	/**
	 * General hook for resource releasing and listener removal when
	 * configurations change or the viewer is disposed of. This implementation
	 * stops of any remaining PropertyChangeUpdateActionContributionItem from
	 * preference listening.
	 * 
	 * @param viewer
	 *            the TreeViewer associated with this configuration
	 */
	public void unconfigure(TreeViewer viewer) {
		if (fToolbarContributions != null) {
			for (int i = 0; i < fToolbarContributions.length; i++) {
				if (fToolbarContributions[i] instanceof PropertyChangeUpdateActionContributionItem) {
					((PropertyChangeUpdateActionContributionItem) fToolbarContributions[i]).disconnect();
				}
			}
			fToolbarContributions = null;
		}
		if (fMenuContributions != null) {
			for (int i = 0; i < fMenuContributions.length; i++) {
				if (fMenuContributions[i] instanceof PropertyChangeUpdateActionContributionItem) {
					((PropertyChangeUpdateActionContributionItem) fMenuContributions[i]).disconnect();
				}
			}
			fMenuContributions = null;
		}
	}
}

Back to the top