How to amend the FileEditor?? Custom FileEditor

Hey folks,

I've added an action to my File menu. When the user selects the action I have a fileEditor opening to some temp file (just because I needed a VirtualFile to be able to create a FileEditor)


This is done as follows.


public void actionPerformed(AnActionEvent e) {

Project project = (Project)e.getDataContext().getData( DataConstants.PROJECT );
FileEditorManager mgr = FileEditorManager.getInstance(project);
VirtualFileSystem fileSystem = LocalFileSystem.getInstance();
VirtualFile vFile = fileSystem.findFileByPath("C:/temp.txt");
Editor editor = mgr.openTextEditor(new OpenFileDescriptor(project, vFile),true);
Component comp = editor.getComponent();

// Add the textField.
JPanel pane = (JPanel)comp;
JTextField fitnesseURLField = new JTextField();
fitnesseURLField.setText("Enter valid fitnesseURL in here");
pane.add(fitnesseURLField);
}


Alas I'm trying to amend this Editor so that I can have a textField at the very top. Is this possible to do? Is there perhaps a subclass implementation of the editor interface I should look into extending?

Is there any discussion available on amending Editors? I see a lot for form generation but the standard TextEditor isn't really sufficient for the purpose of the project.

Thanks,
Mark.

0
5 comments

Hello Mark,

Alas I'm trying to amend this Editor so that I can have a textField at
the very top. Is this possible to do? Is there perhaps a subclass
implementation of the editor interface I should look into extending?


You should use FileEditorManager.addTopComponent() to add a custom component
to the editor. You should use FileEditorManagerListener to detect when editors
are opened or closed to find out when and to what editor your panel should
be added.

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


1

Hey Dmitry,

Thanks a million for your help Dmitry. I would have been here for a long time before I figured out how to do that.
If it's okay I'll just post the code up here in case anyone else is looking for a solution to the same problem. Would you know
if it's possible to open an editor without having to supply it with a file name. The editor is hopefully going to amend a URL as opposed
to a file on the system. Perhaps even if it could open to a temp file under the users .intellij directory in their home that could be deleted afterwards?
Would there be a constant name available for the user's home dir like $USER_DIR?

Thanks again for your help,
Mark.

public void actionPerformed(AnActionEvent e) {

Project project = (Project)e.getDataContext().getData( DataConstants.PROJECT );
FileEditorManager mgr = FileEditorManager.getInstance(project);
VirtualFileSystem fileSystem = LocalFileSystem.getInstance();
VirtualFile vFile = fileSystem.findFileByPath("C:/temp.txt");
FileEditor editor = mgr.openFile(vFile,true)[0];
Component comp = editor.getComponent();

JTextField fitnesseURLField = new JTextField();
fitnesseURLField.setText("Enter valid fitnesseURL in here");
mgr.addTopComponent(editor, fitnesseURLField);

}

1

Hey Dmitry,

I'm a little worried about the architecture that's growing around my custom editor.
The custom editor opens in response to the user selecting 'Open Editor...' menu option from the File Menu.
In the actionPerformed method of the action that handles this event I create an instance of a custom EditorListener class
passing it the AnActionEvent as it's the only way I can get access to the DataContext when a user performs an action in the custom
editor on one of the regular awt components I've introduced. I have a textfield at the top and a save button at the bottom with ActionListeners
for each. When a user performs an action on either, the singleton EditorListener is then called and uses the AnActionEvent it was initialised with
to be able to retrieve a handle to the VirtualFile. So you see of two layers of listener in order to handle events and I'm sure this is not the correct way
to go about it. Would you know how I associated the textfield and save button with a listener that will have access to the DataContext?

Thanks,
Mark.

0

Hello Mark,

I'm a little worried about the architecture that's
growing around my custom editor.
The custom editor opens in response to the user selecting 'Open
Editor...' menu option from the File Menu.
In the actionPerformed method of the action that handles this event I
create an instance of a custom EditorListener class
passing it the AnActionEvent as it's the only way I can get access to
the DataContext when a user performs an action in the custom
editor on one of the regular awt components I've introduced. I have a
textfield at the top and a save button at the bottom with
ActionListeners
for each. When a user performs an action on either, the singleton
EditorListener is then called and uses the AnActionEvent it was
initialised with
to be able to retrieve a handle to the VirtualFile. So you see of two
layers of listener in order to handle events and I'm sure this is not
the correct way
to go about it. Would you know how I associated the textfield and save
button with a listener that will have access to the DataContext?


You can use DataManager.getDataContext() to get the DataContext instance
fetched from a particular UI component.

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

Hey Dmitry,

How/where do I apply the helpful/correct answer detail?

Thanks,
Mark.

0

Please sign in to leave a comment.