Gef faq 4: Non-rectangular Elements

Source: Internet
Author: User
Tags getcolor

Now, if we want to replace the rectangular element nodes in the original gefpractice example with an elliptical shape, what changes do we need? Obviously, you must first change the nodefigure class that originally inherits rectanglefigure to ellipse:

Public   Class Nodefigure Extends Ellipse /* Rectanglefigure */ {

}

After this modification, we can see that the elements in the editor have become elliptical. However, if you click an element, the selected border (selection box) is still rectangular, as shown in 1:


Figure 1 elliptical node and rectangle selection box

If you think the selection box of the rectangle is not very harmonious, you can modify it by overwriting the createchildeditpolicy () method of diagramlayouteditpolicy. By default, this method returns a resizableeditpolicy. We need to define our own subclass (ellipseresizableeditpolicy) to replace it as the return value.

Ellipseresizableeditpolicy needs to cover the two methods of resizableeditpolicy. The createselectionhandles () method determines the situations related to the resizehandle and movehandle. Our implementation is as follows:

Protected List createselectionhandles (){
List list =   New Arraylist ();
// Add selection box
// Resizablehandlekit. addmovehandle (graphicaleditpart) gethost (), list );
List. Add ( New Movehandle (graphicaleditpart) gethost ()){
Protected   Void Initialize (){
Super . Initialize ();
Setborder ( New Lineborder ( 1 ){
Public   Void Paint (ifigure figure, graphics, insets ){
Temprect. setbounds (getpaintrectangle (figure, insets ));
If (Getwidth () %   2   =   1 ){
Temprect. Width -- ;
Temprect. Height -- ;
}
Temprect. Shrink (getwidth () /   2 , Getwidth () /   2 );
Graphics. setlinewidth (getwidth ());
If (Getcolor () ! =   Null )
Graphics. setforegroundcolor (getcolor ());
// Replacing a rectangle with an elliptical shape
// Graphics. drawrectangle (temprect );
Graphics. drawoval (temprect );
}
});
}
});

// Add control handle
Resizablehandlekit. addhandle (graphicaleditpart) gethost (), list, positionconstants. East );
Resizablehandlekit. addhandle (graphicaleditpart) gethost (), list, positionconstants. South );
Resizablehandlekit. addhandle (graphicaleditpart) gethost (), list, positionconstants. West );
Resizablehandlekit. addhandle (graphicaleditpart) gethost (), list, positionconstants. North );
Return List;
}

The createdragsourcefeedbackfigure () method determines the shape and color of a translucent image (that is, a ghost image) that moves with the mouse when you drag the image. Therefore, we overwrite this method to display an elliptical ghost image.

Protected Ifigure createdragsourcefeedbackfigure (){
// Replacing a rectangle with an ellipse
// Rectanglefigure r = new rectanglefigure ();
Ellipse R =   New Ellipse ();
Figureutilities. makeghostshape (R );
R. setlinestyle (graphics. line_dot );
R. setforegroundcolor (colorconstants. White );
R. setbounds (getinitialfeedbackbounds ());
Addfeedback (R );
Return R;
}

After these modifications, we can see that the selection box and ghost shadows are all elliptical, as shown in figure 2.


Figure 2 select boxes and shadows with the same shape as nodes

Click here to download the project. This project is modified from gefpractice in the GEF application instance, and the extension of the target file is changed to. gefpracticeel.

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.