?? colorcomboexampler.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 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.group.SimpleGroupStrategy;
public class ColorComboExampler 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 Button flat;
private Button showSWT;
private Button showDefault;
private Button border;
public ColorComboExampler(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) {
}};
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);
showSWT = new Button(sgStyles.getBody(),SWT.CHECK | SWT.FLAT);
showSWT.setText("ColorComboStrategy.SHOW_SWTPALETTE");
colorStyler.add(showSWT);
showSWT.addSelectionListener(sListener);
showDefault = new Button(sgStyles.getBody(),SWT.CHECK | SWT.FLAT);
showDefault.setText("ColorComboStrategy.SHOW_DEFAULT");
colorStyler.add(showDefault);
showDefault.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();
}
}});
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;
if (showDefault.getSelection())
style = style | ColorComboStrategy.SHOW_DEFAULT;
if (showSWT.getSelection())
style = style | ColorComboStrategy.SHOW_SWTPALETTE;
ColorComboStrategy strategy = new ColorComboStrategy(style);
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);
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 + -