Skip to main content
summaryrefslogtreecommitdiffstats
blob: 31ad867fcc40c0beb98df40432e08adff6047e35 (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
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. 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:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.editors.actions;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IContributionManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jst.pagedesigner.IHTMLConstants;
import org.eclipse.jst.pagedesigner.PDPlugin;
import org.eclipse.jst.pagedesigner.actions.range.DesignerToolBarAction;
import org.eclipse.jst.pagedesigner.common.logging.Logger;
import org.eclipse.jst.pagedesigner.editors.HTMLEditor;
import org.eclipse.jst.pagedesigner.editors.SimpleGraphicalEditor;
import org.eclipse.jst.pagedesigner.viewer.IHTMLGraphicalViewer;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.part.EditorActionBarContributor;
import org.eclipse.ui.texteditor.ITextEditorActionConstants;
import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds;

/**
 * sub action contributor for the designer page.
 * 
 * @author mengbo
 */
public class DesignPageActionContributor extends EditorActionBarContributor {
	private static final Logger _log = PDPlugin
			.getLogger(DesignPageActionContributor.class);

	public static final String PARAGRAPH_ACTION_ID = "paragraph";

	protected IEditorPart _editorPart;

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

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

	public void init(IActionBars bars) {
		IToolBarManager toolbar = bars.getToolBarManager();
		initToolbar(toolbar);
	}

	/**
	 * @param toolbar
	 */
	private void initToolbar(IToolBarManager toolbar) {
		DesignActionBarFactory factory = DesignActionBarFactory.getInstance();

		Action action = factory.getStyleAction(IHTMLConstants.TAG_U);
		toolbar.add(action);

		action = factory.getStyleAction(IHTMLConstants.TAG_B);
		toolbar.add(action);

		action = factory.getStyleAction(IHTMLConstants.TAG_I);
		toolbar.add(action);

		action = factory.getStyleAction(IHTMLConstants.TAG_SMALL);
		toolbar.add(action);

		action = factory.getStyleAction(IHTMLConstants.TAG_BIG);
		toolbar.add(action);
		// action = factory.getStyleAction(PARAGRAPH_ACTION_ID);
		// toolbar.add(action);
	}

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

	public void setViewerSpecificContributionsEnabled(boolean enabled) {
		HTMLEditor htmlEditor = null;
		if (_editorPart instanceof HTMLEditor) {
			htmlEditor = (HTMLEditor) _editorPart;
		} else if (_editorPart instanceof SimpleGraphicalEditor) {
			htmlEditor = ((SimpleGraphicalEditor) _editorPart).getHTMLEditor();
		}

		if (htmlEditor == null)
			return;

		SimpleGraphicalEditor graphicalEditor = (SimpleGraphicalEditor) htmlEditor
				.getDesignViewer();
		IWorkbenchPartSite site = htmlEditor.getSite();
		if (site instanceof IEditorSite) {
			IActionBars actionBars = ((IEditorSite) site).getActionBars();

			if (enabled) {
				// // we always let the text editor to handle UNDO and REDO
				// actionBars.setGlobalActionHandler(ITextEditorActionConstants.UNDO,
				// textEditor
				// .getAction(ITextEditorActionConstants.UNDO));
				// actionBars.setGlobalActionHandler(ITextEditorActionConstants.REDO,
				// textEditor
				// .getAction(ITextEditorActionConstants.REDO));
				// lium: the above behavior changed, since we now use
				// DesignerUndoRedoAction.
				// see comments in DesignerUndoRedoAction
				actionBars.setGlobalActionHandler(
						ITextEditorActionConstants.UNDO, graphicalEditor
								.getAction(IWorkbenchActionDefinitionIds.UNDO));
				actionBars.setGlobalActionHandler(
						ITextEditorActionConstants.REDO, graphicalEditor
								.getAction(IWorkbenchActionDefinitionIds.REDO));

				// cut/copy/paste is delegated to design actions
				actionBars
						.setGlobalActionHandler(
								ITextEditorActionConstants.DELETE,
								graphicalEditor
										.getAction(IWorkbenchActionDefinitionIds.DELETE));
				actionBars.setGlobalActionHandler(
						ITextEditorActionConstants.CUT, graphicalEditor
								.getAction(IWorkbenchActionDefinitionIds.CUT));
				actionBars.setGlobalActionHandler(
						ITextEditorActionConstants.COPY, graphicalEditor
								.getAction(IWorkbenchActionDefinitionIds.COPY));
				actionBars
						.setGlobalActionHandler(
								ITextEditorActionConstants.PASTE,
								graphicalEditor
										.getAction(IWorkbenchActionDefinitionIds.PASTE));
			} else {
				actionBars.setGlobalActionHandler(
						ITextEditorActionConstants.UNDO, null);
				actionBars.setGlobalActionHandler(
						ITextEditorActionConstants.REDO, null);

				actionBars.setGlobalActionHandler(
						ITextEditorActionConstants.DELETE, null);
				actionBars.setGlobalActionHandler(
						ITextEditorActionConstants.CUT, null);
				actionBars.setGlobalActionHandler(
						ITextEditorActionConstants.COPY, null);
				actionBars.setGlobalActionHandler(
						ITextEditorActionConstants.PASTE, null);
			}
		}
	}

	/**
	 * The active editor passed in could be the following: HTMLEditor,
	 * SimpleGraphicalEditor, null.
	 */
	public void setActiveEditor(IEditorPart targetEditor) {
		_editorPart = targetEditor;

		// temp code.
		if (targetEditor instanceof SimpleGraphicalEditor) {
			IHTMLGraphicalViewer viewer = ((SimpleGraphicalEditor) targetEditor)
					.getGraphicViewer();
			setViewerOnActions(viewer);
		} else if (targetEditor instanceof HTMLEditor) {
			IHTMLGraphicalViewer viewer = ((HTMLEditor) targetEditor)
					.getDesignViewer().getGraphicViewer();
			setViewerOnActions(viewer);
		} else {
			setViewerOnActions(null);
		}

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

	private void setViewerOnActions(IHTMLGraphicalViewer viewer) {
		IContributionItem[] items = getActionBars().getToolBarManager()
				.getItems();
		if (items != null) {
			for (int i = 0; i < items.length; i++) {
				if (items[i] instanceof ActionContributionItem) {
					IAction action = ((ActionContributionItem) items[i])
							.getAction();
					if (action instanceof DesignerToolBarAction) {
						((DesignerToolBarAction) action).setViewer(viewer);
					}
				}
			}
		}

	}

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

Back to the top