Skip to main content
summaryrefslogtreecommitdiffstats
blob: bef925c5fa3774078415c92188293ae3dad743e5 (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
/*******************************************************************************
 * Copyright (c) 2004, 2007 Mylyn project committers 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
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.ui.wizards;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.mylyn.internal.tasks.core.LocalAttachment;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiImages;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
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;

/**
 * A wizard page to enter details of a new attachment.
 * 
 * @author Jeff Pound
 */
public class NewAttachmentPage extends WizardPage {

	private LocalAttachment attachment;

	private Text filePath;

	private Text attachmentDesc;

	private Text attachmentComment;

	private Button isPatchButton;

	private Button attachContextButton;

	private static List<String> contentTypes;

	private static Map<String, String> extensions2Types;

	static {
		/* For UI */
		contentTypes = new LinkedList<String>();
		contentTypes.add("text/plain");
		contentTypes.add("text/html");
		contentTypes.add("application/xml");
		contentTypes.add("image/gif");
		contentTypes.add("image/jpeg");
		contentTypes.add("image/png");
		contentTypes.add("application/octet-stream");

		/* For auto-detect */
		extensions2Types = new HashMap<String, String>();
		extensions2Types.put("txt", "text/plain");
		extensions2Types.put("html", "text/html");
		extensions2Types.put("htm", "text/html");
		extensions2Types.put("jpg", "image/jpeg");
		extensions2Types.put("jpeg", "image/jpeg");
		extensions2Types.put("gif", "image/gif");
		extensions2Types.put("png", "image/png");
		extensions2Types.put("xml", "application/xml");
		extensions2Types.put("zip", "application/octet-stream");
		extensions2Types.put("tar", "application/octet-stream");
		extensions2Types.put("gz", "application/octet-stream");
	}

	protected NewAttachmentPage(LocalAttachment att) {
		super("AttachmentDetails");
		setTitle("Attachment Details");
		setMessage("Provide a summary and verify the content type of the attachment.");
		attachment = att;
	}

	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		composite.setLayout(gridLayout);
		setControl(composite);

		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		composite.setLayout(new GridLayout(3, false));

		final NewAttachmentPage thisPage = this;

		new Label(composite, SWT.NONE).setText("File");
		filePath = new Text(composite, SWT.BORDER);
		filePath.setEditable(false);
		filePath.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 2, 1));

		new Label(composite, SWT.NONE).setText("Description");
		attachmentDesc = new Text(composite, SWT.BORDER);
		attachmentDesc.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 2, 1));

		attachmentDesc.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				if ("".equals(attachmentDesc.getText().trim())) {
					thisPage.setErrorMessage("Description required");
				} else {
					if (!"".equals(filePath.getText())) {
						thisPage.setPageComplete(true);
						thisPage.setErrorMessage(null);
					}
				}
			}

		});

		Label label = new Label(composite, SWT.NONE);
		label.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false));
		label.setText("Comment");
		attachmentComment = new Text(composite, SWT.V_SCROLL | SWT.BORDER | SWT.WRAP);
		attachmentComment.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));

		new Label(composite, SWT.NONE).setText("Content Type");// .setBackground(parent.getBackground());

		final Combo contentTypeList = new Combo(composite, SWT.BORDER | SWT.DROP_DOWN | SWT.READ_ONLY);
		contentTypeList.setLayoutData(new GridData(SWT.DEFAULT, SWT.DEFAULT, false, false, 2, 1));
		final HashMap<String, Integer> contentTypeIndices = new HashMap<String, Integer>();
		Iterator<String> iter = contentTypes.iterator();
		int i = 0;
		while (iter.hasNext()) {
			String next = iter.next();
			contentTypeList.add(next);
			contentTypeIndices.put(next, new Integer(i));
			i++;
		}

		/* Update attachment on select content type */
		contentTypeList.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {
				// ignore
			}

			public void widgetSelected(SelectionEvent e) {
				attachment.setContentType(contentTypeList.getItem(contentTypeList.getSelectionIndex()));
			}
		});
		contentTypeList.select(0);
		attachment.setContentType(contentTypeList.getItem(0));

		// TODO: is there a better way to pad?
		new Label(composite, SWT.NONE);

		isPatchButton = new Button(composite, SWT.CHECK);
		isPatchButton.setLayoutData(new GridData(SWT.DEFAULT, SWT.DEFAULT, false, false, 2, 1));
		isPatchButton.setText("Patch");

		// TODO: is there a better way to pad?
		new Label(composite, SWT.NONE);

		attachContextButton = new Button(composite, SWT.CHECK);
		attachContextButton.setLayoutData(new GridData(SWT.DEFAULT, SWT.DEFAULT, false, false, 2, 1));
		attachContextButton.setText("Attach Context");
		attachContextButton.setImage(TasksUiImages.getImage(TasksUiImages.CONTEXT_ATTACH));
		attachContextButton.setEnabled((((NewAttachmentWizard) getWizard()).hasContext()));

		/*
		 * Attachment file name listener, update the local attachment
		 * accordingly
		 */
		filePath.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				// Determine type by extension
				int index = filePath.getText().lastIndexOf(".");
				if (index > 0 && index < filePath.getText().length()) {
					String ext = filePath.getText().substring(index + 1);
					String type = extensions2Types.get(ext.toLowerCase(Locale.ENGLISH));
					if (type != null) {
						contentTypeList.select(contentTypeIndices.get(type));
						attachment.setContentType(type);
					}
				}

				// check page completenes
				if ("".equals(attachmentDesc.getText())) {
					thisPage.setErrorMessage("Description required");
				} else {
					if (!"".equals(filePath.getText())) {
						thisPage.setPageComplete(true);
						thisPage.setErrorMessage(null);
					}
				}
			}
		});

		filePath.setText(attachment.getFilePath());

		/* Listener for isPatch */
		isPatchButton.addSelectionListener(new SelectionListener() {
			private int lastSelected;

			public void widgetDefaultSelected(SelectionEvent e) {
				// ignore
			}

			public void widgetSelected(SelectionEvent e) {
				attachment.setPatch(isPatchButton.getSelection());
				if (isPatchButton.getSelection()) {
					lastSelected = contentTypeList.getSelectionIndex();
					contentTypeList.select(0);
					contentTypeList.setEnabled(false);
					if (attachContextButton.isEnabled()) {
						attachContextButton.setSelection(true);
					}
				} else {
					contentTypeList.setEnabled(true);
					contentTypeList.select(lastSelected);
				}
			}
		});
	}

	@Override
	public boolean isPageComplete() {
		return !"".equals(filePath.getText().trim()) && !"".equals(attachmentDesc.getText().trim());
	}

	public void populateAttachment() {
		attachment.setDescription(attachmentDesc.getText());
		attachment.setComment(attachmentComment.getText());
	}

	public LocalAttachment getAttachment() {
		return attachment;
	}

	@Override
	public boolean canFlipToNextPage() {
		return isPageComplete();
	}

	public void setFilePath(String path) {
		filePath.setText(path);
		if (path.endsWith(".patch")) {
			isPatchButton.setSelection(true);
			if (attachContextButton.isEnabled()) {
				attachContextButton.setSelection(true);
			}
		}
	}

	@Override
	public IWizardPage getNextPage() {
		populateAttachment();
		PreviewAttachmentPage page = new PreviewAttachmentPage(getAttachment());
		page.setWizard(getWizard());
		return page;
	}

	public boolean getAttachContext() {
		return attachContextButton.getSelection();
	}
}

Back to the top