1 package net.sourceforge.phpdt.internal.ui.util;
3 import org.eclipse.swt.SWT;
4 import org.eclipse.swt.events.ModifyEvent;
5 import org.eclipse.swt.events.ModifyListener;
6 import org.eclipse.swt.events.SelectionAdapter;
7 import org.eclipse.swt.events.SelectionEvent;
8 import org.eclipse.swt.layout.GridData;
9 import org.eclipse.swt.layout.GridLayout;
10 import org.eclipse.swt.widgets.Button;
11 import org.eclipse.swt.widgets.Composite;
12 import org.eclipse.swt.widgets.Shell;
13 import org.eclipse.swt.widgets.Text;
15 public abstract class ResourceSelector {
16 protected final static String EMPTY_STRING = "";
18 protected Composite composite;
20 protected Button browseButton;
22 protected Text textField;
24 protected String browseDialogMessage = EMPTY_STRING;
26 protected String browseDialogTitle = EMPTY_STRING;
28 protected String validatedSelectionText = EMPTY_STRING;
30 public ResourceSelector(Composite parent) {
31 composite = new Composite(parent, SWT.NONE);
32 GridLayout compositeLayout = new GridLayout();
33 compositeLayout.marginWidth = 0;
34 compositeLayout.marginHeight = 0;
35 compositeLayout.numColumns = 2;
36 composite.setLayout(compositeLayout);
38 textField = new Text(composite, SWT.SINGLE | SWT.BORDER);
39 textField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
40 textField.addModifyListener(new ModifyListener() {
41 public void modifyText(ModifyEvent e) {
42 validatedSelectionText = validateResourceSelection();
46 browseButton = new Button(composite, SWT.PUSH);
47 browseButton.setText("Browse...");
48 browseButton.addSelectionListener(new SelectionAdapter() {
49 public void widgetSelected(SelectionEvent e) {
50 handleBrowseSelected();
55 protected abstract void handleBrowseSelected();
57 protected abstract String validateResourceSelection();
59 protected Shell getShell() {
60 return composite.getShell();
63 public void setLayoutData(Object layoutData) {
64 composite.setLayoutData(layoutData);
67 public void addModifyListener(ModifyListener aListener) {
68 textField.addModifyListener(aListener);
71 public void setBrowseDialogMessage(String aMessage) {
72 browseDialogMessage = aMessage;
75 public void setBrowseDialogTitle(String aTitle) {
76 browseDialogTitle = aTitle;
79 public void setEnabled(boolean enabled) {
80 composite.setEnabled(enabled);
81 textField.setEnabled(enabled);
82 browseButton.setEnabled(enabled);
85 public String getSelectionText() {
86 return textField.getText();
89 public String getValidatedSelectionText() {
90 return validatedSelectionText;
93 public void setSelectionText(String newText) {
94 textField.setText(newText);