1 11 package org.eclipse.pde.internal.ui.commands; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 18 import org.eclipse.core.commands.AbstractParameterValueConverter; 19 import org.eclipse.core.commands.Command; 20 import org.eclipse.core.commands.IParameter; 21 import org.eclipse.core.commands.IParameterValues; 22 import org.eclipse.core.commands.ParameterType; 23 import org.eclipse.core.commands.ParameterValueConversionException; 24 import org.eclipse.core.commands.ParameterValuesException; 25 import org.eclipse.core.commands.Parameterization; 26 import org.eclipse.core.commands.ParameterizedCommand; 27 import org.eclipse.core.commands.common.CommandException; 28 import org.eclipse.core.commands.common.NotDefinedException; 29 import org.eclipse.core.expressions.IEvaluationContext; 30 import org.eclipse.jface.dialogs.IMessageProvider; 31 import org.eclipse.jface.dialogs.MessageDialog; 32 import org.eclipse.osgi.util.NLS; 33 import org.eclipse.pde.internal.ui.PDEPluginImages; 34 import org.eclipse.pde.internal.ui.PDEUIMessages; 35 import org.eclipse.swt.SWT; 36 import org.eclipse.swt.dnd.Clipboard; 37 import org.eclipse.swt.dnd.DND; 38 import org.eclipse.swt.dnd.TextTransfer; 39 import org.eclipse.swt.dnd.Transfer; 40 import org.eclipse.swt.events.DisposeEvent; 41 import org.eclipse.swt.events.DisposeListener; 42 import org.eclipse.swt.events.ModifyEvent; 43 import org.eclipse.swt.events.ModifyListener; 44 import org.eclipse.swt.events.SelectionAdapter; 45 import org.eclipse.swt.events.SelectionEvent; 46 import org.eclipse.swt.graphics.Image; 47 import org.eclipse.swt.layout.GridData; 48 import org.eclipse.swt.layout.GridLayout; 49 import org.eclipse.swt.widgets.Button; 50 import org.eclipse.swt.widgets.Combo; 51 import org.eclipse.swt.widgets.Composite; 52 import org.eclipse.swt.widgets.Label; 53 import org.eclipse.swt.widgets.Text; 54 import org.eclipse.ui.PlatformUI; 55 import org.eclipse.ui.forms.events.HyperlinkAdapter; 56 import org.eclipse.ui.forms.events.HyperlinkEvent; 57 import org.eclipse.ui.forms.widgets.ExpandableComposite; 58 import org.eclipse.ui.forms.widgets.FormToolkit; 59 import org.eclipse.ui.forms.widgets.ImageHyperlink; 60 import org.eclipse.ui.forms.widgets.Section; 61 import org.eclipse.ui.handlers.IHandlerService; 62 import org.eclipse.ui.internal.handlers.HandlerService; 63 64 public class CommandDetails { 65 66 private final HashMap fParameterToValue = new HashMap (); 67 private final ArrayList fObjectParamList = new ArrayList (); 68 private final ArrayList fValueParamList = new ArrayList (); 69 private final ArrayList fTextParamList = new ArrayList (); 70 71 private CommandComposerPart fCCP; 72 private FormToolkit fToolkit; 73 private Command fSelectedCommand; 74 private ParameterizedCommand fPreSel; 75 76 private Text fComIDT; 77 private Text fComPrev; 78 private Button fSurroundCopyText; 79 private Combo fFilterCombo; 80 private Composite fParamComposite; 81 private Composite fParamParent; 82 private Label fParamLabel; 83 private ImageHyperlink fCopyLink; 84 private ImageHyperlink fExecLink; 85 86 public CommandDetails(CommandComposerPart cv, Composite parent) { 87 fCCP = cv; 88 fToolkit = cv.getToolkit(); 89 createCommandDetails(parent); 90 } 91 92 private void createCommandDetails(Composite parent) { 93 Composite c = fCCP.createComposite(parent, GridData.FILL_BOTH, 1, true, 5); 94 95 Section section = fToolkit.createSection(c, ExpandableComposite.TITLE_BAR); 96 section.setText(PDEUIMessages.CommandDetails_groupName); 97 section.setLayoutData(new GridData(GridData.FILL_BOTH)); 98 99 Composite comp = fCCP.createComposite(section); 100 101 createBasicInfo(comp); 102 103 if (fCCP.getFilterType() == CommandComposerPart.F_FILTER_NOT_SET) 104 createPreviewLabelComp(comp); 105 createParameters(comp); 106 107 section.setClient(comp); 108 109 createLinks(c); 110 } 111 112 private void createBasicInfo(Composite parent) { 113 Composite comp = fCCP.createComposite(parent, GridData.FILL_HORIZONTAL, 2, false, 0); 114 fToolkit.createLabel(comp, PDEUIMessages.CommandDetails_id); 115 fComIDT = fToolkit.createText(comp, PDEUIMessages.CommandDetails_noComSelected, SWT.BORDER); 116 fComIDT.setEditable(false); 117 fComIDT.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 118 } 119 120 private void createParameters(Composite parent) { 121 Composite comp = fCCP.createComposite(parent, GridData.FILL_HORIZONTAL, 1, false, 0); 122 123 fParamLabel = fToolkit.createLabel(comp, PDEUIMessages.CommandDetails_noParameters); 124 fParamLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 125 126 fParamParent = parent; 127 createBlankParamComp(); 128 } 129 130 private void createLinks(Composite parent) { 131 Composite comp = fCCP.createComposite(parent, 132 GridData.FILL_HORIZONTAL | GridData.HORIZONTAL_ALIGN_END, 133 1, false, 0); 134 135 fExecLink = fToolkit.createImageHyperlink(comp, SWT.NONE); 136 final Image execImage = PDEPluginImages.DESC_RUN_EXC.createImage(); 137 fExecLink.setImage(execImage); 138 fExecLink.setText(PDEUIMessages.CommandDetails_executeText); 139 fExecLink.setToolTipText(PDEUIMessages.CommandDetails_execute); 140 fExecLink.addHyperlinkListener(new ExecCommand()); 141 fExecLink.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END)); 142 fExecLink.setVisible(false); 143 fExecLink.addDisposeListener(new DisposeListener() 144 { public void widgetDisposed(DisposeEvent e) { execImage.dispose(); } }); 145 146 fCopyLink = fToolkit.createImageHyperlink(comp, SWT.NONE); 147 final Image clipImage = PDEPluginImages.DESC_CLIPBOARD.createImage(); 148 fCopyLink.setImage(clipImage); 149 fCopyLink.setText(PDEUIMessages.CommandDetails_copyToClipboard); 150 fCopyLink.setToolTipText(PDEUIMessages.CommandDetails_copytooltip); 151 fCopyLink.addHyperlinkListener(new CopyToClipboard()); 152 fCopyLink.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END)); 153 fCopyLink.setVisible(false); 154 fCopyLink.addDisposeListener(new DisposeListener() 155 { public void widgetDisposed(DisposeEvent e) { clipImage.dispose(); }} ); 156 } 157 158 private void createPreviewLabelComp(Composite parent) { 159 Composite preLabelComp = fCCP.createComposite(parent, GridData.FILL_HORIZONTAL, 3, false, 0); 160 fToolkit.createLabel(preLabelComp, PDEUIMessages.CommandDetails_preview, SWT.NONE).setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 161 162 fSurroundCopyText = fToolkit.createButton(preLabelComp, PDEUIMessages.CommandDetails_includeMarkup, SWT.CHECK); 163 fSurroundCopyText.setToolTipText(PDEUIMessages.CommandDetails_markupTooltip); 164 fSurroundCopyText.addSelectionListener(new SelectionAdapter() { 165 public void widgetSelected(SelectionEvent e) { 166 updatePreviewText(); 167 } 168 }); 169 170 fFilterCombo = new Combo(preLabelComp, SWT.READ_ONLY | SWT.DROP_DOWN); 171 CommandCopyFilter[] filters = CommandCopyFilter.getFilters(); 172 for (int i = 0; i < filters.length; i++) 173 fFilterCombo.add(filters[i].getLabelText()); 174 fFilterCombo.select(CommandCopyFilter.indexOf(CommandCopyFilter.NONE)); 175 fFilterCombo.addSelectionListener(new SelectionAdapter() { 176 public void widgetSelected(SelectionEvent e) { 177 updatePreviewText(); 178 } 179 }); 180 fToolkit.adapt(fFilterCombo, true, true); 181 182 fComPrev = fToolkit.createText(parent, "", SWT.MULTI | SWT.V_SCROLL | SWT.BORDER | SWT.WRAP); GridData gd = new GridData(GridData.FILL_HORIZONTAL); 184 gd.heightHint = 60; 185 fComPrev.setLayoutData(gd); 186 fComPrev.setEditable(false); 187 } 188 189 protected ParameterizedCommand buildParameterizedCommand() { 190 191 ArrayList list = new ArrayList (); 192 for (Iterator i = fParameterToValue.keySet().iterator(); i.hasNext(); ) { 193 IParameter parameter = (IParameter)i.next(); 194 String value = (String )fParameterToValue.get(parameter); 195 list.add(new Parameterization(parameter, value)); 196 } 197 Parameterization[] parameterizations = (Parameterization[]) list.toArray(new Parameterization[list.size()]); 198 199 ParameterizedCommand pCommand = new ParameterizedCommand(fSelectedCommand, parameterizations); 200 return pCommand; 201 } 202 203 private class ExecCommand extends HyperlinkAdapter { 204 public void linkActivated(HyperlinkEvent e) { 205 ParameterizedCommand pCommand = buildParameterizedCommand(); 206 try { 207 Object obj = null; 208 IHandlerService service = getGlobalHandlerService(); 209 IEvaluationContext context = fCCP.getSnapshotContext(); 210 if ((service instanceof HandlerService) && 212 (context != null)) { 213 obj = ((HandlerService) service).executeCommandInContext( 214 pCommand, null, context); 215 } else { 216 obj = service.executeCommand(pCommand, null); 219 } 220 String resultString = null; 221 if (obj instanceof String ) { 222 resultString = (String )obj; 223 } else { 224 ParameterType returnType = pCommand.getCommand().getReturnType(); 225 if (returnType != null && returnType.getValueConverter() != null) 226 resultString = returnType.getValueConverter().convertToString(obj); 227 } 228 if (resultString != null) { 229 MessageDialog.openInformation( 230 fComIDT.getShell(), 231 PDEUIMessages.CommandDetails_commandResult, 232 resultString); 233 } 234 } catch (CommandException ex) { 235 MessageDialog.openError( 236 fComIDT.getShell(), 237 PDEUIMessages.CommandDetails_execError, 238 ex.toString()); 239 } 240 } 241 } 242 243 private String getFilteredCommand() { 244 ParameterizedCommand pCommand = buildParameterizedCommand(); 245 String serializedCommand = pCommand.serialize(); 246 String markupLabel; 247 try { 248 markupLabel = fSelectedCommand.getName(); 249 } catch (NotDefinedException ex) { 250 markupLabel = null; 251 } 252 int index; 253 boolean surroundWithMarkup = false; 254 if (fCCP.getFilterType() == CommandComposerPart.F_FILTER_NOT_SET) { 255 surroundWithMarkup = fSurroundCopyText.getSelection(); 256 index = fFilterCombo.getSelectionIndex(); 257 } else { 258 index = fCCP.getFilterType(); 259 } 260 CommandCopyFilter ccf = CommandCopyFilter.getFilter(index); 261 return ccf.filter(serializedCommand, surroundWithMarkup, markupLabel); 262 } 263 264 private class CopyToClipboard extends HyperlinkAdapter { 265 public void linkActivated(HyperlinkEvent e) { 266 String filteredCommand = getFilteredCommand(); 267 268 Object [] data = new Object [] { filteredCommand, }; 269 Transfer[] transfers = new Transfer[] { TextTransfer.getInstance(), }; 270 271 Clipboard clipboard = new Clipboard(null); 272 clipboard.setContents(data, transfers, DND.CLIPBOARD); 273 clipboard.dispose(); 274 } 275 } 276 277 private class ValuesParameterControl extends SelectionAdapter implements ModifyListener { 278 private final IParameter fParameter; 279 private final Map fValues; 280 private final Combo fValuesCombo; 281 private final Button fClearButton; 282 public ValuesParameterControl(IParameter parameter, Map values, Combo valuesCombo, Button clearButton) { 283 fParameter = parameter; 284 fValues = values; 285 286 fValuesCombo = valuesCombo; 287 fValuesCombo.addModifyListener(this); 288 if (fPreSel != null && fValues != null) { 289 Object obj = fPreSel.getParameterMap().get(parameter.getId()); 290 if (obj != null) { 291 for (Iterator i = fValues.keySet().iterator(); i.hasNext();) { 292 Object next = i.next(); 293 if (obj.equals(fValues.get(next))) { 294 fValuesCombo.setText(next.toString()); 295 break; 296 } 297 } 298 } 299 } 300 301 fClearButton = clearButton; 302 fClearButton.addSelectionListener(this); 303 } 304 305 public void widgetSelected(SelectionEvent e) { 307 fValuesCombo.deselectAll(); 308 fParameterToValue.remove(fParameter); 309 } 310 311 public void modifyText(ModifyEvent e) { 313 String key = fValuesCombo.getText(); 314 String value = (String )fValues.get(key); 315 if (value == null) 316 fParameterToValue.remove(fParameter); 317 else 318 fParameterToValue.put(fParameter, value); 319 updatePreviewText(); 320 } 321 322 protected void dispose() { 323 if (!fValuesCombo.isDisposed()) 324 fValuesCombo.removeModifyListener(this); 325 if (!fClearButton.isDisposed()) 326 fClearButton.removeSelectionListener(this); 327 } 328 } 329 330 private class ObjectParameterControl implements ModifyListener { 331 private final IParameter fParameter; 332 private final AbstractParameterValueConverter fValueConverter; 333 private final Text fParameterText; 334 public ObjectParameterControl(IParameter parameter, AbstractParameterValueConverter valueConverter, Text parameterText, Object selectedObject) { 335 fParameter = parameter; 336 fValueConverter = valueConverter; 337 338 fParameterText = parameterText; 339 fParameterText.addModifyListener(this); 340 341 if (selectedObject != null) 342 setParameterText(selectedObject); 343 344 if (fPreSel != null) { 345 Object obj = fPreSel.getParameterMap().get(parameter.getId()); 346 if (obj != null) 347 fParameterText.setText(obj.toString()); 348 } 349 } 350 351 public void modifyText(ModifyEvent e) { 352 String text = fParameterText.getText(); 353 if ((text == null) || (text.trim().equals(""))) fParameterToValue.remove(fParameter); 355 else 356 fParameterToValue.put(fParameter, text); 357 updatePreviewText(); 358 validate(); 359 } 360 361 private void setParameterText(Object selectedObject) { 362 try { 363 String converted = fValueConverter.convertToString(selectedObject); 364 if (converted != null) 365 fParameterText.setText(converted); 366 } catch (ParameterValueConversionException ex) { 367 } 369 } 370 371 protected void dispose() { 372 if (!fParameterText.isDisposed()) 373 fParameterText.removeModifyListener(this); 374 } 375 376 private void validate() { 377 String text = fParameterText.getText(); 378 String error = null; 379 if (text.length() > 0) { 380 try { 381 fValueConverter.convertToObject(text); 382 } catch (ParameterValueConversionException e1) { 383 error = e1.getMessage(); 384 } 385 } 386 if (error == null) 387 fCCP.setMessage(null, IMessageProvider.NONE); 388 else 389 fCCP.setMessage( 390 NLS.bind(PDEUIMessages.CommandDetails_paramValueMessage, fParameter.getName(), error), 391 IMessageProvider.WARNING); 392 } 393 } 394 395 private class TextParameterControl implements ModifyListener { 396 private final IParameter fParameter; 397 private final Text fParameterText; 398 public TextParameterControl(IParameter parameter, Text parameterText) { 399 fParameter = parameter; 400 fParameterText = parameterText; 401 fParameterText.addModifyListener(this); 402 403 if (fPreSel != null) { 404 Object obj = fPreSel.getParameterMap().get(parameter.getId()); 405 if (obj != null) 406 fParameterText.setText(obj.toString()); 407 } 408 } 409 public void modifyText(ModifyEvent e) { 410 String text = fParameterText.getText(); 411 if ((text == null) || (text.trim().equals(""))) fParameterToValue.remove(fParameter); 413 else 414 fParameterToValue.put(fParameter, text); 415 updatePreviewText(); 416 } 417 public void dispose() { 418 if (!fParameterText.isDisposed()) 419 fParameterText.removeModifyListener(this); 420 } 421 } 422 423 protected void dispose() { 424 for (int i = 0; i < fObjectParamList.size(); i++) 425 ((ObjectParameterControl)fObjectParamList.get(i)).dispose(); 426 for (int i = 0; i < fValueParamList.size(); i++) 427 ((ValuesParameterControl)fValueParamList.get(i)).dispose(); 428 for (int i = 0; i < fTextParamList.size(); i++) 429 ((TextParameterControl)fTextParamList.get(i)).dispose(); 430 } 431 432 private void populateParams(Command command, Object selectedObject) throws NotDefinedException { 433 434 createBlankParamComp(); 435 436 IParameter[] parameters = command.getParameters(); 437 if (parameters == null || parameters.length == 0) { 438 fParamLabel.setText(PDEUIMessages.CommandDetails_noParameters); 439 } else { 440 fParamLabel.setText(PDEUIMessages.CommandDetails_numParams); 441 Composite paramLine = fToolkit.createComposite(fParamComposite); 442 443 GridLayout paramLineLayout = new GridLayout(); 444 paramLine.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 445 paramLineLayout.numColumns = 3; 446 paramLineLayout.marginHeight = 0; 447 paramLine.setLayout(paramLineLayout); 448 for (int i = 0; i < parameters.length; i++) { 449 IParameter parameter = parameters[i]; 450 451 String nameText = parameter.getName(); 452 if (!parameter.isOptional()) nameText += '*'; 453 fToolkit.createLabel(paramLine, NLS.bind(PDEUIMessages.CommandDetails_param, nameText)); 454 455 IParameterValues parameterValues = getParameterValues(parameter); 456 if (parameterValues != null) { 457 Combo parameterValuesCombo = new Combo(paramLine, SWT.READ_ONLY | SWT.DROP_DOWN); 458 parameterValuesCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 459 fToolkit.adapt(parameterValuesCombo, true, true); 460 461 Map values = parameterValues.getParameterValues(); 462 for (Iterator keys = values.keySet().iterator(); keys.hasNext();) 463 parameterValuesCombo.add((String ) keys.next()); 464 465 Button clearButton = fToolkit.createButton(paramLine, PDEUIMessages.CommandDetails_clear, SWT.PUSH); 466 467 fValueParamList.add(new ValuesParameterControl(parameter, values, parameterValuesCombo, clearButton)); 468 469 continue; 470 } 471 472 ParameterType parameterType = command.getParameterType(parameter.getId()); 473 if ((parameterType != null) && (parameterType.getValueConverter() != null)) { 474 Text parameterText = fToolkit.createText(paramLine, "", SWT.SINGLE | SWT.BORDER); GridData gd = new GridData(GridData.FILL_HORIZONTAL); 476 gd.horizontalSpan = 2; 477 parameterText.setLayoutData(gd); 478 fObjectParamList.add(new ObjectParameterControl(parameter, parameterType.getValueConverter(), parameterText, selectedObject)); 479 480 continue; 481 } 482 483 Text parameterText = fToolkit.createText(paramLine, "", SWT.SINGLE | SWT.BORDER); GridData gd = new GridData(GridData.FILL_HORIZONTAL); 485 gd.horizontalSpan = 2; 486 parameterText.setLayoutData(gd); 487 fTextParamList.add(new TextParameterControl(parameter, parameterText)); 488 } 489 } 490 fPreSel = null; 492 fParamParent.layout(); 493 } 494 495 496 private IParameterValues getParameterValues(IParameter parameter) { 497 try { 498 return parameter.getValues(); 499 } catch (ParameterValuesException ex) { 500 return null; 501 } 502 } 503 504 public void showDetailsFor(Object object) { 505 if (object instanceof ParameterizedCommand) 506 object = (fPreSel = (ParameterizedCommand)object).getCommand(); 507 508 if (!(object instanceof Command)) { 509 resetAllFields(); 510 return; 511 } 512 fSelectedCommand = (Command)object; 513 fComIDT.setText(fSelectedCommand.getId()); 514 515 fParameterToValue.clear(); 516 fObjectParamList.clear(); 517 fValueParamList.clear(); 518 519 fExecLink.setVisible(fSelectedCommand.isEnabled()); 520 fCopyLink.setVisible(true); 521 try { 522 populateParams(fSelectedCommand, object); 523 } catch (NotDefinedException e) { 524 createNoParamComp(); 525 } 526 updatePreviewText(); 527 } 528 529 private void resetAllFields() { 530 fSelectedCommand = null; 531 fComIDT.setText(PDEUIMessages.CommandDetails_noComSelected); 532 fParamLabel.setText(PDEUIMessages.CommandDetails_noParameters); 533 534 if (fComPrev != null) 535 fComPrev.setText(""); 537 fExecLink.setVisible(false); 538 fCopyLink.setVisible(false); 539 540 fParameterToValue.clear(); 541 fObjectParamList.clear(); 542 fValueParamList.clear(); 543 544 createNoParamComp(); 545 } 546 547 private void createNoParamComp() { 548 createBlankParamComp(); 549 fParamParent.layout(); 550 } 551 552 private void createBlankParamComp() { 553 if (fParamComposite != null) 554 fParamComposite.dispose(); 555 fParamComposite = fCCP.createComposite(fParamParent, GridData.FILL_BOTH, 1, true, 0); 556 } 557 558 private void updatePreviewText() { 559 if (fComPrev != null) 560 fComPrev.setText(getFilteredCommand()); 561 } 562 563 protected Command getCommand() { 564 return fSelectedCommand; 565 } 566 567 public String getCommandName() { 568 if (fSelectedCommand != null) 569 try { 570 return fSelectedCommand.getName(); 571 } catch (NotDefinedException e) { 572 return fSelectedCommand.getId(); 573 } 574 return null; 575 } 576 577 public String getSerializedString() { 578 if (fSelectedCommand != null) 579 return getFilteredCommand(); 580 return null; 581 } 582 583 public HashMap getParameters() { 584 if (fSelectedCommand != null) 585 return fParameterToValue; 586 587 return null; 588 } 589 590 593 private IHandlerService getGlobalHandlerService() { 594 return (IHandlerService) PlatformUI.getWorkbench().getService( 595 IHandlerService.class); 596 } 597 } 598 | Popular Tags |