GEF (graphical Editor Framework) Eclipse Project Starter series (3)---draw2d example demo

Source: Internet
Author: User
Tags event listener

In the "GEF (graphical Editor Framework) Eclipse Project Starter series (2)---draw2d development environment" article, I give you an introduction to the draw2d development environment of the building. Next, according to the software industry practice, need to show an example, so that everyone is more interested in learning and explore the technology. Well, nonsense, the author Jiehuaxianfo, using Dan Rubel,jaimen Wren and Eric Clayberg An example of draw2d example and share it with you. This example includes two classes, Genealogyview and Figuremover. Where Genealogyview is a program to draw a three-port relationship, Figuremover is a mouse event listener, through which the user can drag a three-port position. The effect of the program after running is as follows.




Note that after running, the position of the top three squares can be changed by dragging and dragging the mouse.


The specific program code is as follows:

(1) Genealogyview class: Main program entrance

Import Org.eclipse.draw2d.*;import Org.eclipse.draw2d.geometry.*;import Org.eclipse.swt.swt;import Org.eclipse.swt.layout.griddata;import Org.eclipse.swt.layout.gridlayout;import Org.eclipse.swt.widgets.Canvas; Import Org.eclipse.swt.widgets.composite;import Org.eclipse.swt.widgets.display;import Org.eclipse.swt.widgets.shell;public class Genealogyview {private Connection connect (ifigure figure1, Ifigure figure2) {polylineconnection connection = new Polylineconnection (); Connection.setsourceanchor (new Chopboxanchor (Figure1)); Connection.settargetanchor (New Chopboxanchor (Figure2)); return connection;} Private Ifigure Createmarriagefigure () {Rectangle r = new Rectangle (0, 0, 50, 50); Polygonshape polygonshape = new Polygonshape ();/** * top:25,0 left:0,25 bottom:25,50 right:5 0,25 */system.out.println ("Top:" +r.gettop (). x+ "," +r.gettop (). y); System.out.println ("Left:" +r.getleft (). x+ "," +r.getleft (). y); System.out.println ("Bottom:" +r.getbottom (). x+ "," +r.getbottom (). y); SysTem.out.println ("Right:" +r.getright (). x+ "," +r.getright (). Y);p Olygonshape.setstart (R.gettop ()); Polygonshape.addpoint (R.gettop ());p Olygonshape.addpoint (R.getleft ());p Olygonshape.addpoint (R.getBottom ()); Polygonshape.addpoint (R.getright ());p Olygonshape.addpoint (R.gettop ());p Olygonshape.setend (R.getTop ()); Polygonshape.setfill (True);p Olygonshape.setbackgroundcolor (Colorconstants.lightgray); Polygonshape.setpreferredsize (R.getsize ()); new Figuremover (Polygonshape); return polygonshape;} Private Ifigure createpersonfigure (String name) {rectanglefigure rectanglefigure = new Rectanglefigure (); Rectanglefigure.setbackgroundcolor (Colorconstants.lightgray); Rectanglefigure.setlayoutmanager (New ToolbarLayout ()); Rectanglefigure.setpreferredsize (+), Rectanglefigure.add (new Label), New Figuremover ( rectanglefigure); return rectanglefigure;} Private Canvas Creatediagram (Composite parent) {//Create a root figure and simple layout to contain//all other FIGURESFI Gure root = new figure (); Root.setfont (PARent.getfont ()); Xylayout layout = new Xylayout (); Root.setlayoutmanager (layout);//Add the father andyifigure andy = createpersonfigure ("A Ndy "); Root.add (Andy), Layout.setconstraint (Andy,new Rectangle (new Point), Andy.getpreferredsize ());//Add the Mother "Betty" Ifigure betty = Createpersonfigure ("Betty"); Root.add (Betty); Layout.setconstraint (Betty,new Rectangle ( New Point (+), betty.getpreferredsize ());//Add the Son "Carl" ifigure carl = Createpersonfigure ("Carl"); Root.add ( Carl); Layout.setconstraint (Carl,new Rectangle (new Point), Carl.getpreferredsize ());//added marriage Relationshipifigure marriage = Createmarriagefigure () root.add (marriage,new Rectangle (new Point (145, 35), Marriage.getpreferredsize ());//connect persons as Marriage Relationshiproot.add (Connect (Andy, Marriage)); Root.add ( Connect (Betty, marriage)); Root.add (Connect (Carl, marriage));//Create a canvas to display the root Figurecanvas canvas = n EW Canvas (parent, SWT. double_buffered); Canvas.setbackground (Colorconstants.cyan); Lightweightsystem LWS = new Lightweightsystem (canvas); lws.setcontents (root); return canvas;} private void Run () {Shell Shell = new Shell (new Display ()); Shell.setsize (365, 280); Shell.settext ("genealogy"); Shell.setlayout (New GridLayout ()); Canvas canvas = creatediagram (shell); Canvas.setlayoutdata (new Griddata (Griddata.fill_both));D isplay Display = Shell.getdisplay (); Shell.open (); while (!shell.isdisposed ()) {while (!display.readanddispatch ()) {display.sleep ();}}} public static void Main (string[] args) {new Genealogyview (). Run ();}}

(2) Figuremover class: Mouse Drag-and-drop program monitoring

Import Org.eclipse.draw2d.*;import org.eclipse.draw2d.geometry.*;/** * A draw2d mouse listener for dragging figures Aroun d the diagram. Listeners such as this * is useful for manipulating draw2d diagrams, but is superseded by higher level GEF * Functionali Ty if the GEF framework is used. */public class Figuremoverimplements MouseListener, mousemotionlistener{private final ifigure figure;private Point location;/** * Construct A new instance for dragging the specified figure around the diagram. * * @param figure of the figure to be dragged. */public Figuremover (ifigure) {this.figure = Figure;figure.addmouselistener (this); Figure.addmousemotionlistener (this);} /** * Cache the mouse down location and mark the event as consumed so, the draw2d * Event dispatcher'll send all MoU SE events to the figure associated with this * listener until the mouse button is released. */public void mousepressed (MouseEvent event) {location = Event.getlocation (); Event.consume ();} /** * Process Mouse DRag events by moving the associated figure and updating the * appropriate figure in the diagram. */public void Mousedragged (MouseEvent event) {if (location = = null) return; Point newlocation = Event.getlocation (); if (newlocation = = null) return;dimension offset = newlocation.getdifference ( Location), if (Offset.width = = 0 && Offset.height = = 0) return;location = newlocation; Updatemanager updatemgr = Figure.getupdatemanager (); LayoutManager layoutmgr = Figure.getparent (). Getlayoutmanager (); Rectangle bounds = Figure.getbounds () updatemgr.adddirtyregion (Figure.getparent (), bounds);//Copy the Rectangle using Getcopy () to prevent undesired side-effectsbounds = Bounds.getcopy (). Translate (offset.width, offset.height); Layoutmgr.setconstraint (figure, bounds); Figure.translate (Offset.width, offset.height); Updatemgr.adddirtyregion ( Figure.getparent (), bounds); Event.consume ();} /** * Clear The last cached mouse location signaling the end of the drag figure * operation. */public void mousereleased (MOuseevent event) {if (location = = null) Return;location = Null;event.consume ();} public void Mousemoved (MouseEvent event) {}public void mousedoubleclicked (MouseEvent event) {}public void mouseentered ( MouseEvent event) {}public void mouseexited (MouseEvent event) {}public void MouseHover (MouseEvent event) {}}

in the chapter, I will give you the code of this demo program. Please pay attention and look forward to ....



GEF (graphical Editor Framework) Eclipse Project Starter series (3)---draw2d example demo

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.