6.16 图形的缩放实现zoomManager

时间:2022-03-21 19:34:46

http://blog.csdn.net/evilinside/article/details/5153657

实现图形缩放的步骤:

 

 1 protected void configureGraphicalViewer() {
 2         super.configureGraphicalViewer();
 3 
 4         viewer = getGraphicalViewer();
 5

             1.提供缩放能力。 设置根图形的RootEditPartScalableRootEditPart:

 6         ScalableRootEditPart rootEditPart = new ScalableRootEditPart();
 7         viewer.setRootEditPart(rootEditPart);
 8

             2.实际缩放的实现。ScalableRootEditPart 提供了一个 ZoomManager
             类,可以被用来管理图形的最大化,最小化等操作。对图形的缩放操作实际上是通过这个
             ZoomManager 实现的。

 9         ZoomManager zoomManager = rootEditPart.getZoomManager();   //获得ZoomManager
//注册放大Action
10 IAction action = new ZoomInAction(zoomManager); 11 getActionRegistry().registerAction(action);
//注册缩小Action
12 action = new ZoomOutAction(zoomManager); 13 getActionRegistry().registerAction(action); 14 15 viewer.setEditPartFactory(new GraphicalPartFactory());这个是用来干嘛的?? 16 17 ContextMenuProvider provider = new DiagramContextMenuProvider(viewer, 18 getActionRegistry()); 19 viewer.setContextMenu(provider); 20 getSite().registerContextMenu(provider, viewer); 21 22 KeyHandler keyHandler = new KeyHandler(); 23 keyHandler.put(KeyStroke.getPressed(SWT.DEL, 127, 0), 24 getActionRegistry().getAction(ActionFactory.DELETE.getId())); 25 getGraphicalViewer().setKeyHandler( 26 new GraphicalViewerKeyHandler(getGraphicalViewer()) 27 .setParent(keyHandler)); 28 29 }
  1 package ttcn3.gft.editor.ui.actions;
  2 
  3 import org.eclipse.gef.ui.actions.ActionBarContributor;
  4 
  5 import org.eclipse.gef.ui.actions.DeleteRetargetAction;
  6 import org.eclipse.gef.ui.actions.GEFActionConstants;
  7 import org.eclipse.gef.ui.actions.RedoRetargetAction;
  8 import org.eclipse.gef.ui.actions.UndoRetargetAction;
  9 import org.eclipse.gef.ui.actions.ZoomInRetargetAction;
 10 import org.eclipse.gef.ui.actions.ZoomOutRetargetAction;
 11 import org.eclipse.jface.action.IToolBarManager;
 12 import org.eclipse.jface.action.Separator;
 13 import org.eclipse.ui.IEditorPart;
 14 import org.eclipse.ui.actions.ActionFactory;
 15 import org.eclipse.ui.actions.RetargetAction;
 16 
 17 import ttcn3.gft.editor.action.FilterAction;
 18 import ttcn3.gft.editor.editor.DiagramEditor;
 19 import ttcn3.gft.editor.ui.helper.IImageKey;
 20 
 21 /**
 22  * action bar for diagram editor
 23  * 
 24  * @author user
 25  * 
 26  */
 27 
 28 public class TTCNActionBarContributor extends ActionBarContributor {
 29     3.添加缩放工具按钮
 30     @Override
 31     protected void buildActions() {
 32         // TODO Auto-generated method stub
 33         RetargetAction retargetAction = new RetargetAction(ActionFactory.SAVE.getId(), "SAVE");
 35         retargetAction.setImageDescriptor(IImageKey.getImageDescriptor(IImageKey.KEY_SAVE));
 37         addRetargetAction(retargetAction);
 38 
 39         retargetAction = new RetargetAction(ActionFactory.COPY.getId(), "COPY");
 40         retargetAction.setImageDescriptor(IImageKey.getImageDescriptor(IImageKey.KEY_COPY));
 42         addRetargetAction(retargetAction);
 43 
 44         retargetAction = new RetargetAction(ActionFactory.PASTE.getId(),"PASTE");
 46         retargetAction.setImageDescriptor(IImageKey.getImageDescriptor(IImageKey.KEY_PASTE));
 48         addRetargetAction(retargetAction);
 49 
 50         retargetAction = new UndoRetargetAction();
 51         addRetargetAction(retargetAction);
 52 
 53         retargetAction = new RedoRetargetAction();
 54         addRetargetAction(retargetAction);
 55 
 56         retargetAction = new DeleteRetargetAction();
 57         retargetAction.setImageDescriptor(IImageKey.getImageDescriptor(IImageKey.KEY_DELETE));
 59         addRetargetAction(retargetAction);
 60         
 61         addRetargetAction(new ZoomInRetargetAction());  62         addRetargetAction(new ZoomOutRetargetAction());  63 
 64         retargetAction = new RetargetAction(GenerateAction.getID(), "Generate");
 65         retargetAction.setImageDescriptor(IImageKey.getImageDescriptor(IImageKey.KEY_GENERATE));
 67         addRetargetAction(retargetAction);
 68 
 69         retargetAction = new RetargetAction(FilterAction.getID(), "Filter");
 70         retargetAction.setImageDescriptor(IImageKey.getImageDescriptor(IImageKey.KEY_FILTER));
 72         addRetargetAction(retargetAction);
 73 
 74         retargetAction = new RetargetAction(CompileAction.getID(), "Compile");
 75         retargetAction.setImageDescriptor(IImageKey.getImageDescriptor(IImageKey.KEY_COMPILE));
 77         addRetargetAction(retargetAction);
 78 
 79         retargetAction = new RetargetAction(ActionFactory.PRINT.getId(),"Print");
 81         retargetAction.setImageDescriptor(IImageKey.getImageDescriptor(IImageKey.KEY_PRINT));
 83         addRetargetAction(retargetAction);
 84 
 85     }
 86 
 87     @Override
 88     protected void declareGlobalActionKeys() {
 89         // TODO Auto-generated method stub
 90 
 91     }
 92 
 93     @Override
 94     public void contributeToToolBar(IToolBarManager toolBarManager) {
 95         toolBarManager.add(getAction(ActionFactory.SAVE.getId()));
 96         toolBarManager.add(new Separator());
 97 
 98         toolBarManager.add(getAction(ActionFactory.COPY.getId()));
 99         toolBarManager.add(getAction(ActionFactory.PASTE.getId()));
100         toolBarManager.add(new Separator());
101 
102         toolBarManager.add(getAction(ActionFactory.UNDO.getId()));
103         toolBarManager.add(getAction(ActionFactory.REDO.getId()));
104         toolBarManager.add(getAction(ActionFactory.DELETE.getId()));
105         toolBarManager.add(new Separator());
106 
107         toolBarManager.add(getAction(GEFActionConstants.ZOOM_IN));
108         toolBarManager.add(getAction(GEFActionConstants.ZOOM_OUT));
109         toolBarManager.add(new Separator()); 110 
111         toolBarManager.add(getAction(FilterAction.getID()));
112         toolBarManager.add(getAction(GenerateAction.getID()));
113         toolBarManager.add(getAction(CompileAction.getID()));
114         toolBarManager.add(getAction(ActionFactory.PRINT.getId()));
115 
116         // toolBarManager.add(new ZoomComboContributionItem(getPage()));
117     }
118 
119     @Override
120     public void setActiveEditor(IEditorPart editor) {
121         // TODO Auto-generated method stub
122         super.setActiveEditor(editor);
123 
124         if (editor instanceof DiagramEditor) {
125             getActionBars().getStatusLineManager().setMessage(
126                     ((DiagramEditor) editor).getPartName());
127         }
128     }
129 }