[Eclipse] GEF Getting Started series (X. One implementation of the table)

Source: Internet
Author: User

In the current GEF version (3.1M6), there are not many layoutmanager available, and in newsgroups you will often see posts that require more layouts, and others offer their own implementations, such as this gridlayout, equivalent to SWT GridLayout draw2d implementation, and so on. While it is certain that a future version of GEF will add more layouts for developers to use (which may take a long time), there is no straightforward way to use GEF to implement table operations, and here's what I do for reference only.

The way to implement a table depends on the design of the model, and at first it seems like we should have these classes: tables (table), Rows (row), Columns (column), and cells (cell), each model object corresponds to a editpart, and a Figure, Tablepart should contain Rowpart and Columnpart, the problem is that rowfigure and columnfigure will cross, imagine what kind of layout you should use for your form to accommodate them? Using such a model is not impossible (for example, using stacklayout), but I think this model requires a lot of extra work, so I use a column based model.

In my table model, there are only three objects: table, column, and cell, but column has a subclass Headercolumn represents the first column, and the cell has a subclass Headercell represents the cells in the first column, after The function of these two classes is primarily to simulate the operation of the rows--to convert the operations of the rows to Headercell operations. For example, to create a new row to add a new cell to the first column, of course, at the same time we want the program to add a single cell to each of the remaining columns.

Figure 1 Table Editor

The problem now is how to make it invisible to the user that we are working on a cell rather than on a row. There are a few places to change: one is to create a new row or change the position of the line to display a consistent ruled line, and second, when the user clicks in the first column of the cell (Headercell) when the entire row is selected, the third is to allow the user to drag the mouse to change the height of the row, Finally, the correct echo (Feedback) graphic is displayed when you change the position or size of the row. Here's how they are implemented.

Adjust the width of the insertion line

In our palette there is a row tool item that represents a row in the table, and its function is to create a new row. Note that the name of this tool item is called row, and it is actually created with a Headercell object, and the code to create it is as follows:

Tool = new Combinedtemplatecreationentry ("Row", "Create a new Row", Headercell.class, new Simplefactory (Headercell.class ), Cbmplugin.getimagedescriptor (Iconstants.img_row), null);

The way to create a new row is to drag it from the palette to where you want it. As you drag, the editor should be able to display a straight line as the mouse is positioned to indicate where you want to release the new row of the mouse when it is inserted. Because this tool represents a cell, by default GEF displays an insertion line that is the same length as the cell, and in order for the user to feel the insertion line, we must change the width of the insertion line. The specific approach is to overwrite the Showlayouttargetfeedback () method in the Editpolicy (inheritance flowlayouteditpolicy) of the headercolumnpart responsible for layout, with the following modified code:

protected void showLayoutTargetFeedback(Request request) {
   super.showLayoutTargetFeedback(request);
   // Expand feedback line's width
   Diagram diagram = (Diagram) getHost().getParent().getModel();
   Column column = (Column) getHost().getModel();
   Point p2 = getLineFeedback().getPoints().getPoint(1);
   p2.x = p2.x + (diagram.getColumns().size() - 1) * (column.getWidth() + IConstants.COLUMN_SPACING);
   getLineFeedback().setPoint(p2, 1);
}

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.