- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我知道要在 Composite 上绘图,您可以添加一个绘图监听器,但这会导致在子项下绘图。如果我想在 children 的上方画画怎么办?
下面画了一条线,但是 subc 画在了它上面。
Composite c = new Composite(shell, 0);
c.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_BLUE));
c.setBounds(100, 100, 800, 600);
c.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
e.gc.drawLine(0, 0, 500, 1000);
}
});
final Composite subc = new Composite(c, 0);
subc.setLayout(null);
subc.setBounds(10, 10, 600, 400);
最佳答案
解决起来并不难:-)
您不仅需要将 SWT.Paint
监听器添加到 Composite
,还需要将其添加到所有包含的子项(递归)。诀窍是为每个控件适本地映射坐标...
为了说明,我附上了我在许多项目中使用的一些代码。是的,我确实知道缺少类(class),但您或许可以从中得到启发。
/*******************************************************************************
* Copyright (c) 2007, 2011 The RCP Company and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* The RCP Company - initial API and implementation
*******************************************************************************/
package com.rcpcompany.uibindings.utils;
import org.eclipse.jface.fieldassist.ControlDecoration;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import com.rcpcompany.uibindings.IDisposable;
import com.rcpcompany.uibindings.internal.utils.PaintDecorationManager;
/**
* Support for arbitrary decorations for {@link Control controls}.
* <p>
* <p>
* Differs from {@link ControlDecoration} in a number of ways:
* <ul>
* <li>Support for cells in tables</li>
* <li>Vastly more efficient when there are many decorations</li>
* </ul>
*
* @author Tonny Madsen, The RCP Company
*/
public interface IPaintDecoration {
/**
* Factory for {@link IPaintDecoration}.
*/
final class Factory {
private Factory() {
}
/**
* Adds a new decoration.
* <p>
* The decoration is only added if the control of the decoration is non-<code>null</code>.
* <p>
* If the decoration supports the {@link IDisposable} interface, it will be notified when
* the decoration is no longer in use - e.g. when the decoration is removed with
* {@link #removeDecoration(IPaintDecoration)} or if the control is disposed.
*
* @param decoration the new decoration
*/
public static void addDecoration(IPaintDecoration decoration) {
PaintDecorationManager.addDecoration(decoration);
}
public static IPaintDecoration paintRectangle(final Control c, Rectangle rect, final Color color) {
final Rectangle r;
if (rect == null) {
final Point s = c.getSize();
r = new Rectangle(0, 0, s.x, s.y);
} else {
r = rect;
}
final IPaintDecoration pd = new IPaintDecoration() {
@Override
public void paint(Event event, Rectangle area) {
// LogUtils.debug(this, event.widget + ": clip=" + event.gc.getClipping() +
// " area=" + area);
final Color oldForeground = event.gc.getForeground();
event.gc.setForeground(color);
event.gc.drawRectangle(area.x, area.y, area.width - 1, area.height - 1);
event.gc.setForeground(oldForeground);
}
@Override
public Control getControl() {
return c;
}
@Override
public Rectangle getArea() {
return r;
}
};
addDecoration(pd);
return pd;
}
/**
* Removes an existing decoration.
*
* @param decoration the decoration to remove
*/
public static void removeDecoration(IPaintDecoration decoration) {
PaintDecorationManager.removeDecoration(decoration);
}
};
/**
* The control of this decoration.
* <p>
* The control of a specific decoration may not change during the lifetime of the decoration.
*
* @return the control
*/
Control getControl();
/**
* Returns the area of this decoration in relation to the control.
*
* @return the relative location
*/
Rectangle getArea();
/**
* Paints the decoration.
*
* @param area TODO
*/
void paint(Event event, Rectangle area);
}
和
/*******************************************************************************
* Copyright (c) 2007, 2011 The RCP Company and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* The RCP Company - initial API and implementation
*******************************************************************************/
package com.rcpcompany.uibindings.internal.utils;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.eclipse.jface.util.Util;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import com.rcpcompany.uibindings.IDisposable;
import com.rcpcompany.uibindings.internal.Activator;
import com.rcpcompany.uibindings.utils.IPaintDecoration;
import com.rcpcompany.utils.logging.LogUtils;
/**
* Simple manager that can draw arbitrary "stuff".
* <p>
* A manager exists for each {@link Shell} of the application and is automatically disposed with the
* shell.
* <p>
* Each decoration of the manager is handled internally via an {@link DecorationData} object.
*
* @author Tonny Madsen, The RCP Company
*/
public final class PaintDecorationManager implements IDisposable, Listener {
/**
* The shell of this manager.
*/
private final Shell myShell;
/**
* Cached platform flag for dealing with platform-specific issue:
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=219326 : Shell with custom region and
* SWT.NO_TRIM still has border
*/
private static boolean MAC = Util.isMac();
/**
* Constructs and returns a new manager.
*
* @param shell the shell of the manager
*/
private PaintDecorationManager(Shell shell) {
myShell = shell;
theManagers.put(getShell(), this);
hookControl(getShell());
}
@Override
public void dispose() {
/*
* Unhook all controls. This is automatically remove all decorations.
*/
for (final Control c : myHookedControls.toArray(new Control[myHookedControls.size()])) {
unhookControl(c);
}
theManagers.remove(getShell());
}
public static void addDecoration(IPaintDecoration decoration) {
final PaintDecorationManager mng = getManager(decoration);
if (mng != null) {
mng.addADecoration(decoration);
}
}
public static void removeDecoration(IPaintDecoration decoration) {
final PaintDecorationManager mng = getManager(decoration);
if (mng != null) {
mng.removeADecoration(decoration);
}
}
/**
* Mapping of all decorations of this manager to internal data for the same decoration.
*/
private final Map<IPaintDecoration, DecorationData> myDecorations = new HashMap<IPaintDecoration, DecorationData>();
public void addADecoration(IPaintDecoration decoration) {
DecorationData dd = myDecorations.get(decoration);
if (dd == null) {
dd = new DecorationData(decoration);
}
dd.update();
}
public void removeADecoration(IPaintDecoration decoration) {
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS) {
LogUtils.debug(this, "control: " + decoration.getControl() + "@" + decoration.getControl().hashCode() + "/"
+ decoration.getArea());
}
final DecorationData dd = myDecorations.get(decoration);
if (dd == null) return;
dd.dispose();
}
/**
* Map with all defined managers indexed by the shell.
*/
private static Map<Shell, PaintDecorationManager> theManagers = new HashMap<Shell, PaintDecorationManager>();
/**
* Returns the shell of the manager.
*
* @return the shell
*/
private Shell getShell() {
return myShell;
}
/**
* Returns the manager for the specified decoration.
* <p>
* Creates a new manager if none exists
*
* @param decoration the decoration
* @return the manager for the shell of the decoration
*/
private static PaintDecorationManager getManager(IPaintDecoration decoration) {
final Control c = decoration.getControl();
if (c == null) return null;
final Shell shell = c.getShell();
PaintDecorationManager mng = theManagers.get(shell);
if (mng == null) {
mng = new PaintDecorationManager(shell);
}
return mng;
}
/**
* The hooked controls of this manager.
* <p>
* A control is hooked when first referred in a decoration or a parent...
* <p>
* It is not unhooked until the control or this manager is disposed.
*/
private final Set<Control> myHookedControls = new HashSet<Control>();
/**
* Hooks the specified control into this manager.
* <p>
* Also hooks all parent controls.
*
* @param control the control
*/
public void hookControl(Control control) {
if (myHookedControls.contains(control)) return;
myHookedControls.add(control);
control.addListener(SWT.Dispose, this);
control.addListener(SWT.Paint, this);
if (control != getShell()) {
hookControl(control.getParent());
}
}
/**
* Unhooks a specific control from the manager.
*
* @param control the control
*/
public void unhookControl(Control control) {
if (!myHookedControls.contains(control)) return;
myHookedControls.remove(control);
if (!control.isDisposed()) {
control.removeListener(SWT.Dispose, this);
control.removeListener(SWT.Paint, this);
}
for (final DecorationData dd : myDecorations.values()
.toArray(new DecorationData[myDecorations.values().size()])) {
if (dd.getControl() == control) {
dd.dispose();
}
}
}
@Override
public void handleEvent(Event event) {
// LogUtils.debug(this, ToStringUtils.toString(event));
switch (event.type) {
case SWT.Dispose:
handleDispose(event);
break;
case SWT.Paint:
handlePaint(event);
break;
default:
break;
}
}
/**
* Handles the dispose event.
*
* @param event the event
*/
private void handleDispose(Event event) {
if (event.widget == getShell()) {
dispose();
return;
}
unhookControl((Control) event.widget);
}
/**
* Handles the paint event.
*
* @param event the event
*/
private void handlePaint(Event event) {
final Control c = (Control) event.widget;
final Display display = c.getDisplay();
final Rectangle area = display.map(c, null, event.x, event.y, event.width, event.height);
for (final DecorationData dd : myDecorations.values()) {
if (dd.intersects(area)) {
dd.paint(event);
}
}
}
/**
* Manager internal decoration data for one decoration.
*/
protected class DecorationData implements IDisposable {
private final IPaintDecoration myDecoration;
/**
* The previous area painted by this decoration relative to the display.
*/
private Rectangle myPreviousArea = null;
/**
* Set to true when the decoration is disposed
*/
private boolean isDisposed = false;
/**
* Constructs and returns a new decoration data object
*
* @param decoration he base decoration
*/
protected DecorationData(IPaintDecoration decoration) {
myDecoration = decoration;
myDecorations.put(getDecoration(), this);
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS) {
LogUtils.debug(this, "control: " + this);
}
getManager().hookControl(getDecoration().getControl());
}
/**
* Returns the control of the decoration
*
* @return the control
*/
public Control getControl() {
return getDecoration().getControl();
}
@Override
public void dispose() {
isDisposed = true;
update();
myDecorations.remove(getDecoration());
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS) {
LogUtils.debug(this, "control: " + this);
}
}
/**
* Returns the manager of this decoration
*
* @return the manager
*/
public PaintDecorationManager getManager() {
return PaintDecorationManager.this;
}
/**
* Returns the decoration itself
*
* @return the decoration
*/
public IPaintDecoration getDecoration() {
return myDecoration;
}
/**
* Updates this decoration
*/
public void update() {
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS) {
LogUtils.debug(this, "control: " + this);
}
/*
* Calculate new area
*/
final Rectangle newArea = getDecorationRectangle(getShell());
/*
* Compare with last area and image
*/
if ((newArea == null ? myPreviousArea == null : newArea.equals(myPreviousArea))) {
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS_VERBOSE) {
LogUtils.debug(this, "-- return");
}
return;
}
Rectangle r = null;
if (myPreviousArea != null) {
r = myPreviousArea;
if (newArea != null) {
r.add(newArea);
}
} else {
r = newArea;
}
myPreviousArea = newArea;
if (r != null) {
// LogUtils.debug(this, "redraw: " + r);
getShell().redraw(r.x, r.y, r.width, r.height, true);
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS_VERBOSE) {
LogUtils.debug(this, "redraw " + r);
}
}
}
/**
* Calculates the area taken by the image translated to a specified target control
*
* @param c the target control or null for the Display
*
* @return the {@link Rectangle} for the image or <code>null</code> if no image is specified
*/
private Rectangle getDecorationRectangle(Control c) {
final Control control = getDecoration().getControl();
final Rectangle b = getDecoration().getArea();
final Rectangle bounds = new Rectangle(b.x, b.y, b.width + 1, b.height + 1);
return getShell().getDisplay().map(control, c, bounds);
}
/**
* Paints this decoration.
*
* @param event the {@link SWT#Paint} event
*/
public void paint(Event event) {
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS_VERBOSE) {
LogUtils.debug(this, "paint: " + event.widget);
}
// if (!shouldShowDecoration()) {
// return;
// }
final Rectangle rect = getDecorationRectangle((Control) event.widget);
if (Activator.getDefault().TRACE_CONTROL_DECORATIONS_VERBOSE) {
LogUtils.debug(this, "paint: " + event.widget + "/" + event.widget.hashCode() + ": rect=" + rect);
}
getDecoration().paint(event, rect);
}
/**
* Returns whether this decoration intersects with specified rectangle.
*
* @param eventArea the area to check
* @return <code>true</code> if the decoration is visible and the area intersects
*/
public boolean intersects(Rectangle eventArea) {
if (isDisposed) return false;
if (!getControl().isVisible()) return false;
final Rectangle area = getDecorationRectangle(null);
if (area == null) return false;
if (!area.intersects(eventArea)) return false;
return true;
}
@Override
public String toString() {
return getControl() + "@" + getControl().hashCode() + " " + getDecoration().getArea() + " area "
+ getDecorationRectangle(null);
}
}
}
关于java - 如何在 SWT 中绘制 Composite 的子元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6849216/
我实际上是在尝试在 Java SWT 中重现类似的东西,但到目前为止还没有成功: 我看到的问题是,据我所知,SWT.TITLE 需要显示 SWT.CLOSE、SWT.MAX 和 SWT.MIN 谁能告
以下是我的代码: Group leftGroup = new Group(parent, SWT.NONE); leftListWidget = new org.eclipse.swt.widgets
我正在尝试运行一个 hello world SWT 应用程序: public static void main(String args[]) throws IOException{ Displ
我有一个 SWT 窗口,其中有组小部件,我在其中放置了几个其他小部件,我设置了组的标题及其工作正常。组标题颜色始终为蓝色(在我的情况下我不确定)并且不会与组内的其他 child 同步。所以我想知道是否
我正在使用 SWT CTabFolder 在我的应用程序中构建选项卡。我有点不喜欢标签的视觉外观,它太像 Eclipse。有没有办法改变标签的方面?我想更改关闭按钮,例如标签的位置,它们的外观可能..
对于我在使用大型 SWT 表的 RCP 应用程序中追逐了一段时间的问题,这更多是我想分享的答案。 问题是 SWT Table.remove(int start, int end) 方法的性能。它提供了
如何在不使用鼠标监听器的情况下编辑 SWT 表值? 最佳答案 做 TableEditor以下链接中的片段有帮助吗? SWT Snippets TableEditor中的第一个例子部分使用 Select
我想要一个带有两行工具提示的控件。例如 Label: Label label = new Label( parent, SWT.NONE ); label.setText( "label text"
A 想在我的 View 上加载下拉菜单时添加默认文本。我怎样才能在 SWT 和 JFace 中做到这一点? 最佳答案 我猜您想要做的是在将某些内容添加到 View 时在组合中显示某些内容(默认情况下它
在我的登录对话框中,有一个按钮: Text pwdT = new Text(container, SWT.BORDER|SWT.PASSWORD); Button plainBtn = new Bu
如何以编程方式垂直滚动 SWT 表? 在表上实现搜索功能。当找到一个项目时,它将滚动到找到的项目。 最佳答案 您可能想尝试几种方法: Table.showItem(TableItem) Table.s
我有一个 Composite我希望能够以编程方式启用/禁用。 Control.setEnabled(boolean enabled)方法工作正常,但它没有提供任何小部件被禁用的视觉信息。 我想做的是让
有几个“高级”表格/电子表格 SWT 小部件(Nattable、Nebula Grid),但它们都不支持真正的大数据集。 Nattable 是最接近的一种,但它在使用的数据类型方面仍然存在限制,导致表
以下问题让我感到困惑:我有一个 SWT Java 应用程序,并且在以下情况下从 SWT 3 切换到 SWT 4 会导致 KeyDown 事件出现奇怪的行为: 我有一个包含浏览器小部件的外壳。我正在通过
我在 eclipse SWT 应用程序中有许多表。它们是 Table/TableViewer 表格,由一个标题、左下方一行文本和一系列图形组成。除文本列外还有 32 列,编号从 0 到 31。在经典模
原目标: 我有一个 TreeMenu 用于显示我的菜单。 在这棵树中,用户可以选择不同的项目。 我想禁用树,以便用户在选择第一个项目后无法选择新项目。 问题是,我们不能使用 setEnabled,因为
是否已经有人在 IDEA 中编写了类似于“导航栏”的 SWT 控件?对于那些不知道我的意思的人:它看起来像 path1 > path2 > path3 > 其中每个“路径”都是模拟一个按钮的完整路径的
我需要使用 SWT/JFace 实现多选组合框,最好的方法是什么?我应该更改源代码还是应该尝试扩展组合? 最佳答案 不可能扩展 Combo 可以通过重写 checkSubclass() 来扩展 Com
我想用大量行(最多 3 mio)填充 SWT 虚拟表。当我使用懒惰的内容提供者时,我不能使用过滤器,而当我使用普通的内容提供者时,更改过滤器的性能会变得非常糟糕。有没有办法在 SWT 或 JFace
除了外观之外,组控制和复合之间有什么区别?我可以将容器放入组内吗?如果是,我必须使用什么布局?我尝试将容器放置在组内,但容器内的控件未显示。 Group grp= new Group(containe
我是一名优秀的程序员,十分优秀!