autocompleteexampler.java

来自「开源的关于SWT开发的图形应用库」· Java 代码 · 共 163 行

JAVA
163
字号
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.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.Label;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;

import com.swtplus.utility.Styler;
import com.swtplus.widgets.AutoComplete;
import com.swtplus.widgets.PGroup;
import com.swtplus.widgets.group.SimpleGroupStrategy;

public class AutoCompleteExampler extends Composite implements IWidgetExampler {
	
	Composite exampleArea;

	private Text t;
	Label l;
	
	Button resize;
	Spinner itemsToShow;

	public AutoCompleteExampler(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());
		

		
		resize = new Button(sgStyles.getBody(),SWT.CHECK | SWT.FLAT);
		resize.setText("AutoComplete.RESIZE");
		colorStyler.add(resize);
		resize.addSelectionListener(sListener);
		
		
		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));
		
		Label l = new Label(sgColors.getBody(),SWT.NONE);
		l.setText("Maximum Visible Item Count:");
		colorStyler.add(l);
		
		itemsToShow = new Spinner(sgColors.getBody(), SWT.FLAT);
		itemsToShow.setSelection(5);
		itemsToShow.addModifyListener(mListener);
		borderStyler.add(itemsToShow);
		
		colorStyler.style();
		borderStyler.style();
		
	}

	private void recreate() {
		
		if (t != null && !t.isDisposed()){	
			t.dispose();
		}
		if (l != null && !l.isDisposed()){	
			l.dispose();
		}

		
		int style = 0;
		
		if (resize.getSelection())
			style = style | AutoComplete.RESIZE;

		
		t = new Text(exampleArea,SWT.BORDER);

		GridData gd = new GridData(SWT.CENTER,SWT.TOP,true,false);
		gd.verticalIndent = 100;
		gd.widthHint = 200;
		t.setLayoutData(gd);
		
		AutoComplete ac = new AutoComplete(t,style);
		ac.setVisibleItemCount(itemsToShow.getSelection());
		
		ac.setValues(new String[]{"a sample value","another sample value","a sample 2","and more values","almost done","and this is the last sample value"});
		
		
		l = new Label(exampleArea,SWT.WRAP);
		l.setText("All AutoComplete examples begin with the letter 'a'.  Please type an 'a' above.\r\n\r\nAutoComplete can be attached to any regular Text widget.");

		gd = new GridData(SWT.CENTER,SWT.TOP,true,true);
		gd.widthHint = 200;
		l.setLayoutData(gd);
		
		exampleArea.layout();		
	}

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

	public void dispose() {
		super.dispose();
		if (t != null && !t.isDisposed()){	
			t.dispose();
		}
		if (l != null && !l.isDisposed()){	
			l.dispose();
		}
	}
	
}

⌨️ 快捷键说明

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