Added
Link Here
|
1 |
package org.netbeans.modules.gsf.api; |
2 |
|
3 |
import java.io.IOException; |
4 |
import java.util.logging.Level; |
5 |
import java.util.logging.Logger; |
6 |
import javax.swing.JEditorPane; |
7 |
import javax.swing.text.Document; |
8 |
import javax.swing.text.JTextComponent; |
9 |
import org.openide.ErrorManager; |
10 |
import org.openide.cookies.EditorCookie; |
11 |
import org.openide.cookies.InstanceCookie; |
12 |
import org.openide.cookies.LineCookie; |
13 |
import org.openide.filesystems.FileObject; |
14 |
import org.openide.filesystems.FileUtil; |
15 |
import org.openide.loaders.DataObject; |
16 |
import org.openide.loaders.DataObjectNotFoundException; |
17 |
import org.openide.text.Line; |
18 |
import org.openide.util.Exceptions; |
19 |
import org.openide.util.Lookup; |
20 |
|
21 |
/** |
22 |
* Provides the FileObject from a Document. Normally this means getting the info |
23 |
* from the Document's stream, but other implementations might exist. |
24 |
* |
25 |
* @author Emilian Bold |
26 |
*/ |
27 |
public abstract class DataLoadersBridge { |
28 |
|
29 |
private static Logger log = Logger.getLogger(DataLoadersBridge.class.getName()); |
30 |
|
31 |
public abstract Object createInstance(FileObject file); |
32 |
|
33 |
public abstract Document getDocument(FileObject fo); |
34 |
|
35 |
/** |
36 |
* @return Text of the given line in the document |
37 |
*/ |
38 |
public abstract String getLine(Document doc, int lineNumber); |
39 |
|
40 |
public abstract JEditorPane[] getOpenedPanes(FileObject fo); |
41 |
|
42 |
public abstract FileObject getFileObject(Document doc); |
43 |
|
44 |
public FileObject getFileObject(JTextComponent text) { |
45 |
return getFileObject(text.getDocument()); |
46 |
} |
47 |
//--- |
48 |
private static DataLoadersBridge DEFAULT_ADAPTOR = new DataLoadersBridge() { |
49 |
|
50 |
@Override |
51 |
public FileObject getFileObject(Document doc) { |
52 |
Object o = doc.getProperty(Document.StreamDescriptionProperty); |
53 |
if (o instanceof DataObject) { |
54 |
return ((DataObject) o).getPrimaryFile(); |
55 |
} else if (o instanceof FileObject) { |
56 |
return (FileObject) o; |
57 |
} else if (o != null) { |
58 |
log.warning("Unable to return FileObject for Document " + doc + ". StreamDescriptionProperty points to non-DataLoader, non-FileObject instace: " + o); |
59 |
} |
60 |
return null; |
61 |
} |
62 |
|
63 |
@Override |
64 |
public Document getDocument(FileObject file) { |
65 |
try { |
66 |
DataObject d = DataObject.find(file); |
67 |
EditorCookie ec = (EditorCookie) d.getCookie(EditorCookie.class); |
68 |
|
69 |
if (ec == null) { |
70 |
return null; |
71 |
} |
72 |
return ec.getDocument(); |
73 |
} catch (IOException e) { |
74 |
Logger.global.log(Level.INFO, "SemanticHighlighter: Cannot find DataObject for file: " + FileUtil.getFileDisplayName(file), e); |
75 |
return null; |
76 |
} |
77 |
} |
78 |
|
79 |
@Override |
80 |
public JEditorPane[] getOpenedPanes(FileObject fo) { |
81 |
DataObject dobj; |
82 |
try { |
83 |
dobj = DataObject.find(fo); |
84 |
} catch (DataObjectNotFoundException ex) { |
85 |
return new JEditorPane[0]; |
86 |
} |
87 |
|
88 |
EditorCookie editorCookie = dobj.getCookie(EditorCookie.class); |
89 |
|
90 |
if (editorCookie == null) { |
91 |
return new JEditorPane[0]; |
92 |
} |
93 |
|
94 |
return editorCookie.getOpenedPanes(); |
95 |
} |
96 |
|
97 |
@Override |
98 |
public Object createInstance(FileObject file) { |
99 |
assert file.getExt().equals("instance"); // NOI18N |
100 |
// Construct the service lazily using the instance cookie on the provided data object |
101 |
try { |
102 |
DataObject dobj = DataObject.find(file); |
103 |
InstanceCookie ic = dobj.getCookie(InstanceCookie.class); |
104 |
return ic.instanceCreate(); |
105 |
} catch (ClassNotFoundException e) { |
106 |
ErrorManager.getDefault().notify(e); |
107 |
} catch (DataObjectNotFoundException e) { |
108 |
ErrorManager.getDefault().notify(e); |
109 |
} catch (IOException e) { |
110 |
ErrorManager.getDefault().notify(e); |
111 |
} |
112 |
return null; |
113 |
} |
114 |
|
115 |
@Override |
116 |
public FileObject getPrimaryFile(FileObject fileObject) { |
117 |
try { |
118 |
DataObject dobj = DataObject.find(fileObject); |
119 |
|
120 |
if (dobj != null) { |
121 |
return dobj.getPrimaryFile(); |
122 |
} |
123 |
return null; |
124 |
} catch (DataObjectNotFoundException ex) { |
125 |
Exceptions.printStackTrace(ex); |
126 |
return null; |
127 |
} |
128 |
} |
129 |
|
130 |
@Override |
131 |
public String getLine(Document doc, int lineNumber) { |
132 |
DataObject dObj = (DataObject) doc.getProperty(doc.StreamDescriptionProperty); |
133 |
LineCookie lc = dObj.getCookie(LineCookie.class); |
134 |
Line line = lc.getLineSet().getCurrent(lineNumber); |
135 |
|
136 |
return line.getText(); |
137 |
} |
138 |
}; |
139 |
private static DataLoadersBridge instance = null; |
140 |
|
141 |
public synchronized static DataLoadersBridge getDefault() { |
142 |
if (instance == null) { |
143 |
instance = Lookup.getDefault().lookup(DataLoadersBridge.class); |
144 |
//TODO: listen on the lookup ? Seems too much |
145 |
if (instance == null) { |
146 |
instance = DEFAULT_ADAPTOR; |
147 |
} |
148 |
} |
149 |
return instance; |
150 |
} |
151 |
|
152 |
public abstract FileObject getPrimaryFile(FileObject fileObject); |
153 |
} |