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