📄 tablecomboexampler.java
字号:
package com.swtplus.gallery;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import com.swtplus.utility.Styler;
import com.swtplus.widgets.PCombo;
import com.swtplus.widgets.PGroup;
import com.swtplus.widgets.combo.ColorComboStrategy;
import com.swtplus.widgets.combo.NamedRGB;
import com.swtplus.widgets.combo.TableComboStrategy;
import com.swtplus.widgets.group.SimpleGroupStrategy;
public class TableComboExampler extends Composite implements IWidgetExampler {
private Button resize;
private Button readOnly;
private ColorComboStrategy foregroundComboStrat;
private PCombo foregroundCombo;
private ColorComboStrategy backgroundComboStrat;
private PCombo backgroundCombo;
protected Font font;
private Composite exampleArea;
private boolean firstCreate = true;
private PCombo pCombo;
private Color foreground;
private Color background;
private Label l;
private Spinner itemsToShow;
private Button flat;
private Button border;
public TableComboExampler(Composite c) {
super(c, SWT.NONE);
Styler colorStyler = new Styler();
colorStyler.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
Styler borderStyler = new Styler();
borderStyler.setBorderColor(Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
SelectionListener sListener = new SelectionListener(){
public void widgetSelected(SelectionEvent arg0) {
recreate();
}
public void widgetDefaultSelected(SelectionEvent arg0) {
}};
ModifyListener mListener = new ModifyListener() {
public void modifyText(ModifyEvent e) {
getDisplay().asyncExec(new Runnable(){
public void run() {
recreate();
}
});
}
};
this.setLayout(new FillLayout());
final Composite container = new Composite (this,SWT.NONE);
container.setBackground(c.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
container.setLayout(new GridLayout());
SimpleGroupStrategy sgs = new SimpleGroupStrategy(SWT.NONE);
PGroup sgStyles = new PGroup(container,sgs);
sgStyles.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
sgStyles.setText("Styles");
colorStyler.add(sgStyles);
colorStyler.add(sgStyles.getBody());
sgStyles.getBody().setLayout(new GridLayout());
border = new Button(sgStyles.getBody(),SWT.CHECK | SWT.FLAT);
border.setText("PCombo.BORDER");
colorStyler.add(border);
border.setSelection(true);
border.addSelectionListener(sListener);
resize = new Button(sgStyles.getBody(),SWT.CHECK | SWT.FLAT);
resize.setText("PCombo.RESIZE");
colorStyler.add(resize);
resize.addSelectionListener(sListener);
readOnly = new Button(sgStyles.getBody(),SWT.CHECK | SWT.FLAT);
readOnly.setText("PCombo.READ_ONLY");
colorStyler.add(readOnly);
readOnly.addSelectionListener(sListener);
flat = new Button(sgStyles.getBody(),SWT.CHECK | SWT.FLAT);
flat.setText("PCombo.FLAT");
colorStyler.add(flat);
flat.addSelectionListener(sListener);
sgs = new SimpleGroupStrategy(SWT.NONE);
PGroup sg = new PGroup(container,sgs);
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
sg.setLayoutData(gd);
sg.setText("Colors and Font");
c = sg.getBody();
GridLayout gl = new GridLayout();
gl.numColumns = 2;
c.setLayout(gl);
colorStyler.add(sg);
colorStyler.add(sg.getBody());
Label colorForegroundLabel = new Label(c,SWT.NONE);
colorForegroundLabel.setText("Foreground Color:");
colorStyler.add(colorForegroundLabel);
foregroundComboStrat = new ColorComboStrategy(ColorComboStrategy.SHOW_DEFAULT | ColorComboStrategy.SHOW_SWTPALETTE);
foregroundCombo = new PCombo(c,PCombo.READ_ONLY | PCombo.FLAT,foregroundComboStrat);
gd = new GridData(GridData.FILL_HORIZONTAL);
foregroundCombo.setLayoutData(gd);
borderStyler.add(foregroundCombo);
Label backLabel = new Label(c,SWT.NONE);
backLabel.setText("Background Color:");
colorStyler.add(backLabel);
backgroundComboStrat = new ColorComboStrategy(ColorComboStrategy.SHOW_DEFAULT | ColorComboStrategy.SHOW_SWTPALETTE);
backgroundCombo = new PCombo(c,PCombo.READ_ONLY | PCombo.FLAT,backgroundComboStrat);
gd = new GridData(GridData.FILL_HORIZONTAL);
backgroundCombo.setLayoutData(gd);
borderStyler.add(backgroundCombo);
Label l = new Label(c,SWT.NONE);
l.setText("Font:");
colorStyler.add(l);
Button fontButton = new Button(c,SWT.PUSH | SWT.FLAT);
fontButton.setText("Change Font...");
fontButton.addSelectionListener(new SelectionListener(){
public void widgetDefaultSelected(SelectionEvent arg0) {
}
public void widgetSelected(SelectionEvent arg0) {
FontDialog fd = new FontDialog(Display.getCurrent().getActiveShell());
FontData fds = fd.open();
if (fds != null){
if (font != null)
font.dispose();
font = new Font(Display.getCurrent(),fds);
recreate();
}
}});
SimpleGroupStrategy sgs2 = new SimpleGroupStrategy(SWT.NONE);
PGroup sgColors = new PGroup(container,sgs2);
sgColors.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
sgColors.setText("Other Options");
colorStyler.add(sgColors);
colorStyler.add(sgColors.getBody());
sgColors.getBody().setLayout(new GridLayout(2,false));
Label l4 = new Label(sgColors.getBody(),SWT.NONE);
l4.setText("Visible Item Count:");
colorStyler.add(l4);
itemsToShow = new Spinner(sgColors.getBody(),SWT.FLAT);
itemsToShow.setSelection(5);
itemsToShow.addModifyListener(mListener);
borderStyler.add(itemsToShow);
colorStyler.style();
borderStyler.style();
registerListeners(this);
}
public void setExampleArea(Composite area) {
exampleArea = area;
GridLayout gl = new GridLayout();
gl.marginWidth = 100;
area.setLayout(gl);
recreate();
firstCreate = false;
}
public void recreate(){
if (pCombo != null){
pCombo.dispose();
}
if (l != null){
l.dispose();
}
int style = 0;
TableComboStrategy strategy = new TableComboStrategy(style,SWT.FULL_SELECTION);
strategy.setVisibleItemCount(itemsToShow.getSelection());
if (foreground != null)
foreground.dispose();
if (background != null)
background.dispose();
style = SWT.NONE;
if (border.getSelection())
style = style | PCombo.BORDER;
if (readOnly.getSelection())
style = style | PCombo.READ_ONLY;
if (resize.getSelection())
style = style | PCombo.RESIZE;
if (flat.getSelection())
style = style | PCombo.FLAT;
pCombo = new PCombo(exampleArea,style,strategy);
if (firstCreate){
foregroundComboStrat.setDefaultRGB(pCombo.getForeground().getRGB());
foregroundCombo.setValue(new NamedRGB("Default",pCombo.getForeground().getRGB()));
backgroundComboStrat.setDefaultRGB(pCombo.getBackground().getRGB());
backgroundCombo.setValue(new NamedRGB("Default",pCombo.getBackground().getRGB()));
font = null;
} else {
foreground = new Color(Display.getCurrent(), ((NamedRGB)foregroundCombo.getValue()).getRGB());
pCombo.setForeground(foreground);
background = new Color(Display.getCurrent(), ((NamedRGB)backgroundCombo.getValue()).getRGB());
pCombo.setBackground(background);
if (font != null)
pCombo.setFont(font);
}
// GridData gd = new GridData(SWT.CENTER,SWT.TOP,true,false);
// gd.verticalIndent = 100;
GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gd.verticalIndent = 100;
//gd.horizontalAlignment = GridData.HORIZONTAL_ALIGN_CENTER;
//gd.widthHint = 200;
//gd.heightHint = 300;
pCombo.setLayoutData(gd);
l = new Label(exampleArea,SWT.NONE);
gd = new GridData(SWT.CENTER,SWT.TOP,true,false);
gd.heightHint = 70;
l.setLayoutData(gd);
Table table = strategy.getTable();
table.setHeaderVisible(true);
TableColumn tc = new TableColumn(table,SWT.NONE);
tc.setText("Column 1");
tc.setWidth(87);
tc = new TableColumn(table,SWT.NONE);
tc.setText("Column 2");
tc.setWidth(87);
TableItem ti = new TableItem(table,SWT.NONE);
ti.setText(0,"Example 1");
ti.setText(1,"data 1");
ti = new TableItem(table,SWT.NONE);
ti.setText(0,"Example 2");
ti.setText(1,"data 2");
ti = new TableItem(table,SWT.NONE);
ti.setText(0,"Example 3");
ti.setText(1,"data 3");
ti.setImage(GalleryImageRegistry.getImage(this.getClass(),"list1_small.png"));
ti = new TableItem(table,SWT.NONE);
ti.setText(0,"Example 4");
ti.setText(1,"data 4");
ti = new TableItem(table,SWT.NONE);
ti.setText(0,"Example 5");
ti.setText(1,"data 5");
ti = new TableItem(table,SWT.NONE);
ti.setText(0,"Example 6");
ti.setText(1,"data 6");
ti = new TableItem(table,SWT.NONE);
ti.setText(0,"Example 7");
ti.setText(1,"data 7");
exampleArea.layout();
}
public void registerListeners (Composite master){
Control [] children = master.getChildren();
for (int i = 0; i < children.length; i++) {
Control child = children[i];
if (child instanceof Composite){
if (child instanceof Combo){
Combo combo = (Combo) child;
combo.addSelectionListener(new SelectionListener(){
public void widgetSelected(SelectionEvent arg0) {
//recreate();
}
public void widgetDefaultSelected(SelectionEvent arg0) {}
});
} else if (child instanceof PCombo){
PCombo pCombo = (PCombo) child;
pCombo.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
recreate();
}
});
} else {
registerListeners ((Composite) child);
}
}else if (child instanceof Button){
Button button = (Button) child;
button.addSelectionListener(new SelectionListener(){
public void widgetSelected(SelectionEvent arg0) {
recreate();
}
public void widgetDefaultSelected(SelectionEvent arg0) {}
});
}
}
}
public void dispose() {
if (pCombo != null){
pCombo.dispose();
}
if (l != null){
l.dispose();
}
super.dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -