实现双击事件,只需在ShipPart中覆写performRequest()方法即可。
@Override
public
void performRequest(Request request)
{
if (request.getType() == RequestConstants.
REQ_OPEN)
{
//获得所点击的模型信息
ShipModel shipModel=(ShipModel)getModel();
ShipInfo shipInfo=shipModel.getShipInfo();
ShipInfoDialog sid=
new ShipInfoDialog(BerthUtil.
getInstances().
getShell());
sid.setShipInfo(shipInfo);
sid.open();
}
}
|
双击图标,可以实现dialog的打开:
第一步在BerthGraphicalEditor中添加对右键的支持。
protected
void configureGraphicalViewer() {
……
ContextMenuProvider provider =
new BerthContextMenuProvider(
this,viewer, getActionRegistry());
viewer.setContextMenu(provider);
getSite().registerContextMenu("org.jport.gef.editor.contextmenu",provider, viewer);
……
}
|
第二步在BerthGraphicalEditor中创建Action
@SuppressWarnings("unchecked")
public
void
createActions() {
super.createActions();
ActionRegistry registry = getActionRegistry();
IAction action =
new RefreshAction(
this);
registry.registerAction(action);
getSelectionActions().add(action.getId());
}
|
第三步 创建BerthContextMenuProviderAction.java
public
void buildContextMenu(IMenuManager menu) {
// Add standard action groups to the menu
GEFActionConstants.addStandardActionGroups(menu);
if (part
instanceof BerthGraphicalEditor) {
EditPart focusEditPart = getViewer().getFocusEditPart();
//判断不同的模型,为不同的模型添加不同的menu
if (focusEditPart
instanceof ShipPart) {
addShipModelPopuMenu(menu);
}
}
}
private
void
addShipModelPopuMenu(IMenuManager menu) {
//you can new an action here,but is inefficient,for everytime you click right click will create an new class
menu.add(getAction("moveUp"));
}
|
Ok,试试看吧
图形的更新分为两部分:
1、更新模型属性,使图形显示与模型同步。本文通过Tabbed修改模型的属性来触发图形的改变。
//
TODO Auto-generated method stub
try {
// invoke save service
ShipInfo shipInfo=shipModel.getShipInfo();
shipInfo.setShipName(shipNameT.getText());
shipModel.setShipInfo(shipInfo);
MessageDialogInfo.openInformation("提示信息", "位信息保存成功!");
}
catch (
Exception e) {
MessageDialogInfo.openError("错误提示", "保存位信息出错,请重试");
}
}
|
在ShipModle中的setShipInfo中,触发监听事件:
public
void setShipInfo(ShipInfo shipInfo) {
this.shipInfo = shipInfo;
getListeners().firePropertyChange(
PROPERTY_UPDATEMODEL,
null, shipInfo);
}
|
在ShipPart中实现监听,
public
void propertyChange(PropertyChangeEvent evt) {
……
if (evt.getPropertyName().equals(ShipModel.
PROPERTY_UPDATEMODEL))
refreshVisuals();
}
protected
void
refreshVisuals(){
ShipFigure figure = (ShipFigure)getFigure();
ShipModel model = (ShipModel)getModel();
figure.setShipName(model.getShipInfo().getShipName());
figure.setLayout(model.getLayout());
}
|
Ok,when update the ship name in the tabbed and click the save button ,you can see this:
2. 更新图形显示。由于图形是在图形显示时就绘制好的,不能在绘制完成后再重绘。如不能改变图形的“渐变背景”,注意如果背景是通过setBackgroundColor()方式设置的背景,可以像类似的属性更新的方式改变图形的背景。GEF没有提供图形的刷新方式,可以通过removeChild()和addChild()来实现。
本文同右键功能来实现图形的实现图形的刷新。
在RefreshAction的run方法做模型的刷新处理,其实质就是removeChild后再addChild;
@Override
public
void run() {
super.run();
ShipPart shipPart = (ShipPart) getSelectedObjects().get(0);
DiagramModel parent=(DiagramModel)shipPart.getParent().getModel();
ShipModel shipModel=(ShipModel)shipPart.getModel();
//just for fun
shipModel.setBgType(shipModel.getBgType()+1);
parent.removeChild(shipModel);
parent.addChild(shipModel);
}
|
Now you will see this:
右键点击双击时,即可看到背景颜色的改变。
1、对导航列表添加双击事件
public
class Navigate
extends ViewPart {
……
public
void createPartControl(Composite parent) {
……
viewer.addDoubleClickListener(
new TreeDoubleClick(viewer));
}
|
package org.jport.gef.berth.action;
public
class
TreeDoubleClick
implements IDoubleClickListener {
……
public
void doubleClick(DoubleClickEvent event)
{
IStructuredSelection selection = (IStructuredSelection) tv.getSelection();
String companyName = (String) (selection.getFirstElement());
try
{
//
//input 自动根据输入的不同来判断该Editor是否打开
input=
new BerthEditorInput(companyName);
window.getActivePage().openEditor(input, BerthGraphicalEditor.
ID);
}
catch (WorkbenchException e) {
//
TODO Auto-generated catch block
}
}
}
|
在打开Editot时,对于同一个公司名,始终保持打开且仅打开一个editor,这是由editorInput的name属性决定的,因为在editor中实现了:
@Override
public
boolean exists() {
return (
this.name !=
null);
}
public
boolean equals(Object o) {
if (!(o
instanceof BerthEditorInput))
return
false;
return ((BerthEditorInput)o).getName().equals(getName());
}
|
转载于:https://blog.51cto.com/growup/551852