diff options
Diffstat (limited to 'plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/views/TerminalViewTab.java')
-rw-r--r-- | plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/views/TerminalViewTab.java | 457 |
1 files changed, 457 insertions, 0 deletions
diff --git a/plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/views/TerminalViewTab.java b/plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/views/TerminalViewTab.java new file mode 100644 index 0000000..bbb2d02 --- /dev/null +++ b/plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/views/TerminalViewTab.java | |||
@@ -0,0 +1,457 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2002, 2009 IBM Corporation and others. | ||
3 | * All rights reserved. This program and the accompanying materials | ||
4 | * are made available under the terms of the Eclipse Public License v1.0 | ||
5 | * which accompanies this distribution, and is available at | ||
6 | * http://www.eclipse.org/legal/epl-v10.html | ||
7 | * | ||
8 | * Initial Contributors: | ||
9 | * The following IBM employees contributed to the Remote System Explorer | ||
10 | * component that contains this file: David McKnight, Kushal Munir, | ||
11 | * Michael Berger, David Dykstal, Phil Coulthard, Don Yantzi, Eric Simpson, | ||
12 | * Emily Bruner, Mazen Faraj, Adrian Storisteanu, Li Ding, and Kent Hawley. | ||
13 | * | ||
14 | * Contributors: | ||
15 | * David McKnight (IBM) - [165680] "Show in Remote Shell View" does not work | ||
16 | * Yu-Fen Kuo (MontaVista) - Adapted from CommandsViewWorkbook | ||
17 | * Anna Dushistova (MontaVista) - Adapted from CommandsViewWorkbook | ||
18 | * Yu-Fen Kuo (MontaVista) - [227572] RSE Terminal doesn't reset the "connected" state when the shell exits | ||
19 | * Martin Oberhuber (Wind River) - [227571] RSE Terminal should honor Encoding set on the IHost | ||
20 | * Michael Scharf (Wind River) - [236203] [rseterminal] Potentially UI blocking code in TerminalViewTab.createTabItem | ||
21 | * Anna Dushistova (MontaVista) - [244437] [rseterminal] Possible race condition when multiple Terminals are launched after each other | ||
22 | * Martin Oberhuber (Wind River) - [247700] Terminal uses ugly fonts in JEE package | ||
23 | * Anna Dushistova (MontaVista) - [267609] [rseterminal] The first "Launch Terminal" command creates no terminal tab | ||
24 | ********************************************************************************/ | ||
25 | package org.yocto.sdk.remotetools.views; | ||
26 | |||
27 | import java.io.UnsupportedEncodingException; | ||
28 | |||
29 | import org.eclipse.core.runtime.IAdaptable; | ||
30 | import org.eclipse.jface.action.IMenuListener; | ||
31 | import org.eclipse.jface.action.IMenuManager; | ||
32 | import org.eclipse.jface.action.MenuManager; | ||
33 | import org.eclipse.jface.action.Separator; | ||
34 | import org.eclipse.jface.resource.FontRegistry; | ||
35 | import org.eclipse.jface.util.IPropertyChangeListener; | ||
36 | import org.eclipse.jface.util.PropertyChangeEvent; | ||
37 | import org.eclipse.rse.core.model.IHost; | ||
38 | import org.eclipse.swt.SWT; | ||
39 | import org.eclipse.swt.custom.CTabFolder; | ||
40 | import org.eclipse.swt.custom.CTabItem; | ||
41 | import org.eclipse.swt.events.DisposeEvent; | ||
42 | import org.eclipse.swt.events.DisposeListener; | ||
43 | import org.eclipse.swt.events.MenuEvent; | ||
44 | import org.eclipse.swt.events.MenuListener; | ||
45 | import org.eclipse.swt.graphics.Font; | ||
46 | import org.eclipse.swt.layout.FillLayout; | ||
47 | import org.eclipse.swt.layout.GridData; | ||
48 | import org.eclipse.swt.widgets.Composite; | ||
49 | import org.eclipse.swt.widgets.Control; | ||
50 | import org.eclipse.swt.widgets.Display; | ||
51 | import org.eclipse.swt.widgets.Menu; | ||
52 | import org.eclipse.tm.internal.terminal.control.ITerminalListener; | ||
53 | import org.eclipse.tm.internal.terminal.control.ITerminalViewControl; | ||
54 | import org.eclipse.tm.internal.terminal.control.TerminalViewControlFactory; | ||
55 | import org.eclipse.tm.internal.terminal.control.actions.TerminalActionClearAll; | ||
56 | import org.eclipse.tm.internal.terminal.control.actions.TerminalActionCopy; | ||
57 | import org.eclipse.tm.internal.terminal.control.actions.TerminalActionCut; | ||
58 | import org.eclipse.tm.internal.terminal.control.actions.TerminalActionPaste; | ||
59 | import org.eclipse.tm.internal.terminal.control.actions.TerminalActionSelectAll; | ||
60 | import org.eclipse.tm.internal.terminal.provisional.api.ITerminalConnector; | ||
61 | import org.eclipse.tm.internal.terminal.provisional.api.TerminalState; | ||
62 | import org.eclipse.ui.PlatformUI; | ||
63 | import org.eclipse.ui.themes.IThemeManager; | ||
64 | |||
65 | /** | ||
66 | * This is the desktop view wrapper of the System View viewer. | ||
67 | */ | ||
68 | public class TerminalViewTab extends Composite { | ||
69 | |||
70 | public static String DATA_KEY_CONTROL = "$_control_$"; //$NON-NLS-1$ | ||
71 | |||
72 | private final CTabFolder tabFolder; | ||
73 | |||
74 | private IPropertyChangeListener propertyChangeListener; | ||
75 | |||
76 | private Menu menu; | ||
77 | |||
78 | private boolean fMenuAboutToShow; | ||
79 | |||
80 | private TerminalActionCopy fActionEditCopy; | ||
81 | |||
82 | private TerminalActionCut fActionEditCut; | ||
83 | |||
84 | private TerminalActionPaste fActionEditPaste; | ||
85 | |||
86 | private TerminalActionClearAll fActionEditClearAll; | ||
87 | |||
88 | private TerminalActionSelectAll fActionEditSelectAll; | ||
89 | |||
90 | protected class TerminalContextMenuHandler implements MenuListener, | ||
91 | IMenuListener { | ||
92 | public void menuHidden(MenuEvent event) { | ||
93 | fMenuAboutToShow = false; | ||
94 | fActionEditCopy.updateAction(fMenuAboutToShow); | ||
95 | } | ||
96 | |||
97 | public void menuShown(MenuEvent e) { | ||
98 | |||
99 | } | ||
100 | |||
101 | public void menuAboutToShow(IMenuManager menuMgr) { | ||
102 | fMenuAboutToShow = true; | ||
103 | fActionEditCopy.updateAction(fMenuAboutToShow); | ||
104 | fActionEditCut.updateAction(fMenuAboutToShow); | ||
105 | fActionEditSelectAll.updateAction(fMenuAboutToShow); | ||
106 | fActionEditPaste.updateAction(fMenuAboutToShow); | ||
107 | fActionEditClearAll.updateAction(fMenuAboutToShow); | ||
108 | } | ||
109 | } | ||
110 | |||
111 | public TerminalViewTab(final Composite parent, TerminalViewer viewer) { | ||
112 | super(parent, SWT.NONE); | ||
113 | tabFolder = new CTabFolder(this, SWT.NONE); | ||
114 | tabFolder.setLayout(new FillLayout()); | ||
115 | tabFolder.setLayoutData(new GridData(GridData.FILL_BOTH)); | ||
116 | setLayout(new FillLayout()); | ||
117 | tabFolder.setBackground(parent.getBackground()); | ||
118 | tabFolder.setSimple(false); | ||
119 | tabFolder.setUnselectedImageVisible(false); | ||
120 | tabFolder.setUnselectedCloseVisible(false); | ||
121 | |||
122 | tabFolder.setMinimizeVisible(false); | ||
123 | tabFolder.setMaximizeVisible(false); | ||
124 | //TODO setupActions(); | ||
125 | } | ||
126 | |||
127 | public void dispose() { | ||
128 | if (propertyChangeListener != null) { | ||
129 | IThemeManager mgr = PlatformUI.getWorkbench().getThemeManager(); | ||
130 | mgr.removePropertyChangeListener(propertyChangeListener); | ||
131 | propertyChangeListener = null; | ||
132 | } | ||
133 | if (!tabFolder.isDisposed()) { | ||
134 | tabFolder.dispose(); | ||
135 | } | ||
136 | super.dispose(); | ||
137 | } | ||
138 | |||
139 | public CTabFolder getFolder() { | ||
140 | return tabFolder; | ||
141 | } | ||
142 | |||
143 | public void remove(Object root) { | ||
144 | |||
145 | } | ||
146 | |||
147 | public int getItemCount(){ | ||
148 | return tabFolder.getItemCount(); | ||
149 | } | ||
150 | |||
151 | public CTabItem getSelectedTab() { | ||
152 | if (tabFolder.getItemCount() > 0) { | ||
153 | int index = tabFolder.getSelectionIndex(); | ||
154 | CTabItem item = tabFolder.getItem(index); | ||
155 | return item; | ||
156 | } | ||
157 | |||
158 | return null; | ||
159 | } | ||
160 | |||
161 | public void showCurrentPage() { | ||
162 | tabFolder.setFocus(); | ||
163 | } | ||
164 | |||
165 | public void showPageFor(Object root) { | ||
166 | for (int i = 0; i < tabFolder.getItemCount(); i++) { | ||
167 | CTabItem item = tabFolder.getItem(i); | ||
168 | if (item.getData() == root) { | ||
169 | tabFolder.setSelection(item); | ||
170 | } | ||
171 | |||
172 | } | ||
173 | } | ||
174 | |||
175 | public void showPageFor(String tabName) { | ||
176 | for (int i = 0; i < tabFolder.getItemCount(); i++) { | ||
177 | CTabItem item = tabFolder.getItem(i); | ||
178 | if (item.getText().equals(tabName)) { | ||
179 | tabFolder.setSelection(item); | ||
180 | return; | ||
181 | } | ||
182 | |||
183 | } | ||
184 | } | ||
185 | |||
186 | public void disposePageFor(String tabName) { | ||
187 | for (int i = 0; i < tabFolder.getItemCount(); i++) { | ||
188 | CTabItem item = tabFolder.getItem(i); | ||
189 | if (item.getText().equals(tabName)) { | ||
190 | item.dispose(); | ||
191 | return; | ||
192 | } | ||
193 | |||
194 | } | ||
195 | } | ||
196 | |||
197 | public void propertyChange(PropertyChangeEvent e) { | ||
198 | // for now always update | ||
199 | if (tabFolder!=null) { | ||
200 | CTabItem[] items = tabFolder.getItems(); | ||
201 | for (int i=0; i<items.length; i++) { | ||
202 | Object control = items[i].getData(DATA_KEY_CONTROL); | ||
203 | if (control instanceof ITerminalViewControl) { | ||
204 | updateTheme((ITerminalViewControl) control); | ||
205 | } | ||
206 | } | ||
207 | } | ||
208 | } | ||
209 | |||
210 | public void updateTheme(final ITerminalViewControl control) { | ||
211 | if (control != null) { | ||
212 | IThemeManager mgr = PlatformUI.getWorkbench().getThemeManager(); | ||
213 | Font font; | ||
214 | FontRegistry fr = mgr.getCurrentTheme().getFontRegistry(); | ||
215 | if (fr.hasValueFor("terminal.views.view.font.definition")) { //$NON-NLS-1$ | ||
216 | //Terminal View font if available | ||
217 | font = fr.get("terminal.views.view.font.definition"); //$NON-NLS-1$ | ||
218 | } else if (fr.hasValueFor("REMOTE_COMMANDS_VIEW_FONT")) { //$NON-NLS-1$ | ||
219 | //fallback: "Remote Shell Font" | ||
220 | font = fr.get("REMOTE_COMMANDS_VIEW_FONT"); //$NON-NLS-1$ | ||
221 | } else { | ||
222 | //fallback: "Basic Text Font" | ||
223 | font = fr.get("org.eclipse.jface.textfont"); //$NON-NLS-1$ | ||
224 | } | ||
225 | control.setFont(font); | ||
226 | if (propertyChangeListener == null) { | ||
227 | final TerminalViewTab myself = this; | ||
228 | propertyChangeListener = new IPropertyChangeListener() { | ||
229 | public void propertyChange(PropertyChangeEvent event) { | ||
230 | myself.propertyChange(event); | ||
231 | } | ||
232 | }; | ||
233 | mgr.addPropertyChangeListener(propertyChangeListener); | ||
234 | } | ||
235 | } | ||
236 | } | ||
237 | /* TODO | ||
238 | public CTabItem createTabItem(IAdaptable root, | ||
239 | final String initialWorkingDirCmd,final String initialCmd) { | ||
240 | final CTabItem item = new CTabItem(tabFolder, SWT.CLOSE); | ||
241 | setTabTitle(root, item); | ||
242 | |||
243 | item.setData(root); | ||
244 | Composite c = new Composite(tabFolder, SWT.NONE); | ||
245 | c.setLayout(new FillLayout()); | ||
246 | |||
247 | tabFolder.getParent().layout(true); | ||
248 | if (root instanceof IHost) { | ||
249 | final IHost host = (IHost) root; | ||
250 | |||
251 | ITerminalConnector connector = new TCFTerminalConnector(host); | ||
252 | ITerminalViewControl terminalControl = TerminalViewControlFactory | ||
253 | .makeControl(new ITerminalListener() | ||
254 | { | ||
255 | |||
256 | public void setState(final TerminalState state) { | ||
257 | if (state == TerminalState.CLOSED | ||
258 | || state == TerminalState.CONNECTED) { | ||
259 | Display.getDefault().asyncExec(new Runnable() { | ||
260 | public void run() { | ||
261 | if (!item.isDisposed()) { | ||
262 | final ITerminalServiceSubSystem terminalServiceSubSystem = TerminalServiceHelper | ||
263 | .getTerminalSubSystem(host); | ||
264 | |||
265 | if (state == TerminalState.CONNECTED) | ||
266 | TerminalServiceHelper | ||
267 | .updateTerminalShellForTerminalElement(item); | ||
268 | |||
269 | setTabImage(host, item); | ||
270 | ISystemRegistry registry = RSECorePlugin | ||
271 | .getTheSystemRegistry(); | ||
272 | registry | ||
273 | .fireEvent(new SystemResourceChangeEvent( | ||
274 | terminalServiceSubSystem, | ||
275 | ISystemResourceChangeEvents.EVENT_REFRESH, | ||
276 | terminalServiceSubSystem)); | ||
277 | } | ||
278 | if (state == TerminalState.CONNECTED) { | ||
279 | |||
280 | if (initialWorkingDirCmd != null) { | ||
281 | Object data = item | ||
282 | .getData(DATA_KEY_CONTROL); | ||
283 | if (data instanceof ITerminalViewControl) | ||
284 | ((ITerminalViewControl) data) | ||
285 | .pasteString(initialWorkingDirCmd); | ||
286 | } | ||
287 | } | ||
288 | } | ||
289 | }); | ||
290 | } | ||
291 | |||
292 | } | ||
293 | |||
294 | public void setTerminalTitle(String title) { | ||
295 | |||
296 | } | ||
297 | }, | ||
298 | c, new ITerminalConnector[] { connector }); | ||
299 | // Specify Encoding for Terminal | ||
300 | try { | ||
301 | terminalControl.setEncoding(host.getDefaultEncoding(true)); | ||
302 | } catch (UnsupportedEncodingException e) { | ||
303 | // ignore and allow fallback to default encoding | ||
304 | } | ||
305 | terminalControl.setConnector(connector); | ||
306 | item.setData(DATA_KEY_CONTROL, terminalControl); | ||
307 | updateTheme(terminalControl); | ||
308 | terminalControl.connectTerminal(); | ||
309 | } | ||
310 | item.setControl(c); | ||
311 | tabFolder.setSelection(item); | ||
312 | item.addDisposeListener(new DisposeListener() { | ||
313 | |||
314 | public void widgetDisposed(DisposeEvent e) { | ||
315 | Object source = e.getSource(); | ||
316 | if (source instanceof CTabItem) { | ||
317 | CTabItem currentItem = (CTabItem) source; | ||
318 | Object data = currentItem.getData(DATA_KEY_CONTROL); | ||
319 | if (data instanceof ITerminalViewControl) { | ||
320 | ((ITerminalViewControl) data).disposeTerminal(); | ||
321 | } | ||
322 | data = currentItem.getData(); | ||
323 | if (data instanceof IHost) { | ||
324 | TerminalServiceHelper.removeTerminalElementFromHost( | ||
325 | currentItem, (IHost) data); | ||
326 | } | ||
327 | } | ||
328 | |||
329 | } | ||
330 | |||
331 | }); | ||
332 | |||
333 | setupContextMenus(); | ||
334 | return item; | ||
335 | |||
336 | } | ||
337 | |||
338 | protected void setupActions() { | ||
339 | fActionEditCopy = new TerminalActionCopy() { | ||
340 | protected ITerminalViewControl getTarget() { | ||
341 | return getCurrentTerminalViewControl(); | ||
342 | } | ||
343 | }; | ||
344 | fActionEditCut = new TerminalActionCut() { | ||
345 | protected ITerminalViewControl getTarget() { | ||
346 | return getCurrentTerminalViewControl(); | ||
347 | } | ||
348 | }; | ||
349 | fActionEditPaste = new TerminalActionPaste() { | ||
350 | protected ITerminalViewControl getTarget() { | ||
351 | return getCurrentTerminalViewControl(); | ||
352 | } | ||
353 | }; | ||
354 | fActionEditClearAll = new TerminalActionClearAll() { | ||
355 | protected ITerminalViewControl getTarget() { | ||
356 | return getCurrentTerminalViewControl(); | ||
357 | } | ||
358 | }; | ||
359 | fActionEditSelectAll = new TerminalActionSelectAll() { | ||
360 | protected ITerminalViewControl getTarget() { | ||
361 | return getCurrentTerminalViewControl(); | ||
362 | } | ||
363 | }; | ||
364 | } | ||
365 | |||
366 | protected void setupContextMenus() { | ||
367 | ITerminalViewControl terminalViewControl = getCurrentTerminalViewControl(); | ||
368 | if (terminalViewControl == null) | ||
369 | return; | ||
370 | |||
371 | if (menu == null) { | ||
372 | MenuManager menuMgr = new MenuManager("#PopupMenu"); //$NON-NLS-1$ | ||
373 | menu = menuMgr.createContextMenu(tabFolder); | ||
374 | loadContextMenus(menuMgr); | ||
375 | TerminalContextMenuHandler contextMenuHandler = new TerminalContextMenuHandler(); | ||
376 | menuMgr.addMenuListener(contextMenuHandler); | ||
377 | menu.addMenuListener(contextMenuHandler); | ||
378 | } | ||
379 | Control ctlText = terminalViewControl.getControl(); | ||
380 | ctlText.setMenu(menu); | ||
381 | } | ||
382 | |||
383 | protected void loadContextMenus(IMenuManager menuMgr) { | ||
384 | menuMgr.add(fActionEditCopy); | ||
385 | menuMgr.add(fActionEditPaste); | ||
386 | menuMgr.add(new Separator()); | ||
387 | menuMgr.add(fActionEditClearAll); | ||
388 | menuMgr.add(fActionEditSelectAll); | ||
389 | menuMgr.add(new Separator()); | ||
390 | |||
391 | // Other plug-ins can contribute there actions here | ||
392 | menuMgr.add(new Separator("Additions")); //$NON-NLS-1$ | ||
393 | } | ||
394 | |||
395 | private void setTabTitle(IAdaptable root, CTabItem titem) { | ||
396 | ISystemViewElementAdapter va = (ISystemViewElementAdapter) root | ||
397 | .getAdapter(ISystemViewElementAdapter.class); | ||
398 | if (va != null) { | ||
399 | updateWithUniqueTitle(va.getName(root), titem); | ||
400 | setTabImage(root, titem); | ||
401 | } | ||
402 | } | ||
403 | |||
404 | private void setTabImage(IAdaptable root, CTabItem titem) { | ||
405 | ISystemViewElementAdapter va = (ISystemViewElementAdapter) root | ||
406 | .getAdapter(ISystemViewElementAdapter.class); | ||
407 | if (va != null) { | ||
408 | if (root instanceof IHost) { | ||
409 | ITerminalServiceSubSystem terminalServiceSubSystem = TerminalServiceHelper | ||
410 | .getTerminalSubSystem((IHost) root); | ||
411 | TerminalElement element = terminalServiceSubSystem | ||
412 | .getChild(titem.getText()); | ||
413 | if (element != null) { | ||
414 | va = (ISystemViewElementAdapter) element | ||
415 | .getAdapter(ISystemViewElementAdapter.class); | ||
416 | titem | ||
417 | .setImage(va.getImageDescriptor(element) | ||
418 | .createImage()); | ||
419 | return; | ||
420 | } | ||
421 | } | ||
422 | |||
423 | titem.setImage(va.getImageDescriptor(root).createImage()); | ||
424 | } | ||
425 | } | ||
426 | |||
427 | private void updateWithUniqueTitle(String title, CTabItem currentItem) { | ||
428 | CTabItem[] items = tabFolder.getItems(); | ||
429 | int increment = 1; | ||
430 | String temp = title; | ||
431 | for (int i = 0; i < items.length; i++) { | ||
432 | if (items[i] != currentItem) { | ||
433 | String name = items[i].getText(); | ||
434 | if (name != null) { | ||
435 | if (name.equals(temp)) { | ||
436 | temp = title + " " + increment++; //$NON-NLS-1$ | ||
437 | } | ||
438 | } | ||
439 | |||
440 | } | ||
441 | } | ||
442 | currentItem.setText(temp); | ||
443 | } | ||
444 | |||
445 | private ITerminalViewControl getCurrentTerminalViewControl() { | ||
446 | if (tabFolder != null && !tabFolder.isDisposed()) { | ||
447 | CTabItem item = tabFolder.getSelection(); | ||
448 | if (item != null && !item.isDisposed()) { | ||
449 | Object data = item.getData(DATA_KEY_CONTROL); | ||
450 | if (data instanceof ITerminalViewControl) | ||
451 | return ((ITerminalViewControl) data); | ||
452 | } | ||
453 | } | ||
454 | return null; | ||
455 | } | ||
456 | */ | ||
457 | } | ||