Rough guide to xml gui editor type plugin?
I'd like to write a plugin that does allows me to show an graphical (jgraph) view of an xml file. The file would be editable both as xml, and within the graph. Can someone let me know what the very broad strokes/rough outline of how to get started? Any other similar plugins I could mug?
Please sign in to leave a comment.
Look like you need to write custom editor. Custom editor, for example, is realized in Images plugin, sources of Images plugins you can find in Development Package.
Ok, I'll look into that. I imagine/hope it'd also have support for viewing the file 'normally', ie as XML, without the custom editor?
Something like how the form designer is viewed in package viewer, one parent node with two subnodes denoting different views.
Hi,
Below is sample code for editor provider.
public class XXX implements ApplicationComponent,FileEditorProvider
{
public void initComponent()
}
public void disposeComponent()
{
}
public String getComponentName()
{
return "XXX";
}
public boolean accept(Project project, VirtualFile file)
{
return file.getFileType() == StdFileTypes.HTML;
}
public FileEditor createEditor(Project project, final VirtualFile file)
{
return new XXXEditor(project, file);
}
public void disposeEditor(FileEditor editor)
{
}
public FileEditorState readState(Element sourceElement, Project
project, VirtualFile file)
{
return DummyFileEditorState.DUMMY;
}
public void writeState(FileEditorState state, Project project,
Element targetElement)
{
}
@NonNls
public String getEditorTypeId()
{
return getComponentName();
}
public FileEditorPolicy getPolicy()
{
return FileEditorPolicy.NONE;
}
private static class DummyFileEditorState implements FileEditorState
{
public static final FileEditorState DUMMY = new DummyFileEditorState();
public boolean canBeMergedWith(FileEditorState otherState,
FileEditorStateLevel level)
{
return false;
}
}
private class XXXEditor implements FileEditor,
FileEditorManagerListener, WebBrowserEventsHandler
{
JPanel mainPanel, browserPanel;
org.jdesktop.jdic.browser.WebBrowser browser;
private final VirtualFile file;
private final Project project;
public XXXEditor(Project project, VirtualFile file)
{
this.file = file;
this.project = project;
mainPanel = new JPanel(new BorderLayout());
browserPanel = new JPanel(new BorderLayout());
try
{
browser = new org.jdesktop.jdic.browser.WebBrowser( );
}
catch (Exception e)
{
return;
}
browserPanel.add(browser, BorderLayout.CENTER);
//browser.setEventHandler(this);
mainPanel.add(browserPanel, BorderLayout.CENTER);
browserPanel.setBorder(BorderFactory.createEtchedBorder());
FileEditorManager.getInstance(project).addFileEditorManagerListener(this);
}
/**
@return component which represents editor in the UI.
The method should never return <code>null</code>.
*/
public JComponent getComponent()
{
return mainPanel;
}
/**
Returns component to be focused when editor is opened. Method
should never return null.
*/
public JComponent getPreferredFocusedComponent()
{
return mainPanel;
}
/**
@return editor's name, a string that identifies editor among
other editors. For example, UI form might have two
editor: "GUI Designer"
and "Text". So "GUI Designer" can be a name of one
editor and "Text"
can be a name of other editor. The method should never
return null]]>.
*/
@NonNls
public String getName()
{
return "Preview";
}
/**
@return editor's internal state. Method should never return
null]]>.
*/
public FileEditorState getState(FileEditorStateLevel level)
{
return DummyFileEditorState.DUMMY;
}
/**
Applies given state to the editor.
*
@param state cannot be null
*/
public void setState(FileEditorState state)
{
}
/**
@return whether the editor's content is modified in comparision
with its file.
*/
public boolean isModified()
{
return false;
}
/**
@return whether the editor is valid or not. For some reasons
editor can become invalid. For example, text editor
becomes invalid when its file is deleted.
*/
public boolean isValid()
{
return true;
}
/**
This method is invoked each time when the editor is selected.
This can happen in two cases: editor is selected because the
selected file
has been changed or editor for the selected file has been changed.
*/
public void selectNotify()
{
}
private void reloadContent()
{
FileDocumentManager fileDocManager =
FileDocumentManager.getInstance();
Document doc = fileDocManager.getDocument(file);
fileDocManager.saveDocument( doc );
try
{
browser.setURL ( new URL ( file.getUrl() ) );
}
catch (MalformedURLException e)
{
}
}
/**
This method is invoked each time when the editor is deselected.
*/
public void deselectNotify()
{
}
/**
Removes specified listener
*
@param listener to be added
*/
public void addPropertyChangeListener(PropertyChangeListener listener)
{
}
/**
Adds specified listener
*
@param listener to be removed
*/
public void removePropertyChangeListener(PropertyChangeListener
listener)
{
}
/**
@return highlighter object to perform background analysis and
highlighting activities.
Return <code>null</code> if no background highlighting
activity necessary for this file editor.
*/
public BackgroundEditorHighlighter getBackgroundHighlighter()
{
return null;
}
/**
The method is optional. Currently is used only by find usages
subsystem
*
@return the location of user focus. Typically it's a caret or
any other form of selection start.
*/
public FileEditorLocation getCurrentLocation()
{
return null;
}
public StructureViewBuilder getStructureViewBuilder()
{
return null;
}
public T getUserData(Key key) { return null; } public void putUserData(Key]]> key, T value)
{
}
/**
Called before the text of the document is changed.
*
@param event the event containing the information about the change.
*/
public void beforeDocumentChange(DocumentEvent event)
{
}
/**
Called after the text of the document has been changed.
*
@param event the event containing the information about the change.
*/
public void documentChanged(DocumentEvent event)
{
//reloadContent();
}
/**
write javadoc
*/
public void fileOpened(FileEditorManager source, VirtualFile file)
{
}
/**
write javadoc
*/
public void fileClosed(FileEditorManager source, VirtualFile file)
{
}
/**
*
*/
public void selectionChanged(FileEditorManagerEvent event)
{
if (
FileEditorManager.getInstance(project).getSelectedEditor(file) == this )
reloadContent ();
}
public boolean beforeNavigate(WebBrowser webBrowser, final String
url, String string1, String string2, String string3)
{
final VirtualFile target =
ApplicationManager.getApplication().runReadAction( new
Computable]]> () {
public VirtualFile compute()
{
String _url = url.replace(File.separatorChar,'/');
if ( file.getPath().equals(_url) )
return null;
return LocalFileSystem.getInstance().findFileByPath( _url );
}
} );
if ( target == null )
return false;
else
{
ApplicationManager.getApplication().invokeLater( new Runnable()
{
public void run()
{
FileEditorManager.getInstance(project).openFile(target,true);
}
} );
return true;
}
}
public boolean beforeFileDownload()
{
return false;
}
public boolean windowClosing(boolean b)
{
return false;
}
public Dimension clientAreaSizeRequested(Dimension dimension)
{
return null;
}
public boolean navigationErrorOccured(WebBrowser webBrowser, String
string, String string1, StatusCode statusCode)
{
return false;
}
}
}
Hani Suleiman wrote:
--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"
Very cool, thanks! Now if only there were a good online reference for this sort of thing...
Since this page is linked from your FAQ, is there any possibility to get a link to a plugin from which one can study a similar solution? I cannot find anything for Images plugin as suggested in the first answer and that answer was from 2006, so maybe it does not exists.
So, please can you post any ref to a GitHub repository with such a plugin?
我参考了intelli-community的源代码, 写一个简单的自定义的编辑器, 已经提交到github上了, 下面是链接地址:
I refer to the source code of intelli-community, write a simple custom editor, which has been submitted to github. Here is the link address:
https://github.com/liang0-0/custom-editor