Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6e1e1d718ee45456e51982541ca7ab710df9e743 (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
/*******************************************************************************
 * Copyright (c) 2001, 2004 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.preferences.ui;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.preference.IPreferenceNode;
import org.eclipse.jface.preference.PreferenceDialog;
import org.eclipse.jface.preference.PreferenceManager;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.help.WorkbenchHelp;
import org.eclipse.wst.sse.core.IModelManager;
import org.eclipse.wst.sse.core.IStructuredModel;
import org.eclipse.wst.sse.core.StructuredModelManager;
import org.eclipse.wst.sse.core.util.DocumentInputStream;
import org.eclipse.wst.sse.ui.StructuredTextEditor;
import org.eclipse.wst.sse.ui.extension.ExtendedConfigurationBuilder;
import org.eclipse.wst.sse.ui.extension.IExtendedEditorAction;
import org.eclipse.wst.sse.ui.extension.IExtendedSimpleEditor;
import org.eclipse.wst.sse.ui.extensions.ConfigurationPointCalculator;
import org.eclipse.wst.sse.ui.internal.SSEUIPlugin;
import org.eclipse.wst.sse.ui.internal.editor.EditorPluginImageHelper;
import org.eclipse.wst.sse.ui.internal.editor.EditorPluginImages;
import org.eclipse.wst.sse.ui.internal.editor.IHelpContextIds;

/**
 * This action displays the preferences relative to the current editor
 * 
 * @author amywu
 */
public class EditStructuredTextEditorPreferencesAction extends Action implements IExtendedEditorAction {
	private final String EXTENSION_ATTRIBUTE_PREFERENCE_IDS = "preferenceids"; //$NON-NLS-1$
	private final String EXTENSION_TYPE_ID = "preferencepages"; //$NON-NLS-1$
	private IExtendedSimpleEditor fEditor;

	public EditStructuredTextEditorPreferencesAction() {
		super(SSEUIPlugin.getResourceString("%EditPreferences.label"), EditorPluginImageHelper.getInstance().getImageDescriptor(EditorPluginImages.IMG_OBJ_PREFERENCES)); //$NON-NLS-1$
		WorkbenchHelp.setHelp(this, IHelpContextIds.CONTMNU_PREFERENCES_HELPID);
	}

	/**
	 * Create the preference manager to use to display in the preference
	 * dialog
	 * 
	 * @return a preference manager or null if there is no editor/preference
	 *         page to display
	 */
	protected PreferenceManager buildPreferenceManager() {
		if (getEditor() == null) {
			return null;
		}

		// gather all preference page ids to display
		String[] ids = buildPreferencePageIds();

		// if no preference pages to display, return null
		if (ids.length == 0) {
			return null;
		}

		// create a new preference manager for this action/dialog
		PreferenceManager manager = new PreferenceManager();

		// get the workbench preference manager
		PreferenceManager platformManager = PlatformUI.getWorkbench().getPreferenceManager();

		for (int i = 0; i < ids.length; ++i) {
			final IPreferenceNode targetNode = platformManager.find(ids[i]);
			if (targetNode != null) {
				manager.addToRoot(targetNode);
			}
		}

		return manager;
	}

	/**
	 * Create a list of preference page ids of the preference pages to display
	 * 
	 * @return String[]
	 */
	protected String[] buildPreferencePageIds() {
		List prefIds = new ArrayList();

		// figure out all preference page ids contributed
		String[] pointIds = null;
		Object editor = getEditor();
		Class rootclass = getEditor().getClass();
		if(editor instanceof StructuredTextEditor)
			rootclass = StructuredTextEditor.class;
		if (getEditor() instanceof IEditorPart) {
			pointIds = ConfigurationPointCalculator.getConfigurationPoints((IEditorPart) getEditor(), getContentType(), ConfigurationPointCalculator.SOURCE, rootclass);
		} else {
			pointIds = ConfigurationPointCalculator.getConfigurationPoints(getEditor().getEditorPart(), getContentType(), ConfigurationPointCalculator.SOURCE, getEditor().getEditorPart().getClass());
		}
		ExtendedConfigurationBuilder builder = ExtendedConfigurationBuilder.getInstance();

		// go through each configuration point and extract the preference page
		// ids
		// going through configuration points backwards so base fEditor
		// preferences show up first
		for (int i = pointIds.length - 1; i >= 0; --i) {
			IConfigurationElement config = builder.getConfigurationElement(EXTENSION_TYPE_ID, pointIds[i]);
			// get the list of preference page ids from the preferenceids
			// attribute
			if (config != null) {
				String preferenceIdsString = config.getAttribute(EXTENSION_ATTRIBUTE_PREFERENCE_IDS);
				// separate out the list of preference ids
				if (preferenceIdsString != null) {
					StringTokenizer tokenizer = new StringTokenizer(preferenceIdsString, ","); //$NON-NLS-1$
					while (tokenizer.hasMoreTokens()) {
						String prefId = tokenizer.nextToken().trim();
						prefIds.add(prefId);
					}
				}
			}
		}
		return (String[]) prefIds.toArray(new String[prefIds.size()]);
	}

	/**
	 * Return the content type of the current fEditor for this action
	 * 
	 * @return content type identifier of the current fEditor
	 */
	private String getContentType() {
		String contentType = null;

		IStructuredModel model = getModelManager().getExistingModelForRead(getEditor().getDocument());
		if (model != null) {
			contentType = model.getContentTypeIdentifier();
			model.releaseFromRead();
		}
		if (contentType == null) {
			IContentType type = null;
			try {
				type = Platform.getContentTypeManager().findContentTypeFor(new DocumentInputStream(getEditor().getDocument()), getEditor().getEditorPart().getEditorInput().getName());
			} catch (IOException e) {
				// do nothing, shouldn't even be possible
			}
			if (type != null) {
				contentType = type.getId();
			}
		}
		return contentType;
	}

	private IExtendedSimpleEditor getEditor() {
		return fEditor;
	}

	private IModelManager getModelManager() {
		return StructuredModelManager.getInstance().getModelManager();
	}

	public boolean isVisible() {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.action.IAction#run()
	 */
	public void run() {
		PreferenceManager manager = buildPreferenceManager();
		if (manager != null) {
			showPreferencePage(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), manager);
		}
	}

	public void setActiveExtendedEditor(IExtendedSimpleEditor targetEditor) {
		fEditor = targetEditor;
	}

	/**
	 * Open the given preference page in a preference dialog.
	 * 
	 * @param shell
	 *            The shell to open on
	 * @param manager
	 *            The preference manager to use/display in the preference
	 *            dialog
	 * @return Returns <code>true</code> if the user ended the page by
	 *         pressing OK.
	 */
	public boolean showPreferencePage(Shell shell, PreferenceManager manager) {
		final PreferenceDialog dialog = new PreferenceDialog(shell, manager);
		final boolean[] result = new boolean[]{false};
		BusyIndicator.showWhile(shell.getDisplay(), new Runnable() {
			public void run() {
				dialog.create();
				dialog.setMessage(SSEUIPlugin.getResourceString("%EditStructuredTextEditorPreferencesAction.0")); //$NON-NLS-1$
				result[0] = (dialog.open() == Window.OK);
			}
		});
		return result[0];
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.texteditor.IUpdate#update()
	 */
	public void update() {
		if (getEditor() == null) {
			setEnabled(false);
		} else {
			setEnabled(true);
		}
	}
}

Back to the top