KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > composite > views > DescriptionPanel


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.ui.internal.cheatsheets.composite.views;
13
14 import org.eclipse.jface.resource.JFaceResources;
15 import org.eclipse.osgi.util.NLS;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.ui.forms.IFormColors;
22 import org.eclipse.ui.forms.ManagedForm;
23 import org.eclipse.ui.forms.events.IHyperlinkListener;
24 import org.eclipse.ui.forms.widgets.FormText;
25 import org.eclipse.ui.forms.widgets.FormToolkit;
26 import org.eclipse.ui.forms.widgets.ScrolledForm;
27 import org.eclipse.ui.forms.widgets.TableWrapData;
28 import org.eclipse.ui.forms.widgets.TableWrapLayout;
29 import org.eclipse.ui.internal.cheatsheets.CheatSheetPlugin;
30 import org.eclipse.ui.internal.cheatsheets.ICheatSheetResource;
31 import org.eclipse.ui.internal.cheatsheets.Messages;
32 import org.eclipse.ui.internal.cheatsheets.composite.model.AbstractTask;
33 import org.eclipse.ui.internal.cheatsheets.composite.model.SuccesorTaskFinder;
34 import org.eclipse.ui.internal.cheatsheets.composite.model.TaskStateUtilities;
35 import org.eclipse.ui.internal.cheatsheets.composite.parser.ICompositeCheatsheetTags;
36 import org.eclipse.ui.internal.cheatsheets.composite.parser.MarkupParser;
37 import org.eclipse.ui.internal.provisional.cheatsheets.ICompositeCheatSheetTask;
38 import org.eclipse.ui.internal.provisional.cheatsheets.IEditableTask;
39 import org.eclipse.ui.internal.provisional.cheatsheets.ITaskGroup;
40
41 /**
42  * The description panel of a composite cheat sheet panel. This panel shows the introduction
43  * message and depending upon the type and state of the task a selection of the following:
44  * Completion message
45  * A message indicating that the task has been skipped.
46  * A message indicating that a parent task has been skipped.
47  * A message indicating that the task is blocked.
48  * A message indicationg that a parent choice is already satisfied.
49  * A link to a successor task.
50  * All tasks completed message.
51  */

52
53 public class DescriptionPanel {
54     
55     public static final String JavaDoc REVIEW_IMAGE = "review"; //$NON-NLS-1$
56
private static final String JavaDoc GOTO_IMAGE = "goto"; //$NON-NLS-1$
57
private static final String JavaDoc SKIP_IMAGE = "skip"; //$NON-NLS-1$
58
private static final String JavaDoc START_IMAGE = "start"; //$NON-NLS-1$
59
private static final String JavaDoc WARNING_IMAGE = "warning"; //$NON-NLS-1$
60
private static final String JavaDoc INFORMATION_IMAGE = "info"; //$NON-NLS-1$
61
private Composite panel;
62     private Composite control;
63     private FormText upperText;
64     private FormText lowerText;
65     private ScrolledForm form;
66     protected DescriptionPanel() {}
67     
68     public DescriptionPanel(ManagedForm mform, Composite parent) {
69         
70         FormToolkit toolkit = mform.getToolkit();
71         control = new Composite(parent, SWT.NULL);
72         final GridLayout controlLayout = new GridLayout();
73         controlLayout.marginHeight = 0;
74         controlLayout.marginWidth = 0;
75         control.setLayout(controlLayout);
76         form = toolkit.createScrolledForm(control);
77         panel = form.getBody();
78         form.setLayoutData(new GridData(GridData.FILL_BOTH));
79         toolkit.adapt(panel);
80         
81         TableWrapLayout layout = new TableWrapLayout();
82         panel.setLayout(layout);
83
84         upperText = mform.getToolkit().createFormText(panel, false);
85         mform.getToolkit().adapt(upperText, false, false);
86         
87         Composite separator = toolkit.createCompositeSeparator(panel);
88         
89         TableWrapData data = new TableWrapData();
90         data.align = TableWrapData.FILL;
91         data.grabHorizontal = true;
92         data.maxHeight = 1;
93         separator.setLayoutData(data);
94          
95         lowerText = mform.getToolkit().createFormText(panel, false);
96         mform.getToolkit().adapt(lowerText, false, false);
97
98         upperText.marginWidth = 5;
99         upperText.marginHeight = 5;
100         upperText.setFont("header", JFaceResources.getHeaderFont()); //$NON-NLS-1$
101
upperText.setColor("title", toolkit.getColors().getColor(IFormColors.TITLE)); //$NON-NLS-1$
102
lowerText.marginWidth = 5;
103         lowerText.marginHeight = 5;
104         lowerText.setImage(START_IMAGE, CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.COMPOSITE_TASK_START));
105         lowerText.setImage(SKIP_IMAGE, CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.COMPOSITE_TASK_SKIP));
106         lowerText.setImage(GOTO_IMAGE, CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.COMPOSITE_GOTO_TASK));
107         lowerText.setImage(REVIEW_IMAGE, CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.COMPOSITE_TASK_REVIEW));
108         lowerText.setImage(WARNING_IMAGE, CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.WARNING));
109         lowerText.setImage(INFORMATION_IMAGE, CheatSheetPlugin.getPlugin().getImage(ICheatSheetResource.INFORMATION));
110     }
111     
112     public Control getControl() {
113         return control;
114     }
115     
116     public void addHyperlinkListener(IHyperlinkListener listener) {
117         lowerText.addHyperlinkListener(listener);
118     }
119     
120     public void showDescription(final ICompositeCheatSheetTask task) {
121         StringBuffer JavaDoc upperMessage = new StringBuffer JavaDoc();
122         upperMessage.append("<form>"); //$NON-NLS-1$
123
upperMessage.append("<p><span color=\"title\" font=\"header\">"); //$NON-NLS-1$
124
upperMessage.append(MarkupParser.escapeText(task.getName()));
125         upperMessage.append("</span></p>"); //$NON-NLS-1$
126
upperMessage.append(MarkupParser.createParagraph(task.getDescription(), null));
127         upperMessage.append("</form>"); //$NON-NLS-1$
128
upperText.setText(upperMessage.toString(), true, false);
129     
130         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
131         buf.append("<form>"); //$NON-NLS-1$
132

133         boolean startable = false;
134         boolean isBlocked = false;
135         boolean isSkippable = ((AbstractTask)task).isSkippable();
136         
137         if (task.getState() == ICompositeCheatSheetTask.COMPLETED) {
138             buf.append(MarkupParser.createParagraph(task.getCompletionMessage(), null));
139             isSkippable = false;
140         } else if (task.getState() == ICompositeCheatSheetTask.SKIPPED) {
141             buf.append(MarkupParser.createParagraph(Messages.THIS_TASK_SKIPPED, INFORMATION_IMAGE));
142             isSkippable = false;
143         } else if (TaskStateUtilities.findSkippedAncestor(task) != null) {
144             ICompositeCheatSheetTask skipped = TaskStateUtilities.findSkippedAncestor(task);
145             String JavaDoc skipParentMsg = NLS.bind(Messages.PARENT_SKIPPED,
146                 (new Object JavaDoc[] {MarkupParser.escapeText((skipped.getName()))}));
147             buf.append(MarkupParser.createParagraph(skipParentMsg, WARNING_IMAGE));
148             isSkippable = false;
149         } else if (TaskStateUtilities.findCompletedAncestor(task) != null) {
150             ICompositeCheatSheetTask completed = TaskStateUtilities.findCompletedAncestor(task);
151             String JavaDoc completedParentMsg = NLS.bind(Messages.PARENT_COMPLETED,
152                (new Object JavaDoc[] {MarkupParser.escapeText(completed.getName())}));
153             buf.append(MarkupParser.createParagraph(completedParentMsg, WARNING_IMAGE));
154             isSkippable = false;
155         } else if (!task.requiredTasksCompleted()) {
156             isBlocked = true;
157             showBlockingTasks(Messages.COMPOSITE_PAGE_BLOCKED, task, buf);
158         } else if (TaskStateUtilities.findBlockedAncestor(task) != null) {
159             isBlocked = true;
160             ICompositeCheatSheetTask blockedAncestor = TaskStateUtilities.findBlockedAncestor(task);
161             String JavaDoc blockingAncestorMsg = NLS.bind(Messages.PARENT_BLOCKED,
162                     (new Object JavaDoc[] {MarkupParser.escapeText(blockedAncestor.getName())}));
163             showBlockingTasks(blockingAncestorMsg , blockedAncestor, buf);
164         } else {
165             startable = task instanceof IEditableTask && task.getState() == ICompositeCheatSheetTask.NOT_STARTED;
166         }
167         
168         if (startable) {
169             addHyperlink(buf, CompositeCheatSheetPage.START_HREF, START_IMAGE, Messages.COMPOSITE_PAGE_START_TASK);
170         }
171
172         if (task instanceof IEditableTask && task.getState() == ICompositeCheatSheetTask.COMPLETED ) {
173             addHyperlink(buf, CompositeCheatSheetPage.REVIEW_TAG, REVIEW_IMAGE, Messages.COMPOSITE_PAGE_REVIEW_TASK);
174         }
175         
176         if (isSkippable) {
177             String JavaDoc skipMessage;
178             if (task instanceof ITaskGroup) {
179                 skipMessage = Messages.COMPOSITE_PAGE_SKIP_TASK_GROUP;
180             } else {
181                 skipMessage = Messages.COMPOSITE_PAGE_SKIP_TASK;
182             }
183             addHyperlink(buf, CompositeCheatSheetPage.SKIP_HREF, SKIP_IMAGE, skipMessage);
184         }
185
186         if (!startable && !isBlocked) {
187             showSuccesorTaskLinks(task, buf);
188         }
189     
190         buf.append("</form>"); //$NON-NLS-1$
191

192         lowerText.setText(buf.toString(), true, false);
193         getControl().setData(ICompositeCheatsheetTags.TASK, task);
194         form.reflow(true);
195     }
196
197     private void showBlockingTasks(String JavaDoc message, final ICompositeCheatSheetTask task, StringBuffer JavaDoc buf) {
198         buf.append("<p/>"); //$NON-NLS-1$
199
buf.append("<p>"); //$NON-NLS-1$
200
buf.append("<img HREF=\""); //$NON-NLS-1$
201
buf.append(WARNING_IMAGE);
202         buf.append("\"/> "); //$NON-NLS-1$
203
buf.append(message);
204         buf.append("</p>"); //$NON-NLS-1$// Add the list of blocking tasks
205

206         ICompositeCheatSheetTask[] requiredTasks = task.getRequiredTasks();
207         for (int i = 0; i < requiredTasks.length; i++) {
208             warnOfIncompleteTask(buf, requiredTasks[i]);
209         }
210         buf.append("<p>"); //$NON-NLS-1$
211
buf.append("</p>"); //$NON-NLS-1$
212
}
213     
214     private void addHyperlink(StringBuffer JavaDoc buf, String JavaDoc href, String JavaDoc imageRef, String JavaDoc message) {
215         buf.append("<p><a HREF=\""); //$NON-NLS-1$
216
buf.append(href);
217         buf.append("\">"); //$NON-NLS-1$
218
buf.append("<img HREF=\""); //$NON-NLS-1$
219
buf.append(imageRef);
220         buf.append("\"/> "); //$NON-NLS-1$
221
buf.append(message);
222         buf.append("</a></p>"); //$NON-NLS-1$
223
}
224     
225     /*
226      * If this task is incomplete create a message to that effect
227      */

228     private void warnOfIncompleteTask(StringBuffer JavaDoc buf, ICompositeCheatSheetTask task) {
229         if (task.getState() != ICompositeCheatSheetTask.COMPLETED &&
230             task.getState() != ICompositeCheatSheetTask.SKIPPED ) {
231             buf.append("<li>"); //$NON-NLS-1$
232
buf.append("<a HREF=\""); //$NON-NLS-1$
233
buf.append(CompositeCheatSheetPage.GOTO_TASK_TAG);
234             buf.append(task.getId());
235             buf.append("\">"); //$NON-NLS-1$
236
buf.append(NLS.bind(Messages.COMPOSITE_PAGE_TASK_NOT_COMPLETE, (new Object JavaDoc[]
237                 {MarkupParser.escapeText(task.getName())})));
238             buf.append("</a>"); //$NON-NLS-1$
239
buf.append("</li>"); //$NON-NLS-1$
240
}
241     }
242     
243     private void showSuccesorTaskLinks(ICompositeCheatSheetTask task, StringBuffer JavaDoc buf) {
244         // Add the links to the next tasks
245
ICompositeCheatSheetTask[] successorTasks = new SuccesorTaskFinder(task).getRecommendedSuccessors();
246         for (int i = 0; i < successorTasks.length; i++) {
247             ICompositeCheatSheetTask successor = successorTasks[i];
248             String JavaDoc message = NLS.bind(Messages.COMPOSITE_PAGE_GOTO_TASK, (new Object JavaDoc[]
249                  {MarkupParser.escapeText(successor.getName())}));
250             addHyperlink(buf, CompositeCheatSheetPage.GOTO_TASK_TAG + successor.getId(), GOTO_IMAGE, message);
251         }
252     }
253     
254 }
255
Popular Tags