Skip to main content
summaryrefslogtreecommitdiffstats
blob: e2cbe782da187229cc1a9a6d7719936da8422467 (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*******************************************************************************
 * 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.bugzilla.ui.tasklist;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.mylar.bugzilla.ui.query.GetQueryDialog;
import org.eclipse.mylar.bugzilla.ui.query.SaveQueryDialog;
import org.eclipse.mylar.bugzilla.ui.search.BugzillaSearchPage;
import org.eclipse.search.ui.ISearchPageContainer;
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.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkingSet;

/**
 * @author Shawn Minto
 */
public class BugzillaQueryDialog extends Dialog {

	private String url;
	private String name = "";
	private BugzillaSearchOptionPage searchOptionPage;
	private String startingUrl = null;
	private String maxHits;
	
	public BugzillaQueryDialog(Shell parentShell) {
		super(parentShell);
		searchOptionPage = new BugzillaSearchOptionPage();
		
	}
	
	public BugzillaQueryDialog(Shell parentShell, String startingUrl, String name, String maxHits) {
		super(parentShell);
		searchOptionPage = new BugzillaSearchOptionPage();
		this.startingUrl = startingUrl;
		this.maxHits = maxHits;
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public String getUrl() {
		return url;
	}
	
	public String getMaxHits(){
		return maxHits;
	}
	
	@Override
	protected Control createContents(Composite parent) {
		searchOptionPage.createControl(parent);
		
		searchOptionPage.setVisible(true);
		Control c = super.createContents(parent);
		if(startingUrl != null){
			try{
				searchOptionPage.updateDefaults(startingUrl, maxHits);
			} catch (UnsupportedEncodingException e){
				// ignore, should never get this
			}
		}
		return c;
	}

	@Override
	protected void okPressed(){
		url = searchOptionPage.getSearchURL();
		if(url == null || url.equals("")){
			/*
			 * Should never get here. Every implementation of the Java platform is required
			 * to support the standard charset "UTF-8"
			 */
			return;
		}
		maxHits = searchOptionPage.getMaxHits();
		InputDialog getNameDialog = new InputDialog(Display.getCurrent().getActiveShell(), "Bugzilla Query Category Name", "Please enter a name for the bugzilla query category",name, new IInputValidator(){

			public String isValid(String newText) {
				if(newText != null && !newText.equals("")){
					return null;
				} else {
					return "You must enter a name for the category";
				}
			}
				
		});
		getNameDialog.setBlockOnOpen(true);
		if(getNameDialog.open() == InputDialog.OK){
			name = getNameDialog.getValue();
			
			super.okPressed();
		} else {
			super.cancelPressed();
		}
	}
	
	private class BugzillaSearchOptionPage extends BugzillaSearchPage{
		
		public BugzillaSearchOptionPage() {
			
			preselectedStatusValues = new String[0];
			
			scontainer = new ISearchPageContainer() {
				public ISelection getSelection() {
					// TODO Auto-generated method stub
					return null;
				}

				public IRunnableContext getRunnableContext() {
					return null;
				}

				public void setPerformActionEnabled(boolean state) {
					Button ok = BugzillaQueryDialog.this.getButton(Dialog.OK);
					if (ok != null)
						ok.setEnabled(state);
				}

				public int getSelectedScope() {
					return 0;
				}

				public void setSelectedScope(int scope) {
				}

				public boolean hasValidScope() {
					return true;
				}

				public IWorkingSet[] getSelectedWorkingSets() {
					return null;
				}

				public void setSelectedWorkingSets(IWorkingSet[] workingSets) {
				}
			};
		}
		
		public void updateDefaults(String startingUrl, String maxHits) throws UnsupportedEncodingException{
//			String serverName = startingUrl.substring(0, startingUrl.indexOf("?"));
			startingUrl = startingUrl.substring(startingUrl.indexOf("?") + 1);
			String[] options = startingUrl.split("&");
			for(String option: options){
				String key = option.substring(0, option.indexOf("="));
				String value = URLDecoder.decode(option.substring(option.indexOf("=") + 1), "UTF-8");
				if (key == null)
					continue;
				
				if (key.equals("short_desc")) {
					summaryPattern.setText(value);
				} else if(key.equals("short_desc_type")){
					if(value.equals("allwordssubstr"))
						value = "all words";
					else if(value.equals("anywordssubstr"))
						value = "any word";
					int index = 0;
					for(String item: summaryOperation.getItems()){
						if(item.compareTo(value) == 0)
							break;
						index++;
					}
					if(index < summaryOperation.getItemCount()){
						summaryOperation.select(index);
					}
				} else if(key.equals("product")){
					String [] sel = product.getSelection();
					List<String> selList = Arrays.asList(sel);
					selList = new ArrayList<String>(selList);
					selList.add(value);
					sel = new String[selList.size()];
					product.setSelection(selList.toArray(sel));
				} else if(key.equals("component")){
					String [] sel = component.getSelection();
					List<String> selList = Arrays.asList(sel);
					selList = new ArrayList<String>(selList);
					selList.add(value);
					sel = new String[selList.size()];
					component.setSelection(selList.toArray(sel));
				} else if(key.equals("version")){
					String [] sel = version.getSelection();
					List<String> selList = Arrays.asList(sel);
					selList = new ArrayList<String>(selList);
					selList.add(value);
					sel = new String[selList.size()];
					version.setSelection(selList.toArray(sel));
				} else if(key.equals("target_milestone")){ //XXX
					String [] sel = target.getSelection();
					List<String> selList = Arrays.asList(sel);
					selList = new ArrayList<String>(selList);
					selList.add(value);
					sel = new String[selList.size()];
					target.setSelection(selList.toArray(sel));
				} else if(key.equals("version")){
					String [] sel = version.getSelection();
					List<String> selList = Arrays.asList(sel);
					selList = new ArrayList<String>(selList);
					selList.add(value);
					sel = new String[selList.size()];
					version.setSelection(selList.toArray(sel));
				} else if(key.equals("long_desc_type")){
					if(value.equals("allwordssubstr"))
						value = "all words";
					else if(value.equals("anywordssubstr"))
						value = "any word";
					int index = 0;
					for(String item: commentOperation.getItems()){
						if(item.compareTo(value) == 0)
							break;
						index++;
					}
					if(index < commentOperation.getItemCount()){
						commentOperation.select(index);
					}
				} else if(key.equals("long_desc")){
					commentPattern.setText(value);
				} else if(key.equals("bug_status")){
					String [] sel = status.getSelection();
					List<String> selList = Arrays.asList(sel);
					selList = new ArrayList<String>(selList);
					selList.add(value);
					sel = new String[selList.size()];
					status.setSelection(selList.toArray(sel));
				} else if(key.equals("resolution")){
					String [] sel = resolution.getSelection();
					List<String> selList = Arrays.asList(sel);
					selList = new ArrayList<String>(selList);
					selList.add(value);
					sel = new String[selList.size()];
					resolution.setSelection(selList.toArray(sel));
				} else if(key.equals("bug_severity")){
					String [] sel = severity.getSelection();
					List<String> selList = Arrays.asList(sel);
					selList = new ArrayList<String>(selList);
					selList.add(value);
					sel = new String[selList.size()];
					severity.setSelection(selList.toArray(sel));
				} else if(key.equals("priority")){
					String [] sel = priority.getSelection();
					List<String> selList = Arrays.asList(sel);
					selList = new ArrayList<String>(selList);
					selList.add(value);
					sel = new String[selList.size()];
					priority.setSelection(selList.toArray(sel));
				} else if(key.equals("ref_platform")){
					String [] sel = hardware.getSelection();
					List<String> selList = Arrays.asList(sel);
					selList = new ArrayList<String>(selList);
					selList.add(value);
					sel = new String[selList.size()];
					hardware.setSelection(selList.toArray(sel));
				} else if(key.equals("op_sys")){
					String [] sel = os.getSelection();
					List<String> selList = Arrays.asList(sel);
					selList = new ArrayList<String>(selList);
					selList.add(value);
					sel = new String[selList.size()];
					os.setSelection(selList.toArray(sel));
				} else if(key.equals("emailassigned_to1")){ // HACK: email buttons assumed to be in same position
					if(value.equals("1"))
						emailButton[0].setSelection(true);
					else
						emailButton[0].setSelection(false);
				} else if(key.equals("emailreporter1")){ // HACK: email buttons assumed to be in same position
					if(value.equals("1"))
						emailButton[1].setSelection(true);
					else
						emailButton[1].setSelection(false);
				} else if(key.equals("emailcc1") ){ // HACK: email buttons assumed to be in same position
					if(value.equals("1"))
						emailButton[2].setSelection(true);
					else
						emailButton[2].setSelection(false);
				} else if(key.equals("emaillongdesc1")){ // HACK: email buttons assumed to be in same position
					if(value.equals("1"))
						emailButton[3].setSelection(true);
					else
						emailButton[3].setSelection(false);
				} else if(key.equals("emailtype1")){
					int index = 0;
					for(String item: emailOperation.getItems()){
						if(item.compareTo(value) == 0)
							break;
						index++;
					}
					if(index < emailOperation.getItemCount()){
						emailOperation.select(index);
					}
				} else if(key.equals("email1")){
					emailPattern.setText(value);
				} else if(key.equals("changedin")){
					daysText.setText(value);
				}
			}
			this.maxHits = maxHits;
			maxHitsText.setText(maxHits);
		}
				
		
		public String getSearchURL() {
			try{
				if(rememberedQuery){
					return getQueryURL(new StringBuffer(input.getQueryParameters(selIndex)));
				} else {
					return getQueryURL(getQueryParameters());
				}
			} catch (UnsupportedEncodingException e){
				/*
				 * Do nothing. Every implementation of the Java platform is required
				 * to support the standard charset "UTF-8"
				 */
			}
			return "";
		}
		
		@Override
		protected Control createSaveQuery(Composite control) {
			GridLayout layout;
			GridData gd;

			Group group = new Group(control, SWT.NONE);
			layout = new GridLayout(3, false);
			group.setLayout(layout);
			group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			gd = new GridData(GridData.BEGINNING | GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
			gd.horizontalSpan = 2;
			group.setLayoutData(gd);
			
			loadButton = new Button(group, SWT.PUSH | SWT.LEFT);
			loadButton.setText("Saved Queries...");
			loadButton.addSelectionListener(new SelectionAdapter() {
				
				@Override
				public void widgetSelected(SelectionEvent event) {
					GetQueryDialog qd = new GetQueryDialog(getShell(),
							"Saved Queries", input){
						@Override
						protected void createButtonsForButtonBar(Composite parent) {
							super.createButtonsForButtonBar(parent);
							Button okButton = super.getButton(IDialogConstants.OK_ID);
							if(okButton != null)
								okButton.setText("Select");
						}
					};
					
					if (qd.open() == InputDialog.OK) {
						selIndex = qd.getSelected();
						if (selIndex != -1) {
							rememberedQuery = true;
						}
					} else {
						rememberedQuery = false;
					}
				}
			});
			loadButton.setEnabled(true);
			loadButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
			
			saveButton = new Button(group, SWT.PUSH | SWT.LEFT);
			saveButton.setText("Remember...");
			saveButton.addSelectionListener(new SelectionAdapter() {
				
				@Override
				public void widgetSelected(SelectionEvent event) {
					SaveQueryDialog qd = new SaveQueryDialog(getShell(),
							"Remember Query");
					if (qd.open() == InputDialog.OK) {
						String qName = qd.getText();
						if (qName != null && qName.compareTo("") != 0) {
							try {
								input.add(getQueryParameters().toString(), qName, summaryPattern.getText());
							}
							catch (UnsupportedEncodingException e) {
								/*
								 * Do nothing. Every implementation of the Java platform is required
								 * to support the standard charset "UTF-8"
								 */
							}
						}
					}
				}
			});
			saveButton.setEnabled(true);
			saveButton.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
			
			return group;
		}
	}
}

Back to the top