packagedev.equo.draw2d;importorg.eclipse.swt.layout.FillLayout;importorg.eclipse.swt.widgets.Display;importorg.eclipse.swt.widgets.Shell;importorg.eclipse.draw2d.FigureCanvas;importorg.eclipse.draw2d.Label;/** * Demonstrates the simplest Draw2D example using a FigureCanvas and Label. * This example creates a basic Draw2D application that displays "Hello World" * using Draw2D's figure system on a canvas. */publicclassDraw2DHelloWorld{publicstaticvoidmain(String[]args){Displayd=newDisplay();Shellshell=newShell(d);shell.setLayout(newFillLayout());FigureCanvascanvas=newFigureCanvas(shell);canvas.setContents(newLabel("Hello World"));//$NON-NLS-1$shell.setText("Draw2d");//$NON-NLS-1$shell.setSize(200,100);shell.open();while(!shell.isDisposed()){while(!d.readAndDispatch()){d.sleep();}}}}
packagedev.equo.draw2d;importorg.eclipse.draw2d.Figure;importorg.eclipse.draw2d.FigureCanvas;importorg.eclipse.draw2d.Graphics;importorg.eclipse.draw2d.ScaledGraphics;importorg.eclipse.swt.layout.FillLayout;importorg.eclipse.swt.widgets.Display;importorg.eclipse.swt.widgets.Shell;publicclassDraw2DHelloWorldScaledGraphics{staticclassScaledStringFigureextendsFigure{privateStringtext;privatedoublescale;publicScaledStringFigure(Stringtext,doublescale){this.text=text;this.scale=scale;}@OverrideprotectedvoidpaintFigure(Graphicsgraphics){super.paintFigure(graphics);// Create a ScaledGraphics to force the flow through ScaledGraphics#drawStringScaledGraphicsscaledGraphics=newScaledGraphics(graphics);scaledGraphics.scale(scale);// This will call ScaledGraphics#drawStringscaledGraphics.drawString(text,10,10);scaledGraphics.dispose();}}publicstaticvoidmain(String[]args){Displayd=newDisplay();Shellshell=newShell(d);shell.setLayout(newFillLayout());FigureCanvascanvas=newFigureCanvas(shell);// Create a scaled figure that will use ScaledGraphics#drawStringScaledStringFigurefigure=newScaledStringFigure("Hello World via ScaledGraphics!",3);//$NON-NLS-1$canvas.setContents(figure);shell.setText("Draw2d with ScaledGraphics");//$NON-NLS-1$shell.setSize(400,200);shell.open();while(!shell.isDisposed()){while(!d.readAndDispatch()){d.sleep();}}}}
packagedev.equo.draw2d.tree;importorg.eclipse.swt.SWT;importorg.eclipse.swt.graphics.FontData;importorg.eclipse.swt.layout.GridData;importorg.eclipse.swt.layout.GridLayout;importorg.eclipse.swt.widgets.Button;importorg.eclipse.swt.widgets.Composite;importorg.eclipse.swt.widgets.Group;importorg.eclipse.swt.widgets.Label;importorg.eclipse.swt.widgets.Scale;importorg.eclipse.swt.widgets.Shell;importorg.eclipse.draw2d.ColorConstants;importorg.eclipse.draw2d.IFigure;importorg.eclipse.draw2d.MouseEvent;importorg.eclipse.draw2d.MouseListener;importorg.eclipse.draw2d.PositionConstants;importdev.equo.draw2d.AbstractExample;/** * Demonstrates an interactive tree visualization using Draw2D with dynamic manipulation capabilities. * This example creates a hierarchical tree structure with multiple nodes and branches, allowing users * to expand/collapse nodes, add/remove children, change alignment (center/left), toggle between normal * and hanging styles, adjust spacing, enable animation, and switch between horizontal/vertical orientations. * Features interactive node selection and real-time tree layout updates. */publicclassDraw2DTreeExampleextendsAbstractExample{privatestaticfinalStringNORMAL_STYLE="Normal Style";//$NON-NLS-1$privatestaticfinalStringCHILD_2_NAME="Child 2";//$NON-NLS-1$privatestaticfinalStringCHILD_1_NAME="Child 1";//$NON-NLS-1$privatestaticfinalStringBASIC_CHILD_NAME="child";//$NON-NLS-1$booleananimate;publicstaticvoidmain(String[]args){newDraw2DTreeExample().run();}privateTreeRootroot;privatePageNodeselected;IFigurecreatePageNode(Stringtitle){finalPageNodenode=newPageNode(title);node.addMouseListener(newMouseListener.Stub(){@OverridepublicvoidmousePressed(MouseEventme){setSelected(node);}@OverridepublicvoidmouseDoubleClicked(MouseEventme){doExpandCollapse();}});returnnode;}voiddoAddChild(){if(selected==null){return;}TreeBranchparent=(TreeBranch)selected.getParent();parent.addBranch(newTreeBranch(createPageNode(BASIC_CHILD_NAME),parent.getStyle()));}voiddoAlignCenter(){if(selected==null){return;}TreeBranchparent=(TreeBranch)selected.getParent();parent.setAlignment(PositionConstants.CENTER);}voiddoAlignLeft(){if(selected==null){return;}TreeBranchparent=(TreeBranch)selected.getParent();parent.setAlignment(PositionConstants.LEFT);}voiddoDeleteChild(){if(selected==null){return;}TreeBranchparent=(TreeBranch)selected.getParent();IFigurecontents=parent.getContentsPane();if(contents.getChildren().isEmpty()){return;}contents.remove(contents.getChildren().get(contents.getChildren().size()-1));}voiddoExpandCollapse(){if(selected==null){return;}TreeBranchparent=(TreeBranch)selected.getParent();if(parent.getSubtrees().isEmpty()){return;}if(animate){if(parent.isExpanded()){parent.collapse();}else{parent.expand();}}else{parent.setExpanded(!parent.isExpanded());}}voiddoStyleHanging(){if(selected==null){return;}TreeBranchparent=(TreeBranch)selected.getParent();parent.setStyle(TreeBranch.STYLE_HANGING);}voiddoStyleNormal(){if(selected==null){return;}TreeBranchparent=(TreeBranch)selected.getParent();parent.setStyle(TreeBranch.STYLE_NORMAL);}/** * @see org.eclipse.draw2d.examples.AbstractExample#createContents() */@OverrideprotectedIFigurecreateContents(){getFigureCanvas().setBackground(ColorConstants.white);root=newTreeRoot(createPageNode("Graph Root"));//$NON-NLS-1$TreeBranchbranch=newTreeBranch(createPageNode(NORMAL_STYLE));root.addBranch(branch);root.addBranch(newTreeBranch(createPageNode("Child")));//$NON-NLS-1$branch.addBranch(newTreeBranch(createPageNode(CHILD_1_NAME)));branch.addBranch(newTreeBranch(createPageNode(CHILD_2_NAME)));TreeBranchsubbranch=newTreeBranch(createPageNode("Child 3"));//$NON-NLS-1$branch.addBranch(subbranch);subbranch.addBranch(newTreeBranch(createPageNode(BASIC_CHILD_NAME)));subbranch.addBranch(newTreeBranch(createPageNode(BASIC_CHILD_NAME)));subbranch.addBranch(newTreeBranch(createPageNode(BASIC_CHILD_NAME)));branch=newTreeBranch(createPageNode(NORMAL_STYLE),TreeBranch.STYLE_NORMAL);root.addBranch(branch);root.addBranch(newTreeBranch(createPageNode("Child")));//$NON-NLS-1$branch.addBranch(newTreeBranch(createPageNode(CHILD_1_NAME),TreeBranch.STYLE_HANGING));subbranch=newTreeBranch(createPageNode(CHILD_2_NAME),TreeBranch.STYLE_HANGING);branch.addBranch(subbranch);subbranch.addBranch(newTreeBranch(createPageNode(BASIC_CHILD_NAME)));subbranch.addBranch(newTreeBranch(createPageNode(BASIC_CHILD_NAME)));branch=newTreeBranch(createPageNode(NORMAL_STYLE));root.addBranch(branch);branch.addBranch(newTreeBranch(createPageNode(CHILD_1_NAME)));branch.addBranch(newTreeBranch(createPageNode(CHILD_2_NAME)));branch.addBranch(newTreeBranch(createPageNode("Child 3")));//$NON-NLS-1$returnroot;}/** * @see org.eclipse.draw2d.examples.AbstractExample#run() */@OverrideprotectedvoidhookShell(Shellshell){CompositelocalShell=newComposite(shell,0);localShell.setLayoutData(newGridData(GridData.FILL_VERTICAL));localShell.setLayout(newGridLayout());GrouprootGroup=newGroup(localShell,0);rootGroup.setText("Root Properties");//$NON-NLS-1$FontDatadata=rootGroup.getFont().getFontData()[0];data.setStyle(SWT.BOLD);rootGroup.setLayout(newGridLayout());finalButtonorientation=newButton(rootGroup,SWT.CHECK);orientation.setText("Horizontal Orientation");//$NON-NLS-1$orientation.setSelection(true);orientation.addListener(SWT.Selection,e->root.setHorizontal(orientation.getSelection()));finalButtonuseAnimation=newButton(rootGroup,SWT.CHECK);useAnimation.setText("Use Animation");//$NON-NLS-1$useAnimation.setSelection(false);useAnimation.addListener(SWT.Selection,e->animate=useAnimation.getSelection());finalButtoncompress=newButton(rootGroup,SWT.CHECK);compress.setText("Compress Tree");//$NON-NLS-1$compress.setSelection(false);compress.addListener(SWT.Selection,e->{root.setCompression(compress.getSelection());root.invalidateTree();root.revalidate();});finalLabelmajorLabel=newLabel(rootGroup,0);majorLabel.setText("Major Spacing: 10");//$NON-NLS-1$finalScalemajor=newScale(rootGroup,0);major.setMinimum(5);major.setIncrement(5);major.setMaximum(50);major.setSelection(10);major.addListener(SWT.Selection,e->{root.setMajorSpacing(major.getSelection());majorLabel.setText("Major Spacing: "+root.getMajorSpacing());//$NON-NLS-1$});finalLabelminorLabel=newLabel(rootGroup,0);minorLabel.setText("Minor Spacing: 10");//$NON-NLS-1$finalScaleminor=newScale(rootGroup,0);minor.setMinimum(5);minor.setIncrement(5);minor.setMaximum(50);minor.setSelection(10);minor.addListener(SWT.Selection,e->{root.setMinorSpacing(minor.getSelection());minorLabel.setText("Minor Spacing: "+root.getMinorSpacing());//$NON-NLS-1$});GroupselectedGroup=newGroup(localShell,0);selectedGroup.setText("Selected Node:");//$NON-NLS-1$selectedGroup.setLayout(newGridLayout(2,true));ButtonaddChild=newButton(selectedGroup,0);addChild.setText("More Children");//$NON-NLS-1$addChild.addListener(SWT.Selection,e->doAddChild());ButtonremoveChild=newButton(selectedGroup,0);removeChild.setText("Fewer Children");//$NON-NLS-1$removeChild.addListener(SWT.Selection,e->doDeleteChild());ButtonalignCenter=newButton(selectedGroup,0);alignCenter.setText("Align Center");//$NON-NLS-1$alignCenter.addListener(SWT.Selection,e->doAlignCenter());ButtonalignLeft=newButton(selectedGroup,0);alignLeft.setText("Align Top/Left");//$NON-NLS-1$alignLeft.addListener(SWT.Selection,e->doAlignLeft());Buttonnormal=newButton(selectedGroup,0);normal.setText("Normal");//$NON-NLS-1$normal.addListener(SWT.Selection,e->doStyleNormal());Buttonhanging=newButton(selectedGroup,0);hanging.setText("Hanging");//$NON-NLS-1$hanging.addListener(SWT.Selection,e->doStyleHanging());ButtonexpandCollapse=newButton(selectedGroup,0);expandCollapse.setText("expand/collapse");//$NON-NLS-1$expandCollapse.addListener(SWT.Selection,e->doExpandCollapse());}voidsetSelected(PageNodenode){if(selected!=null){selected.setSelected(false);}selected=node;selected.setSelected(true);}}
packagedev.equo.draw2d.uml;importorg.eclipse.swt.graphics.Color;importorg.eclipse.swt.layout.FillLayout;importorg.eclipse.swt.widgets.Display;importorg.eclipse.swt.widgets.Shell;importorg.eclipse.draw2d.ChopboxAnchor;importorg.eclipse.draw2d.ColorConstants;importorg.eclipse.draw2d.ConnectionEndpointLocator;importorg.eclipse.draw2d.ConnectionLocator;importorg.eclipse.draw2d.Figure;importorg.eclipse.draw2d.FigureCanvas;importorg.eclipse.draw2d.IFigure;importorg.eclipse.draw2d.Label;importorg.eclipse.draw2d.LineBorder;importorg.eclipse.draw2d.PolygonDecoration;importorg.eclipse.draw2d.PolylineConnection;importorg.eclipse.draw2d.XYLayout;importorg.eclipse.draw2d.geometry.PointList;importorg.eclipse.draw2d.geometry.Rectangle;/** * Demonstrates creating a UML class diagram using Draw2D with connections and decorations. * This example creates two UML class figures connected by a polyline association with containment * decoration, endpoint labels, a connection label, and a sticky note. Shows how to use anchors, * decorations, and connection locators for creating structured diagrams. */publicclassDraw2DUMLClassDiagram{publicstaticvoidmain(String[]args){Displayd=newDisplay();Shellshell=newShell(d);shell.setLayout(newFillLayout());shell.setText("UML Diagram");FigureCanvascanvas=newFigureCanvas(shell);canvas.setBackground(ColorConstants.white);Figurediagram=newFigure();diagram.setLayoutManager(newXYLayout());canvas.setContents(diagram);IFigurec1,c2;diagram.add(c1=newUMLClassFigure(),newRectangle(20,20,-1,-1));diagram.add(c2=newUMLClassFigure(),newRectangle(230,102,-1,-1));PolylineConnectionassoc=newPolylineConnection();assoc.setTargetAnchor(newChopboxAnchor(c1));assoc.setSourceAnchor(newChopboxAnchor(c2));PolygonDecorationcontainment=newPolygonDecoration();containment.setTemplate(newPointList(newint[]{-2,0,-1,1,0,0,-1,-1}));assoc.setTargetDecoration(containment);diagram.add(assoc);Labelref=newLabel("end1");//$NON-NLS-1$ConnectionEndpointLocatorlocator=newConnectionEndpointLocator(assoc,false);locator.setUDistance(8);assoc.add(ref,locator);LabelconnLabel=newLabel("connection");//$NON-NLS-1$connLabel.setBorder(newLineBorder());connLabel.setOpaque(true);connLabel.setBackgroundColor(newColor(255,255,255));assoc.add(connLabel,newConnectionLocator(assoc,ConnectionLocator.MIDDLE));diagram.add(newStickyNote(),newRectangle(180,10,90,-1));shell.setSize(500,300);shell.open();while(!shell.isDisposed()){while(!d.readAndDispatch()){d.sleep();}}}}