KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > cheatsheet > simple > details > SimpleCSCommandDetails


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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
12 package org.eclipse.pde.internal.ui.editor.cheatsheet.simple.details;
13
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
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 /**
60  * SimpleCSCommandDetailsSection
61  *
62  */

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 JavaDoc F_NO_COMMAND = PDEUIMessages.SimpleCSCommandDetails_6;
76     
77     private static final int F_COMMAND_INSERTION_INDEX = 1;
78     
79     /**
80      * @param section
81      */

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     /* (non-Javadoc)
93      * @see org.eclipse.pde.internal.ui.editor.cheatsheet.CSAbstractDetails#setData(java.lang.Object)
94      */

95     public void setData(ISimpleCSRun object) {
96         // Set data
97
fRun = object;
98     }
99     
100     /* (non-Javadoc)
101      * @see org.eclipse.pde.internal.ui.editor.cheatsheet.simple.details.ISimpleCSDetails#createDetails(org.eclipse.swt.widgets.Composite)
102      */

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         // Create command section
113
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         // Create container for command section
122
Composite commandSectionClient = toolkit.createComposite(commandSection);
123         commandSectionClient.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, columnSpan));
124
125         // Element: command
126
// Label
127
label = toolkit.createLabel(commandSectionClient, PDEUIMessages.SimpleCSItemDetails_7, SWT.WRAP);
128         label.setForeground(foreground);
129         // Combo box
130
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         // Insertion index is 0 for no command combo box entry
136
// Always keep this entry as the first entry
137
fCommandCombo.add(F_NO_COMMAND);
138         fCommandCombo.setText(F_NO_COMMAND);
139         fCommandCombo.populate();
140         // Always insert new command keys obtained from other combo boxes in
141
// the position after the no command entry
142
fCommandCombo.setNewCommandKeyIndex(F_COMMAND_INSERTION_INDEX);
143         // Limit the combo box to the 11 most recent entries (includes no
144
// command entry)
145
fCommandCombo.setComboEntryLimit(11);
146         
147         createCommandInfoDecoration();
148         // Button
149
fCommandBrowse = toolkit.createButton(commandSectionClient, PDEUIMessages.GeneralInfoSection_browse, SWT.PUSH);
150         
151         // Element: command
152
// Label for parameters
153
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.setHeaderVisible(true);
165
fCommandTable.setLinesVisible(true);
166         //fCommandTable.setForeground(foreground);
167
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         // Bind widgets
174
toolkit.paintBordersFor(commandSectionClient);
175         commandSection.setClient(commandSectionClient);
176         // Mark as a details part to enable cut, copy, paste, etc.
177
markDetailsPart(commandSection);
178     }
179
180     /**
181      * @param label
182      */

183     private void createCommandInfoDecoration() {
184         // Command info decoration
185
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     /* (non-Javadoc)
197      * @see org.eclipse.pde.internal.ui.editor.cheatsheet.simple.details.ISimpleCSDetails#hookListeners()
198      */

199     public void hookListeners() {
200
201         // Element: command
202
fCommandCombo.addSelectionListener(new SelectionAdapter() {
203             public void widgetSelected(SelectionEvent e) {
204                 // Ensure data object is defined
205
if (fRun == null) {
206                     return;
207                 }
208                 String JavaDoc selection = fCommandCombo.getSelection();
209                 if (selection.equals(F_NO_COMMAND) == false) {
210                     // Get the associated serialization stored as data against the
211
// command name
212
String JavaDoc serialization = fCommandCombo.getValue(selection);
213                     if (PDETextHelper.isDefined(serialization)) {
214                         // Create the new command in the model
215
createCommandInModel(serialization);
216                         
217                         ParameterizedCommand result =
218                             getParameterizedCommand(serialization);
219                         if (result != null) {
220                             updateCommandTable(result.getParameterMap());
221                         }
222                     }
223                 } else {
224                     // The empty entry was selected
225
// Delete the existing command
226
fRun.setExecutable(null);
227                     fCommandTable.clearAll();
228                 }
229                 // Update the master section buttons
230
getMasterSection().updateButtons();
231             }
232         });
233         
234         fCommandBrowse.addSelectionListener(new SelectionAdapter() {
235             public void widgetSelected(SelectionEvent e) {
236                 // Ensure data object is defined
237
if (fRun == null) {
238                     return;
239                 }
240                 // Open the command composer dialog using the input from the
241
// currently selected command
242
CommandComposerDialog dialog = new CommandComposerDialog(
243                         fCommandBrowse.getShell(),
244                         CommandComposerPart.F_CHEATSHEET_FILTER,
245                         getParameterizedCommand(fRun),
246                         getSnapshotContext());
247                 // Check result of dialog
248
if (dialog.open() == Window.OK) {
249                     // Command composer exited successfully
250
// Update accordingly
251
updateCommandCombo(dialog.getCommand(), true);
252                     // Update the master section buttons
253
getMasterSection().updateButtons();
254                 }
255             }
256         });
257         
258     }
259     
260     /* (non-Javadoc)
261      * @see org.eclipse.pde.internal.ui.editor.cheatsheet.simple.details.ISimpleCSDetails#updateFields()
262      */

263     public void updateFields() {
264         // Ensure data object is defined
265
if (fRun == null) {
266             return;
267         }
268         // i.e. Action: class
269
ParameterizedCommand command = getParameterizedCommand(fRun);
270         if (command == null) {
271             // Since, this page is static the command combo and command table
272
// must be reset
273
clearCommandUI();
274         } else {
275             updateCommandCombo(command, false);
276         }
277         updateCommandEnablement();
278     }
279
280     /**
281      *
282      */

283     private void clearCommandUI() {
284         // Clear the command combo
285
fCommandCombo.setText(F_NO_COMMAND);
286         // Clear the command table
287
fCommandTable.clearAll();
288     }
289     
290     /**
291      *
292      */

293     private void updateCommandEnablement() {
294         // Ensure data object is defined
295
if (fRun == null) {
296             return;
297         }
298         boolean editable = isEditableElement();
299         
300         if (fRun.getType() == ISimpleCSConstants.TYPE_ITEM) {
301             ISimpleCSItem item = (ISimpleCSItem)fRun;
302             // Preserve cheat sheet validity
303
// Semantic Rule: Cannot have a subitem and any of the following
304
// together: perform-when, command, action
305
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     /**
320      * @param serialization
321      */

322     private void createCommandInModel(String JavaDoc serialization) {
323         // Ensure data object is defined
324
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     /**
334      * @param result
335      * @param createInModel
336      */

337     private void updateCommandCombo(ParameterizedCommand result,
338             boolean createInModel) {
339
340         if (result == null) {
341             return;
342         }
343         // Get serialization
344
String JavaDoc serialization = result.serialize();
345         // Get presentable command name
346
String JavaDoc commandName = null;
347         try {
348             commandName = result.getCommand().getName();
349         } catch (NotDefinedException e) {
350             // Ignore, name will be undefined
351
}
352         // Get command ID
353
String JavaDoc commandId = result.getId();
354
355         if (PDETextHelper.isDefined(serialization)
356                 && PDETextHelper.isDefined(commandId)) {
357             if (createInModel) {
358                 // Create the new command in the model
359
createCommandInModel(serialization);
360             }
361             // Determine the presentable name to use in the combo box and the
362
// key to store the serialization data against in the widget
363
String JavaDoc nameToUse = null;
364             if (PDETextHelper.isDefined(commandName)) {
365                 nameToUse = commandName;
366             } else {
367                 nameToUse = commandId;
368             }
369             // Add new selection to the combo box if it is not already there
370
// Associate the serialization with the command name
371
// in the widget to retrieve for later use
372
fCommandCombo.putValue(nameToUse, serialization,
373                     F_COMMAND_INSERTION_INDEX);
374             // Select it
375
fCommandCombo.setText(nameToUse);
376             // Update the command table parameters
377
updateCommandTable(result.getParameterMap());
378         } else {
379             // No serialization, something bad happened
380
fCommandCombo.setText(F_NO_COMMAND);
381         }
382
383     }
384     
385     /**
386      * @param serialization
387      * @return
388      */

389     private ParameterizedCommand getParameterizedCommand(String JavaDoc 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     /**
412      * @param run
413      * @return
414      */

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     /**
429      * @param parameters
430      */

431     private void updateCommandTable(Map JavaDoc parameters) {
432         // Clear the table contents
433
fCommandTable.clearAll();
434         
435         if ((parameters != null) &&
436                 (parameters.isEmpty() == false)) {
437             // Iterate over the keys in the map
438
Iterator JavaDoc it = parameters.keySet().iterator();
439             int rowCount = 0;
440             while (it.hasNext()) {
441                 // Track number of keys / rows processed
442
TableItem item = null;
443                 // Determine if there is an existing row already at that index
444
if (rowCount < fCommandTable.getItemCount()) {
445                     // There is, reuse it
446
item = fCommandTable.getItem(rowCount);
447                 } else {
448                     // There isn't, create a new one
449
item = new TableItem (fCommandTable, SWT.NONE);
450                 }
451                 // Get key
452
Object JavaDoc key = it.next();
453                 if (key instanceof String JavaDoc) {
454                     String JavaDoc keyString = (String JavaDoc)key;
455                     // If present, remove the fully qualified ID from the
456
// paramater key
457
// i.e. "org.eclipse.ui.perspective" becomes just
458
// "perspective"
459
int dotIndex = keyString.lastIndexOf('.');
460                     if ((dotIndex != -1) &&
461                             (dotIndex != (keyString.length() - 1))) {
462                         keyString = keyString.substring(dotIndex + 1);
463                     }
464                     // Set parameter key in first column
465
item.setText(0, keyString);
466                 }
467                 Object JavaDoc value = parameters.get(key);
468                 if (value instanceof String JavaDoc) {
469                     // Set parameter value in second column
470
item.setText(1, (String JavaDoc)value);
471                 }
472                 rowCount++;
473             }
474             // Pack the columns with the new data
475
for (int i = 0; i < fCommandTable.getColumnCount(); i++) {
476                 TableColumn tableColumn = fCommandTable.getColumn(i);
477                 tableColumn.pack();
478             }
479         }
480     }
481
482     /**
483      * @return
484      */

485     private static ICommandService getCommandService() {
486         IWorkbench workbench = PlatformUI.getWorkbench();
487         return (ICommandService)workbench.getAdapter(ICommandService.class);
488     }
489     
490     /**
491      * @return
492      */

493     private static IHandlerService getGlobalHandlerService() {
494         return (IHandlerService) PlatformUI.getWorkbench().getService(
495                 IHandlerService.class);
496     }
497     
498     /**
499      * @return
500      */

501     private static IEvaluationContext getSnapshotContext() {
502         IHandlerService service = getGlobalHandlerService();
503         // TODO: MP: SimpleCS: Get rid of internal class use when context snapshots are made API
504
if (service instanceof HandlerService) {
505             return ((HandlerService)service).getContextSnapshot();
506         }
507         return null;
508     }
509     
510     /**
511      *
512      */

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     /* (non-Javadoc)
523      * @see org.eclipse.ui.forms.AbstractFormPart#commit(boolean)
524      */

525     public void commit(boolean onSave) {
526         super.commit(onSave);
527         // NO-OP
528
// No form entries
529
}
530 }
531
Popular Tags