📄 sashexample.java
字号:
package com.hnjchina.example.tableviewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.LineBackgroundEvent;
import org.eclipse.swt.custom.LineBackgroundListener;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
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.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Sash;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;
public class SashExample extends ViewPart {
public void createPartControl(Composite parent) {
createContents(parent);
SashFormExample(parent);
SashFormExample2(parent);
StyleText(parent);
}
public void setFocus() {
}
public void createContents(Composite composite) {
composite.setLayout(new FormLayout());
// Create the sash first, so the other controls
// can be attached to it.
final Sash sash = new Sash(composite, SWT.VERTICAL);
FormData data = new FormData();
data.top = new FormAttachment(0, 0); // Attach to top
data.bottom = new FormAttachment(100, 0); // Attach to bottom
data.left = new FormAttachment(50, 0); // Attach halfway across
sash.setLayoutData(data);
sash.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
// We reattach to the left edge, and we use the x value of the event to
// determine the offset from the left
((FormData) sash.getLayoutData()).left = new FormAttachment(0, event.x);
// Until the parent window does a layout, the sash will not be redrawn in
// its new location.
sash.getParent().layout();
}
});
// Create the first text box and attach its right edge
// to the sash
Text one = new Text(composite, SWT.BORDER);
data = new FormData();
data.top = new FormAttachment(0, 0);
data.bottom = new FormAttachment(100, 0);
data.left = new FormAttachment(0, 0);
data.right = new FormAttachment(sash, 0);
one.setLayoutData(data);
Text Three = new Text(composite, SWT.BORDER);
data = new FormData();
data.top = new FormAttachment(0, 0);
data.bottom = new FormAttachment(100, 0);
data.left = new FormAttachment(sash, 0);
data.right = new FormAttachment(100, 0);
Three.setLayoutData(data);
}
public void SashFormExample(Composite composite) {
SashForm sashForm;
final SashForm sashForm2;
composite.setLayout(new FillLayout());
sashForm = new SashForm(composite, SWT.HORIZONTAL);
Text text1 = new Text(sashForm, SWT.CENTER);
text1.setText("Text in pane #1");
Text text2 = new Text(sashForm, SWT.CENTER);
text2.setText("Text in pane #2");
sashForm2 = new SashForm(sashForm, SWT.VERTICAL);
final Label labelA = new Label(sashForm2, SWT.BORDER | SWT.CENTER);
labelA.setText("Label in pane A");
final Label labelB = new Label(sashForm2, SWT.BORDER |SWT.CENTER);
labelB.setText("Label in pane B");
text1.addControlListener(new ControlListener() {
public void controlMoved(ControlEvent e) {
}
public void controlResized(ControlEvent e) {
System.out.println("Resized");
}
});
sashForm.setWeights(new int[]{1, 2, 3});
labelA.addMouseListener(new MouseListener() {
public void mouseDoubleClick(MouseEvent e) {
if(sashForm2.getMaximizedControl() == labelA)
sashForm2.setMaximizedControl(null);
else
sashForm2.setMaximizedControl(labelA);
}
public void mouseDown(MouseEvent e) {
}
public void mouseUp(MouseEvent e) {
}
});
}
private void SashFormExample2(Composite parent) {
// Our layout will have a row of buttons, and
// then a SashForm below it.
parent.setLayout(new GridLayout(1, false));
// Create the row of buttons
Composite buttonBar = new Composite(parent, SWT.NONE);
buttonBar.setLayout(new RowLayout());
Button flip = new Button(buttonBar, SWT.PUSH);
flip.setText("Switch Orientation");
Button weights = new Button(buttonBar, SWT.PUSH);
weights.setText("Restore Weights");
// Create the SashForm
Composite sash = new Composite(parent, SWT.NONE);
sash.setLayout(new FillLayout());
sash.setLayoutData(new GridData(GridData.FILL_BOTH));
final SashForm sashForm = new SashForm(sash, SWT.HORIZONTAL);
// Change the width of the sashes
sashForm.SASH_WIDTH = 20;
// Change the color used to paint the sashes
sashForm.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GREEN));
// Create the buttons and their event handlers
final Button one = new Button(sashForm, SWT.PUSH);
one.setText("One");
one.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
maximizeHelper(one, sashForm);
}
});
final Button two = new Button(sashForm, SWT.PUSH);
two.setText("Two");
two.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
maximizeHelper(two, sashForm);
}
});
final Button three = new Button(sashForm, SWT.PUSH);
three.setText("Three");
three.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
maximizeHelper(three, sashForm);
}
});
// Set the relative weights for the buttons
sashForm.setWeights(new int[] { 1, 2, 3});
// Add the Switch Orientation functionality
flip.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
switch (sashForm.getOrientation()) {
case SWT.HORIZONTAL:
sashForm.setOrientation(SWT.VERTICAL);
break;
case SWT.VERTICAL:
sashForm.setOrientation(SWT.HORIZONTAL);
break;
}
}
});
// Add the Restore Weights functionality
weights.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
sashForm.setWeights(new int[] { 1, 2, 3});
}
});
}
/**
* Helper method for our maximize behavior. If the passed control is already
* maximized, restore it. Otherwise, maximize it
*
* @param control the control to maximize or restore
* @param sashForm the parent SashForm
*/
private void maximizeHelper(Control control, SashForm sashForm) {
// See if the control is already maximized
if (control == sashForm.getMaximizedControl()) {
// Already maximized; restore it
sashForm.setMaximizedControl(null);
} else {
// Not yet maximized, so maximize it
sashForm.setMaximizedControl(control);
}
}
public void StyleText(final Composite parent){
final StyledText styledText;
parent.setLayout(new GridLayout());
styledText = new StyledText(parent, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
styledText.setLayoutData(new GridData(GridData.FILL_BOTH));
styledText.addLineBackgroundListener(new LineBackgroundListener() {
public void lineGetBackground(LineBackgroundEvent event) {
if(styledText.getLineAtOffset(event.lineOffset) % 2 == 1)
event.lineBackground = parent.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
}
});
styledText.setText("Line 0\r\nLine 1\r\nLine 2\r\nLine 3\r\nLine 4\r\nLine 5\r\nLine 6");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -