Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2cb0e4a187cd651edfd2723fe3ad8021ea7d5586 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*******************************************************************************
 * Copyright (c) 2004, 2009 David Green 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:
 *     David Green - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.ui.editors;

import java.util.SortedSet;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.mylyn.commons.ui.CommonImages;
import org.eclipse.mylyn.commons.workbench.WorkbenchUtil;
import org.eclipse.mylyn.internal.tasks.ui.editors.TaskEditorExtensions.RegisteredTaskEditorExtension;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.ui.wizards.AbstractTaskRepositoryPageContribution;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Widget;

/**
 * A contribution that adds a section for 'Editor' on the task repository settings page.
 * 
 * @author David Green
 */
public class TaskEditorExtensionSettingsContribution extends AbstractTaskRepositoryPageContribution {

	private static final String LABEL_NONE = Messages.TaskEditorExtensionSettingsContribution_Plain_Text;

	private static final String LABEL_DEFAULT_SUFFIX = Messages.TaskEditorExtensionSettingsContribution__default_;

	private static final String DATA_EDITOR_EXTENSION = "editorExtension"; //$NON-NLS-1$

	private final SelectionListener listener = new SelectionAdapter() {
		@Override
		public void widgetSelected(SelectionEvent e) {
			selectedExtensionId = (String) ((Widget) e.getSource()).getData(DATA_EDITOR_EXTENSION);
			fireValidationRequired();
		}
	};

	private String selectedExtensionId;

	private Button avatarSupportButton;

	public TaskEditorExtensionSettingsContribution() {
		super(Messages.TaskEditorExtensionSettingsContribution_Editor,
				Messages.TaskEditorExtensionSettingsContribution_Select_the_capabilities_of_the_task_editor);
	}

	@Override
	public void applyTo(TaskRepository repository) {
		TaskEditorExtensions.setTaskEditorExtensionId(repository, selectedExtensionId == null ? "none" //$NON-NLS-1$
				: selectedExtensionId);
		repository.setProperty(TaskEditorExtensions.REPOSITORY_PROPERTY_AVATAR_SUPPORT,
				Boolean.toString(avatarSupportButton.getSelection()));
	}

	@Override
	public boolean canFlipToNextPage() {
		return true;
	}

	@Override
	public boolean isPageComplete() {
		return true;
	}

	@Override
	public Control createControl(Composite parentControl) {
		Composite parent = new Composite(parentControl, SWT.NONE);
		GridLayout layout = new GridLayout(1, true);
		layout.marginWidth = 0;
		parent.setLayout(layout);

		createGravatarControl(parent);

		Group group = new Group(parent, SWT.NONE);
		group.setText(Messages.TaskEditorExtensionSettingsContribution_Rendering_Group_Label);
		group.setLayout(new GridLayout(1, true));
		GridDataFactory.fillDefaults().grab(true, false).applyTo(group);
		createTaskEditorExtensionsControl(group);

		return parent;
	}

	private void createGravatarControl(Composite parent) {
		avatarSupportButton = new Button(parent, SWT.CHECK);
		avatarSupportButton.setText(Messages.TaskEditorExtensionSettingsContribution_Avatar_Button_Label);
		avatarSupportButton.setSelection(getRepository() != null
				&& Boolean.parseBoolean(getRepository().getProperty(
						TaskEditorExtensions.REPOSITORY_PROPERTY_AVATAR_SUPPORT)));
	}

	private void createTaskEditorExtensionsControl(Composite parent) {
		Composite infoComposite = new Composite(parent, SWT.NONE);
		GridLayoutFactory.fillDefaults().numColumns(2).applyTo(infoComposite);
		Label infoImage = new Label(infoComposite, SWT.NONE);
		infoImage.setImage(CommonImages.getImage(CommonImages.INFORMATION));
		Label infoLabel = new Label(infoComposite, SWT.NONE);
		infoLabel.setText(Messages.TaskEditorExtensionSettingsContribution_Rendering_Group_Info);

		String defaultExtensionId = TaskEditorExtensions.getDefaultTaskEditorExtensionId(getConnectorKind());
		selectedExtensionId = getRepository() == null
				? defaultExtensionId
				: TaskEditorExtensions.getTaskEditorExtensionId(getRepository());

		// configure a 'Plain Text' (none) button
		Button noneButton = new Button(parent, SWT.RADIO);
		String noneTitle = LABEL_NONE;
		boolean isDefault = defaultExtensionId == null || defaultExtensionId.length() == 0;
		if (isDefault) {
			noneTitle += LABEL_DEFAULT_SUFFIX;
		}
		noneButton.setText(noneTitle);
		noneButton.addSelectionListener(listener);

		boolean foundSelection = false;

		// now add selection buttons for all registered extensions
		SortedSet<RegisteredTaskEditorExtension> allEditorExtensions = TaskEditorExtensions.getTaskEditorExtensions();
		for (RegisteredTaskEditorExtension editorExtension : allEditorExtensions) {
			if (WorkbenchUtil.allowUseOf(editorExtension)) {
				String name = editorExtension.getName();
				isDefault = editorExtension.getId().equals(defaultExtensionId);
				if (isDefault) {
					name += LABEL_DEFAULT_SUFFIX;
				}
				Button button = new Button(parent, SWT.RADIO);
				button.setText(name);

				if (editorExtension.getId().equals(selectedExtensionId)) {
					foundSelection = true;
					button.setSelection(true);
				}
				button.setText(name);
				button.setData(DATA_EDITOR_EXTENSION, editorExtension.getId());
				button.addSelectionListener(listener);
			}
		}
		if (!foundSelection) {
			noneButton.setSelection(true);
		}
	}

	@Override
	public IStatus validate() {
		// nothing to validate
		return null;
	}

	/**
	 * only enabled when there are installed/registered task editor extensions.
	 */
	@Override
	public boolean isEnabled() {
		return !TaskEditorExtensions.getTaskEditorExtensions().isEmpty();
	}
}

Back to the top