Exemple #1
0
  public void save_Event() {
    FileDialog fd = new FileDialog(ZestDebuggerView.this.getSite().getShell(), SWT.SAVE);
    fd.setText("Save");
    fd.setFilterPath("C:/");
    String[] filterExt = {"*.jpg"};
    fd.setFilterExtensions(filterExt);
    String selected = fd.open();
    System.out.println(selected);

    if (selected != null) {
      GC gc = new GC(viewer.getControl());
      org.eclipse.swt.graphics.Rectangle bounds = viewer.getControl().getBounds();

      Image image = new Image(viewer.getControl().getDisplay(), bounds);
      try {
        gc.copyArea(image, 0, 0);
        ImageLoader imageLoader = new ImageLoader();
        imageLoader.data = new ImageData[] {image.getImageData()};
        imageLoader.save(selected, SWT.IMAGE_JPEG);
        MessageDialog.openInformation(
            ZestDebuggerView.this.getSite().getShell(),
            "Success",
            "The image has been saved successfully");
      } catch (Exception e) {
        e.printStackTrace();
        MessageDialog.openError(
            ZestDebuggerView.this.getSite().getShell(), "Failure", "The image can't be saved");
      } finally {
        image.dispose();
        gc.dispose();
      }
    }
  }
  /**
   * This method converts the Graph into an actual image.
   *
   * @param canvas The graph that needs to be converted to an image
   * @return The Image that was generated by the supplied Graph.
   */
  public ImageData getImage(AbstractChartBuilder canvas) {
    GC gc = new GC(canvas);
    Image image = new Image(canvas.getDisplay(), canvas.getSize().x, canvas.getSize().y);
    gc.copyArea(image, 0, 0);
    gc.dispose();

    ImageData data = image.getImageData();
    image.dispose();
    return data;
  }
 public void drawRectangle(GC gc) {
   offsetX = endX - startX;
   offsetY = endY - startY;
   if (gc != null) {
     gc.setLineWidth(3);
     gc.setForeground(new Color(Display.getDefault(), 255, 0, 0));
     setParams(startX, startY, offsetX, offsetY);
     gc.drawRectangle(startX, startY, offsetX, offsetY);
     tempImage = new Image(Display.getDefault(), "./temp/temp.png");
     gc.copyArea(tempImage, 0, 0);
     imagesList.add(image);
   }
 }
  /*
   * @see org.eclipse.swt.dnd.DragSourceAdapter#dragStart(org.eclipse.swt.dnd.DragSourceEvent)
   */
  @Override
  public void dragStart(DragSourceEvent event) {
    Point linkSize = link.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    GC gc = new GC(link);

    /* Erst altes Image disposen falls nötig */
    dispose();

    /* Neues Bild erzeugen und als DragSourceEffect festlegen */
    image = new Image(link.getDisplay(), linkSize.x, linkSize.y);
    gc.copyArea(image, 0, 0); // Kopiert das UI des Widgets in das Bild
    gc.dispose();

    event.image = image;
  }
  public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Widget");

    final Table table = new Table(shell, SWT.MULTI);
    table.setLinesVisible(true);
    table.setBounds(10, 10, 100, 100);
    for (int i = 0; i < 9; i++) {
      new TableItem(table, SWT.NONE).setText("item" + i);
    }

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Capture");
    button.pack();
    button.setLocation(10, 140);
    button.addListener(
        SWT.Selection,
        event -> {
          Point tableSize = table.getSize();
          GC gc = new GC(table);
          final Image image = new Image(display, tableSize.x, tableSize.y);
          gc.copyArea(image, 0, 0);
          gc.dispose();

          Shell popup = new Shell(shell);
          popup.setText("Image");
          popup.addListener(SWT.Close, e -> image.dispose());

          Canvas canvas = new Canvas(popup, SWT.NONE);
          canvas.setBounds(10, 10, tableSize.x + 10, tableSize.y + 10);
          canvas.addPaintListener(e -> e.gc.drawImage(image, 0, 0));
          popup.pack();
          popup.open();
        });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) display.sleep();
    }
    display.dispose();
  }
Exemple #6
0
  @Override
  protected void okPressed() {
    result = ProtocolFactory.eINSTANCE.createAssert();

    AssertImageData data = ProtocolFactory.eINSTANCE.createAssertImageData();
    Rectangle selection = getSelection();
    data.setX(selection.x);
    data.setY(selection.y);
    data.setWidth(imgBounds.width);
    data.setHeight(imgBounds.height);
    result.setImageData(data);

    int index = assertKind.getSelectionIndex();
    switch (index) {
      case TYPE_CONTAIN_IMAGE:
        // Capture image
        GC gc = new GC(img);
        Image copyImg = new Image(img.getDevice(), selection.width, selection.height);
        gc.copyArea(copyImg, selection.x, selection.y);
        gc.dispose();
        ImageLoader loader = new ImageLoader();
        loader.data = new ImageData[] {copyImg.getImageData()};
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        loader.save(stream, SWT.IMAGE_PNG);
        data.setImage(stream.toByteArray());
        copyImg.dispose();
        result.setAttribute("Contain image"); // $NON-NLS-1$
        result.setKind(AssertKind.CONTAINS_IMAGE);
        break;
      case TYPE_IMAGE_TEXT_CONTAINS:
        result.setAttribute("Contain text"); // $NON-NLS-1$
        result.setKind(AssertKind.IMAGE_CONTAINS_TEXT);
        String resultText = replaceMultilines(text.getText());
        result.setValue(resultText);
        data.setSx(selection.width);
        data.setSy(selection.height);
        break;
    }

    super.okPressed();
  }
  /** This method embed a button in a table cell */
  public Image getImage(Object element) {
    Shell shell = new Shell(viewer.getControl().getShell(), SWT.NO_TRIM);
    Button button = new Button(shell, SWT.PUSH);
    button.setImage(image);
    button.setBounds(0, 0, BUTTON_WIDTH, BUTTON_HEIGHT);

    Point bsize = button.computeSize(SWT.DEFAULT, SWT.DEFAULT);
    button.setSize(bsize);

    shell.setSize(bsize);
    shell.open();

    GC gc = new GC(button);
    Image i = new Image(viewer.getControl().getShell().getDisplay(), bsize.x, bsize.y);
    gc.copyArea(i, 0, 0);
    gc.dispose();

    shell.close();

    return i;
  }
  private static Image takeScreenshot(final Display display, final Shell parent) {
    /* Take the screen shot */
    GC gc = new GC(parent);
    Image image = new Image(display, parent.getClientArea());
    gc.copyArea(image, 0, 0);
    GC gcImage = new GC(image);
    gcImage.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
    gcImage.setAdvanced(true);
    Region region = parent.getRegion();

    data = image.getImageData();
    if (region != null) {
      int height = image.getBounds().height;
      int width = image.getBounds().width;
      byte[] alphaData = new byte[height * width];
      int currentPosition = 0;
      for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
          if (!region.contains(j, i)) {
            gcImage.drawPoint(j, i);
            alphaData[currentPosition] = 0;
          } else {
            alphaData[currentPosition] = (byte) 255;
          }
          currentPosition++;
        }
      }
      data.alphaData = alphaData;
    }

    image.dispose();
    gcImage.dispose();
    gc.dispose();

    return new Image(display, data);
  }
Exemple #9
0
  protected void onMouseMove(MouseEvent e) {
    if (m_Model == null) return;

    // show resize cursor?
    if ((m_ResizeColumnIndex != -1) || (getColumnForResize(e.x, e.y) >= 0))
      setCursor(new Cursor(m_Display, SWT.CURSOR_SIZEWE));
    else if ((m_ResizeRowIndex != -1) || (getRowForResize(e.x, e.y) >= 0))
      setCursor(new Cursor(m_Display, SWT.CURSOR_SIZENS));
    else setCursor(null);

    if (e.button == 1) {
      // extend selection?
      if (m_ClickColumnIndex != -1 && m_MultiSelectMode) {
        int row = calcRowNum(e.y);
        int col = calcColumnNum(e.x);

        if (row >= m_Model.getFixedRowCount() && col >= m_Model.getFixedColumnCount()) {

          m_ClickColumnIndex = col;
          m_ClickRowIndex = row;

          focusCell(col, row, (e.stateMask | SWT.SHIFT));
        }
      }
    }
    // column resize?
    if (m_ResizeColumnIndex != -1) {
      Rectangle rect = getClientArea();
      int oldSize = m_Model.getColumnWidth(m_ResizeColumnIndex);
      if (e.x > rect.x + rect.width - 1) e.x = rect.x + rect.width - 1;
      int newSize = e.x - m_ResizeColumnLeft;
      if (newSize < 5) newSize = 5;

      int leftX = getColumnLeft(m_ResizeColumnIndex);
      int rightX = getColumnRight(m_ResizeColumnIndex);

      m_Model.setColumnWidth(m_ResizeColumnIndex, newSize);
      newSize = m_Model.getColumnWidth(m_ResizeColumnIndex);

      GC gc = new GC(this);
      gc.copyArea(rightX, 0, rect.width - rightX, rect.height, leftX + newSize, 0);
      drawCol(gc, m_ResizeColumnIndex);
      if (newSize < oldSize) {
        int delta = oldSize - newSize;
        redraw(rect.width - delta, 0, delta, rect.height, false);
      }
      gc.dispose();
    }

    // row resize?
    if (m_ResizeRowIndex != -1) {
      Rectangle rect = getClientArea();
      GC gc = new GC(this);

      // calculate new size
      if (e.y > rect.y + rect.height - 1) e.y = rect.y + rect.height - 1;
      m_NewRowSize = e.y - m_ResizeRowTop;
      if (m_NewRowSize < m_Model.getRowHeightMinimum())
        m_NewRowSize = m_Model.getRowHeightMinimum();

      // restore old line area
      if (m_LineRestore != null) {
        gc.drawImage(m_LineRestore, m_LineX, m_LineY);
      }

      // safe old picture and draw line
      gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
      int lineEnd = getColumnRight(m_LeftColumn + m_ColumnsVisible - 1);
      m_LineRestore = new Image(m_Display, lineEnd, 1);
      m_LineX = rect.x + 1;
      m_LineY = m_ResizeRowTop + m_NewRowSize - 1;
      gc.copyArea(m_LineRestore, m_LineX, m_LineY);
      gc.drawLine(m_LineX, m_LineY, rect.x + lineEnd, m_LineY);
      gc.dispose();
    }
  }
  void doRename(boolean showPreview) {
    cancel();

    Image image = null;
    Label label = null;

    fShowPreview |= showPreview;
    try {
      ISourceViewer viewer = fEditor.getViewer();
      if (viewer instanceof SourceViewer) {
        SourceViewer sourceViewer = (SourceViewer) viewer;
        Control viewerControl = sourceViewer.getControl();
        if (viewerControl instanceof Composite) {
          Composite composite = (Composite) viewerControl;
          Display display = composite.getDisplay();

          // Flush pending redraw requests:
          while (!display.isDisposed() && display.readAndDispatch()) {}

          // Copy editor area:
          GC gc = new GC(composite);
          Point size;
          try {
            size = composite.getSize();
            image = new Image(gc.getDevice(), size.x, size.y);
            gc.copyArea(image, 0, 0);
          } finally {
            gc.dispose();
            gc = null;
          }

          // Persist editor area while executing refactoring:
          label = new Label(composite, SWT.NONE);
          label.setImage(image);
          label.setBounds(0, 0, size.x, size.y);
          label.moveAbove(null);
        }
      }

      String newName = fNamePosition.getContent();
      if (fOriginalName.equals(newName)) {
        return;
      }
      RenameSupport renameSupport = undoAndCreateRenameSupport(newName);
      if (renameSupport == null) {
        return;
      }

      Shell shell = fEditor.getSite().getShell();
      if (renameSupport.hasUnresolvedNameReferences()) {
        fShowPreview = true;
      }
      boolean executed;
      if (fShowPreview) { // could have been updated by undoAndCreateRenameSupport(..)
        executed = renameSupport.openDialog(shell, true);
      } else {
        renameSupport.perform(shell, fEditor.getSite().getWorkbenchWindow());
        executed = true;
      }
      if (executed) {
        restoreFullSelection();
      }
    } catch (CoreException ex) {
      DartToolsPlugin.log(ex);
    } catch (InterruptedException ex) {
      // canceling is OK -> redo text changes in that case?
    } catch (InvocationTargetException ex) {
      DartToolsPlugin.log(ex);
    } catch (BadLocationException e) {
      DartToolsPlugin.log(e);
    } finally {
      if (label != null) {
        label.dispose();
      }
      if (image != null) {
        image.dispose();
      }
    }
  }