Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5858c02e8e7524653038309b9c6be9b61fb5e966 (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
/*******************************************************************************
 * Copyright (c) 2011 protos software gmbh (http://www.protos.de).
 * 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:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.generator.launch;

import java.util.ArrayList;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.variables.VariablesPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.ResourceListSelectionDialog;
import org.eclipse.ui.dialogs.ResourceSelectionDialog;

/**
 * @author Henrik Rentz-Reichert (initial contribution)
 *
 */
public abstract class GeneratorMainTab extends AbstractLaunchConfigurationTab {

	private List modelList;
	
	@Override
	public void createControl(Composite parent) {
		try{
			// Create main composite
			Composite mainComposite = new Composite(parent,SWT.NONE);
			setControl(mainComposite);
			
			GridLayout layout= new GridLayout();
			layout.numColumns = 2;
			layout.marginHeight = 0;
			layout.marginWidth = 10;
			mainComposite.setLayout(layout);
	
			createModelsEditor(mainComposite);
			
			Dialog.applyDialogFont(mainComposite);
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param mainComposite
	 */
	private void createModelsEditor(Composite mainComposite) {
		createModelList(mainComposite);
		createModelButtons(mainComposite);
	}

	/**
	 * @param parent
	 */
	private void createModelButtons(Composite parent) {
		Composite buttons = new Composite(parent,SWT.NONE | SWT.TOP);
		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		buttons.setLayout(layout);
		GridData gd = new GridData();
		gd.verticalAlignment = SWT.BEGINNING;
		buttons.setLayoutData(gd);

		makeButton(buttons, new AddModel(), "Add (filtered)");
		makeButton(buttons, new AddModelAlt(), "Add (selection)");
		makeButton(buttons, new RemoveModel(), "Remove");
		makeButton(buttons, new MoveModelUp(), "Move Up");
		makeButton(buttons, new MoveModelDown(), "Move Down");
	}

	/**
	 * @param parent
	 */
	private void createModelList(Composite parent) {
		Label label = new Label(parent, SWT.NONE);
		label.setText("Models to generate:");
		GridData gd = new GridData(SWT.BEGINNING, SWT.BEGINNING, true, false, 2, 1);
		gd.verticalIndent = 20;
		label.setLayoutData(gd);

		modelList = new List(parent,SWT.NONE | SWT.BORDER | SWT.SINGLE);
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.verticalAlignment = GridData.FILL;
		gridData.grabExcessVerticalSpace = true;
		modelList.setLayoutData(gridData);
	}

	/**
	 * @param buttons
	 * @param listener
	 * @param label
	 * @return
	 */
	private GridData makeButton(Composite buttons, Listener listener, String label) {
		Button addBtn = new Button(buttons, SWT.PUSH | SWT.CENTER | SWT.TOP);
		addBtn.setText(label);
		GridData gd = new GridData();
		gd.horizontalAlignment = GridData.FILL;
		gd.grabExcessHorizontalSpace = false;
		addBtn.setLayoutData(gd);
		addBtn.addListener(SWT.Selection,listener);
		return gd;
	}

	@Override
	public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
	}

	@Override
	public void initializeFrom(ILaunchConfiguration configuration) {
		try {
			modelList.removeAll();
			ArrayList<String> param = new ArrayList<String>();
			param = (ArrayList<String>) configuration.getAttribute("ModelFiles", param);
			for (String tmp : param) {
				addModelFile(tmp);
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void performApply(ILaunchConfigurationWorkingCopy configuration) {
		try {
			ArrayList<String> param = new ArrayList<String>();
			String[] tmp = modelList.getItems();
			for (int i = 0; i < tmp.length; i++) {
				param.add(tmp[i]);
			}
			configuration.setAttribute("ModelFiles", param);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public String getName() {
		return "Main";
	}

	private void addModelFile(String str) {
		modelList.add(str);
		setDirty(true);
		updateLaunchConfigurationDialog();
	}
	
	protected abstract boolean isValidModelFile(IResource resource);
	
	private class AddModel implements Listener {
		@Override
		public void handleEvent(Event event) {
			ModelSelectionDialog dialog = new ModelSelectionDialog(modelList.getShell());
			dialog.setTitle("Resource Selection");
			dialog.setMessage("Select Room Model:");
			if (dialog.open() == Window.OK) {
				Object[] tmp = dialog.getResult();
				if (tmp != null) {
					IResource f;
					for (int i = 0; i < tmp.length; i++) {
						f = (IResource) tmp[i];
			            String arg = f.getFullPath().toString();
			            String fileLoc = VariablesPlugin.getDefault().getStringVariableManager().generateVariableExpression("workspace_loc", arg); //$NON-NLS-1$
						addModelFile(fileLoc);
					}
				}
			}
		}
	}
	
	private class AddModelAlt implements Listener {
		@Override
		public void handleEvent(Event event) {
			ResourceSelectionDialog dialog = new ResourceSelectionDialog(modelList.getShell(), ResourcesPlugin.getWorkspace().getRoot(), "");
			dialog.setTitle("Resource Selection");
			dialog.setMessage("Select Room Model:");
			if (dialog.open() == Window.OK) {
				Object[] tmp = dialog.getResult();
				if (tmp != null) {
					IResource f;
					for (int i = 0; i < tmp.length; i++) {
						f = (IResource) tmp[i];
			            String arg = f.getFullPath().toString();
			            String fileLoc = VariablesPlugin.getDefault().getStringVariableManager().generateVariableExpression("workspace_loc", arg); //$NON-NLS-1$
						addModelFile(fileLoc);
					}
				}
			}
		}
	}

	private class RemoveModel implements Listener{
		@Override
		public void handleEvent(Event event) {
			String[] arr = modelList.getSelection();
			for (int i = 0; i < arr.length; i++) {
				modelList.remove(arr[i]);
			}
			setDirty(true);
			updateLaunchConfigurationDialog();
		}
	}
	
	private class MoveModelUp implements Listener{
		@Override
		public void handleEvent(Event event) {
			String[] arr=modelList.getSelection();
			for(int i=0; i<arr.length;i++){
				int index=modelList.indexOf(arr[i]);
				if(index!=0){
					modelList.remove(arr[i]);
					modelList.add(arr[i], index-1);
					modelList.setSelection(index-1);
				}
			}
			setDirty(true);
			updateLaunchConfigurationDialog();
		}
	}
	private class MoveModelDown implements Listener{
		@Override
		public void handleEvent(Event event) {
			String[] arr = modelList.getSelection();
			for (int i = 0; i < arr.length; i++) {
				int index = modelList.indexOf(arr[i]);
				if (index != (modelList.getItemCount() - 1)) {
					modelList.remove(arr[i]);
					modelList.add(arr[i], index + 1);
					modelList.setSelection(index + 1);
				}
			}
			setDirty(true);
			updateLaunchConfigurationDialog();
		}
	}
	
	private class ModelSelectionDialog extends ResourceListSelectionDialog {

		public ModelSelectionDialog(Shell parentShell) {
			super(parentShell, ResourcesPlugin.getWorkspace().getRoot(), IResource.FILE);
		}

		@Override
		protected boolean select(IResource resource) {
			return isValidModelFile(resource);
		}
	}
}

Back to the top