1 11 12 package org.eclipse.pde.internal.ui.editor.cheatsheet.simple.details; 13 14 import java.util.Iterator ; 15 import java.util.Map ; 16 17 import org.eclipse.core.commands.ParameterizedCommand; 18 import org.eclipse.core.commands.SerializationException; 19 import org.eclipse.core.commands.common.NotDefinedException; 20 import org.eclipse.core.expressions.IEvaluationContext; 21 import org.eclipse.jface.fieldassist.ControlDecoration; 22 import org.eclipse.jface.fieldassist.FieldDecorationRegistry; 23 import org.eclipse.jface.window.Window; 24 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSCommand; 25 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSConstants; 26 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSItem; 27 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSRun; 28 import org.eclipse.pde.internal.core.icheatsheet.simple.ISimpleCSRunContainerObject; 29 import org.eclipse.pde.internal.core.util.PDETextHelper; 30 import org.eclipse.pde.internal.ui.PDEPlugin; 31 import org.eclipse.pde.internal.ui.PDEUIMessages; 32 import org.eclipse.pde.internal.ui.commands.CommandComposerDialog; 33 import org.eclipse.pde.internal.ui.commands.CommandComposerPart; 34 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory; 35 import org.eclipse.pde.internal.ui.editor.cheatsheet.CSAbstractSubDetails; 36 import org.eclipse.pde.internal.ui.editor.cheatsheet.ICSMaster; 37 import org.eclipse.pde.internal.ui.editor.cheatsheet.simple.SimpleCSInputContext; 38 import org.eclipse.swt.SWT; 39 import org.eclipse.swt.events.SelectionAdapter; 40 import org.eclipse.swt.events.SelectionEvent; 41 import org.eclipse.swt.graphics.Color; 42 import org.eclipse.swt.layout.GridData; 43 import org.eclipse.swt.widgets.Button; 44 import org.eclipse.swt.widgets.Composite; 45 import org.eclipse.swt.widgets.Label; 46 import org.eclipse.swt.widgets.Table; 47 import org.eclipse.swt.widgets.TableColumn; 48 import org.eclipse.swt.widgets.TableItem; 49 import org.eclipse.ui.IWorkbench; 50 import org.eclipse.ui.PlatformUI; 51 import org.eclipse.ui.commands.ICommandService; 52 import org.eclipse.ui.forms.IFormColors; 53 import org.eclipse.ui.forms.widgets.ExpandableComposite; 54 import org.eclipse.ui.forms.widgets.FormToolkit; 55 import org.eclipse.ui.forms.widgets.Section; 56 import org.eclipse.ui.handlers.IHandlerService; 57 import org.eclipse.ui.internal.handlers.HandlerService; 58 59 63 public class SimpleCSCommandDetails extends CSAbstractSubDetails { 64 65 private ISimpleCSRun fRun; 66 67 private Table fCommandTable; 68 69 private SimpleCSCommandComboPart fCommandCombo; 70 71 private ControlDecoration fCommandInfoDecoration; 72 73 private Button fCommandBrowse; 74 75 private static final String F_NO_COMMAND = PDEUIMessages.SimpleCSCommandDetails_6; 76 77 private static final int F_COMMAND_INSERTION_INDEX = 1; 78 79 82 public SimpleCSCommandDetails(ICSMaster section) { 83 super(section, SimpleCSInputContext.CONTEXT_ID); 84 fRun = null; 85 86 fCommandTable = null; 87 fCommandCombo = null; 88 fCommandInfoDecoration = null; 89 fCommandBrowse = null; 90 } 91 92 95 public void setData(ISimpleCSRun object) { 96 fRun = object; 98 } 99 100 103 public void createDetails(Composite parent) { 104 105 int columnSpan = 3; 106 Section commandSection = null; 107 FormToolkit toolkit = getToolkit(); 108 Color foreground = toolkit.getColors().getColor(IFormColors.TITLE); 109 GridData data = null; 110 Label label = null; 111 112 commandSection = toolkit.createSection(parent, Section.DESCRIPTION | ExpandableComposite.TITLE_BAR); 114 commandSection.clientVerticalSpacing = FormLayoutFactory.SECTION_HEADER_VERTICAL_SPACING; 115 commandSection.setText(PDEUIMessages.SimpleCSItemDetails_5); 116 commandSection.setDescription(PDEUIMessages.SimpleCSItemDetails_6); 117 commandSection.setLayout(FormLayoutFactory.createClearGridLayout(false, 1)); 118 data = new GridData(GridData.FILL_HORIZONTAL); 119 commandSection.setLayoutData(data); 120 121 Composite commandSectionClient = toolkit.createComposite(commandSection); 123 commandSectionClient.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, columnSpan)); 124 125 label = toolkit.createLabel(commandSectionClient, PDEUIMessages.SimpleCSItemDetails_7, SWT.WRAP); 128 label.setForeground(foreground); 129 fCommandCombo = new SimpleCSCommandComboPart(); 131 fCommandCombo.createControl(commandSectionClient, toolkit, SWT.READ_ONLY); 132 data = new GridData(GridData.FILL_HORIZONTAL); 133 data.horizontalIndent = FormLayoutFactory.CONTROL_HORIZONTAL_INDENT; 134 fCommandCombo.getControl().setLayoutData(data); 135 fCommandCombo.add(F_NO_COMMAND); 138 fCommandCombo.setText(F_NO_COMMAND); 139 fCommandCombo.populate(); 140 fCommandCombo.setNewCommandKeyIndex(F_COMMAND_INSERTION_INDEX); 143 fCommandCombo.setComboEntryLimit(11); 146 147 createCommandInfoDecoration(); 148 fCommandBrowse = toolkit.createButton(commandSectionClient, PDEUIMessages.GeneralInfoSection_browse, SWT.PUSH); 150 151 label = toolkit.createLabel(commandSectionClient, PDEUIMessages.SimpleCSItemDetails_8, SWT.WRAP); 154 label.setForeground(foreground); 155 data = new GridData(GridData.FILL_HORIZONTAL); 156 data.horizontalSpan = columnSpan; 157 label.setLayoutData(data); 158 159 fCommandTable = toolkit.createTable(commandSectionClient, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION); 160 data = new GridData(GridData.FILL_HORIZONTAL); 161 data.heightHint = 25; 162 data.horizontalSpan = columnSpan; 163 fCommandTable.setLayoutData(data); 164 fCommandTable.setLinesVisible(true); 166 TableColumn tableColumn1 = new TableColumn(fCommandTable, SWT.LEFT); 168 tableColumn1.setText(PDEUIMessages.SimpleCSItemDetails_9); 169 TableColumn tableColumn2 = new TableColumn(fCommandTable, SWT.LEFT); 170 tableColumn2.setText(PDEUIMessages.SimpleCSItemDetails_10); 171 172 173 toolkit.paintBordersFor(commandSectionClient); 175 commandSection.setClient(commandSectionClient); 176 markDetailsPart(commandSection); 178 } 179 180 183 private void createCommandInfoDecoration() { 184 int bits = SWT.TOP | SWT.LEFT; 186 fCommandInfoDecoration = 187 new ControlDecoration(fCommandCombo.getControl(), bits); 188 fCommandInfoDecoration.setMarginWidth(1); 189 fCommandInfoDecoration.setDescriptionText(PDEUIMessages.SimpleCSCommandDetails_msgFieldDisabledCommand); 190 updateCommandInfoDecoration(false); 191 fCommandInfoDecoration.setImage( 192 FieldDecorationRegistry.getDefault().getFieldDecoration( 193 FieldDecorationRegistry.DEC_INFORMATION).getImage()); 194 } 195 196 199 public void hookListeners() { 200 201 fCommandCombo.addSelectionListener(new SelectionAdapter() { 203 public void widgetSelected(SelectionEvent e) { 204 if (fRun == null) { 206 return; 207 } 208 String selection = fCommandCombo.getSelection(); 209 if (selection.equals(F_NO_COMMAND) == false) { 210 String serialization = fCommandCombo.getValue(selection); 213 if (PDETextHelper.isDefined(serialization)) { 214 createCommandInModel(serialization); 216 217 ParameterizedCommand result = 218 getParameterizedCommand(serialization); 219 if (result != null) { 220 updateCommandTable(result.getParameterMap()); 221 } 222 } 223 } else { 224 fRun.setExecutable(null); 227 fCommandTable.clearAll(); 228 } 229 getMasterSection().updateButtons(); 231 } 232 }); 233 234 fCommandBrowse.addSelectionListener(new SelectionAdapter() { 235 public void widgetSelected(SelectionEvent e) { 236 if (fRun == null) { 238 return; 239 } 240 CommandComposerDialog dialog = new CommandComposerDialog( 243 fCommandBrowse.getShell(), 244 CommandComposerPart.F_CHEATSHEET_FILTER, 245 getParameterizedCommand(fRun), 246 getSnapshotContext()); 247 if (dialog.open() == Window.OK) { 249 updateCommandCombo(dialog.getCommand(), true); 252 getMasterSection().updateButtons(); 254 } 255 } 256 }); 257 258 } 259 260 263 public void updateFields() { 264 if (fRun == null) { 266 return; 267 } 268 ParameterizedCommand command = getParameterizedCommand(fRun); 270 if (command == null) { 271 clearCommandUI(); 274 } else { 275 updateCommandCombo(command, false); 276 } 277 updateCommandEnablement(); 278 } 279 280 283 private void clearCommandUI() { 284 fCommandCombo.setText(F_NO_COMMAND); 286 fCommandTable.clearAll(); 288 } 289 290 293 private void updateCommandEnablement() { 294 if (fRun == null) { 296 return; 297 } 298 boolean editable = isEditableElement(); 299 300 if (fRun.getType() == ISimpleCSConstants.TYPE_ITEM) { 301 ISimpleCSItem item = (ISimpleCSItem)fRun; 302 if (item.hasSubItems()) { 306 editable = false; 307 updateCommandInfoDecoration(true); 308 } else { 309 updateCommandInfoDecoration(false); 310 } 311 } 312 313 fCommandCombo.setEnabled(editable); 314 fCommandTable.setEnabled(true); 315 fCommandBrowse.setEnabled(editable); 316 317 } 318 319 322 private void createCommandInModel(String serialization) { 323 if (fRun == null) { 325 return; 326 } 327 ISimpleCSCommand command = 328 fRun.getModel().getFactory().createSimpleCSCommand(fRun); 329 command.setSerialization(serialization); 330 fRun.setExecutable(command); 331 } 332 333 337 private void updateCommandCombo(ParameterizedCommand result, 338 boolean createInModel) { 339 340 if (result == null) { 341 return; 342 } 343 String serialization = result.serialize(); 345 String commandName = null; 347 try { 348 commandName = result.getCommand().getName(); 349 } catch (NotDefinedException e) { 350 } 352 String commandId = result.getId(); 354 355 if (PDETextHelper.isDefined(serialization) 356 && PDETextHelper.isDefined(commandId)) { 357 if (createInModel) { 358 createCommandInModel(serialization); 360 } 361 String nameToUse = null; 364 if (PDETextHelper.isDefined(commandName)) { 365 nameToUse = commandName; 366 } else { 367 nameToUse = commandId; 368 } 369 fCommandCombo.putValue(nameToUse, serialization, 373 F_COMMAND_INSERTION_INDEX); 374 fCommandCombo.setText(nameToUse); 376 updateCommandTable(result.getParameterMap()); 378 } else { 379 fCommandCombo.setText(F_NO_COMMAND); 381 } 382 383 } 384 385 389 private ParameterizedCommand getParameterizedCommand(String serialization) { 390 if (PDETextHelper.isDefined(serialization)) { 391 ICommandService service = getCommandService(); 392 if (service != null) { 393 try { 394 return service.deserialize(serialization); 395 } catch (NotDefinedException e) { 396 PDEPlugin.logException(e, 397 PDEUIMessages.SimpleCSCommandDetails_DFErrorTitle, 398 PDEUIMessages.SimpleCSCommandDetails_DFErrorBody 399 + serialization); 400 } catch (SerializationException e) { 401 PDEPlugin.logException(e, 402 PDEUIMessages.SimpleCSCommandDetails_DFErrorTitle, 403 PDEUIMessages.SimpleCSCommandDetails_DFErrorBody 404 + serialization); 405 } 406 } 407 } 408 return null; 409 } 410 411 415 private ParameterizedCommand getParameterizedCommand(ISimpleCSRun run) { 416 if (run == null) { 417 return null; 418 } 419 ISimpleCSRunContainerObject object = run.getExecutable(); 420 if ((object != null) && 421 (object.getType() == ISimpleCSConstants.TYPE_COMMAND)) { 422 ISimpleCSCommand command = (ISimpleCSCommand)object; 423 return getParameterizedCommand(command.getSerialization()); 424 } 425 return null; 426 } 427 428 431 private void updateCommandTable(Map parameters) { 432 fCommandTable.clearAll(); 434 435 if ((parameters != null) && 436 (parameters.isEmpty() == false)) { 437 Iterator it = parameters.keySet().iterator(); 439 int rowCount = 0; 440 while (it.hasNext()) { 441 TableItem item = null; 443 if (rowCount < fCommandTable.getItemCount()) { 445 item = fCommandTable.getItem(rowCount); 447 } else { 448 item = new TableItem (fCommandTable, SWT.NONE); 450 } 451 Object key = it.next(); 453 if (key instanceof String ) { 454 String keyString = (String )key; 455 int dotIndex = keyString.lastIndexOf('.'); 460 if ((dotIndex != -1) && 461 (dotIndex != (keyString.length() - 1))) { 462 keyString = keyString.substring(dotIndex + 1); 463 } 464 item.setText(0, keyString); 466 } 467 Object value = parameters.get(key); 468 if (value instanceof String ) { 469 item.setText(1, (String )value); 471 } 472 rowCount++; 473 } 474 for (int i = 0; i < fCommandTable.getColumnCount(); i++) { 476 TableColumn tableColumn = fCommandTable.getColumn(i); 477 tableColumn.pack(); 478 } 479 } 480 } 481 482 485 private static ICommandService getCommandService() { 486 IWorkbench workbench = PlatformUI.getWorkbench(); 487 return (ICommandService)workbench.getAdapter(ICommandService.class); 488 } 489 490 493 private static IHandlerService getGlobalHandlerService() { 494 return (IHandlerService) PlatformUI.getWorkbench().getService( 495 IHandlerService.class); 496 } 497 498 501 private static IEvaluationContext getSnapshotContext() { 502 IHandlerService service = getGlobalHandlerService(); 503 if (service instanceof HandlerService) { 505 return ((HandlerService)service).getContextSnapshot(); 506 } 507 return null; 508 } 509 510 513 private void updateCommandInfoDecoration(boolean showDecoration) { 514 if (showDecoration) { 515 fCommandInfoDecoration.show(); 516 } else { 517 fCommandInfoDecoration.hide(); 518 } 519 fCommandInfoDecoration.setShowHover(showDecoration); 520 } 521 522 525 public void commit(boolean onSave) { 526 super.commit(onSave); 527 } 530 } 531 | Popular Tags |