Skip to main content
summaryrefslogtreecommitdiffstats
blob: e52238989725ecbceca90057c3fabaea98522368 (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
/*******************************************************************************
 * 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.ArrayList;
import java.util.Iterator;

import org.eclipse.mylar.internal.bugzilla.core.NewBugzillaReport;
import org.eclipse.mylar.internal.bugzilla.ui.BugzillaImages;
import org.eclipse.mylar.internal.bugzilla.ui.IBugzillaReportSelection;
import org.eclipse.mylar.internal.tasklist.RepositoryTaskData;
import org.eclipse.mylar.internal.tasklist.Comment;
import org.eclipse.swt.graphics.Image;

/**
 * A node for the tree in the <code>BugzillaOutlinePage</code>.
 * 
 * @author Mik Kersten (hardening of prototype)
 */
public class BugzillaOutlineNode implements IBugzillaReportSelection {

	/** The id of the Bugzilla object that the selection was on. */
	protected int id;

	/** The server of the Bugzilla object that the selection was on. */
	protected String server;

	/** The label for this piece of data. */
	private String key;

	/** The children of this node. */
	private ArrayList<BugzillaOutlineNode> nodeChildren;

	/** The parent of this node or null if it is the bug report */
	private BugzillaOutlineNode parent;

	/** This node's image. */
	private Image image;

	private Object data = null;

	private String bugSummary;

	private boolean fromEditor = false;

	private boolean isCommentHeader = false;

	private boolean isDescription = false;

	/**
	 * Creates a new <code>BugzillaOutlineNode</code>.
	 * 
	 * @param id
	 *            The id of the bug this outline is for.
	 * @param server
	 *            The server of the bug this outline is for.
	 * @param key
	 *            The label for this node.
	 * @param image
	 *            The image that will be displayed by this node in the tree.
	 * @param data
	 *            The data, if necessary, this node represents.
	 * @param parent
	 *            The parent of this node
	 */
	public BugzillaOutlineNode(int id, String server, String key, Image image, Object data, String summary) {
		this.id = id;
		this.server = server;
		this.key = key;
		this.nodeChildren = null;
		this.image = image;
		this.data = data;
		this.parent = null;
		this.bugSummary = summary;
	}

	public boolean isFromEditor() {
		return fromEditor;
	}

	/**
	 * @return The children of this node, represented as an <code>Object</code>
	 *         array.
	 */
	public BugzillaOutlineNode[] getChildren() {
		return (nodeChildren == null) ? new BugzillaOutlineNode[0] : nodeChildren
				.toArray(new BugzillaOutlineNode[nodeChildren.size()]);
	}

	/**
	 * Adds a node to this node's list of children.
	 * 
	 * @param bugNode
	 *            The new child.
	 */
	public void addChild(BugzillaOutlineNode bugNode) {
		if (nodeChildren == null) {
			nodeChildren = new ArrayList<BugzillaOutlineNode>();
		}
		bugNode.setParent(this);
		nodeChildren.add(bugNode);
	}

	/**
	 * @return The label of this node.
	 */
	public String getKey() {
		return key;
	}

	// /**
	// * Set the label of this node.
	// * @param key The new label.
	// */
	// public void setKey(String key) {
	// this.key = key;
	// }

	/**
	 * TODO: remove, nodes don't need to know about image decorator
	 */
	public Image getImage() {
		return image;
	}

	/**
	 * Sets the decorator image for this node.
	 * 
	 * @param newImage
	 *            The new image.
	 */
	public void setImage(Image newImage) {
		this.image = newImage;
	}

	/**
	 * @return <code>true</code> if the given object is another node
	 *         representing the same piece of data in the editor.
	 */
	@Override
	public boolean equals(Object arg0) {
		if (arg0 instanceof BugzillaOutlineNode) {
			BugzillaOutlineNode bugNode = (BugzillaOutlineNode) arg0;
			return getKey().equals(bugNode.getKey());
		}
		return super.equals(arg0);
	}

	@Override
	public int hashCode() {
		return getKey().hashCode();
	}

	/**
	 * @return The name of this node.
	 */
	public String getName() {
		return getKey();
	}

	/**
	 * @return The data (where applicable) this node represents.
	 */
	public Object getData() {
		return data;
	}

	/**
	 * Sets the data that this node represents.
	 * 
	 * @param data
	 *            The new piece of data.
	 */
	public void setData(Object data) {
		this.data = data;
	}

	/**
	 * Parses the given <code>IBugzillaBug</code> into a tree of
	 * <code>BugzillaOutlineNode</code>'s suitable for use in the
	 * <code>BugzillaOutlinePage</code> view.
	 * 
	 * @param bug
	 *            The bug that needs parsing.
	 * @return The tree of <code>BugzillaOutlineNode</code>'s.
	 */
	public static BugzillaOutlineNode parseBugReport(RepositoryTaskData bug) {
		// Choose the appropriate parsing function based on
		// the type of IBugzillaBug.
		if (bug instanceof NewBugzillaReport) {
			return parseNewBugReport((NewBugzillaReport) bug);
		} else if (bug instanceof RepositoryTaskData) {
			return parseExistingBugReport((RepositoryTaskData) bug);
		} else {
			return null;
		}
	}

	/**
	 * Parses the given <code>NewBugModel</code> into a tree of
	 * <code>BugzillaOutlineNode</code>'s suitable for use in the
	 * <code>BugzillaOutlinePage</code> view.
	 * 
	 * @param bug
	 *            The <code>NewBugModel</code> that needs parsing.
	 * @return The tree of <code>BugzillaOutlineNode</code>'s.
	 */
	protected static BugzillaOutlineNode parseNewBugReport(NewBugzillaReport bug) {
		int bugId = bug.getId();
		String bugServer = bug.getRepositoryUrl();
		Image bugImage = BugzillaImages.getImage(BugzillaImages.BUG);
		Image defaultImage = BugzillaImages.getImage(BugzillaImages.BUG_COMMENT);
		BugzillaOutlineNode topNode = new BugzillaOutlineNode(bugId, bugServer, bug.getLabel(), bugImage, bug, bug
				.getSummary());

		topNode.addChild(new BugzillaOutlineNode(bugId, bugServer, "New Description", defaultImage, null, bug
				.getSummary()));

		BugzillaOutlineNode titleNode = new BugzillaOutlineNode(bugId, bugServer, "NewBugModel Object", defaultImage,
				null, bug.getSummary());
		titleNode.addChild(topNode);

		return titleNode;
	}

	/**
	 * Parses the given <code>BugReport</code> into a tree of
	 * <code>BugzillaOutlineNode</code>'s suitable for use in the
	 * <code>BugzillaOutlinePage</code> view.
	 * 
	 * @param bug
	 *            The <code>BugReport</code> that needs parsing.
	 * @return The tree of <code>BugzillaOutlineNode</code>'s.
	 */
	protected static BugzillaOutlineNode parseExistingBugReport(RepositoryTaskData bug) {

		int bugId = bug.getId();
		String bugServer = bug.getRepositoryUrl();
		Image bugImage = BugzillaImages.getImage(BugzillaImages.BUG);
		Image defaultImage = BugzillaImages.getImage(BugzillaImages.BUG_COMMENT);
		BugzillaOutlineNode topNode = new BugzillaOutlineNode(bugId, bugServer, bug.getLabel(), bugImage, bug, bug
				.getSummary());

		BugzillaOutlineNode desc = new BugzillaOutlineNode(bugId, bugServer, "Description", defaultImage, bug
				.getDescription(), bug.getSummary());
		desc.setIsDescription(true);

		topNode.addChild(desc);

		BugzillaOutlineNode comments = null;
		for (Iterator<Comment> iter = bug.getComments().iterator(); iter.hasNext();) {
			Comment comment = iter.next();
			// first comment is the bug description
			if(comment.getNumber() == 0) continue;
			if (comments == null) {
				comments = new BugzillaOutlineNode(bugId, bugServer, "Comments", defaultImage, comment, bug
						.getSummary());
				comments.setIsCommentHeader(true);
			}
			comments.addChild(new BugzillaOutlineNode(bugId, bugServer, comment.getCreated().toString(), defaultImage,
					comment, bug.getSummary()));
		}
		if (comments != null) {
			topNode.addChild(comments);
		}

		topNode
				.addChild(new BugzillaOutlineNode(bugId, bugServer, "New Comment", defaultImage, null, bug.getSummary()));

		BugzillaOutlineNode titleNode = new BugzillaOutlineNode(bugId, bugServer, "BugReport Object", defaultImage,
				null, bug.getSummary());
		titleNode.addChild(topNode);

		return titleNode;
	}

	public boolean hasComment() {
		// If the comment category was selected, then the comment object is
		// not the intended selection (it is just used to help find the correct
		// location in the editor).
		return (data instanceof Comment) && !(key.toLowerCase().equals("comments"));
	}

	public Comment getComment() {
		return (hasComment()) ? (Comment) data : null;
	}

	public void setComment(Comment comment) {
		data = comment;
	}

	public String getContents() {
		return key;
	}

	public void setContents(String contents) {
		key = contents;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getServer() {
		return server;
	}

	public void setServer(String server) {
		this.server = server;
	}

	public boolean isEmpty() {
		return (server == null) || ((getContents() == null) && (getComment() == null));
	}

	public BugzillaOutlineNode getParent() {
		return parent;
	}

	public void setParent(BugzillaOutlineNode parent) {
		this.parent = parent;
	}

	public boolean isCommentHeader() {
		return isCommentHeader;
	}

	public boolean isDescription() {
		return isDescription;
	}

	public void setIsCommentHeader(boolean isCommentHeader) {
		this.isCommentHeader = isCommentHeader;
	}

	public void setIsDescription(boolean isDescription) {
		this.isDescription = isDescription;
	}

	public String getBugSummary() {
		return bugSummary;
	}
}

Back to the top