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