Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6583b5f555abb0904334a8740cd4a88e90bbac93 (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
/*******************************************************************************
 * Copyright (c) 2016 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:
 * 		hrentz (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.generator.ui.wizard.internal;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.etrice.generator.base.AbstractGeneratorOptions;
import org.eclipse.etrice.generator.ui.wizard.WizardHelpers;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
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.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.wizards.datatransfer.ZipFileStructureProvider;

/**
 * @author hrentz
 *
 */
public class COptionsPage extends WizardPage {

	private IProjectPathProvider projectPathProvider;
	private Button copyRuntimeButton = null;
	private Combo platformCombo = null;
	private Text mainMethodName = null;

	public COptionsPage(String title, IProjectPathProvider projectPathProvider) {
		super(title);
		this.projectPathProvider = projectPathProvider;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createControl(Composite parent) {
		Composite buttonComposite = new Composite(parent, SWT.NONE);

		GridData gd = new GridData(
				GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_END);
		buttonComposite.setLayoutData(gd);
		buttonComposite.setLayout(new GridLayout(2, false));
		
		copyRuntimeButton = new Button(buttonComposite, SWT.CHECK);
		copyRuntimeButton.setText("&Copy C-Runtime into project");
		copyRuntimeButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});
		gd = new GridData();
		gd.horizontalSpan = 2;
		copyRuntimeButton.setLayoutData(gd);
		
		Label l = new Label(buttonComposite, SWT.NONE);
		l.setText("Select active &platform:");
		l.setLayoutData(new GridData(SWT.NONE));
		
		platformCombo = new Combo(buttonComposite, SWT.READ_ONLY);
		gd = new GridData(SWT.HORIZONTAL);
		gd.widthHint = 200;
		platformCombo.setLayoutData(gd);
		platformCombo.setVisibleItemCount(10);
		
		l = new Label(buttonComposite, SWT.NONE);
		l.setText("eTrice &entry function:");
		l.setLayoutData(new GridData(SWT.NONE));
		
		mainMethodName = new Text(buttonComposite, SWT.SINGLE | SWT.BORDER);
		mainMethodName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		mainMethodName.setText(AbstractGeneratorOptions.MAIN_NAME.getDefaultValue());
		
		setErrorMessage(null);
		setMessage(null);
		setControl(buttonComposite);
	}
	
    @Override
    public void setVisible(boolean visible)
    {
		super.setVisible(visible);
		
		if (visible) {
			refresh();
		}
    }

	private void refresh() {
		copyRuntimeButton.setEnabled(false);
		platformCombo.setEnabled(false);
		platformCombo.removeAll();
		
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		IPath chosenPath = projectPathProvider.getPath();
		IProject project = (chosenPath.segmentCount()==1)?
				workspace.getRoot().getProject(chosenPath.lastSegment())
				: workspace.getRoot().getFolder(chosenPath).getProject();
				
		try {
			if (project.hasNature("org.eclipse.cdt.core.cnature")) {
				copyRuntimeButton.setEnabled(true);
				platformCombo.setEnabled(true);
				
				List<String> platformNames = getPlatformNames();
				for (String name : platformNames) {
					platformCombo.add(name);
				}
				
				platformCombo.select(0);
			}
		} catch (CoreException e) {
			e.printStackTrace();
		}
	}

	private List<String> getPlatformNames() {
		List<String> result = new ArrayList<String>();

		ZipFile zipFile = WizardHelpers.getCRuntimeZip();
		if (zipFile != null) {
			ZipFileStructureProvider structureProvider = new ZipFileStructureProvider(zipFile);
			List<?> children = structureProvider.getChildren(structureProvider.getRoot());
			ZipEntry current = null;
			for (Object child : children) {
				if (child instanceof ZipEntry) {
					if (((ZipEntry) child).getName().equals("src/")) {
						current = (ZipEntry) child;
						break;
					}
				}
			}
			if (current!=null) {
				children = structureProvider.getChildren(current);
				current = null;
				for (Object child : children) {
					if (child instanceof ZipEntry) {
						if (((ZipEntry) child).getName().equals("src/platforms/")) {
							current = (ZipEntry) child;
							break;
						}
					}
				}
			}
			if (current!=null) {
				children = structureProvider.getChildren(current);
				for (Object child : children) {
					if (child instanceof ZipEntry) {
						String name = ((ZipEntry) child).getName();
						if (name.endsWith("/")) {
							String[] split = name.split("/");
							result.add(split[split.length-1]);
						}
					}
				}
			}
			try {
				zipFile.close();
			} catch (IOException e) {
				// Ignore.
			}
		}
		
		return result;
	}
	
	public boolean getCopyRuntime() {
		if (copyRuntimeButton!=null && !copyRuntimeButton.isDisposed()) {
			return copyRuntimeButton.getSelection();
		}
		return false;
	}
	
	public String getPlatform() {
		if (platformCombo!=null && !platformCombo.isDisposed()) {
			if (platformCombo.getSelectionIndex()>=0) {
				return platformCombo.getText();
			}
		}
		
		return "";
	}
	
	public String getMainMethodName() {
		if (mainMethodName!=null && !mainMethodName.isDisposed()) {
			return mainMethodName.getText();
		}
		
		return AbstractGeneratorOptions.MAIN_NAME.getDefaultValue();
	}
}

Back to the top