1 11 package org.eclipse.ant.internal.ui.launchConfigurations; 12 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 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 40 public class TaskLinkManager { 41 42 45 private static Map fgProcessToLinks; 46 47 50 private static Map fgProcessToNewLines; 51 52 private static List fgAntBuilds; 53 54 private static class HyperlinkEntry { 55 private IHyperlink fLink; 56 private IRegion fRegion; 57 private String fMessage; 58 59 public HyperlinkEntry(IHyperlink link, IRegion region, String 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 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 99 private TaskLinkManager() { 100 super(); 101 } 102 103 112 public static synchronized void addTaskHyperlink(IProcess process, IHyperlink link, IRegion region, String message) { 113 if (fgProcessToNewLines != null) { 114 List newLines = (List )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 (); 128 129 } 130 List links = (List )fgProcessToLinks.get(process); 131 if (links == null) { 132 links = new ArrayList (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 message) { 140 141 int length = region.getLength(); 142 143 String 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 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 links = (List )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 (); 196 } 197 List newLines = (List )fgProcessToNewLines.get(process); 198 if (newLines == null) { 199 newLines= new ArrayList (); 200 } 201 202 newLines.add(new LineEntry(console, newLine)); 203 fgProcessToNewLines.put(process, newLines); 204 } 205 206 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 230 public static synchronized void registerAntBuild(IProcess process) { 231 if (fgProcessToNewLines != null) { 232 List newLines = (List )fgProcessToNewLines.get(process); 233 if (newLines != null) { 234 for (Iterator 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 (); 245 } 246 fgAntBuilds.add(process); 247 } 248 249 private static boolean linkBuildFileMessage(IConsole console, IRegion region) { 250 251 String message= ""; int offset= region.getOffset(); 253 try { 254 message = console.getDocument().get(offset, region.getLength()); 255 } catch (BadLocationException e) { 256 } 257 if (message.startsWith("Buildfile:")) { String 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 |