1 package net.sourceforge.phpdt.externaltools.variable;
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
10 **********************************************************************/
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;
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;
38 * Visual component to edit the resource type variable value.
40 * This class is not intended to be extended by clients.
43 public class ResourceComponent implements IVariableComponent {
44 private IGroupDialogPage page;
46 private boolean isValid = true;
48 protected Group mainGroup;
50 protected Button selectedResourceButton;
52 protected Button specificResourceButton;
54 protected TreeViewer resourceList;
56 private IResource selectedResource;
59 * Creates the component
61 public ResourceComponent() {
66 * (non-Javadoc) Method declared on IVariableComponent.
68 public void createContents(Composite parent, String varTag,
69 IGroupDialogPage page) {
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));
84 createSelectedResourceOption();
85 createSpecificResourceOption();
88 updateResourceListEnablement();
92 * Creates the list of resources.
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());
101 resourceList = new TreeViewer(tree);
103 .addSelectionChangedListener(new ISelectionChangedListener() {
104 public void selectionChanged(SelectionChangedEvent event) {
105 validateResourceListSelection();
106 selectedResource = (IResource) ((IStructuredSelection) event
107 .getSelection()).getFirstElement();
110 resourceList.setContentProvider(new WorkbenchContentProvider());
111 resourceList.setLabelProvider(new WorkbenchLabelProvider());
112 resourceList.setInput(ResourcesPlugin.getWorkspace().getRoot());
116 * Creates the option button for using the selected resource.
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);
129 * Creates the option button for using a specific resource.
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);
140 specificResourceButton.addSelectionListener(new SelectionAdapter() {
141 public void widgetSelected(SelectionEvent e) {
142 updateResourceListEnablement();
148 * (non-Javadoc) Method declared on IVariableComponent.
150 public Control getControl() {
155 * Returns the dialog page this component is part of
157 protected final IGroupDialogPage getPage() {
162 * (non-Javadoc) Method declared on IVariableComponent.
164 public String getVariableValue() {
165 if (selectedResourceButton != null
166 && selectedResourceButton.getSelection())
169 if (resourceList != null) {
170 if (selectedResource != null)
171 return selectedResource.getFullPath().toString();
178 * Returns the number of items to be visible in the resource list. This will
179 * determine the initial height.
181 protected int getInitialVisibleItemCount() {
186 * (non-Javadoc) Method declared on IVariableComponent.
188 public boolean isValid() {
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
198 * <code>true</code> if all values valid, <code>false</code>
201 protected final void setIsValid(boolean isValid) {
202 if (this.isValid != isValid) {
203 this.isValid = isValid;
204 this.page.updateValidState();
209 * Updates the enablement of the resource list if needed
211 protected void updateResourceListEnablement() {
212 if (specificResourceButton != null && resourceList != null)
213 resourceList.getTree().setEnabled(
214 specificResourceButton.getSelection());
218 * (non-Javadoc) Method declared on IVariableComponent.
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);
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);
238 resourceList.setSelection(new StructuredSelection(member),
241 resourceList.setSelection(StructuredSelection.EMPTY);
247 * (non-Javadoc) Method declared on IVariableComponent.
249 public void validate() {
250 if (specificResourceButton != null
251 && specificResourceButton.getSelection()) {
252 validateResourceListSelection();
255 getPage().setMessage(null, IMessageProvider.NONE);
260 * Returns whether that the resource list selection is valid. If the list
261 * was not created, returns <code>true</code>.
263 * @return <code>true</code> to continue validating other fields,
264 * <code>false</code> to stop.
266 protected boolean validateResourceListSelection() {
267 if (resourceList == null)
270 if (resourceList.getSelection().isEmpty()) {
273 ExternalToolsModelMessages
274 .getString("ResourceComponent.selectionRequired"), IMessageProvider.WARNING); //$NON-NLS-1$