110 likes | 207 Views
This guide walks you through creating a custom shape called "CrazyShape" using the Eclipse Graphical Editing Framework (GEF). You'll learn how to define the shape's appearance, including its icon and visual properties, and how to handle its connection points. The tutorial covers creating subclasses for both shape and figure, customizing their behavior, and configuring the drawing palette. By the end, you will have a personalized shape ready to use in your GEF applications.
E N D
ReuseTool Auxiliando o Processo de Instanciação
Processo de Instanciação Frameworks
Código I • package org.eclipse.gef.examples.shapes.mymodel • /** • * A crazy shape. • * @author Toacy Oliveira • */ • public class CrazyShape extends Shape { • /** A 16x16 pictogram of a crazy shape. */ • private static final Image CRAZY_ICON = createImage("icons/crazy16.gif"); • private static final long serialVersionUID = 1; • public Image getIcon() { • return CRAZY_ICON; • } • public String toString() { • return "Crazy " + hashCode(); • } • public IFiguregetFigure() { • return new CrazyFigure ("Pop"); • } • public ConnectionAnchorgetConnectionAnchor(IFigureiFigure) { • return new ChopboxAnchor(iFigure); • } • public booleancanConnect(Shape target) { • return (super.canConnect(target)) && !(target instanceofRectangularShape); • } • }
Código II • package org.eclipse.gef.examples.shapes.myfigures; • public class CrazyFigure extends RoundedRectangle { • public CrazyFigure(String name) { • super(); • ToolbarLayout layout = new ToolbarLayout(); • setLayoutManager(layout); • setBorder(new LineBorder(ColorConstants.black, 1)); • setBackgroundColor(ColorConstants.red); • setForegroundColor(ColorConstants.yellow); • setOpaque(true); • Label l = new Label("Crazy!" + name); • l.setForegroundColor(ColorConstants.red); • l.setBackgroundColor(ColorConstants.blue); • l.setFont(new Font(null, "Arial", 20, SWT.BOLD)); • l.setBorder(new LineBorder(ColorConstants.black, 2)); • add(l); • } • public void paintFigure(Graphics graphics) { • super.paintFigure(graphics); • graphics.setForegroundColor(ColorConstants.orange); • graphics.setLineStyle(Graphics.LINE_SOLID); • graphics.setLineWidth(3); • Rectangle r = getBounds(); • graphics.drawLine(new Point(r.x + r.width / 2, r.y + r.height * 0.3), • new Point(r.x + r.width / 2, r.y + r.height * 0.7)); • // horizontal line • graphics.drawLine(new Point(r.x + r.width * 0.3, r.y + r.height / 2), • new Point(r.x + r.width * 0.7, r.y + r.height / 2)); • }; • }
Hotspots • Configurando a Forma • Ícones • Aparência • Conexões • Alterando a Palette • Inserindo novo elemento na Palette
A Solução RDL COOKBOOK ShapesProducts RECIPE main{ packA =NEW_PACKAGE(FrameworkModel,"org.reusetool.example.myshapeseditor"); LOOP("Create another shape?") { // PREPARE Images EXTERNAL_TASK("Define 16x16 icon"); EXTERNAL_TASK("Define 24x24 icon"); // Define Shape SubClass shapeClass = CLASS_EXTENSION(Shape, packA,"?"); // Refine Abstract Methods m = METHOD_EXTENSION(Shape,shapeClass,addConnectionAnchor); ADD_CODE(shapeClass,m,"return new ChopboxAnchor(iFigure);"); m = METHOD_EXTENSION(Shape,shapeClass,getFigure); ADD_CODE(shapeClass,m,"return new IFIGURE_SUBCLASS);"); m = METHOD_EXTENSION(Shape,shapeClass,getIcon); ADD_CODE(shapeClass,m,"returncreateImage(\"NEW_SHAPE.gif\");"); // Configure Palette ADD_CODE(ShapesEditorPaletteFactory,createShapesDrawer, "component = new CombinedTemplateCreationEntry( \"NEW_SHAPE\", \"Create a NEW_SHAPE shape\", NEW_SHAPE.class, new SimpleFactory(NEW_SHAPE.class), ImageDescriptor.createFromFile(ShapesPlugin.class, \"icons/crazy16.gif\"), ImageDescriptor.createFromFile(ShapesPlugin.class, \"icons/crazy24.gif\")); componentsDrawer.add(component); • "); • // Redefine Method to treat How Connections can be handled • IF ("Add Feature - Restrict Connections?") THEN { • m = METHOD_EXTENSION(Shape,shapeClass,canConnect); • ADD_CODE(shapeClass,m,"Code that checks if the connection can be made."); • } • IF ("Add Feature - New Figure?") THEN{ • // Define Shape SubClass • figClass = CLASS_EXTENSION(Figure, packA,"?"); • m = METHOD_EXTENSION(Figure,figClass,paintFigure); • } • } • } • END_COOKBOOK