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.ToolMessages;
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
41 * This class is not intended to be extended by clients.
44 public class ResourceComponent implements IVariableComponent {
45 private IGroupDialogPage page;
46 private boolean isValid = true;
48 protected Group mainGroup;
49 protected Button selectedResourceButton;
50 protected Button specificResourceButton;
51 protected TreeViewer resourceList;
52 private IResource selectedResource;
55 * Creates the component
57 public ResourceComponent() {
62 * Method declared on IVariableComponent.
64 public void createContents(Composite parent, String varTag, IGroupDialogPage page) {
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));
79 createSelectedResourceOption();
80 createSpecificResourceOption();
83 updateResourceListEnablement();
87 * Creates the list of resources.
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());
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();
103 resourceList.setContentProvider(new WorkbenchContentProvider());
104 resourceList.setLabelProvider(new WorkbenchLabelProvider());
105 resourceList.setInput(ResourcesPlugin.getWorkspace().getRoot());
109 * Creates the option button for using the selected
112 protected void createSelectedResourceOption() {
113 selectedResourceButton = new Button(mainGroup, SWT.RADIO);
114 selectedResourceButton.setText(ToolMessages.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);
122 * Creates the option button for using a specific
125 protected void createSpecificResourceOption() {
126 specificResourceButton = new Button(mainGroup, SWT.RADIO);
127 specificResourceButton.setText(ToolMessages.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);
133 specificResourceButton.addSelectionListener(new SelectionAdapter() {
134 public void widgetSelected(SelectionEvent e) {
135 updateResourceListEnablement();
141 * Method declared on IVariableComponent.
143 public Control getControl() {
148 * Returns the dialog page this component is part of
150 protected final IGroupDialogPage getPage() {
155 * Method declared on IVariableComponent.
157 public String getVariableValue() {
158 if (selectedResourceButton != null && selectedResourceButton.getSelection())
161 if (resourceList != null) {
162 if (selectedResource != null)
163 return selectedResource.getFullPath().toString();
170 * Returns the number of items to be visible in the
171 * resource list. This will determine the initial height.
173 protected int getInitialVisibleItemCount() {
178 * Method declared on IVariableComponent.
180 public boolean isValid() {
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.
189 * @param isValid <code>true</code> if all values valid,
190 * <code>false</code> otherwise
192 protected final void setIsValid(boolean isValid) {
193 if (this.isValid != isValid) {
194 this.isValid = isValid;
195 this.page.updateValidState();
200 * Updates the enablement of the resource list if needed
202 protected void updateResourceListEnablement() {
203 if (specificResourceButton != null && resourceList != null)
204 resourceList.getTree().setEnabled(specificResourceButton.getSelection());
208 * Method declared on IVariableComponent.
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);
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);
227 resourceList.setSelection(new StructuredSelection(member), true);
229 resourceList.setSelection(StructuredSelection.EMPTY);
235 * Method declared on IVariableComponent.
237 public void validate() {
238 if (specificResourceButton != null && specificResourceButton.getSelection()) {
239 validateResourceListSelection();
242 getPage().setMessage(null, IMessageProvider.NONE);
247 * Returns whether that the resource list selection is valid.
248 * If the list was not created, returns <code>true</code>.
250 * @return <code>true</code> to continue validating other
251 * fields, <code>false</code> to stop.
253 protected boolean validateResourceListSelection() {
254 if (resourceList == null)
257 if (resourceList.getSelection().isEmpty()) {
258 getPage().setMessage(ToolMessages.getString("ResourceComponent.selectionRequired"), IMessageProvider.WARNING); //$NON-NLS-1$