Search in sources :

Example 51 with Canvas

use of org.eclipse.swt.widgets.Canvas in project pentaho-kettle by pentaho.

the class JobMetricsDelegate method setupContent.

public void setupContent() {
    if (metricsComposite.isDisposed()) {
        return;
    }
    // 
    for (Control control : metricsComposite.getChildren()) {
        if (!control.isDisposed()) {
            control.dispose();
        }
    }
    emptyGraph = false;
    canvas = new Canvas(metricsComposite, SWT.NONE);
    spoon.props.setLook(canvas);
    FormData fdCanvas = new FormData();
    fdCanvas.left = new FormAttachment(0, 0);
    fdCanvas.right = new FormAttachment(100, 0);
    fdCanvas.top = new FormAttachment(0, 0);
    fdCanvas.bottom = new FormAttachment(100, 0);
    canvas.setLayoutData(fdCanvas);
    metricsComposite.addControlListener(new ControlAdapter() {

        public void controlResized(ControlEvent event) {
            updateGraph();
        }
    });
    metricsComposite.addDisposeListener(new DisposeListener() {

        public void widgetDisposed(DisposeEvent event) {
            if (image != null) {
                image.dispose();
            }
        }
    });
    canvas.addPaintListener(new PaintListener() {

        public void paintControl(PaintEvent event) {
            if (jobGraph.job != null && (jobGraph.job.isFinished() || jobGraph.job.isStopped())) {
                refreshImage(event.gc);
                if (!Const.isRunningOnWebspoonMode()) {
                    if (image != null && !image.isDisposed()) {
                        event.gc.drawImage(image, 0, 0);
                    }
                }
            } else {
                Rectangle bounds = canvas.getBounds();
                if (bounds.width <= 0 || bounds.height <= 0) {
                    return;
                }
                event.gc.setForeground(GUIResource.getInstance().getColorWhite());
                event.gc.setBackground(GUIResource.getInstance().getColorWhite());
                event.gc.fillRectangle(new Rectangle(0, 0, bounds.width, bounds.height));
                event.gc.setForeground(GUIResource.getInstance().getColorBlack());
                String metricsMessage = BaseMessages.getString(PKG, "JobMetricsDelegate.JobIsNotRunning.Message");
                org.eclipse.swt.graphics.Point extent = event.gc.textExtent(metricsMessage);
                event.gc.drawText(metricsMessage, (bounds.width - extent.x) / 2, (bounds.height - extent.y) / 2);
            }
        }
    });
    // Refresh automatically every 5 seconds as well.
    // 
    final Timer timer = new Timer("JobMetricsDelegate Timer");
    timer.schedule(new TimerTask() {

        public void run() {
            updateGraph();
        }
    }, 0, 5000);
    // When the tab is closed, we remove the update timer
    // 
    jobMetricsTab.addDisposeListener(new DisposeListener() {

        public void widgetDisposed(DisposeEvent arg0) {
            timer.cancel();
        }
    });
    if (Const.isRunningOnWebspoonMode()) {
        // When the browser tab/window is closed, we remove the update timer
        jobMetricsTab.getDisplay().disposeExec(new Runnable() {

            @Override
            public void run() {
                timer.cancel();
            }
        });
    }
    // Show tool tips with details...
    // 
    canvas.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseDown(MouseEvent event) {
            if (drawAreas == null) {
                return;
            }
            for (int i = drawAreas.size() - 1; i >= 0; i--) {
                MetricsDrawArea drawArea = drawAreas.get(i);
                if (drawArea.getArea().contains(event.x, event.y)) {
                    MetricsDuration duration = drawArea.getDuration();
                    if (duration == null) {
                        continue;
                    }
                    System.out.println(duration.toString());
                    LoggingObjectInterface loggingObject = LoggingRegistry.getInstance().getLoggingObject(duration.getLogChannelId());
                    if (loggingObject == null) {
                        return;
                    }
                    System.out.println(loggingObject.getObjectType() + " : " + loggingObject.getObjectName());
                }
            }
        }
    });
    canvas.addControlListener(new ControlAdapter() {

        @Override
        public void controlResized(ControlEvent arg0) {
            // force a refresh
            lastRefreshTime = 0;
        }
    });
}
Also used : FormData(org.eclipse.swt.layout.FormData) MetricsDrawArea(org.pentaho.di.core.logging.MetricsPainter.MetricsDrawArea) DisposeListener(org.eclipse.swt.events.DisposeListener) PaintEvent(org.eclipse.swt.events.PaintEvent) MouseEvent(org.eclipse.swt.events.MouseEvent) ControlAdapter(org.eclipse.swt.events.ControlAdapter) PaintListener(org.eclipse.swt.events.PaintListener) Canvas(org.eclipse.swt.widgets.Canvas) Rectangle(org.eclipse.swt.graphics.Rectangle) MouseAdapter(org.eclipse.swt.events.MouseAdapter) Point(org.pentaho.di.core.gui.Point) DisposeEvent(org.eclipse.swt.events.DisposeEvent) Control(org.eclipse.swt.widgets.Control) Timer(java.util.Timer) TimerTask(java.util.TimerTask) ControlEvent(org.eclipse.swt.events.ControlEvent) LoggingObjectInterface(org.pentaho.di.core.logging.LoggingObjectInterface) MetricsDuration(org.pentaho.di.core.metrics.MetricsDuration) FormAttachment(org.eclipse.swt.layout.FormAttachment)

Example 52 with Canvas

use of org.eclipse.swt.widgets.Canvas in project pentaho-kettle by pentaho.

the class EnterPrintDialog method open.

public int open() {
    Shell parent = getParent();
    Display display = parent.getDisplay();
    retval = SWT.OK;
    shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.SHEET | SWT.RESIZE | SWT.MAX | SWT.MIN);
    props.setLook(shell);
    FormLayout formLayout = new FormLayout();
    formLayout.marginWidth = Const.FORM_MARGIN;
    formLayout.marginHeight = Const.FORM_MARGIN;
    shell.setLayout(formLayout);
    shell.setText(BaseMessages.getString(PKG, "EnterPrintDialog.Title"));
    int middle = props.getMiddlePct();
    int margin = Const.MARGIN;
    // Canvas
    wlCanvas = new Label(shell, SWT.NONE);
    wlCanvas.setText(BaseMessages.getString(PKG, "EnterPrintDialog.PrintArea.Label"));
    props.setLook(wlCanvas);
    fdlCanvas = new FormData();
    fdlCanvas.left = new FormAttachment(0, 0);
    fdlCanvas.top = new FormAttachment(0, margin);
    wlCanvas.setLayoutData(fdlCanvas);
    wCanvas = new Canvas(shell, SWT.BORDER);
    props.setLook(wCanvas);
    wCanvas.addPaintListener(new PaintListener() {

        public void paintControl(PaintEvent pe) {
            repaint(pe.gc, pe.width, pe.height);
        }
    });
    fdCanvas = new FormData();
    fdCanvas.left = new FormAttachment(0, 0);
    fdCanvas.top = new FormAttachment(wlCanvas, margin);
    fdCanvas.right = new FormAttachment(100, 0);
    fdCanvas.bottom = new FormAttachment(100, -220);
    wCanvas.setLayoutData(fdCanvas);
    // Rows
    wlRows = new Label(shell, SWT.NONE);
    wlRows.setText(BaseMessages.getString(PKG, "EnterPrintDialog.Rows.Label"));
    props.setLook(wlRows);
    fdlRows = new FormData();
    fdlRows.left = new FormAttachment(0, 0);
    fdlRows.right = new FormAttachment(middle, -margin);
    fdlRows.top = new FormAttachment(wCanvas, margin);
    wlRows.setLayoutData(fdlRows);
    wRows = new Slider(shell, SWT.HORIZONTAL);
    wRows.setIncrement(1);
    wRows.setMinimum(1);
    wRows.setMaximum(11);
    wRows.setThumb(1);
    wRows.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent se) {
            Slider sl = (Slider) se.widget;
            nrrows = sl.getSelection();
            wCanvas.redraw();
        }
    });
    props.setLook(wRows);
    fdRows = new FormData();
    fdRows.left = new FormAttachment(middle, 0);
    fdRows.top = new FormAttachment(wCanvas, margin);
    fdRows.right = new FormAttachment(100, 0);
    wRows.setLayoutData(fdRows);
    // Cols
    wlCols = new Label(shell, SWT.NONE);
    wlCols.setText(BaseMessages.getString(PKG, "EnterPrintDialog.Cols.Label"));
    props.setLook(wlCols);
    fdlCols = new FormData();
    fdlCols.left = new FormAttachment(0, 0);
    fdlCols.right = new FormAttachment(middle, -margin);
    fdlCols.top = new FormAttachment(wRows, margin);
    wlCols.setLayoutData(fdlCols);
    wCols = new Slider(shell, SWT.HORIZONTAL);
    wCols.setIncrement(1);
    wCols.setMinimum(1);
    wCols.setMaximum(11);
    wCols.setThumb(1);
    wCols.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent se) {
            Slider sl = (Slider) se.widget;
            nrcols = sl.getSelection();
            wCanvas.redraw();
        }
    });
    props.setLook(wCols);
    fdCols = new FormData();
    fdCols.left = new FormAttachment(middle, 0);
    fdCols.top = new FormAttachment(wRows, margin);
    fdCols.right = new FormAttachment(100, 0);
    wCols.setLayoutData(fdCols);
    // Scale
    wlScale = new Label(shell, SWT.NONE);
    wlScale.setText(BaseMessages.getString(PKG, "EnterPrintDialog.Scaling.Label"));
    props.setLook(wlScale);
    fdlScale = new FormData();
    fdlScale.left = new FormAttachment(0, 0);
    fdlScale.right = new FormAttachment(middle, -margin);
    fdlScale.top = new FormAttachment(wCols, margin);
    wlScale.setLayoutData(fdlScale);
    wScale = new Slider(shell, SWT.HORIZONTAL);
    wScale.setIncrement(10);
    wScale.setMinimum(10);
    wScale.setMaximum(500);
    wScale.setThumb(10);
    wScale.setPageIncrement(25);
    wScale.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent se) {
            Slider sl = (Slider) se.widget;
            scale = sl.getSelection();
            wCanvas.redraw();
        }
    });
    props.setLook(wScale);
    fdScale = new FormData();
    fdScale.left = new FormAttachment(middle, 0);
    fdScale.top = new FormAttachment(wCols, margin);
    fdScale.right = new FormAttachment(100, 0);
    wScale.setLayoutData(fdScale);
    // Left
    wlLeft = new Label(shell, SWT.NONE);
    wlLeft.setText(BaseMessages.getString(PKG, "EnterPrintDialog.LeftMargin.Label"));
    props.setLook(wlLeft);
    fdlLeft = new FormData();
    fdlLeft.left = new FormAttachment(0, 0);
    fdlLeft.right = new FormAttachment(middle, -margin);
    fdlLeft.top = new FormAttachment(wScale, margin);
    wlLeft.setLayoutData(fdlLeft);
    wLeft = new Text(shell, SWT.BORDER);
    wLeft.addModifyListener(new ModifyListener() {

        public void modifyText(ModifyEvent e) {
            Text w = (Text) e.widget;
            leftMargin = Const.toDouble(w.getText(), 0.00);
        }
    });
    props.setLook(wLeft);
    fdLeft = new FormData();
    fdLeft.left = new FormAttachment(middle, 0);
    fdLeft.top = new FormAttachment(wScale, margin);
    fdLeft.right = new FormAttachment(100, 0);
    wLeft.setLayoutData(fdLeft);
    // Right
    wlRight = new Label(shell, SWT.NONE);
    wlRight.setText(BaseMessages.getString(PKG, "EnterPrintDialog.RightMargin.Label"));
    props.setLook(wlRight);
    fdlRight = new FormData();
    fdlRight.left = new FormAttachment(0, 0);
    fdlRight.right = new FormAttachment(middle, -margin);
    fdlRight.top = new FormAttachment(wLeft, margin);
    wlRight.setLayoutData(fdlRight);
    wRight = new Text(shell, SWT.BORDER);
    wRight.addModifyListener(new ModifyListener() {

        public void modifyText(ModifyEvent e) {
            Text w = (Text) e.widget;
            rightMargin = Const.toDouble(w.getText(), 0.00);
        }
    });
    props.setLook(wRight);
    fdRight = new FormData();
    fdRight.left = new FormAttachment(middle, 0);
    fdRight.top = new FormAttachment(wLeft, margin);
    fdRight.right = new FormAttachment(100, 0);
    wRight.setLayoutData(fdRight);
    // Top
    wlTop = new Label(shell, SWT.NONE);
    wlTop.setText(BaseMessages.getString(PKG, "EnterPrintDialog.TopMargin.Label"));
    props.setLook(wlTop);
    fdlTop = new FormData();
    fdlTop.left = new FormAttachment(0, 0);
    fdlTop.right = new FormAttachment(middle, -margin);
    fdlTop.top = new FormAttachment(wRight, margin);
    wlTop.setLayoutData(fdlTop);
    wTop = new Text(shell, SWT.BORDER);
    wTop.addModifyListener(new ModifyListener() {

        public void modifyText(ModifyEvent e) {
            Text w = (Text) e.widget;
            topMargin = Const.toDouble(w.getText(), 0.00);
        }
    });
    props.setLook(wTop);
    fdTop = new FormData();
    fdTop.left = new FormAttachment(middle, 0);
    fdTop.top = new FormAttachment(wRight, margin);
    fdTop.right = new FormAttachment(100, 0);
    wTop.setLayoutData(fdTop);
    // Bottom
    wlBottom = new Label(shell, SWT.NONE);
    wlBottom.setText(BaseMessages.getString(PKG, "EnterPrintDialog.BottomMargin.Label"));
    props.setLook(wlBottom);
    fdlBottom = new FormData();
    fdlBottom.left = new FormAttachment(0, 0);
    fdlBottom.right = new FormAttachment(middle, -margin);
    fdlBottom.top = new FormAttachment(wTop, margin);
    wlBottom.setLayoutData(fdlBottom);
    wBottom = new Text(shell, SWT.BORDER);
    wBottom.addModifyListener(new ModifyListener() {

        public void modifyText(ModifyEvent e) {
            Text w = (Text) e.widget;
            bottomMargin = Const.toDouble(w.getText(), 0.00);
        }
    });
    props.setLook(wBottom);
    fdBottom = new FormData();
    fdBottom.left = new FormAttachment(middle, 0);
    fdBottom.top = new FormAttachment(wTop, margin);
    fdBottom.right = new FormAttachment(100, 0);
    wBottom.setLayoutData(fdBottom);
    // Some buttons
    wOK = new Button(shell, SWT.PUSH);
    wOK.setText(BaseMessages.getString(PKG, "System.Button.OK"));
    wCancel = new Button(shell, SWT.PUSH);
    wCancel.setText(BaseMessages.getString(PKG, "System.Button.Cancel"));
    fdOK = new FormData();
    fdOK.left = new FormAttachment(33, 0);
    fdOK.bottom = new FormAttachment(100, 0);
    wOK.setLayoutData(fdOK);
    fdCancel = new FormData();
    fdCancel.left = new FormAttachment(66, 0);
    fdCancel.bottom = new FormAttachment(100, 0);
    wCancel.setLayoutData(fdCancel);
    // Add listeners
    lsCancel = new Listener() {

        public void handleEvent(Event e) {
            cancel();
        }
    };
    lsOK = new Listener() {

        public void handleEvent(Event e) {
            ok();
        }
    };
    wOK.addListener(SWT.Selection, lsOK);
    wCancel.addListener(SWT.Selection, lsCancel);
    // Detect [X] or ALT-F4 or something that kills this window...
    shell.addShellListener(new ShellAdapter() {

        public void shellClosed(ShellEvent e) {
            cancel();
        }
    });
    getData();
    BaseStepDialog.setSize(shell);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    return retval;
}
Also used : FormLayout(org.eclipse.swt.layout.FormLayout) FormData(org.eclipse.swt.layout.FormData) ShellAdapter(org.eclipse.swt.events.ShellAdapter) PaintEvent(org.eclipse.swt.events.PaintEvent) PaintListener(org.eclipse.swt.events.PaintListener) Listener(org.eclipse.swt.widgets.Listener) ModifyListener(org.eclipse.swt.events.ModifyListener) Slider(org.eclipse.swt.widgets.Slider) ModifyListener(org.eclipse.swt.events.ModifyListener) PaintListener(org.eclipse.swt.events.PaintListener) Canvas(org.eclipse.swt.widgets.Canvas) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Label(org.eclipse.swt.widgets.Label) ShellEvent(org.eclipse.swt.events.ShellEvent) Text(org.eclipse.swt.widgets.Text) Point(org.eclipse.swt.graphics.Point) Shell(org.eclipse.swt.widgets.Shell) ModifyEvent(org.eclipse.swt.events.ModifyEvent) Button(org.eclipse.swt.widgets.Button) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Event(org.eclipse.swt.widgets.Event) PaintEvent(org.eclipse.swt.events.PaintEvent) ModifyEvent(org.eclipse.swt.events.ModifyEvent) ShellEvent(org.eclipse.swt.events.ShellEvent) SelectionEvent(org.eclipse.swt.events.SelectionEvent) FormAttachment(org.eclipse.swt.layout.FormAttachment) Display(org.eclipse.swt.widgets.Display)

Example 53 with Canvas

use of org.eclipse.swt.widgets.Canvas in project archi by archimatetool.

the class SimpleSWTExample method createMainPanel.

private void createMainPanel() {
    mainComposite = new Canvas(mainShell, SWT.NO_BACKGROUND);
    GridData mainGridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL, GridData.VERTICAL_ALIGN_FILL, true, true);
    mainGridData.widthHint = INITIAL_PANEL_WIDTH;
    mainGridData.heightHint = INITIAL_PANEL_HEIGHT;
    mainComposite.setLayoutData(mainGridData);
    mainComposite.addPaintListener(new GraphPaintListener());
    mainComposite.setBackground(new Color(255, 255, 255));
    mainComposite.setLayout(null);
    mainComposite.addMouseMoveListener(new MouseMoveListener() {

        @Override
        public void mouseMove(MouseEvent e) {
            if (selectedEntity == null) {
                // Nothing selected, lets use a mouse hover
                SimpleNode oldEntity = hoverEntity;
                hoverEntity = null;
                for (Iterator iter = entities.iterator(); iter.hasNext() && selectedEntity == null; ) {
                    SimpleNode entity = (SimpleNode) iter.next();
                    double x = entity.getX();
                    double y = entity.getY();
                    double w = entity.getWidth();
                    double h = entity.getHeight();
                    Rectangle rect = new Rectangle((int) x, (int) y, (int) w, (int) h);
                    if (rect.contains(e.x, e.y)) {
                        hoverEntity = entity;
                        hoverEntity.ignoreInLayout(true);
                        hoverEntity.setSelected();
                        break;
                    }
                }
                if (oldEntity != null && oldEntity != hoverEntity) {
                    oldEntity.ignoreInLayout(false);
                    oldEntity.setUnSelected();
                }
            }
        }
    });
    mainComposite.addMouseListener(new MouseListener() {

        @Override
        public void mouseDoubleClick(MouseEvent e) {
        }

        @Override
        public void mouseDown(MouseEvent e) {
            selectedEntity = null;
            hoverEntity = null;
            for (Iterator iter = entities.iterator(); iter.hasNext() && selectedEntity == null; ) {
                SimpleNode entity = (SimpleNode) iter.next();
                double x = entity.getX();
                double y = entity.getY();
                double w = entity.getWidth();
                double h = entity.getHeight();
                Rectangle rect = new Rectangle((int) x, (int) y, (int) w, (int) h);
                if (rect.contains(e.x, e.y)) {
                    selectedEntity = entity;
                }
            }
            if (selectedEntity != null) {
                mouseDownPoint = new Point(e.x, e.y);
                selectedEntityPositionAtMouseDown = new Point((int) selectedEntity.getX(), (int) selectedEntity.getY());
                selectedEntity.ignoreInLayout(true);
                selectedEntity.setSelected();
            } else {
                mouseDownPoint = null;
                selectedEntityPositionAtMouseDown = null;
            }
        }

        @Override
        public void mouseUp(MouseEvent e) {
            if (selectedEntity != null) {
                selectedEntity.ignoreInLayout(false);
                selectedEntity.setUnSelected();
                List relatedNodes = selectedEntity.getRelatedEntities();
                for (Iterator iter = relatedNodes.iterator(); iter.hasNext(); ) {
                    SimpleNode element = (SimpleNode) iter.next();
                    element.setUnSelected();
                }
                SimpleRelationship[] rels = selectedEntity.getRelationships();
                for (int i = 0; i < rels.length; i++) {
                    rels[i].resetLineWidth();
                }
            }
            selectedEntity = null;
            mouseDownPoint = null;
            selectedEntityPositionAtMouseDown = null;
        }
    });
    // stops the algorithm when the window is closed
    mainComposite.addDisposeListener(new DisposeListener() {

        @Override
        public void widgetDisposed(DisposeEvent e) {
            if (currentLayoutAlgorithm != null) {
                currentLayoutAlgorithm.stop();
            }
        }
    });
    mainComposite.addMouseMoveListener(new MouseMoveListener() {

        @Override
        public void mouseMove(MouseEvent e) {
            if (selectedEntity != null && mouseDownPoint != null) {
                double dx = e.x - mouseDownPoint.x;
                double dy = e.y - mouseDownPoint.y;
                selectedEntity.setLocation(selectedEntityPositionAtMouseDown.x + dx, selectedEntityPositionAtMouseDown.y + dy);
                mainComposite.redraw();
            }
        }
    });
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) MouseEvent(org.eclipse.swt.events.MouseEvent) Canvas(org.eclipse.swt.widgets.Canvas) Color(org.eclipse.swt.graphics.Color) Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point) LayoutBendPoint(org.eclipse.zest.layouts.LayoutBendPoint) DisposeEvent(org.eclipse.swt.events.DisposeEvent) SimpleNode(org.eclipse.zest.layouts.exampleStructures.SimpleNode) MouseMoveListener(org.eclipse.swt.events.MouseMoveListener) MouseListener(org.eclipse.swt.events.MouseListener) GridData(org.eclipse.swt.layout.GridData) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList)

Example 54 with Canvas

use of org.eclipse.swt.widgets.Canvas in project archi by archimatetool.

the class ProfilesManagerDialog method createButtonPanel.

/**
 * Create the button panel
 */
private void createButtonPanel(Composite parent) {
    Composite client = new Composite(parent, SWT.NULL);
    GridLayout layout = new GridLayout();
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    client.setLayout(layout);
    GridDataFactory.create(GridData.VERTICAL_ALIGN_BEGINNING).applyTo(client);
    fButtonNew = new Button(client, SWT.PUSH);
    fButtonNew.setText(Messages.ProfilesManagerDialog_10);
    GridDataFactory.create(GridData.FILL_HORIZONTAL).applyTo(fButtonNew);
    fButtonNew.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            createNewProfile();
        }
    });
    fButtonDelete = new Button(client, SWT.PUSH);
    fButtonDelete.setText(Messages.ProfilesManagerDialog_11);
    fButtonDelete.setEnabled(false);
    GridDataFactory.create(GridData.FILL_HORIZONTAL).applyTo(fButtonDelete);
    fButtonDelete.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            deleteSelectedProfiles();
        }
    });
    fImageButton = new Button(client, SWT.PUSH);
    fImageButton.setText(Messages.ProfilesManagerDialog_12);
    fImageButton.setEnabled(false);
    GridDataFactory.create(GridData.FILL_HORIZONTAL).applyTo(fImageButton);
    // Create sub-menu for Image button
    MenuManager menuManager = new MenuManager();
    menuManager.add(fActionChooseImage);
    menuManager.add(fActionClearImage);
    Menu menu = menuManager.createContextMenu(fImageButton.getShell());
    // Image Button is clicked...
    fImageButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            // Display sub menu below button
            Rectangle buttonBounds = fImageButton.getBounds();
            menu.setLocation(fImageButton.getParent().toDisplay(buttonBounds.x - 0, buttonBounds.y + buttonBounds.height));
            menu.setVisible(true);
        }
    });
    fImagePreview = new Canvas(client, SWT.BORDER);
    GridDataFactory.create(SWT.NONE).hint(IMAGE_SIZE, IMAGE_SIZE).applyTo(fImagePreview);
    fImagePreview.addPaintListener(new PaintListener() {

        @Override
        public void paintControl(PaintEvent e) {
            if (fPreviewImage != null) {
                e.gc.setAntialias(SWT.ON);
                e.gc.setInterpolation(SWT.HIGH);
                Rectangle imageBounds = fPreviewImage.getBounds();
                Rectangle newSize = ImageFactory.getScaledImageSize(fPreviewImage, IMAGE_SIZE);
                // Centre the image
                int x = (IMAGE_SIZE - newSize.width) / 2;
                int y = (IMAGE_SIZE - newSize.height) / 2;
                e.gc.drawImage(fPreviewImage, 0, 0, imageBounds.width, imageBounds.height, x, y, newSize.width, newSize.height);
            }
        }
    });
    fImagePreview.addListener(SWT.MouseDoubleClick, new Listener() {

        @Override
        public void handleEvent(Event event) {
            if (fActionChooseImage.isEnabled()) {
                chooseImage();
            }
        }
    });
    fImagePreview.addDisposeListener((e) -> {
        disposePreviewImage();
    });
}
Also used : PaintEvent(org.eclipse.swt.events.PaintEvent) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) PaintListener(org.eclipse.swt.events.PaintListener) Listener(org.eclipse.swt.widgets.Listener) Composite(org.eclipse.swt.widgets.Composite) PaintListener(org.eclipse.swt.events.PaintListener) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Canvas(org.eclipse.swt.widgets.Canvas) Rectangle(org.eclipse.swt.graphics.Rectangle) GridLayout(org.eclipse.swt.layout.GridLayout) Button(org.eclipse.swt.widgets.Button) SelectionEvent(org.eclipse.swt.events.SelectionEvent) MenuManager(org.eclipse.jface.action.MenuManager) PaintEvent(org.eclipse.swt.events.PaintEvent) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) Event(org.eclipse.swt.widgets.Event) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Menu(org.eclipse.swt.widgets.Menu)

Example 55 with Canvas

use of org.eclipse.swt.widgets.Canvas in project archi by archimatetool.

the class IconSection method createControls.

@Override
protected void createControls(Composite parent) {
    createLabel(parent, Messages.IconSection_9, ITabbedLayoutConstants.STANDARD_LABEL_WIDTH, SWT.NONE);
    fCanvas = new Canvas(parent, SWT.BORDER);
    getWidgetFactory().adapt(fCanvas);
    GridData gd = new GridData(SWT.NONE, SWT.NONE, false, false);
    gd.widthHint = IMAGE_SIZE;
    gd.heightHint = IMAGE_SIZE;
    fCanvas.setLayoutData(gd);
    GridLayout layout = new GridLayout();
    layout.marginWidth = 0;
    layout.marginHeight = 0;
    fCanvas.setLayout(layout);
    fCanvas.addDisposeListener(new DisposeListener() {

        @Override
        public void widgetDisposed(DisposeEvent e) {
            disposeImage();
        }
    });
    fCanvas.addListener(SWT.MouseDoubleClick, new Listener() {

        @Override
        public void handleEvent(Event event) {
            if (fImageButton.isEnabled()) {
                chooseImage();
            }
        }
    });
    fCanvas.addPaintListener(new PaintListener() {

        @Override
        public void paintControl(PaintEvent e) {
            if (fImage != null) {
                e.gc.setAntialias(SWT.ON);
                e.gc.setInterpolation(SWT.HIGH);
                Rectangle imageBounds = fImage.getBounds();
                Rectangle newSize = ImageFactory.getScaledImageSize(fImage, IMAGE_SIZE);
                // Centre the image
                int x = (IMAGE_SIZE - newSize.width) / 2;
                int y = (IMAGE_SIZE - newSize.height) / 2;
                e.gc.drawImage(fImage, 0, 0, imageBounds.width, imageBounds.height, x, y, newSize.width, newSize.height);
            }
        }
    });
    String tooltip = Messages.IconSection_10;
    fCanvas.setToolTipText(tooltip);
    DropTarget target = new DropTarget(fCanvas, DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT);
    target.setTransfer(new Transfer[] { FileTransfer.getInstance() });
    target.addDropListener(new DropTargetAdapter() {

        @Override
        public void drop(DropTargetEvent event) {
            if (event.data instanceof String[] && fImageButton.isEnabled()) {
                File file = new File(((String[]) event.data)[0]);
                setImage(file);
            }
        }
    });
    // Image Button
    createImageButton(parent);
    // Position
    createLabel(parent, Messages.IconSection_11, ITabbedLayoutConstants.STANDARD_LABEL_WIDTH, SWT.CENTER);
    fComboPosition = new Combo(parent, SWT.READ_ONLY);
    fComboPosition.setItems(fComboPositionItems);
    getWidgetFactory().adapt(fComboPosition, true, true);
    gd = new GridData(SWT.NONE, SWT.NONE, false, false);
    fComboPosition.setLayoutData(gd);
    fComboPosition.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            CompoundCommand result = new CompoundCommand();
            for (EObject iconic : getEObjects()) {
                if (isAlive(iconic)) {
                    Command cmd = new EObjectFeatureCommand(Messages.IconSection_12, iconic, IArchimatePackage.Literals.ICONIC__IMAGE_POSITION, fComboPosition.getSelectionIndex());
                    if (cmd.canExecute()) {
                        result.add(cmd);
                    }
                }
            }
            executeCommand(result.unwrap());
        }
    });
    // Help
    PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, HELP_ID);
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) PaintListener(org.eclipse.swt.events.PaintListener) DisposeListener(org.eclipse.swt.events.DisposeListener) Listener(org.eclipse.swt.widgets.Listener) PaintEvent(org.eclipse.swt.events.PaintEvent) PaintListener(org.eclipse.swt.events.PaintListener) Canvas(org.eclipse.swt.widgets.Canvas) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Rectangle(org.eclipse.swt.graphics.Rectangle) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) Combo(org.eclipse.swt.widgets.Combo) DisposeEvent(org.eclipse.swt.events.DisposeEvent) CompoundCommand(org.eclipse.gef.commands.CompoundCommand) DropTargetAdapter(org.eclipse.swt.dnd.DropTargetAdapter) GridLayout(org.eclipse.swt.layout.GridLayout) CompoundCommand(org.eclipse.gef.commands.CompoundCommand) EObjectFeatureCommand(com.archimatetool.editor.model.commands.EObjectFeatureCommand) Command(org.eclipse.gef.commands.Command) EObjectFeatureCommand(com.archimatetool.editor.model.commands.EObjectFeatureCommand) EObject(org.eclipse.emf.ecore.EObject) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) DisposeEvent(org.eclipse.swt.events.DisposeEvent) Event(org.eclipse.swt.widgets.Event) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) PaintEvent(org.eclipse.swt.events.PaintEvent) SelectionEvent(org.eclipse.swt.events.SelectionEvent) DropTarget(org.eclipse.swt.dnd.DropTarget) File(java.io.File)

Aggregations

Canvas (org.eclipse.swt.widgets.Canvas)111 GridData (org.eclipse.swt.layout.GridData)47 GridLayout (org.eclipse.swt.layout.GridLayout)38 Composite (org.eclipse.swt.widgets.Composite)38 PaintEvent (org.eclipse.swt.events.PaintEvent)36 PaintListener (org.eclipse.swt.events.PaintListener)35 Point (org.eclipse.swt.graphics.Point)32 Rectangle (org.eclipse.swt.graphics.Rectangle)32 Label (org.eclipse.swt.widgets.Label)31 Text (org.eclipse.swt.widgets.Text)24 Button (org.eclipse.swt.widgets.Button)22 Color (org.eclipse.swt.graphics.Color)20 Shell (org.eclipse.swt.widgets.Shell)20 MouseEvent (org.eclipse.swt.events.MouseEvent)18 GC (org.eclipse.swt.graphics.GC)17 FillLayout (org.eclipse.swt.layout.FillLayout)17 Event (org.eclipse.swt.widgets.Event)17 SelectionEvent (org.eclipse.swt.events.SelectionEvent)16 Listener (org.eclipse.swt.widgets.Listener)16 Display (org.eclipse.swt.widgets.Display)15