简体   繁体   中英

Netbeans Platform Getting File Path in Editor's Action Extension

I've created an Action To HTML Editor and Added it to its Menu Context following the tutorial here

and now I have the following:

public final class PreviewAction implements ActionListener {

private final EditorCookie context;

public PreviewAction(EditorCookie context) {
    this.context = context;
}

@Override
public void actionPerformed(ActionEvent ev) {

}

The Action Provides me an EditorCookie but after doing some research I find out that it won't give me file path information and I should get it through a DataObject

I have a double click action somewhere else that opens HTML Files in Editor like this:

DataObject.find(FileUtil.toFileObject(filePath)).getLookup().lookup(OpenCookie.class).open();

so is there anyway that I can Access File Path in the my Editor's Action?
or maybe passing Editor's DataObject to the Action instead of EditorCookie?
or is there any kind of "linking" between Editor's Cookie and DataObject?

Any Help is Appreciated, Thanks in Advance.

Bingo!
I got it through Lookup that associated to the Current Activated TopComponent like this:

TopComponent activeTC = TopComponent.getRegistry().getActivated();
DataObject dataLookup = activeTC.getLookup().lookup(DataObject.class);
String filePath = FileUtil.toFile(dataLookup.getPrimaryFile()).getAbsolutePath();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM