summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/TerminalHandler.java
blob: af5a5dac7f0b27c08c550d6c63f62556da6b8931 (plain)
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
/*******************************************************************************
 * Copyright (c) 2013 Intel Corporation.
 * 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:
 * Intel - initial API and implementation
 *******************************************************************************/
package org.yocto.remote.utils;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.rse.core.model.IHost;
import org.eclipse.rse.internal.terminals.ui.TerminalServiceHelper;
import org.eclipse.rse.internal.terminals.ui.views.RSETerminalConnector;
import org.eclipse.rse.internal.terminals.ui.views.TerminalViewTab;
import org.eclipse.rse.internal.terminals.ui.views.TerminalViewer;
import org.eclipse.rse.internal.terminals.ui.views.TerminalsUI;
import org.eclipse.rse.services.terminals.ITerminalShell;
import org.eclipse.rse.subsystems.terminals.core.ITerminalServiceSubSystem;
import org.eclipse.rse.subsystems.terminals.core.elements.TerminalElement;
import org.eclipse.rse.ui.SystemBasePlugin;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.tm.internal.terminal.control.ITerminalViewControl;
import org.eclipse.tm.internal.terminal.provisional.api.ITerminalConnector;

abstract public class TerminalHandler extends AbstractHandler {


	protected Shell shell;

	protected String changeTerm = "export TERM=vt100;";
	
	abstract protected String getInitCmd();
	abstract protected String getConnnectionName();
	abstract protected String getDialogTitle();

	protected String changeTerm() {
		return changeTerm;
	}
	
	protected ITerminalShell getTerminalShellFromTab(CTabItem item) {
        ITerminalShell terminalShell = null;
        ITerminalViewControl terminalViewControl = (ITerminalViewControl) item
                .getData(TerminalViewTab.DATA_KEY_CONTROL);
        ITerminalConnector terminalConnector = terminalViewControl
                .getTerminalConnector();
        if (terminalConnector instanceof RSETerminalConnector) {
            RSETerminalConnector rseTerminalConnector = (RSETerminalConnector) terminalConnector;
            terminalShell = rseTerminalConnector.getTerminalHostShell();
        }
        return terminalShell;
    }

	protected boolean preProcess(final ITerminalServiceSubSystem terminalSubSystem) {
		if (!terminalSubSystem.isConnected()) {
			try {
				ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell);
				dialog.run(true, true, new IRunnableWithProgress(){
				    @Override
					public void run(IProgressMonitor monitor) {
				        monitor.beginTask("Connecting to remote target ...", 100);
				        try {
				        terminalSubSystem.connect(new NullProgressMonitor(), false);
				        monitor.done();
				        } catch (Exception e) {
				        	CommonHelper.showErrorDialog("Connection failure", null, e.getMessage());
				        	monitor.done();

				        }
				    }
				});
			} catch (OperationCanceledException e) {
				// user canceled, return silently
				return false;
			} catch (Exception e) {
				SystemBasePlugin.logError(e.getLocalizedMessage(), e);
				return false;
			}
		} else
			return true;
		return false;
	}

	public void execute(IHost host) throws ExecutionException {

		final ITerminalServiceSubSystem terminalSubSystem = RemoteHelper.getTerminalSubSystem(host);

		if (terminalSubSystem != null) {
			TerminalsUI terminalsUI = TerminalsUI.getInstance();
			TerminalViewer viewer = terminalsUI.activateTerminalsView();
			if (preProcess(terminalSubSystem)) {
				CTabItem tab = viewer.getTabFolder().createTabItem(
						terminalSubSystem.getHost(), changeTerm() + getInitCmd());
				//since RSETerminalConnector not exit the shell during the diconnection,
				//we have manually exit it here
				try {
					tab.addDisposeListener(new DisposeListener() {
						@Override
						public void widgetDisposed(DisposeEvent e) {
							Object source = e.getSource();
							if (source instanceof CTabItem) {
								CTabItem currentItem = (CTabItem) source;
								ITerminalShell shell=getTerminalShellFromTab(currentItem);
								if(shell!=null) {
									shell.exit();
								}
							}
						}
					});
				}catch(Exception e) {
					e.printStackTrace();
				}
				TerminalElement element = TerminalServiceHelper
						.createTerminalElement(tab, terminalSubSystem);
				terminalSubSystem.addChild(element);

			}
		}
	}

}