コード例 #1
0
 /**
  * Returns the receiver's image data. This is the icon that is associated with the receiver in the
  * operating system.
  *
  * @return the image data for the program, may be null
  */
 public ImageData getImageData() {
   NSAutoreleasePool pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
   try {
     NSWorkspace workspace = NSWorkspace.sharedWorkspace();
     NSString fullPath;
     if (this.fullPath != null) {
       fullPath = NSString.stringWith(this.fullPath);
     } else {
       fullPath = workspace.fullPathForApplication(NSString.stringWith(name));
     }
     if (fullPath != null) {
       NSImage nsImage = workspace.iconForFile(fullPath);
       if (nsImage != null) {
         NSSize size = new NSSize();
         size.width = size.height = 16;
         nsImage.setSize(size);
         nsImage.retain();
         Image image = Image.cocoa_new(Display.getCurrent(), SWT.BITMAP, nsImage);
         ImageData imageData = image.getImageData();
         image.dispose();
         return imageData;
       }
     }
     return null;
   } finally {
     pool.release();
   }
 }
コード例 #2
0
 public static void main(String[] args) {
   final Display display = new Display();
   final Shell shell = new Shell(display);
   shell.setLayout(new FillLayout());
   final StyledText styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER);
   styledText.setText(text);
   FontData data = display.getSystemFont().getFontData()[0];
   Font font = new Font(display, data.getName(), 16, SWT.BOLD);
   styledText.setFont(font);
   styledText.setForeground(display.getSystemColor(SWT.COLOR_BLUE));
   styledText.addListener(
       SWT.Resize,
       new Listener() {
         public void handleEvent(Event event) {
           Rectangle rect = styledText.getClientArea();
           Image newImage = new Image(display, 1, Math.max(1, rect.height));
           GC gc = new GC(newImage);
           gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
           gc.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
           gc.fillGradientRectangle(rect.x, rect.y, 1, rect.height, true);
           gc.dispose();
           styledText.setBackgroundImage(newImage);
           if (oldImage != null) oldImage.dispose();
           oldImage = newImage;
         }
       });
   shell.setSize(700, 400);
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) display.sleep();
   }
   if (oldImage != null) oldImage.dispose();
   font.dispose();
   display.dispose();
 }
コード例 #3
0
 /** Frees the resources */
 void freeResources() {
   if (images != null) {
     for (Image image : images) {
       if (image != null) image.dispose();
     }
     images = null;
   }
 }
コード例 #4
0
ファイル: ToolItem.java プロジェクト: Crellis/Laptopworkspace
 void releaseWidget() {
   super.releaseWidget();
   releaseImages();
   control = null;
   toolTipText = null;
   disabledImage = hotImage = null;
   if (disabledImage2 != null) disabledImage2.dispose();
   disabledImage2 = null;
 }
コード例 #5
0
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(2, false));

    final Text text = new Text(shell, SWT.SEARCH | SWT.ICON_CANCEL);
    Image image = null;
    if ((text.getStyle() & SWT.ICON_CANCEL) == 0) {
      image = display.getSystemImage(SWT.ICON_ERROR);
      ToolBar toolBar = new ToolBar(shell, SWT.FLAT);
      ToolItem item = new ToolItem(toolBar, SWT.PUSH);
      item.setImage(image);
      item.addSelectionListener(
          new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
              text.setText("");
              System.out.println("Search cancelled");
            }
          });
    }
    text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    text.setText("Search text");
    text.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetDefaultSelected(SelectionEvent e) {
            if (e.detail == SWT.CANCEL) {
              System.out.println("Search cancelled");
            } else {
              System.out.println("Searching for: " + text.getText() + "...");
            }
          }
        });

    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) display.sleep();
    }
    if (image != null) image.dispose();
    display.dispose();
  }
コード例 #6
0
  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();
  }
コード例 #7
0
 LRESULT wmDrawChild(long /*int*/ wParam, long /*int*/ lParam) {
   DRAWITEMSTRUCT struct = new DRAWITEMSTRUCT();
   OS.MoveMemory(struct, lParam, DRAWITEMSTRUCT.sizeof);
   if (image != null) {
     GCData data = new GCData();
     data.device = display;
     GC gc = GC.win32_new(struct.hDC, data);
     /*
      * Bug in Windows.  When a bitmap is included in the
      * menu bar, the HDC seems to already include the left
      * coordinate.  The fix is to ignore this value when
      * the item is in a menu bar.
      */
     int x = (parent.style & SWT.BAR) != 0 ? MARGIN_WIDTH * 2 : struct.left;
     Image image = getEnabled() ? this.image : new Image(display, this.image, SWT.IMAGE_DISABLE);
     gc.drawImage(image, x, struct.top + MARGIN_HEIGHT);
     if (this.image != image) image.dispose();
     gc.dispose();
   }
   if (parent.foreground != -1) OS.SetTextColor(struct.hDC, parent.foreground);
   return null;
 }
コード例 #8
0
 public static void main(String[] args) {
   final Display display = new Display();
   final Image image = new Image(display, 16, 16);
   GC gc = new GC(image);
   gc.setBackground(display.getSystemColor(SWT.COLOR_RED));
   gc.fillRectangle(image.getBounds());
   gc.dispose();
   final Shell shell = new Shell(display);
   shell.setText("Lazy Table");
   shell.setLayout(new FillLayout());
   final Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
   table.setSize(200, 200);
   Thread thread =
       new Thread() {
         @Override
         public void run() {
           for (int i = 0; i < 20000; i++) {
             if (table.isDisposed()) return;
             final int[] index = new int[] {i};
             display.syncExec(
                 () -> {
                   if (table.isDisposed()) return;
                   TableItem item = new TableItem(table, SWT.NONE);
                   item.setText("Table Item " + index[0]);
                   item.setImage(image);
                 });
           }
         }
       };
   thread.start();
   shell.setSize(200, 200);
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) display.sleep();
   }
   image.dispose();
   display.dispose();
 }
コード例 #9
0
ファイル: ToolItem.java プロジェクト: andreyvit/yoursway-swt
  int XmNexposureCallback(int w, int client_data, int call_data) {
    if ((style & SWT.SEPARATOR) != 0) return 0;
    int xDisplay = OS.XtDisplay(handle);
    if (xDisplay == 0) return 0;
    int xWindow = OS.XtWindow(handle);
    if (xWindow == 0) return 0;
    int[] argList = {
      OS.XmNcolormap, 0,
      OS.XmNwidth, 0,
      OS.XmNheight, 0,
    };
    OS.XtGetValues(handle, argList, argList.length / 2);
    int width = argList[3], height = argList[5];

    Image currentImage = image;
    boolean enabled = getEnabled();

    if ((parent.style & SWT.FLAT) != 0) {
      boolean hasCursor = hasCursor();

      /* Set the shadow thickness */
      int thickness = 0;
      if (set || (hasCursor && enabled)) {
        thickness = Math.min(2, display.buttonShadowThickness);
      }
      argList = new int[] {OS.XmNshadowThickness, thickness};
      OS.XtSetValues(handle, argList, argList.length / 2);

      /* Determine if hot image should be used */
      if (enabled && hasCursor && hotImage != null) {
        currentImage = hotImage;
      }
    }

    GCData data = new GCData();
    data.device = display;
    data.display = xDisplay;
    data.drawable = xWindow;
    data.font = parent.font;
    data.colormap = argList[1];
    int xGC = OS.XCreateGC(xDisplay, xWindow, 0, null);
    if (xGC == 0) SWT.error(SWT.ERROR_NO_HANDLES);
    GC gc = GC.motif_new(xGC, data);

    XmAnyCallbackStruct cb = new XmAnyCallbackStruct();
    OS.memmove(cb, call_data, XmAnyCallbackStruct.sizeof);
    if (cb.event != 0) {
      XExposeEvent xEvent = new XExposeEvent();
      OS.memmove(xEvent, cb.event, XExposeEvent.sizeof);
      Rectangle rect = new Rectangle(xEvent.x, xEvent.y, xEvent.width, xEvent.height);
      gc.setClipping(rect);
    }

    if (!enabled) {
      currentImage = disabledImage;
      if (currentImage == null && image != null) {
        currentImage = new Image(display, image, SWT.IMAGE_DISABLE);
      }
      Color disabledColor = display.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
      gc.setForeground(disabledColor);
    } else {
      gc.setForeground(parent.getForeground());
    }
    gc.setBackground(parent.getBackground());

    int textX = 0, textY = 0, textWidth = 0, textHeight = 0;
    if (text.length() != 0) {
      int flags = SWT.DRAW_DELIMITER | SWT.DRAW_TAB | SWT.DRAW_MNEMONIC;
      Point textExtent = gc.textExtent(text, flags);
      textWidth = textExtent.x;
      textHeight = textExtent.y;
    }
    int imageX = 0, imageY = 0, imageWidth = 0, imageHeight = 0;
    if (currentImage != null) {
      Rectangle imageBounds = currentImage.getBounds();
      imageWidth = imageBounds.width;
      imageHeight = imageBounds.height;
    }

    int spacing = 0;
    if (textWidth != 0 && imageWidth != 0) spacing = 2;
    if ((parent.style & SWT.RIGHT) != 0) {
      imageX = (width - imageWidth - textWidth - spacing) / 2;
      imageY = (height - imageHeight) / 2;
      textX = spacing + imageX + imageWidth;
      textY = (height - textHeight) / 2;
    } else {
      imageX = (width - imageWidth) / 2;
      imageY = (height - imageHeight - textHeight - spacing) / 2;
      textX = (width - textWidth) / 2;
      textY = spacing + imageY + imageHeight;
    }

    if ((style & SWT.DROP_DOWN) != 0) {
      textX -= 6;
      imageX -= 6;
    }
    if (textWidth > 0) {
      int flags = SWT.DRAW_DELIMITER | SWT.DRAW_TAB | SWT.DRAW_MNEMONIC | SWT.DRAW_TRANSPARENT;
      gc.drawText(text, textX, textY, flags);
    }
    if (imageWidth > 0) gc.drawImage(currentImage, imageX, imageY);
    if ((style & SWT.DROP_DOWN) != 0) {
      int startX = width - 12, startY = (height - 2) / 2;
      int[] arrow = {startX, startY, startX + 3, startY + 3, startX + 6, startY};
      gc.setBackground(parent.getForeground());
      gc.fillPolygon(arrow);
      gc.drawPolygon(arrow);
    }
    gc.dispose();
    OS.XFreeGC(xDisplay, xGC);

    if (!enabled && disabledImage == null) {
      if (currentImage != null) currentImage.dispose();
    }
    return 0;
  }
コード例 #10
0
  public static void main(String[] args) {
    final Display display = new Display();
    final Image image = display.getSystemImage(SWT.ICON_INFORMATION);
    final Shell shell = new Shell(display, SWT.NO_TRIM);
    Region region = new Region();
    final ImageData imageData = image.getImageData();
    if (imageData.alphaData != null) {
      Rectangle pixel = new Rectangle(0, 0, 1, 1);
      for (int y = 0; y < imageData.height; y++) {
        for (int x = 0; x < imageData.width; x++) {
          if (imageData.getAlpha(x, y) == 255) {
            pixel.x = imageData.x + x;
            pixel.y = imageData.y + y;
            region.add(pixel);
          }
        }
      }
    } else {
      ImageData mask = imageData.getTransparencyMask();
      Rectangle pixel = new Rectangle(0, 0, 1, 1);
      for (int y = 0; y < mask.height; y++) {
        for (int x = 0; x < mask.width; x++) {
          if (mask.getPixel(x, y) != 0) {
            pixel.x = imageData.x + x;
            pixel.y = imageData.y + y;
            region.add(pixel);
          }
        }
      }
    }
    shell.setRegion(region);

    Listener l =
        new Listener() {
          /** The x/y of the MouseDown, relative to top-left of the shell. */
          int startX, startY;

          @Override
          public void handleEvent(Event e) {
            if (e.type == SWT.KeyDown && e.character == SWT.ESC) {
              shell.dispose();
            }
            if (e.type == SWT.MouseDown && e.button == 1) {
              Point p = shell.toDisplay(e.x, e.y);
              Point loc = shell.getLocation();
              startX = p.x - loc.x;
              startY = p.y - loc.y;
            }
            if (e.type == SWT.MouseMove && (e.stateMask & SWT.BUTTON1) != 0) {
              Point p = shell.toDisplay(e.x, e.y);
              p.x -= startX;
              p.y -= startY;
              shell.setLocation(p);
            }
            if (e.type == SWT.Paint) {
              e.gc.drawImage(image, imageData.x, imageData.y);
            }
          }
        };
    shell.addListener(SWT.KeyDown, l);
    shell.addListener(SWT.MouseDown, l);
    shell.addListener(SWT.MouseMove, l);
    shell.addListener(SWT.Paint, l);

    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) display.sleep();
    }
    region.dispose();
    image.dispose();
    display.dispose();
  }
コード例 #11
0
ファイル: ToolItem.java プロジェクト: Crellis/Laptopworkspace
  void updateImages(boolean enabled) {
    if ((style & SWT.SEPARATOR) != 0) return;
    long /*int*/ hwnd = parent.handle;
    TBBUTTONINFO info = new TBBUTTONINFO();
    info.cbSize = TBBUTTONINFO.sizeof;
    info.dwMask = OS.TBIF_IMAGE;
    OS.SendMessage(hwnd, OS.TB_GETBUTTONINFO, id, info);
    if (info.iImage == OS.I_IMAGENONE && image == null) return;
    ImageList imageList = parent.getImageList();
    ImageList hotImageList = parent.getHotImageList();
    ImageList disabledImageList = parent.getDisabledImageList();
    if (info.iImage == OS.I_IMAGENONE) {
      Rectangle bounds = image.getBounds();
      int listStyle = parent.style & SWT.RIGHT_TO_LEFT;
      if (imageList == null) {
        imageList = display.getImageListToolBar(listStyle, bounds.width, bounds.height);
      }
      if (disabledImageList == null) {
        disabledImageList =
            display.getImageListToolBarDisabled(listStyle, bounds.width, bounds.height);
      }
      if (hotImageList == null) {
        hotImageList = display.getImageListToolBarHot(listStyle, bounds.width, bounds.height);
      }
      Image disabled = disabledImage;
      if (disabledImage == null) {
        if (disabledImage2 != null) disabledImage2.dispose();
        disabledImage2 = null;
        disabled = image;
        if (!enabled) {
          disabled = disabledImage2 = new Image(display, image, SWT.IMAGE_DISABLE);
        }
      }
      /*
       * Bug in Windows.  When a tool item with the style
       * BTNS_CHECK or BTNS_CHECKGROUP is selected and then
       * disabled, the item does not draw using the disabled
       * image.  The fix is to assign the disabled image in
       * all image lists.
       */
      Image image2 = image, hot = hotImage;
      if ((style & (SWT.CHECK | SWT.RADIO)) != 0) {
        if (!enabled) image2 = hot = disabled;
      }
      info.iImage = imageList.add(image2);
      disabledImageList.add(disabled);
      hotImageList.add(hot != null ? hot : image2);
      parent.setImageList(imageList);
      parent.setDisabledImageList(disabledImageList);
      parent.setHotImageList(hotImageList);
    } else {
      Image disabled = null;
      if (disabledImageList != null) {
        if (image != null) {
          if (disabledImage2 != null) disabledImage2.dispose();
          disabledImage2 = null;
          disabled = disabledImage;
          if (disabledImage == null) {
            disabled = image;
            if (!enabled) {
              disabled = disabledImage2 = new Image(display, image, SWT.IMAGE_DISABLE);
            }
          }
        }
        disabledImageList.put(info.iImage, disabled);
      }
      /*
       * Bug in Windows.  When a tool item with the style
       * BTNS_CHECK or BTNS_CHECKGROUP is selected and then
       * disabled, the item does not draw using the disabled
       * image.  The fix is to use the disabled image in all
       * image lists.
       */
      Image image2 = image, hot = hotImage;
      if ((style & (SWT.CHECK | SWT.RADIO)) != 0) {
        if (!enabled) image2 = hot = disabled;
      }
      if (imageList != null) imageList.put(info.iImage, image2);
      if (hotImageList != null) {
        hotImageList.put(info.iImage, hot != null ? hot : image2);
      }
      if (image == null) info.iImage = OS.I_IMAGENONE;
    }

    /*
     * Bug in Windows.  If the width of an item has already been
     * calculated, the tool bar control will not recalculate it to
     * include the space for the image.  The fix is to set the width
     * to zero, forcing the control recalculate the width for the item.
     */
    info.dwMask |= OS.TBIF_SIZE;
    info.cx = 0;
    OS.SendMessage(hwnd, OS.TB_SETBUTTONINFO, id, info);
    long /*int*/ hFont = OS.SendMessage(hwnd, OS.WM_GETFONT, 0, 0);
    OS.SendMessage(hwnd, OS.WM_SETFONT, hFont, 0);
    parent.layoutItems();
  }
コード例 #12
0
  Image getDragSourceImage(DragSourceEvent event) {
    if (dragSourceImage != null) dragSourceImage.dispose();
    dragSourceImage = null;

    Table table = (Table) control;
    if (OS.GTK_VERSION < OS.VERSION(2, 2, 0)) return null;
    // TEMPORARY CODE
    if (table.isListening(SWT.EraseItem) || table.isListening(SWT.PaintItem)) return null;
    /*
     * Bug in GTK.  gtk_tree_selection_get_selected_rows() segmentation faults
     * in versions smaller than 2.2.4 if the model is NULL.  The fix is
     * to give a valid pointer instead.
     */
    long /*int*/ handle = table.handle;
    long /*int*/ selection = OS.gtk_tree_view_get_selection(handle);
    long /*int*/[] model = OS.GTK_VERSION < OS.VERSION(2, 2, 4) ? new long /*int*/[1] : null;
    long /*int*/ list = OS.gtk_tree_selection_get_selected_rows(selection, model);
    if (list == 0) return null;
    int count = Math.min(10, OS.g_list_length(list));

    Display display = table.getDisplay();
    if (count == 1) {
      long /*int*/ path = OS.g_list_nth_data(list, 0);
      long /*int*/ pixmap = OS.gtk_tree_view_create_row_drag_icon(handle, path);
      dragSourceImage = Image.gtk_new(display, SWT.ICON, pixmap, 0);
    } else {
      int width = 0, height = 0;
      int[] w = new int[1], h = new int[1];
      int[] yy = new int[count], hh = new int[count];
      long /*int*/[] pixmaps = new long /*int*/[count];
      GdkRectangle rect = new GdkRectangle();
      for (int i = 0; i < count; i++) {
        long /*int*/ path = OS.g_list_nth_data(list, i);
        OS.gtk_tree_view_get_cell_area(handle, path, 0, rect);
        pixmaps[i] = OS.gtk_tree_view_create_row_drag_icon(handle, path);
        OS.gdk_drawable_get_size(pixmaps[i], w, h);
        width = Math.max(width, w[0]);
        height = rect.y + h[0] - yy[0];
        yy[i] = rect.y;
        hh[i] = h[0];
      }
      long /*int*/ source = OS.gdk_pixmap_new(OS.GDK_ROOT_PARENT(), width, height, -1);
      long /*int*/ gcSource = OS.gdk_gc_new(source);
      long /*int*/ mask = OS.gdk_pixmap_new(OS.GDK_ROOT_PARENT(), width, height, 1);
      long /*int*/ gcMask = OS.gdk_gc_new(mask);
      GdkColor color = new GdkColor();
      color.pixel = 0;
      OS.gdk_gc_set_foreground(gcMask, color);
      OS.gdk_draw_rectangle(mask, gcMask, 1, 0, 0, width, height);
      color.pixel = 1;
      OS.gdk_gc_set_foreground(gcMask, color);
      for (int i = 0; i < count; i++) {
        OS.gdk_draw_drawable(source, gcSource, pixmaps[i], 0, 0, 0, yy[i] - yy[0], -1, -1);
        OS.gdk_draw_rectangle(mask, gcMask, 1, 0, yy[i] - yy[0], width, hh[i]);
        OS.g_object_unref(pixmaps[i]);
      }
      OS.g_object_unref(gcSource);
      OS.g_object_unref(gcMask);
      dragSourceImage = Image.gtk_new(display, SWT.ICON, source, mask);
    }
    OS.g_list_free(list);

    return dragSourceImage;
  }
コード例 #13
0
 /**
  * This implementation of <code>dragFinished</code> disposes the image that was created in <code>
  * TableDragSourceEffect.dragStart</code>.
  *
  * <p>Subclasses that override this method should call <code>super.dragFinished(event)</code> to
  * dispose the image in the default implementation.
  *
  * @param event the information associated with the drag finished event
  */
 public void dragFinished(DragSourceEvent event) {
   if (dragSourceImage != null) dragSourceImage.dispose();
   dragSourceImage = null;
 }
コード例 #14
0
  public static void main(String[] args) {
    Shell shell = new Shell();
    Display display = shell.getDisplay();
    shell.setLayout(new FillLayout());
    shell.setText("ExpandBar Example");

    Menu menubar = new Menu(shell, SWT.BAR);
    shell.setMenuBar(menubar);
    MenuItem fileItem = new MenuItem(menubar, SWT.CASCADE);
    fileItem.setText("&File");
    Menu submenu = new Menu(shell, SWT.DROP_DOWN);
    fileItem.setMenu(submenu);
    item = new MenuItem(submenu, SWT.PUSH);
    item.setText("New ExpandItem");

    bar = new ExpandBar(shell, SWT.V_SCROLL);
    image = display.getSystemImage(SWT.ICON_QUESTION);

    // First item
    Composite composite = new Composite(bar, SWT.NONE);
    Menu popupmenu = new Menu(shell, SWT.POP_UP);
    MenuItem popupItem = new MenuItem(popupmenu, SWT.PUSH);
    popupItem.setText("Popup");
    composite.setMenu(popupmenu);

    GridLayout layout = new GridLayout(2, false);
    layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 10;
    layout.verticalSpacing = 10;
    composite.setLayout(layout);
    Label label = new Label(composite, SWT.NONE);
    label.setImage(display.getSystemImage(SWT.ICON_ERROR));
    label = new Label(composite, SWT.NONE);
    label.setText("SWT.ICON_ERROR");
    label = new Label(composite, SWT.NONE);
    label.setImage(display.getSystemImage(SWT.ICON_INFORMATION));
    label = new Label(composite, SWT.NONE);
    label.setText("SWT.ICON_INFORMATION");
    label = new Label(composite, SWT.NONE);
    label.setImage(display.getSystemImage(SWT.ICON_WARNING));
    label = new Label(composite, SWT.NONE);
    label.setText("SWT.ICON_WARNING");
    label = new Label(composite, SWT.NONE);
    label.setImage(display.getSystemImage(SWT.ICON_QUESTION));
    label = new Label(composite, SWT.NONE);
    label.setText("SWT.ICON_QUESTION");
    ExpandItem item1 = new ExpandItem(bar, SWT.NONE);
    item1.setText("What is your favorite icon");
    item1.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
    item1.setControl(composite);
    item1.setImage(image);

    // Second item
    composite = new Composite(bar, SWT.NONE);
    layout = new GridLayout(2, true);
    layout.marginLeft = layout.marginTop = layout.marginRight = layout.marginBottom = 10;
    layout.verticalSpacing = 10;
    composite.setLayout(layout);
    Button button1 = new Button(composite, SWT.PUSH);
    button1.setText("Button 1");
    Button button2 = new Button(composite, SWT.PUSH);
    button2.setText("Button 2");

    ExpandItem item0 = new ExpandItem(bar, SWT.NONE);
    item0.setText("What is your favorite button");
    item0.setHeight(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
    item0.setControl(composite);
    item0.setImage(image);
    item0.setExpanded(false);

    item.addListener(
        SWT.Selection,
        new Listener() {
          @Override
          public void handleEvent(Event e) {
            ExpandItem item2 = new ExpandItem(bar, SWT.NONE);
            Composite composite = new Composite(bar, SWT.NONE);
            GridLayout layout = new GridLayout(2, false);
            composite.setLayout(layout);
            Label label = new Label(composite, SWT.NONE);
            label.setText("What is your name?");
            Composite pcomposite = new Composite(composite, SWT.NONE);
            Text text = new Text(pcomposite, SWT.NONE);
            item2.setText("New Question");
            text.pack();
            composite.pack();
            Point size = composite.computeSize(SWT.DEFAULT, SWT.DEFAULT);
            item2.setHeight(size.y);
            item2.setControl(composite);
            item2.setImage(image);
            item2.setExpanded(true);
          }
        });

    bar.setSpacing(8);
    shell.setSize(400, 550);
    shell.open();
    System.out.println("------------------------------");
    System.out.println("getSize: " + button1.getSize());
    System.out.println("getBounds: " + button1.getBounds());
    System.out.println("computeSize: " + button1.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    System.out.println("getLocation: " + button1.getLocation());

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    image.dispose();
    display.dispose();
  }
コード例 #15
0
  public static void main(String[] args) {
    final Display display = new Display();
    final Clipboard clipboard = new Clipboard(display);
    final Shell shell = new Shell(display, SWT.SHELL_TRIM);
    shell.setLayout(new GridLayout());
    shell.setText("Clipboard ImageTransfer");

    final Button imageButton = new Button(shell, SWT.NONE);
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    gd.minimumHeight = 400;
    gd.minimumWidth = 600;
    imageButton.setLayoutData(gd);

    final Text imageText = new Text(shell, SWT.BORDER);
    imageText.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));

    Composite buttons = new Composite(shell, SWT.NONE);
    buttons.setLayout(new GridLayout(4, true));
    Button button = new Button(buttons, SWT.PUSH);
    button.setText("Open");
    button.addListener(
        SWT.Selection,
        event -> {
          FileDialog dialog = new FileDialog(shell, SWT.OPEN);
          dialog.setText("Open an image file or cancel");
          String string = dialog.open();
          if (string != null) {
            imageButton.setText("");
            Image image = imageButton.getImage();
            if (image != null) image.dispose();
            image = new Image(display, string);
            imageButton.setImage(image);
            imageText.setText(string);
          }
        });

    button = new Button(buttons, SWT.PUSH);
    button.setText("Copy");
    button.addListener(
        SWT.Selection,
        event -> {
          Image image = imageButton.getImage();
          if (image != null) {
            ImageTransfer imageTransfer = ImageTransfer.getInstance();
            TextTransfer textTransfer = TextTransfer.getInstance();
            clipboard.setContents(
                new Object[] {image.getImageData(), imageText.getText()},
                new Transfer[] {imageTransfer, textTransfer});
          }
        });

    button = new Button(buttons, SWT.PUSH);
    button.setText("Paste");
    button.addListener(
        SWT.Selection,
        event -> {
          ImageData imageData = (ImageData) clipboard.getContents(ImageTransfer.getInstance());
          if (imageData != null) {
            imageButton.setText("");
            Image image = imageButton.getImage();
            if (image != null) image.dispose();
            image = new Image(display, imageData);
            imageButton.setImage(image);
          } else {
            imageButton.setText("No image");
            imageButton.setImage(null);
          }
          String text = (String) clipboard.getContents(TextTransfer.getInstance());
          if (text != null) {
            imageText.setText(text);
          } else {
            imageText.setText("");
          }
        });

    button = new Button(buttons, SWT.PUSH);
    button.setText("Clear");
    button.addListener(
        SWT.Selection,
        event -> {
          imageButton.setText("");
          Image image = imageButton.getImage();
          if (image != null) image.dispose();
          imageButton.setImage(null);
          imageText.setText("");
        });

    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) display.sleep();
    }
    display.dispose();
  }