サッシュ(sash)は、英語で帯を意味します。 つまりSashクラスは帯を作成します。 帯をつくって何か意味があるのでしょうか? いえ、意味があるからそういったAPIが提供されているはずです。
まずは、Sashの最も簡単なサンプルプログラムを書いてみます。 このサンプルプログラムは、ただ単にSashを作成するだけです。 しかしSashがどのようなものなのかを理解するには十分です。
Sashを作成するには、org.eclipse.swt.widgets.Sashクラスを使います。 コンストラクタへの引数に指定するスタイルは、VERTICALが縦長の、HORIZONTALが横長のSashを作成するときに使用します。 そして、縦長のSashは横方向に、横長のSashは縦方向にドラッグすることができます。
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Sash;
public class SashTest extends ApplicationWindow {
private SashTest() {
super(null);
}
protected Control createContents(Composite parent) {
parent.setLayout(null);
parent.setSize(100, 100);
Sash sash = new Sash(parent, SWT.VERTICAL);
sash.setBackground(new Color(Display.getCurrent(), 0xAA, 0xAA, 0xAA));
sash.setBounds(49, 0, 2, 100);
return parent;
}
public static void main(String[] args) {
Window w = new SashTest();
w.setBlockOnOpen(true);
w.open();
Display.getCurrent().dispose();
}
}
今回作成したSashプログラムは、何も動きません。 マウスでドラッグしようとすると、ドラッグマークは出るのですが、ドロップするとまた元の位置に戻ってしまいます。 つまり、Sashの位置が移動しません。 しかし、Sashがどんな用途に使えるだろうかと想像することはできたと思います。
では次に、きちんと動くサンプルプログラムを作成してみます。
Sashをきちんと移動させるためには、Sashウィジェットに対してSelectionListenerを登録する必要があります。
Sashには、Sash#addSelectionListener()メソッドにより、
それがドラッグされたことを知るためのイベントリスナを登録することができます。
SelectionListenerクラスのメソッドのうち、実際に呼び出されるのは widgetSelected()メソッドです。
この時送られるSelectionEventには、幾つかのフィールドがありますが、
今回のサンプルプログラムでは、width(Sashの幅)とx(SashのX座標)のフィールドを使用しています。
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Sash;
public class MeaningfulSash extends ApplicationWindow {
private MeaningfulSash() {
super(null);
}
protected Control createContents(Composite parent) {
parent.setLayout(null);
final Button leftButton = new Button(parent, SWT.PUSH);
leftButton.setText("left");
leftButton.setBounds(0, 0, 49, 100);
final Sash sash = new Sash(parent, SWT.VERTICAL);
sash.setBackground(new Color(Display.getCurrent(), 0xFF, 0xFF, 0x00));
sash.setBounds(49, 0, 2, 100);
final Button rightButton = new Button(parent, SWT.PUSH);
rightButton.setText("right");
rightButton.setBounds(51, 0, 49, 100);
sash.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
// this method is not be called
}
public void widgetSelected(SelectionEvent e) {
leftButton.setBounds(0, 0, e.x, 100);
sash.setLocation(e.x, 0);
rightButton.setBounds(e.x, 0, 100 - e.width - e.x, 100);
}
});
parent.pack();
return parent;
}
public static void main(String[] args) {
Window w = new MeaningfulSash();
w.setBlockOnOpen(true);
w.open();
Display.getCurrent().dispose();
}
}
今回は、前回のサンプルプログラムと比べて、Sashの意義がわかる内容に仕上がっていると思います。 Sashは、ウィジェットの大きさや、 コンポジットの領域の関係を調整するためのユーザインターフェイスというわけです。