Skip to main content
summaryrefslogtreecommitdiffstats
blob: ae21dc02683786696fd4e7c4e9bedf7d6849968c (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
/*******************************************************************************
 * Copyright (c) 2004 - 2005 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.tasklist.ui.views;

import java.util.Date;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.mylar.core.MylarPlugin;
import org.eclipse.mylar.tasklist.MylarTasklistPlugin;
import org.eclipse.mylar.tasklist.contribution.DatePicker;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.TitleEvent;
import org.eclipse.swt.browser.TitleListener;
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.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;
import org.eclipse.jface.dialogs.MessageDialog;

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

	private 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;
	private Text taskNameTextWidget = null;
	private Text issueURLTextWidget = null;
	private Button getDescButton = null;

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

	protected Control createDialogArea(Composite parent) {
		Composite composite = (Composite) super.createDialogArea(parent);
		GridLayout gl = new GridLayout(4, 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 + 100);
		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);
		taskNameTextWidget.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL
				| GridData.HORIZONTAL_ALIGN_FILL));

		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);
			}
		});
			        
        final DatePicker datePicker = new DatePicker(composite, SWT.NULL);	
        datePicker.setDateText("<reminder>");
		datePicker.addPickerSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent arg0) {
				reminderDate = datePicker.getDate().getTime();
			}

			public void widgetDefaultSelected(SelectionEvent arg0) {
				// ignore
			}
		});
		
		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 = 2;
		issueURLTextWidget.setLayoutData(urlData);
		
		getDescButton = new Button(composite, SWT.PUSH);
		getDescButton.setText("Get Description");
		getDescButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
		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) {
				String desc = retrieveTaskDescription(issueURLTextWidget.getText());
				if (!desc.equals("")){
					taskNameTextWidget.setText(desc);
				}
			}
			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 = MylarPlugin.getDefault().getPreferenceStore().getString(MylarTasklistPlugin.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;
		}
		
		String defaultPrefix = MylarPlugin.getDefault().getPreferenceStore().getString(MylarTasklistPlugin.DEFAULT_URL_PREFIX);
		if (!defaultPrefix.equals("")){
			return defaultPrefix;
		}
		
		return taskURL;
	}
	
	/**
	 * Attempts to set the task description to the title from
	 * the specified url
	 */
	protected String retrieveTaskDescription(final String url){
		
		try {
			final Shell shell = new Shell(Display.getDefault());
			shell.setVisible(false);
			Browser browser = new Browser(shell, SWT.NONE);
			browser.setUrl(url);
			
			browser.addTitleListener(new TitleListener(){
				
				//Determines when to ignore the second call to changed()
				boolean ignore = false;
				
				public void changed(TitleEvent event) {
					if (!ignore){
						if (event.title.equals(url)){
							return;
						}
						else{
							ignore = true;
							if (event.title.equals(url + "/") ||
									event.title.equals("Object not found!") || 
									event.title.equals("No page to display") || 
									event.title.equals("Cannot find server") || 
									event.title.equals("Invalid Bug ID")){ //Last one is bugzilla-specific
								MessageDialog.openError(Display.getDefault().getActiveShell(), "Task Description Error", "Could not retrieve a description from the specified web page.");
							}
							else{
								taskNameTextWidget.setText(event.title);
							}							
						}
					}
				}
			});
			
		} catch (RuntimeException e) {
			MylarPlugin.fail(e, "could not open task web page", false);
		}		

		return new String();
	}
	
	/**
	 * 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 new String();
		}
	}

	public String getSelectedPriority() {
		return priority;
	}

	public String getTaskname() {
		return taskName;
	}
	
	public Date getReminderDate() {
		return reminderDate;
	}

	public String getIssueURL(){
		return taskURL;
	}
	
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			taskName = taskNameTextWidget.getText();
			taskURL = issueURLTextWidget.getText();
		} else {
			taskName = null;
		}
		super.buttonPressed(buttonId);
	}

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

}

Back to the top