shapeseditor.java
来自「mywork是rcp开发的很好的例子」· Java 代码 · 共 407 行 · 第 1/2 页
JAVA
407 行
new AlignmentAction((IWorkbenchPart)this,PositionConstants.RIGHT), new AlignmentAction((IWorkbenchPart)this,PositionConstants.TOP), new AlignmentAction((IWorkbenchPart)this,PositionConstants.MIDDLE), new AlignmentAction((IWorkbenchPart)this,PositionConstants.BOTTOM), new MatchWidthAction((IWorkbenchPart)this), new MatchHeightAction((IWorkbenchPart)this), new ChangePropertiesAction((IWorkbenchPart)this), //查看编辑属性 new ExpandAction((IWorkbenchPart)this), //mm展开/折叠节点 new ChangeConnectionStyleAction((IWorkbenchPart)this), //更换连线样式 new SelectSameAction((IWorkbenchPart)this), //更换连线样式 new ChangeLayoutAction((IWorkbenchPart)this,FreeNoteConstants.LAYOUT_NORMAL), //mm样式 new ChangeLayoutAction((IWorkbenchPart)this,FreeNoteConstants.LAYOUT_HANGING), //mm样式 new ChangeLayoutAction((IWorkbenchPart)this,FreeNoteConstants.LAYOUT_STAR) //mm样式 }; for (IAction a : actions) { ar.registerAction(a); getSelectionActions().add(a.getId()); } //其他action ar.registerAction(new SaveAsAction(this.getSite().getWorkbenchWindow())); ar.registerAction(new SaveAllAction(this.getSite().getWorkbenchWindow())); ar.registerAction(new ExportShapeAction(this.getSite().getWorkbenchWindow())); ar.registerAction(new ChangeZoomLevelAction(this.getSite().getPage())); } // 绑定键盘快捷键 protected KeyHandler getCommonKeyHandler() { if (sharedKeyHandler == null) { sharedKeyHandler = new KeyHandler(); ActionRegistry ar = getActionRegistry(); sharedKeyHandler.put(KeyStroke.getPressed(SWT.F2, 0), ar.getAction(GEFActionConstants.DIRECT_EDIT)); //19为^S,115为小写字母s,keystroke的实现还要进一步了解,TODO:不知道ctrl+a被哪一层截取了 sharedKeyHandler.put(KeyStroke.getPressed((char)01,(int)'a',SWT.CTRL), ar.getAction(ActionFactory.SELECT_ALL.getId())); sharedKeyHandler.put(KeyStroke.getPressed((char)19,(int)'s',SWT.CTRL), ar.getAction(ActionFactory.SAVE.getId())); sharedKeyHandler.put(KeyStroke.getPressed((char)127,127,0), ar.getAction(ActionFactory.DELETE.getId())); sharedKeyHandler.put(KeyStroke.getPressed((char)26,(int)'z',SWT.CTRL), ar.getAction(ActionFactory.UNDO.getId())); sharedKeyHandler.put(KeyStroke.getPressed((char)25,(int)'y',SWT.CTRL), ar.getAction(ActionFactory.REDO.getId())); sharedKeyHandler.put(KeyStroke.getPressed((char)16,(int)'p',SWT.CTRL), ar.getAction(FreeNoteConstants.PROPERTY_EDITOR)); } return sharedKeyHandler; } /* * (non-Javadoc) * * @see org.eclipse.gef.ui.parts.GraphicalEditor#commandStackChanged(java.util.EventObject) */ public void commandStackChanged(EventObject event) { firePropertyChange(IEditorPart.PROP_DIRTY); super.commandStackChanged(event); } /* * (non-Javadoc) * * @see org.eclipse.gef.ui.parts.GraphicalEditorWithFlyoutPalette#createPaletteViewerProvider() */ protected PaletteViewerProvider createPaletteViewerProvider() { return new PaletteViewerProvider(getEditDomain()) { protected void configurePaletteViewer(PaletteViewer viewer) { super.configurePaletteViewer(viewer); viewer.addDragSourceListener(new TemplateTransferDragSourceListener(viewer)); } }; } /** * Create a transfer drop target listener. When using a * CombinedTemplateCreationEntry tool in the palette, this will enable model * element creation by dragging from the palette. * * @see #createPaletteViewerProvider() */ private TransferDropTargetListener createTransferDropTargetListener() { return new TemplateTransferDropTargetListener(getGraphicalViewer()) { protected CreationFactory getFactory(Object template) { return new SimpleFactory((Class) template); } }; } /* * (non-Javadoc) * * @see org.eclipse.ui.ISaveablePart#doSave(org.eclipse.core.runtime.IProgressMonitor) */ public void doSave(IProgressMonitor monitor) { //如果未设置文件名,则视同另存 if(getEditorInput() instanceof NullEditorInput){ doSaveAs(); }else{ File file = ((IPathEditorInput) getEditorInput()).getPath().toFile(); writeContent(file); } } private String openFileDialog() { FileDialog fd=new FileDialog(getSite().getShell(),SWT.SAVE); fd.setText("请输入一个文件名"); fd.setFilterExtensions(new String[]{"*.fn"}); return fd.open(); } /* * (non-Javadoc) * * @see org.eclipse.ui.ISaveablePart#doSaveAs() */ public void doSaveAs() { String path=openFileDialog(); if(path !=null){ if(!path.toLowerCase().endsWith(".fn"))path += ".fn"; File file = new File(path); if(! file.exists() || MessageDialog.openConfirm(this.getSite().getShell(), "文件覆盖确认","文件已经存在,是否覆盖?")){ writeContent(file); setInput(new PathEditorInput(new Path(path))); OpenShapeAction.addToMRU(path); } } } public Object getAdapter(Class type){ if(type == ZoomManager.class) return getGraphicalViewer().getProperty(ZoomManager.class.toString()); return super.getAdapter(type); } ShapesDiagram getModel() { return diagram; } protected PaletteRoot getPaletteRoot() { if (PALETTE_MODEL == null) PALETTE_MODEL = ShapesEditorPaletteFactory.createPalette(); return PALETTE_MODEL; } private void handleLoadException(Exception e) { System.err.println("** Load failed. Using default model. **"); diagram = new ShapesDiagram(); } protected void initializeGraphicalViewer() { super.initializeGraphicalViewer(); GraphicalViewer viewer = getGraphicalViewer(); viewer.setContents(getModel()); // set the contents of this editor // listen for dropped parts viewer.addDropTargetListener(createTransferDropTargetListener()); } @Override public GraphicalViewer getGraphicalViewer() { return super.getGraphicalViewer(); } public boolean isSaveAsAllowed() { return true; } //写文件 private void writeContent(File file){ try { FileOutputStream fos = new FileOutputStream(file);// ObjectOutputStream oos = new ObjectOutputStream(fos);// oos.writeObject(getModel());// oos.close(); xstream.toXML(getModel(), fos); fos.close(); getCommandStack().markSaveLocation(); } catch (IOException e) { e.printStackTrace(); } } //读文件 protected void setInput(IEditorInput input) { super.setInput(input); //处理示例 try { if(input instanceof NullEditorInput && StringUtil.isNotNull(((NullEditorInput)input).getToolTipText())){ String path=((NullEditorInput)input).getToolTipText(); URL url=FileLocator.find(ShapesPlugin.getDefault().getBundle(), new Path(path),null); InputStream in = url.openStream(); diagram= (ShapesDiagram) xstream.fromXML(in); in.close(); }else{ File file = ((IPathEditorInput) input).getPath().toFile(); FileInputStream fis = new FileInputStream(file);// ObjectInputStream in = new ObjectInputStream(fis);// diagram = (ShapesDiagram) in.readObject();// in.close(); diagram= (ShapesDiagram) xstream.fromXML(fis); fis.close(); setPartName(file.getName()); } } catch (Exception e) { handleLoadException(e); } } public IAction getAction(String id) { return getActionRegistry().getAction(id); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?