Skip to main content
summaryrefslogtreecommitdiffstats
blob: 699b6dab833af774ba7b80295cbf63acc6565e5f (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
/*******************************************************************************
 * Copyright (c) 2003 - 2006 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.internal.bugzilla.ui.editor;

import java.util.Iterator;

import javax.security.auth.login.LoginException;

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.dialogs.MessageDialog;
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.IBugzillaBug;
import org.eclipse.mylar.internal.bugzilla.core.BugReportPostHandler;
import org.eclipse.mylar.internal.bugzilla.core.BugzillaException;
import org.eclipse.mylar.internal.bugzilla.core.BugzillaPlugin;
import org.eclipse.mylar.internal.bugzilla.core.NewBugModel;
import org.eclipse.mylar.internal.bugzilla.core.PossibleBugzillaFailureException;
import org.eclipse.mylar.internal.bugzilla.ui.OfflineView;
import org.eclipse.mylar.internal.bugzilla.ui.WebBrowserDialog;
import org.eclipse.mylar.internal.bugzilla.ui.actions.RefreshBugzillaReportsAction;
import org.eclipse.mylar.internal.core.util.MylarStatusHandler;
import org.eclipse.mylar.internal.tasklist.TaskRepository;
import org.eclipse.mylar.internal.tasklist.ui.views.TaskListView;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
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.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.WorkspaceModifyOperation;

/**
 * An editor used to view a locally created bug that does not yet exist on a
 * server. It uses a <code>NewBugModel</code> object to store the data.
 */
public class NewBugEditor extends AbstractBugEditor {

	protected NewBugModel bug;

	protected Text descriptionText;

	protected String newSummary = "";

	protected String newDescription = "";

	/**
	 * Creates a new <code>NewBugEditor</code>.
	 */
	public NewBugEditor(TaskRepository repository) {
		super.repository = repository;
	}

	@Override
	public IBugzillaBug getBug() {
		return bug;
	}

	@Override
	protected void addKeywordsList(String keywords, Composite attributesComposite) {
		// Since NewBugModels have no keywords, there is no
		// GUI for them.
	}

	@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

		Composite descriptionTitleComposite = new Composite(descriptionComposite, SWT.NONE);
		GridLayout descriptionTitleLayout = new GridLayout();
		descriptionTitleLayout.horizontalSpacing = 0;
		descriptionTitleLayout.marginWidth = 0;
		descriptionTitleComposite.setLayout(descriptionTitleLayout);
		descriptionTitleComposite.setBackground(background);
		GridData descriptionTitleData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		descriptionTitleData.horizontalSpan = 4;
		descriptionTitleData.grabExcessVerticalSpace = false;
		descriptionTitleComposite.setLayoutData(descriptionTitleData);
		newLayout(descriptionTitleComposite, 4, "Description:", HEADER).addListener(SWT.FocusIn,
				new DescriptionListener());

		descriptionText = new Text(descriptionComposite, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.WRAP);
		descriptionText.setFont(COMMENT_FONT);
		GridData descriptionTextData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		descriptionTextData.horizontalSpan = 4;
		descriptionTextData.widthHint = DESCRIPTION_WIDTH;
		descriptionTextData.heightHint = DESCRIPTION_HEIGHT;
		descriptionText.setLayoutData(descriptionTextData);
		descriptionText.setText(bug.getDescription());
		descriptionText.addListener(SWT.KeyUp, new Listener() {
			public void handleEvent(Event event) {
				String sel = descriptionText.getText() + event.character;
				if (!(newDescription.equals(sel))) {
					newDescription = sel;
					changeDirtyStatus(true);
				}
			}
		});
		descriptionText.addListener(SWT.FocusIn, new DescriptionListener());

		super.descriptionTextBox = descriptionText;

		this.createSeparatorSpace(descriptionComposite);
	}

	@Override
	protected void createCommentLayout() {
		// Since NewBugModels have no comments, there is no
		// GUI for them.
	}

	@Override
	protected void addRadioButtons(Composite buttonComposite) {
		// Since NewBugModels have no special submitting actions,
		// no radio buttons are required.
	}

	@Override
	protected String getTitleString() {
		return bug.getLabel();
	}

	@Override
	protected void submitBug() {
		final BugReportPostHandler form = new BugReportPostHandler();
		form.setPrefix(BugReportPostHandler.FORM_PREFIX_BUG_218);
		form.setPrefix2(BugReportPostHandler.FORM_PREFIX_BUG_220);

		form.setPostfix(BugReportPostHandler.FORM_POSTFIX_216);
		form.setPostfix2(BugReportPostHandler.FORM_POSTFIX_218);
		updateBug();

		setURL(form, "post_bug.cgi");
		// go through all of the attributes and add them to the bug post
		Iterator<Attribute> itr = bug.getAttributes().iterator();
		while (itr.hasNext()) {
			Attribute a = itr.next();
			if (a != null && a.getParameterName() != null && a.getParameterName().compareTo("") != 0 && !a.isHidden()) {
				String key = a.getName();
				String value = null;

				// get the values from the attribute
				if (key.equalsIgnoreCase("OS")) {
					value = a.getValue();
				} else if (key.equalsIgnoreCase("Version")) {
					value = a.getValue();
				} else if (key.equalsIgnoreCase("Severity")) {
					value = a.getValue();
				} else if (key.equalsIgnoreCase("Platform")) {
					value = a.getValue();
				} else if (key.equalsIgnoreCase("Component")) {
					value = a.getValue();
				} else if (key.equalsIgnoreCase("Priority")) {
					value = a.getValue();
				} else if (key.equalsIgnoreCase("URL")) {
					value = a.getValue();
				} else if (key.equalsIgnoreCase("Assign To") || key.equalsIgnoreCase("Assigned To")) {
					value = a.getValue();
				}

				// add the attribute to the bug post
				if (value == null)
					value = "";

				form.add(a.getParameterName(), value);
			} else if (a != null && a.getParameterName() != null && a.getParameterName().compareTo("") != 0
					&& a.isHidden()) {
				// we have a hidden attribute, add it to the posting
				form.add(a.getParameterName(), a.getValue());

			}

		}

		// set the summary, and description

		// add the summary to the bug post
		form.add("short_desc", bug.getSummary());

		// format the description of the bug so that it is roughly in 80
		// character lines
		bug.setDescription(formatText(bug.getDescription()));

		if (bug.getDescription().length() != 0) {
			// add the new comment to the bug post if there is some text in
			// it
			form.add("comment", bug.getDescription());
		}

		final WorkspaceModifyOperation op = new WorkspaceModifyOperation() {
			protected void execute(final IProgressMonitor monitor) throws CoreException {
				PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
					public void run() {
						// update the bug on the server
						try {
							String id = form.post();

							// If the bug was successfully sent...
							if (id != null && NewBugEditor.this != null && !NewBugEditor.this.isDisposed()) {
								changeDirtyStatus(false);
								BugzillaPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage()
										.closeEditor(NewBugEditor.this, false);
							}
							OfflineView.removeReport(bug);
						} catch (BugzillaException e) {
							MessageDialog.openError(null, "I/O Error", "Bugzilla could not post your bug.");
							BugzillaPlugin.log(e);
						} catch (PossibleBugzillaFailureException e) {
							WebBrowserDialog.openAcceptAgreement(null, "Possible Bugzilla Client Failure",
									"Bugzilla may not have posted your bug.\n" + e.getMessage(), form.getError());
							BugzillaPlugin.log(e);
						} catch (LoginException e) {
							e.printStackTrace();
							// if we had an error with logging in, display an
							// error
							MessageDialog.openError(null, "Posting Error",
									"Bugzilla could not post your bug since your login name or password is incorrect."
											+ "\nPlease check your settings in the bugzilla preferences. ");
						}
					}
				});
			}
		};

		Job job = new Job("Submitting New Bug") {

			@Override
			protected IStatus run(IProgressMonitor monitor) {
				try {
					op.run(monitor);
				} catch (Exception e) {
					MylarStatusHandler.log(e, "Failed to submit bug");
					return new Status(Status.ERROR, "org.eclipse.mylar.internal.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) {
							new RefreshBugzillaReportsAction().run();
						}
					}
				});
				return Status.OK_STATUS;
			}

		};

		job.schedule();
	}

	@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();
			a.setValue(a.getNewValue());
		}

		// Update some other fields as well.
		bug.setSummary(newSummary);
		bug.setDescription(newDescription);
	}

	@Override
	protected void restoreBug() {
		// 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());
		}
	}

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

	/**
	 * A listener for selection of the description textbox.
	 */
	protected class DescriptionListener implements Listener {
		public void handleEvent(Event event) {
			fireSelectionChanged(new SelectionChangedEvent(selectionProvider, new StructuredSelection(
					new BugzillaReportSelection(bug.getId(), bug.getRepository(), "New Description", false, bug
							.getSummary()))));
		}
	}

	@Override
	public void handleSummaryEvent() {
		String sel = summaryText.getText();
		if (!(newSummary.equals(sel))) {
			newSummary = sel;
			changeDirtyStatus(true);
		}
	}

	@Override
	protected void addCCList(String value, Composite attributesComposite) {
		// do nothing here right now
	}

}

Back to the top