KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > views > CoreItem


1 /*******************************************************************************
2  * Copyright (c) 2002, 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 package org.eclipse.ui.internal.cheatsheets.views;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.StringTokenizer JavaDoc;
16
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.osgi.util.NLS;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.graphics.Color;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.Text;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.forms.events.HyperlinkAdapter;
31 import org.eclipse.ui.forms.events.HyperlinkEvent;
32 import org.eclipse.ui.forms.widgets.FormText;
33 import org.eclipse.ui.forms.widgets.FormToolkit;
34 import org.eclipse.ui.forms.widgets.ImageHyperlink;
35 import org.eclipse.ui.forms.widgets.TableWrapData;
36 import org.eclipse.ui.forms.widgets.TableWrapLayout;
37 import org.eclipse.ui.internal.cheatsheets.CheatSheetPlugin;
38 import org.eclipse.ui.internal.cheatsheets.ICheatSheetResource;
39 import org.eclipse.ui.internal.cheatsheets.Messages;
40 import org.eclipse.ui.internal.cheatsheets.data.AbstractExecutable;
41 import org.eclipse.ui.internal.cheatsheets.data.AbstractSubItem;
42 import org.eclipse.ui.internal.cheatsheets.data.ConditionalSubItem;
43 import org.eclipse.ui.internal.cheatsheets.data.IParserTags;
44 import org.eclipse.ui.internal.cheatsheets.data.Item;
45 import org.eclipse.ui.internal.cheatsheets.data.RepeatedSubItem;
46 import org.eclipse.ui.internal.cheatsheets.data.SubItem;
47
48 public class CoreItem extends ViewItem {
49
50     protected boolean buttonsHandled = false;
51
52     private static final int SUBITEM_COLUMNS = 6;
53
54     /**
55      * Constructor for CoreItem.
56      * @param parent
57      * @param contentItem
58      */

59     public CoreItem(CheatSheetPage page, Item item, Color itemColor, CheatSheetViewer viewer) {
60         super(page, item, itemColor, viewer);
61     }
62
63     private void createButtonComposite() {
64         buttonComposite = page.getToolkit().createComposite(bodyWrapperComposite);
65         GridLayout buttonlayout = new GridLayout(1, false);
66         buttonlayout.marginHeight = 2;
67         buttonlayout.marginWidth = 2;
68         buttonlayout.verticalSpacing = 2;
69
70         TableWrapData buttonData = new TableWrapData(TableWrapData.FILL);
71
72         buttonComposite.setLayout(buttonlayout);
73         buttonComposite.setLayoutData(buttonData);
74         buttonComposite.setBackground(itemColor);
75
76     }
77
78     private void createButtons(AbstractExecutable executable) {
79         /*
80          * Actions are disabled while inside dialogs.
81          */

82         boolean isActionShown = false;
83         if (executable != null && !isInDialogMode()) {
84             isActionShown = true;
85             final ImageHyperlink startButton = createButtonWithText(buttonComposite, CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.CHEATSHEET_ITEM_BUTTON_START), this, itemColor, Messages.PERFORM_TASK_TOOLTIP);
86             startButton.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
87             startButton.addHyperlinkListener(new HyperlinkAdapter() {
88                 public void linkActivated(HyperlinkEvent e) {
89                     viewer.runPerformExecutable(startButton);
90                 }
91             });
92         }
93         if (!isActionShown || executable.isConfirm() || !executable.isRequired()) {
94             final ImageHyperlink completeButton = createButtonWithText(buttonComposite, CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.CHEATSHEET_ITEM_BUTTON_COMPLETE), this, itemColor, Messages.COMPLETE_TASK_TOOLTIP);
95             completeButton.addHyperlinkListener(new HyperlinkAdapter() {
96                 public void linkActivated(HyperlinkEvent e) {
97                     viewer.advanceItem(completeButton, true);
98                 }
99             });
100         }
101         if (item.isSkip()) {
102             final ImageHyperlink skipButton = createButtonWithText(buttonComposite, CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.CHEATSHEET_ITEM_SKIP), this, itemColor, Messages.SKIP_TASK_TOOLTIP);
103             skipButton.addHyperlinkListener(new HyperlinkAdapter() {
104                 public void linkActivated(HyperlinkEvent e) {
105                     viewer.advanceItem(skipButton, false);
106                 }
107             });
108         }
109     }
110
111     private void createSubItemButtonComposite() {
112         buttonComposite = page.getToolkit().createComposite(bodyWrapperComposite);
113         
114         TableWrapLayout xbuttonlayout = new TableWrapLayout();
115         xbuttonlayout.numColumns = SUBITEM_COLUMNS;
116         xbuttonlayout.leftMargin = 0;
117         xbuttonlayout.rightMargin = 0;
118         xbuttonlayout.horizontalSpacing = 0;
119
120         TableWrapData xbuttonData = new TableWrapData(TableWrapData.FILL);
121
122         buttonComposite.setLayout(xbuttonlayout);
123         buttonComposite.setLayoutData(xbuttonData);
124         buttonComposite.setBackground(itemColor);
125     }
126
127     private void createSubItemButtons(SubItem sub, String JavaDoc thisValue, int index) {
128         int added = 0;
129         if (index != 0) {
130             addSeparator();
131         }
132         final int LABEL_MARGIN = 5; // space to the left and right of the label
133
SubItemCompositeHolder holder = new SubItemCompositeHolder(sub);
134         
135         //Spacer label added.
136
Label checkDoneLabel = page.getToolkit().createLabel(buttonComposite, null);
137         checkDoneLabel.setImage(CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.CHEATSHEET_ITEM_COMPLETE));
138         checkDoneLabel.setVisible(false);
139         checkDoneLabel.setBackground(itemColor);
140         holder.setCheckDoneLabel(checkDoneLabel);
141         added++;
142
143         //Now add the label.
144
String JavaDoc labelText = null;
145         if( thisValue != null ) {
146             labelText = performLineSubstitution(sub.getLabel(), "${this}", thisValue); //$NON-NLS-1$
147
} else {
148             labelText = sub.getLabel();
149         }
150         Text subitemLabel = new Text(buttonComposite, SWT.READ_ONLY + SWT.WRAP);
151         subitemLabel.setText(labelText);
152         TableWrapData labelData = new TableWrapData();
153         labelData.indent = LABEL_MARGIN;
154         subitemLabel.setLayoutData(labelData);
155         subitemLabel.setBackground(itemColor);
156         holder.setSubitemLabel(subitemLabel);
157         added++;
158         
159         // Add some space to the right of the label
160

161         Label spacer = page.getToolkit().createLabel(buttonComposite, null);
162         TableWrapData spacerData = new TableWrapData();
163         spacerData.maxWidth = 0;
164         spacerData.indent = LABEL_MARGIN;
165         spacer.setLayoutData(spacerData);
166         added++;
167
168         AbstractExecutable subExecutable = null;
169         if(sub.getPerformWhen() != null) {
170             sub.getPerformWhen().setSelectedExecutable(viewer.getManager());
171             subExecutable = sub.getPerformWhen().getSelectedExecutable();
172         } else {
173             subExecutable = sub.getExecutable();
174         }
175         
176         /*
177          * Actions are disabled while inside dialogs.
178          */

179         final int fi = index;
180         ImageHyperlink startButton = null;
181         boolean isActionShown = false;
182         if (subExecutable != null && !isInDialogMode()) {
183             added++;
184             isActionShown = true;
185             startButton = createButton(buttonComposite, CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.CHEATSHEET_ITEM_BUTTON_START), this, itemColor, Messages.PERFORM_TASK_TOOLTIP);
186             final ImageHyperlink finalStartButton = startButton;
187             startButton.addHyperlinkListener(new HyperlinkAdapter() {
188                 public void linkActivated(HyperlinkEvent e) {
189                     viewer.runSubItemPerformExecutable(finalStartButton, fi);
190                 }
191             });
192             holder.setStartButton(startButton);
193         }
194         if (!isActionShown || subExecutable.isConfirm() || !subExecutable.isRequired()) {
195             added++;
196             final ImageHyperlink completeButton = createButton(buttonComposite, CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.CHEATSHEET_ITEM_BUTTON_COMPLETE), this, itemColor, Messages.COMPLETE_TASK_TOOLTIP);
197             completeButton.addHyperlinkListener(new HyperlinkAdapter() {
198                 public void linkActivated(HyperlinkEvent e) {
199                     viewer.advanceSubItem(completeButton, true, fi);
200                 }
201             });
202             holder.setCompleteButton(completeButton);
203         }
204         if (sub.isSkip()) {
205             added++;
206             final ImageHyperlink skipButton = createButton(buttonComposite, CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.CHEATSHEET_ITEM_SKIP), this, itemColor, Messages.SKIP_TASK_TOOLTIP);
207             skipButton.addHyperlinkListener(new HyperlinkAdapter() {
208                 public void linkActivated(HyperlinkEvent e) {
209                     viewer.advanceSubItem(skipButton, false, fi);
210                 }
211             });
212             holder.setSkipButton(skipButton);
213         }
214
215         while (added < SUBITEM_COLUMNS) {
216             // Add filler labels as needed to complete the row
217
Label filler = page.getToolkit().createLabel(buttonComposite, null);
218             TableWrapData fillerData = new TableWrapData();
219             fillerData.maxWidth = 0;
220             filler.setLayoutData(fillerData);
221             added++;
222         }
223         holder.setThisValue(thisValue);
224         listOfSubItemCompositeHolders.add(holder);
225     }
226
227     private void addSeparator() {
228         Label pad = page.getToolkit().createLabel(buttonComposite, null);
229         TableWrapData padData = new TableWrapData();
230         padData.maxWidth = 0;
231         pad.setLayoutData(padData);
232         Label separator = new Label(buttonComposite, SWT.SEPARATOR + SWT.HORIZONTAL);
233         TableWrapData separatorData = new TableWrapData();
234         separatorData.align = TableWrapData.FILL;
235         separatorData.grabHorizontal = true;
236         separatorData.maxHeight = 1;
237         separatorData.valign = TableWrapData.MIDDLE;
238         separator.setLayoutData(separatorData);
239         for (int i = 3; i <= SUBITEM_COLUMNS; i++) {
240             Label filler = page.getToolkit().createLabel(buttonComposite, null);
241             TableWrapData fillerData = new TableWrapData();
242             fillerData.maxWidth = 0;
243             filler.setLayoutData(fillerData);
244         }
245     }
246
247     private AbstractExecutable getExecutable() {
248         AbstractExecutable executable = item.getExecutable();
249         if(executable == null) {
250             if(item.getPerformWhen() != null){
251                 executable = item.getPerformWhen().getSelectedExecutable();
252             }
253         }
254         return executable;
255     }
256
257     private AbstractExecutable getExecutable(int index) {
258         if (item.getSubItems() != null && item.getSubItems().size()>0 && listOfSubItemCompositeHolders != null) {
259             SubItemCompositeHolder s = (SubItemCompositeHolder) listOfSubItemCompositeHolders.get(index);
260             if(s != null) {
261                 SubItem subItem = s.getSubItem();
262                 AbstractExecutable executable = subItem.getExecutable();
263                 if(executable == null) {
264                     if(subItem.getPerformWhen() != null){
265                         executable = subItem.getPerformWhen().getSelectedExecutable();
266                     }
267                 }
268                 return executable;
269             }
270         }
271         return null;
272     }
273
274     public ArrayList JavaDoc getListOfSubItemCompositeHolders() {
275         return listOfSubItemCompositeHolders;
276     }
277
278     private ImageHyperlink getStartButton() {
279         if (item.getSubItems() != null && item.getSubItems().size() > 0) {
280             // Bug 137332 - don't look in items with subitems
281
return null;
282         }
283         if(buttonComposite != null) {
284             Control[] controls = buttonComposite.getChildren();
285             for (int i = 0; i < controls.length; i++) {
286                 Control control = controls[i];
287                 if(control instanceof ImageHyperlink) {
288                     String JavaDoc toolTipText = control.getToolTipText();
289                     if( toolTipText != null &&
290                         (toolTipText.equals(Messages.PERFORM_TASK_TOOLTIP) ||
291                          toolTipText.equals(Messages.RESTART_TASK_TOOLTIP))) {
292                         return (ImageHyperlink)control;
293                     }
294                 }
295             }
296         }
297         return null;
298     }
299
300     /**
301      * @see org.eclipse.ui.internal.cheatsheets.ViewItem#handleButtons()
302      */

303     /*package*/ void handleButtons() {
304         if(item.isDynamic()) {
305             handleDynamicButtons();
306             return;
307         } else if( item.getSubItems() != null && item.getSubItems().size() > 0) {
308             handleSubButtons();
309         }
310
311         if (buttonsHandled)
312             return;
313
314         createButtonComposite();
315         createButtons(item.getExecutable());
316         buttonsHandled = true;
317     }
318
319     private void handleDynamicButtons() {
320         if( item.getSubItems() != null && item.getSubItems().size() > 0 ) {
321             handleDynamicSubItemButtons();
322         } else if( item.getPerformWhen() != null ) {
323             handlePerformWhenButtons();
324         }
325     }
326
327     private void handleDynamicSubItemButtons() {
328         boolean refreshRequired = false;
329         if(buttonComposite != null) {
330             Control[] children = buttonComposite.getChildren();
331             for (int i = 0; i < children.length; i++) {
332                 Control control = children[i];
333                 control.dispose();
334             }
335             
336             refreshRequired = true;
337         } else {
338             createSubItemButtonComposite();
339         }
340
341         //Instantiate the list to store the sub item composites.
342
listOfSubItemCompositeHolders = new ArrayList JavaDoc(20);
343
344         //loop throught the number of sub items, make a new composite for each sub item.
345
//Add the spacer, the label, then the buttons that are applicable for each sub item.
346
int i=0;
347         for (Iterator JavaDoc iter = item.getSubItems().iterator(); iter.hasNext(); i++) {
348             AbstractSubItem subItem = (AbstractSubItem)iter.next();
349             if( subItem instanceof RepeatedSubItem ) {
350
351                 //Get the sub item to add.
352
RepeatedSubItem repeatedSubItem = (RepeatedSubItem)subItem;
353                 String JavaDoc values = repeatedSubItem.getValues();
354                 values = viewer.getManager().getVariableData(values);
355                 if(values == null || values.length() <= 0 || (values.startsWith("${") && values.endsWith("}"))) { //$NON-NLS-1$ //$NON-NLS-2$
356
String JavaDoc message = NLS.bind(Messages.ERROR_DATA_MISSING_LOG, (new Object JavaDoc[] {repeatedSubItem.getValues()}));
357                     IStatus status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, null);
358                     CheatSheetPlugin.getPlugin().getLog().log(status);
359
360                     status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, Messages.ERROR_DATA_MISSING, null);
361                     CheatSheetPlugin.getPlugin().getLog().log(status);
362                     org.eclipse.jface.dialogs.ErrorDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), null, null, status);
363                     break;
364                 }
365
366                 SubItem sub = (SubItem)repeatedSubItem.getSubItems().get(0);
367                 
368                 StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(values, ","); //$NON-NLS-1$
369
while(tokenizer.hasMoreTokens()) {
370                     String JavaDoc value = tokenizer.nextToken();
371                     createSubItemButtons(sub, value, i++);
372                 }
373                 
374                 // Decrement the counter by because the outer loop increments it prior to the next iteration
375
i--;
376             } else if( subItem instanceof ConditionalSubItem ) {
377                 //Get the sub item to add.
378
ConditionalSubItem sub = (ConditionalSubItem)subItem;
379
380                 sub.setSelectedSubItem(viewer.getManager());
381                 SubItem selectedSubItem = sub.getSelectedSubItem();
382
383                 if(selectedSubItem == null) {
384                     String JavaDoc message = NLS.bind(Messages.ERROR_CONDITIONAL_DATA_MISSING_LOG, (new Object JavaDoc[] {sub.getCondition(), getItem().getTitle()}));
385                     IStatus status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, null);
386                     CheatSheetPlugin.getPlugin().getLog().log(status);
387
388                     status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, Messages.ERROR_DATA_MISSING, null);
389                     CheatSheetPlugin.getPlugin().getLog().log(status);
390                     org.eclipse.jface.dialogs.ErrorDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), null, null, status);
391                     break;
392                 }
393
394                 createSubItemButtons(selectedSubItem, null, i);
395             } else if( subItem instanceof SubItem ) {
396                 createSubItemButtons((SubItem)subItem, null, i);
397             }
398         }
399
400         if(refreshRequired) {
401             refresh(buttonComposite);
402         }
403     }
404
405     private void handlePerformWhenButtons() {
406         boolean refreshRequired = false;
407
408         if(buttonComposite != null) {
409             Control[] controls = buttonComposite.getChildren();
410             for (int i = 0; i < controls.length; i++) {
411                 Control control = controls[i];
412                 if(control instanceof ImageHyperlink) {
413                     control.dispose();
414                 }
415             }
416             
417             refreshRequired = true;
418         } else {
419             createButtonComposite();
420         }
421
422         item.getPerformWhen().setSelectedExecutable(viewer.getManager());
423         AbstractExecutable performExecutable = item.getPerformWhen().getSelectedExecutable();
424
425         createButtons(performExecutable);
426         
427         if(refreshRequired) {
428             refresh(buttonComposite);
429         }
430     }
431
432     private void handleSubButtons() {
433         if (buttonsHandled)
434             return;
435         //Instantiate the list to store the sub item composites.
436
listOfSubItemCompositeHolders = new ArrayList JavaDoc(20);
437
438         ArrayList JavaDoc sublist = item.getSubItems();
439         
440         createSubItemButtonComposite();
441
442         //loop throught the number of sub items, make a new composite for each sub item.
443
//Add the spacer, the label, then the buttons that are applicable for each sub item.
444
for (int i = 0; i < sublist.size(); i++) {
445             createSubItemButtons((SubItem)sublist.get(i), null, i);
446         }
447         buttonsHandled = true;
448     }
449     
450     /*package*/
451     boolean hasConfirm() {
452         AbstractExecutable executable = getExecutable();
453
454         if (executable == null || executable.isConfirm()) {
455             return true;
456         }
457         return false;
458     }
459
460     /*package*/
461     boolean hasConfirm(int index) {
462         AbstractExecutable executable = getExecutable(index);
463
464         if (executable == null || executable.isConfirm()) {
465             return true;
466         }
467         return false;
468     }
469
470     public String JavaDoc performLineSubstitution(String JavaDoc line, String JavaDoc variable, String JavaDoc value) {
471         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(line.length());
472
473         StringDelimitedTokenizer tokenizer = new StringDelimitedTokenizer(line, variable);
474         boolean addValue = false;
475
476         while (tokenizer.hasMoreTokens()) {
477             if (addValue) {
478                 buffer.append(value);
479             }
480             buffer.append(tokenizer.nextToken());
481             addValue = true;
482         }
483         if (tokenizer.endsWithDelimiter()) {
484             buffer.append(value);
485         }
486
487         return buffer.toString();
488     }
489     
490     /*package*/
491     IStatus runExecutable(CheatSheetManager csm) {
492         return runExecutable(getExecutable(), csm);
493     }
494     
495     IStatus runExecutable(AbstractExecutable executable, CheatSheetManager csm) {
496         if(executable != null) {
497             return executable.execute(csm);
498         }
499         return Status.OK_STATUS;
500     }
501
502     /*package*/
503     byte runSubItemExecutable(CheatSheetManager csm, int index) {
504         if (item.getSubItems() != null && item.getSubItems().size()>0 && listOfSubItemCompositeHolders != null) {
505             SubItemCompositeHolder s = (SubItemCompositeHolder) listOfSubItemCompositeHolders.get(index);
506             if(s != null) {
507                 AbstractExecutable executable = getExecutable(index);
508
509                 if(executable != null) {
510                     try {
511                         if(s.getThisValue() != null) {
512                             csm.setData("this", s.getThisValue()); //$NON-NLS-1$
513
}
514                         IStatus status = runExecutable(executable, csm);
515                         if ( status.isOK()) {
516                             return VIEWITEM_ADVANCE;
517                         }
518                         if ( status.getSeverity() == IStatus.ERROR) {
519                             CheatSheetPlugin.getPlugin().getLog().log(status);
520                             org.eclipse.jface.dialogs.ErrorDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), null, null, status);
521                         }
522                         return VIEWITEM_DONOT_ADVANCE;
523                     } finally {
524                         if(s.getThisValue() != null) {
525                             csm.setData("this", null); //$NON-NLS-1$
526
}
527                     }
528                 }
529             }
530         }
531         return VIEWITEM_ADVANCE;
532     }
533
534     /*package*/void setButtonsHandled(boolean handled){
535         buttonsHandled = handled;
536     }
537     
538     /*package*/ void setIncomplete() {
539         super.setIncomplete();
540             
541         //check for sub items and reset their icons.
542
ArrayList JavaDoc l = getListOfSubItemCompositeHolders();
543         if(l != null){
544             for(int j=0; j<l.size(); j++){
545                 SubItemCompositeHolder s = (SubItemCompositeHolder)l.get(j);
546                 if(s.isCompleted() || s.isSkipped())
547                     s.getCheckDoneLabel().setVisible(false); //setImage(null);
548
if(s.getStartButton() != null) {
549                     s.getStartButton().setImage(CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.CHEATSHEET_ITEM_BUTTON_START));
550                     s.getStartButton().setToolTipText(Messages.PERFORM_TASK_TOOLTIP);
551                 }
552             }
553         }
554     }
555
556     /*package*/ void setRestartImage() {
557         ImageHyperlink startButton = getStartButton();
558         if (startButton != null) {
559             startButton.setImage(CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.CHEATSHEET_ITEM_BUTTON_RESTART));
560             startButton.setText(Messages.RESTART_TASK_TOOLTIP);
561             startButton.setToolTipText(Messages.RESTART_TASK_TOOLTIP);
562         }
563     }
564
565     /*package*/ void setStartImage() {
566         ImageHyperlink startButton = getStartButton();
567         if (startButton != null) {
568             startButton.setImage(CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.CHEATSHEET_ITEM_BUTTON_START));
569             if (startButton.getText() != null) {
570                 startButton.setText(Messages.PERFORM_TASK_TOOLTIP);
571             }
572             startButton.setToolTipText(Messages.PERFORM_TASK_TOOLTIP);
573         }
574     }
575
576     boolean hasCompletionMessage() {
577         return item.getCompletionMessage() != null;
578     }
579     
580     
581     /*
582      * (non-Javadoc)
583      * Create a composite to hold the message defined in an onCompletion element
584      * and a button to advance to the next step or return to the introduction if
585      * this is the last step.
586      */

587     void createCompletionComposite(boolean isFinalItem) {
588         String JavaDoc completionMessage = viewer.getManager().performVariableSubstitution
589             (item.getCompletionMessage());
590         if (completionMessage != null) {
591             Color backgroundColor = bodyWrapperComposite.getBackground();
592             completionComposite = page.getToolkit().createComposite(
593                     bodyWrapperComposite);
594             TableWrapLayout completionlayout = new TableWrapLayout();
595             completionlayout.numColumns = 1;
596
597             TableWrapData completionData = new TableWrapData(TableWrapData.FILL);
598
599             completionComposite.setLayout(completionlayout);
600             completionComposite.setLayoutData(completionData);
601             completionComposite.setBackground(backgroundColor);
602
603             FormText completionText = page.getToolkit().createFormText(completionComposite, false);
604             completionText.setText(completionMessage, completionMessage.startsWith(IParserTags.FORM_START_TAG), false);
605             completionText.setBackground(backgroundColor);
606             final ImageHyperlink completeButton = createButtonWithText(
607                     completionComposite,
608                     getCompletionButtonIcon(isFinalItem),
609                     this,
610                     backgroundColor,
611                     getCompletionButtonTooltip(isFinalItem));
612             completeButton.addHyperlinkListener(new HyperlinkAdapter() {
613                 public void linkActivated(HyperlinkEvent e) {
614                     viewer.advanceItem(completeButton, true);
615                 }
616             });
617             completionComposite.setVisible(false);
618             // The line below is necessary because without it the color of
619
// the composite containing the button does not get set
620
setBackgroundColor(completionComposite, backgroundColor);
621             refresh(completionComposite);
622         }
623     }
624
625     private Image getCompletionButtonIcon(boolean isFinalItem) {
626         if (isFinalItem) {
627             return CheatSheetPlugin
628             .getPlugin()
629             .getImage(
630                     ICheatSheetResource.CHEATSHEET_RETURN);
631         }
632         return CheatSheetPlugin
633                 .getPlugin()
634                 .getImage(
635                         ICheatSheetResource.CHEATSHEET_ITEM_BUTTON_COMPLETE);
636     }
637     
638     private String JavaDoc getCompletionButtonTooltip(boolean isFinalItem) {
639         if (isFinalItem) {
640             return Messages.RETURN_TO_INTRO_TOOLTIP;
641         }
642         return Messages.ADVANCE_TASK_TOOLTIP;
643     }
644     
645     private void refresh(Composite composite) {
646         composite.layout();
647         getMainItemComposite().layout();
648         page.getForm().reflow(true);
649     }
650     
651     public void refreshItem() {
652         if (buttonComposite != null) {
653             refresh(buttonComposite);
654         }
655     }
656     
657     protected void setFocus() {
658         ArrayList JavaDoc list = getListOfSubItemCompositeHolders();
659         Control subitemLabel = null;
660         SubItemCompositeHolder holder = null;
661         if (list != null) {
662             for (Iterator JavaDoc iter = list.iterator(); iter.hasNext() && subitemLabel == null ;) {
663                 holder = (SubItemCompositeHolder)iter.next();
664                 if (!holder.isCompleted() && !holder.isSkipped()) {
665                     subitemLabel = holder.getSubitemLabel();
666                 }
667             }
668         }
669         if (subitemLabel != null) {
670             FormToolkit.ensureVisible(subitemLabel);
671             if (holder.getStartButton() != null) {
672                 holder.getStartButton().setFocus();
673             } else if (holder.getCompleteButton() != null) {
674                 holder.getCompleteButton().setFocus();
675             }
676         } else {
677             FormToolkit.ensureVisible(getMainItemComposite());
678             super.setFocus();
679         }
680     }
681         
682 }
683
Popular Tags