如何将图像与表格单元格的中心对齐(SWT Table)

发布于 2024-12-28 13:25:40 字数 512 浏览 0 评论 0原文

我开发 Eclipse RCP 应用程序并遇到 Table 问题。我们在数据库中有一些布尔格式的数据,用户希望使用 checkbox 查看该字段。

我尝试使用 Button(SWT.CHECK) 作为 Table-Editor 来实现它,但它运行得太慢了:(

我尝试使用 2 个图像 - 已选中和未选中的检查-框,它可以工作,但我无法将它们对齐到中心,它们会自动向左对齐,

我什至找到了如何捕获 SWT.MeasureItemSWT.PaintItem 事件。并通过更改手动处理它们event.x 字段,但我遇到了一个问题 - 我目前无法获取测量或绘制的列,因为 Event 没有向我提供该信息,

这是否是对齐图像的唯一方法 。通过修改重绘时的事件数据来居中,或者可能有其他方法通过复选框来表示布尔数据?我现在不需要编辑它们,所以只读模式应该足够了。

I develop Eclipse RCP application and got a problem with a Table. We have some data in database in boolean format and users wants to see that field using checkbox.

I tried to implement it using Button(SWT.CHECK) as Table-Editor, but it worked too slow :(

I tried to use 2 images - checked and unchecked check-boxes, it works, but I can't align them to center, they are aligned to left automatically.

I even found how to catch SWT.MeasureItem and SWT.PaintItem events and process them manually by change event.x field, but I got a problem - I can't get what column measuring or painting at the moment, because Event doesn't provides me that information.

Is it the only way to align images to center by modify event data on redrawing, or may be there is other ways to represent boolean data via check-boxes? I don't need to edit them now, so read-only mode should be enough.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

倾城月光淡如水﹏ 2025-01-04 13:25:40

您可以将 PaintListener 添加到您的表格中,当它绘制选定的列(在我的例子中为第 5 列)时,检查行的大小并自行对齐图像。

testTable.addListener(SWT.PaintItem, new Listener() {

    @Override
    public void handleEvent(Event event) {
        // Am I on collumn I need..?
        if(event.index == 5) {
            Image tmpImage = IMAGE_TEST_PASS;
            int tmpWidth = 0;
            int tmpHeight = 0;
            int tmpX = 0;
            int tmpY = 0;

            tmpWidth = testTable.getColumn(event.index).getWidth();
            tmpHeight = ((TableItem)event.item).getBounds().height;

            tmpX = tmpImage.getBounds().width;
            tmpX = (tmpWidth / 2 - tmpX / 2);
            tmpY = tmpImage.getBounds().height;
            tmpY = (tmpHeight / 2 - tmpY / 2);
            if(tmpX <= 0) tmpX = event.x;
            else tmpX += event.x;
            if(tmpY <= 0) tmpY = event.y;
            else tmpY += event.y;
            event.gc.drawImage(tmpImage, tmpX, tmpY);
        }
    }
});

You may add PaintListener to your table and when it will paint selected column (column 5 in my case), check the size of row and align the image by yourself..

testTable.addListener(SWT.PaintItem, new Listener() {

    @Override
    public void handleEvent(Event event) {
        // Am I on collumn I need..?
        if(event.index == 5) {
            Image tmpImage = IMAGE_TEST_PASS;
            int tmpWidth = 0;
            int tmpHeight = 0;
            int tmpX = 0;
            int tmpY = 0;

            tmpWidth = testTable.getColumn(event.index).getWidth();
            tmpHeight = ((TableItem)event.item).getBounds().height;

            tmpX = tmpImage.getBounds().width;
            tmpX = (tmpWidth / 2 - tmpX / 2);
            tmpY = tmpImage.getBounds().height;
            tmpY = (tmpHeight / 2 - tmpY / 2);
            if(tmpX <= 0) tmpX = event.x;
            else tmpX += event.x;
            if(tmpY <= 0) tmpY = event.y;
            else tmpY += event.y;
            event.gc.drawImage(tmpImage, tmpX, tmpY);
        }
    }
});
无声无音无过去 2025-01-04 13:25:40

以下是使用 OwnerDrawLabelProvider 的示例: http://bingjava.appspot.com/snippet.jsp? id=3221

我将其与 Tonny 的链接合并到TableViewers 和 Nativelooking 复选框 并创建了方便的抽象 CenteredCheckboxLabelProvider 类

import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.OwnerDrawLabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;

public abstract class CenteredCheckboxLabelProvider extends OwnerDrawLabelProvider {
    private static final String CHECKED_KEY = "CHECKED";
    private static final String UNCHECK_KEY = "UNCHECKED";

    public CenteredCheckboxLabelProvider(ColumnViewer viewer) {
        if (JFaceResources.getImageRegistry().getDescriptor(CHECKED_KEY) == null) {
            JFaceResources.getImageRegistry().put(UNCHECK_KEY, makeShot(viewer.getControl().getShell(), false));
            JFaceResources.getImageRegistry().put(CHECKED_KEY, makeShot(viewer.getControl().getShell(), true));
        }
    }

    private Image makeShot(Shell shell, boolean type) {
        Shell s = new Shell(shell, SWT.NO_TRIM);
        Button b = new Button(s, SWT.CHECK);
        b.setSelection(type);
        Point bsize = b.computeSize(SWT.DEFAULT, SWT.DEFAULT);
        b.setSize(bsize);
        b.setLocation(0, 0);
        s.setSize(bsize);
        s.open();

        GC gc = new GC(b);
        Image image = new Image(shell.getDisplay(), bsize.x, bsize.y);
        gc.copyArea(image, 0, 0);
        gc.dispose();

        s.close();

        return image;
    }

    public Image getImage(Object element) {
        if (isChecked(element)) {
            return JFaceResources.getImageRegistry().get(CHECKED_KEY);
        } else {
            return JFaceResources.getImageRegistry().get(UNCHECK_KEY);
        }
    }

    @Override
    protected void measure(Event event, Object element) {
    }

    @Override
    protected void paint(Event event, Object element) {
        Image img = getImage(element);

        if (img != null) {
            Rectangle bounds = ((TableItem) event.item).getBounds(event.index);
            Rectangle imgBounds = img.getBounds();
            bounds.width /= 2;
            bounds.width -= imgBounds.width / 2;
            bounds.height /= 2;
            bounds.height -= imgBounds.height / 2;

            int x = bounds.width > 0 ? bounds.x + bounds.width : bounds.x;
            int y = bounds.height > 0 ? bounds.y + bounds.height : bounds.y;

            event.gc.drawImage(img, x, y);
        }
    }

    protected abstract boolean isChecked(Object element);
}

Here is the example using OwnerDrawLabelProvider: http://bingjava.appspot.com/snippet.jsp?id=3221

I combined it with Tonny's link to TableViewers and Nativelooking Checkboxes and created handy abstract CenteredCheckboxLabelProvider class

import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.OwnerDrawLabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;

public abstract class CenteredCheckboxLabelProvider extends OwnerDrawLabelProvider {
    private static final String CHECKED_KEY = "CHECKED";
    private static final String UNCHECK_KEY = "UNCHECKED";

    public CenteredCheckboxLabelProvider(ColumnViewer viewer) {
        if (JFaceResources.getImageRegistry().getDescriptor(CHECKED_KEY) == null) {
            JFaceResources.getImageRegistry().put(UNCHECK_KEY, makeShot(viewer.getControl().getShell(), false));
            JFaceResources.getImageRegistry().put(CHECKED_KEY, makeShot(viewer.getControl().getShell(), true));
        }
    }

    private Image makeShot(Shell shell, boolean type) {
        Shell s = new Shell(shell, SWT.NO_TRIM);
        Button b = new Button(s, SWT.CHECK);
        b.setSelection(type);
        Point bsize = b.computeSize(SWT.DEFAULT, SWT.DEFAULT);
        b.setSize(bsize);
        b.setLocation(0, 0);
        s.setSize(bsize);
        s.open();

        GC gc = new GC(b);
        Image image = new Image(shell.getDisplay(), bsize.x, bsize.y);
        gc.copyArea(image, 0, 0);
        gc.dispose();

        s.close();

        return image;
    }

    public Image getImage(Object element) {
        if (isChecked(element)) {
            return JFaceResources.getImageRegistry().get(CHECKED_KEY);
        } else {
            return JFaceResources.getImageRegistry().get(UNCHECK_KEY);
        }
    }

    @Override
    protected void measure(Event event, Object element) {
    }

    @Override
    protected void paint(Event event, Object element) {
        Image img = getImage(element);

        if (img != null) {
            Rectangle bounds = ((TableItem) event.item).getBounds(event.index);
            Rectangle imgBounds = img.getBounds();
            bounds.width /= 2;
            bounds.width -= imgBounds.width / 2;
            bounds.height /= 2;
            bounds.height -= imgBounds.height / 2;

            int x = bounds.width > 0 ? bounds.x + bounds.width : bounds.x;
            int y = bounds.height > 0 ? bounds.y + bounds.height : bounds.y;

            event.gc.drawImage(img, x, y);
        }
    }

    protected abstract boolean isChecked(Object element);
}
盗梦空间 2025-01-04 13:25:40

您可能还想查看此博客条目: TableViewers 和 Nativelooking 复选框

You might also want to have a look at this blog entry: TableViewers and Nativelooking Checkboxes.

木格 2025-01-04 13:25:40

巫师的解决方案派上了用场。我已将其改编为自定义 IStyledLabelProvider,以便在 DelegatingStyledCellLabelProvider 中使用:

public class MutedLabelProvider extends LabelProvider implements IStyledLabelProvider {

  public MutedLabelProvider(Tree containerTree, int columnIndex) {
    registerIconListener(containerTree, columnIndex);
  }

  @Override
  public StyledString getStyledText(Object element) {
    return new StyledString();
  }

  private void registerIconListener(Tree containerTree, int columnIndex) {
    containerTree.addListener(SWT.PaintItem, new Listener() {
      @Override
      public void handleEvent(Event event) {
        if (event.index == columnIndex) {
          TreeItem treeItem = (TreeItem) event.item;
          Object data = treeItem.getData();
          Image tmpImage = getImageFromData(data);
          if (tmpImage != null) {
            int tmpWidth = 0;
            int tmpHeight = 0;
            int tmpX = 0;
            int tmpY = 0;
            tmpWidth = containerTree.getColumn(event.index).getWidth();
            tmpHeight = treeItem.getBounds().height;
            tmpX = tmpImage.getBounds().width;
            tmpX = (tmpWidth / 2 - tmpX / 2);
            tmpY = tmpImage.getBounds().height;
            tmpY = (tmpHeight / 2 - tmpY / 2);
            if (tmpX <= 0) tmpX = event.x;
            else tmpX += event.x;
            if (tmpY <= 0) tmpY = event.y;
            else tmpY += event.y;
            event.gc.drawImage(tmpImage, tmpX, tmpY);
          }
        }
      }
    });
  }

  private Image getImageFromData(Object element) {
    if (element != null && element instanceof IMarker) {
      IMarker marker = (IMarker) element;
      boolean isItTrue = marker.getAttribute("MyBooleanAttr", false);
      if (isItTrue) {
        // TODO return here the image you want for true state
        return JFaceResources.getImageRegistry().get("MyImageForTrue");
      } else {
        // TODO return here the image you want for false state
        // Alternatively, you can return null for no image
        return null;
      }
    } else {
      return null;
    }
  }

}

请注意,我使用 IMarker 作为元素和 TreeViewer 的数据(因此我正在接收 TreeItems),但您的数据可能会有所不同,并且您的查看器可能会有所不同成为 TableViewer;你只需要做自己的调整。

Sorceror's solution came in handy. I've adapted it to a custom IStyledLabelProvider to be used within a DelegatingStyledCellLabelProvider:

public class MutedLabelProvider extends LabelProvider implements IStyledLabelProvider {

  public MutedLabelProvider(Tree containerTree, int columnIndex) {
    registerIconListener(containerTree, columnIndex);
  }

  @Override
  public StyledString getStyledText(Object element) {
    return new StyledString();
  }

  private void registerIconListener(Tree containerTree, int columnIndex) {
    containerTree.addListener(SWT.PaintItem, new Listener() {
      @Override
      public void handleEvent(Event event) {
        if (event.index == columnIndex) {
          TreeItem treeItem = (TreeItem) event.item;
          Object data = treeItem.getData();
          Image tmpImage = getImageFromData(data);
          if (tmpImage != null) {
            int tmpWidth = 0;
            int tmpHeight = 0;
            int tmpX = 0;
            int tmpY = 0;
            tmpWidth = containerTree.getColumn(event.index).getWidth();
            tmpHeight = treeItem.getBounds().height;
            tmpX = tmpImage.getBounds().width;
            tmpX = (tmpWidth / 2 - tmpX / 2);
            tmpY = tmpImage.getBounds().height;
            tmpY = (tmpHeight / 2 - tmpY / 2);
            if (tmpX <= 0) tmpX = event.x;
            else tmpX += event.x;
            if (tmpY <= 0) tmpY = event.y;
            else tmpY += event.y;
            event.gc.drawImage(tmpImage, tmpX, tmpY);
          }
        }
      }
    });
  }

  private Image getImageFromData(Object element) {
    if (element != null && element instanceof IMarker) {
      IMarker marker = (IMarker) element;
      boolean isItTrue = marker.getAttribute("MyBooleanAttr", false);
      if (isItTrue) {
        // TODO return here the image you want for true state
        return JFaceResources.getImageRegistry().get("MyImageForTrue");
      } else {
        // TODO return here the image you want for false state
        // Alternatively, you can return null for no image
        return null;
      }
    } else {
      return null;
    }
  }

}

Note that I'm using IMarker as data for elements, and a TreeViewer (so I'm receiving TreeItems), but your data might differ, and your viewer might be a TableViewer; you just have to make your own adjustments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文