KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > model > AntModelCore


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.ant.internal.ui.model;
12
13 import com.ibm.icu.text.MessageFormat;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.ant.internal.ui.AntUIPlugin;
19 import org.eclipse.ant.internal.ui.debug.model.AntLineBreakpoint;
20 import org.eclipse.ant.internal.ui.debug.model.DebugModelMessages;
21 import org.eclipse.core.resources.IMarker;
22 import org.eclipse.core.resources.IMarkerDelta;
23 import org.eclipse.core.resources.IWorkspaceRunnable;
24 import org.eclipse.core.resources.ResourcesPlugin;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.debug.core.DebugPlugin;
28 import org.eclipse.debug.core.IBreakpointsListener;
29 import org.eclipse.debug.core.model.IBreakpoint;
30
31 public class AntModelCore implements IBreakpointsListener {
32     
33     private static AntModelCore inst;
34     
35     public static AntModelCore getDefault() {
36         if (inst == null) {
37             inst= new AntModelCore();
38         }
39             
40         return inst;
41     }
42     
43     private List JavaDoc fModelChangeListeners= new ArrayList JavaDoc();
44     
45     private AntModelCore() { }
46
47     public void addAntModelListener(IAntModelListener listener) {
48         synchronized (fModelChangeListeners) {
49             fModelChangeListeners.add(listener);
50         }
51     }
52     
53     public void removeAntModelListener(IAntModelListener listener) {
54         synchronized (fModelChangeListeners) {
55             fModelChangeListeners.remove(listener);
56         }
57     }
58     
59     public void notifyAntModelListeners(AntModelChangeEvent event) {
60         Iterator JavaDoc i;
61         synchronized (fModelChangeListeners) {
62             i= new ArrayList JavaDoc(fModelChangeListeners).iterator();
63         }
64         while (i.hasNext()) {
65             ((IAntModelListener)i.next()).antModelChanged(event);
66         }
67     }
68     
69     /**
70      * Updates message attributes on any Ant line breakpoints if there are active Ant editors
71      * with Ant models
72      *
73      * @see org.eclipse.debug.core.IBreakpointsListener#breakpointsAdded(org.eclipse.debug.core.model.IBreakpoint[])
74      */

75     private void updateBreakpointMessages(final IBreakpoint[] breakpoints) {
76         IWorkspaceRunnable runnable = new IWorkspaceRunnable() {
77             public void run(IProgressMonitor monitor) throws CoreException {
78                 for (int i = 0; i < breakpoints.length; i++) {
79                     IBreakpoint breakpoint= breakpoints[i];
80                     if (breakpoint instanceof AntLineBreakpoint) {
81                         IMarker marker = breakpoint.getMarker();
82                         if (marker.exists()) {
83                             int lineNumber= marker.getAttribute(IMarker.LINE_NUMBER, 0);
84                             marker.setAttribute(IMarker.MESSAGE, MessageFormat.format(DebugModelMessages.AntLineBreakpoint_0, new String JavaDoc[] {Integer.toString(lineNumber)}));
85                         }
86                     }
87                 }
88             }
89         };
90         try {
91             ResourcesPlugin.getWorkspace().run(runnable, null, 0, null);
92         } catch (CoreException e) {
93             AntUIPlugin.log(e);
94         }
95     }
96
97     public void breakpointsAdded(IBreakpoint[] breakpoints) {
98     }
99
100     public void breakpointsRemoved(IBreakpoint[] breakpoints, IMarkerDelta[] deltas) {
101     }
102
103     public void breakpointsChanged(IBreakpoint[] breakpoints, IMarkerDelta[] deltas) {
104         updateBreakpointMessages(breakpoints);
105     }
106
107     public void stopBreakpointListening() {
108         DebugPlugin.getDefault().getBreakpointManager().removeBreakpointListener(this);
109     }
110     
111     public void startBreakpointListening() {
112         DebugPlugin.getDefault().getBreakpointManager().addBreakpointListener(this);
113     }
114 }
115
Popular Tags