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
|
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Eclipse Public License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.eclipse.org/org/documents/epl-v10.php
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.ide.eclipse.adt.internal.wizards.templates;
import static com.android.ide.eclipse.adt.internal.wizards.templates.NewProjectWizard.CATEGORY_ACTIVITIES;
import static com.android.ide.eclipse.adt.internal.wizards.templates.TemplateHandler.PREVIEW_PADDING;
import static com.android.ide.eclipse.adt.internal.wizards.templates.TemplateHandler.PREVIEW_WIDTH;
import com.android.ide.eclipse.adt.AdtPlugin;
import com.android.ide.eclipse.adt.internal.editors.layout.gle2.ImageControl;
import com.google.common.io.Files;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
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.Label;
import org.eclipse.swt.widgets.List;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
class ActivityPage extends WizardPage implements SelectionListener {
private final NewProjectWizardState mValues;
private List mList;
private Button mCreateToggle;
private java.util.List<File> mTemplates;
private boolean mIgnore;
private boolean mShown;
private ImageControl mPreview;
private Image mPreviewImage;
private Label mHeading;
private Label mDescription;
/**
* Create the wizard.
*/
ActivityPage(NewProjectWizardState values) {
super("activityPage"); //$NON-NLS-1$
mValues = values;
setTitle("Create Activity");
setDescription("Select whether to create an activity, and if so, what kind of activity.");
}
@Override
public void createControl(Composite parent) {
Composite container = new Composite(parent, SWT.NULL);
setControl(container);
}
@SuppressWarnings("unused") // SWT constructors have side effects and aren't unused
private void onEnter() {
Composite container = (Composite) getControl();
container.setLayout(new GridLayout(3, false));
mCreateToggle = new Button(container, SWT.CHECK);
mCreateToggle.setSelection(true);
mCreateToggle.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1));
mCreateToggle.setText("Create Activity");
mCreateToggle.addSelectionListener(this);
mList = new List(container, SWT.BORDER | SWT.V_SCROLL);
mList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
mTemplates = mValues.template.getManager().getTemplates(CATEGORY_ACTIVITIES);
java.util.List<String> names = new ArrayList<String>(mTemplates.size());
File current = mValues.activityValues.getTemplateLocation();
int index = -1;
for (int i = 0, n = mTemplates.size(); i < n; i++) {
File template = mTemplates.get(i);
names.add(template.getName());
if (template.equals(current)) {
index = i;
}
}
String[] items = names.toArray(new String[names.size()]);
mList.setItems(items);
if (index == -1 && !mTemplates.isEmpty()) {
mValues.activityValues.setTemplateLocation(mTemplates.get(0));
index = 0;
}
if (index >= 0) {
mList.setSelection(index);
mList.addSelectionListener(this);
}
// Preview
mPreview = new ImageControl(container, SWT.NONE, null);
GridData gd_mImage = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
gd_mImage.widthHint = PREVIEW_WIDTH + 2 * PREVIEW_PADDING;
mPreview.setLayoutData(gd_mImage);
new Label(container, SWT.NONE);
mHeading = new Label(container, SWT.NONE);
mHeading.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
new Label(container, SWT.NONE);
mDescription = new Label(container, SWT.WRAP);
mDescription.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 2, 1));
Font font = JFaceResources.getFontRegistry().getBold(JFaceResources.BANNER_FONT);
if (font != null) {
mHeading.setFont(font);
}
updatePreview();
}
private void updatePreview() {
Image oldImage = mPreviewImage;
mPreviewImage = null;
String title = "";
String description = "";
TemplateHandler handler = mValues.activityValues.getTemplateHandler();
TemplateMetadata template = handler.getTemplate();
if (template != null) {
String thumb = template.getThumbnailPath();
if (thumb != null && !thumb.isEmpty()) {
File file = new File(mValues.activityValues.getTemplateLocation(),
thumb.replace('/', File.separatorChar));
if (file != null) {
try {
byte[] bytes = Files.toByteArray(file);
ByteArrayInputStream input = new ByteArrayInputStream(bytes);
mPreviewImage = new Image(getControl().getDisplay(), input);
input.close();
} catch (IOException e) {
AdtPlugin.log(e, null);
}
}
}
title = template.getTitle();
description = template.getDescription();
}
mHeading.setText(title);
mDescription.setText(description);
mPreview.setImage(mPreviewImage);
mPreview.fitToWidth(PREVIEW_WIDTH);
if (oldImage != null) {
oldImage.dispose();
}
Composite parent = (Composite) getControl();
parent.layout(true, true);
parent.redraw();
}
@Override
public void dispose() {
super.dispose();
if (mPreviewImage != null) {
mPreviewImage.dispose();
mPreviewImage = null;
}
}
@Override
public void setVisible(boolean visible) {
if (visible && !mShown) {
onEnter();
}
super.setVisible(visible);
if (visible) {
mShown = true;
try {
mIgnore = true;
mCreateToggle.setSelection(mValues.createActivity);
} finally {
mIgnore = false;
}
}
validatePage();
}
private void validatePage() {
IStatus status = null;
if (mList.getSelectionCount() < 1) {
status = new Status(IStatus.ERROR, AdtPlugin.PLUGIN_ID,
"Select an activity type");
} else {
TemplateHandler templateHandler = mValues.activityValues.getTemplateHandler();
status = templateHandler.validateTemplate(mValues.minSdkLevel);
}
setPageComplete(status == null || status.getSeverity() != IStatus.ERROR);
if (status != null) {
setMessage(status.getMessage(),
status.getSeverity() == IStatus.ERROR
? IMessageProvider.ERROR : IMessageProvider.WARNING);
} else {
setErrorMessage(null);
setMessage(null);
}
}
@Override
public boolean isPageComplete() {
// Ensure that the Finish button isn't enabled until
// the user has reached and completed this page
if (!mShown) {
return false;
}
return super.isPageComplete();
}
// ---- Implements SelectionListener ----
@Override
public void widgetSelected(SelectionEvent e) {
if (mIgnore) {
return;
}
Object source = e.getSource();
if (source == mCreateToggle) {
mValues.createActivity = mCreateToggle.getSelection();
mList.setEnabled(mValues.createActivity);
} else if (source == mList) {
int index = mList.getSelectionIndex();
if (index >= 0 && index < mTemplates.size()) {
File template = mTemplates.get(index);
mValues.activityValues.setTemplateLocation(template);
updatePreview();
}
}
validatePage();
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
}
|