KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > viewers > update > LaunchProxy


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

11 package org.eclipse.debug.internal.ui.viewers.update;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.debug.core.DebugPlugin;
17 import org.eclipse.debug.core.ILaunch;
18 import org.eclipse.debug.core.ILaunchManager;
19 import org.eclipse.debug.core.ILaunchesListener2;
20 import org.eclipse.debug.internal.ui.viewers.model.provisional.IModelDelta;
21 import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext;
22 import org.eclipse.debug.internal.ui.viewers.model.provisional.ModelDelta;
23 import org.eclipse.debug.internal.ui.viewers.provisional.AbstractModelProxy;
24 import org.eclipse.jface.viewers.Viewer;
25
26 /**
27  * Model proxy for launch object.
28  *
29  * @since 3.3
30  */

31 public class LaunchProxy extends AbstractModelProxy implements ILaunchesListener2 {
32
33     private ILaunch fLaunch;
34     
35     /**
36      * Set of launch's previous children. When a child is added,
37      * its model proxy is installed.
38      */

39     private Set JavaDoc fPrevChildren = new HashSet JavaDoc();
40
41     /**
42      * Constructs a new model proxy for the given launch.
43      *
44      * @param launch
45      */

46     public LaunchProxy(ILaunch launch) {
47         fLaunch = launch;
48     }
49     
50     /* (non-Javadoc)
51      * @see org.eclipse.debug.internal.ui.viewers.AbstractModelProxy#init(org.eclipse.debug.internal.ui.viewers.IPresentationContext)
52      */

53     public void init(IPresentationContext context) {
54         super.init(context);
55         DebugPlugin.getDefault().getLaunchManager().addLaunchListener(this);
56     }
57     
58     /* (non-Javadoc)
59      * @see org.eclipse.debug.internal.ui.viewers.provisional.AbstractModelProxy#installed(org.eclipse.jface.viewers.Viewer)
60      */

61     public void installed(Viewer viewer) {
62         // install model proxies for existing children
63
installModelProxies();
64     }
65
66     /* (non-Javadoc)
67      * @see org.eclipse.debug.internal.ui.viewers.AbstractModelProxy#dispose()
68      */

69     public void dispose() {
70         super.dispose();
71         DebugPlugin.getDefault().getLaunchManager().removeLaunchListener(this);
72         fPrevChildren.clear();
73         fLaunch = null;
74     }
75
76     /* (non-Javadoc)
77      * @see org.eclipse.debug.core.ILaunchesListener2#launchesTerminated(org.eclipse.debug.core.ILaunch[])
78      */

79     public void launchesTerminated(ILaunch[] launches) {
80         for (int i = 0; i < launches.length; i++) {
81             if (launches[i] == fLaunch) {
82                 fireDelta(IModelDelta.STATE | IModelDelta.CONTENT | IModelDelta.UNINSTALL);
83             }
84         }
85     }
86
87     /* (non-Javadoc)
88      * @see org.eclipse.debug.core.ILaunchesListener#launchesRemoved(org.eclipse.debug.core.ILaunch[])
89      */

90     public void launchesRemoved(ILaunch[] launches) {
91     }
92
93     /* (non-Javadoc)
94      * @see org.eclipse.debug.core.ILaunchesListener#launchesAdded(org.eclipse.debug.core.ILaunch[])
95      */

96     public void launchesAdded(ILaunch[] launches) {
97     }
98
99     /* (non-Javadoc)
100      * @see org.eclipse.debug.core.ILaunchesListener#launchesChanged(org.eclipse.debug.core.ILaunch[])
101      */

102     public void launchesChanged(ILaunch[] launches) {
103         for (int i = 0; i < launches.length; i++) {
104             if (launches[i] == fLaunch) {
105                 fireDelta(IModelDelta.STATE | IModelDelta.CONTENT);
106                 installModelProxies();
107             }
108         }
109     }
110     
111     /**
112      * Installs model proxies for any new children in the given launch.
113      *
114      * @param launch
115      */

116     protected void installModelProxies() {
117         boolean changes = false;
118         ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
119         ILaunch[] allLaunches = manager.getLaunches();
120         ModelDelta root = new ModelDelta(manager, 0, IModelDelta.NO_CHANGE, allLaunches.length);
121         Object JavaDoc[] children = fLaunch.getChildren();
122         ModelDelta launchDelta = root.addNode(fLaunch, indexOf(fLaunch, allLaunches), IModelDelta.EXPAND, children.length);
123         for (int j = 0; j < children.length; j++) {
124             Object JavaDoc child = children[j];
125             if (fPrevChildren.add(child)) {
126                 changes = true;
127                 launchDelta.addNode(child, indexOf(child, children), IModelDelta.INSTALL, -1);
128             }
129         }
130         if (changes) {
131             fireModelChanged(root);
132         }
133     }
134     
135     /**
136      * Finds the index of the selected element in the given list
137      * @param element the element to get the index for
138      * @param list the list to search for the index
139      * @return the index of the specified element in the given array or -1 if not found
140      */

141     protected int indexOf(Object JavaDoc element, Object JavaDoc[] list) {
142         for (int i = 0; i < list.length; i++) {
143             if (element == list[i]) {
144                 return i;
145             }
146         }
147         return -1;
148     }
149     
150     /**
151      * Convenience method to fire a delta
152      * @param flags the flags to set on the delta
153      */

154     protected void fireDelta(int flags) {
155         ModelDelta delta = new ModelDelta(DebugPlugin.getDefault().getLaunchManager(), IModelDelta.NO_CHANGE);
156         delta.addNode(fLaunch, flags);
157         fireModelChanged(delta);
158     }
159
160 }
161
Popular Tags