KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > JavaEvaluationEngineManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.ui;
12
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.eclipse.debug.core.DebugEvent;
18 import org.eclipse.debug.core.DebugPlugin;
19 import org.eclipse.debug.core.IDebugEventSetListener;
20 import org.eclipse.jdt.core.IJavaProject;
21 import org.eclipse.jdt.debug.core.IJavaDebugTarget;
22 import org.eclipse.jdt.debug.eval.EvaluationManager;
23 import org.eclipse.jdt.debug.eval.IAstEvaluationEngine;
24
25 public class JavaEvaluationEngineManager implements IDebugEventSetListener {
26
27     /**
28      * A mapping of maps that associates combinations of debug
29      * targets and projects to evaluation engines.
30      *
31      * The outer map associates debug
32      * targets with a map. The inner maps map projects to
33      * evaluation engines.
34      *
35      */

36     HashMap JavaDoc fTargetMap= new HashMap JavaDoc();
37
38     public JavaEvaluationEngineManager() {
39         DebugPlugin.getDefault().addDebugEventListener(this);
40     }
41
42     /**
43      * @see IDebugEventSetListener#handleDebugEvent(DebugEvent)
44      *
45      * Removes debug targets from the engine map when they terminate,
46      * and dispose of engines.
47      */

48     public void handleDebugEvents(DebugEvent[] events) {
49         DebugEvent event;
50         for (int i= 0, numEvents= events.length; i < numEvents; i++) {
51             event= events[i];
52             if (event.getKind() == DebugEvent.TERMINATE && event.getSource() instanceof IJavaDebugTarget) {
53                 HashMap JavaDoc map = (HashMap JavaDoc)fTargetMap.remove(event.getSource());
54                 if (map != null) {
55                     Iterator JavaDoc iter = map.values().iterator();
56                     while (iter.hasNext()) {
57                         ((IAstEvaluationEngine)iter.next()).dispose();
58                     }
59                     map.clear();
60                 }
61             }
62         }
63     }
64     
65     /**
66      * Returns an evaluation engine for the given project and debug target.
67      * If an engine already exists for this project and target combination,
68      * that same engine is returned. Otherwise, a new engine is created.
69      */

70     public IAstEvaluationEngine getEvaluationEngine(IJavaProject project, IJavaDebugTarget target) {
71         IAstEvaluationEngine engine= null;
72         HashMap JavaDoc map= (HashMap JavaDoc)fTargetMap.get(target);
73         if (map == null) {
74             map= new HashMap JavaDoc();
75             fTargetMap.put(target, map);
76         }
77         engine= (IAstEvaluationEngine)map.get(project);
78         if (engine == null) {
79             engine= EvaluationManager.newAstEvaluationEngine(project, target);
80             map.put(project, engine);
81         }
82         return engine;
83     }
84     
85     /**
86      * Disposes this evaluation engine manager.
87      * When disposed, the manager disposes all engines
88      * it is currently managing.
89      *
90      * After this evaluation engine manager has been disposed, it
91      * must not be reused.
92      */

93     public void dispose() {
94         HashMap JavaDoc engines;
95         Iterator JavaDoc iter= fTargetMap.values().iterator();
96         while (iter.hasNext()) {
97             engines= ((HashMap JavaDoc)iter.next());
98             Iterator JavaDoc engineIter= engines.values().iterator();
99             while (engineIter.hasNext()) {
100                 IAstEvaluationEngine engine = (IAstEvaluationEngine)engineIter.next();
101                 engine.dispose();
102             }
103             engines.clear();
104         }
105         DebugPlugin.getDefault().removeDebugEventListener(this);
106     }
107
108 }
109
Popular Tags