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 = "";
17 protected Composite composite;
18 protected Button browseButton;
19 protected Text textField;
20 protected String browseDialogMessage = EMPTY_STRING;
21 protected String browseDialogTitle = EMPTY_STRING;
22 protected String validatedSelectionText = EMPTY_STRING;
24 public ResourceSelector(Composite parent) {
25 composite = new Composite(parent, SWT.NONE);
26 GridLayout compositeLayout = new GridLayout();
27 compositeLayout.marginWidth = 0;
28 compositeLayout.marginHeight = 0;
29 compositeLayout.numColumns = 2;
30 composite.setLayout(compositeLayout);
32 textField = new Text(composite, SWT.SINGLE | SWT.BORDER);
33 textField.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
34 textField.addModifyListener(new ModifyListener() {
35 public void modifyText(ModifyEvent e) {
36 validatedSelectionText = validateResourceSelection();
40 browseButton = new Button(composite, SWT.PUSH);
41 browseButton.setText("Browse...");
42 browseButton.addSelectionListener(new SelectionAdapter() {
43 public void widgetSelected(SelectionEvent e) {
44 handleBrowseSelected();
49 protected abstract void handleBrowseSelected();
50 protected abstract String validateResourceSelection();
52 protected Shell getShell() {
53 return composite.getShell();
56 public void setLayoutData(Object layoutData) {
57 composite.setLayoutData(layoutData);
60 public void addModifyListener(ModifyListener aListener) {
61 textField.addModifyListener(aListener);
64 public void setBrowseDialogMessage(String aMessage) {
65 browseDialogMessage = aMessage;
68 public void setBrowseDialogTitle(String aTitle) {
69 browseDialogTitle = aTitle;
72 public void setEnabled(boolean enabled) {
73 composite.setEnabled(enabled);
74 textField.setEnabled(enabled);
75 browseButton.setEnabled(enabled);
78 public String getSelectionText() {
79 return textField.getText();
82 public String getValidatedSelectionText() {
83 return validatedSelectionText;
86 public void setSelectionText(String newText) {
87 textField.setText(newText);