1 11 package org.eclipse.jdt.debug.ui.launchConfigurations; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 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 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 78 public void modifyText(ModifyEvent e) { 79 updateLaunchConfigurationDialog(); 80 } 81 82 85 public void widgetSelected(SelectionEvent e) { 86 Object 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 EMPTY_STRING = ""; 104 107 public static final int DEFAULT_APPLET_WIDTH = 200; 108 109 112 public static final int DEFAULT_APPLET_HEIGHT = 200; 113 114 117 private TableViewer fViewer; 118 119 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 [] getElements(Object inputElement) { 214 Map params = (Map ) inputElement; 215 return params.keySet().toArray(); 216 } 217 public void dispose() { 218 } 219 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 220 } 221 }); 222 223 fViewer.setLabelProvider(new ITableLabelProvider() { 224 public Image getColumnImage(Object element, int columnIndex) { 225 return null; 226 } 227 public String getColumnText(Object element, int columnIndex) { 228 if (columnIndex == 0) { 229 return element.toString(); 230 } 231 232 String key = (String ) element; 233 Map params = (Map ) fViewer.getInput(); 234 Object 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 element, String 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 276 public boolean isValid(ILaunchConfiguration launchConfig) { 277 setErrorMessage(null); 278 try { 279 Integer.parseInt(getWidthText()); 280 } catch(NumberFormatException nfe) { 281 setErrorMessage(LauncherMessages.appletlauncher_argumenttab_width_error_notaninteger); 282 return false; 283 } 284 try { 285 Integer.parseInt(getHeightText()); 286 } catch(NumberFormatException 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 [] {LauncherMessages.appletlauncher_argumenttab_parameters_dialog_add_name_text, LauncherMessages.appletlauncher_argumenttab_parameters_dialog_add_value_text}, new String [] {EMPTY_STRING, EMPTY_STRING}); 299 openNewParameterDialog(dialog, null); 300 setParametersButtonsEnableState(); 301 } 302 303 private void handleParametersEditButtonSelected() { 304 IStructuredSelection selection = (IStructuredSelection) fViewer.getSelection(); 305 String key = (String ) selection.getFirstElement(); 306 Map params = (Map ) fViewer.getInput(); 307 String value = (String ) params.get(key); 308 309 NameValuePairDialog dialog = 310 new NameValuePairDialog(getShell(), 311 LauncherMessages.appletlauncher_argumenttab_parameters_dialog_edit_title, 312 new String [] {LauncherMessages.appletlauncher_argumenttab_parameters_dialog_edit_name_text, LauncherMessages.appletlauncher_argumenttab_parameters_dialog_edit_value_text}, new String [] {key, value}); 314 315 openNewParameterDialog(dialog, key); 316 } 317 318 private void handleParametersRemoveButtonSelected() { 319 IStructuredSelection selection = (IStructuredSelection) fViewer.getSelection(); 320 Object [] keys = selection.toArray(); 321 for (int i = 0; i < keys.length; i++) { 322 String key = (String ) keys[i]; 323 Map params = (Map ) fViewer.getInput(); 324 params.remove(key); 325 } 326 fViewer.refresh(); 327 setParametersButtonsEnableState(); 328 updateLaunchConfigurationDialog(); 329 } 330 331 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 358 private void openNewParameterDialog(NameValuePairDialog dialog, String key) { 359 if (dialog.open() != Window.OK) { 360 return; 361 } 362 String [] nameValuePair = dialog.getNameValuePair(); 363 Map params = (Map ) fViewer.getInput(); 364 params.remove(key); 365 params.put(nameValuePair[0], nameValuePair[1]); 366 fViewer.refresh(); 367 updateLaunchConfigurationDialog(); 368 } 369 370 373 public void performApply(ILaunchConfigurationWorkingCopy configuration) { 374 try { 375 configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_WIDTH, Integer.parseInt(getWidthText())); 376 } catch (NumberFormatException e) { 377 } 378 try { 379 configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_HEIGHT, Integer.parseInt(getHeightText())); 380 } catch (NumberFormatException e) { 381 } 382 configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_NAME, fNameText.getText()); 383 configuration.setAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_PARAMETERS, (Map ) fViewer.getInput()); 384 } 385 386 390 private String getWidthText() { 391 return fWidthText.getText().trim(); 392 } 393 394 398 private String getHeightText() { 399 return fHeightText.getText().trim(); 400 } 401 402 405 public void setDefaults(ILaunchConfigurationWorkingCopy configuration) { 406 } 407 408 409 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 input = new HashMap (); 430 try { 431 Map params = config.getAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_PARAMETERS, (Map ) null); 432 if (params != null) 433 input.putAll(params); 434 } catch (CoreException e) { 435 } 436 437 fViewer.setInput(input); 438 } 439 440 443 private void createVerticalSpacer(Composite comp) { 444 new Label(comp, SWT.NONE); 445 } 446 447 450 public String getName() { 451 return LauncherMessages.appletlauncher_argumenttab_name; 452 } 453 454 459 public String getId() { 460 return "org.eclipse.jdt.debug.ui.appletParametersTab"; } 462 463 466 public Image getImage() { 467 return JavaDebugImages.get(JavaDebugImages.IMG_VIEW_ARGUMENTS_TAB); 468 } 469 470 473 public void activated(ILaunchConfigurationWorkingCopy workingCopy) { 474 } 476 477 480 public void deactivated(ILaunchConfigurationWorkingCopy workingCopy) { 481 } 483 } 484 485 | Popular Tags |