A plug-in was developed some days ago. The initial development of the Eclipse plug-in is not very easy. Now the project is nearing the end, so I will write a little bit of experience for your reference and others.
This project is developed for the event tree. That is, you need to implement operations such as creating, modifying, adding, deleting, and calculating the probability of an event tree. Considering that everything starts from the bottom layer, it is very troublesome. So I checked some information and learned that jgraph and GEF can be used to draw graphics, so I chose jgraph.
I have extended the eclipse editor and view, because the GUI on the editor is based on SWT, and jgraph is based on Swing, so I used the bridge to make it put on the jpanel. (The Code is as follows)
Composite container = new composite (parent, SWT. Embedded );
Container. setlayout (New filllayout (SWT. Vertical ));
Final frame = swt_awt.new_frame (container );
System. setproperty ("Sun. java2d. d3d", "false ");
Frame. Add (New paintingview ());
Then, you added a toolbar and buttons at the top. It involves a lot of jgraph things. Unfortunately, on the Internet, there are not many articles about jgraph in books, and many places need to explore and study on their own. Fortunately, some netizens wrote a lot of experiences and experiences on their own fights, which is of great help to me ~~
I will pick up some difficult points, or something I want to solve for a long time. I will discuss it with you. Maybe it's through the bridge. I double-click the jgraph cell and will jump out of the window, it can be modified, but I run it in the form of Eclipse plug-in, but it jumps out of a window that cannot be edited. Why? I guess it is still related to the bridge. With the help of my classmates, I solved this problem. (The Code is as follows)
Public class mygraph extends jgraph {
......
@ Suppresswarnings ("unchecked ")
Protected void completeediting (Boolean messagestop,
Boolean messagecancel,
Boolean messagegraph ){
If (stopeditingincompleteediting & editingcomponent! = NULL &&
Editdialog! = NULL ){
Component oldcomponent = editingcomponent;
Object oldcell = editingcell;
Graphcelleditor oldeditor = celleditor;
Object newvalue = oldeditor. getcelleditorvalue ();
// If (double. isnan (v ))
Rectangle2d editingbounds = graph. getcellbounds (editingcell );
Boolean requestfocus = (graph! = NULL &&
(Graph. hasfocus () | editingcomponent. hasfocus ()));
Editingcell = NULL;
Editingcomponent = NULL;
If (messagestop ){
Oldeditor. stopcellediting ();
}
Else if (messagecancel ){
Oldeditor. cancelcellediting ();
}
Editdialog. Dispose ();
If (requestfocus)
Graph. requestfocus ();
If (messagegraph ){
.... // Add the operation you need.
}
}
}
Protected Boolean startediting (Object cell, mouseevent event ){
If (graph. iscelleditable (cell) & editdialog = NULL ){
If (graph. GetModel (). isedge (cell )){
Name = false;
}
Else {
Name = true;
}
Cellview TMP = graphlayoutcache. getmapping (cell, false );
Celleditor = TMP. geteditor ();
Editingcomponent = celleditor. getgraphcelleditorcomponent (graph, cell,
Graph. iscellselected (cell ));
If (celleditor. iscelleditable (event )){
System. Out. println ("zxc1 ");
Editingcell = cell;
Dimension editorsize = editingcomponent. getpreferredsize ();
Editdialog = new jframe ("edit" + graph. convertvaluetostring (cell ));
// New jdialog (myframe, "edit" + graph. convertvaluetostring (cell), true );
Editdialog. setsize (editorsize. Width, editorsize. Height );
Editdialog. getcontentpane (). Add (editingcomponent );
Editingcomponent. Validate ();
Editdialog. Pack ();
Editdialog. Show ();
// Add editor listener
If (celleditorlistener = NULL)
Celleditorlistener = createcelleditorlistener ();
If (celleditor! = NULL & celleditorlistener! = NULL)
Celleditor. addcelleditorlistener (celleditorlistener );
If (celleditor. shouldselectcell (event )){
Stopeditingincompleteediting = false;
Try {
Graph. setselectioncell (cell );
} Catch (exception e ){
System. Err. println ("editing exception:" + E );
}
Stopeditingincompleteediting = true;
}
If (event instanceof mouseevent ){
Point componentpoint = swingutilities. convertpoint
(Graph, new point (event. getx (), event. Gety ()),
Editingcomponent );
Component activecomponent = swingutilities.
Getdeepestcomponentat (editingcomponent,
Componentpoint. X, componentpoint. y );
If (activecomponent! = NULL ){
New mouseinputhandler (graph, activecomponent, event );
}
}
Return true;
}
Else {
Editingcomponent = NULL;
Return true;
}
}
Else {
Return false;
}
}
.....
}
In this case, you double-click the cell and call the startediting method to determine whether the cell is located. Then, the completeediting method is called.
Jgraph also implements the image export function (the Code is as follows)
Fileoutputstream out = new fileoutputstream (Nn + ". jpg ");
Color BG = graph. getbackground ();
Bufferedimage IMG = graph. getimage (BG, 0 );
ImageIO. Write (IMG, "PNG", out );
Later, I encountered a problem, that is, how to click a button on the toolbar to interact with the view class, because I want to calculate the probability and display the result on the View class. After searching for a while on the internet, I often see things that are related to the class that inherits viewpart, and I am a bridge jpanel, which is often unavailable. Later, I had to set the method of the View class to static, so that I could call it. I also need to pay attention to the problem that SWT cannot support two threads to modify the interface at the same time, the code for solving this problem is as follows.
Display. getdefault (). syncexec (New runnable (){
Public void run (){
..... // The operation to be performed
}
}
)
Now there are so many problems that I think jgraph is still good in Java graphics development. It would be better if it can support the popular SWT!