Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1999f37d818504a3aafc7cb7b71e291df8b58a4b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*******************************************************************************
 *  Copyright (c) 2005, 2008 IBM Corporation and others.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 * 
 *  Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.ui.internal.cheatsheets.composite.views;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Dictionary;

import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.cheatsheets.CheatSheetListener;
import org.eclipse.ui.cheatsheets.CheatSheetViewerFactory;
import org.eclipse.ui.cheatsheets.ICheatSheetEvent;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.internal.cheatsheets.Messages;
import org.eclipse.ui.internal.cheatsheets.composite.parser.ICompositeCheatsheetTags;
import org.eclipse.ui.internal.cheatsheets.state.MementoStateManager;
import org.eclipse.ui.internal.cheatsheets.views.CheatSheetViewer;
import org.eclipse.ui.internal.provisional.cheatsheets.IEditableTask;
import org.eclipse.ui.internal.provisional.cheatsheets.TaskEditor;

public class CheatsheetTaskEditor extends TaskEditor {
	private CheatSheetViewer viewer;
	private IEditableTask task;

	public void createControl(Composite parent, FormToolkit toolkit) {
		viewer = (CheatSheetViewer)CheatSheetViewerFactory.createCheatSheetView();
		viewer.createPartControl(parent);
	}
	
	public Control getControl() {
		return viewer.getControl();
	}


	public void setInput(IEditableTask task, IMemento memento) {
		this.task = task;	
		Dictionary params = task.getParameters();
		String id = (String)params.get(ICompositeCheatsheetTags.CHEATSHEET_TASK_ID);
		String path = (String)params.get(ICompositeCheatsheetTags.CHEATSHEET_TASK_PATH);
		boolean showIntro = true;
		String showIntroParam = (String)params.get(ICompositeCheatsheetTags.CHEATSHEET_TASK_SHOW_INTRO);
		if (showIntroParam != null) {
			showIntro = showIntroParam.equalsIgnoreCase("true"); //$NON-NLS-1$
		}
		
		MementoStateManager stateManager = new MementoStateManager(memento, task.getCompositeCheatSheet().getCheatSheetManager());
		if (path != null) {
			URL url;
			try {
				url = task.getInputUrl(path);
				if (id == null) { 
					id = task.getId();
				}
				if (url != null) {
				    viewer.setInput(id, task.getName(), url, stateManager, false);	
				} else {
					errorBadUrl(path);
				}
			} catch (MalformedURLException e) {
				errorBadUrl(path);
			}
		} else if (id != null){
		    viewer.setInput(id, stateManager);
		} else {
			viewer.showError(Messages.CHEATSHEET_TASK_NO_ID);
		}
		if (!showIntro) {
			viewer.advanceIntroItem();
		}
		viewer.addListener(new TaskListener());
	}

	private void errorBadUrl(String path) {
		String message = NLS.bind(Messages.ERROR_OPENING_FILE_IN_PARSER, (new Object[] {path}));			
		viewer.showError(message);
	}
	
	/*
	 * Listener for the cheatsheet used by this class
	 */
	private class TaskListener extends CheatSheetListener {

		public void cheatSheetEvent(ICheatSheetEvent event) {
			if (event.getEventType() == ICheatSheetEvent.CHEATSHEET_COMPLETED) {
				task.complete();
			}	
		}	
	}

	public void saveState(IMemento memento) {
		viewer.saveState(memento);
	}
}

Back to the top