001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.cheatsheets.composite.views;
011:
012: import org.eclipse.jface.resource.JFaceResources;
013: import org.eclipse.osgi.util.NLS;
014: import org.eclipse.swt.SWT;
015: import org.eclipse.swt.layout.GridData;
016: import org.eclipse.swt.layout.GridLayout;
017: import org.eclipse.swt.widgets.Composite;
018: import org.eclipse.swt.widgets.Control;
019: import org.eclipse.ui.forms.IFormColors;
020: import org.eclipse.ui.forms.ManagedForm;
021: import org.eclipse.ui.forms.events.IHyperlinkListener;
022: import org.eclipse.ui.forms.widgets.FormText;
023: import org.eclipse.ui.forms.widgets.FormToolkit;
024: import org.eclipse.ui.forms.widgets.ScrolledForm;
025: import org.eclipse.ui.forms.widgets.TableWrapData;
026: import org.eclipse.ui.forms.widgets.TableWrapLayout;
027: import org.eclipse.ui.internal.cheatsheets.CheatSheetPlugin;
028: import org.eclipse.ui.internal.cheatsheets.ICheatSheetResource;
029: import org.eclipse.ui.internal.cheatsheets.Messages;
030: import org.eclipse.ui.internal.cheatsheets.composite.model.AbstractTask;
031: import org.eclipse.ui.internal.cheatsheets.composite.model.SuccesorTaskFinder;
032: import org.eclipse.ui.internal.cheatsheets.composite.model.TaskStateUtilities;
033: import org.eclipse.ui.internal.cheatsheets.composite.parser.ICompositeCheatsheetTags;
034: import org.eclipse.ui.internal.cheatsheets.composite.parser.MarkupParser;
035: import org.eclipse.ui.internal.provisional.cheatsheets.ICompositeCheatSheetTask;
036: import org.eclipse.ui.internal.provisional.cheatsheets.IEditableTask;
037: import org.eclipse.ui.internal.provisional.cheatsheets.ITaskGroup;
038:
039: /**
040: * The description panel of a composite cheat sheet panel. This panel shows the introduction
041: * message and depending upon the type and state of the task a selection of the following:
042: * Completion message
043: * A message indicating that the task has been skipped.
044: * A message indicating that a parent task has been skipped.
045: * A message indicating that the task is blocked.
046: * A message indicationg that a parent choice is already satisfied.
047: * A link to a successor task.
048: * All tasks completed message.
049: */
050:
051: public class DescriptionPanel {
052:
053: public static final String REVIEW_IMAGE = "review"; //$NON-NLS-1$
054: private static final String GOTO_IMAGE = "goto"; //$NON-NLS-1$
055: private static final String SKIP_IMAGE = "skip"; //$NON-NLS-1$
056: private static final String START_IMAGE = "start"; //$NON-NLS-1$
057: private static final String WARNING_IMAGE = "warning"; //$NON-NLS-1$
058: private static final String INFORMATION_IMAGE = "info"; //$NON-NLS-1$
059: private Composite panel;
060: private Composite control;
061: private FormText upperText;
062: private FormText lowerText;
063: private ScrolledForm form;
064:
065: protected DescriptionPanel() {
066: }
067:
068: public DescriptionPanel(ManagedForm mform, Composite parent) {
069:
070: FormToolkit toolkit = mform.getToolkit();
071: control = new Composite(parent, SWT.NULL);
072: final GridLayout controlLayout = new GridLayout();
073: controlLayout.marginHeight = 0;
074: controlLayout.marginWidth = 0;
075: control.setLayout(controlLayout);
076: form = toolkit.createScrolledForm(control);
077: panel = form.getBody();
078: form.setLayoutData(new GridData(GridData.FILL_BOTH));
079: toolkit.adapt(panel);
080:
081: TableWrapLayout layout = new TableWrapLayout();
082: panel.setLayout(layout);
083:
084: upperText = mform.getToolkit().createFormText(panel, false);
085: mform.getToolkit().adapt(upperText, false, false);
086:
087: Composite separator = toolkit.createCompositeSeparator(panel);
088:
089: TableWrapData data = new TableWrapData();
090: data.align = TableWrapData.FILL;
091: data.grabHorizontal = true;
092: data.maxHeight = 1;
093: separator.setLayoutData(data);
094:
095: lowerText = mform.getToolkit().createFormText(panel, false);
096: mform.getToolkit().adapt(lowerText, false, false);
097:
098: upperText.marginWidth = 5;
099: upperText.marginHeight = 5;
100: upperText.setFont("header", JFaceResources.getHeaderFont()); //$NON-NLS-1$
101: upperText
102: .setColor(
103: "title", toolkit.getColors().getColor(IFormColors.TITLE)); //$NON-NLS-1$
104: lowerText.marginWidth = 5;
105: lowerText.marginHeight = 5;
106: lowerText.setImage(START_IMAGE, CheatSheetPlugin.getPlugin()
107: .getImage(ICheatSheetResource.COMPOSITE_TASK_START));
108: lowerText.setImage(SKIP_IMAGE, CheatSheetPlugin.getPlugin()
109: .getImage(ICheatSheetResource.COMPOSITE_TASK_SKIP));
110: lowerText.setImage(GOTO_IMAGE, CheatSheetPlugin.getPlugin()
111: .getImage(ICheatSheetResource.COMPOSITE_GOTO_TASK));
112: lowerText.setImage(REVIEW_IMAGE, CheatSheetPlugin.getPlugin()
113: .getImage(ICheatSheetResource.COMPOSITE_TASK_REVIEW));
114: lowerText.setImage(WARNING_IMAGE, CheatSheetPlugin.getPlugin()
115: .getImage(ICheatSheetResource.WARNING));
116: lowerText.setImage(INFORMATION_IMAGE, CheatSheetPlugin
117: .getPlugin().getImage(ICheatSheetResource.INFORMATION));
118: }
119:
120: public Control getControl() {
121: return control;
122: }
123:
124: public void addHyperlinkListener(IHyperlinkListener listener) {
125: lowerText.addHyperlinkListener(listener);
126: }
127:
128: public void showDescription(final ICompositeCheatSheetTask task) {
129: StringBuffer upperMessage = new StringBuffer();
130: upperMessage.append("<form>"); //$NON-NLS-1$
131: upperMessage
132: .append("<p><span color=\"title\" font=\"header\">"); //$NON-NLS-1$
133: upperMessage.append(MarkupParser.escapeText(task.getName()));
134: upperMessage.append("</span></p>"); //$NON-NLS-1$
135: upperMessage.append(MarkupParser.createParagraph(task
136: .getDescription(), null));
137: upperMessage.append("</form>"); //$NON-NLS-1$
138: upperText.setText(upperMessage.toString(), true, false);
139:
140: StringBuffer buf = new StringBuffer();
141: buf.append("<form>"); //$NON-NLS-1$
142:
143: boolean startable = false;
144: boolean isBlocked = false;
145: boolean isSkippable = ((AbstractTask) task).isSkippable();
146:
147: if (task.getState() == ICompositeCheatSheetTask.COMPLETED) {
148: buf.append(MarkupParser.createParagraph(task
149: .getCompletionMessage(), null));
150: isSkippable = false;
151: } else if (task.getState() == ICompositeCheatSheetTask.SKIPPED) {
152: buf.append(MarkupParser.createParagraph(
153: Messages.THIS_TASK_SKIPPED, INFORMATION_IMAGE));
154: isSkippable = false;
155: } else if (TaskStateUtilities.findSkippedAncestor(task) != null) {
156: ICompositeCheatSheetTask skipped = TaskStateUtilities
157: .findSkippedAncestor(task);
158: String skipParentMsg = NLS.bind(Messages.PARENT_SKIPPED,
159: (new Object[] { MarkupParser.escapeText((skipped
160: .getName())) }));
161: buf.append(MarkupParser.createParagraph(skipParentMsg,
162: WARNING_IMAGE));
163: isSkippable = false;
164: } else if (TaskStateUtilities.findCompletedAncestor(task) != null) {
165: ICompositeCheatSheetTask completed = TaskStateUtilities
166: .findCompletedAncestor(task);
167: String completedParentMsg = NLS.bind(
168: Messages.PARENT_COMPLETED,
169: (new Object[] { MarkupParser.escapeText(completed
170: .getName()) }));
171: buf.append(MarkupParser.createParagraph(completedParentMsg,
172: WARNING_IMAGE));
173: isSkippable = false;
174: } else if (!task.requiredTasksCompleted()) {
175: isBlocked = true;
176: showBlockingTasks(Messages.COMPOSITE_PAGE_BLOCKED, task,
177: buf);
178: } else if (TaskStateUtilities.findBlockedAncestor(task) != null) {
179: isBlocked = true;
180: ICompositeCheatSheetTask blockedAncestor = TaskStateUtilities
181: .findBlockedAncestor(task);
182: String blockingAncestorMsg = NLS.bind(
183: Messages.PARENT_BLOCKED,
184: (new Object[] { MarkupParser
185: .escapeText(blockedAncestor.getName()) }));
186: showBlockingTasks(blockingAncestorMsg, blockedAncestor, buf);
187: } else {
188: startable = task instanceof IEditableTask
189: && task.getState() == ICompositeCheatSheetTask.NOT_STARTED;
190: }
191:
192: if (startable) {
193: addHyperlink(buf, CompositeCheatSheetPage.START_HREF,
194: START_IMAGE, Messages.COMPOSITE_PAGE_START_TASK);
195: }
196:
197: if (task instanceof IEditableTask
198: && task.getState() == ICompositeCheatSheetTask.COMPLETED) {
199: addHyperlink(buf, CompositeCheatSheetPage.REVIEW_TAG,
200: REVIEW_IMAGE, Messages.COMPOSITE_PAGE_REVIEW_TASK);
201: }
202:
203: if (isSkippable) {
204: String skipMessage;
205: if (task instanceof ITaskGroup) {
206: skipMessage = Messages.COMPOSITE_PAGE_SKIP_TASK_GROUP;
207: } else {
208: skipMessage = Messages.COMPOSITE_PAGE_SKIP_TASK;
209: }
210: addHyperlink(buf, CompositeCheatSheetPage.SKIP_HREF,
211: SKIP_IMAGE, skipMessage);
212: }
213:
214: if (!startable && !isBlocked) {
215: showSuccesorTaskLinks(task, buf);
216: }
217:
218: buf.append("</form>"); //$NON-NLS-1$
219:
220: lowerText.setText(buf.toString(), true, false);
221: getControl().setData(ICompositeCheatsheetTags.TASK, task);
222: form.reflow(true);
223: }
224:
225: private void showBlockingTasks(String message,
226: final ICompositeCheatSheetTask task, StringBuffer buf) {
227: buf.append("<p/>"); //$NON-NLS-1$
228: buf.append("<p>"); //$NON-NLS-1$
229: buf.append("<img href=\""); //$NON-NLS-1$
230: buf.append(WARNING_IMAGE);
231: buf.append("\"/> "); //$NON-NLS-1$
232: buf.append(message);
233: buf.append("</p>"); //$NON-NLS-1$// Add the list of blocking tasks
234:
235: ICompositeCheatSheetTask[] requiredTasks = task
236: .getRequiredTasks();
237: for (int i = 0; i < requiredTasks.length; i++) {
238: warnOfIncompleteTask(buf, requiredTasks[i]);
239: }
240: buf.append("<p>"); //$NON-NLS-1$
241: buf.append("</p>"); //$NON-NLS-1$
242: }
243:
244: private void addHyperlink(StringBuffer buf, String href,
245: String imageRef, String message) {
246: buf.append("<p><a href=\""); //$NON-NLS-1$
247: buf.append(href);
248: buf.append("\">"); //$NON-NLS-1$
249: buf.append("<img href=\""); //$NON-NLS-1$
250: buf.append(imageRef);
251: buf.append("\"/> "); //$NON-NLS-1$
252: buf.append(message);
253: buf.append("</a></p>"); //$NON-NLS-1$
254: }
255:
256: /*
257: * If this task is incomplete create a message to that effect
258: */
259: private void warnOfIncompleteTask(StringBuffer buf,
260: ICompositeCheatSheetTask task) {
261: if (task.getState() != ICompositeCheatSheetTask.COMPLETED
262: && task.getState() != ICompositeCheatSheetTask.SKIPPED) {
263: buf.append("<li>"); //$NON-NLS-1$
264: buf.append("<a href=\""); //$NON-NLS-1$
265: buf.append(CompositeCheatSheetPage.GOTO_TASK_TAG);
266: buf.append(task.getId());
267: buf.append("\">"); //$NON-NLS-1$
268: buf.append(NLS.bind(
269: Messages.COMPOSITE_PAGE_TASK_NOT_COMPLETE,
270: (new Object[] { MarkupParser.escapeText(task
271: .getName()) })));
272: buf.append("</a>"); //$NON-NLS-1$
273: buf.append("</li>"); //$NON-NLS-1$
274: }
275: }
276:
277: private void showSuccesorTaskLinks(ICompositeCheatSheetTask task,
278: StringBuffer buf) {
279: // Add the links to the next tasks
280: ICompositeCheatSheetTask[] successorTasks = new SuccesorTaskFinder(
281: task).getRecommendedSuccessors();
282: for (int i = 0; i < successorTasks.length; i++) {
283: ICompositeCheatSheetTask successor = successorTasks[i];
284: String message = NLS.bind(
285: Messages.COMPOSITE_PAGE_GOTO_TASK,
286: (new Object[] { MarkupParser.escapeText(successor
287: .getName()) }));
288: addHyperlink(buf, CompositeCheatSheetPage.GOTO_TASK_TAG
289: + successor.getId(), GOTO_IMAGE, message);
290: }
291: }
292:
293: }
|