Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a520562b487436d306548486fc2d2a03038fbc8b (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
/*******************************************************************************
 * Copyright (c) 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
 *******************************************************************************/
package org.eclipse.wst.xml.ui.internal.tabletree;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IContributionManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.ITextEditorActionConstants;
import org.eclipse.wst.common.contentmodel.modelquery.ModelQuery;
import org.eclipse.wst.common.contentmodel.modelqueryimpl.CMDocumentLoader;
import org.eclipse.wst.common.contentmodel.modelqueryimpl.InferredGrammarBuildingCMDocumentLoader;
import org.eclipse.wst.sse.core.IStructuredModel;
import org.eclipse.wst.sse.ui.StructuredTextEditor;
import org.eclipse.wst.xml.core.document.XMLModel;
import org.eclipse.wst.xml.core.modelquery.ModelQueryUtil;
import org.eclipse.wst.xml.internal.ui.IDesignViewer;
import org.eclipse.wst.xml.internal.ui.IDesignViewerActionBarContributor;
import org.eclipse.wst.xml.internal.ui.XMLMultiPageEditorPart;
import org.eclipse.wst.xml.ui.nls2.ResourceHandler;
import org.eclipse.wst.xml.ui.util.SharedXMLEditorPluginImageHelper;
import org.w3c.dom.Document;


/**
 *  
 */
public class XMLTableTreeActionBarContributor implements IDesignViewerActionBarContributor {

	protected IEditorPart editorPart;
	protected final static String DESIGN_VIEWER_SEPARATOR_1_ID = "sed.tabletree.separator.1"; //$NON-NLS-1$
	protected final static String DESIGN_VIEWER_SEPARATOR_2_ID = "sed.tabletree.separator.2"; //$NON-NLS-1$
	protected final static String VALIDATE_XML_ID = "sed.tabletree.validateXML"; //$NON-NLS-1$
	protected final static String RELOAD_GRAMMAR_ID = "sed.tabletree.reloadGrammar"; //$NON-NLS-1$
	protected final static String TOGGLE_EDIT_MODE_ID = "sed.tabletree.toggleEditMode"; //$NON-NLS-1$
	protected final static String EXPAND_ALL_ID = "sed.tabletree.expandAll"; //$NON-NLS-1$
	protected final static String COLLAPSE_ALL_ID = "sed.tabletree.collapseAll"; //$NON-NLS-1$

	protected ToggleEditModeAction toggleAction;
	protected ReloadGrammarAction reloadGrammarAction;
	//	protected ValidateXMLAction validateXMLAction;
	protected ViewerExpandCollapseAction expandAction;
	protected ViewerExpandCollapseAction collapseAction;
	protected ViewerExpandCollapseAction xmlMenuExpandAction;
	protected ViewerExpandCollapseAction xmlMenuCollapseAction;

	public XMLTableTreeActionBarContributor() {
	}

	protected void removeContributions(IContributionManager manager) {
		try {
			doRemove(manager, DESIGN_VIEWER_SEPARATOR_1_ID);
			doRemove(manager, DESIGN_VIEWER_SEPARATOR_2_ID);
			doRemove(manager, VALIDATE_XML_ID);
			doRemove(manager, RELOAD_GRAMMAR_ID);
			doRemove(manager, TOGGLE_EDIT_MODE_ID);
			doRemove(manager, EXPAND_ALL_ID);
			doRemove(manager, COLLAPSE_ALL_ID);
		} catch (Exception e) {
		}
	}

	protected void doRemove(IContributionManager manager, String id) {
		try {
			if (manager.find(id) != null) {
				manager.remove(id);
			}
		} catch (Exception e) {
		}
	}

	public void init(IActionBars bars, IWorkbenchPage page) {
		init(bars);
	}

	public void init(IActionBars bars) {
		IToolBarManager tbm = bars.getToolBarManager();

		IMenuManager xmlMenu = bars.getMenuManager().findMenuUsingPath("com.ibm.etools.sed.editor.xmlmenu"); //$NON-NLS-1$

		if (xmlMenu == null) {
			xmlMenu = new MenuManager(ResourceHandler.getString("XMLTableTreeActionBarContributor.0"), "com.ibm.etools.sed.editor.xmlmenu"); //$NON-NLS-1$ //$NON-NLS-2$
			bars.getMenuManager().insertBefore(IWorkbenchActionConstants.M_WINDOW, xmlMenu);
		} else {
			removeContributions(xmlMenu);
		}

		tbm.add(new Separator("DESIGN_VIEWER_SEPARATOR_1_ID")); //$NON-NLS-1$

		// ValidateXMLAction
		//                        
		// ugly hack ... we don't contribute the generic XML validator if we
		// already
		// have a DAD validator contribution (in the case of the DAD editor).
		// This hack will be removed when defect 212448 is fixed.
		//
		//		if (tbm.find("validate.dad.validateDAD") == null) {
		//			validateXMLAction = new ValidateXMLAction();
		//			tbm.add(validateXMLAction);
		//			xmlMenu.add(validateXMLAction);
		//		}

		// ToggleEditModeAction
		//           
		toggleAction = new ToggleEditModeAction();
		toggleAction.setId(TOGGLE_EDIT_MODE_ID);
		xmlMenu.add(toggleAction);
		tbm.add(toggleAction);

		// ReloadGrammarAction
		//
		reloadGrammarAction = new ReloadGrammarAction();
		reloadGrammarAction.setId(RELOAD_GRAMMAR_ID);
		tbm.add(reloadGrammarAction);
		xmlMenu.add(reloadGrammarAction);

		xmlMenu.add(new Separator());

		// ExpandCollapseAction
		//
		xmlMenuExpandAction = new ViewerExpandCollapseAction(true);
		xmlMenuExpandAction.setId(EXPAND_ALL_ID);
		xmlMenuExpandAction.setText(ResourceHandler.getString("XMLTableTreeActionBarContributor.1")); //$NON-NLS-1$
		xmlMenu.add(xmlMenuExpandAction);

		xmlMenuCollapseAction = new ViewerExpandCollapseAction(false);
		xmlMenuCollapseAction.setId(COLLAPSE_ALL_ID);
		xmlMenuCollapseAction.setId(EXPAND_ALL_ID);
		xmlMenuCollapseAction.setText(ResourceHandler.getString("XMLTableTreeActionBarContributor.2")); //$NON-NLS-1$
		xmlMenu.add(xmlMenuCollapseAction);
	}

	protected void addActionWithId(IMenuManager menuManager, Action action, String id) {
		action.setId(id);
		menuManager.add(action);
	}

	public void initViewerSpecificContributions(IActionBars bars) {
		IToolBarManager tbm = bars.getToolBarManager();
		tbm.add(new Separator(DESIGN_VIEWER_SEPARATOR_2_ID));

		expandAction = new ViewerExpandCollapseAction(true);
		expandAction.setId(EXPAND_ALL_ID);
		tbm.add(expandAction);

		collapseAction = new ViewerExpandCollapseAction(false);
		collapseAction.setId(COLLAPSE_ALL_ID);
		tbm.add(collapseAction);
	}

	public void setViewerSpecificContributionsEnabled(boolean enabled) {
		if (expandAction != null) {
			expandAction.setEnabled(enabled);
			xmlMenuExpandAction.setEnabled(enabled);
		}

		if (collapseAction != null) {
			collapseAction.setEnabled(enabled);
			xmlMenuCollapseAction.setEnabled(enabled);
		}
	}

	public void setActiveEditor(IEditorPart targetEditor) {
		editorPart = targetEditor;

		IStructuredModel model = getModelForEditorPart(targetEditor);
		reloadGrammarAction.setModel(model);
		toggleAction.setModelQuery(ModelQueryUtil.getModelQuery(model));

		XMLTableTreeViewer tableTreeViewer = getTableTreeViewerForEditorPart(editorPart);
		if (tableTreeViewer != null) {
			expandAction.setViewer(tableTreeViewer);
			collapseAction.setViewer(tableTreeViewer);

			xmlMenuExpandAction.setViewer(tableTreeViewer);
			xmlMenuCollapseAction.setViewer(tableTreeViewer);
		}

		if (editorPart instanceof XMLMultiPageEditorPart) {
			IWorkbenchPartSite site = editorPart.getSite();
			if (site instanceof IEditorSite) {
				ITextEditor textEditor = ((XMLMultiPageEditorPart) editorPart).getTextEditor();
				IActionBars actionBars = ((IEditorSite) site).getActionBars();
				actionBars.setGlobalActionHandler(ITextEditorActionConstants.UNDO, getAction(textEditor, ITextEditorActionConstants.UNDO));
				actionBars.setGlobalActionHandler(ITextEditorActionConstants.REDO, getAction(textEditor, ITextEditorActionConstants.REDO));
			}
		}

		// TODO... uncomment this and investigate NPE
		//
		// add the cut/copy/paste for text fields
		// ActionHandlerPlugin.connectPart(editorPart);
	}

	protected final IAction getAction(ITextEditor editor, String actionId) {
		return (editor == null ? null : editor.getAction(actionId));
	}

	protected IStructuredModel getModelForEditorPart(IEditorPart targetEditor) {
		IStructuredModel result = null;
		if (editorPart instanceof XMLMultiPageEditorPart) {
			StructuredTextEditor textEditor = ((XMLMultiPageEditorPart) targetEditor).getTextEditor();
			result = (textEditor != null) ? textEditor.getModel() : null;
		}
		return result;
	}

	protected XMLTableTreeViewer getTableTreeViewerForEditorPart(IEditorPart targetEditor) {
		XMLTableTreeViewer result = null;
		Object object = targetEditor.getAdapter(IDesignViewer.class);
		if (object instanceof XMLTableTreeViewer) {
			result = (XMLTableTreeViewer) object;
		}
		return result;
	}

	/**
	 *  
	 */
	public class ToggleEditModeAction extends Action {
		protected ImageDescriptor onImage = SharedXMLEditorPluginImageHelper.getImageDescriptor(SharedXMLEditorPluginImageHelper.IMG_ETOOL_CONSTRAINON);
		protected ImageDescriptor offImage = SharedXMLEditorPluginImageHelper.getImageDescriptor(SharedXMLEditorPluginImageHelper.IMG_ETOOL_CONSTRAINOFF);
		protected ModelQuery modelQuery;

		public ToggleEditModeAction() {
			setAppearanceForEditMode(ModelQuery.EDIT_MODE_CONSTRAINED_STRICT);
		}

		public void run() {
			if (modelQuery != null) {
				int newState = getNextState(modelQuery.getEditMode());
				modelQuery.setEditMode(newState);
				setAppearanceForEditMode(newState);
			}
		}

		public void setModelQuery(ModelQuery newModelQuery) {
			modelQuery = newModelQuery;
			if (modelQuery != null) {
				setAppearanceForEditMode(modelQuery.getEditMode());
			}
		}

		public void setAppearanceForEditMode(int editMode) {
			if (editMode == ModelQuery.EDIT_MODE_CONSTRAINED_STRICT) {
				setToolTipText(ResourceHandler.getString("XMLTableTreeActionBarContributor.3")); //$NON-NLS-1$
				setText(ResourceHandler.getString("XMLTableTreeActionBarContributor.4")); //$NON-NLS-1$
				setImageDescriptor(onImage);
			} else {
				setToolTipText(ResourceHandler.getString("XMLTableTreeActionBarContributor.5")); //$NON-NLS-1$
				setText(ResourceHandler.getString("XMLTableTreeActionBarContributor.6")); //$NON-NLS-1$
				setImageDescriptor(offImage);
			}
		}

		public int getNextState(int editMode) {
			int result = -1;
			if (editMode == ModelQuery.EDIT_MODE_CONSTRAINED_STRICT) {
				result = ModelQuery.EDIT_MODE_UNCONSTRAINED;
			} else {
				result = ModelQuery.EDIT_MODE_CONSTRAINED_STRICT;
			}
			return result;
		}
	}

	/**
	 *  
	 */
	public class ReloadGrammarAction extends Action {
		protected IStructuredModel model;

		public ReloadGrammarAction() {
			setDisabledImageDescriptor(SharedXMLEditorPluginImageHelper.getImageDescriptor(SharedXMLEditorPluginImageHelper.IMG_DTOOL_RLDGRMR));
			setImageDescriptor(SharedXMLEditorPluginImageHelper.getImageDescriptor(SharedXMLEditorPluginImageHelper.IMG_ETOOL_RLDGRMR));
			setToolTipText(ResourceHandler.getString("XMLTableTreeActionBarContributor.7")); //$NON-NLS-1$
			setText(ResourceHandler.getString("XMLTableTreeActionBarContributor.8")); //$NON-NLS-1$
		}

		public void setModel(IStructuredModel newModel) {
			this.model = newModel;
		}

		public void run() {
			if (model != null) {
				ModelQuery modelQuery = ModelQueryUtil.getModelQuery(model);
				Document document = ((XMLModel) model).getDocument();
				if (modelQuery != null && modelQuery.getCMDocumentManager() != null) {
					modelQuery.getCMDocumentManager().getCMDocumentCache().clear();
					// TODO... need to figure out how to access the
					// DOMObserver via ModelQuery
					//  ...why?
					CMDocumentLoader loader = new InferredGrammarBuildingCMDocumentLoader(document, modelQuery);
					loader.loadCMDocuments();
				}
			}
		}
	}

	/**
	 * @see org.eclipse.ui.IEditorActionBarContributor#dispose()
	 */
	public void dispose() {
	}
}

Back to the top