Skip to main content
summaryrefslogtreecommitdiffstats
blob: ff1b62176c1155e712191671174eb863621710a6 (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
/*******************************************************************************
 * Copyright (c) 2013, 2014 Ericsson and others.
 * 
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * https://www.eclipse.org/legal/epl-2.0
 * 
 * SPDX-License-Identifier: EPL-2.0
 *
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.reviews.ui.spi.factories;

import org.apache.commons.lang.StringUtils;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.internal.reviews.ui.ReviewsUiPlugin;
import org.eclipse.mylyn.reviews.core.spi.remote.review.IReviewRemoteFactoryProvider;
import org.eclipse.mylyn.reviews.ui.spi.editor.AbstractReviewSection;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.TaskData;
import org.eclipse.mylyn.tasks.ui.editors.AbstractTaskEditorPage;
import org.eclipse.mylyn.tasks.ui.editors.TaskEditor;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.statushandlers.StatusManager;

/**
 * Support UI context and implementation neutral creation of controls for a single component that modifies the state of
 * a model object and it's related remote objects. (For convenience, the factory delegates back to the supplied UI
 * context.)
 * 
 * @author Miles Parker
 */
public abstract class AbstractUiFactory<EObjectType> implements IUiContext {

	String name;

	private final EObjectType object;

	private final IUiContext context;

	private Button button;

	public AbstractUiFactory(String name, IUiContext context, EObjectType object) {
		this.context = context;
		this.name = name;
		this.object = object;
	}

	/**
	 * Creates a control.
	 * 
	 * @param context
	 * @param parent
	 * @param toolkit
	 * @return the created control; may be null in the case where the factory isn't executable.
	 */
	public Control createControl(IUiContext context, Composite parent, FormToolkit toolkit) {
		button = toolkit.createButton(parent, name, SWT.PUSH);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				handleExecute();
			}
		});
		button.setEnabled(!isExecutableStateKnown() || isExecutable());
		return button;
	}

	private void handleExecute() {
		if (isExecutableStateKnown()) {
			execute();
		} else {
			handleExecutionStateError();
		}
	}

	protected void handleExecutionStateError() {
		String message = NLS.bind(
				"Cannot {0}. Try re-synchronizing the review task. If that fails, there may be a problem with your repository connection.", //$NON-NLS-1$
				StringUtils.removeEnd(name, "...")); //$NON-NLS-1$
		StatusManager.getManager().handle(new Status(IStatus.ERROR, ReviewsUiPlugin.PLUGIN_ID, message),
				StatusManager.SHOW | StatusManager.LOG);
	}

	protected abstract boolean isExecutableStateKnown();

	public abstract boolean isExecutable();

	public abstract void execute();

	public EObjectType getModelObject() {
		return object;
	}

	public Shell getShell() {
		return context.getShell();
	}

	public ITask getTask() {
		return context.getTask();
	}

	public TaskData getTaskData() {
		return context.getTaskData();
	}

	public TaskEditor getEditor() {
		return context.getEditor();
	}

	public TaskRepository getTaskRepository() {
		return context.getTaskRepository();
	}

	public IReviewRemoteFactoryProvider getFactoryProvider() {
		return context.getFactoryProvider();
	}

	public IUiContext getContext() {
		return context;
	}

	/**
	 * May return null, e.g. in the case where a factory was used outside of an editor context.
	 * 
	 * @return
	 */
	public AbstractTaskEditorPage getTaskEditorPage() {
		if (getContext() instanceof AbstractTaskEditorPage) {
			return (AbstractTaskEditorPage) getContext();
		} else if (getContext() instanceof AbstractReviewSection) {
			return ((AbstractReviewSection) getContext()).getReviewEditorPage();
		}
		return null;
	}
}

Back to the top