Search in sources :

Example 6 with ScalableFreeformRootEditPart

use of org.eclipse.gef.editparts.ScalableFreeformRootEditPart in project yamcs-studio by yamcs.

the class OPIRuntimeDelegate method createGUI.

public void createGUI(Composite parent) {
    viewer = new PatchedScrollingGraphicalViewer();
    if (displayModel != null) {
        displayModel.setOpiRuntime(opiRuntime);
        displayModel.setViewer(viewer);
    }
    ScalableFreeformRootEditPart root = new PatchedScalableFreeformRootEditPart() {

        // In Run mode, clicking the Display or container should de-select
        // all widgets.
        @Override
        public DragTracker getDragTracker(Request req) {
            return new DragEditPartsTracker(this);
        }

        @Override
        public boolean isSelectable() {
            return false;
        }
    };
    // set clipping strategy for connection layer of connection can be hide
    // when its source or target is not showing.
    ConnectionLayer connectionLayer = (ConnectionLayer) root.getLayer(LayerConstants.CONNECTION_LAYER);
    connectionLayer.setClippingStrategy(new PatchedConnectionLayerClippingStrategy(connectionLayer));
    viewer.createControl(parent);
    viewer.setRootEditPart(root);
    viewer.setEditPartFactory(new WidgetEditPartFactory(ExecutionMode.RUN_MODE, site));
    // viewer.addDropTargetListener(new
    // ProcessVariableNameTransferDropPVTargetListener(viewer));
    // viewer.addDropTargetListener(new
    // TextTransferDropPVTargetListener(viewer));
    // Add drag listener will make click feel stagnant.
    // viewer.addDragSourceListener(new DragPVSourceListener(viewer));
    // this will make viewer as a selection provider
    EditDomain editDomain = new EditDomain() {

        @Override
        public void loadDefaultTool() {
            setActiveTool(new RuntimePatchedSelectionTool());
        }
    };
    editDomain.addViewer(viewer);
    // connect the CSS menu
    ContextMenuProvider cmProvider = new OPIRunnerContextMenuProvider(viewer, opiRuntime);
    viewer.setContextMenu(cmProvider);
    opiRuntime.getSite().registerContextMenu(cmProvider, viewer);
    if (displayModelFilled) {
        viewer.setContents(displayModel);
        displayModel.setViewer(viewer);
        displayModel.setOpiRuntime(opiRuntime);
        updateEditorTitle();
    }
    zoomManager = root.getZoomManager();
    if (zoomManager != null) {
        List<String> zoomLevels = new ArrayList<>(3);
        zoomLevels.add(Draw2dSingletonUtil.ZoomManager_FIT_ALL);
        zoomLevels.add(Draw2dSingletonUtil.ZoomManager_FIT_WIDTH);
        zoomLevels.add(Draw2dSingletonUtil.ZoomManager_FIT_HEIGHT);
        zoomManager.setZoomLevelContributions(zoomLevels);
        zoomManager.setZoomLevels(createZoomLevels());
    // IAction zoomIn = new ZoomInAction(zoomManager);
    // IAction zoomOut = new ZoomOutAction(zoomManager);
    // getActionRegistry().registerAction(zoomIn);
    // getActionRegistry().registerAction(zoomOut);
    }
    /* scroll-wheel zoom */
    viewer.setProperty(MouseWheelHandler.KeyGenerator.getKey(SWT.MOD1), MouseWheelZoomHandler.SINGLETON);
    /*
         * When Figure instance which corresponds to RootEditPart is updated, calculate the frame rate and set the
         * measured rate to "frame_rate" property of the corresponding DisplayModel instance.
         *
         * By default, org.eclipse.draw2d.DeferredUpdateManager is used. This update manager queues update requests from
         * figures and others, and it repaints requested figures at once when GUI thread is ready to repaint.
         * notifyPainting() method of UpdateLister is called when it repaints. The frame rate is calculated based on the
         * timing of notifyPainting().
         *
         * Note that the update manager repaints only requested figures. It does not repaint all figures at once. For
         * example, if there are only two widgets in one display, these widgets might be repainted alternately. In that
         * case, the frame rate indicates the inverse of the time between the repainting of one widget and the
         * repainting of the other widget, which is different from our intuition. Thus, you have to be careful about the
         * meaning of "frame rate" calculated by the following code.
         */
    if (displayModelFilled && displayModel.isFreshRateEnabled()) {
        UpdateManager updateManager = root.getFigure().getUpdateManager();
        updateManager.addUpdateListener(new UpdateListener() {

            // in milliseconds
            private long updateCycle = -1;

            private Date previousDate = null;

            @Override
            public void notifyPainting(Rectangle damage, @SuppressWarnings("rawtypes") Map dirtyRegions) {
                Date currentDate = new Date();
                if (previousDate == null) {
                    previousDate = currentDate;
                    return;
                }
                synchronized (previousDate) {
                    updateCycle = currentDate.getTime() - previousDate.getTime();
                    displayModel.setFrameRate(1000.0 / updateCycle);
                    previousDate = currentDate;
                }
            }

            @Override
            public void notifyValidating() {
            // Do nothing
            }
        });
    }
}
Also used : EditDomain(org.eclipse.gef.EditDomain) WidgetEditPartFactory(org.csstudio.opibuilder.editparts.WidgetEditPartFactory) Request(org.eclipse.gef.Request) ArrayList(java.util.ArrayList) Rectangle(org.eclipse.draw2d.geometry.Rectangle) DragEditPartsTracker(org.eclipse.gef.tools.DragEditPartsTracker) ConnectionLayer(org.eclipse.draw2d.ConnectionLayer) ContextMenuProvider(org.eclipse.gef.ContextMenuProvider) Date(java.util.Date) ScalableFreeformRootEditPart(org.eclipse.gef.editparts.ScalableFreeformRootEditPart) UpdateManager(org.eclipse.draw2d.UpdateManager) UpdateListener(org.eclipse.draw2d.UpdateListener) Map(java.util.Map)

Example 7 with ScalableFreeformRootEditPart

use of org.eclipse.gef.editparts.ScalableFreeformRootEditPart in project jbosstools-hibernate by jbosstools.

the class ExportImageAction method run.

public void run() {
    boolean createdSaveDialog = false;
    if (saveDialog == null) {
        saveDialog = new SaveAsDialog(getDiagramViewer().getSite().getWorkbenchWindow().getShell());
        createdSaveDialog = true;
    }
    saveDialog.setOriginalName(getDiagramViewer().getStoreFileName());
    saveDialog.open();
    final IPath pathSave = saveDialog.getResult();
    saveDialog = null;
    if (pathSave == null) {
        return;
    }
    final IFigure fig = ((ScalableFreeformRootEditPart) getDiagramViewer().getEditPartViewer().getRootEditPart()).getLayer(LayerConstants.PRINTABLE_LAYERS);
    int imageTypeTmp = SWT.IMAGE_BMP;
    String ext = pathSave.getFileExtension();
    if (ext != null) {
        ext = ext.toLowerCase();
        if (ext.endsWith("jpg")) {
            // $NON-NLS-1$
            imageTypeTmp = SWT.IMAGE_JPEG;
        } else if (ext.endsWith("png")) {
            // $NON-NLS-1$
            imageTypeTmp = SWT.IMAGE_PNG;
        } else if (ext.endsWith("gif")) {
            // $NON-NLS-1$
            imageTypeTmp = SWT.IMAGE_GIF;
        } else if (ext.endsWith("bmp")) {
            // $NON-NLS-1$
            imageTypeTmp = SWT.IMAGE_BMP;
        }
    }
    IPath pathTmp = pathSave;
    if (ext == null) {
        // $NON-NLS-1$
        pathTmp = pathTmp.addFileExtension("bmp");
    }
    final IPath path = pathTmp;
    final int imageType = imageTypeTmp;
    final IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
    WorkspaceModifyOperation op = new WorkspaceModifyOperation() {

        public void execute(final IProgressMonitor monitor) {
            ByteArrayInputStream inputStream = null;
            try {
                if (file != null) {
                    byte[] imageData = createImage(fig, imageType);
                    if (file.exists()) {
                        file.delete(true, null);
                    }
                    if (!file.exists()) {
                        inputStream = new ByteArrayInputStream(imageData);
                        file.create(inputStream, true, null);
                    }
                }
            } catch (CoreException e) {
                // $NON-NLS-1$
                HibernateConsolePlugin.getDefault().logErrorMessage("ExportImageAction", e);
                if (showErrDialog) {
                    MessageDialog.openInformation(getDiagramViewer().getSite().getShell(), DiagramViewerMessages.ExportImageAction_error, DiagramViewerMessages.ExportImageAction_failed_to_export_image + e.getMessage());
                }
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                    // ignore
                    }
                }
            }
        }
    };
    try {
        new ProgressMonitorDialog(createdSaveDialog ? getDiagramViewer().getSite().getWorkbenchWindow().getShell() : null).run(false, true, op);
    } catch (InvocationTargetException e) {
    } catch (InterruptedException e) {
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) SaveAsDialog(org.eclipse.ui.dialogs.SaveAsDialog) WorkspaceModifyOperation(org.eclipse.ui.actions.WorkspaceModifyOperation) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) ScalableFreeformRootEditPart(org.eclipse.gef.editparts.ScalableFreeformRootEditPart) ByteArrayInputStream(java.io.ByteArrayInputStream) IFigure(org.eclipse.draw2d.IFigure)

Example 8 with ScalableFreeformRootEditPart

use of org.eclipse.gef.editparts.ScalableFreeformRootEditPart in project jbosstools-hibernate by jbosstools.

the class ExportImageActionTest method testAction.

public void testAction() {
    final DiagramViewer editor = context.mock(DiagramViewer.class);
    final SaveAsDialog saveDialog = context.mock(SaveAsDialog.class);
    final GraphicalViewer graphicalViewer = context.mock(GraphicalViewer.class);
    final ScalableFreeformRootEditPart scalableFreeformRootEditPart = context.mock(ScalableFreeformRootEditPart.class);
    final IFigure figure = context.mock(IFigure.class);
    final Control control = context.mock(Control.class);
    final Display display = context.mock(Display.class);
    final Rectangle rectangle = new Rectangle(0, 0, 20, 10);
    // $NON-NLS-1$
    final String filePath = PROJECT_NAME + File.separator + "test.jpg";
    final IPath resPath = new Path(filePath);
    context.checking(new Expectations() {

        {
            allowing(editor).getStoreFileName();
            will(returnValue(filePath));
            allowing(saveDialog).setOriginalName(filePath);
            oneOf(saveDialog).open();
            will(returnValue(0));
            oneOf(saveDialog).getResult();
            will(returnValue(resPath));
            allowing(editor).getEditPartViewer();
            will(returnValue(graphicalViewer));
            allowing(graphicalViewer).getRootEditPart();
            will(returnValue(scalableFreeformRootEditPart));
            allowing(scalableFreeformRootEditPart).getLayer(LayerConstants.PRINTABLE_LAYERS);
            will(returnValue(figure));
            allowing(figure).getBounds();
            will(returnValue(rectangle));
        }
    });
    final ExportImageAction exportImageAction = new ExportImageAction(editor);
    exportImageAction.setSaveDialog(saveDialog);
    exportImageAction.setShowErrDialog(false);
    exportImageAction.run();
    // test is the file created
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IPath path = workspace.getRoot().getFullPath().append(resPath);
    path = workspace.getRoot().getLocation().append(path);
    File file = path.toFile();
    assertTrue(file.exists() && file.isFile());
    // 
    boolean res = file.delete();
    assertTrue(res);
    // GENERAL TEST:
    // check for all expectations
    context.assertIsSatisfied();
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) Expectations(org.jmock.Expectations) GraphicalViewer(org.eclipse.gef.GraphicalViewer) IPath(org.eclipse.core.runtime.IPath) SaveAsDialog(org.eclipse.ui.dialogs.SaveAsDialog) Rectangle(org.eclipse.draw2d.geometry.Rectangle) ExportImageAction(org.jboss.tools.hibernate.ui.diagram.editors.actions.ExportImageAction) DiagramViewer(org.jboss.tools.hibernate.ui.diagram.editors.DiagramViewer) Control(org.eclipse.swt.widgets.Control) ScalableFreeformRootEditPart(org.eclipse.gef.editparts.ScalableFreeformRootEditPart) IWorkspace(org.eclipse.core.resources.IWorkspace) File(java.io.File) IFigure(org.eclipse.draw2d.IFigure) Display(org.eclipse.swt.widgets.Display)

Example 9 with ScalableFreeformRootEditPart

use of org.eclipse.gef.editparts.ScalableFreeformRootEditPart in project jbosstools-hibernate by jbosstools.

the class AbstractGraphViewPart method initActions.

private void initActions() {
    ScalableFreeformRootEditPart root = ((ScalableFreeformRootEditPart) viewer.getRootEditPart());
    this.actionGroup = new GraphViewActionGroup(this, ConsolePreferencesConstants.ENTITY_MODEL_LAYOUT, root);
    IActionBars actionBars = getViewSite().getActionBars();
    this.actionGroup.fillActionBars(actionBars);
    List<String> zoomLevels = new ArrayList<String>(3);
    zoomLevels.add(ZoomManager.FIT_ALL);
    zoomLevels.add(ZoomManager.FIT_WIDTH);
    zoomLevels.add(ZoomManager.FIT_HEIGHT);
    root.getZoomManager().setZoomLevelContributions(zoomLevels);
    root.getZoomManager().setZoomLevels(new double[] { .067, .125, .25, .5, .75, 1.0, 1.5, 2.0, 2.5, 3, 4 });
}
Also used : ScalableFreeformRootEditPart(org.eclipse.gef.editparts.ScalableFreeformRootEditPart) ArrayList(java.util.ArrayList) IActionBars(org.eclipse.ui.IActionBars)

Example 10 with ScalableFreeformRootEditPart

use of org.eclipse.gef.editparts.ScalableFreeformRootEditPart in project jbosstools-hibernate by jbosstools.

the class AbstractGraphViewPart method createPartControl.

public void createPartControl(Composite parent) {
    viewer = new ScrollingGraphicalViewer();
    viewer.createControl(parent);
    viewer.setRootEditPart(new ScalableFreeformRootEditPart());
    viewer.getControl().setBackground(ColorConstants.white);
    viewer.setEditPartFactory(createEditPartFactory());
    editDomain.addViewer(viewer);
    getSite().setSelectionProvider(viewer);
    setupListener();
    initActions();
}
Also used : ScrollingGraphicalViewer(org.eclipse.gef.ui.parts.ScrollingGraphicalViewer) ScalableFreeformRootEditPart(org.eclipse.gef.editparts.ScalableFreeformRootEditPart)

Aggregations

ScalableFreeformRootEditPart (org.eclipse.gef.editparts.ScalableFreeformRootEditPart)25 Viewport (org.eclipse.draw2d.Viewport)6 Point (org.eclipse.draw2d.geometry.Point)6 Rectangle (org.eclipse.draw2d.geometry.Rectangle)6 RootEditPart (org.eclipse.gef.RootEditPart)6 GraphicalViewerKeyHandler (org.eclipse.gef.ui.parts.GraphicalViewerKeyHandler)5 ScrollingGraphicalViewer (org.eclipse.gef.ui.parts.ScrollingGraphicalViewer)5 ZoomManager (org.eclipse.gef.editparts.ZoomManager)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 EditPart (org.eclipse.gef.EditPart)3 ExtendedViewportAutoexposeHelper (com.archimatetool.editor.diagram.util.ExtendedViewportAutoexposeHelper)2 IPath (org.eclipse.core.runtime.IPath)2 IFigure (org.eclipse.draw2d.IFigure)2 Insets (org.eclipse.draw2d.geometry.Insets)2 PrecisionPoint (org.eclipse.draw2d.geometry.PrecisionPoint)2 ContextMenuProvider (org.eclipse.gef.ContextMenuProvider)2 GraphicalViewer (org.eclipse.gef.GraphicalViewer)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2 SaveAsDialog (org.eclipse.ui.dialogs.SaveAsDialog)2