⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplelistexampler.java

📁 开源的关于SWT开发的图形应用库
💻 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.PListItem;
import com.swtplus.widgets.PCombo;
import com.swtplus.widgets.PGroup;
import com.swtplus.widgets.PList;
import com.swtplus.widgets.combo.ColorComboStrategy;
import com.swtplus.widgets.combo.NamedRGB;
import com.swtplus.widgets.group.SimpleGroupStrategy;
import com.swtplus.widgets.list.SimpleListStrategy;
import com.swtplus.widgets.list.TitleAndDescListStrategy;

public class SimpleListExampler extends Composite implements IWidgetExampler {

	private Button single;
	private ColorComboStrategy foregroundComboStrat;
	private PCombo foregroundCombo;
	private ColorComboStrategy backgroundComboStrat;
	private PCombo backgroundCombo;
	protected Font font;
	private Composite exampleArea;
	private boolean firstCreate = true;
	
	private PList pList;
	private Color foreground;
	private Color background;
	private Label l;
	private PCombo selectionCombo;
	private ColorComboStrategy selectionComboStrat;
	private ColorComboStrategy selTextComboStrat;
	private PCombo selTextCombo;
	private Color selection;
	private Color selectionText;
	private Label l1;
	private Label l2;
	private Button border;

	public SimpleListExampler(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("PList.BORDER");
		colorStyler.add(border);
		border.setSelection(true);
		border.addSelectionListener(sListener);
		
		single = new Button(sgStyles.getBody(),SWT.CHECK | SWT.FLAT);
		single.setText("PList.SINGLE");
		colorStyler.add(single);
		single.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 selectLabel = new Label(c,SWT.NONE);
		selectLabel.setText("Selection Color:");
		colorStyler.add(selectLabel);
		
		selectionComboStrat = new ColorComboStrategy(ColorComboStrategy.SHOW_DEFAULT | ColorComboStrategy.SHOW_SWTPALETTE);
		selectionCombo = new PCombo(c,PCombo.READ_ONLY | PCombo.FLAT,selectionComboStrat);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		selectionCombo.setLayoutData(gd);
		borderStyler.add(selectionCombo);

		Label selTextLabel = new Label(c,SWT.NONE);
		selTextLabel.setText("Selection Text Color:");
		colorStyler.add(selTextLabel);
		
		selTextComboStrat = new ColorComboStrategy(ColorComboStrategy.SHOW_DEFAULT | ColorComboStrategy.SHOW_SWTPALETTE);
		selTextCombo = new PCombo(c,PCombo.READ_ONLY | PCombo.FLAT,selTextComboStrat);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		selTextCombo.setLayoutData(gd);
		borderStyler.add(selTextCombo);
		
		
		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;
		
		area.setLayout(new GridLayout(3,false));
		recreate();
		
		firstCreate = false;		
	}
	
	public void recreate(){
		
		if (pList != null){
			pList.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;
		
		SimpleListStrategy strategy = new SimpleListStrategy(style);
		
		
		if (foreground != null)
			foreground.dispose();
		if (background != null)
			background.dispose();
		
		style = SWT.NONE;
		if (border.getSelection())
			style = style | PList.BORDER;
		if (single.getSelection())
			style = style | PList.SINGLE;
		
		pList = new PList(exampleArea,style,strategy);
		
		l2 = new Label(exampleArea,SWT.NONE);
		gd = new GridData();
		gd.widthHint = 100;
		l2.setLayoutData(gd);

		if (firstCreate){
			foregroundComboStrat.setDefaultRGB(pList.getForeground().getRGB());
			foregroundCombo.setValue(new NamedRGB("Default",pList.getForeground().getRGB()));
			backgroundComboStrat.setDefaultRGB(pList.getBackground().getRGB());
			backgroundCombo.setValue(new NamedRGB("Default",pList.getBackground().getRGB()));

			selectionComboStrat.setDefaultRGB(pList.getSelectionColor().getRGB());
			selectionCombo.setValue(new NamedRGB("Default",pList.getSelectionColor().getRGB()));
			selTextComboStrat.setDefaultRGB(pList.getSelectionTextColor().getRGB());
			selTextCombo.setValue(new NamedRGB("Default",pList.getSelectionTextColor().getRGB()));

			font = null;
		} else {
			foreground = new Color(Display.getCurrent(), ((NamedRGB)foregroundCombo.getValue()).getRGB());
			pList.setForeground(foreground);
			
			background = new Color(Display.getCurrent(), ((NamedRGB)backgroundCombo.getValue()).getRGB());
			pList.setBackground(background);
			
			selection = new Color(Display.getCurrent(), ((NamedRGB)selectionCombo.getValue()).getRGB());
			pList.setSelectionColor(selection);
			
			selectionText = new Color(Display.getCurrent(), ((NamedRGB)selTextCombo.getValue()).getRGB());
			pList.setSelectionTextColor(selectionText);
			
			if (font != null)
				pList.setFont(font);			
		}
		
		
		gd = new GridData(GridData.FILL_BOTH);
		gd.verticalIndent = 100;
		pList.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);
		
		PListItem li = new PListItem(pList,SWT.NONE);
		li.setText("Example 1");
		li.setImage(GalleryImageRegistry.getImage(this.getClass(),"list1_small.png"));
		li.setData(TitleAndDescListStrategy.DESCRIPTION,"This is a description for the first example item");
		
		li = new PListItem(pList,SWT.NONE);
		li.setText("Example 2");
		li.setImage(GalleryImageRegistry.getImage(this.getClass(),"list2_small.png"));
		li.setData(TitleAndDescListStrategy.DESCRIPTION,"This is another desc for the second item in the list");
		
		li = new PListItem(pList,SWT.NONE);
		li.setText("Example 3");
		li.setImage(GalleryImageRegistry.getImage(this.getClass(),"list3_small.png"));
		li.setData(TitleAndDescListStrategy.DESCRIPTION,"3rd example description with a large string that will demonstrate the wrapping features of this list strategy");
		
		li = new PListItem(pList,SWT.NONE);
		li.setText("Example 4");
		li.setImage(GalleryImageRegistry.getImage(this.getClass(),"list4_small.png"));
		li.setData(TitleAndDescListStrategy.DESCRIPTION,"Description 4");

		li = new PListItem(pList,SWT.NONE);
		li.setText("Example 5");
		li.setImage(GalleryImageRegistry.getImage(this.getClass(),"list5_small.png"));
		li.setData(TitleAndDescListStrategy.DESCRIPTION,"This is the final description.");
		
		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 (pList != null){
			pList.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 + -