KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > launchConfigurations > TaskLinkManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.launchConfigurations;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.ant.internal.ui.AntUtil;
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.debug.core.model.IProcess;
23 import org.eclipse.debug.ui.console.FileLink;
24 import org.eclipse.debug.ui.console.IConsole;
25 import org.eclipse.jface.text.BadLocationException;
26 import org.eclipse.jface.text.IRegion;
27 import org.eclipse.ui.console.IHyperlink;
28
29 /**
30  * Manages task links per process. As messages are logged to the console from
31  * build events, hyperlinks are created to link task names to the associated ant
32  * buildfile. The build logger registers a task hyperlink with this manager for
33  * each build event associated with a task. When the associated line is later
34  * appended to the console, the corresponding text region in the console
35  * document is determined (as the length of a console document can not be
36  * determined beforehand), and the hyperlink is added to the document.
37  * The new line is added to the console, information from that line
38  * may be stored to process future incoming tasks hyperlinks.
39  */

40 public class TaskLinkManager {
41     
42     /**
43      * A map of processes to lists of queued task hyperlink entries
44      */

45     private static Map JavaDoc fgProcessToLinks;
46     
47     /**
48      * A map of processes to lists of queued new line regions
49      */

50     private static Map JavaDoc fgProcessToNewLines;
51     
52     private static List JavaDoc fgAntBuilds;
53     
54     private static class HyperlinkEntry {
55         private IHyperlink fLink;
56         private IRegion fRegion;
57         private String JavaDoc fMessage;
58         
59         public HyperlinkEntry(IHyperlink link, IRegion region, String JavaDoc message) {
60             fLink = link;
61             fRegion = region;
62             fMessage = message;
63         }
64         
65         public IRegion getRegion() {
66             return fRegion;
67         }
68         
69         public IHyperlink getLink() {
70             return fLink;
71         }
72         
73         public String JavaDoc getMessage() {
74             return fMessage;
75         }
76     }
77     
78     private static class LineEntry {
79         private IConsole fConsole;
80         private IRegion fRegion;
81     
82         public LineEntry(IConsole console, IRegion region) {
83             fConsole = console;
84             fRegion = region;
85         }
86     
87         public IRegion getRegion() {
88             return fRegion;
89         }
90     
91         public IConsole getConsole() {
92             return fConsole;
93         }
94     }
95
96     /**
97      * Not to be called.
98      */

99     private TaskLinkManager() {
100         super();
101     }
102     
103     /**
104      * Registers a hyperlink for the given process and task name. The given
105      * region is relative to the beginning of the line (not the document).
106      *
107      * @param process the process associated with the link
108      * @param link the link for the process
109      * @param region The region within the line
110      * @param message The message related to the link
111      */

112     public static synchronized void addTaskHyperlink(IProcess process, IHyperlink link, IRegion region, String JavaDoc message) {
113         if (fgProcessToNewLines != null) {
114             List JavaDoc newLines = (List JavaDoc)fgProcessToNewLines.get(process);
115             if (newLines != null) {
116                 for (int index= 0; index < newLines.size(); index++) {
117                     LineEntry newLine = (LineEntry) newLines.get(index);
118                     if (addLink(newLine.getConsole(), link, newLine.getRegion(), region, message)) {
119                         newLines.subList(0, index + 1).clear();
120                         return;
121                     }
122                 }
123             }
124         }
125                 
126         if (fgProcessToLinks == null) {
127             fgProcessToLinks = new HashMap JavaDoc();
128             
129         }
130         List JavaDoc links = (List JavaDoc)fgProcessToLinks.get(process);
131         if (links == null) {
132             links = new ArrayList JavaDoc(10);
133             fgProcessToLinks.put(process, links);
134         }
135         
136         links.add(new HyperlinkEntry(link, region, message));
137     }
138     
139     private static boolean addLink(IConsole console, IHyperlink link, IRegion lineRegion, IRegion region, String JavaDoc message) {
140         
141         int length = region.getLength();
142         
143         String JavaDoc text;
144         try {
145             text = console.getDocument().get(lineRegion.getOffset(), lineRegion.getLength());
146         } catch (BadLocationException e) {
147             return false;
148         }
149         if (text.trim().equals(message)) {
150             int offset = lineRegion.getOffset() + region.getOffset();
151             console.addLink(link, offset, length);
152             return true;
153         }
154         return false;
155     }
156
157     /**
158      * A new line has been added to the given console. Adds any task hyperlink
159      * associated with the line, to the console.
160      * The new line may be stored to process future incoming tasks hyperlinks.
161      *
162      * @param console
163      * @param newLine
164      */

165     public static synchronized void processNewLine(IConsole console, IRegion newLine) {
166         IProcess process = console.getProcess();
167         if (fgAntBuilds != null && fgAntBuilds.contains(process)) {
168             if (linkBuildFileMessage(console, newLine)) {
169                 fgAntBuilds.remove(process);
170                 return;
171             }
172         }
173         if (fgProcessToLinks == null) {
174             addNewLine(console, newLine, process);
175             return;
176         }
177         
178         List JavaDoc links = (List JavaDoc)fgProcessToLinks.get(process);
179         if (links == null) {
180             addNewLine(console, newLine, process);
181             return;
182         }
183         
184         for (int index= 0; index < links.size(); index++) {
185             HyperlinkEntry link = (HyperlinkEntry) links.get(index);
186             if (addLink(console, link.getLink(), newLine, link.getRegion(), link.getMessage())) {
187                 links.subList(0, index + 1).clear();
188                 return;
189             }
190         }
191     }
192     
193     private static void addNewLine(IConsole console, IRegion newLine, IProcess process) {
194         if (fgProcessToNewLines == null) {
195             fgProcessToNewLines = new HashMap JavaDoc();
196         }
197         List JavaDoc newLines = (List JavaDoc)fgProcessToNewLines.get(process);
198         if (newLines == null) {
199             newLines= new ArrayList JavaDoc();
200         }
201     
202         newLines.add(new LineEntry(console, newLine));
203         fgProcessToNewLines.put(process, newLines);
204     }
205
206     /**
207      * Disposes any information stored for the given process.
208      *
209      * @param process
210      */

211     public static void dispose(IProcess process) {
212         if (fgProcessToLinks != null) {
213             fgProcessToLinks.remove(process);
214         }
215         if (fgProcessToNewLines != null) {
216             fgProcessToNewLines.remove(process);
217         }
218         if (fgAntBuilds != null) {
219             fgAntBuilds.remove(process);
220         }
221     }
222     
223     /**
224      * Registers the specified process as an Ant build process.
225      * Allows for the generation of the "Buildfile: somefile" link in the
226      * Ant output.
227      *
228      * @param process
229      */

230     public static synchronized void registerAntBuild(IProcess process) {
231         if (fgProcessToNewLines != null) {
232             List JavaDoc newLines = (List JavaDoc)fgProcessToNewLines.get(process);
233             if (newLines != null) {
234                 for (Iterator JavaDoc iter = newLines.iterator(); iter.hasNext();) {
235                     LineEntry newLine = (LineEntry) iter.next();
236                     if (linkBuildFileMessage(newLine.getConsole(), newLine.getRegion())){
237                         iter.remove();
238                         return;
239                     }
240                 }
241             }
242         }
243         if (fgAntBuilds == null) {
244             fgAntBuilds= new ArrayList JavaDoc();
245         }
246         fgAntBuilds.add(process);
247     }
248     
249     private static boolean linkBuildFileMessage(IConsole console, IRegion region) {
250         
251         String JavaDoc message= ""; //$NON-NLS-1$
252
int offset= region.getOffset();
253         try {
254             message = console.getDocument().get(offset, region.getLength());
255         } catch (BadLocationException e) {
256         }
257         if (message.startsWith("Buildfile:")) { //$NON-NLS-1$
258
String JavaDoc fileName = message.substring(10).trim();
259             IFile file = AntUtil.getFileForLocation(fileName, null);
260             if (file != null) {
261                 FileLink link = new FileLink(file, null, -1, -1, -1);
262                 console.addLink(link, offset + 11, fileName.length());
263                 return true;
264             }
265         }
266         return false;
267     }
268 }
269
Popular Tags