1 /*
2 Bloof - visualize the evolution of your software project
3 Copyright ( C ) 2003 Lukasz Pekacki <lukasz@pekacki.de>
4 http://bloof.sf.net/
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along with
15 this program; if not, write to the Free Software Foundation, Inc.,
16 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 $RCSfile: ScriptController.java,v $
19 Created on $Date: 2003/09/06 08:46:35 $
20 */
21 package net.sf.bloof.script;
22
23 import java.sql.SQLException;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.logging.Logger;
29
30 import net.sf.bloof.Bloof;
31 import net.sf.bloof.BloofException;
32 import net.sf.bloof.BloofExceptionListener;
33 import net.sf.bloof.db.Database;
34 import net.sf.bloof.db.DbAccess;
35 import net.sf.bloof.db.StringIterator;
36 import net.sf.bloof.events.BloofEvent;
37 import net.sf.bloof.events.BloofEventListener;
38 import net.sf.bloof.metrics.Metric;
39 import net.sf.bloof.metrics.MetricRegistry;
40 import net.sf.bloof.metrics.TimeInterval;
41 import net.sf.bloof.scm.ScmAccess;
42 import net.sf.bloof.script.events.ScriptAction;
43 import net.sf.bloof.script.events.ScriptActionListener;
44 import net.sf.bloof.script.events.ScriptEvent;
45 import net.sf.bloof.script.events.ScriptEventListener;
46 import net.sf.bloof.util.ProgressEvent;
47 import net.sf.bloof.util.ProgressListener;
48
49 /***
50 * Acts as the controller of the gui, interacting between model and view
51 * @author Lukasz Pekacki <pekacki@users.sourceforge.net>
52 * @version $Id: ScriptController.java,v 1.1 2003/09/06 08:46:35 pekacki Exp $
53 */
54 public class ScriptController
55 implements
56 ScriptEventListener,
57 ScriptActionListener,
58 ProgressListener,
59 BloofExceptionListener,
60 BloofEventListener {
61
62 public void addScriptEventListener(ScriptEventListener aListener) {
63 mScriptEventListeners.add(aListener);
64 }
65
66 /* (non-Javadoc)
67 * @see net.sf.bloof.events.BloofEventListener#bloofEventOccured(net.sf.bloof.events.BloofEvent)
68 */
69 public void bloofEventOccured(BloofEvent aBloofEvent) {
70 HashMap params = aBloofEvent.getParams();
71 switch (aBloofEvent.getEventType()) {
72 case BloofEvent.TYPE_EXPORT_FINISHED :
73 {
74 informScriptEventListeners(
75 new ScriptEvent(
76 ScriptEvent.MESSAGE,
77 ScriptEvent.PARAM_OBJECT,
78 "Result exported."));
79 break;
80 }
81 case BloofEvent.TYPE_METRIC_RUN_FINISHED :
82 {
83 Metric newMetric = (Metric) params.get(BloofEvent.PARAM_OBJECT);
84 String name = createNewListIdent(newMetric.getName());
85 MetricResult mr = new MetricResult(name, newMetric);
86 mAvailableMetricResults.add(mr);
87 informScriptEventListeners(
88 new ScriptEvent(
89 ScriptEvent.NEW_METRIC_RUN,
90 ScriptEvent.PARAM_OBJECT,
91 newMetric));
92 break;
93 }
94 case BloofEvent.TYPE_PROJECT_IMPORTED :
95 {
96 mCurrentProject = mToBeImported;
97 mCurrentProject.setModified();
98 informScriptEventListeners(
99 new ScriptEvent(
100 ScriptEvent.PROJECT_CHANGED,
101 ScriptEvent.PARAM_OBJECT,
102 mCurrentProject));
103 break;
104 }
105 case BloofEvent.TYPE_PROJECT_OPENED :
106 case BloofEvent.TYPE_UPDATE_FINISHED :
107 {
108 informScriptEventListeners(
109 new ScriptEvent(
110 ScriptEvent.PROJECT_CHANGED,
111 ScriptEvent.PARAM_OBJECT,
112 mCurrentProject));
113 break;
114 }
115 case BloofEvent.TYPE_PROJECT_CLOSED :
116 {
117 cleanControl();
118 informScriptEventListeners(ScriptEvent.PROJECT_CLOSED);
119 break;
120 }
121 }
122
123 }
124
125 /* (non-Javadoc)
126 * @see net.sf.bloof.BloofExceptionListener#bloofExceptionHappend(net.sf.bloof.BloofException)
127 */
128 public void bloofExceptionHappend(BloofException aException) {
129 sLogger.warning(aException.toString());
130 showError(aException.toString());
131
132 }
133 /* (non-Javadoc)
134 * @see net.sf.bloof.shell.BrowserActionListener#browserActionOccured(net.sf.bloof.shell.BrowserAction)
135 */
136 public void scriptActionOccured(ScriptAction bE) {
137 HashMap params = bE.getParams();
138 switch (bE.getSwitchType()) {
139 case ScriptAction.TYPE_EXECUTE_EXPORT_RESULT :
140 {
141 String resultName = (String) params.get(PARAM_RESULT);
142 Metric m = null;
143 for (Iterator iter = mAvailableMetricResults.iterator(); iter.hasNext();) {
144 MetricResult element = (MetricResult) iter.next();
145 if (element.getTitle().equals(resultName)) {
146 m = element.getMetric();
147 }
148 }
149 if (m == null) {
150 showError("No such result found:"+resultName);
151 }
152 String fileName = (String) params.get(PARAM_FILE_NAME);
153 Bloof.exportResult(m, fileName);
154 informScriptEventListeners(
155 new ScriptEvent(
156 ScriptEvent.MESSAGE,
157 ScriptEvent.PARAM_OBJECT,
158 "Successfully exported to:"+fileName));
159
160 break;
161 }
162 case ScriptAction.TYPE_EXIT :
163 {
164 Bloof.exit();
165 System.exit(0);
166 break;
167 }
168 case ScriptAction.TYPE_ERROR :
169 {
170 showError((String) params.get(ScriptAction.PARAM_OBJECT));
171 break;
172 }
173 case ScriptAction.TYPE_EXECUTE_METRIC :
174 {
175 Metric m = (Metric) params.get(ScriptAction.PARAM_OBJECT);
176 Bloof.runMetric(m);
177 break;
178 }
179 case ScriptAction.TYPE_LIST_RESULTS :
180 {
181 StringBuffer sb = new StringBuffer();
182 for (Iterator iter = mAvailableMetricResults.iterator(); iter.hasNext();) {
183 MetricResult element = (MetricResult) iter.next();
184 sb.append(element.getTitle());
185 sb.append("\n");
186 }
187 informScriptEventListeners(
188 new ScriptEvent(
189 ScriptEvent.MESSAGE,
190 ScriptEvent.PARAM_OBJECT,
191 sb.toString()));
192
193 break;
194 }
195 case ScriptAction.TYPE_LIST_METRICS :
196 {
197 StringBuffer sb = new StringBuffer();
198 for (Iterator iter = MetricRegistry.getAvailableMetricNames(); iter.hasNext();) {
199 String element = (String) iter.next();
200 sb.append(element);
201 sb.append("\n");
202 }
203 informScriptEventListeners(
204 new ScriptEvent(
205 ScriptEvent.MESSAGE,
206 ScriptEvent.PARAM_OBJECT,
207 sb.toString()));
208
209 break;
210 }
211 case ScriptAction.TYPE_EXECUTE_OPEN_PROJECT :
212 {
213 DbAccess db = (DbAccess) params.get(PARAM_DB_ACCESS);
214 try {
215 Bloof.openProject(db);
216 } catch (SQLException e) {
217 showError("Could not open project." + e.toString());
218 }
219 break;
220 }
221 case ScriptAction.TYPE_EXECUTE_IMPORT_PROJECT :
222 {
223 DbAccess dbA = (DbAccess) params.get(PARAM_DB_ACCESS);
224 ScmAccess scmA = (ScmAccess) params.get(PARAM_SCM_ACCESS);
225 String projectName = (String) params.get(PARAM_PROJECT_NAME);
226 HashMap projectParams = new HashMap();
227 projectParams.put(Project.PARAM_SCM_ACCESS, scmA);
228 projectParams.put(Project.PARAM_PROJECT_NAME, projectName);
229 projectParams.put(Project.PARAM_USERNAME, dbA.getLogin());
230 projectParams.put(Project.PARAM_PASSWORD, dbA.getPassword());
231 projectParams.put(Project.PARAM_URL, dbA.getDatabaseUrl());
232 mToBeImported = new Project(projectParams);
233 mCurrentProject = null;
234 sLogger.fine(
235 "Importing Project with scm:"
236 + scmA.toString()
237 + " to database:"
238 + dbA.toString());
239 try {
240 Bloof.importProject(scmA, dbA);
241 } catch (Exception e) {
242 showError("Could not import project. Reason:" + e.toString());
243 mCurrentProject = null;
244 }
245 break;
246 }
247 case ScriptAction.TYPE_SHOW_USER_INFO :
248 {
249 informScriptEventListeners(
250 new ScriptEvent(
251 ScriptEvent.MESSAGE,
252 ScriptEvent.PARAM_OBJECT,
253 params.get(ScriptAction.PARAM_OBJECT)));
254 break;
255 }
256 }
257
258 }
259
260 /* (non-Javadoc)
261 * @see net.sf.bloof.shell.BrowserEventListener#browserEventOccured(net.sf.bloof.shell.BrowserEvent)
262 */
263 public void scriptEventOccured(ScriptEvent bE) {
264 sLogger.fine("Received:" + bE.toString());
265
266 }
267
268 /***
269 * Cleans all state information and makes the GuiControl ready for a new project
270 */
271 public void cleanControl() {
272 mCurrentProject = null;
273 mAvailableMetricResults.clear();
274 }
275
276 /***
277 * Checks if the specified name is already used in a result list or a filter
278 * and adds a counter in case of collision
279 * @param aIdent name to be checked
280 * @return unique name
281 */
282 public static String createNewListIdent(String aIdent) {
283 if (sFilterNames.containsKey(aIdent)) {
284 int count = ((Integer) sFilterNames.get(aIdent)).intValue();
285 count++;
286 sFilterNames.put(aIdent, new Integer(count));
287 return aIdent + " - " + count;
288 } else {
289 sFilterNames.put(aIdent, new Integer(1));
290 return aIdent;
291 }
292 }
293
294
295 public HashMap getBasicParams() {
296 HashMap h = new HashMap();
297 h.put(ScriptController.PARAM_CONTROLLER, this);
298 return h;
299 }
300
301 /***
302 * @return
303 */
304 public Project getCurrentProject() {
305 return mCurrentProject;
306 }
307
308 public int getCurrentResult() {
309 return mCurrentResult;
310 }
311 /***
312 * Returns the available DeveloperGroups
313 * @return Iterator on available DeveloperGroups
314 */
315 public Iterator getDeveloperGroups() {
316 return mCurrentProject.getDeveloperGroups();
317 }
318
319 public StringIterator getDeveloperList() {
320 try {
321 Database db = Bloof.getDatabase();
322 return db.getDevelopersOrderedByName();
323 } catch (SQLException e) {
324 showError("Could not fetch file list from database:" + e.toString());
325 return null;
326 }
327 }
328 /***
329 * Returns the available FileGroups
330 * @return Iterator on available FileGroups
331 */
332 public Iterator getFileGroups() {
333 return mCurrentProject.getFileGroups();
334 }
335
336 public StringIterator getFileList() {
337 try {
338 Database db = Bloof.getDatabase();
339 return db.getFilesOrderdByPath();
340 } catch (SQLException e) {
341 showError("Could not fetch file list from database:" + e.toString());
342 return null;
343 }
344 }
345
346 /***
347 * Returns the available TimeSpans
348 * @return Iterator on available TimeSpans
349 */
350 public Iterator getTimespans() {
351 return mCurrentProject.getTimeIntervals();
352 }
353
354 public TimeInterval getWholeTimespan() {
355 try {
356 Database db = Bloof.getDatabase();
357 return db.getTimespanBoundaries();
358 } catch (SQLException e) {
359 showError("Could not get the timespan from database. Reason:" + e.toString());
360 return null;
361 }
362 }
363
364 private void informScriptEventListeners(ScriptEvent aEvent) {
365 for (Iterator iter = mScriptEventListeners.iterator(); iter.hasNext();) {
366 ScriptEventListener element = (ScriptEventListener) iter.next();
367 element.scriptEventOccured(aEvent);
368 }
369 }
370
371 /***
372 * Helper for code convention. No variable is allowed to be unread.
373 * But what about ActionEvents that are useless? This method is the answer.
374 * @param e useLessEvent
375 */
376 protected void processUselessEvent(Object aE) {
377 if (aE == null) {
378 // do nothing
379 }
380 }
381 /* (non-Javadoc)
382 * @see net.sf.bloof.util.ProgressListener#progressHappend(net.sf.bloof.util.ProgressEvent)
383 */
384 public void progressHappend(ProgressEvent aPe) {
385 }
386
387 public void removeBrowserEventListener(ScriptEventListener aListener) {
388 mScriptEventListeners.remove(aListener);
389 }
390
391 /***
392 *
393 */
394 protected void setCurrentResult(int aIndex) {
395 mCurrentResult = aIndex;
396 }
397
398 /***
399 * @param aString
400 */
401 private void showError(String aString) {
402 informScriptEventListeners(
403 new ScriptEvent(ScriptEvent.ERROR, ScriptEvent.PARAM_OBJECT, aString));
404 }
405 private static Logger sLogger = Logger.getLogger(ScriptController.class.getName());
406
407 private ArrayList mAvailableMetricResults = new ArrayList();
408 private HashSet mScriptEventListeners = new HashSet();
409 private Project mCurrentProject, mToBeImported;
410 private int mCurrentResult = 0;
411 private static HashMap sFilterNames = new HashMap();
412 public static final String PARAM_CONTROLLER = "gui controler",
413 PARAM_DB_ACCESS = "db access",
414 PARAM_METRIC = "metric",
415 PARAM_RESULT = "metric",
416 PARAM_FILE_NAME = "file name",
417 PARAM_SCM_ACCESS = "scm access",
418 PARAM_PROJECT_NAME = "project name";
419
420 }
This page was automatically generated by Maven