Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7a486551bd74e229e164157fe65b8fe46ccf626b (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*******************************************************************************
 * Copyright (c) 2004 - 2006 University Of British Columbia 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:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylar.internal.tasks.ui.views;

import java.util.Date;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.mylar.context.core.MylarStatusHandler;
import org.eclipse.mylar.internal.tasks.ui.RetrieveTitleFromUrlJob;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.SelectionAdapter;
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.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * @author Ken Sueda
 * @author Wesley Coelho (Extended to allow URL input)
 * @author Mik Kersten
 */
public class TaskInputDialog extends Dialog {

	public static final String LABEL_SHELL = "New Task";

	private static final String LABEL_DESCRIPTION = "Description:";

	private String taskName = "";

	private String priority = "P3";

	private String taskURL = "http://";

	private Date reminderDate = null;

	Text taskNameTextWidget = null;

	private Text issueURLTextWidget = null;

	private Button getDescButton = null;

	public TaskInputDialog(Shell parentShell) {
		super(parentShell);
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = (Composite) super.createDialogArea(parent);
		GridLayout gl = new GridLayout(5, false);
		composite.setLayout(gl);
		GridData data = new GridData(GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL | GridData.HORIZONTAL_ALIGN_FILL
				| GridData.VERTICAL_ALIGN_CENTER);
		data.widthHint = convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH + 180);
		composite.setLayoutData(data);

		Label taskNameLabel = new Label(composite, SWT.WRAP);
		taskNameLabel.setText(LABEL_DESCRIPTION);
		taskNameLabel.setFont(parent.getFont());

		taskNameTextWidget = new Text(composite, SWT.SINGLE | SWT.BORDER);
		GridData taskNameGD = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL);
		taskNameGD.widthHint = 200;
		taskNameGD.horizontalSpan = 1; 
		taskNameTextWidget.setLayoutData(taskNameGD);

		final Combo c = new Combo(composite, SWT.NO_BACKGROUND | SWT.MULTI | SWT.V_SCROLL | SWT.READ_ONLY
				| SWT.DROP_DOWN);
		c.setItems(TaskListView.PRIORITY_LEVELS);
		c.setText(priority);
		c.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				priority = c.getText();
			}

			public void widgetDefaultSelected(SelectionEvent e) {
				widgetSelected(e);
			}
		});

//		Label spacer = new Label(composite, SWT.NONE);
//		GridData spacerGD = new GridData();
//		spacerGD.horizontalSpan = 1;
////		spacerGD.widthHint = 5;
//		spacer.setLayoutData(spacerGD);
//		
//		Composite reminderComp = new Composite(composite, SWT.NONE);
//		GridLayout reminderCompGL = new GridLayout(3, false);
//		reminderCompGL.marginHeight = 0;
//		reminderCompGL.marginWidth = 0;		
//		reminderComp.setLayout(reminderCompGL);
//		GridData reminderCompGD = new GridData();
//		reminderCompGD.horizontalSpan = 1;		
//		reminderCompGD.horizontalAlignment = SWT.RIGHT;
//		reminderComp.setLayoutData(reminderCompGD);
//		Label reminderLabel = new Label(reminderComp, SWT.NONE);
		final DatePicker datePicker = new DatePicker(composite, SWT.BORDER, DatePicker.LABEL_CHOOSE);
		datePicker.addPickerSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent arg0) {
				if (datePicker.getDate() != null) {
					reminderDate = datePicker.getDate().getTime();
				}
			}

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// ignore
			}
		});
		
		Button removeReminder = new Button(composite, SWT.PUSH | SWT.CENTER);
		removeReminder.setText("Clear");
		removeReminder.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				datePicker.setDate(null);
				reminderDate = null;
			}
		});
		
//		
//		datePicker.setLayout(new GridLayout());
//		GridData datePickerGD = new GridData();
//		datePickerGD.widthHint = 300;
//		datePicker.setLayoutData(datePickerGD);
		
		
		Label urlLabel = new Label(composite, SWT.WRAP);
		urlLabel.setText("Web Link:");
		urlLabel.setFont(parent.getFont());

		issueURLTextWidget = new Text(composite, SWT.SINGLE | SWT.BORDER);
		issueURLTextWidget.setText(getDefaultIssueUrl());
		GridData urlData = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL);
		urlData.horizontalSpan = 3;
		urlData.grabExcessHorizontalSpace = true;
		issueURLTextWidget.setLayoutData(urlData);

		getDescButton = new Button(composite, SWT.PUSH);
		getDescButton.setText("Get Description");
		getDescButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		setButtonStatus();

		issueURLTextWidget.addKeyListener(new KeyListener() {
			public void keyPressed(KeyEvent e) {
				setButtonStatus();
			}

			public void keyReleased(KeyEvent e) {
				setButtonStatus();
			}
		});

		getDescButton.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				retrieveTaskDescription(issueURLTextWidget.getText());
			}

			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});

		return composite;
	}

	/**
	 * Sets the Get Description button enabled or not depending on whether there
	 * is a URL specified
	 */
	protected void setButtonStatus() {
		String url = issueURLTextWidget.getText();

		if (url.length() > 10 && (url.startsWith("http://") || url.startsWith("https://"))) {
//			String defaultPrefix = ContextCorePlugin.getDefault().getPreferenceStore().getString(
//					TaskListPreferenceConstants.DEFAULT_URL_PREFIX);
//			if (url.equals(defaultPrefix)) {
//				getDescButton.setEnabled(false);
//			} else {
				getDescButton.setEnabled(true);
//			}
		} else {
			getDescButton.setEnabled(false);
		}
	}

	/**
	 * Returns the default URL text for the task by first checking the contents
	 * of the clipboard and then using the default prefix preference if that
	 * fails
	 */
	protected String getDefaultIssueUrl() {

		String clipboardText = getClipboardText();
		if ((clipboardText.startsWith("http://") || clipboardText.startsWith("https://") && clipboardText.length() > 10)) {
			return clipboardText;
		} else {
			return taskURL;
		}
//		String defaultPrefix = ContextCorePlugin.getDefault().getPreferenceStore().getString(
//				TaskListPreferenceConstants.DEFAULT_URL_PREFIX);
//		if (!defaultPrefix.equals("")) {
//			return defaultPrefix;
//		}
	}

	/**
	 * Attempts to set the task pageTitle to the title from the specified url
	 */
	protected void retrieveTaskDescription(final String url) {

		try {
			RetrieveTitleFromUrlJob job = new RetrieveTitleFromUrlJob(issueURLTextWidget.getText()) {

				@Override
				protected void setTitle(final String pageTitle) {
					taskNameTextWidget.setText(pageTitle);
				}

			};
			job.schedule();

		} catch (RuntimeException e) {
			MylarStatusHandler.fail(e, "could not open task web page", false);
		}
	}

	/**
	 * Returns the contents of the clipboard or "" if no text content was
	 * available
	 */
	protected String getClipboardText() {
		Clipboard clipboard = new Clipboard(Display.getDefault());
		TextTransfer transfer = TextTransfer.getInstance();
		String contents = (String) clipboard.getContents(transfer);
		if (contents != null) {
			return contents;
		} else {
			return "";
		}
	}

	public String getSelectedPriority() {
		return priority;
	}

	public String getTaskname() {
		return taskName;
	}

	public Date getReminderDate() {
		return reminderDate;
	}

	public String getIssueURL() {
		return taskURL;
	}

	@Override
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			taskName = taskNameTextWidget.getText();
			taskURL = issueURLTextWidget.getText();
		} else {
			taskName = null;
		}
		super.buttonPressed(buttonId);
	}

	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText(LABEL_SHELL);
	}
}

Back to the top