1 19 package org.netbeans.modules.xml.core.actions; 20 21 import java.beans.PropertyChangeEvent ; 22 import java.beans.PropertyChangeListener ; 23 import java.io.IOException ; 24 import java.io.File ; 25 import java.text.MessageFormat ; 26 import java.util.*; 27 import java.net.*; 28 29 import org.openide.loaders.DataObject; 30 import org.openide.loaders.DataObjectNotFoundException; 31 import org.openide.text.Line; 32 import org.openide.filesystems.FileObject; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.filesystems.FileStateInvalidException; 35 import org.openide.filesystems.URLMapper; 36 import org.openide.*; 37 import org.openide.nodes.*; 38 import org.openide.cookies.*; 39 import org.openide.windows.*; 40 import org.openide.util.WeakSet; 41 42 import org.xml.sax.SAXParseException ; 43 44 import org.netbeans.api.xml.cookies.*; 45 import org.openide.text.Annotatable; 46 import org.openide.text.Annotation; 47 48 59 public final class InputOutputReporter implements CookieObserver { 60 61 private final String FORMAT = "{0} [{1}] {2}"; 64 private String ioName; 65 66 private DataObject dataObject; 67 68 private static final Set hyperlinks = 70 Collections.synchronizedSet(new WeakSet()); 72 77 public InputOutputReporter() { 78 this(Util.THIS.getString("TITLE_XML_check_window")); 79 } 80 81 public InputOutputReporter(String name) { 82 initInputOutput(name); 83 } 84 85 89 public void setNode(Node node) { 90 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("InputOutputReporter.setNode: " + node, new RuntimeException ("Who calls InputOutputReporter.setNode")); 91 92 dataObject = (DataObject) node.getCookie(DataObject.class); 93 } 94 95 98 private DataObject dataObject() { 99 return dataObject; 100 } 101 102 public void receive(CookieMessage msg) { 103 Object detail = msg.getDetail(XMLProcessorDetail.class); 104 105 if ( Util.THIS.isLoggable() ) { 106 Util.THIS.debug ("InputOutputReporter.receive:"); 107 Util.THIS.debug (" dataObject = " + dataObject); 108 Util.THIS.debug (" Message = " + msg); 109 Util.THIS.debug (" detail = " + detail); 110 if ( detail == null ) { 111 Util.THIS.debug (new RuntimeException ("Message's Detail is _null_!!!")); 112 } 113 } 114 115 if (detail instanceof XMLProcessorDetail) { 116 display(dataObject(), msg.getMessage(), (XMLProcessorDetail) detail); 117 } else { 118 message(msg.getMessage()); 119 } 120 } 121 122 123 126 public void message(String message) { 127 out().println(message); 128 } 129 130 133 public final void moveToFront() { 134 moveToFront(false); 135 } 136 137 141 public final void moveToFront(boolean lastMessage) { 142 boolean wasFocusTaken = tab().isFocusTaken(); 143 tab().select(); 144 tab().setFocusTaken(true); 145 out().write("\r"); 146 tab().setFocusTaken(wasFocusTaken); 147 if (lastMessage) { 148 out().close(); 149 } 150 } 151 152 153 private void display(DataObject dobj, String message, XMLProcessorDetail detail) { 154 157 DataObject actualDataObject = null; 158 try { 159 String systemId = detail.getSystemId(); 160 URL url = new URL (systemId); 161 FileObject fos = URLMapper.findFileObject(url); 162 if (fos != null) { 163 actualDataObject = DataObject.find(fos); 164 } 165 166 if ( Util.THIS.isLoggable() ) { 167 Util.THIS.debug ("InputOutputReporter.display: " + message); 168 Util.THIS.debug (" systemId = " + detail.getSystemId()); 169 Util.THIS.debug (" url = " + url); 170 Util.THIS.debug (" fos = " + fos); 171 } 172 } catch (MalformedURLException ex) { 173 if ( Util.THIS.isLoggable() ) Util.THIS.debug (ex); 175 } catch (DataObjectNotFoundException ex) { 176 if ( Util.THIS.isLoggable() ) Util.THIS.debug (ex); 178 } 179 180 182 String external = ""; 184 if (actualDataObject == null) { 185 external = detail.getSystemId(); 186 } 187 188 189 display ( 190 actualDataObject, message, external, 191 detail.getLineNumber(), 192 detail.getColumnNumber() 193 ); 194 } 195 196 197 198 private void display(DataObject dobj, String message, String ext, int line, int col) { 199 200 String text = null; 201 if (line >= 0) { 202 Object [] args = new Object [] { 203 message, 204 new Integer (line), 205 ext 206 }; 207 208 text = MessageFormat.format(FORMAT, args); 209 } else { 210 text = message; 212 } 213 214 if (dobj == null) { 215 out().println(text); } else { 217 try { 218 Hyperlink ec = new Hyperlink ( 219 text, 220 dobj, 221 Math.max(line - 1, 0), 222 Math.max(col - 1, 0) 223 ); 224 out().println(text, ec); 225 } catch (IOException catchIt) { 226 out().println(text); } 228 } 229 } 230 231 234 private void initInputOutput (String name) { 235 ioName = name; 236 tab().setFocusTaken (false); 237 238 try { 240 out().reset(); 241 } catch (IOException e) { 242 ErrorManager.getDefault().notify(e); 243 } 244 } 245 246 private OutputWriter out() { 247 return tab().getOut(); 248 } 249 250 private InputOutput tab() { 251 return IOProvider.getDefault().getIO(ioName, false); 252 } 253 254 257 public static void releaseAllAnnotations() { 258 synchronized (hyperlinks) { 259 Iterator it = hyperlinks.iterator(); 260 while (it.hasNext()) { 261 ((Hyperlink)it.next()).detach(); 262 } 263 hyperlinks.clear(); 264 } 265 } 266 267 private static class Hyperlink extends Annotation implements OutputListener, PropertyChangeListener { 268 269 270 private Line xline; 271 272 273 private int column; 274 275 private final String message; 276 277 public Hyperlink (String message, DataObject data, int line, int column) throws IOException { 278 this.column = column; 279 this.message = message; 280 LineCookie cookie = (LineCookie)data.getCookie(LineCookie.class); 281 if (cookie == null) { 282 throw new java.io.FileNotFoundException (); 283 } else { 284 xline = cookie.getLineSet ().getCurrent(line); 285 } 286 } 287 288 public void outputLineSelected (OutputEvent ev) { 289 try { 290 markError(); 291 show (Line.SHOW_TRY_SHOW); 292 } catch (IndexOutOfBoundsException ex) { 293 } catch (ClassCastException ex) { 294 } 296 } 297 298 public void outputLineAction (OutputEvent ev) { 299 try { 300 markError(); 301 show(Line.SHOW_GOTO); 302 } catch (IndexOutOfBoundsException ex) { 303 } catch (ClassCastException ex) { 304 } 306 } 307 308 public void outputLineCleared (OutputEvent ev) { 309 hyperlinks.remove(this); 310 detach(); 311 } 312 313 protected void notifyDetached(Annotatable ann) { 314 ann.removePropertyChangeListener(this); 315 } 316 317 protected void notifyAttached(Annotatable ann) { 318 ann.addPropertyChangeListener(this); 319 } 320 321 324 private Annotatable createAnnotatable() { 325 return xline; 327 } 332 333 private void show(int mode) { 335 if (column == -1) { 336 xline.show(mode); 337 } else { 338 xline.show(mode, column); 339 } 340 } 341 342 private void markError() { 344 releaseAllAnnotations(); 345 hyperlinks.add(this); 346 attach(createAnnotatable()); 347 } 348 349 354 public String getAnnotationType() { 355 return "org-netbeans-modules-xml-core-error"; } 357 358 361 public String getShortDescription() { 362 return message; 363 } 364 365 public void propertyChange(PropertyChangeEvent ev) { 367 String prop = ev.getPropertyName(); 368 if (prop == null || 369 prop.equals(Annotatable.PROP_TEXT) || 370 prop.equals(Annotatable.PROP_DELETED)) { 371 column = -1; 374 hyperlinks.remove(this); 375 detach(); 376 } 377 } 378 } 379 380 } 381 | Popular Tags |