Skip to main content
summaryrefslogtreecommitdiffstats
blob: 43791c4bdc7e40e6f467042dae7c3d76f91558b0 (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
175
176
/*******************************************************************************
 * Copyright (c) 2014 Frank Becker 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:
 *     Frank Becker - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.bugzilla.rest.ui;

import java.text.MessageFormat;

import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.mylyn.commons.workbench.browser.BrowserUtil;
import org.eclipse.mylyn.internal.bugzilla.rest.core.BugzillaRestCore;
import org.eclipse.mylyn.internal.bugzilla.rest.core.IBugzillaRestConstants;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.internal.tasks.ui.util.TasksUiInternal;
import org.eclipse.mylyn.tasks.core.AbstractRepositoryConnector;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.ui.AbstractRepositoryConnectorUi;
import org.eclipse.mylyn.tasks.ui.wizards.AbstractRepositorySettingsPage;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
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.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
import org.eclipse.ui.forms.events.HyperlinkAdapter;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Hyperlink;

import com.google.common.base.Strings;

public class BugzillaRestRepositorySettingsPage extends AbstractRepositorySettingsPage {
	private static final String LABEL_VERSION_NUMBER = "5.0"; //$NON-NLS-1$

	private static final String DESCRIPTION = MessageFormat
			.format(Messages.BugzillaRestRepositorySettingsPage_SupportsVersionMessage, LABEL_VERSION_NUMBER);

	private Button useApiKey;

	private Text apiKey;

	private String oldApiKeyValue;

	private Hyperlink apikeyPreferenceLink;

	private Label apiKeyLabel;

	private final String userprefs = "/userprefs.cgi?tab=apikey"; //$NON-NLS-1$

	public BugzillaRestRepositorySettingsPage(TaskRepository taskRepository, AbstractRepositoryConnector connector,
			AbstractRepositoryConnectorUi connectorUi) {
		super(Messages.BugzillaRestRepositorySettingsPage_RestRepositorySetting, DESCRIPTION, taskRepository, connector,
				connectorUi);
		setNeedsAnonymousLogin(true);
		setNeedsEncoding(false);
		setNeedsAdvanced(true);
		setNeedsValidateOnFinish(true);
	}

	@Override
	public String getConnectorKind() {
		return BugzillaRestCore.CONNECTOR_KIND;
	}

	@Override
	protected void createAdditionalControls(Composite parent) {
		FormToolkit toolkit = new FormToolkit(TasksUiPlugin.getDefault().getFormColors(parent.getDisplay()));
		Composite apiKeyContainer = new Composite(parent, SWT.NONE);
		GridLayoutFactory.fillDefaults().margins(0, 0).numColumns(2).applyTo(apiKeyContainer);
		GridDataFactory.fillDefaults().grab(true, true).align(SWT.FILL, SWT.FILL).applyTo(apiKeyContainer);

		Label useApiKeyLabel = new Label(apiKeyContainer, SWT.NONE);
		useApiKeyLabel.setText(Messages.BugzillaRestRepositorySettingsPage_use_api_key);
		useApiKey = new Button(apiKeyContainer, SWT.CHECK | SWT.LEFT);
		useApiKey.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				boolean selectedValue = useApiKey.getSelection();
				apiKey.setEnabled(selectedValue);
				if (!selectedValue) {
					String apiKeyText = apiKey.getText();
					if (!Strings.isNullOrEmpty(apiKeyText)) {
						oldApiKeyValue = apiKeyText;
					}
					apiKey.setText(""); //$NON-NLS-1$
				} else {
					if (!Strings.isNullOrEmpty(oldApiKeyValue)) {
						apiKey.setText(oldApiKeyValue);
					}
				}
			}
		});

		apiKeyLabel = new Label(apiKeyContainer, SWT.NONE);
		apiKeyLabel.setText(Messages.BugzillaRestRepositorySettingsPage_api_key);
		apiKey = new Text(apiKeyContainer, SWT.BORDER);
		GridDataFactory.fillDefaults()
				.grab(true, false)
				.align(SWT.FILL, SWT.CENTER)
				.hint(300, SWT.DEFAULT)
				.applyTo(apiKey);
		apiKey.setEnabled(false);
		apikeyPreferenceLink = toolkit.createHyperlink(apiKeyContainer, "", SWT.NONE); //$NON-NLS-1$
		GridDataFactory.fillDefaults()
				.grab(true, false)
				.span(2, 1)
				.align(SWT.FILL, SWT.BEGINNING)
				.hint(300, SWT.DEFAULT)
				.applyTo(apikeyPreferenceLink);
		apikeyPreferenceLink.setBackground(apiKeyContainer.getBackground());
		apikeyPreferenceLink.addHyperlinkListener(new HyperlinkAdapter() {
			@Override
			public void linkActivated(HyperlinkEvent e) {
				BrowserUtil.openUrl(apikeyPreferenceLink.getText(), IWorkbenchBrowserSupport.AS_EXTERNAL);
			}
		});
		if (repository != null) {
			boolean useApiKeyValue = Boolean
					.parseBoolean(repository.getProperty(IBugzillaRestConstants.REPOSITORY_USE_API_KEY));
			useApiKey.setSelection(useApiKeyValue);
			String apiKeyValue = repository.getProperty(IBugzillaRestConstants.REPOSITORY_API_KEY);
			apiKey.setText(Strings.nullToEmpty(apiKeyValue));
			apiKey.setEnabled(useApiKeyValue);
		}

		updateURLInformation();
		serverUrlCombo.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				updateURLInformation();
			}
		});
		serverUrlCombo.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				updateURLInformation();
			}
		});
	}

	protected void updateURLInformation() {
		String url = serverUrlCombo.getText() + userprefs;
		apiKey.setToolTipText(
				NLS.bind(Messages.BugzillaRestRepositorySettingsPage_Please_create_or_copy_the_API_Key_from, url));
		apiKeyLabel.setToolTipText(
				NLS.bind(Messages.BugzillaRestRepositorySettingsPage_Please_create_or_copy_the_API_Key_from, url));
		apikeyPreferenceLink.setText(url);
		apikeyPreferenceLink
				.setToolTipText(NLS.bind(Messages.BugzillaRestRepositorySettingsPage_View_your_apikey_settings, url));
		apikeyPreferenceLink.setEnabled(TasksUiInternal.isValidUrl(url));
	}

	@Override
	public void applyTo(TaskRepository repository) {
		repository.setProperty(IBugzillaRestConstants.REPOSITORY_USE_API_KEY,
				Boolean.toString(useApiKey.getSelection()));
		repository.setProperty(IBugzillaRestConstants.REPOSITORY_API_KEY, apiKey.getText());
		super.applyTo(repository);
	}
}

Back to the top