KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > bugs > scarab > ScarabEngine


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.bugs.scarab;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Date JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.text.MessageFormat JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.openide.ErrorManager;
32 import org.openide.util.NbBundle;
33 import org.openide.util.RequestProcessor;
34 import org.openide.util.Utilities;
35
36 import org.netbeans.modules.tasklist.core.TaskListView;
37 import org.netbeans.modules.tasklist.bugs.*;
38 import org.openide.awt.HtmlBrowser;
39 import org.openide.awt.StatusDisplayer;
40
41 import javax.swing.*;
42
43 /**
44  * Bridge which provides Scarab data to the BugList
45  * This class is almost exactally the same as IZBugEngine.
46  *
47  * @author Tor Norbye, serff
48  */

49 public final class ScarabEngine implements BugEngine {
50
51     public ScarabEngine() {
52     }
53
54     /**
55      * Return the user name of the engine
56      */

57     public String JavaDoc getName() {
58         return (NbBundle.getMessage(ScarabEngine.class, "Scarab")); // NOI18N;
59
}
60
61     public JComponent getQueryCustomizer(final BugQuery query,
62             final boolean edit) {
63         
64         return new SourcePanel();
65     }
66
67     public void refresh(final BugQuery query, final BugList list) {
68         // Do in the background
69
RequestProcessor.postRequest(new Runnable JavaDoc() {
70             public void run() {
71                 doRefresh(query, list);
72             }
73         });
74     }
75
76     /**
77      *
78      */

79     public void doRefresh(final BugQuery inQuery, final BugList list) {
80         if( !(inQuery instanceof ScarabBugQuery) ){
81             throw new IllegalArgumentException JavaDoc("ScarabEngine.doRefresh only excepts ScarabBugQuery argument"); //NOI18N
82
}
83         final ScarabBugQuery sbQuery = (ScarabBugQuery)inQuery;
84         final TaskListView v = TaskListView.getCurrent();
85         BugsView view = null;
86         if (v instanceof BugsView) {
87             view = (BugsView) v;
88             view.setCursor(Utilities.createProgressCursor(view));
89         }
90         try {
91             if ((inQuery.getBaseUrl() == null || inQuery.getBaseUrl().equals("")) //NOI18N
92
|| (inQuery.getQueryString() == null || inQuery.getQueryString().equals(""))) { //NOI18N
93
//They didn't enter anything on the gui
94
StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(ScarabEngine.class,
95                         "BadQuery")); // NOI18N
96
return;
97             }
98             // Do a bug query
99
final String JavaDoc baseurl = inQuery.getBaseUrl();
100             final String JavaDoc query = "downloadtype=1&go="+inQuery.getQueryString(); //NOI18N
101

102             System.out.println("Baseurl = " + baseurl + " query = " + query); //NOI18N
103

104             StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(ScarabEngine.class,
105                     "Refreshing")); // NOI18N
106
URL JavaDoc url = null;
107             try {
108                 url = new URL JavaDoc(baseurl);
109             } catch (MalformedURLException JavaDoc e) {
110                 ErrorManager.getDefault().notify(e);
111             }
112             if (url != null) {
113                 final Scarab scarab = new Scarab(url);
114                 try {
115                     StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(ScarabEngine.class,
116                             "DoingQuery")); // NOI18N
117

118                     // Successful list fetch -- replace the contents
119
final List JavaDoc issues = scarab.query(query);
120                     final List JavaDoc bugs = new ArrayList JavaDoc();
121                     for (Iterator JavaDoc it = issues.iterator(); it.hasNext();) {
122                         
123                         final Issue issue = (Issue)it.next();
124                         StatusDisplayer.getDefault().setStatusText(MessageFormat.format(NbBundle.getMessage(ScarabEngine.class,
125                                 "QueryingBug"), // NOI18N
126
new String JavaDoc[]{issue.getId()}));
127
128                         final Object JavaDoc priorityObj = issue.getAttribute(sbQuery.getAttributeName(Issue.PRIORITY));
129                         final int priority = ( priorityObj != null && priorityObj instanceof Number JavaDoc)
130                             ? ((Number JavaDoc)priorityObj).intValue()
131                             : 0;
132                         final Object JavaDoc votesObj = issue.getAttribute(sbQuery.getAttributeName(Issue.VOTES));
133                         final int votes = ( votesObj != null && votesObj instanceof Number JavaDoc)
134                             ? ((Number JavaDoc)votesObj).intValue()
135                             : 0;
136                         
137                         final Bug bug = new Bug(issue.getId(),
138                                 (String JavaDoc) issue.getAttribute(sbQuery.getAttributeName(Issue.SUMMARY)),
139                                 priority,
140                                 issue.getType(),
141                                 (String JavaDoc) issue.getAttribute(sbQuery.getAttributeName(Issue.COMPONENT)),
142                                 (String JavaDoc) issue.getAttribute(sbQuery.getAttributeName(Issue.SUBCOMPONENT)),
143                                 issue.getCreated(),
144                                 (String JavaDoc) issue.getAttribute(sbQuery.getAttributeName(Issue.KEYWORDS)),
145                                 (String JavaDoc) issue.getAttribute(sbQuery.getAttributeName(Issue.ASSIGNED_TO)),
146                                 issue.getReportedBy(),
147                                 (String JavaDoc) issue.getAttribute(sbQuery.getAttributeName(Issue.STATUS)),
148                                 (String JavaDoc) issue.getAttribute(sbQuery.getAttributeName(Issue.TARGET)),
149                                 votes);
150                         bug.setEngine(this);
151
152                         bugs.add(bug);
153                     }
154                     list.setBugs(bugs);
155                     
156                 } catch (org.xml.sax.SAXException JavaDoc se) {
157                     ErrorManager.getDefault().notify(se);
158                     System.out.println("Couldn't read bug list: sax exception"); //NOI18N
159
} catch (java.net.UnknownHostException JavaDoc uhe) {
160                     StatusDisplayer.getDefault().setStatusText(MessageFormat.format(NbBundle.getMessage(ScarabEngine.class,
161                             "NoNet"), // NOI18N
162
new String JavaDoc[]{baseurl}));
163
164                 } catch (java.io.IOException JavaDoc ioe) {
165                     ErrorManager.getDefault().notify(ioe);
166                     System.out.println("Couldn't read bug list: io exception"); //NOI18N
167
}
168                 StatusDisplayer.getDefault().setStatusText(""); //NOI18N
169
}
170         } finally {
171             if (view != null) {
172                 view.setCursor(null);
173             }
174         }
175     }
176
177     /**
178      * View a particular bug.
179      */

180     public void viewBug(final Bug bug, final String JavaDoc server) {
181         // Show URL
182
try {
183             // XXX why server/service doe not contain bugzilla too?
184
final URL JavaDoc url = new URL JavaDoc(new URL JavaDoc(server), "id/" + bug.getId());
185             HtmlBrowser.URLDisplayer.getDefault().showURL(url);
186         } catch (MalformedURLException JavaDoc e) {
187             ErrorManager.getDefault().notify(e);
188         }
189     }
190 }
191
Popular Tags