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 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;
37 * Visual component to edit the resource type variable
40 * This class is not intended to be extended by clients.
43 public class ResourceComponent implements IVariableComponent {
44 private IGroupDialogPage page;
45 private boolean isValid = true;
47 protected Group mainGroup;
48 protected Button selectedResourceButton;
49 protected Button specificResourceButton;
50 protected TreeViewer resourceList;
51 private IResource selectedResource;
54 * Creates the component
56 public ResourceComponent() {
61 * Method declared on IVariableComponent.
63 public void createContents(Composite parent, String varTag, IGroupDialogPage page) {
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));
78 createSelectedResourceOption();
79 createSpecificResourceOption();
82 updateResourceListEnablement();
86 * Creates the list of resources.
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());
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();
102 resourceList.setContentProvider(new WorkbenchContentProvider());
103 resourceList.setLabelProvider(new WorkbenchLabelProvider());
104 resourceList.setInput(ResourcesPlugin.getWorkspace().getRoot());
108 * Creates the option button for using the selected
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);
121 * Creates the option button for using a specific
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);
132 specificResourceButton.addSelectionListener(new SelectionAdapter() {
133 public void widgetSelected(SelectionEvent e) {
134 updateResourceListEnablement();
140 * Method declared on IVariableComponent.
142 public Control getControl() {
147 * Returns the dialog page this component is part of
149 protected final IGroupDialogPage getPage() {
154 * Method declared on IVariableComponent.
156 public String getVariableValue() {
157 if (selectedResourceButton != null && selectedResourceButton.getSelection())
160 if (resourceList != null) {
161 if (selectedResource != null)
162 return selectedResource.getFullPath().toString();
169 * Returns the number of items to be visible in the
170 * resource list. This will determine the initial height.
172 protected int getInitialVisibleItemCount() {
177 * Method declared on IVariableComponent.
179 public boolean isValid() {
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.
188 * @param isValid <code>true</code> if all values valid,
189 * <code>false</code> otherwise
191 protected final void setIsValid(boolean isValid) {
192 if (this.isValid != isValid) {
193 this.isValid = isValid;
194 this.page.updateValidState();
199 * Updates the enablement of the resource list if needed
201 protected void updateResourceListEnablement() {
202 if (specificResourceButton != null && resourceList != null)
203 resourceList.getTree().setEnabled(specificResourceButton.getSelection());
207 * Method declared on IVariableComponent.
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);
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);
226 resourceList.setSelection(new StructuredSelection(member), true);
228 resourceList.setSelection(StructuredSelection.EMPTY);
234 * Method declared on IVariableComponent.
236 public void validate() {
237 if (specificResourceButton != null && specificResourceButton.getSelection()) {
238 validateResourceListSelection();
241 getPage().setMessage(null, IMessageProvider.NONE);
246 * Returns whether that the resource list selection is valid.
247 * If the list was not created, returns <code>true</code>.
249 * @return <code>true</code> to continue validating other
250 * fields, <code>false</code> to stop.
252 protected boolean validateResourceListSelection() {
253 if (resourceList == null)
256 if (resourceList.getSelection().isEmpty()) {
257 getPage().setMessage(ToolMessages.getString("ResourceComponent.selectionRequired"), IMessageProvider.WARNING); //$NON-NLS-1$