Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3f5db9169e03e712a6765f49d4049184956768c6 (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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
/*******************************************************************************
 * Copyright (c) 2003 - 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.search;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;

import javax.security.auth.login.LoginException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.DialogPage;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.mylar.bugzilla.core.BugzillaPlugin;
import org.eclipse.mylar.bugzilla.core.BugzillaRepositoryUtil;
import org.eclipse.mylar.bugzilla.core.IBugzillaConstants;
import org.eclipse.mylar.bugzilla.core.search.BugzillaSearchOperation;
import org.eclipse.mylar.bugzilla.core.search.BugzillaSearchQuery;
import org.eclipse.mylar.bugzilla.core.search.BugzillaSearchResultCollector;
import org.eclipse.mylar.bugzilla.core.search.IBugzillaSearchOperation;
import org.eclipse.mylar.bugzilla.core.search.IBugzillaSearchResultCollector;
import org.eclipse.mylar.bugzilla.ui.BugzillaUITools;
import org.eclipse.mylar.core.util.MylarStatusHandler;
import org.eclipse.mylar.internal.tasklist.MylarTaskListPlugin;
import org.eclipse.mylar.internal.tasklist.TaskRepositoryManager;
import org.eclipse.mylar.tasklist.TaskRepository;
import org.eclipse.search.ui.ISearchPage;
import org.eclipse.search.ui.ISearchPageContainer;
import org.eclipse.search.ui.NewSearchUI;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
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.Combo;
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.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.internal.help.WorkbenchHelpSystem;

/**
 * Bugzilla search page
 * 
 * @author Mik Kersten (hardening of prototype)
 */
public class BugzillaSearchPage extends DialogPage implements ISearchPage {

	private static final int HEIGHT_ATTRIBUTE_COMBO = 60;

	private TaskRepository repository = null;

	protected Combo summaryPattern = null;

	protected Combo repositoryCombo = null;

	private static ArrayList<BugzillaSearchData> previousSummaryPatterns = new ArrayList<BugzillaSearchData>(20);

	private static ArrayList<BugzillaSearchData> previousEmailPatterns = new ArrayList<BugzillaSearchData>(20);

	private static ArrayList<BugzillaSearchData> previousCommentPatterns = new ArrayList<BugzillaSearchData>(20);

	protected ISearchPageContainer scontainer = null;

	private boolean firstTime = true;

	private IDialogSettings fDialogSettings;

	protected Text maxHitsText;

	private static final String[] patternOperationText = { "all words", "any word", "regexp" };

	private static final String[] patternOperationValues = { "allwordssubstr", "anywordssubstr", "regexp" };

	private static final String[] emailOperationText = { "substring", "exact", "regexp" };

	private static final String[] emailOperationValues = { "substring", "exact", "regexp" };

	private static final String[] emailRoleValues = { "emailassigned_to1", "emailreporter1", "emailcc1",
			"emaillongdesc1" };

	protected IPreferenceStore prefs = BugzillaPlugin.getDefault().getPreferenceStore();

//	private TaskRepository selectedRepository = null;

	private static class BugzillaSearchData {
		/** Pattern to match on */
		String pattern;

		/** Pattern matching criterion */
		int operation;

		BugzillaSearchData(String pattern, int operation) {
			this.pattern = pattern;
			this.operation = operation;
		}
	}

	public BugzillaSearchPage() {
		super();
	}

	public BugzillaSearchPage(TaskRepository repository) {
		super();
		this.repository = repository;		
	}

	public void createControl(Composite parent) {
		readConfiguration();

		Composite control = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout(2, false);
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		control.setLayout(layout);
		GridData gd = new GridData(GridData.FILL_BOTH);
		control.setLayoutData(gd);

		createRepositoryGroup(control);
		createSearchGroup(control);
		createOptionsGroup(control);

		createEmail(control);
		createLastDays(control);
		// createSaveQuery(control);
		// createMaxHits(control);
		input = new SavedQueryFile(BugzillaPlugin.getDefault().getStateLocation().toString(), "/queries");
		// createUpdate(control);

		setControl(control);
		WorkbenchHelpSystem.getInstance().setHelp(control, IBugzillaConstants.SEARCH_PAGE_CONTEXT);
	}

	private void createRepositoryGroup(Composite control) {
		Group group = new Group(control, SWT.NONE);
		group.setText("Select Repository");
		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		group.setLayout(layout);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		group.setLayoutData(gd);

		repositoryCombo = new Combo(group, SWT.SINGLE | SWT.BORDER);
		repositoryCombo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String repositoryUrl = repositoryCombo.getItem(repositoryCombo.getSelectionIndex());
				repository = MylarTaskListPlugin.getRepositoryManager().getRepository(BugzillaPlugin.REPOSITORY_KIND,
						repositoryUrl);
				updateAttributesFromRepository(repositoryUrl, false);
			}
		});
		gd = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
		repositoryCombo.setLayoutData(gd);
	}

	private void createSearchGroup(Composite control) {
		Group group = new Group(control, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		group.setLayout(layout);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 5;
		group.setLayoutData(gd);

		createTextSearchComposite(group);
		createComment(group);
	}

	protected Control createTextSearchComposite(Composite control) {
		GridData gd;
		Label label;

		Composite group = new Composite(control, SWT.NONE);
		GridLayout 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);

		// Info text
		label = new Label(group, SWT.LEFT);
		label.setText("Summary contains: ");
		gd = new GridData(GridData.BEGINNING);
		gd.horizontalSpan = 1;
		label.setLayoutData(gd);

		// Pattern combo
		summaryPattern = new Combo(group, SWT.SINGLE | SWT.BORDER);
		summaryPattern.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				scontainer.setPerformActionEnabled(canQuery());
			}
		});
		summaryPattern.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				handleWidgetSelected(summaryPattern, summaryOperation, previousSummaryPatterns);
			}
		});
		gd = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
		summaryPattern.setLayoutData(gd);

		summaryOperation = new Combo(group, SWT.SINGLE | SWT.READ_ONLY | SWT.BORDER);
		summaryOperation.setItems(patternOperationText);
		summaryOperation.setText(patternOperationText[0]);
		summaryOperation.select(0);

		return group;
	}

	private Control createComment(Composite control) {
		GridData gd;
		Label label;

		Composite group = new Composite(control, SWT.NONE);
		GridLayout 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);

		// Info text
		label = new Label(group, SWT.LEFT);
		label.setText("Comment contains: ");
		gd = new GridData(GridData.BEGINNING);
		label.setLayoutData(gd);

		// Comment pattern combo
		commentPattern = new Combo(group, SWT.SINGLE | SWT.BORDER);
		commentPattern.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				scontainer.setPerformActionEnabled(canQuery());
			}
		});
		commentPattern.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				handleWidgetSelected(commentPattern, commentOperation, previousCommentPatterns);
			}
		});
		gd = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
		commentPattern.setLayoutData(gd);

		commentOperation = new Combo(group, SWT.SINGLE | SWT.READ_ONLY | SWT.BORDER);
		commentOperation.setItems(patternOperationText);
		commentOperation.setText(patternOperationText[0]);
		commentOperation.select(0);

		return group;
	}

	protected Control createOptionsGroup(Composite control) {
		Group group = new Group(control, SWT.NONE);
		// group.setText("Bug Attributes");
		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		group.setLayout(layout);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 5;
		group.setLayoutData(gd);

		createProductAttributes(group);
		createLists(group);
		createUpdate(group);
		return group;
	}

	/**
	 * Creates the area for selection on product/component/version.
	 */
	protected Control createProductAttributes(Composite control) {

		GridData gd;
		GridLayout layout;

		// Search expression
		Composite group = new Composite(control, SWT.NONE);
		layout = new GridLayout();
		layout.numColumns = 4;
		group.setLayout(layout);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 5;
		group.setLayoutData(gd);

		// Labels
		Label label = new Label(group, SWT.LEFT);
		label.setText("Product");

		label = new Label(group, SWT.LEFT);
		label.setText("Component");

		label = new Label(group, SWT.LEFT);
		label.setText("Version");

		label = new Label(group, SWT.LEFT);
		label.setText("Milestone");

		// Lists
		product = new List(group, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.heightHint = HEIGHT_ATTRIBUTE_COMBO;
		product.setLayoutData(gd);

		component = new List(group, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.heightHint = HEIGHT_ATTRIBUTE_COMBO;
		component.setLayoutData(gd);

		version = new List(group, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.heightHint = HEIGHT_ATTRIBUTE_COMBO;
		version.setLayoutData(gd);

		target = new List(group, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.heightHint = HEIGHT_ATTRIBUTE_COMBO;
		target.setLayoutData(gd);

		return group;
	}

	/**
	 * Creates the area for selection of bug attributes (status, etc.)
	 */
	protected Control createLists(Composite control) {
		GridData gd;
		GridLayout layout;

		// Search expression
		Composite group = new Composite(control, SWT.NONE);
		layout = new GridLayout();
		layout.numColumns = 6;
		group.setLayout(layout);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 5;
		group.setLayoutData(gd);

		// Labels
		Label label = new Label(group, SWT.LEFT);
		label.setText("Status");

		label = new Label(group, SWT.LEFT);
		label.setText("Resolution");

		label = new Label(group, SWT.LEFT);
		label.setText("Severity");

		label = new Label(group, SWT.LEFT);
		label.setText("Priority");

		label = new Label(group, SWT.LEFT);
		label.setText("Hardware");

		label = new Label(group, SWT.LEFT);
		label.setText("OS");

		// Lists
		status = new List(group, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.heightHint = 40;
		status.setLayoutData(gd);

		resolution = new List(group, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.heightHint = 40;
		resolution.setLayoutData(gd);

		severity = new List(group, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.heightHint = 40;
		severity.setLayoutData(gd);

		priority = new List(group, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.heightHint = 40;
		priority.setLayoutData(gd);

		hardware = new List(group, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.heightHint = 40;
		hardware.setLayoutData(gd);

		os = new List(group, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.heightHint = 40;
		os.setLayoutData(gd);

		return group;
	}

	protected Text daysText;

	protected Control createLastDays(Composite control) {
		GridLayout layout;
		GridData gd;

		Group group = new Group(control, SWT.NONE);
		layout = new GridLayout(6, 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);

		Label label = new Label(group, SWT.LEFT);
		label.setText("Only bugs changed in the last ");

		// operation combo
		daysText = new Text(group, SWT.BORDER);
		daysText.setTextLimit(5);
		GridData daysLayoutData = new GridData();
		daysLayoutData.widthHint = 30;
		daysText.setLayoutData(daysLayoutData);
		daysText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				String days = daysText.getText();
				if (days.length() == 0)
					return;
				for (int i = days.length() - 1; i >= 0; i--) {
					try {
						if (days.equals("") || Integer.parseInt(days) > -1) {
							if (i == days.length() - 1)
								return;
							else
								break;
						}
					} catch (NumberFormatException ex) {
						days = days.substring(0, i);
					}
				}
				daysText.setText(days);
			}
		});
		label = new Label(group, SWT.LEFT);
		label.setText(" days.");

		label = new Label(group, SWT.LEFT);
		label.setText("  Show a maximum of ");

		// operation combo
		maxHitsText = new Text(group, SWT.BORDER);
		maxHitsText.setTextLimit(6);
		maxHitsText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				String maxHitss = maxHitsText.getText();
				if (maxHitss.length() == 0)
					return;
				for (int i = maxHitss.length() - 1; i >= 0; i--) {
					try {
						if (maxHitss.equals("") || Integer.parseInt(maxHitss) > -1) {
							if (i == maxHitss.length() - 1) {
								maxHits = maxHitss;
								return;
							} else {
								break;
							}
						}
					} catch (NumberFormatException ex) {
						maxHitss = maxHitss.substring(0, i);
					}
				}

				BugzillaSearchPage.this.maxHits = maxHitss;
			}
		});
		gd = new GridData();
		gd.widthHint = 30;
		maxHitsText.setLayoutData(gd);
		label = new Label(group, SWT.LEFT);
		label.setText(" hits.");

		maxHits = "100";
		maxHitsText.setText(maxHits);

		return group;
	}

	// protected Control createMaxHits(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);
	//
	// Label label = new Label(group, SWT.LEFT);
	// label.setText("Show a maximum of ");
	//
	// // operation combo
	// maxHitsText = new Text(group, SWT.BORDER);
	// maxHitsText.setTextLimit(5);
	// maxHitsText.addModifyListener(new ModifyListener() {
	// public void modifyText(ModifyEvent e) {
	// String maxHitss = maxHitsText.getText();
	// if (maxHitss.length() == 0)
	// return;
	// for (int i = maxHitss.length() - 1; i >= 0; i--) {
	// try {
	// if (maxHitss.equals("") || Integer.parseInt(maxHitss) > -1) {
	// if (i == maxHitss.length() - 1) {
	// maxHits = maxHitss;
	// return;
	// } else {
	// break;
	// }
	// }
	// } catch (NumberFormatException ex) {
	// maxHitss = maxHitss.substring(0, i);
	// }
	// }
	//
	// BugzillaSearchPage.this.maxHits = maxHitss;
	// }
	// });
	// gd = new GridData();
	// gd.widthHint = 20;
	// maxHitsText.setLayoutData(gd);
	// label = new Label(group, SWT.LEFT);
	// label.setText(" Hits. (-1 means all hits are returned)");
	//
	// maxHits = "100";
	// maxHitsText.setText(maxHits);
	//
	// return group;
	// }

	protected String maxHits;

	public String getMaxHits() {
		return maxHits;
	}

	private static final String[] emailText = { "bug owner", "reporter", "CC list", "commenter" };

	protected Control createEmail(Composite control) {
		GridLayout layout;
		GridData gd;

		Group group = new Group(control, SWT.NONE);
		layout = new GridLayout(7, 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);

		Label label = new Label(group, SWT.LEFT);
		label.setText("Email: ");

		// pattern combo
		emailPattern = new Combo(group, SWT.SINGLE | SWT.BORDER);
		emailPattern.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				scontainer.setPerformActionEnabled(canQuery());
			}
		});
		emailPattern.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				handleWidgetSelected(emailPattern, emailOperation, previousEmailPatterns);
			}
		});
		gd = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
		emailPattern.setLayoutData(gd);

		// operation combo
		emailOperation = new Combo(group, SWT.SINGLE | SWT.READ_ONLY | SWT.BORDER);
		emailOperation.setItems(emailOperationText);
		emailOperation.setText(emailOperationText[0]);
		emailOperation.select(0);

		// Composite buttons = new Composite(group, SWT.NONE);
		// layout = new GridLayout(4, false);
		// buttons.setLayout(layout);
		// buttons.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		// gd = new GridData(GridData.BEGINNING);
		// gd.horizontalSpan = 3;
		// buttons.setLayoutData(gd);

		emailButton = new Button[emailText.length];
		for (int i = 0; i < emailButton.length; i++) {
			Button button = new Button(group, SWT.CHECK);
			button.setText(emailText[i]);
			emailButton[i] = button;
		}

		return group;
	}

	/**
	 * Creates the buttons for remembering a query and accessing previously
	 * saved queries.
	 */
	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...");
		// final BugzillaSearchPage bsp = this;
		// loadButton.addSelectionListener(new SelectionAdapter() {
		//
		// @Override
		// public void widgetSelected(SelectionEvent event) {
		// GetQueryDialog qd = new GetQueryDialog(getShell(), "Saved Queries",
		// input);
		// if (qd.open() == InputDialog.OK) {
		// selIndex = qd.getSelected();
		// if (selIndex != -1) {
		// rememberedQuery = true;
		// performAction();
		// bsp.getShell().close();
		// }
		// }
		// }
		// });
		// 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;
	}

	public static SavedQueryFile getInput() {
		return input;
	}

	protected Control createUpdate(final Composite control) {
		GridData gd;
		// Label label;

		Composite group = new Composite(control, SWT.NONE);
		GridLayout layout = new GridLayout(2, false);
		group.setLayout(layout);
		group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		gd = new GridData(GridData.BEGINNING);
		gd.horizontalSpan = 2;
		group.setLayoutData(gd);

		// Info text
		// label = new Label(group, SWT.LEFT);
		// label.setText("Update search options from server:");
		// gd = new GridData(GridData.BEGINNING);
		// label.setLayoutData(gd);

		updateButton = new Button(group, SWT.LEFT | SWT.PUSH);
		updateButton.setText("Update Attributes from Repository");

		updateButton.setLayoutData(new GridData());

		updateButton.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseUp(MouseEvent e) {
				if (repository != null) {
					updateAttributesFromRepository(repository.getUrl().toExternalForm(), true);
				} else {
					MessageDialog.openInformation(Display.getCurrent().getActiveShell(),
							IBugzillaConstants.TITLE_MESSAGE_DIALOG, TaskRepositoryManager.MESSAGE_NO_REPOSITORY);
				}
			}
		});

		return group;
	}

	private void handleWidgetSelected(Combo widget, Combo operation, ArrayList<BugzillaSearchData> history) {
		if (widget.getSelectionIndex() < 0)
			return;
		int index = history.size() - 1 - widget.getSelectionIndex();
		BugzillaSearchData patternData = history.get(index);
		if (patternData == null || !widget.getText().equals(patternData.pattern))
			return;
		widget.setText(patternData.pattern);
		operation.setText(operation.getItem(patternData.operation));
	}

	public boolean performAction() {
		if (repository == null) {
			MessageDialog.openInformation(Display.getCurrent().getActiveShell(),
					IBugzillaConstants.TITLE_MESSAGE_DIALOG, TaskRepositoryManager.MESSAGE_NO_REPOSITORY);
			return false;
		}

		getPatternData(summaryPattern, summaryOperation, previousSummaryPatterns);
		getPatternData(commentPattern, commentOperation, previousCommentPatterns);
		getPatternData(this.emailPattern, emailOperation, previousEmailPatterns);

		String summaryText;
		String queryUrl;
		if (rememberedQuery == true) {
			queryUrl = getQueryURL(repository, new StringBuffer(input.getQueryParameters(selIndex)));
			summaryText = input.getSummaryText(selIndex);
		} else {
			try {
				StringBuffer params = getQueryParameters();
				queryUrl = getQueryURL(repository, params);
				summaryText = summaryPattern.getText();
			} catch (UnsupportedEncodingException e) {
				/*
				 * These statements should never be executed. Every
				 * implementation of the Java platform is required to support
				 * the standard charset "UTF-8"
				 */
				queryUrl = "";
				summaryText = "";
			}
		}

		try {
			// if the summary contains a single bug id, open the bug directly
			int id = Integer.parseInt(summaryText);
			return BugzillaUITools.show(repository.getUrl().toExternalForm(), id);
		} catch (NumberFormatException ignored) {
			// ignore this since this means that the text is not a bug id
		}

		// Don't activate the search result view until it is known that the
		// user is not opening a bug directly -- there is no need to open
		// the view if no searching is going to take place.
		NewSearchUI.activateSearchResultView();

		BugzillaPlugin.getDefault().getPreferenceStore().setValue(IBugzillaConstants.MOST_RECENT_QUERY, summaryText);

		IBugzillaSearchResultCollector collector = new BugzillaSearchResultCollector();

		IBugzillaSearchOperation op = new BugzillaSearchOperation(repository, queryUrl, collector, maxHits);

		BugzillaSearchQuery searchQuery = new BugzillaSearchQuery(op);
		NewSearchUI.runQueryInBackground(searchQuery);

		return true;
	}

	/**
	 * @see ISearchPage#setContainer(ISearchPageContainer)
	 */
	public void setContainer(ISearchPageContainer container) {
		scontainer = container;
	}

	@Override
	public void setVisible(boolean visible) {
		if (visible && summaryPattern != null) {
			if (firstTime) {
				firstTime = false;
				// Set item and text here to prevent page from resizing
				summaryPattern.setItems(getPreviousPatterns(previousSummaryPatterns));
				commentPattern.setItems(getPreviousPatterns(previousCommentPatterns));
				emailPattern.setItems(getPreviousPatterns(previousEmailPatterns));

				if (repository == null) {
					repository = MylarTaskListPlugin.getRepositoryManager().getDefaultRepository(
							BugzillaPlugin.REPOSITORY_KIND);
				}
				Set<TaskRepository> repositories = MylarTaskListPlugin.getRepositoryManager().getRepositories(
						BugzillaPlugin.REPOSITORY_KIND);
				String[] repositoryUrls = new String[repositories.size()];
				int i = 0;
				int indexToSelect = 0;
				for (Iterator<TaskRepository> iter = repositories.iterator(); iter.hasNext();) {
					TaskRepository currRepsitory = iter.next();
					// if (i == 0 && repository == null) {
					// repository = currRepsitory;
					// indexToSelect = 0;
					// }
					if (repository != null && repository.equals(currRepsitory)) {
						indexToSelect = i;
					}
					repositoryUrls[i] = currRepsitory.getUrl().toExternalForm();
					i++;
				}
				if (repositoryCombo != null) {
					repositoryCombo.setItems(repositoryUrls);
					if (repositoryUrls.length == 0) {
						MessageDialog.openInformation(Display.getCurrent().getActiveShell(),
								IBugzillaConstants.TITLE_MESSAGE_DIALOG, TaskRepositoryManager.MESSAGE_NO_REPOSITORY);
					} else {
						repositoryCombo.select(indexToSelect);
						updateAttributesFromRepository(repositoryCombo.getItem(indexToSelect), false);
					}
				}
			}
			summaryPattern.setFocus();
			scontainer.setPerformActionEnabled(canQuery());
		}
		super.setVisible(visible);
	}

	/**
	 * Returns <code>true</code> if at least some parameter is given to query
	 * on.
	 */
	private boolean canQuery() {
		return product.getSelectionCount() > 0 || component.getSelectionCount() > 0 || version.getSelectionCount() > 0
				|| target.getSelectionCount() > 0 || status.getSelectionCount() > 0
				|| resolution.getSelectionCount() > 0 || severity.getSelectionCount() > 0
				|| priority.getSelectionCount() > 0 || hardware.getSelectionCount() > 0 || os.getSelectionCount() > 0
				|| summaryPattern.getText().length() > 0 || commentPattern.getText().length() > 0
				|| emailPattern.getText().length() > 0;
	}

	/**
	 * Return search pattern data and update search history list. An existing
	 * entry will be updated or a new one created.
	 */
	private BugzillaSearchData getPatternData(Combo widget, Combo operation,
			ArrayList<BugzillaSearchData> previousSearchQueryData) {
		String pattern = widget.getText();
		if (pattern == null || pattern.trim().equals("")) {
			return null;
		}
		BugzillaSearchData match = null;
		int i = previousSearchQueryData.size() - 1;
		while (i >= 0) {
			match = previousSearchQueryData.get(i);
			if (pattern.equals(match.pattern)) {
				break;
			}
			i--;
		}
		if (i >= 0) {
			match.operation = operation.getSelectionIndex();
			// remove - will be added last (see below)
			previousSearchQueryData.remove(match);
		} else {
			match = new BugzillaSearchData(widget.getText(), operation.getSelectionIndex());
		}
		previousSearchQueryData.add(match);
		return match;
	}

	/**
	 * Returns an array of previous summary patterns
	 */
	private String[] getPreviousPatterns(ArrayList<BugzillaSearchData> patternHistory) {
		int size = patternHistory.size();
		String[] patterns = new String[size];
		for (int i = 0; i < size; i++)
			patterns[i] = (patternHistory.get(size - 1 - i)).pattern;
		return patterns;
	}

	protected String getQueryURL(TaskRepository repository, StringBuffer params) {
		StringBuffer url = new StringBuffer(getQueryURLStart(repository).toString());
		url.append(params);

		// HACK make sure that the searches come back sorted by priority. This
		// should be a search opetion though
		url.append("&order=Importance");
		return url.toString();
	}

	/**
	 * Creates the bugzilla query URL start.
	 * 
	 * Example: https://bugs.eclipse.org/bugs/buglist.cgi?
	 */
	private StringBuffer getQueryURLStart(TaskRepository repository) {
		// StringBuffer sb = new
		// StringBuffer(BugzillaPlugin.getDefault().getServerName());
		StringBuffer sb = new StringBuffer(repository.getUrl().toExternalForm());

		if (sb.charAt(sb.length() - 1) != '/') {
			sb.append('/');
		}
		sb.append("buglist.cgi?");

		// use the username and password if we have it
		if (repository.hasCredentials()) {
			try {
				sb.append("GoAheadAndLogIn=1&Bugzilla_login="
						+ URLEncoder.encode(repository.getUserName(), BugzillaPlugin.ENCODING_UTF_8)
						+ "&Bugzilla_password="
						+ URLEncoder.encode(repository.getPassword(), BugzillaPlugin.ENCODING_UTF_8) + "&");
			} catch (UnsupportedEncodingException e) {
				MylarStatusHandler.fail(e, "unsupported encoding", false);
			}
		}

		return sb;
	}

	/**
	 * Goes through the query form and builds up the query parameters.
	 * 
	 * Example: short_desc_type=substring&amp;short_desc=bla&amp; ...
	 * 
	 * @throws UnsupportedEncodingException
	 */
	protected StringBuffer getQueryParameters() throws UnsupportedEncodingException {
		StringBuffer sb = new StringBuffer();

		sb.append("short_desc_type=");
		sb.append(patternOperationValues[summaryOperation.getSelectionIndex()]);

		sb.append("&short_desc=");
		sb.append(URLEncoder.encode(summaryPattern.getText(), "UTF-8"));

		int[] selected = product.getSelectionIndices();
		for (int i = 0; i < selected.length; i++) {
			sb.append("&product=");
			sb.append(URLEncoder.encode(product.getItem(selected[i]), "UTF-8"));
		}

		selected = component.getSelectionIndices();
		for (int i = 0; i < selected.length; i++) {
			sb.append("&component=");
			sb.append(URLEncoder.encode(component.getItem(selected[i]), "UTF-8"));
		}

		selected = version.getSelectionIndices();
		for (int i = 0; i < selected.length; i++) {
			sb.append("&version=");
			sb.append(URLEncoder.encode(version.getItem(selected[i]), "UTF-8"));
		}

		selected = target.getSelectionIndices();
		for (int i = 0; i < selected.length; i++) {
			sb.append("&target_milestone=");
			sb.append(URLEncoder.encode(target.getItem(selected[i]), "UTF-8"));
		}

		sb.append("&long_desc_type=");
		sb.append(patternOperationValues[commentOperation.getSelectionIndex()]);
		sb.append("&long_desc=");
		sb.append(URLEncoder.encode(commentPattern.getText(), "UTF-8"));

		selected = status.getSelectionIndices();
		for (int i = 0; i < selected.length; i++) {
			sb.append("&bug_status=");
			sb.append(status.getItem(selected[i]));
		}

		selected = resolution.getSelectionIndices();
		for (int i = 0; i < selected.length; i++) {
			sb.append("&resolution=");
			sb.append(resolution.getItem(selected[i]));
		}

		selected = severity.getSelectionIndices();
		for (int i = 0; i < selected.length; i++) {
			sb.append("&bug_severity=");
			sb.append(severity.getItem(selected[i]));
		}

		selected = priority.getSelectionIndices();
		for (int i = 0; i < selected.length; i++) {
			sb.append("&priority=");
			sb.append(priority.getItem(selected[i]));
		}

		selected = hardware.getSelectionIndices();
		for (int i = 0; i < selected.length; i++) {
			sb.append("&ref_platform=");
			sb.append(URLEncoder.encode(hardware.getItem(selected[i]), "UTF-8"));
		}

		selected = os.getSelectionIndices();
		for (int i = 0; i < selected.length; i++) {
			sb.append("&op_sys=");
			sb.append(URLEncoder.encode(os.getItem(selected[i]), "UTF-8"));
		}

		if (emailPattern.getText() != null) {
			for (int i = 0; i < emailButton.length; i++) {
				if (emailButton[i].getSelection()) {
					sb.append("&");
					sb.append(emailRoleValues[i]);
					sb.append("=1");
				}
			}
			sb.append("&emailtype1=");
			sb.append(emailOperationValues[emailOperation.getSelectionIndex()]);
			sb.append("&email1=");
			sb.append(URLEncoder.encode(emailPattern.getText(), "UTF-8"));
		}

		if (daysText.getText() != null && !daysText.getText().equals("")) {
			try {
				Integer.parseInt(daysText.getText());
				sb.append("&changedin=");
				sb.append(URLEncoder.encode(daysText.getText(), "UTF-8"));
			} catch (NumberFormatException ignored) {
				// this means that the days is not a number, so don't worry
			}
		}

		return sb;
	}

	// --------------- Configuration handling --------------

	// Dialog store id constants
	protected final static String PAGE_NAME = "BugzillaSearchPage"; //$NON-NLS-1$

	protected Combo summaryOperation;

	protected List product;

	protected List os;

	protected List hardware;

	protected List priority;

	protected List severity;

	protected List resolution;

	protected List status;

	protected Combo commentOperation;

	protected Combo commentPattern;

	protected List component;

	protected List version;

	protected List target;

	protected Combo emailOperation;

	protected Combo emailPattern;

	protected Button[] emailButton;

	/** File containing saved queries */
	protected static SavedQueryFile input;

	// /** "Remember query" button */
	// protected Button saveButton;

	// /** "Saved queries..." button */
	// protected Button loadButton;

	/** Run a remembered query */
	protected boolean rememberedQuery = false;

	/** Index of the saved query to run */
	protected int selIndex;

	protected Button updateButton;

	protected ProgressMonitorDialog monitorDialog = new ProgressMonitorDialog(BugzillaPlugin.getDefault()
			.getWorkbench().getActiveWorkbenchWindow().getShell());

	/**
	 * Returns the page settings for this Java search page.
	 * 
	 * @return the page settings to be used
	 */
	private IDialogSettings getDialogSettings() {
		IDialogSettings settings = BugzillaPlugin.getDefault().getDialogSettings();
		fDialogSettings = settings.getSection(PAGE_NAME);
		if (fDialogSettings == null)
			fDialogSettings = settings.addNewSection(PAGE_NAME);
		return fDialogSettings;
	}

	/**
	 * Initializes itself from the stored page settings.
	 */
	private void readConfiguration() {
		getDialogSettings();
	}

	private void updateAttributesFromRepository(String repositoryUrl, boolean connect) {
		monitorDialog.open();
		IProgressMonitor monitor = monitorDialog.getProgressMonitor();
		monitor.beginTask("Updating search options...", 55);

		try {
			// TaskRepository repository =
			// MylarTaskListPlugin.getRepositoryManager().getDefaultRepository(
			// BugzillaPlugin.REPOSITORY_KIND);
			// String repositoryUrl = repository.getUrl().toExternalForm();
			if (connect) {
				BugzillaRepositoryUtil.updateQueryOptions(repository, monitor);
			}
			product.setItems(BugzillaRepositoryUtil.getQueryOptions(IBugzillaConstants.VALUES_PRODUCT, repositoryUrl));
			monitor.worked(1);

			component.setItems(BugzillaRepositoryUtil.getQueryOptions(IBugzillaConstants.VALUES_COMPONENT,
					repositoryUrl));
			monitor.worked(1);

			version.setItems(BugzillaRepositoryUtil.getQueryOptions(IBugzillaConstants.VALUES_VERSION, repositoryUrl));
			monitor.worked(1);

			target.setItems(BugzillaRepositoryUtil.getQueryOptions(IBugzillaConstants.VALUES_TARGET, repositoryUrl));
			monitor.worked(1);

			status.setItems(BugzillaRepositoryUtil.getQueryOptions(IBugzillaConstants.VALUES_STATUS, repositoryUrl));
			monitor.worked(1);

			status.setSelection(BugzillaRepositoryUtil.getQueryOptions(IBugzillaConstants.VALUSE_STATUS_PRESELECTED,
					repositoryUrl));
			monitor.worked(1);

			resolution.setItems(BugzillaRepositoryUtil.getQueryOptions(IBugzillaConstants.VALUES_RESOLUTION,
					repositoryUrl));
			monitor.worked(1);

			severity
					.setItems(BugzillaRepositoryUtil.getQueryOptions(IBugzillaConstants.VALUES_SEVERITY, repositoryUrl));
			monitor.worked(1);

			priority
					.setItems(BugzillaRepositoryUtil.getQueryOptions(IBugzillaConstants.VALUES_PRIORITY, repositoryUrl));
			monitor.worked(1);

			hardware
					.setItems(BugzillaRepositoryUtil.getQueryOptions(IBugzillaConstants.VALUES_HARDWARE, repositoryUrl));
			monitor.worked(1);

			os.setItems(BugzillaRepositoryUtil.getQueryOptions(IBugzillaConstants.VALUES_OS, repositoryUrl));
			monitor.worked(1);
		} catch (LoginException exception) {
			// we had a problem that seems to have been caused from bad
			// login info
			MessageDialog
					.openError(
							null,
							"Login Error",
							"Bugzilla could not log you in to get the information you requested since login name or password is incorrect.\nPlease check your settings in the bugzilla preferences. ");
			BugzillaPlugin.log(exception);
		} finally {
			monitor.done();
			monitorDialog.close();
		}
	}

	public TaskRepository getRepository() {
		return repository;
	}

	public void setRepository(TaskRepository repository) {
		this.repository = repository;
	}
}

Back to the top