Fixed NullPointerException (problems == null) may occur
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / externaltools / launchConfigurations / ExternalToolsRefreshTab.java
1 package net.sourceforge.phpdt.externaltools.launchConfigurations;
2
3 /**********************************************************************
4 Copyright (c) 2000, 2002 IBM Corp.  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
10 import net.sourceforge.phpdt.externaltools.group.IGroupDialogPage;
11 import net.sourceforge.phpdt.externaltools.internal.dialog.ExternalToolVariableForm;
12 import net.sourceforge.phpdt.externaltools.internal.model.ExternalToolsImages;
13 import net.sourceforge.phpdt.externaltools.internal.model.ExternalToolsPlugin;
14 import net.sourceforge.phpdt.externaltools.internal.registry.ExternalToolVariable;
15 import net.sourceforge.phpdt.externaltools.model.IExternalToolConstants;
16 import net.sourceforge.phpdt.externaltools.model.ToolUtil;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.debug.core.ILaunchConfiguration;
20 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
21 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
22 import org.eclipse.jface.dialogs.IMessageProvider;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.SelectionAdapter;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.graphics.Image;
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
32 public class ExternalToolsRefreshTab extends AbstractLaunchConfigurationTab implements IGroupDialogPage {
33
34         private ExternalToolVariableForm variableForm;
35         
36         protected Button refreshField;
37         protected Button recursiveField;
38         
39         /**
40          * @see org.eclipse.debug.ui.ILaunchConfigurationTab#createControl(org.eclipse.swt.widgets.Composite)
41          */
42         public void createControl(Composite parent) {
43                 Composite mainComposite = new Composite(parent, SWT.NONE);
44                 setControl(mainComposite);
45                 
46                 GridLayout layout = new GridLayout();
47                 layout.numColumns = 1;
48                 GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
49                 mainComposite.setLayout(layout);
50                 mainComposite.setLayoutData(gridData);
51                 mainComposite.setFont(parent.getFont());
52                 createVerticalSpacer(mainComposite, 1);
53                 createRefreshComponent(mainComposite);
54                 createRecursiveComponent(mainComposite);
55                 createScopeComponent(mainComposite);
56         }
57         
58         /**
59          * Creates the controls needed to edit the refresh recursive
60          * attribute of an external tool
61          * 
62          * @param parent the composite to create the controls in
63          */
64         protected void createRecursiveComponent(Composite parent) {
65                 recursiveField = new Button(parent, SWT.CHECK);
66                 recursiveField.setText(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsRefreshTab.Recursively_&include_sub-folders_1")); //$NON-NLS-1$
67                 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
68                 recursiveField.setLayoutData(data);
69                 recursiveField.setFont(parent.getFont());
70                 recursiveField.addSelectionListener(new SelectionAdapter() {
71                         public void widgetSelected(SelectionEvent e) {
72                                 updateLaunchConfigurationDialog();
73                         }
74                 });
75         }
76         
77         /**
78          * Creates the controls needed to edit the refresh scope
79          * attribute of an external tool
80          * 
81          * @param parent the composite to create the controls in
82          */
83         protected void createRefreshComponent(Composite parent) {
84                 refreshField = new Button(parent, SWT.CHECK);
85                 refreshField.setText(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsRefreshTab.&Refresh_resources_after_running_tool_1")); //$NON-NLS-1$
86                 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
87                 refreshField.setLayoutData(data);
88                 refreshField.setFont(parent.getFont());
89                 refreshField.addSelectionListener(new SelectionAdapter() {
90                         public void widgetSelected(SelectionEvent e) {
91                                 updateEnabledState();
92                                 updateLaunchConfigurationDialog();
93                         }
94                 });
95         }
96         
97         /**
98          * Creates the controls needed to edit the refresh scope variable
99          * attribute of an external tool
100          * 
101          * @param parent the composite to create the controls in
102          */
103         protected void createScopeComponent(Composite parent) {
104                 String label = ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsRefreshTab.Choose_scope_v&ariable___2"); //$NON-NLS-1$
105                 ExternalToolVariable[] vars = ExternalToolsPlugin.getDefault().getRefreshVariableRegistry().getRefreshVariables();
106                 variableForm = new ExternalToolVariableForm(label, vars);
107                 variableForm.createContents(parent, this);
108         }
109         
110
111         /**
112          * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
113          */
114         public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
115         }
116
117         /**
118          * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration)
119          */
120         public void initializeFrom(ILaunchConfiguration configuration) {
121                 updateRefresh(configuration);
122                 updateRecursive(configuration);
123                 updateScope(configuration);
124         }
125         /**
126          * Method udpateScope.
127          * @param configuration
128          */
129         private void updateScope(ILaunchConfiguration configuration) {
130                 String scope = null;
131                 try {
132                         scope= configuration.getAttribute(IExternalToolConstants.ATTR_REFRESH_SCOPE, (String)null);
133                 } catch (CoreException ce) {
134                         ExternalToolsPlugin.getDefault().log(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsRefreshTab.Exception_reading_launch_configuration_3"), ce); //$NON-NLS-1$
135                 }
136                 String varName = null;
137                 String varValue = null;
138                 if (scope != null) {
139                         ToolUtil.VariableDefinition varDef = ToolUtil.extractVariableTag(scope, 0);
140                         varName = varDef.name;
141                         varValue = varDef.argument;
142                 }
143                 variableForm.selectVariable(varName, varValue);
144         }
145         /**
146          * Method updateRecursive.
147          * @param configuration
148          */
149         private void updateRecursive(ILaunchConfiguration configuration) {
150                 boolean recursive= true;
151                 try {
152                         recursive= configuration.getAttribute(IExternalToolConstants.ATTR_REFRESH_RECURSIVE, false);
153                 } catch (CoreException ce) {
154                         ExternalToolsPlugin.getDefault().log(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsRefreshTab.Exception_reading_launch_configuration_3"), ce); //$NON-NLS-1$
155                 }
156                 recursiveField.setSelection(recursive);
157         }
158         /**
159          * Method updateRefresh.
160          * @param configuration
161          */
162         private void updateRefresh(ILaunchConfiguration configuration) {
163                 String scope= null;
164                 try {
165                         scope= configuration.getAttribute(IExternalToolConstants.ATTR_REFRESH_SCOPE, (String)null);
166                 } catch (CoreException ce) {
167                         ExternalToolsPlugin.getDefault().log(ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsRefreshTab.Exception_reading_launch_configuration_3"), ce); //$NON-NLS-1$
168                 }
169                 refreshField.setSelection(scope != null);
170                 updateEnabledState();           
171         }
172
173         /**
174          * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
175          */
176         public void performApply(ILaunchConfigurationWorkingCopy configuration) {
177
178                 if (refreshField.getSelection()) {
179                         configuration.setAttribute(IExternalToolConstants.ATTR_REFRESH_SCOPE, variableForm.getSelectedVariable());
180                 } else {
181                         configuration.setAttribute(IExternalToolConstants.ATTR_REFRESH_SCOPE, (String)null);
182                 }
183                 
184                 setAttribute(IExternalToolConstants.ATTR_REFRESH_RECURSIVE, configuration, recursiveField.getSelection(), false);
185         }
186
187         /**
188          * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
189          */
190         public String getName() {
191                 return ExternalToolsLaunchConfigurationMessages.getString("ExternalToolsRefreshTab.Refres&h_6"); //$NON-NLS-1$
192         }
193         
194         /**
195          * Updates the enablement state of the fields.
196          */
197         protected void updateEnabledState() {
198                 if (refreshField != null) {
199                         if (recursiveField != null) {
200                                 recursiveField.setEnabled(refreshField.getSelection());
201                         }
202                         if (variableForm != null) {
203                                 variableForm.setEnabled(refreshField.getSelection());
204                         }
205                 }
206         }
207         /**
208          * @see net.sourceforge.phpdt.externaltools.group.IGroupDialogPage#convertHeightHint(int)
209          */
210         public int convertHeightHint(int chars) {
211                 return 0;
212         }
213
214         /**
215          * @see net.sourceforge.phpdt.externaltools.group.IGroupDialogPage#setButtonGridData(org.eclipse.swt.widgets.Button)
216          */
217         public GridData setButtonGridData(Button button) {
218                 return null;
219         }
220
221         /**
222          * @see net.sourceforge.phpdt.externaltools.group.IGroupDialogPage#setMessage(java.lang.String, int)
223          */
224         public void setMessage(String newMessage, int newType) {
225                 setMessage(newMessage);
226         }
227
228         /**
229          * @see net.sourceforge.phpdt.externaltools.group.IGroupDialogPage#updateValidState()
230          */
231         public void updateValidState() {
232                 updateLaunchConfigurationDialog();
233         }
234
235         /**
236          * @see org.eclipse.jface.dialogs.IMessageProvider#getMessageType()
237          */
238         public int getMessageType() {
239                 if (getErrorMessage() != null) {
240                         return IMessageProvider.ERROR;
241                 } else if (getMessage() != null) {
242                         return IMessageProvider.WARNING;
243                 }
244                 return IMessageProvider.NONE;
245         }
246         /**
247          * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getImage()
248          */
249         public Image getImage() {
250                 return ExternalToolsImages.getImage(IExternalToolConstants.IMG_ACTION_REFRESH);
251         }
252
253 }