package org.jport.gef.berth.model;
import org.eclipse.draw2d.PositionConstants;
public class DiagramModel extends BaseModel{
private BaseRuler leftRuler, topRuler;
// 标尺是否可见
private boolean rulersVisibility = true;
//return ruler type
public BaseRuler getRuler(int orientation) {
BaseRuler result = null;
switch (orientation) {
case PositionConstants.NORTH :
if(topRuler==null)
topRuler = new BaseRuler(true);
result = topRuler;
break;
case PositionConstants.WEST :
if(leftRuler==null)
leftRuler=new BaseRuler(false);
result = leftRuler;
break;
}
return result;
}
public boolean getRulerVisibility() {
return rulersVisibility;
}
public void setRulerVisibility(boolean newValue) {
rulersVisibility = newValue;
}
public BaseRuler getTopRuler() {
return topRuler;
}
public void setTopRuler(BaseRuler topRuler) {
this.topRuler = topRuler;
}
}
|
// 定义标尺
private JportRulerComposite rulerComp;
@Override
protected void createGraphicalViewer(Composite parent) {
rulerComp = new JportRulerComposite(parent, SWT.NONE);
super.createGraphicalViewer(rulerComp);
rulerComp
.setGraphicalViewer((ScrollingGraphicalViewer) getGraphicalViewer());
}
protected Control getGraphicalControl() {
return rulerComp;
}
|
protected void loadProperties() {
// BerthUtil.CURRENt_COMPANY=getCompanyId();
BaseRuler ruler = getModel().getRuler(PositionConstants.WEST);
RulerProvider provider = null;
if (ruler != null) {
provider = new BerthRulerProvider(ruler);
}
getGraphicalViewer().setProperty(RulerProvider.PROPERTY_VERTICAL_RULER,
provider);
ruler = getModel().getRuler(PositionConstants.NORTH);
provider = null;
if (ruler != null) {
provider = new BerthRulerProvider(ruler);
}
getGraphicalViewer().setProperty(
RulerProvider.PROPERTY_HORIZONTAL_RULER, provider);
getGraphicalViewer().setProperty(
RulerProvider.PROPERTY_RULER_VISIBILITY,
new Boolean(getModel().getRulerVisibility()));
…………
}
|
/**
* 泊位标尺
* @param graphics 绘图对象
* @param dotsPerDivision 放缩比例,在目前的方法实现中并没有使用
*/
private void drawBerthLine(Graphics graphics,double dotsPerDivision) {
// TODO Auto-generated method stub
int berthSum=0;
//画线的位置*放缩比例d
int drawLine_x=0;
List<Berth> berthList=DataService.getInstances().getBerthList();
for(Berth berth:berthList)
{
drawLine_x=berthSum+berth.getBerthLenth();
graphics.setForegroundColor(ColorConstants.red);
graphics.drawLine(drawLine_x,10,drawLine_x,45);
int midPos=berthSum+(drawLine_x-berthSum)/2;
//在泊位中点画出泊位的名称
drawText(graphics,berth.getBerthName(),midPos,10);
berthSum=drawLine_x;
}
}
|
protected void configureGraphicalViewer() {
ScalableRootEditPart root = new ScalableRootEditPart();
// all the figure attach to this root have the zoom ablity
double[] zoomLevels = new double[] { 0.25, 0.5, 0.75, 1.0, 1.25, 1.5,1.75, 2.0, 2.5, 2.5, 10 };
root.getZoomManager().setZoomLevels(zoomLevels);
IAction zoomIn = new ZoomInAction(root.getZoomManager());
IAction zoomOut = new ZoomOutAction(root.getZoomManager());
getActionRegistry().registerAction(zoomIn);
getActionRegistry().registerAction(zoomOut);
getSite().
getSite().
……
}
public Object getAdapter(Class type) {
if (type == ZoomManager.class)
return ((ScalableRootEditPart) getGraphicalViewer()
.getRootEditPart()).getZoomManager();
……
}
|
<extension
point="org.eclipse.ui.editors">
<editor
class="org.jport.gef.berth.ui.BerthGraphicalEditor"
contributorClass="org.jport.gef.berth.action.DiagramActionBarContributor"
default="true"
icon="icons/sample.gif"
id="org.jport.gef.berth.ui.BerthGraphicalEditor"
name="泊位调度">
</editor>
</extension>
|
@Override
public void preWindowOpen() {
IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
configurer.setInitialSize(new Point(400, 300));
//要让工具栏显示,应设为true
configurer.setShowCoolBar(true);
configurer.setShowStatusLine(false);
configurer.setTitle("GEF 企业应用");
}
|
// 定义大纲视图
public OutlinePage outlinePage;
|
public class OutlinePage extends ContentOutlinePage implements IAdaptable {
// 用Logic example 的现实方式,把outline和oview放到pageBook中显示
private PageBook pageBook;
//大纲视图类
private Control outline;
//用来显示缩微图的画布
private Canvas overview;
// 缩微图实现类
private Thumbnail thumbnail;
……
}
|
<extension
point="org.eclipse.ui.views.properties.tabbed.propertyContributor">
<propertyContributor
typeMapper="org.jport.gef.berth.properties.tabbed.ElementTypeMapper"
contributorId="org.jport.gef.berth.ui.BerthGraphicalEditor"
labelProvider="org.jport.gef.berth.properties.tabbed.BerthElementLabelProvider"
>
<propertyCategory category="berth"/>
</propertyContributor>
</extension>
<extension
point="org.eclipse.ui.views.properties.tabbed.propertyTabs">
<propertyTabs contributorId="org.jport.gef.berth.ui.BerthGraphicalEditor">
<propertyTab
category="berth"
id="shipTab"
label="船舶基本信息"/>
</propertyTabs>
</extension>
|
<extension
point="org.eclipse.ui.views.properties.tabbed.propertySections">
<propertySections contributorId="org.jport.gef.berth.ui.BerthGraphicalEditor">
<propertySection
class="org.jport.gef.berth.properties.tabbed.ShipInfoSection"
id="shipInfoSection"
tab="shipTab">
<input
type="org.jport.gef.berth.model.ShipModel">
</input>
</propertySection>
</propertySections>
</extension>
|
@Override
protected PaletteRoot getPaletteRoot() {
// TODO Auto-generated method stub
if( paletteRoot == null ){
paletteRoot =PaletteFactory.createPalette();
}
return paletteRoot;
}
|
……
private static PaletteContainer createComponentsDrawer() {
// TODO Auto-generated method stub
PaletteDrawer drawer = new PaletteDrawer("操作工具", ImageDescriptor
.createFromFile(Activator.class, "icons/comp.gif"));
List entries = new ArrayList();
CombinedTemplateCreationEntry combined = new CombinedTemplateCreationEntry(
"预报信息", "添加预报信息", new SimpleFactory(ShipModel.class),
ImageDescriptor.createFromFile(Activator.class,
"icons/ship16.gif"),
ImageDescriptor.createFromFile(Activator.class,
"icons/ship24.gif")
);
entries.add(combined);
drawer.addAll(entries);
return drawer;
}
……
|
@Override
protected Command getCreateCommand(CreateRequest request) {
if (request.getType() == REQ_CREATE && getHost() instanceof DiagramPart) {
ShipCreateCommand cmd = new ShipCreateCommand();
cmd.setDiagramModel(getHost().getModel());
cmd.setShipModel(request.getNewObject());
Rectangle constraint = (Rectangle)getConstraintFor(request);
constraint.x = (constraint.x < 0) ? 0 : constraint.x;
constraint.y = (constraint.y < 0) ? 0 : constraint.y;
constraint.width = (constraint.width <= 0) ? BerthConstants.SHIP_LENGTH : constraint.width;
constraint.height = (constraint.height <= 0) ? BerthConstants.SHIP_WIDTH: constraint.height;
cmd.setLayout(constraint);
return cmd;
}
return null;
}
|
package org.jport.gef.berth.command;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.Command;
import org.jport.gef.berth.model.DiagramModel;
import org.jport.gef.berth.model.ShipModel;
public class ShipCreateCommand extends Command
{
private ShipModel ship;
private DiagramModel diagram;
public ShipCreateCommand() {
super();
ship = null;
diagram= null;
}
public void setShipModel(Object s)
{
if (s instanceof ShipModel)
this.ship = (ShipModel)s;
}
public void setDiagramModel(Object e) {
if (e instanceof DiagramModel)
this.diagram = (DiagramModel)e;
}
public void setLayout(Rectangle r) {
if (ship == null)
return;
ship.setLayout(r);
}
@Override
public boolean canExecute() {
if (ship == null || diagram == null)
return false;
return true;
}
@Override
public void execute() {
diagram.addChild(ship);
}
@Override
public boolean canUndo() {
if (ship == null || diagram == null)
return false;
return diagram.contains(ship);
}
@Override
public void undo() {
diagram.removeChild(ship);
}
}
|