Re: lookup confusing me
"Ingleby, Graeme" <[email protected]> Fri, 15 Sep 2017 05:39:45 +0000
| Newsgroups | gmane.comp.java.netbeans.modules.openide.devel |
|---|---|
| Message-ID | <45F55A2F088AA84FB3FBCEAE1C8EE9930195CCC1@SATLRCCDLMB514.delta.rl.delta.com> |
--_000_45F55A2F088AA84FB3FBCEAE1C8EE9930195CCC1SATLRCCDLMB514d_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
It's not clear exactly what you are trying to achieve when you say "all ope=
ned files".
As others have said, you don't seem to understand what NetBeans Lookups are=
or what they are used for so I would recommend reading up on Lookup.
If you understood Lookup, you would know that the global Lookup does not al=
low you to find all instances of a particular class type within your applic=
ation.
It's not really clear if you are trying to find all DataObject instances or=
if you are trying to find opened files that are associated with "editor" T=
op Components.
Neil provided a link to a Wiki page http://wiki.netbeans.org/DevFaqGetOpenE=
ditorWindows that shows you how you can determine which editors are open fr=
om which you can get the associated DataObject. If you are trying to find =
editor files - this is all you need.
That however would not find other DataObjects that were opened using the Fi=
leSystem/DataSystems API's directly e.g.
If you called DataObject dobj =3D DataObject.find(FileUtil.toFileObject(new=
File("/path/to/somefile"));
When a DataObject instance is created it is registered in a DataObjectPool =
unfortunately this pool is not public. You could however get the DataObjec=
ts registered in the pool using reflection. Using a dirty hack like this r=
eally isn't recommended and you really need to understand what you are doin=
g.
This approach would return ALL DataObject instances and not just DataObject=
instances that you opened (the platform opens many configuration files, ot=
her plugins could create DataObjects). Determining which DataObjects you a=
re truly interested in would be another problem for you to solve. Manipulat=
ing objects you did not create/own could have serious consequences!
For demonstration purposes only:
Field poolField =3D Class.forName("org.openide.loaders.DataObje=
ctPool").getDeclaredField("POOL");
poolField.setAccessible(true);
Object pool =3D poolField.get(null);
Field mapField =3D pool.getClass().getDeclaredField("map");
mapField.setAccessible(true);
Object map =3D mapField.get(pool);
Map<Object, Object> refMap =3D (Map<Object,Object>)map;
for(Object o: refMap.values()) {
Field objField =3D o.getClass().getDeclaredField("obj");
objField.setAccessible(true);
Reference<DataObject> result =3D (Reference<DataObject>)obj=
Field.get(o);
DataObject dataObject =3D (DataObject)result.get();
System.out.println(dataObject); // Here's the DataObject yo=
u want
}
The above code would print all DataObject instances - you could return them=
in a list, filter objects of interest etc.
Do you intend your modules to be used within the IDE or a custom NB Platfor=
m application where you might be able to customize your harness? If the la=
ter you could provide the functionality you need without using reflection b=
y accessing DataObjectPool and providing a public method to return the info=
rmation you need.
Hopefully you are only interested in Editor files and Neil has already prov=
ided the perfect solution for that.
Using non-public API's is a really bad idea, but sometimes the only way to =
achieve what you want (I've seen reflection used within NB sources).
Just make sure you fully understand the ramifications.
________________________________
From: Peter Cheung [[email protected]]
Sent: Thursday, September 14, 2017 12:39 PM
To: [email protected]
Subject: [platform-dev] lookup confusing me
Hi
I have trouble with lookup, the below code only print out the current fi=
le instead of all opened files.
Lookup lookup =3D Utilities.actionsGlobalContext();
Collection<? extends DataObject> list =3D lookup.lookupAll(DataObject.class=
);
for (DataObject dataObject : list) {
ModuleLib.log(dataObject);
}
If i use "Lookup.getDefault().lookupAll", looupAll() even returns nothing. =
Please help
Thanks
FromPeter
--_000_45F55A2F088AA84FB3FBCEAE1C8EE9930195CCC1SATLRCCDLMB514d_
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<html dir=3D"ltr">
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Diso-8859-=
1">
<style type=3D"text/css" style=3D"">=0A=
<!--=0A=
p=0A=
{margin-top:0;=0A=
margin-bottom:0}=0A=
-->=0A=
</style><style type=3D"text/css" id=3D"owaParaStyle"></style>
</head>
<body dir=3D"ltr" fpstyle=3D"1" ocsi=3D"0">
<div style=3D"direction: ltr;font-family: Tahoma;color: #000000;font-size: =
10pt;">
<div><span style=3D"font-size: 10pt;">It's not clear exactly what you are t=
rying to achieve when you say "all opened files". </span></=
div>
<div><br>
</div>
<div>As others have said, you don't seem to understand what NetBeans Lookup=
s are or what they are used for so I would recommend reading up on Lookup.<=
/div>
<div><span style=3D"font-size: 10pt;"><br>
</span></div>
<div><span style=3D"font-size: 10pt;">If you understood Lookup, you would k=
now that the global Lookup does not allow you to find all instances of a pa=
rticular class type within your application.</span></div>
<div><br>
</div>
<div>It's not really clear if you are trying to find all DataObject instanc=
es or if you are trying to find opened files that are associated with "=
;editor" Top Components.</div>
<div><br>
</div>
<div>Neil provided a link to a Wiki page <a href=3D"http://wiki.netbea=
ns.org/DevFaqGetOpenEditorWindows" target=3D"_blank" style=3D"font-size: 10=
pt; font-family: "Segoe UI", Helvetica, Arial, sans-serif;">http:=
//wiki.netbeans.org/DevFaqGetOpenEditorWindows</a><span style=3D"font-famil=
y: "Segoe UI", Helvetica, Arial, sans-serif; font-size: medium;">=
</span><span style=3D"font-size: 10pt;">that
shows you how you can determine which editors are open from which you can =
get the associated DataObject. If you are trying to find editor files=
- this is all you need.</span></div>
<div><span style=3D"font-size: 10pt;"><br>
</span></div>
<div><span style=3D"font-size: 10pt;">That however would not find other Dat=
aObjects that were opened using the FileSystem/DataSystems API's directly e=
.g. </span><span style=3D"font-size: 10pt;">  =
; </span></div>
<div><span style=3D"font-size: 10pt;"><br>
</span></div>
<div><span style=3D"font-size: 10pt;">If you called </span><span style=
=3D"font-size: 10pt;">DataObject dobj =3D DataObject.find(FileUtil.toFileOb=
ject(new File("/path/to/somefile"));</span></div>
<div><span style=3D"font-size: 10pt;"><br>
</span></div>
<div><span style=3D"font-size: 10pt;">When a DataObject instance is created=
it is registered in a DataObjectPool unfortunately this pool is not public=
. You could however get the DataObjects registered in the pool using =
reflection. </span><font size=3D"2">Using
a dirty hack like this really isn't recommended and you really need to und=
erstand what you are doing. </font></div>
<div><font size=3D"2"><br>
</font></div>
<div><font size=3D"2">This approach would return ALL DataObject instances a=
nd not just DataObject instances </font><span style=3D"font-size: 10pt=
;">that you opened (the platform opens many configuration files, other plug=
ins could create DataObjects). Determining
which DataObjects you are truly interested in would be another problem for=
you to solve. Manipulating objects you did not create/own could have serio=
us consequences!</span></div>
<div><br>
</div>
<div>For demonstration purposes only:</div>
<div><br>
</div>
<div>
<div> Field poolField =3D Class.fo=
rName("org.openide.loaders.DataObjectPool").getDeclaredField(&quo=
t;POOL");</div>
<div> poolField.setAccessible(true=
);</div>
<div> Object pool =3D poolField.ge=
t(null);</div>
<div> Field mapField =3D pool.getC=
lass().getDeclaredField("map");</div>
<div> mapField.setAccessible(true)=
;</div>
<div> Object map =3D mapField.get(=
pool); </div>
<div> Map<Object, Object> re=
fMap =3D (Map<Object,Object>)map;</div>
<div> for(Object o: refMap.values(=
)) {</div>
<div> Field objField=
=3D o.getClass().getDeclaredField("obj");</div>
<div> objField.setAc=
cessible(true);</div>
<div> Reference<D=
ataObject> result =3D (Reference<DataObject>)objField.get(o);</div=
>
<div> DataObject dat=
aObject =3D (DataObject)result.get();</div>
<div> System.out.pri=
ntln(dataObject); // Here's the DataObject you want</div>
<div> }</div>
</div>
<div><br>
</div>
<div>The above code would print all DataObject instances - you could return=
them in a list, filter objects of interest etc.</div>
<div><br>
</div>
<div>Do you intend your modules to be used within the IDE or a custom NB Pl=
atform application where you might be able to customize your harness?  =
;If the later you could provide the functionality you need without using re=
flection by accessing DataObjectPool
and providing a public method to return the information you need.</div>
<div><br>
</div>
<div>Hopefully you are only interested in Editor files and Neil has already=
provided the perfect solution for that. </div>
<div><br>
</div>
<div>Using non-public API's is a really bad idea, but sometimes the only wa=
y to achieve what you want <span style=3D"font-size: 10pt;">(I've seen=
reflection used within NB sources)</span><span style=3D"font-size: 10pt;">=
. </span></div>
<div><span style=3D"font-size: 10pt;"><br>
</span></div>
<div><span style=3D"font-size: 10pt;">Just make sure you fully understand t=
he ramifications.</span></div>
<div><br>
</div>
<div><br>
</div>
<div style=3D"font-family: Times New Roman; color: #000000; font-size: 16px=
">
<hr tabindex=3D"-1">
<div id=3D"divRpF972446" style=3D"direction: ltr;"><font face=3D"Tahoma" si=
ze=3D"2" color=3D"#000000"><b>From:</b> Peter Cheung [[email protected]=
]<br>
<b>Sent:</b> Thursday, September 14, 2017 12:39 PM<br>
<b>To:</b> [email protected]<br>
<b>Subject:</b> [platform-dev] lookup confusing me<br>
</font><br>
</div>
<div></div>
<div>
<div style=3D"font-family:Calibri,Helvetica,sans-serif; font-size:12pt; col=
or:rgb(0,0,0)">
<div>Hi</div>
<div> I have trouble with lookup, the below code only print out=
the current file instead of all opened files.</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre">Lookup lookup=
=3D Utilities.actionsGlobalContext();</span></div>
<div><br>
</div>
<div>Collection<? extends DataObject> list =3D lookup.lookupAll(DataO=
bject.class);</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>for (D=
ataObject dataObject : list) {</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>Module=
Lib.log(dataObject);</div>
<div><span class=3D"Apple-tab-span" style=3D"white-space:pre"></span>}</div=
>
<div><br>
</div>
<div>If i use "<span style=3D"color:rgb(0,0,0); font-family:Calibri,He=
lvetica,sans-serif; font-size:16px; font-style:normal">Lookup.getDefault().=
lookupAll", looupAll() even returns nothing. Please help</span></div>
<div><span style=3D"color:rgb(0,0,0); font-family:Calibri,Helvetica,sans-se=
rif; font-size:16px; font-style:normal"><br>
</span></div>
<div><span style=3D"color:rgb(0,0,0); font-family:Calibri,Helvetica,sans-se=
rif; font-size:16px; font-style:normal">Thanks</span></div>
<div>FromPeter</div>
<br>
</div>
</div>
</div>
</div>
</body>
</html>
--_000_45F55A2F088AA84FB3FBCEAE1C8EE9930195CCC1SATLRCCDLMB514d_--