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

📄 colorcombostrategy.java

📁 利用它可以做出非常漂亮的swt界面,包含的组件有PShelf Plist
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.swtplus.widgets.combo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import com.swtplus.internal.Assert;
import com.swtplus.widgets.PCombo;
import com.swtplus.widgets.combo.internal.MainColorPalette;
import com.swtplus.widgets.combo.internal.SWTColorPalette;

/**
 * ColorComboStrategy creates a color selector widget in a PCombo's dropdown.
 * The value object returned to the PCombo when using a ColorComboStrategy is
 * a NamedRGB.
 */
public class ColorComboStrategy implements IComboStrategy {

	/**
	 * Shows the default value button on the popup.
	 */
	public static final int SHOW_DEFAULT = 1 << 1;
	/**
	 * Shows the SWT palette of system colors in the popup.
	 */
	public static final int SHOW_SWTPALETTE = 1 << 2;
	
	private boolean showDefault = false;
	private ToolItem default1;
	private ToolItem default2;
	
	private Display display;
	private MainColorPalette palette = new MainColorPalette();
	private SWTColorPalette swtPalette = new SWTColorPalette();
	private Composite composite;
	private RGB defaultRGB = null;
	private boolean showSWTPalette = false;
	private IComboUpdater updater;
	
	private ArrayList toolImages = new ArrayList();
	private HashMap valueImages = new HashMap();
	
	/**
	 * Creates a ColorComboStrategy with the given style.
	 * 
	 * @param style style bits
	 */
	public ColorComboStrategy(int style) {
		if ((style & SHOW_DEFAULT) == SHOW_DEFAULT)
            showDefault = true;
		if ((style & SHOW_SWTPALETTE) == SHOW_SWTPALETTE)
			showSWTPalette = true;
	}
	
	/* (non-Javadoc)
	 * @see com.swtplus.widgets.combo.IComboStrategy#getSize(int)
	 */
	public Point getSize(int comboWidth) {
		return composite.computeSize(SWT.DEFAULT,SWT.DEFAULT);
	}

	/* (non-Javadoc)
	 * @see com.swtplus.widgets.combo.IComboStrategy#createContents(org.eclipse.swt.widgets.Composite, com.swtplus.widgets.combo.IComboUpdater)
	 */
	public Control createContents(Composite parent, IComboUpdater updater) {
		display = parent.getDisplay();
		this.updater = updater;
		composite = new Composite(parent,SWT.NONE);
				
		CTabFolder folder = null;
		CTabItem mainTab;
		CTabItem swtTab = null;
		Composite mainComposite;
		if (showSWTPalette){
			composite.setLayout(new FillLayout());
			
			folder = new CTabFolder(composite,SWT.TOP);
			mainTab = new CTabItem(folder,SWT.NONE);
			mainTab.setText("Primary Palette");
			swtTab = new CTabItem(folder,SWT.NONE);
			swtTab.setText("SWT Palette");
			mainComposite = new Composite(folder,SWT.NONE);
			mainTab.setControl(mainComposite);
			
			folder.setSelection(0);
			
		} else {
			mainComposite = composite;
		}
		mainComposite.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
		
		GridLayout gl = new GridLayout();
		gl.marginWidth = 2;
		gl.marginHeight = 2;
		gl.verticalSpacing = 0;
		mainComposite.setLayout(gl);
		
		SelectionListener listener = new SelectionListener(){
			public void widgetSelected(SelectionEvent e) {
				selectItem((ToolItem) e.widget);
			}
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		};

		ToolBar tb = new ToolBar(mainComposite,SWT.FLAT);
		tb.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
		for (int i = 0;i < 8;i++){
			ToolItem ti = new ToolItem(tb,SWT.PUSH);
			ti.setImage(createColorImage(palette.get(i).getRGB()));
			ti.setToolTipText(palette.get(i).getName());		
			ti.addSelectionListener(listener);
			ti.setData("rgb",palette.get(i).getRGB());
		}
		tb = new ToolBar(mainComposite,SWT.FLAT);
		tb.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
		for (int i = 8;i < 16;i++){
			ToolItem ti = new ToolItem(tb,SWT.PUSH);
			ti.setImage(createColorImage(palette.get(i).getRGB()));
			ti.setToolTipText(palette.get(i).getName());	
			ti.addSelectionListener(listener);
			ti.setData("rgb",palette.get(i).getRGB());
		}
		tb = new ToolBar(mainComposite,SWT.FLAT);
		tb.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
		for (int i = 16;i < 24;i++){
			ToolItem ti = new ToolItem(tb,SWT.PUSH);
			ti.setImage(createColorImage(palette.get(i).getRGB()));
			ti.setToolTipText(palette.get(i).getName());
			ti.addSelectionListener(listener);
			ti.setData("rgb",palette.get(i).getRGB());
		}
		tb = new ToolBar(mainComposite,SWT.FLAT);
		tb.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
		for (int i = 24;i < 32;i++){
			ToolItem ti = new ToolItem(tb,SWT.PUSH);
			ti.setImage(createColorImage(palette.get(i).getRGB()));
			ti.setToolTipText(palette.get(i).getName());
			ti.addSelectionListener(listener);
			ti.setData("rgb",palette.get(i).getRGB());
		}
		tb = new ToolBar(mainComposite,SWT.FLAT);
		tb.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
		for (int i = 32;i < 40;i++){
			ToolItem ti = new ToolItem(tb,SWT.PUSH);
			ti.setImage(createColorImage(palette.get(i).getRGB()));
			ti.setToolTipText(palette.get(i).getName());
			ti.addSelectionListener(listener);
			ti.setData("rgb",palette.get(i).getRGB());
		}

		tb = new ToolBar(mainComposite,SWT.FLAT | SWT.RIGHT);
		tb.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
		
		if (showDefault){
			ToolItem ti = new ToolItem(tb,SWT.PUSH);
			ti.setText("Default");
			ti.setToolTipText("Default");
			if (defaultRGB != null)
				ti.setImage(createBigColorImage(defaultRGB));
			ti.setData("rgb",defaultRGB);
			ti.addSelectionListener(listener);
			default1 = ti;
		}
		
		ToolItem ti2 = new ToolItem(tb,SWT.PUSH);
		ti2.setText("More Colors...");
		ti2.addSelectionListener(new SelectionListener(){
			public void widgetSelected(SelectionEvent e) {
				ColorDialog cd = new ColorDialog(display.getActiveShell());
				RGB rgb = cd.open();
				if (rgb != null)
					selectItem(rgb,"Custom");
			}
			public void widgetDefaultSelected(SelectionEvent e) {
			}}
		);
		
		if (showSWTPalette){
			Composite swtComposite = new Composite(folder,SWT.NONE);
			swtTab.setControl(swtComposite);
			
			swtComposite.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
			
			gl = new GridLayout();
			gl.marginWidth = 2;
			gl.marginHeight = 2;
			gl.verticalSpacing = 0;
			swtComposite.setLayout(gl);

			tb = new ToolBar(swtComposite,SWT.FLAT);
			tb.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
			for (int i = 0;i < 8;i++){
				ToolItem ti = new ToolItem(tb,SWT.PUSH);
				ti.setImage(createColorImage(swtPalette.get(i).getRGB()));
				ti.setToolTipText(swtPalette.get(i).getName());	
				ti.addSelectionListener(listener);
				ti.setData("rgb",swtPalette.get(i).getRGB());
			}
			tb = new ToolBar(swtComposite,SWT.FLAT);
			tb.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
			for (int i = 8;i < 16;i++){
				ToolItem ti = new ToolItem(tb,SWT.PUSH);
				ti.setImage(createColorImage(swtPalette.get(i).getRGB()));
				ti.setToolTipText(swtPalette.get(i).getName());
				ti.addSelectionListener(listener);
				ti.setData("rgb",swtPalette.get(i).getRGB());
			}
			tb = new ToolBar(swtComposite,SWT.FLAT);
			tb.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
			for (int i = 16;i < 23;i++){
				ToolItem ti = new ToolItem(tb,SWT.PUSH);
				ti.setImage(createColorImage(swtPalette.get(i).getRGB()));
				ti.setToolTipText(swtPalette.get(i).getName());
				ti.addSelectionListener(listener);
				ti.setData("rgb",swtPalette.get(i).getRGB());
			}
			tb = new ToolBar(swtComposite,SWT.FLAT);
			tb.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
			for (int i = 23;i < 30;i++){
				ToolItem ti = new ToolItem(tb,SWT.PUSH);
				ti.setImage(createColorImage(swtPalette.get(i).getRGB()));
				ti.setToolTipText(swtPalette.get(i).getName());
				ti.addSelectionListener(listener);
				ti.setData("rgb",swtPalette.get(i).getRGB());
			}
			tb = new ToolBar(swtComposite,SWT.FLAT);
			tb.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
			for (int i = 30;i < 35;i++){
				ToolItem ti = new ToolItem(tb,SWT.PUSH);
				ti.setImage(createColorImage(swtPalette.get(i).getRGB()));
				ti.setToolTipText(swtPalette.get(i).getName());
				ti.addSelectionListener(listener);
				ti.setData("rgb",swtPalette.get(i).getRGB());
			}

⌨️ 快捷键说明

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