KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > debug > ui > launchConfigurations > AppletParametersTab


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.debug.ui.launchConfigurations;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.debug.core.ILaunchConfiguration;
18 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
19 import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds;
20 import org.eclipse.jdt.internal.debug.ui.JavaDebugImages;
21 import org.eclipse.jdt.internal.debug.ui.launcher.LauncherMessages;
22 import org.eclipse.jdt.internal.debug.ui.launcher.NameValuePairDialog;
23 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
24 import org.eclipse.jface.dialogs.Dialog;
25 import org.eclipse.jface.viewers.ColumnWeightData;
26 import org.eclipse.jface.viewers.ILabelProviderListener;
27 import org.eclipse.jface.viewers.IStructuredContentProvider;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.jface.viewers.ITableLabelProvider;
30 import org.eclipse.jface.viewers.TableLayout;
31 import org.eclipse.jface.viewers.TableViewer;
32 import org.eclipse.jface.viewers.Viewer;
33 import org.eclipse.jface.viewers.ViewerComparator;
34 import org.eclipse.jface.window.Window;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.events.ModifyEvent;
37 import org.eclipse.swt.events.ModifyListener;
38 import org.eclipse.swt.events.MouseAdapter;
39 import org.eclipse.swt.events.MouseEvent;
40 import org.eclipse.swt.events.SelectionAdapter;
41 import org.eclipse.swt.events.SelectionEvent;
42 import org.eclipse.swt.graphics.Image;
43 import org.eclipse.swt.layout.GridData;
44 import org.eclipse.swt.layout.GridLayout;
45 import org.eclipse.swt.widgets.Button;
46 import org.eclipse.swt.widgets.Composite;
47 import org.eclipse.swt.widgets.Label;
48 import org.eclipse.swt.widgets.Table;
49 import org.eclipse.swt.widgets.TableColumn;
50 import org.eclipse.swt.widgets.Text;
51 import org.eclipse.ui.PlatformUI;
52  
53 /**
54  * This tab appears for java applet launch configurations and allows the user to edit
55  * applet-specific attributes such as width, height, name & applet parameters.
56  * <p>
57  * This class may be instantiated. This class is not intended to be subclassed.
58  * </p>
59  * @since 2.1
60  */

61 public class AppletParametersTab extends JavaLaunchTab {
62     
63     private Label fWidthLabel;
64     private Text fWidthText;
65     private Label fHeightLabel;
66     private Text fHeightText;
67     private Label fNameLabel;
68     private Text fNameText;
69     private Button fParametersAddButton;
70     private Button fParametersRemoveButton;
71     private Button fParametersEditButton;
72     
73     private class AppletTabListener extends SelectionAdapter implements ModifyListener {
74
75         /* (non-Javadoc)
76          * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
77          */

78         public void modifyText(ModifyEvent e) {
79             updateLaunchConfigurationDialog();
80         }
81         
82         /* (non-Javadoc)
83          * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
84          */

85         public void widgetSelected(SelectionEvent e) {
86             Object JavaDoc source= e.getSource();
87             if (source == fViewer.getTable() || source == fViewer) {
88                 setParametersButtonsEnableState();
89             } else if (source == fParametersAddButton) {
90                 handleParametersAddButtonSelected();
91             } else if (source == fParametersEditButton) {
92                 handleParametersEditButtonSelected();
93             } else if (source == fParametersRemoveButton) {
94                 handleParametersRemoveButtonSelected();
95             }
96         }
97
98     }
99     
100     private AppletTabListener fListener= new AppletTabListener();
101
102     private static final String JavaDoc EMPTY_STRING = ""; //$NON-NLS-1$
103

104     /**
105      * The default value for the 'width' attribute.
106      */

107     public static final int DEFAULT_APPLET_WIDTH = 200;
108     
109     /**
110      * The default value for the 'height' attribute.
111      */

112     public static final int DEFAULT_APPLET_HEIGHT = 200;
113     
114     /**
115      * The parameters table viewer
116      */

117     private TableViewer fViewer;
118
119     /**
120      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#createControl(Composite)
121      */

122     public void createControl(Composite parent) {
123         Composite comp = new Composite(parent, SWT.NONE);
124         setControl(comp);
125         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaDebugHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_APPLET_PARAMETERS_TAB);
126         GridLayout topLayout = new GridLayout();
127         comp.setLayout(topLayout);
128         GridData gd;
129         
130         Composite widthHeightNameComp = new Composite(comp, SWT.NONE);
131         gd = new GridData(GridData.FILL_HORIZONTAL);
132         widthHeightNameComp.setLayoutData(gd);
133         GridLayout widthHeightNameLayout = new GridLayout();
134         widthHeightNameLayout.marginHeight = 0;
135         widthHeightNameLayout.marginWidth = 0;
136         widthHeightNameLayout.numColumns = 4;
137         widthHeightNameComp.setLayout(widthHeightNameLayout);
138         
139         fWidthLabel= new Label(widthHeightNameComp, SWT.NONE);
140         fWidthLabel.setText(LauncherMessages.appletlauncher_argumenttab_widthlabel_text);
141         
142         fWidthText = new Text(widthHeightNameComp, SWT.SINGLE | SWT.BORDER);
143         gd = new GridData(GridData.FILL_HORIZONTAL);
144         fWidthText.setLayoutData(gd);
145         fWidthText.addModifyListener(fListener);
146
147         fNameLabel = new Label(widthHeightNameComp, SWT.NONE);
148         fNameLabel.setText(LauncherMessages.appletlauncher_argumenttab_namelabel_text);
149         
150         fNameText = new Text(widthHeightNameComp, SWT.SINGLE | SWT.BORDER);
151         gd = new GridData(GridData.FILL_HORIZONTAL);
152         fNameText.setLayoutData(gd);
153         fNameText.addModifyListener(fListener);
154
155         fHeightLabel= new Label(widthHeightNameComp, SWT.NONE);
156         fHeightLabel.setText(LauncherMessages.appletlauncher_argumenttab_heightlabel_text);
157         
158         fHeightText = new Text(widthHeightNameComp, SWT.SINGLE | SWT.BORDER);
159         gd = new GridData(GridData.FILL_HORIZONTAL);
160         fHeightText.setLayoutData(gd);
161         fHeightText.addModifyListener(fListener);
162         
163         Label blank = new Label(widthHeightNameComp, SWT.NONE);
164         blank.setText(EMPTY_STRING);
165         Label hint = new Label(widthHeightNameComp, SWT.NONE);
166         hint.setText(LauncherMessages.AppletParametersTab__optional_applet_instance_name__1);
167         gd = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
168         hint.setLayoutData(gd);
169                 
170         createVerticalSpacer(comp);
171         
172         Composite parametersComp = new Composite(comp, SWT.NONE);
173         gd = new GridData(GridData.FILL_BOTH);
174         parametersComp.setLayoutData(gd);
175         GridLayout parametersLayout = new GridLayout();
176         parametersLayout.numColumns = 2;
177         parametersLayout.marginHeight = 0;
178         parametersLayout.marginWidth = 0;
179         parametersComp.setLayout(parametersLayout);
180         
181         Label parameterLabel = new Label(parametersComp, SWT.NONE);
182         parameterLabel.setText(LauncherMessages.appletlauncher_argumenttab_parameterslabel_text);
183         gd = new GridData();
184         gd.horizontalSpan = 2;
185         parameterLabel.setLayoutData(gd);
186         
187         
188         fViewer = new TableViewer(parametersComp);
189         Table parametersTable = fViewer.getTable();
190         gd = new GridData(GridData.FILL_BOTH);
191         parametersTable.setLayoutData(gd);
192         TableColumn column1 = new TableColumn(parametersTable, SWT.NONE);
193         column1.setText(LauncherMessages.appletlauncher_argumenttab_parameterscolumn_name_text);
194         TableColumn column2 = new TableColumn(parametersTable, SWT.NONE);
195         column2.setText(LauncherMessages.appletlauncher_argumenttab_parameterscolumn_value_text);
196         TableLayout tableLayout = new TableLayout();
197         parametersTable.setLayout(tableLayout);
198         tableLayout.addColumnData(new ColumnWeightData(100));
199         tableLayout.addColumnData(new ColumnWeightData(100));
200         parametersTable.setHeaderVisible(true);
201         parametersTable.setLinesVisible(true);
202         parametersTable.addSelectionListener(fListener);
203         parametersTable.addMouseListener(new MouseAdapter() {
204             public void mouseDoubleClick(MouseEvent e) {
205                 setParametersButtonsEnableState();
206                 if (fParametersEditButton.isEnabled()) {
207                     handleParametersEditButtonSelected();
208                 }
209             }
210         });
211         
212         fViewer.setContentProvider(new IStructuredContentProvider() {
213             public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
214                 Map JavaDoc params = (Map JavaDoc) inputElement;
215                 return params.keySet().toArray();
216             }
217             public void dispose() {
218             }
219             public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
220             }
221         });
222     
223         fViewer.setLabelProvider(new ITableLabelProvider() {
224             public Image getColumnImage(Object JavaDoc element, int columnIndex) {
225                 return null;
226             }
227             public String JavaDoc getColumnText(Object JavaDoc element, int columnIndex) {
228                 if (columnIndex == 0) {
229                     return element.toString();
230                 }
231
232                 String JavaDoc key = (String JavaDoc) element;
233                 Map JavaDoc params = (Map JavaDoc) fViewer.getInput();
234                 Object JavaDoc object = params.get(key);
235                 if (object != null)
236                     return object.toString();
237                 return null;
238             }
239             public void addListener(ILabelProviderListener listener) {
240             }
241             public void dispose() {
242             }
243             public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {
244                 return false;
245             }
246             public void removeListener(ILabelProviderListener listener) {
247             }
248         });
249         
250         fViewer.setComparator(new ViewerComparator());
251         
252         Composite envButtonComp = new Composite(parametersComp, SWT.NONE);
253         GridLayout envButtonLayout = new GridLayout();
254         envButtonLayout.marginHeight = 0;
255         envButtonLayout.marginWidth = 0;
256         envButtonComp.setLayout(envButtonLayout);
257         gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_FILL);
258         envButtonComp.setLayoutData(gd);
259         
260         fParametersAddButton = createPushButton(envButtonComp ,LauncherMessages.appletlauncher_argumenttab_parameters_button_add_text, null);
261         fParametersAddButton.addSelectionListener(fListener);
262         
263         fParametersEditButton = createPushButton(envButtonComp, LauncherMessages.appletlauncher_argumenttab_parameters_button_edit_text, null);
264         fParametersEditButton.addSelectionListener(fListener);
265         
266         fParametersRemoveButton = createPushButton(envButtonComp, LauncherMessages.appletlauncher_argumenttab_parameters_button_remove_text, null);
267         fParametersRemoveButton.addSelectionListener(fListener);
268         
269         Dialog.applyDialogFont(parent);
270     }
271
272         
273     /**
274      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#isValid(ILaunchConfiguration)
275      */

276     public boolean isValid(ILaunchConfiguration launchConfig) {
277         setErrorMessage(null);
278         try {
279             Integer.parseInt(getWidthText());
280         } catch(NumberFormatException JavaDoc nfe) {
281             setErrorMessage(LauncherMessages.appletlauncher_argumenttab_width_error_notaninteger);
282             return false;
283         }
284         try {
285             Integer.parseInt(getHeightText());
286         } catch(NumberFormatException JavaDoc nfe) {
287             setErrorMessage(LauncherMessages.appletlauncher_argumenttab_height_error_notaninteger);
288             return false;
289         }
290         return true;
291     }
292
293     private void handleParametersAddButtonSelected() {
294         NameValuePairDialog dialog =
295             new NameValuePairDialog(getShell(),
296                 LauncherMessages.appletlauncher_argumenttab_parameters_dialog_add_title,
297                 new String JavaDoc[] {LauncherMessages.appletlauncher_argumenttab_parameters_dialog_add_name_text, LauncherMessages.appletlauncher_argumenttab_parameters_dialog_add_value_text}, //
298
new String JavaDoc[] {EMPTY_STRING, EMPTY_STRING});
299         openNewParameterDialog(dialog, null);
300         setParametersButtonsEnableState();
301     }
302
303     private void handleParametersEditButtonSelected() {
304         IStructuredSelection selection = (IStructuredSelection) fViewer.getSelection();
305         String JavaDoc key = (String JavaDoc) selection.getFirstElement();
306         Map JavaDoc params = (Map JavaDoc) fViewer.getInput();
307         String JavaDoc value = (String JavaDoc) params.get(key);
308         
309         NameValuePairDialog dialog =
310             new NameValuePairDialog(getShell(),
311                 LauncherMessages.appletlauncher_argumenttab_parameters_dialog_edit_title,
312                 new String JavaDoc[] {LauncherMessages.appletlauncher_argumenttab_parameters_dialog_edit_name_text, LauncherMessages.appletlauncher_argumenttab_parameters_dialog_edit_value_text}, //
313
new String JavaDoc[] {key, value});
314         
315         openNewParameterDialog(dialog, key);
316     }
317
318     private void handleParametersRemoveButtonSelected() {
319         IStructuredSelection selection = (IStructuredSelection) fViewer.getSelection();
320         Object JavaDoc[] keys = selection.toArray();
321         for (int i = 0; i < keys.length; i++) {
322             String JavaDoc key = (String JavaDoc) keys[i];
323             Map JavaDoc params = (Map JavaDoc) fViewer.getInput();
324             params.remove(key);
325         }
326         fViewer.refresh();
327         setParametersButtonsEnableState();
328         updateLaunchConfigurationDialog();
329     }
330
331     /**
332      * Set the enabled state of the three environment variable-related buttons based on the
333      * selection in the Table widget.
334      */

335     private void setParametersButtonsEnableState() {
336         IStructuredSelection selection = (IStructuredSelection) fViewer.getSelection();
337         int selectCount = selection.size();
338         if (selectCount < 1) {
339             fParametersEditButton.setEnabled(false);
340             fParametersRemoveButton.setEnabled(false);
341         } else {
342             fParametersRemoveButton.setEnabled(true);
343             if (selectCount == 1) {
344                 fParametersEditButton.setEnabled(true);
345             } else {
346                 fParametersEditButton.setEnabled(false);
347             }
348         }
349         fParametersAddButton.setEnabled(true);
350     }
351
352     /**
353      * Show the specified dialog and update the parameter table based on its results.
354      *
355      * @param updateItem the item to update, or <code>null</code> if
356      * adding a new item
357      */

358     private void openNewParameterDialog(NameValuePairDialog dialog, String JavaDoc key) {
359         if (dialog.open() != Window.OK) {
360             return;
361         }
362         String JavaDoc[] nameValuePair = dialog.getNameValuePair();
363         Map JavaDoc params = (Map JavaDoc) fViewer.getInput();
364         params.remove(key);
365         params.put(nameValuePair[0], nameValuePair[1]);
366         fViewer.refresh();
367         updateLaunchConfigurationDialog();
368     }
369     
370     /**
371      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(ILaunchConfigurationWorkingCopy)
372      */

373     public void performApply(ILaunchConfigurationWorkingCopy configuration) {
374         try {
375             configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_WIDTH, Integer.parseInt(getWidthText()));
376         } catch (NumberFormatException JavaDoc e) {
377         }
378         try {
379             configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_HEIGHT, Integer.parseInt(getHeightText()));
380         } catch (NumberFormatException JavaDoc e) {
381         }
382         configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_NAME, fNameText.getText());
383         configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_PARAMETERS, (Map JavaDoc) fViewer.getInput());
384     }
385
386     /**
387      * Returns the current width specified by the user
388      * @return the width specified by the user
389      */

390     private String JavaDoc getWidthText() {
391         return fWidthText.getText().trim();
392     }
393     
394     /**
395      * Returns the current height specified by the user
396      * @return the height specified by the user
397      */

398     private String JavaDoc getHeightText() {
399         return fHeightText.getText().trim();
400     }
401
402     /**
403      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(ILaunchConfigurationWorkingCopy)
404      */

405     public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
406     }
407     
408
409     /**
410      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(ILaunchConfiguration)
411      */

412     public void initializeFrom(ILaunchConfiguration config) {
413         try {
414             fWidthText.setText(Integer.toString(config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_WIDTH, DEFAULT_APPLET_WIDTH)));
415         } catch(CoreException ce) {
416             fWidthText.setText(Integer.toString(DEFAULT_APPLET_WIDTH));
417         }
418         try {
419             fHeightText.setText(Integer.toString(config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_HEIGHT, DEFAULT_APPLET_HEIGHT)));
420         } catch(CoreException ce) {
421             fHeightText.setText(Integer.toString(DEFAULT_APPLET_HEIGHT));
422         }
423         try {
424             fNameText.setText(config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_NAME, LauncherMessages.appletlauncher_argumenttab_name_defaultvalue));
425         } catch(CoreException ce) {
426             fNameText.setText(LauncherMessages.appletlauncher_argumenttab_name_defaultvalue);
427         }
428         
429         Map JavaDoc input = new HashMap JavaDoc();
430         try {
431              Map JavaDoc params = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_PARAMETERS, (Map JavaDoc) null);
432              if (params != null)
433                  input.putAll(params);
434         } catch (CoreException e) {
435         }
436         
437         fViewer.setInput(input);
438     }
439     
440     /**
441      * Create some empty space
442      */

443     private void createVerticalSpacer(Composite comp) {
444         new Label(comp, SWT.NONE);
445     }
446
447     /**
448      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName()
449      */

450     public String JavaDoc getName() {
451         return LauncherMessages.appletlauncher_argumenttab_name;
452     }
453     
454     /**
455      * @see org.eclipse.debug.ui.AbstractLaunchConfigurationTab#getId()
456      *
457      * @since 3.3
458      */

459     public String JavaDoc getId() {
460         return "org.eclipse.jdt.debug.ui.appletParametersTab"; //$NON-NLS-1$
461
}
462     
463     /**
464      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getImage()
465      */

466     public Image getImage() {
467         return JavaDebugImages.get(JavaDebugImages.IMG_VIEW_ARGUMENTS_TAB);
468     }
469     
470     /* (non-Javadoc)
471      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#activated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
472      */

473     public void activated(ILaunchConfigurationWorkingCopy workingCopy) {
474         // do nothing when activated
475
}
476
477     /* (non-Javadoc)
478      * @see org.eclipse.debug.ui.ILaunchConfigurationTab#deactivated(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy)
479      */

480     public void deactivated(ILaunchConfigurationWorkingCopy workingCopy) {
481         // do nothing when deactivated
482
}
483 }
484
485
Popular Tags