Improved completion processor
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / util / ResourceSelector.java
1 package net.sourceforge.phpdt.internal.ui.util;
2
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;
14
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;
23
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);
31
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();
37                         }
38                 });
39
40                 browseButton = new Button(composite, SWT.PUSH);
41                 browseButton.setText("Browse...");
42                 browseButton.addSelectionListener(new SelectionAdapter() {
43                         public void widgetSelected(SelectionEvent e) {
44                                 handleBrowseSelected();
45                         }
46                 });
47         }
48
49         protected abstract void handleBrowseSelected();
50         protected abstract String validateResourceSelection();
51
52         protected Shell getShell() {
53                 return composite.getShell();
54         }
55
56         public void setLayoutData(Object layoutData) {
57                 composite.setLayoutData(layoutData);
58         }
59
60         public void addModifyListener(ModifyListener aListener) {
61                 textField.addModifyListener(aListener);
62         }
63
64         public void setBrowseDialogMessage(String aMessage) {
65                 browseDialogMessage = aMessage;
66         }
67
68         public void setBrowseDialogTitle(String aTitle) {
69                 browseDialogTitle = aTitle;
70         }
71
72         public void setEnabled(boolean enabled) {
73                 composite.setEnabled(enabled);
74                 textField.setEnabled(enabled);
75                 browseButton.setEnabled(enabled);
76         }
77
78         public String getSelectionText() {
79                 return textField.getText();
80         }
81
82         public String getValidatedSelectionText() {
83                 return validatedSelectionText;
84         }
85
86         public void setSelectionText(String newText) {
87                 textField.setText(newText);
88         }
89 }