KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > rubyproject > execution > OutputProcessor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.ruby.rubyproject.execution;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 import javax.swing.SwingUtilities JavaDoc;
25 import javax.swing.text.Document JavaDoc;
26
27 import org.openide.ErrorManager;
28 import org.openide.cookies.EditorCookie;
29 import org.openide.cookies.LineCookie;
30 import org.openide.cookies.OpenCookie;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileUtil;
33 import org.openide.loaders.DataObject;
34 import org.openide.text.Line;
35 import org.openide.windows.OutputEvent;
36 import org.openide.windows.OutputListener;
37
38 /**
39  * An OutputProcessor takes filename and lineno information
40  * and produces hyperlinks. Actually resolving filenames
41  * into real FileObjects is done lazily via user-supplied
42  * FileLocators when the links are actually clicked.
43  *
44  * @todo Rename me!
45  * @author Tor Norbye
46  */

47 class OutputProcessor implements OutputListener {
48     private final String JavaDoc file;
49     private final int lineno;
50     private final FileLocator fileLocator;
51
52     OutputProcessor(String JavaDoc file, int lineno, FileLocator fileLocator) {
53         if (lineno < 0) {
54             lineno = 0;
55         }
56
57         // TODO : columns?
58
this.file = file;
59         this.lineno = lineno;
60         this.fileLocator = fileLocator;
61     }
62
63     public void outputLineSelected(OutputEvent ev) {
64         //throw new UnsupportedOperationException("Not supported yet.");
65
}
66
67     public void outputLineAction(OutputEvent ev) {
68         // Find file such and such and warp to it
69
FileObject fo = findFile(file);
70
71         if (fo != null) {
72             open(fo, lineno);
73         }
74     }
75
76     private FileObject findFile(String JavaDoc file) {
77         if (fileLocator != null) {
78             FileObject fo = fileLocator.find(file);
79
80             if (fo != null) {
81                 return fo;
82             }
83         }
84
85         // Perhaps it's an absolute path of some sort... try to resolve those
86
// Absolute path? Happens for stack traces in JRuby libraries and such
87
return FileUtil.toFileObject(new File JavaDoc(file));
88     }
89
90     public void outputLineCleared(OutputEvent ev) {
91     }
92
93     public static boolean open(final FileObject fo, final int lineno) {
94         if (!SwingUtilities.isEventDispatchThread()) {
95             SwingUtilities.invokeLater(new Runnable JavaDoc() {
96                     public void run() {
97                         open(fo, lineno);
98                     }
99                 });
100
101             return true; // not exactly accurate, but....
102
}
103
104         try {
105             DataObject od = DataObject.find(fo);
106             EditorCookie ec = (EditorCookie)od.getCookie(EditorCookie.class);
107             LineCookie lc = (LineCookie)od.getCookie(LineCookie.class);
108
109             if ((ec != null) && (lc != null)) {
110                 Document JavaDoc doc = ec.openDocument();
111
112                 if (doc != null) {
113                     int line = lineno;
114
115                     if (line < 1) {
116                         line = 1;
117                     }
118
119                     Line l = lc.getLineSet().getCurrent(line - 1);
120
121                     if (l != null) {
122                         l.show(Line.SHOW_GOTO);
123
124                         return true;
125                     }
126                 }
127             }
128
129             OpenCookie oc = (OpenCookie)od.getCookie(OpenCookie.class);
130
131             if (oc != null) {
132                 oc.open();
133
134                 return true;
135             }
136         } catch (IOException JavaDoc e) {
137             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
138         }
139
140         return false;
141     }
142 }
143
Popular Tags