⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractgraphicaleditor.java

📁 对eclipse gef进行封装,可以生成图形化编辑器
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        }
        return fModel;
	}
    
	
	abstract protected Object createDiagram();
	
    /**
     * 另存为操作时保存到resource中
     * @param os
     * @throws IOException
     */
    private void createOutputStream(OutputStream os) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            resource.save(out,Collections.EMPTY_MAP);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    
    private KeyHandler getKeyHandler() {
        if (keyHandler == null) {
            keyHandler = new EosKeyHandler(getGraphicalViewer());
            keyHandler
                    .put(KeyStroke.getPressed(SWT.DEL, 127, 0),
                            getActionRegistry().getAction(
                                    ActionFactory.DELETE.getId()));
                            
            keyHandler.put(KeyStroke.getPressed(SWT.F2, 0),
                    getActionRegistry().getAction(GEFActionConstants.DIRECT_EDIT));    
         
            
            keyHandler.put(KeyStroke.getPressed('\r',13, 0),
                    getActionRegistry().getAction(Constants.ENTER));   
              
        }
        return keyHandler;
    }
    
    /* (non-Javadoc)q
     * @see org.eclipse.ui.ISaveablePart#doSaveAs()
     */
    public void doSaveAs() {
        // Show a SaveAs dialog
        Shell shell = getSite().getWorkbenchWindow().getShell();
        SaveAsDialog dialog = new SaveAsDialog(shell);
        dialog.setOriginalFile(((IFileEditorInput) getEditorInput()).getFile());
        dialog.open();
        
        IPath path = dialog.getResult();    
        if (path != null) {
            // try to save the editor's contents under a different file name
            final IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
            try {
                new ProgressMonitorDialog(shell).run(
                        false, // don't fork
                        false, // not cancelable
                        new WorkspaceModifyOperation() { // run this operation
                            public void execute(final IProgressMonitor monitor) {
                                try {
                                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                                    createOutputStream(out);
                                    file.create(
                                            new ByteArrayInputStream(out.toByteArray()), // contents
                                            true, // keep saving, even if IFile is out of sync with the Workspace
                                            monitor); // progress monitor
                                }
                                catch (CoreException ce) { ce.printStackTrace(); }
                                catch (IOException ioe) { ioe.printStackTrace(); } 
                            }
                        });
                // set input to the new file
                setInput(new FileEditorInput(file));
                getCommandStack().markSaveLocation();
            }
            catch (InterruptedException ie) {
                // should not happen, since the monitor dialog is not cancelable
                ie.printStackTrace(); 
            }
            catch (InvocationTargetException ite) { 
                ite.printStackTrace(); 
            }
        } 
    }
    
    public String getCompilerFileFullPath(){
    	 final IFile file = ((IFileEditorInput) getEditorInput()).getFile();
         String result = file.getFullPath().toOSString()+"g";
         return result;
    }
    /**
     * 保存模型文件
     */
    public void doSave(IProgressMonitor monitor) {
        try
        {
            final IFile file = ((IFileEditorInput) getEditorInput()).getFile();
            if (file.exists()
                || MessageDialogWithToggle.openConfirm(
                    getSite().getShell(),
                    "createfile",
                    "The file '"
                        + file.getName()
                        + "' doesn't exist. Click OK to create it.")) //$NON-NLS-1$
            {
                IWorkspaceRunnable op = new IWorkspaceRunnable() {  //放到一个workspace runnable中,fix bug: 7489 
                    public void run(IProgressMonitor monitor)
                            throws CoreException {                  
                        saveProperties();
                        save(file, monitor);
                        getCommandStack().markSaveLocation();                    
                    }
                };
                ResourcesPlugin.getWorkspace().run(op, monitor);
            }
        }
        catch (CoreException e)
        {
            ErrorDialog.openError(
                getSite().getShell(),
                "Error During Save",
                "The current model could not be saved.",
                e.getStatus());
        }
    }

    /**
     * 保存文件内容
     * @param file 文件资源
     * @param progressMonitor
     * @throws CoreException
     */
	protected void save(IFile file, IProgressMonitor progressMonitor)
		throws CoreException {
		// TODO Auto-generated method stub
	  if (null == progressMonitor)
	        progressMonitor = new NullProgressMonitor();
	
	    progressMonitor.beginTask("Saving " + file, 2);
	
	    if (null == modelManager)
	    {
	        IStatus status =
	            new Status(
	                IStatus.ERROR,
	                getPluginId(),
	                0,
	                "No model manager found for saving the file.",
	                null);
	        throw new CoreException(status);
	    }
	
	    // save biz to file
	    try
	    {
	        modelManager.save(file.getFullPath());
	
	        progressMonitor.worked(1);
	        file.refreshLocal(
	            IResource.DEPTH_ZERO,
	            new SubProgressMonitor(progressMonitor, 1));
	        progressMonitor.done();
	    }
	    catch (FileNotFoundException e)
	    {
	        IStatus status =
	            new Status(
	                IStatus.ERROR,
	                getPluginId(),
	                0,
	                "Error writing file.",
	                e);
	        throw new CoreException(status);
	    }
	    catch (IOException e)
	    {
	        IStatus status =
	            new Status(
	                IStatus.ERROR,
	                getPluginId(),
	                0,
	                "Error writing file.",
	                e);
	        throw new CoreException(status);
	    }
	}
    
    /**
     * 保存编辑器属性
     *
     */
	protected void saveProperties() {
		// TODO Auto-generated method stub

	}
    
    /**
     * 初始化编辑器属性
     *
     */
	protected void loadProperties() {
		// TODO Auto-generated method stub

	}
    
    /**
     * 创建模型管理器
     * @return
     */
    abstract protected ModelManager createModelManager();
    
    /**
     * 创建属性菜单管理器
     * @return
     */
    abstract protected AbstractPropertyMenuManager createMenuManager();
    
    /**
     * 创建编辑器弹出菜单provider
     * @return
     */
    abstract protected ContextMenuProvider CreateContextMenuProvider();
    /**
     * 选中节点
     * @param node
     */
    public void selectNodeElement(Object node){
        EditPartViewer viewer = getGraphicalViewer(); 
        Object editPart = viewer.getEditPartRegistry().get(node);
        locateToEditPart((EditPart)editPart);
        viewer.select((EditPart)editPart);
    }
    
    /**
     * 定位EditPart
     * @param editPart
     */
    public void locateToEditPart(EditPart editPart) {
        getGraphicalViewer().reveal(editPart);
    }
    
    public GraphicalViewer getGraphicalViewer() {
        return super.getGraphicalViewer();
    }
    
    /**
     * 创建GraphicalViewer
     */
    protected void createGraphicalViewer(Composite parent) {
//        rulerComp = new RulerComposite(parent, SWT.NONE);
        super.createGraphicalViewer(parent);
//        rulerComp.setGraphicalViewer((ScrollingGraphicalViewer) getGraphicalViewer());
    }
    
    protected void configureGraphicalViewer() {
        super.configureGraphicalViewer();
        
        GraphicalViewer viewer = getGraphicalViewer();  
        SelectionManager selectionManager = new SelectionManager();
        viewer.setSelectionManager(selectionManager);
        ScalableFreeformRootEditPart root = new ScalableFreeformRootEditPart();
        viewer.setRootEditPart(root);
        viewer.setKeyHandler(getKeyHandler());
      
        

        Action action = new ZoomInAction(root.getZoomManager());
        getActionRegistry().registerAction(action);
        getSite().getKeyBindingService().registerAction(action);
        action = new ZoomOutAction(root.getZoomManager());
        getActionRegistry().registerAction(action);
        getSite().getKeyBindingService().registerAction(action);
        try{
            loadProperties();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        
        DiagramEditPartFactory factory = ExtenderReader.getInstance().getEditPartFactory(getPluginId());
//        DiagramEditPartFactory factory = new DiagramEditPartFactory();
        factory.setEditor(this);
        viewer.setEditPartFactory(factory);
        fContextMenuProvider = (AbstractContextMenuProvider)CreateContextMenuProvider();
        fContextMenuProvider.iniContextMenuProvider(viewer,  getActionRegistry());
        viewer.setContextMenu(fContextMenuProvider);
        getSite().registerContextMenu(fContextMenuProvider, viewer);
    }
    
    
    /**
     * 初始化编辑器的内容
     */
    protected void initializeGraphicalViewer() {
        super.initializeGraphicalViewer();
        GraphicalViewer viewer = getGraphicalViewer();
        viewer.setContents(getModel());
        // add the ShortestPathConnectionRouter
        ScalableFreeformRootEditPart root = 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -