Skip to main content
summaryrefslogtreecommitdiffstats
blob: 777231a25b521c95d4bde9106ebd4aa63bc383c0 (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
/*******************************************************************************
 * 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.wizard;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.mylar.bugzilla.core.Attribute;
import org.eclipse.mylar.bugzilla.core.NewBugModel;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
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.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.internal.WorkbenchImages;
import org.eclipse.ui.internal.ide.IDEInternalWorkbenchImages;


/**
 * Class that contains shared functions for the last page of the wizards that
 * submit bug reports. This page allows the user to set the bug report's
 * attributes before submitting it.
 */
public abstract class AbstractWizardDataPage extends WizardPage implements
		Listener {

	/** The instance of the workbench */
	protected IWorkbench workbench;

	/** Text field for the bugs url */
	protected Text urlText;

	/** Text field for the description of the bug */
	protected Text descriptionText;

	/** Text field for the summary of the bug */
	protected Text summaryText;
	
	/** Radio button to select when sending the new bug report to the server */
	protected Button serverButton;
	
	/** Radio button to select when saving the new bug report offline */
	protected Button offlineButton;

	/** Combo box for the component that caused the bug */
	protected Combo componentCombo;

	/** Combo box for the priority of the bug */
	protected Combo priorityCombo;

	/** Combo box for the platform the bug occurred on */
	protected Combo platformCombo;

	/** Combo box for the severity of the bug */
	protected Combo severityCombo;

	/** Combo box for the products version */
	protected Combo versionCombo;

	/** Combo box for the OS that the bug occurred under */
	protected Combo oSCombo;

	/** Enum for value */
	protected final String VALUE = "VALUE";

	/** Enum for property */
	protected final String PROPERTY = "PROPERTY";

	/** Enum for header */
	protected final String HEADER = "HEADER";

	/** The horizontal indentation of the labels */
	protected final int HORZ_INDENT = 0;

	/** Status variable for the possible errors on this page */
	protected IStatus attributeStatus;

	/**
	 * Constructor for AbstractWizardDataPage
	 * 
	 * @param pageName
	 *            the name of the page
	 * @param title
	 *            the title of the page
	 * @param description
	 *            the description text for the page
	 * @param workbench
	 *            the instance of the workbench
	 */
	public AbstractWizardDataPage(String pageName, String title,
			String description, IWorkbench workbench) {
		super(pageName);
		setTitle(title);
		setDescription(description);
		this.workbench = workbench;

		// set the status for the page
		attributeStatus = new Status(IStatus.OK, "not_used", 0, "", null);
	}

	/**
	 * Applies the status to the status line of a dialog page.
	 * 
	 * @param status
	 *            The status to apply to the status line
	 */
	protected void applyToStatusLine(IStatus status) {
		String message = status.getMessage();
		if (message.length() == 0)
			message = null;
		switch (status.getSeverity()) {
		case IStatus.OK:
			setErrorMessage(null);
			setMessage(message);
			break;
		case IStatus.WARNING:
			setErrorMessage(null);
			setMessage(message, WizardPage.WARNING);
			break;
		case IStatus.INFO:
			setErrorMessage(null);
			setMessage(message, WizardPage.INFORMATION);
			break;
		default:
			setErrorMessage(null);
			setMessage(message, WizardPage.ERROR);
			break;
		}
	}

	/**
	 * Make sure that a String that is <code>null</code> is changed to a null
	 * string
	 * 
	 * @param text
	 *            The text to check if it is null or not
	 * @return The string in its proper format
	 */
	public String checkText(String text) {
		if (text == null)
			return "";
		else
			return text;
	}

	@Override
	public boolean canFlipToNextPage() {
		// no next page for this path through the wizard
		return false;
	}

	/**
	 * Create a new layout for a component
	 * 
	 * @param composite
	 *            The parent composite
	 * @param colSpan
	 *            The number of columns that this can span
	 * @param text
	 *            The text to add to the control
	 * @param style
	 *            The style that the control should have
	 */
	public void newLayout(Composite composite, int colSpan, String text,
			String style) {
		GridData data = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		data.horizontalSpan = colSpan;

		// create the proper layout for the style
		if (style.equalsIgnoreCase(VALUE)) {
			Label l = new Label(composite, SWT.NONE);
			FontData fontData = l.getFont().getFontData()[0];
			fontData.setStyle(SWT.BOLD | fontData.getStyle());
			Font font = new Font(null, fontData);
			l.setFont(font);
			l.setText(checkText(text));

			data.horizontalIndent = HORZ_INDENT;
			l.setLayoutData(data);
		} else if (style.equalsIgnoreCase(PROPERTY)) {
			Label l = new Label(composite, SWT.NONE);
			FontData fontData = l.getFont().getFontData()[0];
			fontData.setStyle(SWT.BOLD | fontData.getStyle());
			Font font = new Font(null, fontData);
			l.setFont(font);
			l.setText(checkText(text));

			data.horizontalIndent = HORZ_INDENT;
			l.setLayoutData(data);
		} else {
			Composite generalTitleGroup = new Composite(composite, SWT.NONE);
			generalTitleGroup.setLayoutData(new GridData(
					GridData.FILL_HORIZONTAL));
			generalTitleGroup.setLayoutData(data);
			GridLayout generalTitleLayout = new GridLayout();
			generalTitleLayout.numColumns = 2;
			generalTitleLayout.marginWidth = 0;
			generalTitleLayout.marginHeight = 9;
			generalTitleGroup.setLayout(generalTitleLayout);

			Label image = new Label(generalTitleGroup, SWT.NONE);
			image.setImage(WorkbenchImages.getImage(
					IDEInternalWorkbenchImages.IMG_OBJS_WELCOME_ITEM));

			GridData gd = new GridData(GridData.FILL_BOTH);
			gd.verticalAlignment = GridData.VERTICAL_ALIGN_BEGINNING;
			image.setLayoutData(gd);
			Label l = new Label(composite, SWT.NONE);
			FontData fontData = l.getFont().getFontData()[0];
			fontData.setStyle(SWT.BOLD | fontData.getStyle());
			Font font = new Font(null, fontData);
			l.setFont(font);
			l.setText(checkText(text));

			data.horizontalIndent = HORZ_INDENT;
			l.setLayoutData(data);
		}
	}

	/**
	 * Determine if the page is complete when the summary is changed
	 * 
	 * @param e
	 *            The event which occurred
	 */
	public void handleEvent(Event e) {
		boolean pageComplete = isPageComplete();

		// Initialize a variable with the no error status
		Status status = new Status(IStatus.OK, "not_used", 0, "", null);

		setPageComplete(pageComplete);

		if (!pageComplete)
			status = new Status(IStatus.ERROR, "not_used", 0,
					"You must enter a summary and a description", null);

		attributeStatus = status;

		// Show the most serious error
		applyToStatusLine(attributeStatus);

		setPageComplete(pageComplete);
		getWizard().getContainer().updateButtons();
	}

	@Override
	public IWizardPage getNextPage() {
		saveDataToModel();
		return null;
	}

	/**
	 * Sets the completed field on the wizard class when all the needed
	 * information is entered and the wizard can be completed
	 * 
	 * @return true if the wizard can be completed, false otherwise
	 */
	@Override
	public boolean isPageComplete() {
		AbstractBugWizard wizard = (AbstractBugWizard) getWizard();
		if (summaryText.getText() == null || summaryText.getText().equals("")
				|| descriptionText.getText() == null
				|| descriptionText.getText().equals("")) {
			wizard.attributeCompleted = false;
			return false;
		}
		saveDataToModel();
		return true;
	}

	/**
	 * Save the data obtained from this point in the wizard to the model.
	 */
	protected void saveDataToModel() {
		// get the model that we are using
		AbstractBugWizard wizard = (AbstractBugWizard) getWizard();
		NewBugModel nbm = wizard.model;

		nbm.setDescription(descriptionText.getText());
		nbm.setSummary(summaryText.getText());

		// go through each of the attributes and sync their values with the
		// combo boxes
		for (Iterator<Attribute> it = nbm.getAttributes().iterator(); it.hasNext();) {
			Attribute attribute = it.next();
			String key = attribute.getName();
			Map<String, String> values = attribute.getOptionValues();

			if (values == null)
				values = new HashMap<String, String>();

			if (key.equals("OS")) {
				String os = oSCombo.getItem(oSCombo.getSelectionIndex());
				attribute.setValue(os);
			} else if (key.equals("Version")) {
				String version = versionCombo.getItem(versionCombo
						.getSelectionIndex());
				attribute.setValue(version);
			} else if (key.equals("Severity")) {
				String severity = severityCombo.getItem(severityCombo
						.getSelectionIndex());
				attribute.setValue(severity);
			} else if (key.equals("Platform")) {
				String platform = platformCombo.getItem(platformCombo
						.getSelectionIndex());
				attribute.setValue(platform);
			} else if (key.equals("Component")) {
				String component = componentCombo.getItem(componentCombo
						.getSelectionIndex());
				attribute.setValue(component);
			} else if (key.equals("Priority")) {
				String priority = priorityCombo.getItem(priorityCombo
						.getSelectionIndex());
				attribute.setValue(priority);
			} else if (key.equals("URL")) {
				String url = urlText.getText();
				if (url.equalsIgnoreCase("http://"))
					url = "";
				attribute.setValue(url);
			} else {
				// do nothing
			}
		}
		wizard.attributeCompleted = true;
	}

	@Override
	protected void setControl(Control c) {
	    super.setControl(c);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public void createControl(Composite parent) {
		// whether the priority exists or not
		boolean priExist = false;

		String url = null;

		// get the model for the new bug
		AbstractBugWizard wizard = (AbstractBugWizard) getWizard();
		NewBugModel nbm = wizard.model;

		// Attributes Composite- this holds all the combo fields and text
		// fields
		Composite attributesComposite = new Composite(parent, SWT.NONE);
		GridLayout attributesLayout = new GridLayout();
		attributesLayout.numColumns = 4;
		attributesLayout.horizontalSpacing = 14;
		attributesLayout.verticalSpacing = 6;
		attributesComposite.setLayout(attributesLayout);

		GridData attributesData = new GridData(GridData.FILL_BOTH);
		attributesData.horizontalSpan = 1;
		attributesData.grabExcessVerticalSpace = false;
		attributesComposite.setLayoutData(attributesData);
		// End Attributes Composite

		GridLayout attributesTitleLayout = new GridLayout();
		attributesTitleLayout.horizontalSpacing = 0;
		attributesTitleLayout.marginWidth = 0;

		GridData attributesTitleData = new GridData(
				GridData.HORIZONTAL_ALIGN_FILL);
		attributesTitleData.horizontalSpan = 4;

		attributesTitleData.grabExcessVerticalSpace = false;

		// Add the product to the composite
		newLayout(attributesComposite, 1, "Product", PROPERTY);
		newLayout(attributesComposite, 1, nbm.getProduct(), VALUE);

		//	Populate Attributes
		for (Iterator<Attribute> it = nbm.getAttributes().iterator(); it.hasNext();) {
			Attribute attribute = it.next();
			String key = attribute.getParameterName();
			String name = attribute.getName();
			String value = checkText(attribute.getValue());
			Map<String, String> values = attribute.getOptionValues();

			// if it is a hidden field, don't try to display it
			if (attribute.isHidden())
				continue;

			if (key == null)
				key = "";

			if (values == null)
				values = new HashMap<String, String>();

			GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
			data.horizontalSpan = 1;
			data.horizontalIndent = HORZ_INDENT;

			// create and populate the combo fields for the attributes
			if (key.equals("op_sys")) {
				newLayout(attributesComposite, 1, name, PROPERTY);
				oSCombo = new Combo(attributesComposite, SWT.NO_BACKGROUND
						| SWT.MULTI | SWT.V_SCROLL | SWT.READ_ONLY);

				oSCombo.setLayoutData(data);
				Set<String> s = values.keySet();
				String[] a = s.toArray(new String[s.size()]);
				for (int i = 0; i < a.length; i++) {
					oSCombo.add(a[i]);
				}
				int index;
				if ((index = oSCombo.indexOf(value)) == -1)
					index = 0;
				oSCombo.select(index);
				oSCombo.addListener(SWT.Modify, this);
			} else if (key.equals("version")) {
				newLayout(attributesComposite, 1, name, PROPERTY);

				versionCombo = new Combo(attributesComposite, SWT.NO_BACKGROUND
						| SWT.MULTI | SWT.V_SCROLL | SWT.READ_ONLY);

				versionCombo.setLayoutData(data);
				Set<String> s = values.keySet();
				String[] a = s.toArray(new String[s.size()]);
				for (int i = 0; i < a.length; i++) {
					versionCombo.add(a[i]);
				}
				int index;
				if ((index = versionCombo.indexOf(value)) == -1)
					index = 0;
				versionCombo.select(index);
				versionCombo.addListener(SWT.Modify, this);
			} else if (key.equals("bug_severity")) {
				newLayout(attributesComposite, 1, name, PROPERTY);
				severityCombo = new Combo(attributesComposite,
						SWT.NO_BACKGROUND | SWT.MULTI | SWT.V_SCROLL
								| SWT.READ_ONLY);

				severityCombo.setLayoutData(data);
				Set<String> s = values.keySet();
				String[] a = s.toArray(new String[s.size()]);
				for (int i = 0; i < a.length; i++) {
					severityCombo.add(a[i]);
				}
				int index;
				if ((index = severityCombo.indexOf(value)) == -1)
					index = 0;
				severityCombo.select(index);
				severityCombo.addListener(SWT.Modify, this);

			} else if (key.equals("rep_platform")) {
				newLayout(attributesComposite, 1, name, PROPERTY);
				platformCombo = new Combo(attributesComposite,
						SWT.NO_BACKGROUND | SWT.MULTI | SWT.V_SCROLL
								| SWT.READ_ONLY);

				platformCombo.setLayoutData(data);
				Set<String> s = values.keySet();
				String[] a = s.toArray(new String[s.size()]);
				for (int i = 0; i < a.length; i++) {
					platformCombo.add(a[i]);
				}
				int index;
				if ((index = platformCombo.indexOf(value)) == -1)
					index = 0;
				platformCombo.select(index);
				platformCombo.addListener(SWT.Modify, this);
			} else if (key.equals("component")) {
				newLayout(attributesComposite, 1, name, PROPERTY);
				componentCombo = new Combo(attributesComposite,
						SWT.NO_BACKGROUND | SWT.MULTI | SWT.V_SCROLL
								| SWT.READ_ONLY);

				componentCombo.setLayoutData(data);
				Set<String> s = values.keySet();
				String[] a = s.toArray(new String[s.size()]);
				for (int i = 0; i < a.length; i++) {
					componentCombo.add(a[i]);
				}
				int index;
				if ((index = componentCombo.indexOf(value)) == -1)
					index = 0;
				componentCombo.select(index);
				componentCombo.addListener(SWT.Modify, this);
			} else if (key.equals("priority")) {
				newLayout(attributesComposite, 1, name, PROPERTY);
				priorityCombo = new Combo(attributesComposite,
						SWT.NO_BACKGROUND | SWT.MULTI | SWT.V_SCROLL
								| SWT.READ_ONLY);

				priorityCombo.setLayoutData(data);
				Set<String> s = values.keySet();
				String[] a = s.toArray(new String[s.size()]);
				for (int i = 0; i < a.length; i++) {
					priorityCombo.add(a[i]);
				}
				int index;
				if ((index = priorityCombo.indexOf(value)) == -1)
					index = 0;
				priorityCombo.select(index);
				priorityCombo.addListener(SWT.Modify, this);
				priExist = true;
			} else if (key.equals("bug_file_loc")) {
				url = value;
			} else {
				// do nothing if it isn't a standard value to change
			}
		}

		if (priExist) {
			newLayout(attributesComposite, 1, "", PROPERTY);
			newLayout(attributesComposite, 1, "", PROPERTY);
		}

		GridData summaryTextData;

		if (url != null) {
			// add the assigned to text field
			newLayout(attributesComposite, 1, "URL", PROPERTY);
			urlText = new Text(attributesComposite, SWT.BORDER | SWT.SINGLE
					| SWT.WRAP);
			summaryTextData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);

			summaryTextData.horizontalSpan = 3;
			summaryTextData.widthHint = 200;
			urlText.setLayoutData(summaryTextData);
			urlText.setText(url);
			urlText.addListener(SWT.FocusOut, this);
		}

		// add the summary text field
		newLayout(attributesComposite, 1, "Summary", PROPERTY);
		summaryText = new Text(attributesComposite, SWT.BORDER | SWT.SINGLE
				| SWT.WRAP);
		summaryTextData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);

		summaryTextData.horizontalSpan = 3;
		summaryTextData.widthHint = 200;
		summaryText.setLayoutData(summaryTextData);
		summaryText.setText(nbm.getSummary());
		summaryText.addListener(SWT.Modify, this);

		// Description Text
		Composite descriptionComposite = new Composite(attributesComposite,
				SWT.NONE);

		descriptionComposite.setLayout(attributesTitleLayout);

		GridData descriptionData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		descriptionData.horizontalSpan = 4;
		descriptionData.grabExcessVerticalSpace = false;
		descriptionComposite.setLayoutData(descriptionData);
		newLayout(descriptionComposite, 4, "Description:", HEADER);

		// add the description text field
		descriptionText = new Text(attributesComposite, SWT.BORDER | SWT.MULTI
				| SWT.WRAP | SWT.V_SCROLL);

		GridData descriptionTextData = new GridData(
				GridData.HORIZONTAL_ALIGN_FILL);
		descriptionTextData.horizontalSpan = 4;
		descriptionTextData.widthHint = 200;
		descriptionTextData.heightHint = 100;
		descriptionText.setLayoutData(descriptionTextData);
		descriptionText.addListener(SWT.Modify, this);

	    serverButton = new Button(attributesComposite, SWT.RADIO);
		serverButton.setText("Submit bug report to the server.");
	    GridData toServerButtonData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		toServerButtonData.horizontalSpan = 4;
		serverButton.setLayoutData(toServerButtonData);
		serverButton.setSelection(true);

	    offlineButton = new Button(attributesComposite, SWT.RADIO);
		offlineButton.setText("Save bug report offline.");
	    GridData offlineButtonData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
		offlineButtonData.horizontalSpan = 4;
		offlineButton.setLayoutData(offlineButtonData);
		offlineButton.setSelection(false);

		setControl(attributesComposite);
		return;
	}
	
	/**
	 * @return <code>true</code> if the radio button to submit the bug to the server is selected.
	 */
	public boolean serverSelected() {
		return (serverButton == null) ? false : serverButton.getSelection();
	}

	/**
	 * @return <code>true</code> if the radio button to save the bug offline is selected.
	 */
	public boolean offlineSelected() {
		return (offlineButton == null) ? false : offlineButton.getSelection();
	}
}

Back to the top