Skip to main content
summaryrefslogtreecommitdiffstats
blob: 25203dc2b55136294429db3dc7096baaac96b009 (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
/*******************************************************************************
 * 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.editor;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.StringTokenizer;

import javax.security.auth.login.LoginException;

import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.CompareUI;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.JFaceColors;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.mylar.bugzilla.core.Attribute;
import org.eclipse.mylar.bugzilla.core.BugPost;
import org.eclipse.mylar.bugzilla.core.BugReport;
import org.eclipse.mylar.bugzilla.core.BugzillaException;
import org.eclipse.mylar.bugzilla.core.BugzillaPlugin;
import org.eclipse.mylar.bugzilla.core.BugzillaRepository;
import org.eclipse.mylar.bugzilla.core.Comment;
import org.eclipse.mylar.bugzilla.core.IBugzillaBug;
import org.eclipse.mylar.bugzilla.core.IBugzillaConstants;
import org.eclipse.mylar.bugzilla.core.Operation;
import org.eclipse.mylar.bugzilla.core.PossibleBugzillaFailureException;
import org.eclipse.mylar.bugzilla.core.compare.BugzillaCompareInput;
import org.eclipse.mylar.bugzilla.core.internal.HtmlStreamTokenizer;
import org.eclipse.mylar.bugzilla.ui.OfflineView;
import org.eclipse.mylar.bugzilla.ui.WebBrowserDialog;
import org.eclipse.mylar.bugzilla.ui.actions.RefreshBugzillaReportsAction;
import org.eclipse.mylar.bugzilla.ui.outline.BugzillaOutlineNode;
import org.eclipse.mylar.bugzilla.ui.outline.BugzillaReportSelection;
import org.eclipse.mylar.core.MylarPlugin;
import org.eclipse.mylar.tasklist.ui.views.TaskListView;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
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.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.ui.internal.Workbench;


/**
 * An editor used to view a bug report that exists on a server. It uses a
 * <code>BugReport</code> object to store the data.
 */
public class ExistingBugEditor extends AbstractBugEditor
{

	protected Set<String> removeCC = new HashSet<String>();
	protected BugzillaCompareInput compareInput;
	protected Button compareButton;
	
	protected Button[] radios;
	protected Control[] radioOptions;
	protected List keyWordsList;
	protected Text keywordsText;
	protected List ccList;
	protected Text ccText;
	protected Text addCommentsText;
	protected BugReport bug;

    public String getNewCommentText(){
        return addCommentsTextBox.getText();
    }
    
	/**
	 * Creates a new <code>ExistingBugEditor</code>.
	 */
	public ExistingBugEditor() {
		super();
		
		// Set up the input for comparing the bug report to the server
		CompareConfiguration config = new CompareConfiguration();
		config.setLeftEditable(false);
		config.setRightEditable(false);
		config.setLeftLabel("Local Bug Report");
		config.setRightLabel("Remote Bug Report");
		config.setLeftImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT));
		config.setRightImage(PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT));
		compareInput = new BugzillaCompareInput(config);
	}

	@SuppressWarnings("deprecation")
	@Override
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
		if (!(input instanceof ExistingBugEditorInput))
			throw new PartInitException("Invalid Input: Must be ExistingBugEditorInput");
		ExistingBugEditorInput ei = (ExistingBugEditorInput) input;
		setSite(site);
		setInput(input);
		bugzillaInput = ei;
		model = BugzillaOutlineNode.parseBugReport(bugzillaInput.getBug());
		bug = ei.getBug();
		restoreBug();
		isDirty = false;
		updateEditorTitle();
	}

	/**
	 * This overrides the existing implementation in order to add
	 * an "add to favorites" option to the context menu.
	 *  
	 * @see org.eclipse.mylar.bugzilla.ui.AbstractBugEditor#createContextMenu()
	 */
	@Override
	protected void createContextMenu() {
		contextMenuManager = new MenuManager("#BugEditor");
		contextMenuManager.setRemoveAllWhenShown(true);
		contextMenuManager.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
//				manager.add(new AddToFavoritesAction(ExistingBugEditor.this));
//				manager.add(new Separator());
				manager.add(cutAction);
				manager.add(copyAction);
				manager.add(pasteAction);
				manager.add(new Separator());
				manager.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
				if (currentSelectedText == null || 
					currentSelectedText.getSelectionText().length() == 0) {
				
					copyAction.setEnabled(false);
				}
				else {
					copyAction.setEnabled(true);
				}
			}
		});
		getSite().registerContextMenu("#BugEditor", contextMenuManager,
				getSite().getSelectionProvider());
	}

	@Override
	protected void addRadioButtons(Composite buttonComposite) {
		int i = 0;
		Button selected = null;
		radios = new Button[bug.getOperations().size()];
		radioOptions = new Control[bug.getOperations().size()];
		for (Iterator<Operation> it = bug.getOperations().iterator(); it.hasNext(); ) {
			Operation o = it.next();
			radios[i] = new Button(buttonComposite, SWT.RADIO);
			radios[i].setFont(TEXT_FONT);
			GridData radioData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
			if (!o.hasOptions() && !o.isInput())
				radioData.horizontalSpan = 4;
			else
				radioData.horizontalSpan = 3;
			radioData.heightHint = 20;
			String opName = o.getOperationName();
			opName = opName.replaceAll("</.*>", "");
			opName = opName.replaceAll("<.*>", "");
			radios[i].setText(opName);
			radios[i].setLayoutData(radioData);
			radios[i].setBackground(background);
			radios[i].addSelectionListener(new RadioButtonListener());
			radios[i].addListener(SWT.FocusIn, new GenericListener());
			
			if (o.hasOptions()) {
				radioData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
				radioData.horizontalSpan = 1;
				radioData.heightHint = 20;
				radioData.widthHint = AbstractBugEditor.WRAP_LENGTH;
				radioOptions[i] = new Combo(
						buttonComposite,
						SWT.NO_BACKGROUND
							| SWT.MULTI
							| SWT.V_SCROLL
							| SWT.READ_ONLY);
				radioOptions[i].setFont(TEXT_FONT);
				radioOptions[i].setLayoutData(radioData);
				radioOptions[i].setBackground(background);
				
				Object [] a = o.getOptionNames().toArray();
				Arrays.sort(a);
				for (int j = 0; j < a.length; j++) {
					((Combo)radioOptions[i]).add((String) a[j]);
				}
				((Combo)radioOptions[i]).select(0);
				((Combo)radioOptions[i]).addSelectionListener(new RadioButtonListener());
			} else if(o.isInput()){
				radioData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
				radioData.horizontalSpan = 1;
				radioData.widthHint = 120;
				radioOptions[i] = new Text(
						buttonComposite,
						SWT.BORDER | SWT.SINGLE);
				radioOptions[i].setFont(TEXT_FONT);
				radioOptions[i].setLayoutData(radioData);
				radioOptions[i].setBackground(background);
				((Text)radioOptions[i]).setText(o.getInputValue());
				((Text)radioOptions[i]).addModifyListener(new RadioButtonListener());
			}
			
			if (i == 0 || o.isChecked()) {
				if(selected != null)
					selected.setSelection(false);
				selected = radios[i];
				radios[i].setSelection(true);
				if(o.hasOptions() && o.getOptionSelection() != null){
					int j = 0;
					for(String s: ((Combo)radioOptions[i]).getItems()){
						if(s.compareTo(o.getOptionSelection()) == 0){
							((Combo)radioOptions[i]).select(j);
						}
						j++;
					}
				}
				bug.setSelectedOperation(o);
			}
		
			i++;
		}
	}

	@Override
	protected void addActionButtons(Composite buttonComposite) {
		super.addActionButtons(buttonComposite);

		compareButton = new Button(buttonComposite, SWT.NONE);
		compareButton.setFont(TEXT_FONT);
		GridData compareButtonData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		compareButtonData.widthHint = 100;
		compareButtonData.heightHint = 20;
		compareButton.setText("Compare");
		compareButton.setLayoutData(compareButtonData);
		compareButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				OpenCompareEditorJob compareJob = new OpenCompareEditorJob("Comparing bug with remote server...");
				compareJob.schedule();
			}
		});
		compareButton.addListener(SWT.FocusIn, new GenericListener());
		
//		TODO used for spell checking.  Add back when we want to support this
//		checkSpellingButton = new Button(buttonComposite, SWT.NONE);
//		checkSpellingButton.setFont(TEXT_FONT);
//		compareButtonData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
//		compareButtonData.widthHint = 100;
//		compareButtonData.heightHint = 20;
//		checkSpellingButton.setText("CheckSpelling");
//		checkSpellingButton.setLayoutData(compareButtonData);
//		checkSpellingButton.addListener(SWT.Selection, new Listener() {
//			public void handleEvent(Event e) {
//				checkSpelling();
//			}
//		});
//		checkSpellingButton.addListener(SWT.FocusIn, new GenericListener());
	}
	
	/**
	 * @return Returns the compareInput.
	 */
	public BugzillaCompareInput getCompareInput() {
		return compareInput;
	}
	
	@Override
	public IBugzillaBug getBug() {
		return bug;
	}

	@Override
	protected String getTitleString() {
		return bug.getLabel() + ": " + checkText(bug.getAttribute("Summary").getNewValue());
	}

	private String toCommaSeparatedList(String[] strings) {
		StringBuffer buffer = new StringBuffer();
		for(int i = 0; i < strings.length; i++) {
			buffer.append(strings[i]);
			if (i != strings.length - 1) {
				buffer.append(",");
			}
		}
		return buffer.toString();
	}
	
	@Override
	protected void submitBug() {
		submitButton.setEnabled(false);
		ExistingBugEditor.this.showBusy(true);
		final BugPost form = new BugPost();
		
		// set the url for the bug to be submitted to
		setURL(form, "process_bug.cgi");

		// go through all of the attributes and add them to the bug post
		for (Iterator<Attribute> it = bug.getAttributes().iterator(); it.hasNext(); ) {
			Attribute a = it.next();
			if (a != null && a.getParameterName() != null && a.getParameterName().compareTo("") != 0 && !a.isHidden()) {
				String value = a.getNewValue();
				
				// add the attribute to the bug post
				form.add(a.getParameterName(), checkText(value));
			}
			else if(a != null && a.getParameterName() != null && a.getParameterName().compareTo("") != 0 && a.isHidden()) {
				// we have a hidden attribute and we should send it back.
				form.add(a.getParameterName(), a.getValue());
			}
		}
		
		// make sure that the comment is broken up into 80 character lines
		bug.setNewNewComment(formatText(bug.getNewNewComment()));
				
		// add the summary to the bug post
		form.add("short_desc", bug.getAttribute("Summary").getNewValue());

		if(removeCC != null && removeCC.size() > 0){
			String[] s = new String[removeCC.size()];	
			form.add("cc", toCommaSeparatedList(removeCC.toArray(s)));
			form.add("removecc", "true");
		}
		
		// add the operation to the bug post
		Operation o = bug.getSelectedOperation();
		if (o == null)
			form.add("knob", "none");
		else {
			form.add("knob", o.getKnobName());
			if(o.hasOptions()) {
				String sel = o.getOptionValue(o.getOptionSelection());
				form.add(o.getOptionName(), sel);
			} else if (o.isInput()){
				String sel = o.getInputValue();
				form.add(o.getInputName(), sel);
			}
		}
		form.add("form_name", "process_bug");
		

		// add the new comment to the bug post if there is some text in it
		if(bug.getNewNewComment().length() != 0) {
			form.add("comment", bug.getNewNewComment());
		}
		
		final WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
			protected void execute(final IProgressMonitor monitor)
					throws CoreException {
				try {
					form.post();

					PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable(){
						public void run() {
							// TODO what do we do if the editor is closed
							if (ExistingBugEditor.this != null
									&& !ExistingBugEditor.this.isDisposed()) {
								changeDirtyStatus(false);
								BugzillaPlugin.getDefault().getWorkbench()
										.getActiveWorkbenchWindow().getActivePage()
										.closeEditor(ExistingBugEditor.this, true);
							}
							OfflineView.removeReport(bug);
						}
					});
				} catch (final BugzillaException e) {
					PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
						public void run() {
							BugzillaPlugin.getDefault().logAndShowExceptionDetailsDialog(
											e,
											"occurred while posting the bug.",
											"I/O Error");
						}
					});
					submitButton.setEnabled(true);
					ExistingBugEditor.this.showBusy(false);
				} catch (final PossibleBugzillaFailureException e) {
					PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
						public void run() {
							WebBrowserDialog.openAcceptAgreement(null,
									"Possible Bugzilla Failure",
									"Bugzilla may not have posted your bug.\n" + e.getMessage(), 
									form.getError());
							BugzillaPlugin.log(e);
						}
					});
					submitButton.setEnabled(true);
					ExistingBugEditor.this.showBusy(false);
				} catch (final LoginException e) {
					PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
						public void run() {
							MessageDialog.openError(null,
											"Login Error",
											"Bugzilla could not post your bug since your login name or password is incorrect.\nPlease check your settings in the bugzilla preferences. ");
						}
					});
					submitButton.setEnabled(true);
					ExistingBugEditor.this.showBusy(false);
				}

			}
		};
		
		Job job = new Job("Submitting Bug"){

			@Override
			protected IStatus run(IProgressMonitor monitor) {
				try{
					op.run(monitor);
				} catch (Exception e){
					MylarPlugin.log(e, "Failed to submit bug");
					return new Status(Status.ERROR, "org.eclipse.mylar.bugzilla.ui", Status.ERROR, "Failed to submit bug", e);
				}
				
				PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable(){
					public void run() {
						if(TaskListView.getDefault() != null && 
								TaskListView.getDefault().getViewer() != null) {
							
							// TODO: left off here
							
							// find the bug task
							// tell it to refresh, not in a job
							// call update on the task
							new RefreshBugzillaReportsAction().run();
						}
					}
				});
				return Status.OK_STATUS;
			}
			
		};
		
		job.schedule();
	}

	@Override
	protected void createDescriptionLayout() {
		// Description Area
		Composite descriptionComposite = new Composite(infoArea, SWT.NONE);
		GridLayout descriptionLayout = new GridLayout();
		descriptionLayout.numColumns = 4;
		descriptionComposite.setLayout(descriptionLayout);
		descriptionComposite.setBackground(background);
		GridData descriptionData = new GridData(GridData.FILL_BOTH);
		descriptionData.horizontalSpan = 1;
		descriptionData.grabExcessVerticalSpace = false;
		descriptionComposite.setLayoutData(descriptionData);
		// End Description Area
		
		StyledText t = newLayout(descriptionComposite, 4, "Description:", HEADER);
		t.addListener(SWT.FocusIn, new DescriptionListener());
		t = newLayout(descriptionComposite, 4, bug.getDescription(), VALUE);
		t.setFont(COMMENT_FONT);
		t.addListener(SWT.FocusIn, new DescriptionListener());
        
        texts.add(textsindex, t);
        textHash.put(bug.getDescription(), t);
        textsindex++; 
        
	}

	@Override
	protected void createCommentLayout() {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			
			// Additional (read-only) Comments Area
			Composite addCommentsComposite = new Composite(infoArea, SWT.NONE);
			GridLayout addCommentsLayout = new GridLayout();
			addCommentsLayout.numColumns = 4;
			addCommentsComposite.setLayout(addCommentsLayout);
			addCommentsComposite.setBackground(background);
			GridData addCommentsData = new GridData(GridData.FILL_BOTH);
			addCommentsData.horizontalSpan = 1;
			addCommentsData.grabExcessVerticalSpace = false;
			addCommentsComposite.setLayoutData(addCommentsData);
			//	End Additional (read-only) Comments Area
			
            StyledText t = null;
			for (Iterator<Comment> it = bug.getComments().iterator(); it.hasNext(); ) {
				Comment comment = it.next();
				String commentHeader = "Additional comment #" + comment.getNumber() + " from "
						+ comment.getAuthorName() + " on "
						+ df.format(comment.getCreated());
				t = newLayout(addCommentsComposite, 4, commentHeader, HEADER);
				t.addListener(SWT.FocusIn, new CommentListener(comment));
				t = newLayout(addCommentsComposite, 4, comment.getText(), VALUE);
				t.setFont(COMMENT_FONT);
				t.addListener(SWT.FocusIn, new CommentListener(comment));
                                
                //code for outline
                texts.add(textsindex, t);
                textHash.put(comment, t);
                textsindex++;
			}
	
			// Additional Comments Text
			Composite addCommentsTitleComposite =
				new Composite(addCommentsComposite, SWT.NONE);
			GridLayout addCommentsTitleLayout = new GridLayout();
			addCommentsTitleLayout.horizontalSpacing = 0;
			addCommentsTitleLayout.marginWidth = 0;
			addCommentsTitleComposite.setLayout(addCommentsTitleLayout);
			addCommentsTitleComposite.setBackground(background);
			GridData addCommentsTitleData =
				new GridData(GridData.HORIZONTAL_ALIGN_FILL);
			addCommentsTitleData.horizontalSpan = 4;
			addCommentsTitleData.grabExcessVerticalSpace = false;
			addCommentsTitleComposite.setLayoutData(addCommentsTitleData);
			newLayout(
				addCommentsTitleComposite,
				4,
				"Additional Comments:",
				HEADER).addListener(SWT.FocusIn, new NewCommentListener());
			addCommentsText =
				new Text(
					addCommentsComposite,
					SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.WRAP);
			addCommentsText.setFont(COMMENT_FONT);
			GridData addCommentsTextData =
				new GridData(GridData.HORIZONTAL_ALIGN_FILL);
			addCommentsTextData.horizontalSpan = 4;
			addCommentsTextData.widthHint = DESCRIPTION_WIDTH;
			addCommentsTextData.heightHint = DESCRIPTION_HEIGHT;
			
			addCommentsText.setLayoutData(addCommentsTextData);
			addCommentsText.setText(bug.getNewComment());
			addCommentsText.addListener(SWT.KeyUp, new Listener() {
				
				public void handleEvent(Event event) {
					String sel = addCommentsText.getText();
					if (!(bug.getNewNewComment().equals(sel))) {
						bug.setNewNewComment(sel);
						changeDirtyStatus(true);
					}
					validateInput();
				}
			});
			addCommentsText.addListener(SWT.FocusIn, new NewCommentListener());
			// End Additional Comments Text
	
            addCommentsTextBox = addCommentsText;
            
			this.createSeparatorSpace(addCommentsComposite);
		}
	
	@Override
	protected void addKeywordsList(String keywords, Composite attributesComposite) {
		newLayout(attributesComposite, 1, "Keywords:", PROPERTY);
		keywordsText = new Text(attributesComposite, SWT.BORDER);
		keywordsText.setFont(TEXT_FONT);
		keywordsText.setEditable(false);
		keywordsText.setForeground(foreground);
		keywordsText.setBackground(JFaceColors.getErrorBackground(display));
		GridData keywordsData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		keywordsData.horizontalSpan = 2;
		keywordsData.widthHint = 200;
		keywordsText.setLayoutData(keywordsData);
		keywordsText.setText(keywords);
		keywordsText.addListener(SWT.FocusIn, new GenericListener());
		keyWordsList = new List(attributesComposite, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
		keyWordsList.setFont(TEXT_FONT);
		GridData keyWordsTextData =
			new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		keyWordsTextData.horizontalSpan = 1;
		keyWordsTextData.widthHint = 125;
		keyWordsTextData.heightHint = 40;
		keyWordsList.setLayoutData(keyWordsTextData);
		
		// initialize the keywords list with valid values
		java.util.List<String> keywordList = bug.getKeywords();
		if (keywordList != null) {
			for (Iterator<String> it = keywordList.iterator(); it.hasNext(); ) {
				String keyword = it.next();
				keyWordsList.add(keyword);
			}
			
			// get the selected keywords for the bug
			StringTokenizer st = new StringTokenizer(keywords, ",", false);
			ArrayList<Integer> indicies = new ArrayList<Integer>();
			while (st.hasMoreTokens()) {
				String s = st.nextToken().trim();
				int index = keyWordsList.indexOf(s);
				if (index != -1)
					indicies.add(new Integer(index));
			}
	
			// select the keywords that were selected for the bug
			int length = indicies.size();
			int[] sel = new int[length];
			for (int i = 0; i < length; i++) {
				sel[i] = indicies.get(i).intValue();
			}
			keyWordsList.select(sel);
		}
		
		keyWordsList.addSelectionListener(new KeywordListener());
		keyWordsList.addListener(SWT.FocusIn, new GenericListener());
	}

	@Override
	protected void addCCList(String ccValue, Composite attributesComposite) {
		newLayout(attributesComposite, 1, "Add CC:", PROPERTY);
		ccText = new Text(attributesComposite, SWT.BORDER);
		ccText.setFont(TEXT_FONT);
		ccText.setEditable(true);
		ccText.setForeground(foreground);
		ccText.setBackground(background);
		GridData ccData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		ccData.horizontalSpan = 1;
		ccData.widthHint = 200;
		ccText.setLayoutData(ccData);
		ccText.setText(ccValue);
		ccText.addListener(SWT.FocusIn, new GenericListener());
		ccText.addModifyListener(new ModifyListener(){

			public void modifyText(ModifyEvent e) {
				changeDirtyStatus(true);
				Attribute a = bug.getAttributeForKnobName("newcc");
				if(a != null){
					a.setNewValue(ccText.getText());
				}
			}
			
		});
		
		newLayout(attributesComposite, 1, "CC: (Select to remove)", PROPERTY);
		
		ccList = new List(attributesComposite, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
		ccList.setFont(TEXT_FONT);
		GridData ccListData =
			new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		ccListData.horizontalSpan = 1;
		ccListData.widthHint = 125;
		ccListData.heightHint = 40;
		ccList.setLayoutData(ccListData);
		
		// initialize the keywords list with valid values
		Set<String> ccs = bug.getCC();
		if (ccs != null) {
			for (Iterator<String> it = ccs.iterator(); it.hasNext(); ) {
				String cc = it.next();
				ccList.add(HtmlStreamTokenizer.unescape(cc));
			}
		}
		
		ccList.addSelectionListener(new SelectionListener(){

			public void widgetSelected(SelectionEvent e) {
				changeDirtyStatus(true);
				
				for(String cc: ccList.getItems()){
					int index = ccList.indexOf(cc);
					if(ccList.isSelected(index)){
						removeCC.add(cc);
					} else {
						removeCC.remove(cc);
					}
				}
			}

			public void widgetDefaultSelected(SelectionEvent e) {}			
		});
		ccList.addListener(SWT.FocusIn, new GenericListener());
	}

	@Override
	protected void updateBug() {
			
		// go through all of the attributes and update the main values to the new ones
		for (Iterator<Attribute> it = bug.getAttributes().iterator(); it.hasNext(); ) {
			Attribute a = it.next();
			if(a.getNewValue().compareTo(a.getValue()) != 0){
				bug.setHasChanged(true);
			}
			a.setValue(a.getNewValue());
			
		}
		if(bug.getNewComment().compareTo(bug.getNewNewComment()) != 0){
			bug.setHasChanged(true);
		}
		
		// Update some other fields as well.
		bug.setNewComment(bug.getNewNewComment());
		
			
	}

	@Override
	protected void restoreBug() {
		
		if(bug == null)
			return;
		
		// go through all of the attributes and restore the new values to the main ones
		for (Iterator<Attribute> it = bug.getAttributes().iterator(); it.hasNext(); ) {
			Attribute a = it.next();
			a.setNewValue(a.getValue());
		}
		
		// Restore some other fields as well.
		bug.setNewNewComment(bug.getNewComment());
	}

	/**
	 * This job opens a compare editor to compare the current state of the bug
	 * in the editor with the bug on the server.
	 */
	protected class OpenCompareEditorJob extends Job {
		
			public OpenCompareEditorJob(String name) {
				super(name);
			}
		
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				final BugReport serverBug;
				try {
					serverBug = BugzillaRepository.getInstance().getBug(bug.getId());
					// If no bug was found on the server, throw an exception so that the
					// user gets the same message that appears when there is a problem reading the server.
					if (serverBug == null) 
						throw new Exception();
				} catch (Exception e) {
					Workbench.getInstance().getDisplay().asyncExec(new Runnable() {
						public void run() {
							MessageDialog.openInformation(Workbench.getInstance().getActiveWorkbenchWindow().getShell(),
									"Could not open bug.", "Bug #" + bug.getId() + " could not be read from the server.");
						}
					});
					return new Status(IStatus.OK, IBugzillaConstants.PLUGIN_ID, IStatus.OK, "Could not get the bug report from the server.", null);
				}
				Workbench.getInstance().getDisplay().asyncExec(new Runnable() {
					public void run() {
						compareInput.setTitle("Bug #" + bug.getId());
						compareInput.setLeft(bug);
						compareInput.setRight(serverBug);
						CompareUI.openCompareEditor(compareInput);
					}
				});
				return new Status(IStatus.OK, IBugzillaConstants.PLUGIN_ID, IStatus.OK, "", null);
			}
		
		}
		
	/**
	 * Class to handle the selection change of the keywords.
	 */
	protected class KeywordListener implements SelectionListener {
	
		/*
		 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
		 */
		public void widgetSelected(SelectionEvent arg0) {
			changeDirtyStatus(true);
			
			// get the selected keywords and create a string to submit
			StringBuffer keywords = new StringBuffer();
			String [] sel = keyWordsList.getSelection();
			
			// allow unselecting 1 keyword when it is the only one selected
			if(keyWordsList.getSelectionCount() == 1) {
				int index = keyWordsList.getSelectionIndex();
				String keyword = keyWordsList.getItem(index);
				if (bug.getAttribute("Keywords").getNewValue().equals(keyword))
					keyWordsList.deselectAll();
			}
			
			for(int i = 0; i < keyWordsList.getSelectionCount(); i++) {
				keywords.append(sel[i]);
				if (i != keyWordsList.getSelectionCount()-1) {
					keywords.append(",");
				}
			}
			bug.getAttribute("Keywords").setNewValue(keywords.toString());
			
			// update the keywords text field
			keywordsText.setText(keywords.toString());
		}
	
		/* 
		 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
		 */
		public void widgetDefaultSelected(SelectionEvent arg0) {	
			// no need to listen to this
		}
	
	}

	/**
	 * A listener for selection of the description field.
	 */
	protected class DescriptionListener implements Listener {
		public void handleEvent(Event event) {
			fireSelectionChanged(new SelectionChangedEvent(selectionProvider, new StructuredSelection(new BugzillaReportSelection(bug.getId(), bug.getServer(), "Description", true, bug.getSummary()))));
		}
	}
	
	/**
	 * A listener for selection of a comment.
	 */
	protected class CommentListener implements Listener {
		
		/** The comment that this listener is for. */
		private Comment comment;

		/**
		 * Creates a new <code>CommentListener</code>.
		 * @param comment The comment that this listener is for.
		 */
		public CommentListener(Comment comment) {
			this.comment = comment;
		}
		
		public void handleEvent(Event event) {
			fireSelectionChanged(new SelectionChangedEvent(selectionProvider, new StructuredSelection(new BugzillaReportSelection(bug.getId(), bug.getServer(), comment.getCreated().toString(), comment, bug.getSummary()))));
		}
	}
	
	/**
	 * A listener for selection of the textbox where a new comment is entered
	 * in.
	 */
	protected class NewCommentListener implements Listener {
		public void handleEvent(Event event) {
			fireSelectionChanged(new SelectionChangedEvent(selectionProvider, new StructuredSelection(new BugzillaReportSelection(bug.getId(), bug.getServer(), "New Comment", false, bug.getSummary()))));
		}
	}
	
	/**
	 * Class to handle the selection change of the radio buttons.
	 */
	protected class RadioButtonListener implements SelectionListener, ModifyListener {

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

		public void widgetSelected(SelectionEvent e) {
			Button selected = null;
			for (int i = 0; i < radios.length; i++) {
				if (radios[i].getSelection())
					selected = radios[i];
			}
			// determine the operation to do to the bug
			for (int i = 0; i < radios.length; i++) {
				if (radios[i] != e.widget && radios[i] != selected){
					radios[i].setSelection(false);
				}
				
				if (e.widget == radios[i]) {
					Operation o = bug.getOperation(radios[i].getText());
					bug.setSelectedOperation(o);
					ExistingBugEditor.this.changeDirtyStatus(true);
				}
				else if(e.widget == radioOptions[i]) {
					Operation o = bug.getOperation(radios[i].getText());
					o.setOptionSelection(((Combo)radioOptions[i]).getItem(((Combo)radioOptions[i]).getSelectionIndex()));
					
					if(bug.getSelectedOperation() != null)
						bug.getSelectedOperation().setChecked(false);
					o.setChecked(true);
					
					bug.setSelectedOperation(o);
					radios[i].setSelection(true);
		            if(selected != null && selected != radios[i]){
		                selected.setSelection(false);
		            }
		            ExistingBugEditor.this.changeDirtyStatus(true);
				}
			}
			validateInput();
		}

		public void modifyText(ModifyEvent e) {
			Button selected = null;
			for (int i = 0; i < radios.length; i++) {
				if (radios[i].getSelection())
					selected = radios[i];
			}
			// determine the operation to do to the bug
			for (int i = 0; i < radios.length; i++) {
				if (radios[i] != e.widget && radios[i] != selected){
					radios[i].setSelection(false);
				}
				
				if (e.widget == radios[i]) {
					Operation o = bug.getOperation(radios[i].getText());
					bug.setSelectedOperation(o);
					ExistingBugEditor.this.changeDirtyStatus(true);
				}
				else if(e.widget == radioOptions[i]) {
					Operation o = bug.getOperation(radios[i].getText());
					o.setInputValue(((Text)radioOptions[i]).getText());
					
					if(bug.getSelectedOperation() != null)
						bug.getSelectedOperation().setChecked(false);
					o.setChecked(true);
					
					bug.setSelectedOperation(o);
					radios[i].setSelection(true);
		            if(selected != null && selected != radios[i]){
		                selected.setSelection(false);
		            }
		            ExistingBugEditor.this.changeDirtyStatus(true);
				}
			}
			validateInput();
		}
	}

	private void validateInput() {
		Operation o = bug.getSelectedOperation();
		if(o != null && o.getKnobName().compareTo("resolve") == 0 && (addCommentsText.getText() == null || addCommentsText.getText().equals(""))){
			submitButton.setEnabled(false);
		} else{
			submitButton.setEnabled(true);
		}
	}
	
	@Override
	public void handleSummaryEvent() {
		String sel = summaryText.getText();
		Attribute a = getBug().getAttribute("Summary");
		if (!(a.getNewValue().equals(sel))) {
			a.setNewValue(sel);
			changeDirtyStatus(true);
		}
	}
	
//	TODO used for spell checking.  Add back when we want to support this
//	protected Button checkSpellingButton;
//	
//	private void checkSpelling() {
//		SpellingContext context= new SpellingContext();
//		context.setContentType(Platform.getContentTypeManager().getContentType(IContentTypeManager.CT_TEXT));
//		IDocument document = new Document(addCommentsTextBox.getText());
//		ISpellingProblemCollector collector= new SpellingProblemCollector(document);
//		EditorsUI.getSpellingService().check(document, context, collector, new NullProgressMonitor());	
//	}
//	
//	private class SpellingProblemCollector implements ISpellingProblemCollector {
//
//		private IDocument document;
//		
//		private SpellingDialog spellingDialog;
//		
//		public SpellingProblemCollector(IDocument document){
//			this.document = document;
//			spellingDialog = new SpellingDialog(Display.getCurrent().getActiveShell(), "Spell Checking", document);
//		}
//		
//		/*
//		 * @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#accept(org.eclipse.ui.texteditor.spelling.SpellingProblem)
//		 */
//		public void accept(SpellingProblem problem) {
//			try {
//				int line= document.getLineOfOffset(problem.getOffset()) + 1;
//				String word= document.get(problem.getOffset(), problem.getLength());
//				System.out.println(word);
//				for(ICompletionProposal proposal : problem.getProposals()){
//					System.out.println(">>>" + proposal.getDisplayString());
//				}
//				
//				spellingDialog.open(word, problem.getProposals());
//				
//			} catch (BadLocationException x) {
//				// drop this SpellingProblem
//			}
//		}
//
//		/*
//		 * @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#beginCollecting()
//		 */
//		public void beginCollecting() {
//			
//		}
//
//		/*
//		 * @see org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector#endCollecting()
//		 */
//		public void endCollecting() {
//			MessageDialog.openInformation(Display.getCurrent().getActiveShell(), "Spell Checking Finished", "The spell check has finished");
//		}
//	}
}

Back to the top