Database fields, as well as increase the display

  Database fields, as well as increase the display 

  Following nearly a month, I can finally at home - I came back.    As for not moving online, this should have been finished long ago article can only delay so long to "release." 

  In this chapter, I talk about how to use layout, as well as some other on the use of EditPolicy and Palette realized.    Elaborate, sample code and the code in the content of this chapter variance, it is because before I do a bit of writing that this more than I completed, but due to some technical differences, so I will be divided into two articles finish , I suggest that you read Chapter 5 of the code later.    Download the code. 

  1. Re show TableFigure 

  The previous chapter, we simply draw a rectangle, as our graphics database table, now let us re-think how to display this TableFigure. 

  Let us see how Visio is displayed inside the bar: 

  We also like to do it in accordance with a similar! 

  First, we need to show our table, and secondly, we have to field data in the table shows that the decentralization in the region, and then stored in the database table and field regional draw a line between, separated.    See next plans: 

  We seem to have previously written TableFigure far worse, just a rectangle that is not enough. 

  First consider how to display table.    TableFigure displayed in a text very easy, simple way is to draw it in time, the use of Graphics drawText method can be, but doing so is very cumbersome and need to keep the calculation table display position, and, when we table The name change, the size of the table itself to regulate it.    Therefore, we need to use Label to show, because Label can be easily displayed on the table for maintenance, including access to the length of text with the size of the Figure; In addition, when we use the Label to show, TableFigure can also layout manager to calculate their current size, and save our own calculated.    TableFigure open category, add the following code: 

  Public? TableFigure (Table? Model)? ( 
  ???????? Super (); 
  ???????? This. Model? =? Model; 

  ???????? TableNameLabel? =? New? Label (); 
  ???????? TableNameLabel.setText (model.getTableName ()); 
  ???????? FontData? Fd? =? New? FontData (); 

  ???????? Fd.setHeight (10); 
  ???????? Fd.setName ( "Arial"); 
  ???????? Fd.setStyle (SWT.BOLD); 

  ???????? TableNameLabel.setFont (new? Font (null, the fd)); 
  ???????? TableNameLabel.setIcon (ImageProvider.TABLE_ICON.createImage ()); 
  ???????? TableNameLabel.setLabelAlignment (PositionConstants.MIDDLE); 

  ???????? / /? Left point margins, points will be nice 
  ???????? This. SetBorder (new? MarginBorder (8, eight, eight, eight)); 

  ???????? This. Add (tableNameLabel); 
????????
  ???????? This. SetOpaque (true); 
????}

  We TableFigure created, it generated to a Label, and then this sub-Label as TableFigure Figure add to its top, and we have to let this one Label to display data table.    Code used in the above picture settings and font settings, here we only discuss GEF, it is not discussed them. 

  Names can show, let us now the containers out of the field! 

  Field graphics need to protect the containers that it, we just have seen, Visio in the field are in the field from top to bottom in an orderly containers are together, to the problem: how to get they are so obedient to be orderly.    Layout Manager (LayoutManager), as did the desktop applications are aware that, yes, Draw2D also use layout manager to a position on the size of graphics maintain.    In Draw2D ToolbarLayout called in a layout manager, it has to Figure orderly arranged with the top down, this is exactly what we need! 

  Let us now in TableFigure constructor function to add the following code: 

  None containerFigure? =? New? Figure ()?; 

  ???????? ToolbarLayout? TableLayout? =? New? ToolbarLayout (); 
  ? 
  ???????? / /? Bearing Column? Figure containers is ToolbarLayout 
  ???????? ToolbarLayout? ContainerLayout? =? New? ToolbarLayout (); 
  ???????? ContainerLayout.setMinorAlignment (ToolbarLayout.ALIGN_BOTTOMRIGHT); 
  ???????? ContainerFigure.setLayoutManager (containerLayout); 
?????
  ???????? This. Add (containerFigure); 

  — So that our containerFigure on ahead. 

  But this is not enough light.    ContainerFigure and tableNamelable two of the Figure as TableFigure graphics, should be shown here?    How to position them?    Moreover, when our two graphics subsystems size change in the future, TableFigure how do? 
  In fact, all the problems are all from the layout manager to answer. 

  Figure in the category, there is a reputation getPreferredSize method, this method is to get the current size of the Figure.    Default circumstances, getPreferredSize method will check whether the Figure registered a layout manager, and if so, it will call for layout management getPreferredSize obtained size.    The layout manager getPreferredSize method is not simply to see the current Size Figure, but to the adoption of the layout of its own characteristics, and through the Figure in the location and size of graphics, according to a certain stack algorithm and the size of the current Figure . 

  To put it simply, if we use the ToolbarLayout, then when we call a registration of the Figure of the LayoutManager getPreferredSize methods, the layout manager will Figure set to the height of all of its height and Figure; width of all sub-Figure Set in the maximum width. 

  In this way, we have just under the painting of the graphics, and containerFigure tableNameFigure is the top-ranked of all, we can use ToolbarLayout TableFigure, layout manager to manage its size.    We wrote here read out, we set up ToolbarLayout containerFigure, but also to increase the field when we graphical time for layout manager to control its size. 

  We have basically completed TableFigure.    However, if we add in the database field, EditPart do not know to the field as TableFigure Figure is a subset of containerFigure graphics and added to it, and it is the default field TableFigure graphics directly to the drawing, so that we just the idea of a completely fail to realize.    Therefore, let us duplicate the getContentPane TableEditPart: 

  ? Public? IFigure? GetContentPane ()? ( 
  ???????? Return ((TableFigure) getFigure ()). GetContainerFigure (); 
????}

  What does this mean? 
  In the previous chapter I feel like I said, the GEF in a certain EditPart for its sub-EditPart graphics rendering is in the graphics on the EditPart - a recursive process.    But it is not EditPart graphics directly to the default of Fiuger Figure containers, but through methods to obtain getContentPane bearer of the Figure EditPart graphics, of course, the direct successor to GraphicalEditPart If so, the return of getContentPane directly getFigure is, therefore, when we To redefine containers Figure EditPart time, it is necessary to duplicate getContentPane. 

  2. Show ColumnFigure 

  Now we are to achieve graphics field. 
  Graphics field is as simple as a realization of field names and field can be said of the icon, so we will it inherited from the Label: 

  Public? Class? ColumnFigure? Extends? Label? ( 
  ?? Private? Column? Model; 
  ?? Private? Boolean? Selected; 
  ?? Private? Boolean? HasFocus; 
  None public? ColumnFigure (Column? Model) ( 
  ?????????? Super (); 
  ?????????? This. Model? =? Model; 
  ?????????? Model.setColumnName (model.getColumnName ()); 
??????????
  ?????????? FontData? Fd? =? New? FontData (); 
??????????
  ???????????? Fd.setHeight (8); 
  ???????????? Fd.setName ( "Arial"); 
  ???????????? Fd.setStyle (SWT.BOLD); 
  ?????????? This. SetIcon (ImageProvider.COLUMN_ICON.createImage ()); 
  ?????????? This. SetLabelAlignment (PositionConstants.LEFT); 
  ?????????? This. SetFont (new? Font (null, fd)); 
??????}
  ) 
  ColumnEditPart then let it return to the createFigure methods: 

  ?? Protected? IFigure? CreateFigure ()? ( 
  ????????//? TODO? Auto-generated? Method? Stub 
  ???????? Return? New? ColumnFigure ((Column) getModel ()); 
????}

  Ok, now let us amend the initializeGraphicalViewer DbEditor in: 

  Protected? Void? InitializeGraphicalViewer ()? ( 
  ????????//? Hard-coded to create a database model 
  ???????? Schema? Schema? =? New? Schema (); 
  ???????? Table? Table? =? New? Table (); 
  ???????? Table.setTableName (the "Test"); 
  ???????? Column? Column? =? New? Column (); 
  ???????? Column.setColumnName ( "test"); 
  ???????? Table.addChild (column); 
  ???????? Schema.addChild (table); 
  ???????? This.getGraphicalViewer (). SetContents (schema); 
????}
  OK, now we can see a new TableFigure the. 

  But there was a problem: the size of our own TableFigure not calculated, and even fields not displayed.    Surely some people say: is not to say that a layout manager can be a! 

  And so on, but also remember that the last chapter, we can move to rectangular, and the replication refreshVisuals method?    Now we are re-write it again: 

  ? Protected? Void? RefreshVisuals ()? ( 
  ???????? Super.refreshVisuals (); 
  ????????//? Be the size of the current TableFigure, due to the Toolbar layout constraints, it will automatically calculate 
???????
  ???????? Dimension? Size? =? This.getFigure (). GetPreferredSize (); 
????????
  ????????//? Access changes the location, position in the maintenance of the Model 
  ???????? Point? P ?=?(( Table)? GetModel ()). GetLocation (); 
????????
  ????????//? We only changes the location of Table 
  ????????(( GraphicalEditPart)? This.getParent ()). SetLayoutConstraint (this,? This 
  ????????????????. GetFigure (), new? Rectangle (p, the size)); 
????}

  Read the code before a friend discovered the different: the size is no longer simple to obtain the size of the current bounds, but through our above said, the methods used to get the layout getPreferredSize calculation! 

  Later revised look at our TableFigure: 

  Add a few words: size, I found I think it is important to say that a number of other details, such as maximum and minimum size TableFigure bound ah, how the painting tableNameLabel line ah, and ah Label stops, the use of border ah, Gradual rectangular drawing ah, I did not mention these, we can see their own code, if any doubt can post discussion. 

  3. Regenerate PaletteRoot 

  In our previous example, the tools generate panel is a very simple panel, above bare, not by the panel tools to increase our Viewer Figure graphics, every time we need to make in the replication DbEditor revised code, with a hard coding model to achieve the increase. 

  Now we have to construct a Table and Column can create the tools panel, hard-coded to create model hell, let them. 

  Mr. state into a single category: PaletteFactory, and then we generate inside an empty PaletteRoot, Zailong two PaletteDrawer add to PaletteRoot: 

  Public? Class? PaletteFactory? ( 
  ?? 
  ?? Private? PaletteRoot? Root; 
  ?? 
  ?? Private? PaletteDrawer? DefaultTools; 
  ?? 
  ?? Private? PaletteDrawer? DbTools; 
  ?? 
  ?? Private? Static? PaletteFactory? Instance? =? Null; 
  ?? Private? PaletteFactory () () 
  ?? 
  ?? Public? Static? PaletteFactory? INSTANCE () ( 
  ???????? If (instance? ==? Null)? Instance? =? New? PaletteFactory (); 
  ???????? Return? Instance; 
????}
  ?? 
  ?? Public? PaletteRoot? CreatePaletteRoot () ( 
  //???????? If (root? =? Null)? Return? Root; 
????????
  ???????? Root? =? New? PaletteRoot (); 
  ???????? Root.add (createDefaultToolBox ()); 
  ???????? Root.add (createDbToolBox ()); 
  ???????? Return? Root; 
????}
  ?? 
  ?? Private? PaletteDrawer? CreateDefaultToolBox () ( 
  ???????? DefaultTools? =? New? PaletteDrawer ( "Default? Tools"); 
????????
  ???????? DefaultTools.add (new? SelectionToolEntry ()); 
????????
  ???????? Return? DefaultTools; 
????}
  ?? 
  ?? Private? PaletteDrawer? CreateDbToolBox () ( 
  ???????? DbTools? =? New? PaletteDrawer ( "DataBase? Tools ");???????? 
  ???????? Return? DbTools; 
????}
  ) 

  I simply say something, PaletteDrawer is a container can be hidden, it can increase buttons above ToolEntry.    What is ToolEntry?    We can be understood as any in the Palette above elements: buttons, dividing line, containers, etc., there is a corresponding ToolEntry Tool, through the createTool ToolEntry return, if we need some special treatment, we can direct to realize ToolEntry and Tool, but In our example, what we need is the ToolEntry model can be generated, we would not have to study ToolEntry Tool and the working principle. 

  Then descend said. 
  The code, we have to increase defaultTools a SelectionToolEntry containers, it is provided by the GEF itself a tool for the graphical user click on the option, it is not the role of nonsense here:) 

  Below from the code can be seen, we have not increased the creation of the Column and Table ToolEntry, we are now to achieve them. 

  ToolEntry create two were named as TableToolEntry and ColumnToolEntry, they are inherited CreationToolEntry.    CreationToolEntry attention to the structure function, a need for imported CreationFactory, is the creation of this plant we have to return to the model example of the factory, and its two methods: getNewObject () and getObjectType () is returned to the creation of the model and model type. 

  We look at the realization of the factory: 

  Public? Class? DbCreationFactory? Implements? CreationFactory? ( 

  ?? Private? Class? Type; 
  ?? Public? DbCreationFactory (Class? Type) ( 
  ???????? SetType (type); 
????}

  ?? Public? Object? GetNewObject ()? ( 
  ???????? If (type? ==? Table.class) ( 
  ???????????? Return? New? Table (); 
????????}
  ???????? If (type? ==? Column.class)? Return? New? Column (); 
  ???????? Return? Null; 
????}

  ?? Public? Object? GetObjectType ()? ( 
  ????????//? TODO? Auto-generated? Method? Stub 
  ???????? Return? GetType (); 
????}
  ?? ……. 
  ) 
  By creating this type of plant into the parameters of the model can generate the corresponding 

  Let us look at our TableToolEntry ColumnToolEntry and the realization of: 

  Public? Class? TableToolEntry? Extends? CreationToolEntry? ( 
  ?? Public? TableToolEntry ()? ( 
  ???????? Super ( "Table", "Create? A? Table"? New? DbCreationFactory (Table.class)? ImageProvider.TABLE_ICON, the null); 
????}
  ) 

  Public? Class? ColumnToolEntry? Extends? CreationToolEntry? ( 
  ?? Public? ColumnToolEntry () ( 
  ???????? Super ( "Column", "Create? A? Column"? New? DbCreationFactory (Column.class)? ImageProvider.COLUMN_ICON, the null); 
????}
  ) </ Span> 
  All seen, and we are all imported counterparts DbCreationFactory their need to be generated model parameters. 

  Now, we create the category ToolEntry Well, let us add to the PaletteDrawer dbTools: 

  Laws? PaletteFactory category createDbToolBox () method 

  ?? Private? PaletteDrawer? CreateDbToolBox () ( 
  ???????? DbTools? =? New? PaletteDrawer ( "DataBase? Tools"); 
????????
  ???????? DbTools.add (new? TableToolEntry ()); 
  ????????? DbTools.add (new ColumnToolEntry ()); 
?????
  ???????? Return? DbTools; 
????}

  Operation to: 

  4. New Command; FlowEditPolicy usage 

  Completion of the above work, we isolated from the instrument panel to create model also almost: EditPolicy no corresponding Command. 

  Previous chapters, as we all know, the GEF All of the incidents in the package sent out a Request, and then find EditPolicy with EditPolicy go from Command to implement.    We have been generated in the previous chapter on a TableMoveCommand Therefore, I believe we should not unfamiliar to the Command. 

  We generate such a Command: DbItemCreationCommand 

  Public? Class? DbItemCreateCommand? Extends? Command? ( 
  ?? Private? DBBase? Parent; 
  ?? Private? DBBase? Child; 
  ?? Private? Int? Index? =? 1; 
  ?? 
  ?? Public? Void? Execute ()? ( 
  ???????? Assert.isNotNull (parent); 
  ???????? Assert.isNotNull (child); 
  ???????? Parent.addChild (index, child); 
????}

  ?? Public? Void? Redo ()? ( 
  ???????? Execute (); 
????}

  ?? Public? Void? Undo ()? ( 
  ???????? Assert.isNotNull (parent); 
  ???????? Assert.isNotNull (child); 
  ???????? Parent.removeChild (child); 
????}
……
??……
  ) 
  Now, with the Command, we will have to consider it, look at it say to whom the EditPolicy to return. 

  Under normal circumstances, if we want to generate a model, then we should be in the containers EditPolicy father registered a Command, because the majority of the types of containers EditPart have installed ContainerEditPolicy or LayoutEditPolicy, which can be precisely two EditPolicy CreateRequest on the capture and processing.    Therefore, with our example, it is necessary to generate Table model, it would be in his father EditPart - SchemaEditPart in the SchemaLayoutEditPolicy make a fuss about. 

  Opened this category, and discovered that they have a way: getCreateCommand, we will return here in DbItemCreateCommand bar: 

  Protected? Command? GetCreateCommand (CreateRequest? Request)? ( 
  ???????? Object? Obj? =? Request.getNewObject (); 
  ???????? If (obj? =? Null? &? Request.getNewObjectType ()?==? Table.class) ( 
  ???????????? DbItemCreateCommand? Command? =? New? DbItemCreateCommand (); 
  ???????????? Command.setParent ((DBBase) this.getHost (). GetModel ()); 
  ???????????? Command.setChild ((DBBase) obj); 
  ????????????(( Table) obj). SetLocation (request.getLocation ()); 
  ???????????? Return? Command; 
????????}
  ???????? Return? Null; 
????}
  See above code found?    We must CreateRequest carrying an object and the type of examples, and even when we click in the creation of the position in the Viewer also Therefore, we only need to set some DbItemCreateCommand father model and the model can, of course, we also need to in the generation model, the model will be generated by the mouse click position to Table model, so that he will be at the creation of a position.    Too cool! 

  But just add a model EditPart do not know, I need to refresh this notice EditPart.    Remember that the last chapter, rectangular position changes after notice is how EditPart?    I wordy about it: We use the model in the PropertyChangeSupport attribute issued notification of change, and then ran after it intercepted EditPart corresponding action can be: 

  Some code changes DBBase code: 

  Public? Void? AddChild (int? Index?, The DBBase? Child) ( 
  ???????? If (index ?==?- 1) ( 
  ???????? GetChildren (). Add (child); 
  ????????} Else ( 
  ???????????? GetChildren (). Add (index, child); 
????????}
  ???????? Child.setParent (this); 
  ???????? This.fireChildenChange (child); 
????}
  ?? 
  ?? Public? Void? RemoveChild (DBBase? Child) ( 
  ???????? Child.setParent (null); 
  ???????? GetChildren (). Remove (child); 
  ???????? This.fireChildenChange (child); 
????}

  ?? Public? Void? FireChildenChange (DBBase? Child) ( 
  ???????? Support.firePropertyChange (PRO_CHILD, null, child); 
????}

  Let DBEditPartBase to intercepted PRO_CHILD events: 

  ? Public? Void? PropertyChange (PropertyChangeEvent? Evt)? ( 
  ??????? String? PName? =? Evt.getPropertyName (); 

  ??????? If (pName.equals (DBBase.PRO_CHILD)) ( 
  ??????????? This.refreshChildren (); 
  ??????????? This.refreshVisuals (); 
???????}
????}
  Finally, let us DbEditor in the previous generation model in the code removed, let's Content Viewer Schema can be set to one, 
  Well, running a bar, is not to create Table?  :)

  5.FlowLayoutEditPolicy applications and the creation of Column 

  We have to create Table, and now need to create a Column. 

  In the above we have created a model of the formation conditions Column: ColumnToolEntry, DbCreateFactory also DbItemCreateCommand Helper same: we put this Command to where? 

  Past experience tells us that this is the need to ColumnEditPart Command of the EditPolicy EditPart father to return, but its EditPart father, but also on TableEditPart, there has been installed to protect the EditPolicy creation model, we need to create an installation he on. 

  We use a class called FlowLayoutEditPolicy as our new EditPolicy Father category, it is because a specific FlowLayoutEditPolicy ToolbarLayout FlowLayout layout manager and the Figure and done, it can be the location of mobile pairs Figure make some maintenance, such as when We TableFigure drag in the ColumnFigure, FlowLayoutEditPolicy can be inserted into the location a black line: 

  We named this category on TableFlowLayoutEditPolicy: 

  Public? Class? TableFlowLayoutEditPolicy? Extends? FlowLayoutEditPolicy? ( 

…….
?
  ?? Protected? Command? GetCreateCommand (CreateRequest? Request)? ( 
  ???????? Object? Obj? =? Request.getNewObject (); 
  ???????? If (obj? =? Null? &? Request.getNewObjectType ()?==? Column.class) ( 
  ???????????? DbItemCreateCommand? Command? =? New? DbItemCreateCommand (); 
  ???????????? Command.setParent ((DBBase) this.getHost (). GetModel ()); 
  ???????????? Command.setChild ((DBBase) obj); 
????????????
  ???????????? EditPart? After? =? GetInsertionReference (request); 
  ???????????? Int? Index? =? GetHost (). GetChildren (). IndexOf (after); 
  ???????????? Command.setIndex (index); 
  ???????????? Return? Command; 
????????}
  ???????? Return? Null; 
????}
  ? ……. 
?…………
  ?? Protected? Boolean? IsHorizontal ()? ( 
  ???????? IFigure? Figure ?=?(( GraphicalEditPart) getHost ()). GetContentPane (); 
  ???????? LayoutManager? Layout? =? Figure.getLayoutManager (); 
  ???????? If (layout? Instanceof? FlowLayout) 
  ???????? Return ((FlowLayout) figure.getLayoutManager ()). IsHorizontal (); 
  ???????? If (layout? Instanceof? ToolbarLayout) 
  ???????????? Return ((ToolbarLayout) figure.getLayoutManager ()). IsHorizontal (); 
  ???????? Return? False; 
????}
  ) 

  We returned to the getCreateCommand in DbItemCreateCommand, parent Set Table, which is generated child with the request Column, because Column is no concept of the position, we do not have to place parameters to it (no variable to the preservation ah) it is only in this table may be the first of several it, and we are also mentioned in the above, in the mobile ColumnFigure time FlowLayoutEditPolicy can draw a black line that the current location of the inserted, we passed and then we approach the current getInsertionReference black lines of the corresponding index EditPart, and then get in the EditPart EditPart TableEditPart subset of the location,再传handed Command, let us new generation of EditPart add to the position. 

  However, let FlowLayoutEditPolicy showed black lines, we also need to duplicate it? IsHorizontal, because by default, FlowLayoutEidtPolicy? IsHorizontal method at run time that the installation of the EditPolicy EditPart use the FlowLayout, but we use here ToolbarLayout is, if we do not duplicate the case, the switch will be thrown type anomaly. 

  We simply can be modified to the conduct of, or directly had he returned to the true:) 

  As we have already ColumnEditPart Column, as well as the base class has been added after the addition of nodes attribute code changes, it would not have written here. 

  Now we can click on the toolbar of the column in the Table tool in the creation of the Column. 

  Let us look at the overall results: 

  6. CONCLUSION 

  This time, we have basically resolved the database tables and fields of the realization of the database, the next chapter then goes on to talk we will, in fact there has been a code, interested friends can see. 

  The next chapter I will talk about how to achieve in the GEF PropertyPage applications, as well as some of the problems Connection 

  Posted on 2006-10-09 19:02 Dart reading (692) Comments (5) edit their collections quoted Category: GEF 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Recommend Articles

Comments

Leave a Reply