当前位置: 首页 > 工具软件 > Composite > 使用案例 >

自定义Composite

章承
2023-12-01
package org.soyatec.windows.azure.jdt.ui;

import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
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.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.soyatec.windows.azure.jdt.AzureJdtPlugin;
import org.soyatec.windows.azure.util.JFaceUtil;

public class CustomStringButtonDialogField extends Composite {
	private Label fLabel;
	private Text fLocation;
	private Button fButton;
	private static final String DIALOGSTORE_LAST_EXTERNAL_LOC = AzureJdtPlugin.PLUGIN_ID
			+ ".last.external.project";

	public CustomStringButtonDialogField(Composite parent, int style,
			String label, String desc, int width) {
		super(parent, style);
		setLayout(new GridLayout(3, false));
		this.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		doFillIntoGrid(this, label, desc, width);
		addListener();
	}

	public void doFillIntoGrid(Composite parent, String label, String desc,
			int width) {
		fLabel = createLableControl(parent, label, desc, width);
		fLocation = JFaceUtil.createText(parent);
		fButton = getChangeControl(parent);

	}

	/**
	 * Sets the label of the button.
	 */
	public void setButtonLabel(String label) {
		fButton.setLayoutData(label);
	}

	/**
	 * Sets the enable state of the button.
	 */
	public void enableButton(boolean enable) {
		fButton.setEnabled(enable);
	}

	private Label createLableControl(Composite parent, String label,
			String desc, int width) {
		Label tooltipLabel = new Label(parent, SWT.None);
		tooltipLabel.setText(label);
		tooltipLabel.setToolTipText(desc);
		GridData data = new GridData();
		data.widthHint = width;
		tooltipLabel.setLayoutData(data);
		return tooltipLabel;
	}

	void addListener() {

		fButton.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent e) {
				widgetSelected(e);
			}

			public void widgetSelected(SelectionEvent e) {
				onButtonPressed(e);
			}

		});
	}

	public void setLabelText(String text) {

	}

	private Button getChangeControl(Composite parent) {
		return JFaceUtil.createButton(parent, SWT.None, "Browse...", 1);
	}

	public Text getTextControl() {
		return fLocation;
	}

	public Button getButtonControl() {
		return fButton;
	}

	public String getText() {
		return fLocation.getText();
	}

	public void setText(String path) {
		this.fLocation.setText(path);
	}

	public void onButtonPressed(SelectionEvent e) {
		final DirectoryDialog dialog = new DirectoryDialog(this.getShell());
		dialog.setMessage("Choose a directory");
		String directoryName = fLocation.getText().trim();
		if (directoryName.length() == 0) {
			String prevLocation = AzureJdtPlugin.getDefault()
					.getDialogSettings().get(DIALOGSTORE_LAST_EXTERNAL_LOC);
			if (prevLocation != null) {
				directoryName = prevLocation;
			}
		}
		if (directoryName.length() > 0) {
			final File path = new File(directoryName);
			if (path.exists())
				dialog.setFilterPath(directoryName);
		}
		final String selectedDirectory = dialog.open();
		if (selectedDirectory != null) {
			fLocation.setText(selectedDirectory);
			AzureJdtPlugin.getDefault().getDialogSettings().put(
					DIALOGSTORE_LAST_EXTERNAL_LOC, selectedDirectory);
		}
	}
}

 

 类似资料: