misc changes
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / externaltools / variable / ResourceComponent.java
1 package net.sourceforge.phpdt.externaltools.variable;
2
3 /**********************************************************************
4 Copyright (c) 2002 IBM Corp. and others. All rights reserved.
5 This file is made available under the terms of the Common Public License v1.0
6 which accompanies this distribution, and is available at
7 http://www.eclipse.org/legal/cpl-v10.html
8  
9 Contributors:
10 **********************************************************************/
11
12 import net.sourceforge.phpdt.externaltools.group.IGroupDialogPage;
13 import net.sourceforge.phpdt.externaltools.internal.model.ExternalToolsModelMessages;
14 import net.sourceforge.phpdt.externaltools.model.ToolUtil;
15
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.ResourcesPlugin;
18 import org.eclipse.jface.dialogs.IMessageProvider;
19 import org.eclipse.jface.viewers.ISelectionChangedListener;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.jface.viewers.SelectionChangedEvent;
22 import org.eclipse.jface.viewers.StructuredSelection;
23 import org.eclipse.jface.viewers.TreeViewer;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.SelectionAdapter;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Group;
33 import org.eclipse.swt.widgets.Tree;
34 import org.eclipse.ui.model.WorkbenchContentProvider;
35 import org.eclipse.ui.model.WorkbenchLabelProvider;
36
37 /**
38  * Visual component to edit the resource type variable
39  * value.
40  * <p>
41  * This class is not intended to be extended by clients.
42  * </p>
43  */
44 public class ResourceComponent implements IVariableComponent {
45         private IGroupDialogPage page;
46         private boolean isValid = true;
47         
48         protected Group mainGroup;
49         protected Button selectedResourceButton;
50         protected Button specificResourceButton;
51         protected TreeViewer resourceList;
52         private IResource selectedResource;
53         
54         /**
55          * Creates the component
56          */
57         public ResourceComponent() {
58                 super();
59         }
60
61         /* (non-Javadoc)
62          * Method declared on IVariableComponent.
63          */
64         public void createContents(Composite parent, String varTag, IGroupDialogPage page) {
65                 this.page = page;
66                 
67                 // main composite
68                 mainGroup = new Group(parent, SWT.NONE);
69                 GridLayout layout = new GridLayout();
70                 layout.marginWidth = 0;
71                 layout.marginHeight = 0;
72                 layout.numColumns = 1;
73                 GridData gridData = new GridData(GridData.FILL_BOTH);
74                 mainGroup.setLayout(layout);
75                 mainGroup.setLayoutData(gridData);
76                 mainGroup.setFont(parent.getFont());
77                 mainGroup.setText(ToolUtil.buildVariableTag(varTag, null));
78                 
79                 createSelectedResourceOption();
80                 createSpecificResourceOption();
81                 createResourceList();
82                 
83                 updateResourceListEnablement();
84         }
85
86         /**
87          * Creates the list of resources.
88          */
89         protected void createResourceList() {
90                 Tree tree = new Tree(mainGroup, SWT.SINGLE | SWT.BORDER);
91                 GridData data = new GridData(GridData.FILL_BOTH);
92                 data.heightHint = tree.getItemHeight() * getInitialVisibleItemCount();
93                 tree.setLayoutData(data);
94                 tree.setFont(mainGroup.getFont());
95                 
96                 resourceList = new TreeViewer(tree);
97                 resourceList.addSelectionChangedListener(new ISelectionChangedListener() {
98                         public void selectionChanged(SelectionChangedEvent event) {
99                                 validateResourceListSelection();
100                                 selectedResource= (IResource) ((IStructuredSelection)event.getSelection()).getFirstElement();
101                         }
102                 });
103                 resourceList.setContentProvider(new WorkbenchContentProvider());
104                 resourceList.setLabelProvider(new WorkbenchLabelProvider());
105                 resourceList.setInput(ResourcesPlugin.getWorkspace().getRoot());
106         }
107         
108         /**
109          * Creates the option button for using the selected
110          * resource.
111          */
112         protected void createSelectedResourceOption() {
113                 selectedResourceButton = new Button(mainGroup, SWT.RADIO);
114                 selectedResourceButton.setText(ExternalToolsModelMessages.getString("ResourceComponent.selectedResLabel")); //$NON-NLS-1$
115                 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
116                 selectedResourceButton.setLayoutData(data);
117                 selectedResourceButton.setFont(mainGroup.getFont());
118                 selectedResourceButton.setSelection(true);
119         }
120         
121         /**
122          * Creates the option button for using a specific
123          * resource.
124          */
125         protected void createSpecificResourceOption() {
126                 specificResourceButton = new Button(mainGroup, SWT.RADIO);
127                 specificResourceButton.setText(ExternalToolsModelMessages.getString("ResourceComponent.specificResLabel")); //$NON-NLS-1$
128                 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
129                 specificResourceButton.setLayoutData(data);
130                 specificResourceButton.setFont(mainGroup.getFont());
131                 specificResourceButton.setSelection(false);
132                 
133                 specificResourceButton.addSelectionListener(new SelectionAdapter() {
134                         public void widgetSelected(SelectionEvent e) {
135                                 updateResourceListEnablement();
136                         }
137                 });
138         }
139         
140         /* (non-Javadoc)
141          * Method declared on IVariableComponent.
142          */
143         public Control getControl() {
144                 return mainGroup;
145         }
146
147         /**
148          * Returns the dialog page this component is part of
149          */
150         protected final IGroupDialogPage getPage() {
151                 return page;
152         }
153         
154         /* (non-Javadoc)
155          * Method declared on IVariableComponent.
156          */
157         public String getVariableValue() {
158                 if (selectedResourceButton != null && selectedResourceButton.getSelection())
159                         return null;
160                 
161                 if (resourceList != null) {
162                         if (selectedResource != null)
163                                 return selectedResource.getFullPath().toString();
164                 }
165                 
166                 return null;
167         }
168
169         /**
170          * Returns the number of items to be visible in the
171          * resource list. This will determine the initial height.
172          */
173         protected int getInitialVisibleItemCount() {
174                 return 10;
175         }
176         
177         /* (non-Javadoc)
178          * Method declared on IVariableComponent.
179          */
180         public boolean isValid() {
181                 return isValid;
182         }
183
184         /**
185          * Sets whether the component's values are all valid.
186          * Updates the components's page valid state. No action
187          * taken if new valid state same as current one.
188          * 
189          * @param isValid <code>true</code> if all values valid,
190          *              <code>false</code> otherwise
191          */
192         protected final void setIsValid(boolean isValid) {
193                 if (this.isValid != isValid) {
194                         this.isValid = isValid;
195                         this.page.updateValidState();
196                 }
197         }
198         
199         /**
200          * Updates the enablement of the resource list if needed
201          */
202         protected void updateResourceListEnablement() {
203                 if (specificResourceButton != null && resourceList != null)
204                         resourceList.getTree().setEnabled(specificResourceButton.getSelection());
205         }
206         
207         /* (non-Javadoc)
208          * Method declared on IVariableComponent.
209          */
210         public void setVariableValue(String varValue) {
211                 if (varValue == null || varValue.length() == 0) {
212                         if (selectedResourceButton != null)
213                                 selectedResourceButton.setSelection(true);
214                         if (specificResourceButton != null)
215                                 specificResourceButton.setSelection(false);
216                         if (resourceList != null)
217                                 resourceList.getTree().setEnabled(false);
218                 } else {
219                         if (selectedResourceButton != null)
220                                 selectedResourceButton.setSelection(false);
221                         if (specificResourceButton != null)
222                                 specificResourceButton.setSelection(true);
223                         if (resourceList != null) {
224                                 resourceList.getTree().setEnabled(true);
225                                 IResource member = ResourcesPlugin.getWorkspace().getRoot().findMember(varValue);
226                                 if (member != null)
227                                         resourceList.setSelection(new StructuredSelection(member), true);
228                                 else
229                                         resourceList.setSelection(StructuredSelection.EMPTY);
230                         }
231                 }
232         }
233         
234         /* (non-Javadoc)
235          * Method declared on IVariableComponent.
236          */
237         public void validate() {
238                 if (specificResourceButton != null && specificResourceButton.getSelection()) {
239                         validateResourceListSelection();
240                 }
241
242                 getPage().setMessage(null, IMessageProvider.NONE);
243                 setIsValid(true);
244         }
245
246         /**
247          * Returns whether that the resource list selection is valid.
248          * If the list was not created, returns <code>true</code>.
249          * 
250          * @return <code>true</code> to continue validating other
251          *      fields, <code>false</code> to stop.
252          */
253         protected boolean validateResourceListSelection() {
254                 if (resourceList == null)
255                         return true;
256
257                 if (resourceList.getSelection().isEmpty()) {
258                         getPage().setMessage(ExternalToolsModelMessages.getString("ResourceComponent.selectionRequired"), IMessageProvider.WARNING); //$NON-NLS-1$
259                         setIsValid(false);
260                         return false;
261                 }
262                 
263                 return true;
264         }
265 }