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

📄 gallery.java

📁 开源的关于SWT开发的图形应用库
💻 JAVA
字号:
package com.swtplus.gallery;

import java.lang.reflect.Constructor;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import com.swtplus.utility.Styler;
import com.swtplus.widgets.PGroup;
import com.swtplus.widgets.PLink;
import com.swtplus.widgets.group.RectangleGroupStrategy;

public class Gallery {

    protected Shell shell;
    
    ScrolledComposite sc;
    
    Composite exampleComposite;
    
    TreeItem currentItem;
    
    static Styler borderStyler = new Styler();
	
	public Gallery(Composite parent){
		createContents(parent);
	}
	
    public static void main(String[] args) {
        final Display display = new Display();
        borderStyler.setBorderColor(display.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
        Shell shell = new Shell(display);
		shell.setSize(900, 605);
        shell.setText("SWTPlus Examples");
		shell.setLayout(new FillLayout());
		new Gallery(shell);
        shell.layout();
        
        new Splash(shell);
        
        Monitor primary = shell.getMonitor();
	    Rectangle bounds = primary.getBounds ();
	    Rectangle rect = shell.getBounds ();
	    int x = bounds.x + (bounds.width - rect.width) / 2;
	    int y = bounds.y + (bounds.height - rect.height) / 2;
	    if (x < 0)
	    	x = 0;
	    if (y < 0)
	    	y = 0;
	    shell.setLocation (x, y);
	        
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        GalleryImageRegistry.dispose();
    }

    protected void createContents(Composite parent) {
    	Composite c = new Composite(parent,SWT.NONE);
    	GridLayout gl = new GridLayout();
    	gl.marginLeft = 5;
    	gl.marginBottom = 5;
    	gl.marginRight = 5;
    	c.setLayout(gl);
    	
    	SashForm sashForm = new SashForm(c,SWT.HORIZONTAL | SWT.SMOOTH);
    	sashForm.setLayoutData(new GridData(GridData.FILL_BOTH));
    	sashForm.setLayout(new FillLayout());
    	
    	Composite left = new Composite(sashForm,SWT.NONE);
    	gl = new GridLayout();
    	gl.marginHeight = 0;
    	gl.marginWidth = 0;
    	left.setLayout(gl);
    	RectangleGroupStrategy rectStrat = new RectangleGroupStrategy(RectangleGroupStrategy.PAINTIMAGEOUTOFBOUNDS | RectangleGroupStrategy.ROUNDED);
    	PGroup treeGroup = new PGroup(left,rectStrat);
    	treeGroup.setText("Widgets");
    	treeGroup.setImage(GalleryImageRegistry.getImage(this.getClass(),"widgets.png"));
    	treeGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
    	
    	treeGroup.getBody().setLayout(new FillLayout());
    	Tree tree = new Tree(treeGroup.getBody(),SWT.NONE);
    	
    	populateTree(tree);

    	
    	
    	rectStrat = new RectangleGroupStrategy(RectangleGroupStrategy.PAINTIMAGEOUTOFBOUNDS | RectangleGroupStrategy.ROUNDED);
    	PGroup linkGroup = new PGroup(left,rectStrat);
    	linkGroup.setText("Links");
    	linkGroup.setImage(GalleryImageRegistry.getImage(this.getClass(),"links.png"));
    	linkGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    	
    	gl = new GridLayout();
    	gl.marginLeft = 10;
    	gl.marginHeight = 10;
    	linkGroup.getBody().setLayout(gl);
    	
    	createLinks(linkGroup.getBody());
    	

    	exampleComposite = new Composite(sashForm,SWT.NONE);
    	
    	rectStrat = new RectangleGroupStrategy(RectangleGroupStrategy.PAINTIMAGEOUTOFBOUNDS | RectangleGroupStrategy.ROUNDED);
    	PGroup optionsGroup = new PGroup(sashForm,rectStrat);
    	optionsGroup.setText("Options");
    	optionsGroup.setImage(GalleryImageRegistry.getImage(this.getClass(),"options.png"));
    	
		optionsGroup.getBody().setLayout(new FillLayout());
		
		sc = new ScrolledComposite (optionsGroup.getBody(), SWT.V_SCROLL);
		sc.getVerticalBar().setIncrement(10);
		sc.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_LIST_BACKGROUND));

		sc.setContent(new Composite(sc,SWT.NONE));
		sc.getContent().setBackground(optionsGroup.getBody().getBackground());
		
		sc.addControlListener(new ControlListener(){
			public void controlMoved(ControlEvent arg0) {
			}
			public void controlResized(ControlEvent arg0) {
				sc.getContent().setSize(sc.getContent().computeSize(sc.getClientArea().width,SWT.DEFAULT));
			}}
		);
    	
    	sashForm.setWeights(new int[]{21,45,34});
    	

	}
	
    private void populateTree(final Tree tree){
    	
    	TreeItem groupItem = new TreeItem(tree,SWT.NONE);
    	groupItem.setText("PGroup");
    	groupItem.setData("class",RootExampler.class);
    	
    	
    	TreeItem rectItem = new TreeItem(groupItem,SWT.NONE);
    	rectItem.setText("RectangleGroupStrategy");
    	rectItem.setData("class",RectangleGroupExampler.class);
    	
    	TreeItem formItem = new TreeItem(groupItem,SWT.NONE);
    	formItem.setText("FormGroupStrategy");
    	formItem.setData("class",FormGroupExampler.class);
    	
    	TreeItem simpleItem = new TreeItem(groupItem,SWT.NONE);
    	simpleItem.setText("SimpleGroupStrategy");
    	simpleItem.setData("class",SimpleGroupExampler.class);
    	
    	TreeItem linkItem = new TreeItem(tree,SWT.NONE);
    	linkItem.setText("PLink");
    	linkItem.setData("class",LinkExampler.class);
    	
    	TreeItem comboItem = new TreeItem(tree,SWT.NONE);
    	comboItem.setText("PCombo");
    	comboItem.setData("class",RootExampler.class);
    	
    	TreeItem listComboItem = new TreeItem(comboItem,SWT.NONE);
    	listComboItem.setText("ListComboStrategy");
    	listComboItem.setData("class",ListComboExampler.class);
    	
    	TreeItem treeComboItem = new TreeItem(comboItem,SWT.NONE);
    	treeComboItem.setText("TreeComboStrategy");
    	treeComboItem.setData("class",TreeComboExampler.class);
    	
    	TreeItem tableComboItem = new TreeItem(comboItem,SWT.NONE);
    	tableComboItem.setText("TableComboStrategy");
    	tableComboItem.setData("class",TableComboExampler.class);
    	
    	TreeItem colorComboItem = new TreeItem(comboItem,SWT.NONE);
    	colorComboItem.setText("ColorComboStrategy");
    	colorComboItem.setData("class",ColorComboExampler.class);
    	
    	TreeItem listItem = new TreeItem(tree,SWT.NONE);
    	listItem.setText("PList");
    	listItem.setData("class",RootExampler.class);

    	TreeItem simpleListItem = new TreeItem(listItem,SWT.NONE);
    	simpleListItem.setText("SimpleListStrategy");
    	simpleListItem.setData("class",SimpleListExampler.class);
    	
    	TreeItem listBarItem = new TreeItem(listItem,SWT.NONE);
    	listBarItem.setText("ListBarListStrategy");
    	listBarItem.setData("class",ListBarExampler.class);
    	
    	TreeItem descListItem = new TreeItem(listItem,SWT.NONE);
    	descListItem.setText("TitleAndDescListStrategy");
    	descListItem.setData("class",TitleAndDescExampler.class);

    	
    	TreeItem shelfItem = new TreeItem(tree,SWT.NONE);
    	shelfItem.setText("PShelf");
    	shelfItem.setData("class",RootExampler.class);    	
    	
    	TreeItem gefItem = new TreeItem(shelfItem,SWT.NONE);
    	gefItem.setText("PaletteShelfStrategy");
    	gefItem.setData("class",PaletteShelfExampler.class);

    	TreeItem redmondItem = new TreeItem(shelfItem,SWT.NONE);
    	redmondItem.setText("RedmondShelfStrategy");
    	redmondItem.setData("class",RedmondShelfExampler.class);
    	
    	TreeItem autoCompleteItem = new TreeItem(tree,SWT.NONE);
    	autoCompleteItem.setText("AutoComplete");
    	autoCompleteItem.setData("class",AutoCompleteExampler.class);
    	   	
    	
    	groupItem.setExpanded(true);
    	comboItem.setExpanded(true);
    	listItem.setExpanded(true);
    	shelfItem.setExpanded(true);
    	
    	tree.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			public void widgetSelected(SelectionEvent e) {
				if (tree.getSelectionCount() > 0){
					final TreeItem[] items = tree.getSelection();
					
					if (currentItem == items[0])
						return;
					
					BusyIndicator.showWhile(Display.getCurrent(),new Runnable() {
						public void run() {
							Object cls = items[0].getData("class");
							if (cls != null){
								Class clz = (Class) cls;
								Constructor c = null;
								try {
									c = clz.getConstructor(new Class[]{Composite.class});
								} catch (Exception e1) {
									e1.printStackTrace();
								}
								Object o = null;
								try {
									o = c.newInstance(new Object[]{sc});
								} catch (Exception e1) {
									e1.printStackTrace();
								}
								sc.getContent().dispose();
								
								sc.setContent((Control) o);
								
								sc.getContent().setSize(sc.getContent().computeSize(sc.getClientArea().width,SWT.DEFAULT));
								
								IWidgetExampler ex = (IWidgetExampler) o;
								ex.setExampleArea(exampleComposite);
								
								currentItem = items[0];
							}
						}					
					});					
				}
			}		
		});
    	
    }
	
    private void createLinks(Composite c){
    	PLink homeLink = new PLink(c,PLink.UNDERLINE_ACTIVE);
    	//homeLink.setShowFocus(false);
    	homeLink.setBackground(c.getBackground());
    	homeLink.setText("SWTPlus Homepage");
    	homeLink.setImage(GalleryImageRegistry.getImage(this.getClass(),"home.gif"));
    	homeLink.setToolTipText("http://www.swtplus.com");
    	homeLink.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			public void widgetSelected(SelectionEvent e) {
				Program.launch("http://www.swtplus.com");
			}
		});
    	
    	
//    	PLink buyLink = new PLink(c,PLink.UNDERLINE_ACTIVE);
//    	buyLink.setBackground(c.getBackground());
//    	buyLink.setText("Purchase SWTPlus");
//    	buyLink.setImage(GalleryImageRegistry.getImage(this.getClass(),"buy.gif"));
//    	buyLink.addSelectionListener(new SelectionListener() {
//			public void widgetDefaultSelected(SelectionEvent e) {
//			}
//			public void widgetSelected(SelectionEvent e) {
//				Program.launch("http://www.swtplus.com");
//			}
//		});
    	
    	PLink blogLink = new PLink(c,PLink.UNDERLINE_ACTIVE);
    	blogLink.setBackground(c.getBackground());
    	blogLink.setText("Blog");
    	blogLink.setImage(GalleryImageRegistry.getImage(this.getClass(),"blog.gif"));
    	blogLink.setToolTipText("http://www.swtplus.com/wordpress/");
    	blogLink.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			public void widgetSelected(SelectionEvent e) {
				Program.launch("http://www.swtplus.com/wordpress/");
			}
		});
    	
    	PLink forumLink = new PLink(c,PLink.UNDERLINE_ACTIVE);
    	forumLink.setBackground(c.getBackground());
    	forumLink.setText("Forums");
    	forumLink.setImage(GalleryImageRegistry.getImage(this.getClass(),"forums.gif"));
    	forumLink.setToolTipText("http://www.swtplus.com/forums/");
    	forumLink.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			public void widgetSelected(SelectionEvent e) {
				Program.launch("http://www.swtplus.com/forums/");
			}
		});
    	
    	PLink docLink = new PLink(c,PLink.UNDERLINE_ACTIVE);
    	docLink.setBackground(c.getBackground());
    	docLink.setText("Javadoc");
    	docLink.setImage(GalleryImageRegistry.getImage(this.getClass(),"javadoc.gif"));
    	docLink.setToolTipText("http://www.swtplus.com/javadoc/");
    	docLink.addSelectionListener(new SelectionListener() {
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			public void widgetSelected(SelectionEvent e) {
				Program.launch("http://www.swtplus.com/javadoc/");
			}
		});
    }
    

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -