Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2041bcf3eaa51ff99962b32cbe1da55add5f1c3c (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
/*******************************************************************************
 * Copyright (c) 2004 - 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.tasklist;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.security.auth.login.LoginException;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.mylar.bugzilla.core.BugzillaPlugin;
import org.eclipse.mylar.bugzilla.core.IBugzillaConstants;
import org.eclipse.mylar.bugzilla.core.search.BugzillaSearchHit;
import org.eclipse.mylar.bugzilla.ui.BugzillaImages;
import org.eclipse.mylar.bugzilla.ui.BugzillaUiPlugin;
import org.eclipse.mylar.bugzilla.ui.search.BugzillaResultCollector;
import org.eclipse.mylar.bugzilla.ui.tasklist.BugzillaCategorySearchOperation.ICategorySearchListener;
import org.eclipse.mylar.tasklist.AbstractCategory;
import org.eclipse.mylar.tasklist.ITask;
import org.eclipse.mylar.tasklist.ITaskListElement;
import org.eclipse.mylar.tasklist.TaskListImages;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.PlatformUI;

/**
 * @author Shawn Minto
 */
public class BugzillaQueryCategory extends AbstractCategory {
	
	private static final long serialVersionUID = 5517146402031743253L;	
	private String url;
	private int maxHits;
	private List<BugzillaHit> hits = new ArrayList<BugzillaHit>();
	private boolean hasBeenRefreshed = false;
	
	protected Date lastRefresh;
	
	public class BugzillaQueryCategorySearchListener implements ICategorySearchListener { 

		Map<Integer, BugzillaSearchHit> hits = new HashMap<Integer, BugzillaSearchHit>();
		
		public void searchCompleted(BugzillaResultCollector collector) {
			for(BugzillaSearchHit hit: collector.getResults()){
		
				// HACK need the server name and handle properly
				addHit(new BugzillaHit(hit.getId() + ": " + hit.getDescription(), hit.getPriority(), hit.getId(), null, hit.getState()));
			}
		}

	}	
	
	private ICategorySearchListener listener = new BugzillaQueryCategorySearchListener();
	private boolean isMaxReached = false;
	
	public BugzillaQueryCategory(String label, String url, String maxHits) {
		super(label);
		this.url = url;
		try{
			this.maxHits = Integer.parseInt(maxHits);
		} catch (Exception e){
			this.maxHits = -1;
		}
	}

	public String getDescription(boolean label) {
		if (hits.size() > 0 || !label) {
			if(isMaxReached && label){
				return super.getDescription(label) + " <first "+ maxHits +" hits>";
			} else {
				return super.getDescription(label);
			}
		} else if (!hasBeenRefreshed) {
			return super.getDescription(label) + " <needs refresh>";
		} else {
			return super.getDescription(label) + " <no hits>";
		}
	}
	
	
	public Image getIcon() {
		return TaskListImages.getImage(BugzillaImages.CATEGORY_QUERY);
	}
	
	public String getUrl() {
		return url;
	}
	
	public List<BugzillaHit> getChildren() {
		return hits;
	}
	
	public void addHit(BugzillaHit hit) {
		BugzillaTask task = BugzillaUiPlugin.getDefault().getBugzillaTaskListManager().getFromBugzillaTaskRegistry(hit.getHandle());
		hit.setAssociatedTask(task);
		hits.add(hit);
	}

	public void removeHit(BugzillaHit hit) {
		hits.remove(hit);
	}
	
	public void refreshBugs() {
		hits.clear();
		final BugzillaCategorySearchOperation catSearch = new BugzillaCategorySearchOperation(
				getUrl(), maxHits);
		catSearch.addResultsListener(listener);
		final IStatus[] status = new IStatus[1];

		try {
			// execute the search operation
			catSearch.execute(new NullProgressMonitor());
			isMaxReached = catSearch.isMaxReached();
			hasBeenRefreshed = true;
			lastRefresh = new Date();

			// get the status of the search operation
			status[0] = catSearch.getStatus();

			// determine if there was an error, if it was cancelled, or if it is
			// ok
			if (status[0].getCode() == IStatus.CANCEL) {
				// it was cancelled, so just return
				status[0] = Status.OK_STATUS;
				//			                return status[0];
				return;
			} else if (!status[0].isOK()) {
				// there was an error, so display an error message
				PlatformUI.getWorkbench().getDisplay().asyncExec(
						new Runnable() {
							public void run() {
								ErrorDialog.openError(null,
										"Bugzilla Search Error", null,
										status[0]);
							}
						});
				status[0] = Status.OK_STATUS;
				return;
				//			                return status[0];
			}
		} catch (LoginException e) {
			// we had a problem while searching that seems like a login info
			// problem
			// thrown in BugzillaSearchOperation
			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(new Status(IStatus.ERROR,
					IBugzillaConstants.PLUGIN_ID, IStatus.OK, "", e));
		}
		return;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
	public String getPriority() {
		String highestPriority = "P5";
		if (hits.isEmpty()) {
			return "P1";
		}
		for (BugzillaHit hit : hits) {
			if (highestPriority.compareTo(hit.getPriority()) > 0) {
				highestPriority = hit.getPriority();
			}
		}
		return highestPriority;
	}

	public ITask getOrCreateCorrespondingTask() {
		return null;
	}
	
	public boolean hasCorrespondingActivatableTask() {
		return false;
	}

	public boolean isDirectlyModifiable() {
		return true;
	}
	
	public boolean isActivatable() {
		return false;
	}

	public boolean isDragAndDropEnabled() {
		return false;
	}
	
	public Color getForeground() {
       	return null;
	}

	public Font getFont() {
        for (ITaskListElement child : getChildren()) {
			if (child instanceof BugzillaHit){
				BugzillaHit hit = (BugzillaHit) child;
				BugzillaTask task = hit.getAssociatedTask();
				if(task != null && task.isActive()){
					return BOLD;
				}
			}
		}
		return null;
	}

	public boolean isCompleted() {
		return false;
	}

	public String getToolTipText() {
		String tooltip = "";
		if (hits.size() == 1) {
			tooltip += "1 hit";
		} else {
			tooltip += hits.size() + " hits";
		}
		tooltip += BugzillaTask.getLastRefreshTime(lastRefresh);
		return tooltip;
	}

	public int getMaxHits() {
		return maxHits;
	}

	public void setMaxHits(String maxHits) {
		try{
			this.maxHits = Integer.parseInt(maxHits);
		} catch (Exception e){
			this.maxHits = -1;
		}
	}
}

Back to the top