?? paletteshelfexampler.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.List;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import com.swtplus.utility.Styler;
import com.swtplus.widgets.PListItem;
import com.swtplus.widgets.PCombo;
import com.swtplus.widgets.PGroup;
import com.swtplus.widgets.PList;
import com.swtplus.widgets.PShelf;
import com.swtplus.widgets.PShelfItem;
import com.swtplus.widgets.combo.ColorComboStrategy;
import com.swtplus.widgets.combo.NamedRGB;
import com.swtplus.widgets.group.SimpleGroupStrategy;
import com.swtplus.widgets.list.ListBarListStrategy;
import com.swtplus.widgets.shelf.PaletteShelfStrategy;
public class PaletteShelfExampler extends Composite implements IWidgetExampler {
private ColorComboStrategy foregroundComboStrat;
private PCombo foregroundCombo;
private ColorComboStrategy backgroundComboStrat;
private PCombo backgroundCombo;
protected Font font;
private Composite exampleArea;
private boolean firstCreate = true;
private PShelf pShelf;
private Color foreground;
private Color background;
private Label l;
private Button border;
private Label l1;
private Label l2;
private ColorComboStrategy shadeComboStrat;
private PCombo shadeCombo;
private Color shade;
private Button center;
public PaletteShelfExampler(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);
center = new Button(sgStyles.getBody(),SWT.CHECK | SWT.FLAT);
center.setText("PaletteShelfStrategy.CENTER");
colorStyler.add(center);
center.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 shadeLabel = new Label(c,SWT.NONE);
shadeLabel.setText("Shading Color:");
colorStyler.add(shadeLabel);
shadeComboStrat = new ColorComboStrategy(ColorComboStrategy.SHOW_DEFAULT | ColorComboStrategy.SHOW_SWTPALETTE);
shadeCombo = new PCombo(c,PCombo.READ_ONLY | PCombo.FLAT,shadeComboStrat);
gd = new GridData(GridData.FILL_HORIZONTAL);
shadeCombo.setLayoutData(gd);
borderStyler.add(shadeCombo);
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));
colorStyler.style();
borderStyler.style();
registerListeners(this);
}
public void setExampleArea(Composite area) {
exampleArea = area;
GridLayout gl = new GridLayout(3,false);
area.setLayout(gl);
recreate();
firstCreate = false;
}
public void recreate(){
if (pShelf != null){
pShelf.dispose();
}
if (l != null){
l.dispose();
}
if (l2 != null){
l2.dispose();
}
if (l1 != null){
l1.dispose();
}
l1 = new Label(exampleArea,SWT.NONE);
GridData gd = new GridData();
gd.widthHint = 100;
l1.setLayoutData(gd);
int style = 0;
if (center.getSelection())
style = PaletteShelfStrategy.CENTER;
PaletteShelfStrategy strategy = new PaletteShelfStrategy(style);
if (foreground != null)
foreground.dispose();
if (background != null)
background.dispose();
if (shade != null)
shade.dispose();
style = SWT.NONE;
if (border.getSelection())
style = style | PShelf.BORDER;
pShelf = new PShelf(exampleArea,style,strategy);
l2 = new Label(exampleArea,SWT.NONE);
gd = new GridData();
gd.widthHint = 100;
l2.setLayoutData(gd);
if (firstCreate){
foregroundComboStrat.setDefaultRGB(pShelf.getForeground().getRGB());
foregroundCombo.setValue(new NamedRGB("Default",pShelf.getForeground().getRGB()));
backgroundComboStrat.setDefaultRGB(pShelf.getBackground().getRGB());
backgroundCombo.setValue(new NamedRGB("Default",pShelf.getBackground().getRGB()));
shadeComboStrat.setDefaultRGB(strategy.getShadeColor().getRGB());
shadeCombo.setValue(new NamedRGB("Default",strategy.getShadeColor().getRGB()));
font = null;
} else {
foreground = new Color(Display.getCurrent(), ((NamedRGB)foregroundCombo.getValue()).getRGB());
pShelf.setForeground(foreground);
background = new Color(Display.getCurrent(), ((NamedRGB)backgroundCombo.getValue()).getRGB());
pShelf.setBackground(background);
shade = new Color(Display.getCurrent(), ((NamedRGB)shadeCombo.getValue()).getRGB());
strategy.setShadeColor(shade);
if (font != null)
pShelf.setFont(font);
}
gd = new GridData(GridData.FILL_BOTH);
gd.verticalIndent = 100;
pShelf.setLayoutData(gd);
l = new Label(exampleArea,SWT.NONE);
gd = new GridData(SWT.CENTER,SWT.TOP,true,false);
gd.horizontalSpan =3;
gd.heightHint = 70;
l.setLayoutData(gd);
PShelfItem si = new PShelfItem(pShelf,SWT.NONE);
si.setText("Item 1");
si.setImage(GalleryImageRegistry.getImage(this.getClass(),"list4_small.png"));
si.getBody().setLayout(new GridLayout());
Label label = new Label(si.getBody(),SWT.WRAP);
label.setLayoutData(new GridData(GridData.FILL_BOTH));
label.setText("This area is a just a regular SWT Composite. You can access this Composite via ShelfItem.getBody() and add your contents.");
si = new PShelfItem(pShelf,SWT.NONE);
si.setText("Second Item");
si.setImage(GalleryImageRegistry.getImage(this.getClass(),"list5_small.png"));
GridLayout gl = new GridLayout();
gl.marginHeight = 0;
gl.marginWidth = 0;
si.getBody().setLayout(gl);
List list = new List(si.getBody(),SWT.WRAP);
list.setLayoutData(new GridData(GridData.FILL_BOTH));
list.setItems(new String[]{"List item 1","List Item 2","More sample data","Sample data...","Blah blah"});
si = new PShelfItem(pShelf,SWT.NONE);
si.setText("3rd Item");
si.setImage(GalleryImageRegistry.getImage(this.getClass(),"list2_small.png"));
si.getBody().setLayout(new GridLayout());
Text t = new Text(si.getBody(),SWT.BORDER | SWT.MULTI | SWT.WRAP);
t.setLayoutData(new GridData(GridData.FILL_BOTH));
t.setText("The quick brown fox jumps over the lazy dog. One Two Three");
si = new PShelfItem(pShelf,SWT.NONE);
si.setText("Fourth Item");
si.setImage(GalleryImageRegistry.getImage(this.getClass(),"list1_small.png"));
gl = new GridLayout();
gl.marginHeight = 0;
gl.marginWidth = 0;
si.getBody().setLayout(gl);
ListBarListStrategy listStrategy = new ListBarListStrategy(ListBarListStrategy.HOVER);
PList pList = new PList(si.getBody(),PList.SINGLE,listStrategy);
pList.setLayoutData(new GridData(GridData.FILL_BOTH));
PListItem li = new PListItem(pList,SWT.NONE);
li.setText("Example 1");
li.setImage(GalleryImageRegistry.getImage(this.getClass(),"list1.png"));
si = new PShelfItem(pShelf,SWT.NONE);
si.setText("Last Item");
si.setImage(GalleryImageRegistry.getImage(this.getClass(),"list3_small.png"));
gl = new GridLayout();
gl.marginHeight = 0;
gl.marginWidth = 0;
si.getBody().setLayout(gl);
final Tree tree = new Tree (si.getBody(), SWT.V_SCROLL);
tree.setLayoutData(new GridData(GridData.FILL_BOTH));
for (int i=0; i<4; i++) {
TreeItem iItem = new TreeItem (tree, 0);
iItem.setText ("TreeItem (0) -" + i);
for (int j=0; j<4; j++) {
TreeItem jItem = new TreeItem (iItem, 0);
jItem.setText ("TreeItem (1) -" + j);
for (int k=0; k<4; k++) {
TreeItem kItem = new TreeItem (jItem, 0);
kItem.setText ("TreeItem (2) -" + k);
for (int l=0; l<4; l++) {
TreeItem lItem = new TreeItem (kItem, 0);
lItem.setText ("TreeItem (3) -" + l);
}
}
}
}
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 (pShelf != null){
pShelf.dispose();
}
if (l != null){
l.dispose();
}
if (l2 != null){
l2.dispose();
}
if (l1 != null){
l1.dispose();
}
super.dispose();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -