KickJava   Java API By Example, From Geeks To Geeks.

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


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
16 import org.eclipse.help.HelpSystem;
17 import org.eclipse.help.IContext;
18 import org.eclipse.jface.action.Action;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.events.FocusEvent;
21 import org.eclipse.swt.events.FocusListener;
22 import org.eclipse.swt.events.SelectionAdapter;
23 import org.eclipse.swt.events.SelectionEvent;
24 import org.eclipse.swt.graphics.Color;
25 import org.eclipse.swt.graphics.Font;
26 import org.eclipse.swt.graphics.FontData;
27 import org.eclipse.swt.graphics.Image;
28 import org.eclipse.swt.graphics.Point;
29 import org.eclipse.swt.layout.GridData;
30 import org.eclipse.swt.layout.GridLayout;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Control;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.Widget;
35 import org.eclipse.ui.PlatformUI;
36 import org.eclipse.ui.cheatsheets.AbstractItemExtensionElement;
37 import org.eclipse.ui.forms.events.ExpansionAdapter;
38 import org.eclipse.ui.forms.events.ExpansionEvent;
39 import org.eclipse.ui.forms.events.HyperlinkAdapter;
40 import org.eclipse.ui.forms.events.HyperlinkEvent;
41 import org.eclipse.ui.forms.widgets.ExpandableComposite;
42 import org.eclipse.ui.forms.widgets.FormText;
43 import org.eclipse.ui.forms.widgets.FormToolkit;
44 import org.eclipse.ui.forms.widgets.ImageHyperlink;
45 import org.eclipse.ui.forms.widgets.TableWrapData;
46 import org.eclipse.ui.forms.widgets.TableWrapLayout;
47 import org.eclipse.ui.internal.cheatsheets.CheatSheetPlugin;
48 import org.eclipse.ui.internal.cheatsheets.CheatSheetStopWatch;
49 import org.eclipse.ui.internal.cheatsheets.ICheatSheetResource;
50 import org.eclipse.ui.internal.cheatsheets.Messages;
51 import org.eclipse.ui.internal.cheatsheets.data.IParserTags;
52 import org.eclipse.ui.internal.cheatsheets.data.Item;
53
54 public abstract class ViewItem {
55
56     public final static byte VIEWITEM_ADVANCE = 0;
57     public final static byte VIEWITEM_DONOT_ADVANCE = 1;
58     private Composite bodyComp;
59
60     protected FormText bodyText;
61     protected FormText completionText;
62 // protected Label bodyText;
63
protected Composite bodyWrapperComposite;
64     protected Composite buttonComposite;
65     protected Composite completionComposite;
66
67     private boolean buttonExpanded = true;
68     private boolean completionMessageExpanded = false;
69     private Label checkDoneLabel;
70     private boolean completed = false;
71
72     protected Item item;
73
74     // Colors
75
protected Color itemColor;
76
77     private boolean isSkipped = false;
78     private ExpandableComposite mainItemComposite;
79
80     private Composite parent;
81     protected CheatSheetViewer viewer;
82     protected CheatSheetPage page;
83     private Composite titleComposite;
84     private boolean bold = true;
85     private Font boldFont;
86     private Font regularFont;
87     private boolean initialized = false;
88     protected ArrayList JavaDoc listOfSubItemCompositeHolders;
89
90     /**
91      * Constructor for ViewItem.
92      */

93     public ViewItem(CheatSheetPage page, Item item, Color itemColor, CheatSheetViewer viewer) {
94         super();
95         this.page = page;
96         this.parent = page.getForm().getBody();
97         this.item = item;
98         this.itemColor = itemColor;
99         this.viewer = viewer;
100         addItem();
101     }
102
103     //Adds the item to the main composite.
104
private void addItem() {
105         CheatSheetStopWatch.startStopWatch("ViewItem.addItem()"); //$NON-NLS-1$
106
CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after getBannerBackground: "); //$NON-NLS-1$ //$NON-NLS-2$
107

108         // Set up the main composite for the item.******************************************
109
checkDoneLabel = page.getToolkit().createLabel(parent, " "); //$NON-NLS-1$
110
CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after create checkDoneLabel: "); //$NON-NLS-1$ //$NON-NLS-2$
111

112         mainItemComposite = page.getToolkit().createSection(parent, ExpandableComposite.TWISTIE|ExpandableComposite.COMPACT);
113         mainItemComposite.setBackground(itemColor);
114         mainItemComposite.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
115         String JavaDoc title = item.getTitle();
116         if (title != null) {
117             mainItemComposite.setText(ViewUtilities.escapeForLabel(title));
118         }
119         CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after create mainItemComposite: "); //$NON-NLS-1$ //$NON-NLS-2$
120

121         
122         mainItemComposite.addExpansionListener(new ExpansionAdapter() {
123             public void expansionStateChanged(ExpansionEvent e) {
124                 page.getForm().reflow(true);
125             }
126         });
127         CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after addExpansionListener: "); //$NON-NLS-1$ //$NON-NLS-2$
128

129         // handle item extensions here
130
// check number of extensions for this item and adjust layout accordingly
131
int number = 0;
132         ArrayList JavaDoc itemExts = item.getItemExtensions();
133
134         if((itemExts != null && itemExts.size() > 0) || item.getContextId() != null || item.getHref() != null) {
135             // Set up the title composite for the item.
136
titleComposite = page.getToolkit().createComposite(mainItemComposite);
137             titleComposite.setBackground(itemColor);
138         }
139
140         if(itemExts != null) {
141             for (int g = 0; g < itemExts.size(); g++) {
142                 AbstractItemExtensionElement[] eea = (AbstractItemExtensionElement[]) itemExts.get(g);
143                 number += eea.length;
144                 for (int x = 0; x < eea.length; x++) {
145                     eea[x].createControl(titleComposite);
146                 }
147             }
148         }
149         CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after create item extensions: "); //$NON-NLS-1$ //$NON-NLS-2$
150

151         // don't add the help icon unless there is a context id or help link
152
if(item.getContextId() != null || item.getHref() != null) {
153             // adjust the layout count
154
number++;
155             ImageHyperlink helpButton = createButton(titleComposite, CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.CHEATSHEET_ITEM_HELP), this, itemColor, Messages.HELP_BUTTON_TOOLTIP);
156             helpButton.addHyperlinkListener(new HyperlinkAdapter() {
157                 public void linkActivated(HyperlinkEvent e) {
158                     // If we have a context id, handle this first and ignore an hrefs
159
if(item.getContextId() != null) {
160                         openInfopop(e.widget);
161                     } else {
162                         // We only have an href, so let's open it in the help system
163
openHelpTopic();
164                     }
165                 }
166             });
167         }
168         CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after create help button: "); //$NON-NLS-1$ //$NON-NLS-2$
169

170         if(number > 0) {
171             mainItemComposite.setTextClient(titleComposite);
172             GridLayout layout = new GridLayout(number, false);
173             GridData data = new GridData(GridData.FILL_BOTH);
174     
175             titleComposite.setLayout(layout);
176             titleComposite.setLayoutData(data);
177             layout.marginWidth = 0;
178             layout.marginHeight = 0;
179             layout.verticalSpacing = 0;
180         }
181         CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after setTextClient: "); //$NON-NLS-1$ //$NON-NLS-2$
182

183         //Body wrapper here. this composite will be hidden and shown as appropriate.
184
bodyWrapperComposite = page.getToolkit().createComposite(mainItemComposite);
185         mainItemComposite.setClient(bodyWrapperComposite);
186         TableWrapLayout wrapperLayout = new TableWrapLayout();
187         bodyWrapperComposite.setLayout(wrapperLayout);
188         bodyWrapperComposite.setBackground(itemColor);
189         CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after create bodyWrapperComposite: "); //$NON-NLS-1$ //$NON-NLS-2$
190

191         bodyText = page.getToolkit().createFormText(bodyWrapperComposite, false);
192         bodyText.addSelectionListener(new SelectionAdapter() {
193             public void widgetSelected(SelectionEvent e) {
194                 Action copyAction = viewer.getCopyAction();
195                 if (copyAction!=null)
196                     copyAction.setEnabled(bodyText.canCopy());
197             }
198         });
199         bodyText.addFocusListener(new FocusListener() {
200             public void focusGained(FocusEvent e) {
201                 Action copyAction = viewer.getCopyAction();
202                 if (copyAction!=null)
203                     copyAction.setEnabled(bodyText.canCopy());
204             }
205             public void focusLost(FocusEvent e) {
206                 Action copyAction = viewer.getCopyAction();
207                 if (copyAction!=null)
208                     copyAction.setEnabled(false);
209             }
210         });
211 // bodyText = toolkit.createLabel(bodyWrapperComposite, item.getDescription(), SWT.WRAP);
212
bodyText.setText(item.getDescription(), item.getDescription().startsWith(IParserTags.FORM_START_TAG), false);
213
214         //Set up the body text portion here.
215
bodyText.setBackground(itemColor);
216         bodyText.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
217         CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after create FormText: "); //$NON-NLS-1$ //$NON-NLS-2$
218

219         //Handle the sub-steps and regular buttons here.
220
//First Check to see if there is sub steps. If there is, don't create the button comp,
221
//As it will be handled by the CoreItemWithSubs.
222
//If there is no sub steps, create a button composite and Pass it to CoreItem using the handleButtons.
223

224         if(!item.isDynamic()) {
225             handleButtons();
226         }
227         CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after handleButtons(): "); //$NON-NLS-1$ //$NON-NLS-2$
228

229         setButtonsVisible(false);
230         setCollapsed();
231         CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after setting buttons and item collapsed: "); //$NON-NLS-1$ //$NON-NLS-2$
232

233         boldFont = mainItemComposite.getFont();
234         FontData[] fontDatas = boldFont.getFontData();
235         for (int i = 0; i < fontDatas.length; i++) {
236             fontDatas[i].setStyle(fontDatas[i].getStyle() ^ SWT.BOLD);
237         }
238         regularFont = new Font(mainItemComposite.getDisplay(), fontDatas);
239         CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after font initlization: "); //$NON-NLS-1$ //$NON-NLS-2$
240

241         setBold(false);
242         CheatSheetStopWatch.printLapTime("ViewItem.addItem()", "Time in addItem() after setBold: "); //$NON-NLS-1$ //$NON-NLS-2$
243
}
244
245     protected ImageHyperlink createButtonWithText(Composite parent, Image image, ViewItem item, Color color, String JavaDoc linkText) {
246         ImageHyperlink button = page.getToolkit().createImageHyperlink(parent, SWT.NULL);
247         button.setImage(image);
248         button.setData(item);
249         button.setBackground(color);
250         button.setText(linkText);
251         button.setToolTipText(linkText);
252         return button;
253     }
254
255     protected ImageHyperlink createButton(Composite parent, Image image, ViewItem item, Color color, String JavaDoc toolTipText) {
256         ImageHyperlink button = new ImageHyperlink(parent, SWT.NULL);
257         page.getToolkit().adapt(button, true, true);
258         button.setImage(image);
259         button.setData(item);
260         button.setBackground(color);
261         button.setToolTipText(toolTipText);
262 // button.setFAccessibleDescription(bodyText.getText());
263
// button.setFAccessibleName(button.getToolTipText());
264

265         return button;
266     }
267
268     public void dispose() {
269         if (checkDoneLabel != null)
270             checkDoneLabel.dispose();
271         if (bodyText != null)
272             bodyText.dispose();
273         if (buttonComposite != null)
274             buttonComposite.dispose();
275         if (completionComposite != null)
276             completionComposite.dispose();
277         if (bodyComp != null)
278             bodyComp.dispose();
279         if (bodyWrapperComposite != null)
280             bodyWrapperComposite.dispose();
281         if (mainItemComposite != null)
282             mainItemComposite.dispose();
283         if (titleComposite != null)
284             titleComposite.dispose();
285         if (regularFont != null)
286             regularFont.dispose();
287
288         ArrayList JavaDoc itemExts = item.getItemExtensions();
289         if (itemExts != null) {
290             for (int g = 0; g < itemExts.size(); g++) {
291                 AbstractItemExtensionElement[] eea = (AbstractItemExtensionElement[]) itemExts.get(g);
292                 for (int x = 0; x < eea.length; x++) {
293                     eea[x].dispose();
294                 }
295             }
296         }
297     }
298
299     /**
300      * @return
301      */

302     /*package*/
303     Image getCompleteImage() {
304         return CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.CHEATSHEET_ITEM_COMPLETE);
305     }
306
307     /**
308      * @return
309      */

310     public Item getItem() {
311         return item;
312     }
313
314     /**
315      * Returns the mainItemComposite.
316      * @return Composite
317      */

318     /*package*/
319     Composite getMainItemComposite() {
320         return mainItemComposite;
321     }
322
323     /**
324      * @return
325      */

326     /*package*/
327     Image getSkipImage() {
328         return CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.CHEATSHEET_ITEM_SKIP);
329     }
330
331     //Adds the buttons to the buttonComposite.
332
/*package*/
333     abstract void handleButtons();
334
335     /*package*/
336     boolean isBold() {
337         return bold;
338     }
339
340     /**
341      * Returns the completed.
342      * @return boolean
343      */

344     public boolean isCompleted() {
345         return completed;
346     }
347
348     public boolean isExpanded() {
349         return mainItemComposite.isExpanded();
350     }
351     
352     public boolean isCompletionMessageExpanded() {
353         return completionMessageExpanded;
354     }
355
356     /**
357      * Returns whether or not cheat sheet viewer containing this item is in
358      * a modal dialog.
359      *
360      * @return whether the cheat sheet viewer is in a modal dialog
361      */

362     public boolean isInDialogMode() {
363         return viewer.isInDialogMode();
364     }
365     
366     /*package*/
367     boolean isSkipped() {
368         return isSkipped;
369     }
370
371     /**
372      * Open a help topic
373      */

374     private void openHelpTopic() {
375         if (item == null || item.getHref() == null) {
376             return;
377         }
378
379         PlatformUI.getWorkbench().getHelpSystem().displayHelpResource(item.getHref());
380     }
381
382     /**
383      * Open an infopop
384      */

385     private void openInfopop(Widget widget) {
386         if (item == null || item.getContextId() == null) {
387             return;
388         }
389
390         IContext context = HelpSystem.getContext(item.getContextId());
391
392         if (context != null) {
393             // determine a location in the upper right corner of the widget
394
Point point = widget.getDisplay().getCursorLocation();
395             point = new Point(point.x + 15, point.y);
396             // display the help
397
PlatformUI.getWorkbench().getHelpSystem().displayContext(context, point.x, point.y);
398         }
399     }
400
401     public void setAsCurrentActiveItem() {
402         setColorAsCurrent(true);
403         setButtonsVisible(true);
404         setBold(true);
405         setExpanded();
406         setFocus();
407     }
408
409     protected void setFocus() {
410         mainItemComposite.setFocus();
411         FormToolkit.ensureVisible(getMainItemComposite());
412     }
413
414     /*package*/
415     void setAsNormalCollapsed() {
416         setBold(false);
417         setColorAsCurrent(false);
418         if (mainItemComposite.isExpanded())
419             setCollapsed();
420     }
421
422     /*package*/
423     void setAsNormalNonCollapsed() {
424         setColorAsCurrent(false);
425         setBold(false);
426     }
427
428     private void setBodyColor(Color color) {
429         mainItemComposite.setBackground(color);
430         setBackgroundColor(bodyWrapperComposite, color);
431         setBackgroundColor(buttonComposite, color);
432         setBackgroundColor(completionComposite, color);
433     }
434     
435     /*
436      * Set the background color of this composite and its children.
437      * If the composite is null do nothing.
438      */

439     protected void setBackgroundColor(Composite composite, Color color) {
440         if (composite != null) {
441             composite.setBackground(color);
442             Control[] children = composite.getChildren();
443             for (int i = 0; i < children.length; i++) {
444                 children[i].setBackground(color);
445             }
446         }
447     }
448
449     /*package*/
450     void setBold(boolean value) {
451         if(value) {
452             mainItemComposite.setFont(boldFont);
453             if(initialized)
454                 mainItemComposite.layout();
455         } else {
456             mainItemComposite.setFont(regularFont);
457             if(initialized)
458                 mainItemComposite.layout();
459         }
460         bold = value;
461     }
462     
463     //collapse or expand the item
464
/*package*/
465     void setButtonsVisible(boolean isVisible) {
466         if (buttonExpanded != isVisible) {
467             if (listOfSubItemCompositeHolders != null) {
468                 for (Iterator JavaDoc iter = listOfSubItemCompositeHolders.iterator(); iter.hasNext(); ){
469                     ((SubItemCompositeHolder)iter.next()).setButtonsVisible(isVisible);
470                 }
471             } else if (buttonComposite != null) {
472                 buttonComposite.setVisible(isVisible);
473             }
474         }
475         
476         if(isVisible && initialized) {
477             FormToolkit.ensureVisible(getMainItemComposite());
478         }
479         buttonExpanded = isVisible;
480     }
481     
482     protected void setCompletionMessageExpanded(boolean isFinalItem) {
483         if (hasCompletionMessage()) {
484             if (completionComposite == null) {
485                 createCompletionComposite(isFinalItem);
486             }
487             if (!completionMessageExpanded) {
488                 completionComposite.setVisible(true);
489                 completionMessageExpanded = true;
490             }
491         }
492     }
493     
494     abstract void createCompletionComposite(boolean isFinalItem);
495
496     protected void setCompletionMessageCollapsed() {
497         if (completionComposite != null) {
498             if (completionMessageExpanded) {
499                 completionComposite.dispose();
500                 completionComposite = null;
501                 page.getForm().reflow(true);
502             }
503         }
504         completionMessageExpanded = false;
505     }
506
507     //collapses the item
508
/*package*/
509     void setCollapsed() {
510         if (mainItemComposite.isExpanded()) {
511             mainItemComposite.setExpanded(false);
512             if(initialized) {
513                 page.getForm().reflow(true);
514                 FormToolkit.ensureVisible(getMainItemComposite());
515             }
516         }
517     }
518
519     private void setColorAsCurrent(boolean active) {
520         if (active) {
521             setTitleColor(page.getActiveColor());
522             setBodyColor(page.getActiveColor());
523         } else {
524             setTitleColor(itemColor);
525             setBodyColor(itemColor);
526         }
527     }
528
529     //marks the item as complete.
530
/*package*/
531     void setComplete() {
532         completed = true;
533         checkDoneLabel.setImage(getCompleteImage());
534
535         if(initialized) {
536             checkDoneLabel.getParent().layout();
537         }
538     }
539
540     //expands the item
541
/*package*/
542     void setExpanded() {
543         if (!mainItemComposite.isExpanded()) {
544             mainItemComposite.setExpanded(true);
545             if(initialized) {
546                 page.getForm().reflow(true);
547                 FormToolkit.ensureVisible(getMainItemComposite());
548             }
549         }
550     }
551
552     /*package*/
553     void setIncomplete() {
554         checkDoneLabel.setImage(null);
555         completed = false;
556         setStartImage();
557     }
558
559     /**
560      * Sets the itemColor.
561      * @param itemColor The itemColor to set
562      */

563     /*package*/
564     void setItemColor(Color itemColor) {
565         this.itemColor = itemColor;
566     }
567
568     /*package*/
569     void setOriginalColor() {
570         setTitleColor(itemColor);
571         setBodyColor(itemColor);
572         setBold(false);
573     }
574
575     /*package*/
576     abstract void setRestartImage();
577
578     /*package*/
579     void setSkipped() {
580         isSkipped = true;
581         checkDoneLabel.setImage(getSkipImage());
582
583         if(initialized) {
584             checkDoneLabel.getParent().layout();
585         }
586     }
587     /*package*/
588     abstract void setStartImage();
589
590     private void setTitleColor(Color bg) {
591         if(titleComposite != null) {
592             titleComposite.setBackground(bg);
593
594             Control[] titlechildren = titleComposite.getChildren();
595             for (int i = 0; i < titlechildren.length; i++) {
596                 titlechildren[i].setBackground(bg);
597             }
598         }
599     }
600
601     public void initialized() {
602         initialized = true;
603     }
604     
605     public boolean canCopy() {
606         return (bodyText!=null && !bodyText.isDisposed())?bodyText.canCopy():false;
607     }
608     public void copy() {
609         if (bodyText!=null && !bodyText.isDisposed())
610             bodyText.copy();
611     }
612     
613    abstract boolean hasCompletionMessage();
614
615 }
616
Popular Tags