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

📄 linkexampler.java

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

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
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.graphics.Image;
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.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Text;

import com.swtplus.utility.Styler;
import com.swtplus.widgets.PCombo;
import com.swtplus.widgets.PGroup;
import com.swtplus.widgets.PLink;
import com.swtplus.widgets.combo.ColorComboStrategy;
import com.swtplus.widgets.combo.ListComboStrategy;
import com.swtplus.widgets.combo.NamedRGB;
import com.swtplus.widgets.group.SimpleGroupStrategy;

public class LinkExampler extends Composite implements IWidgetExampler {
	
	Composite exampleArea;
	PLink link;
	private Button underlineActive;
	private Button underlineAlways;
	private Button underlineNever;
	private Button wrap;
	private Text t;
	private PCombo ccombo;
	private Button focus;
	private Button focusActive;
	
	private Font font;
	
	private Image browsedImage;
	private ColorComboStrategy foregroundComboStrat;
	private PCombo foregroundCombo;
	private ColorComboStrategy backgroundComboStrat;
	private PCombo backgroundCombo;
	private ColorComboStrategy activeComboStrat;
	private PCombo activeCombo;
	private boolean firstCreate = true;

	public LinkExampler(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());
		
		underlineActive = new Button(sgStyles.getBody(),SWT.RADIO | SWT.FLAT);
		underlineActive.setText("PLink.UNDERLINE_ACTIVE");
		underlineActive.setSelection(true);
		colorStyler.add(underlineActive);
		underlineActive.addSelectionListener(sListener);

		underlineAlways = new Button(sgStyles.getBody(),SWT.RADIO | SWT.FLAT);
		underlineAlways.setText("PLink.UNDERLINE_ALWAYS");
		colorStyler.add(underlineAlways);
		underlineAlways.addSelectionListener(sListener);

		underlineNever = new Button(sgStyles.getBody(),SWT.RADIO | SWT.FLAT);
		underlineNever.setText("PLink.UNDERLINE_NEVER");
		colorStyler.add(underlineNever);
		underlineNever.addSelectionListener(sListener);
		
		wrap = new Button(sgStyles.getBody(),SWT.CHECK | SWT.FLAT);
		wrap.setText("PLink.WRAP (adds width hint to example's layout data)");
		colorStyler.add(wrap);
		wrap.addSelectionListener(sListener);
		
		
		SimpleGroupStrategy sgs2 = new SimpleGroupStrategy(SWT.NONE);
		PGroup sgColors = new PGroup(container,sgs2);
		sgColors.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		sgColors.setText("Colors");
		colorStyler.add(sgColors);
		colorStyler.add(sgColors.getBody());
		
		sgColors.getBody().setLayout(new GridLayout(2,false));
		
		c = sgColors.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);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		foregroundCombo.setLayoutData(gd);
		borderStyler.add(foregroundCombo);
		foregroundCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				recreate();
			}		
		});

		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);
		backgroundCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				recreate();
			}		
		});

		
		Label activeLabel = new Label(c,SWT.NONE);
		activeLabel.setText("Active Color:");
		colorStyler.add(activeLabel);
		
		activeComboStrat = new ColorComboStrategy(ColorComboStrategy.SHOW_DEFAULT | ColorComboStrategy.SHOW_SWTPALETTE);
		activeCombo = new PCombo(c,PCombo.READ_ONLY | PCombo.FLAT,activeComboStrat);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		activeCombo.setLayoutData(gd);
		borderStyler.add(activeCombo);
		activeCombo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				recreate();
			}		
		});
		
		
		SimpleGroupStrategy sgs3 = new SimpleGroupStrategy(SWT.NONE);
		PGroup sgImageText = new PGroup(container,sgs3);
		sgImageText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		sgImageText.setText("Images and Text");
		colorStyler.add(sgImageText);
		colorStyler.add(sgImageText.getBody());	
		sgImageText.getBody().setLayout(new GridLayout(3,false));
		
		Label l = new Label(sgImageText.getBody(),SWT.NONE);
		l.setText("Text:");
		colorStyler.add(l);
		
		t = new Text(sgImageText.getBody(),SWT.NO_FOCUS);
		t.setText("Example of PLink");
		borderStyler.add(t);
		t.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		
		Button apply = new Button(sgImageText.getBody(),SWT.PUSH | SWT.FLAT);
		apply.setText("Apply");
		apply.addSelectionListener(sListener);
		
		l = new Label(sgImageText.getBody(),SWT.NONE);
		l.setText("Font:");
		colorStyler.add(l);
		
		Button fontButton = new Button(sgImageText.getBody(),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();
				}
			}});
		
		new Label(sgImageText.getBody(),SWT.NONE);
		
		l = new Label(sgImageText.getBody(),SWT.NONE);
		l.setText("Images:");
		colorStyler.add(l);
		
		ListComboStrategy imageStrategy = new ListComboStrategy(SWT.NONE);
		ccombo = new PCombo(sgImageText.getBody(),PCombo.READ_ONLY | PCombo.FLAT,imageStrategy);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		ccombo.setLayoutData(gd);
		borderStyler.add(ccombo);
		
		imageStrategy.getList().add("(None)");
		imageStrategy.getList().add("Flashlight Off/Flashlight On When Active");
		imageStrategy.getList().add("Browse...");
		
		ccombo.setValue("Flashlight Off/Flashlight On When Active");
		
		ccombo.addSelectionListener(new SelectionListener(){
			public void widgetSelected(SelectionEvent arg0) {
				if (!ccombo.getValue().equals("Browse..."))
					return;
				
				if (browsedImage != null){
					browsedImage.dispose();
					browsedImage = null;
				}
				
				FileDialog fd = new FileDialog(Display.getCurrent().getActiveShell(),SWT.OPEN);
				String file = fd.open();
				
				if (file != null){
					
					try {
						browsedImage = new Image(Display.getCurrent(),file);
					} catch (SWTException e) {
						MessageBox mb = new MessageBox(Display.getCurrent().getActiveShell());
						mb.setText("Error");
						mb.setMessage(e.toString());
						mb.open();
					}
				}
			}
			public void widgetDefaultSelected(SelectionEvent arg0) {
			}});
		
		ccombo.addSelectionListener(sListener);
		
		new Label(sgImageText.getBody(),SWT.NONE);
		
		
		
		SimpleGroupStrategy sgs4 = new SimpleGroupStrategy(SWT.NONE);
		PGroup sgOther = new PGroup(container,sgs4);
		sgOther.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		sgOther.setText("Other Options");
		colorStyler.add(sgOther);
		colorStyler.add(sgOther.getBody());	
		sgOther.getBody().setLayout(new GridLayout(2,false));
		
		
		focus = new Button(sgOther.getBody(),SWT.CHECK | SWT.FLAT);
		focus.setText("Show Focus");
		focus.setSelection(true);
		colorStyler.add(focus);
		gd = new GridData();
		gd.horizontalSpan =2;
		focus.setLayoutData(gd);
		focus.addSelectionListener(sListener);
		
		focusActive = new Button(sgOther.getBody(),SWT.CHECK | SWT.FLAT);
		focusActive.setText("Show Active when Focused");
		focusActive.setSelection(true);
		colorStyler.add(focusActive);
		focusActive.addSelectionListener(sListener);
		

		
		colorStyler.style();
		borderStyler.style();
		
		//recreate();
	}

	private void recreate() {
		
		if (link != null && !link.isDisposed()){
			link.getActiveColor().dispose();
			link.getForeground().dispose();
			link.getBackground().dispose();			
			link.dispose();

		}
		
		int style = 0;
		
		if (underlineActive.getSelection())
			style = style | PLink.UNDERLINE_ACTIVE;

		if (underlineAlways.getSelection())
			style = style | PLink.UNDERLINE_ALWAYS;

		if (underlineNever.getSelection())
			style = style | PLink.UNDERLINE_NEVER;

		if (wrap.getSelection())
			style = style | PLink.WRAP;
		
		link = new PLink(exampleArea,style);

		GridData gd = new GridData(SWT.CENTER,SWT.TOP,true,true);
		gd.verticalIndent = 100;
		if (wrap.getSelection()){
			gd.widthHint = 150;
		}
		link.setLayoutData(gd);
		
		if (firstCreate ){
			foregroundComboStrat.setDefaultRGB(link.getForeground().getRGB());
			foregroundCombo.setValue(new NamedRGB("Default",link.getForeground().getRGB()));
			backgroundComboStrat.setDefaultRGB(link.getBackground().getRGB());
			backgroundCombo.setValue(new NamedRGB("Default",link.getBackground().getRGB()));

			activeComboStrat.setDefaultRGB(link.getActiveColor().getRGB());
			activeCombo.setValue(new NamedRGB("Default",link.getActiveColor().getRGB()));

		} else {
			link.setForeground(new Color(Display.getCurrent(), ((NamedRGB)foregroundCombo.getValue()).getRGB()));
			link.setBackground(new Color(Display.getCurrent(), ((NamedRGB)backgroundCombo.getValue()).getRGB()));
			link.setActiveColor(new Color(Display.getCurrent(), ((NamedRGB)activeCombo.getValue()).getRGB()));
		}
		
		link.setText(t.getText());
		
		if (font != null)
			link.setFont(font);
		
		if (!ccombo.getValue().equals("(None)")){
			if (!ccombo.getValue().equals("Browse...")){
				link.setImage(GalleryImageRegistry.getImage(this.getClass(),"find_off.png"));
				link.setActiveImage(GalleryImageRegistry.getImage(this.getClass(),"find.gif"));
			} else {
				link.setImage(browsedImage);
			}
		}
		
		link.setShowActiveWhenFocused(focusActive.getSelection());
		link.setShowFocus(focus.getSelection());
		exampleArea.layout();
		
	}

	public void setExampleArea(Composite area) {
		exampleArea = area;		
		area.setLayout(new GridLayout(1,false));
		
		recreate();
		
		firstCreate = false;
	}

	public void dispose() {
		super.dispose();
		if (link != null && !link.isDisposed()){
			link.getActiveColor().dispose();
			link.getForeground().dispose();
			link.getBackground().dispose();			
			link.dispose();

		}
	}
	
}

⌨️ 快捷键说明

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