Sponsored link
Insets (GEF/Draw2D)
FigureのInsetsを表示(上,下,左,右)

BorderはFigureの内側に描かれます。
その描かれる範囲(上・下・左・右からどれだけのPixelを内側に使用するか)を表現するのがInsetsです。
また、Insetsを使って、Rectangleを拡張したり縮小したりできます。
うまくやれば、Figureでマウスイベントが起きたときに、Border上で起きたものかどうか知ることもできます。
自信ありませんが、以下のようにすればいいでしょう。
Insetsは、FigureからBorderを経由して取得できますが、Figureからは、当然ながら設定はできません。
Borderの種類によっては、設定できるのもあるでしょうが、普通、Borderからも設定できません。
public class MouseClicked extends MouseListener.Stub{
public void mousePressed(MouseEvent me) {
Figure figure=(Figure)me.getSource();
//I guess there are other better way.
if(figure.getBounds().contains(me.x,me.y) && !figure.getBounds().getCropped(figure.getInsets()).contains(me.x,me.y)){
shell.setText("click border");
}else{
shell.setText("click inside");
}
}
}
サンプル
GEFに含まれるdraw2d.jarが必要です。
コードはCVSからダウンロードできます。
/*
* Created on 2005/08/02
* Author aki@www.xucker.jpn.org
* License Apache2.0 or Common Public License
*/
package example.draw2d;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.MouseEvent;
import org.eclipse.draw2d.MouseListener;
import org.eclipse.draw2d.Panel;
import org.eclipse.draw2d.SimpleEtchedBorder;
import org.eclipse.draw2d.TitleBarBorder;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/**
*
*
*/
public class InsetsTest {
private Shell shell;
public InsetsTest(Shell shell) {
this.shell=shell;
shell.setBounds(0,0,250,250);
shell.setLayout(new FillLayout(SWT.VERTICAL));
FigureCanvas canvas = new FigureCanvas(shell);
Panel panel=new Panel();
canvas.setContents(panel);
XYLayout layout=new XYLayout();
panel.setLayoutManager(layout);
Label rect1=new Label();
rect1.addMouseListener(new MouseClicked());
rect1.setBorder(new LineBorder(5));
rect1.setText(toInsets(rect1.getInsets()));
panel.add(rect1,new Rectangle(10,10,70,50));
Label rect2 = new Label();
rect2.setBorder(new TitleBarBorder());
rect2.addMouseListener(new MouseClicked());
rect2.setText(toInsets(rect2.getInsets()));
panel.add(rect2,new Rectangle(70,70,70,50));
Label rect3 = new Label();
rect3.addMouseListener(new MouseClicked());
rect3.setBorder(SimpleEtchedBorder.singleton);
rect3.setText(toInsets(rect3.getInsets()));
panel.add(rect3,new Rectangle(140,140,70,50));
}
public String toInsets(Insets in){
return ""+in.top+","+in.bottom+","+in.left+","+in.right;
}
public class MouseClicked extends MouseListener.Stub{
public void mousePressed(MouseEvent me) {
Figure figure=(Figure)me.getSource();
//I guess there are other better way.
if(figure.getBounds().contains(me.x,me.y) && !figure.getBounds().getCropped(figure.getInsets()).contains(me.x,me.y)){
shell.setText("click border");
}else{
shell.setText("click inside");
}
}
}
public static void main(String[] args) {
Display display=new Display();
Shell shell=new Shell(display);
InsetsTest app=new InsetsTest(shell);
shell.open();
while(!shell.isDisposed()){
if (!display.readAndDispatch ()){
display.sleep ();
}
}
display.dispose();
}
}
