Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b3e6cc3f84b9020ccb18d54e7563929d5d7cbfc7 (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
/*******************************************************************************
 * Copyright (c) 2011 Wind River Systems, 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.ui.terminals.tabs;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.jface.action.IMenuListener2;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.tcf.te.ui.terminals.actions.TabScrollLockAction;
import org.eclipse.tcf.te.ui.terminals.interfaces.ITerminalsView;
import org.eclipse.tm.internal.terminal.control.ITerminalViewControl;
import org.eclipse.tm.internal.terminal.control.actions.AbstractTerminalAction;
import org.eclipse.tm.internal.terminal.control.actions.TerminalActionClearAll;
import org.eclipse.tm.internal.terminal.control.actions.TerminalActionCopy;
import org.eclipse.tm.internal.terminal.control.actions.TerminalActionPaste;
import org.eclipse.tm.internal.terminal.control.actions.TerminalActionSelectAll;
import org.eclipse.ui.IWorkbenchActionConstants;

/**
 * Terminals tab folder menu handler.
 */
@SuppressWarnings("restriction")
public class TabFolderMenuHandler extends PlatformObject {
	// Reference to the parent terminal console view
	private final ITerminalsView parentView;
	// Reference to the tab folder context menu manager
	private MenuManager contextMenuManager;
	// Reference to the tab folder context menu
	private Menu contextMenu;
	// The list of actions available within the context menu
	private final List<AbstractTerminalAction> contextMenuActions = new ArrayList<AbstractTerminalAction>();

	/**
	 * Default menu listener implementation.
	 */
	protected class MenuListener implements IMenuListener2 {

		/* (non-Javadoc)
		 * @see org.eclipse.jface.action.IMenuListener2#menuAboutToHide(org.eclipse.jface.action.IMenuManager)
		 */
		@Override
		public void menuAboutToHide(IMenuManager manager) {
			// CQ:WIND00192293 and CQ:WIND194204 - don't update actions on menuAboutToHide
			// See also http://bugs.eclipse.org/296212
			//			updateMenuItems(false);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.action.IMenuListener#menuAboutToShow(org.eclipse.jface.action.IMenuManager)
		 */
		@Override
		public void menuAboutToShow(IMenuManager manager) {
			updateMenuItems(true);
		}

	}

	/**
	 * Constructor.
	 *
	 * @param parentView The parent terminal console view. Must not be <code>null</code>.
	 */
	public TabFolderMenuHandler(ITerminalsView parentView) {
		super();
		Assert.isNotNull(parentView);
		this.parentView = parentView;
	}

	/**
	 * Returns the parent terminal console view.
	 *
	 * @return The parent terminal console view instance.
	 */
	protected final ITerminalsView getParentView() {
		return parentView;
	}

	/**
	 * Returns the tab folder associated with the parent view.
	 *
	 * @return The tab folder or <code>null</code>.
	 */
	protected final CTabFolder getTabFolder() {
		return (CTabFolder)getParentView().getAdapter(CTabFolder.class);
	}

	/**
	 * Dispose the tab folder menu handler instance.
	 */
	public void dispose() {
		// Dispose the context menu
		if (contextMenu != null) { contextMenu.dispose(); contextMenu = null; }
		// Dispose the context menu manager
		if (contextMenuManager != null) { contextMenuManager.dispose(); contextMenuManager = null; }
		// Clear all actions
		contextMenuActions.clear();
	}

	/**
	 * Setup the context menu for the tab folder. The method will return
	 * immediately if the menu handler had been initialized before.
	 *
	 * @param tabFolder The tab folder control. Must not be <code>null</code>.
	 */
	public void initialize() {
		// Return immediately if the menu manager and menu got initialized already
		if (contextMenuManager != null && contextMenu != null) {
			return;
		}

		// Get the tab folder
		CTabFolder tabFolder = getTabFolder();
		if (tabFolder == null) {
			return;
		}

		// Create the menu manager if not done before
		contextMenuManager = new MenuManager("#PopupMenu"); //$NON-NLS-1$
		// Create and associated the menu listener
		contextMenuManager.addMenuListener(new MenuListener());
		// Create the context menu
		contextMenu = contextMenuManager.createContextMenu(tabFolder);

		// Create the context menu action instances
		doCreateContextMenuActions();

		// Fill the context menu
		doFillContextMenu(contextMenuManager);

		// Register to the view site to open the menu for contributions
		getParentView().getSite().registerContextMenu(contextMenuManager, getParentView().getSite().getSelectionProvider());
	}

	/**
	 * Adds the given action to the context menu actions list.
	 *
	 * @param action The action instance. Must be not <code>null</code>.
	 */
	protected final void add(AbstractTerminalAction action) {
		Assert.isNotNull(action);
		contextMenuActions.add(action);
	}

	/**
	 * Create the context menu actions.
	 */
	protected void doCreateContextMenuActions() {
		// Create and add the copy action
		add(new TerminalActionCopy() {
			/* (non-Javadoc)
			 * @see org.eclipse.tcf.internal.terminal.control.actions.AbstractTerminalAction#getTarget()
			 */
			@Override
			protected ITerminalViewControl getTarget() {
				return getActiveTerminalViewControl();
			}
		});

		// Create and add the paste action
		add(new TerminalActionPaste() {
			/* (non-Javadoc)
			 * @see org.eclipse.tcf.internal.terminal.control.actions.AbstractTerminalAction#getTarget()
			 */
			@Override
			protected ITerminalViewControl getTarget() {
				return getActiveTerminalViewControl();
			}
		});

		// Create and add the clear all action
		add(new TerminalActionClearAll() {
			/* (non-Javadoc)
			 * @see org.eclipse.tcf.internal.terminal.control.actions.AbstractTerminalAction#getTarget()
			 */
			@Override
			protected ITerminalViewControl getTarget() {
				return getActiveTerminalViewControl();
			}
		});

		// Create and add the select all action
		add(new TerminalActionSelectAll() {
			/* (non-Javadoc)
			 * @see org.eclipse.tcf.internal.terminal.control.actions.AbstractTerminalAction#getTarget()
			 */
			@Override
			protected ITerminalViewControl getTarget() {
				return getActiveTerminalViewControl();
			}
		});

		// Create and add the scroll lock action
		add (new TabScrollLockAction() {
			/* (non-Javadoc)
			 * @see org.eclipse.tcf.internal.terminal.control.actions.AbstractTerminalAction#getTarget()
			 */
			@Override
			protected ITerminalViewControl getTarget() {
				return getActiveTerminalViewControl();
			}
		});
	}

	/**
	 * Returns the currently active terminal control.
	 *
	 * @return The currently active terminal control or <code>null</code>.
	 */
	protected ITerminalViewControl getActiveTerminalViewControl() {
		ITerminalViewControl terminal = null;

		// Get the active tab item from the tab folder manager
		TabFolderManager manager = (TabFolderManager)getParentView().getAdapter(TabFolderManager.class);
		if (manager != null) {
			// If we have the active tab item, we can get the active terminal control
			CTabItem activeTabItem = manager.getActiveTabItem();
			if (activeTabItem != null) {
				terminal = (ITerminalViewControl)activeTabItem.getData();
			}
		}

		return terminal;
	}

	/**
	 * Fill in the context menu content within the given manager.
	 *
	 * @param manager The menu manager. Must not be <code>null</code>.
	 */
	protected void doFillContextMenu(MenuManager manager) {
		Assert.isNotNull(manager);

		// Loop all actions and add them to the menu manager
		for (AbstractTerminalAction action : contextMenuActions) {
			manager.add(action);
			// Add a separator after the paste action
			if (action instanceof TerminalActionPaste) {
				manager.add(new Separator());
			}
			// Add a separator after the select all action
			if (action instanceof TerminalActionSelectAll) {
				manager.add(new Separator());
			}
		}

		// Menu contributions will end up here
		manager.add(new Separator());
		manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
	}

	/**
	 * Update the context menu items on showing or hiding the context menu.
	 *
	 * @param aboutToShow <code>True</code> if the menu is about to show, <code>false</code> otherwise.
	 */
	protected void updateMenuItems(boolean aboutToShow) {
		// Loop all actions and update the status
		for (AbstractTerminalAction action : contextMenuActions) {
			action.updateAction(aboutToShow);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.WorkbenchPart#getAdapter(java.lang.Class)
	 */
	@Override
	public Object getAdapter(Class adapter) {
		if (MenuManager.class.isAssignableFrom(adapter)) {
			return contextMenuManager;
		} else if (Menu.class.isAssignableFrom(adapter)) {
			return contextMenu;
		}

		// Try the parent view
		Object adapted = getParentView().getAdapter(adapter);
		if (adapted != null) {
			return adapted;
		}

		return super.getAdapter(adapter);
	}
}

Back to the top