summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/actions/BaseSettingDialog.java
blob: 7845959643653bf7138eb1264b1d2f310a746402 (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
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
/*******************************************************************************
 * Copyright (c) 2006, 2010 PalmSource, 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:
 * Ewa Matejska          (PalmSource) - initial API and implementation
 * Martin Oberhuber      (Wind River) - [186773] split ISystemRegistryUI from ISystemRegistry
 * Martin Oberhuber      (Wind River) - [196934] hide disabled system types in remotecdt combo
 * Yu-Fen Kuo            (MontaVista) - [190613] Fix NPE in Remotecdt when RSEUIPlugin has not been loaded
 * Martin Oberhuber      (Wind River) - [cleanup] Avoid using SystemStartHere in production code
 * Johann Draschwandtner (Wind River) - [231827][remotecdt]Auto-compute default for Remote path
 * Johann Draschwandtner (Wind River) - [233057][remotecdt]Fix button enablement
 * Anna Dushistova       (MontaVista) - [181517][usability] Specify commands to be run before remote application launch
 * Anna Dushistova       (MontaVista) - [223728] [remotecdt] connection combo is not populated until RSE is activated
 * Anna Dushistova       (MontaVista) - [267951] [remotecdt] Support systemTypes without files subsystem
 * Lianhao Lu			 (Intel)      - Modified for internal use
 *******************************************************************************/

package org.yocto.sdk.remotetools.actions;

import org.yocto.remote.utils.RSEHelper;
import org.yocto.sdk.remotetools.Messages;
import org.yocto.sdk.remotetools.SWTFactory;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.rse.core.model.IHost;
import org.eclipse.rse.ui.actions.SystemNewConnectionAction;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class BaseSettingDialog extends Dialog {

	protected String title;
	protected String curConn; //current rse connection name
	protected IHost conn;//rse IHost
	
	protected Button newRemoteConnectionButton;
	protected Label connectionLabel;
	protected Combo connectionCombo;
	protected SystemNewConnectionAction action = null;
	
	
	protected BaseSettingDialog(Shell parentShell,String title, String connection) {
		super(parentShell);
		this.title=title;
		this.curConn=connection;
	}
	
	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(title);
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite comp = (Composite)super.createDialogArea(parent);
		GridLayout topLayout = new GridLayout();
		comp.setLayout(topLayout);
		
		/* The RSE Connection dropdown with New button. */
		SWTFactory.createVerticalSpacer(comp, 1);
		createRemoteConnectionGroup(comp);
		
		return comp;
	}
	
	
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		super.createButtonsForButtonBar(parent);
		updateCurConn();
	}

	protected void createRemoteConnectionGroup(Composite parent) {
		Composite projComp = new Composite(parent, SWT.NONE);
		GridLayout projLayout = new GridLayout();
		projLayout.numColumns = 6;
		projLayout.marginHeight = 0;
		projLayout.marginWidth = 0;
		projComp.setLayout(projLayout);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		projComp.setLayoutData(gd);

		connectionLabel = new Label(projComp, SWT.NONE);
		connectionLabel.setText(Messages.BaseSettingDialog_Connection);
		gd = new GridData();
		gd.horizontalSpan = 1;
		connectionLabel.setLayoutData(gd);
		
		connectionCombo = new Combo(projComp, SWT.DROP_DOWN | SWT.READ_ONLY);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 4;
		connectionCombo.setLayoutData(gd);
		connectionCombo.addModifyListener(new ModifyListener() {

			public void modifyText(ModifyEvent e) {
				//setDirty(true);
				//updateLaunchConfigurationDialog();
				//useDefaultsFromConnection();
				updateCurConn();
			}
		});

		newRemoteConnectionButton = SWTFactory.createPushButton(projComp,
				Messages.BaseSettingDialog_New, null);
		newRemoteConnectionButton.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent evt) {
				handleNewRemoteConnectionSelected();
				//updateLaunchConfigurationDialog();
				updateConnectionPulldown();
			}
		});
		gd = new GridData();
		gd.horizontalSpan = 1;
		newRemoteConnectionButton.setLayoutData(gd);

		updateConnectionPulldown();
	}
	
	protected boolean updateOkButton() {
		boolean ret=false;
		Button button=getButton(IDialogConstants.OK_ID);
		if(button!=null)
			button.setEnabled(false);
		IHost currentConnectionSelected = getCurrentConnection();
		if (currentConnectionSelected != null) {
			if (RSEHelper.isHostViable(currentConnectionSelected) && button != null){
					button.setEnabled(true);
					ret=true;
			}
		}
		return ret;
	}
	
	protected void updateCurConn() {
		IHost currentConnectionSelected = getCurrentConnection();
		if (currentConnectionSelected != null) {
			if (RSEHelper.isHostViable(currentConnectionSelected))
				curConn=currentConnectionSelected.getAliasName();
		}
		updateOkButton();
	}
	
	protected IHost getCurrentConnection() {
		int currentSelection = connectionCombo.getSelectionIndex();
		String remoteConnection = currentSelection >= 0 ? connectionCombo
				.getItem(currentSelection) : null;
        return RSEHelper.getRemoteConnectionByName(remoteConnection);
    }
	
	protected void handleNewRemoteConnectionSelected() {
		if (action == null) {
			action = new SystemNewConnectionAction(getShell(),
					false, false, null);
		}

		try {
			action.run();
		} catch (Exception e) {
			// Ignore
		}
	}
	
	protected void updateConnectionPulldown() {
		int index=-1;
		RSEHelper.waitForRSEInitCompletition();
		// already initialized
		connectionCombo.removeAll();
		IHost[] connections = RSEHelper.getSuitableConnections();
		for (int i = 0; i < connections.length; i++) {
			connectionCombo.add(connections[i].getAliasName());
			if(connections[i].getAliasName().equals(curConn))
				index=i;
		}
		
		if(index>=0) {
			connectionCombo.select(index);
		}else if (connections.length > 0) {
			connectionCombo.select(connections.length - 1);
		}
	
		//TODO
		//connectionCombo.computeSize(SWT.DEFAULT, SWT.DEFAULT,true);
		connectionCombo.pack(true);
		connectionCombo.layout();
		connectionCombo.getParent().layout();
		
		updateCurConn();
	}

	@Override
	protected void okPressed() {
		conn=getCurrentConnection();
		super.okPressed();
	}
	
	public IHost getHost() {
		return conn;
	}
}