Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: da1c052caefb921b24b6cc8108773f9fcb94eb45 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*******************************************************************************
 * Copyright (c) 2000, 2017 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Sebastian Davids <sdavids@gmx.de> - bug 54630
 *     Trevor S. Kaufman <endante@gmail.com> - bug 156152
 *******************************************************************************/
package org.eclipse.team.internal.ui.synchronize;

import java.util.Calendar;
import java.util.Date;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.team.internal.ui.TeamUIMessages;
import org.eclipse.team.internal.ui.Utils;
import org.eclipse.team.ui.synchronize.ISynchronizeParticipant;

/**
 * A composite that allows editing a subscriber refresh schedule. A validator can be used to allow
 * containers to show page completion.
 *
 * @since 3.0
 */
public class ConfigureSynchronizeScheduleComposite extends Composite {

	private SubscriberRefreshSchedule schedule;
	private Button enableBackgroundRefresh;
	private Text timeInterval;
	private Combo hoursOrMinutes;
	private IPageValidator validator;
	private DateTime startTime;
	private Button repeatEvery;
	private Label synchronizeAt;

	public ConfigureSynchronizeScheduleComposite(Composite parent, SubscriberRefreshSchedule schedule, IPageValidator validator) {
		super(parent, SWT.NONE);
		this.schedule = schedule;
		this.validator = validator;
		createMainDialogArea(parent);
	}

	private void initializeValues() {
		boolean enableBackground = schedule.isEnabled();
		boolean hours = false;

		enableBackgroundRefresh.setSelection(enableBackground);

		long seconds = schedule.getRefreshInterval();
		if(seconds <= 60) {
			seconds = 60;
		}

		long minutes = seconds / 60;

		if(minutes >= 60) {
			minutes = minutes / 60;
			hours = true;
		}
		hoursOrMinutes.select(hours ? 0 : 1);
		timeInterval.setText(Long.toString(minutes));
		repeatEvery.setSelection(!schedule.getRunOnce());

		Date start = schedule.getRefreshStartTime();
		Calendar cal = Calendar.getInstance();
		if (start != null) {
			cal.setTime(start);
			startTime.setTime(cal.get(Calendar.HOUR_OF_DAY), cal
					.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
		} else {
			startTime.setTime(0, 0, 0); // default to 00:00:00
		}
	}

	protected void createMainDialogArea(Composite parent) {
		GC gc = new GC(parent);
		gc.setFont(JFaceResources.getDialogFont());
		FontMetrics	fontMetrics = gc.getFontMetrics();
		gc.dispose();
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		gridLayout.horizontalSpacing = Dialog.convertHorizontalDLUsToPixels(fontMetrics, IDialogConstants.HORIZONTAL_SPACING);
		gridLayout.verticalSpacing = Dialog.convertVerticalDLUsToPixels(fontMetrics, IDialogConstants.VERTICAL_SPACING);
		setLayout(gridLayout);
		setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		Composite area = this;

		createWrappingLabel(area, NLS.bind(TeamUIMessages.ConfigureRefreshScheduleDialog_1, new String[] { Utils.shortenText(SynchronizeView.MAX_NAME_LENGTH, schedule.getParticipant().getName()) }), 0, 3);

		enableBackgroundRefresh = new Button(area, SWT.CHECK);
		GridData gridData = new GridData();
		gridData.horizontalSpan = 3;
		enableBackgroundRefresh.setLayoutData(gridData);
		enableBackgroundRefresh.setText(TeamUIMessages.ConfigureRefreshScheduleDialog_3);
		enableBackgroundRefresh.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				updateEnablements();
			}
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});

		synchronizeAt = createIndentedLabel(area, TeamUIMessages.ConfigureRefreshScheduleDialog_3a, 20);

		startTime = new DateTime(area, SWT.TIME | SWT.BORDER);
		gridData = new GridData();
		gridData.horizontalSpan = 2;
		startTime.setLayoutData(gridData);

		repeatEvery = createIndentedButton(area, TeamUIMessages.ConfigureRefreshScheduleDialog_4, 20);
		repeatEvery.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				updateEnablements();
			}
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});

		timeInterval = new Text(area, SWT.BORDER | SWT.RIGHT);
		gridData = new GridData();
		gridData.widthHint = 35;
		timeInterval.setLayoutData(gridData);
		timeInterval.addModifyListener(e -> updateEnablements());
		timeInterval.addVerifyListener(e -> {
			String string = e.text;
			char[] chars = new char[string.length()];
			string.getChars(0, chars.length, chars, 0);
			for (char element : chars) {
				if (!('0' <= element && element <= '9')) {
					e.doit = false;
					return;
				}
			}
		});

		hoursOrMinutes = new Combo(area, SWT.READ_ONLY);
		hoursOrMinutes.setItems(TeamUIMessages.ConfigureRefreshScheduleDialog_5, TeamUIMessages.ConfigureRefreshScheduleDialog_6); //
		hoursOrMinutes.setLayoutData(new GridData());

		final Label label = new Label(area, SWT.WRAP);
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = 3;
		label.setLayoutData(gridData);
		label.setText(NLS.bind(TeamUIMessages.ConfigureRefreshScheduleDialog_2, new String[] { SubscriberRefreshSchedule.refreshEventAsString(schedule.getLastRefreshEvent()) }));

		initializeValues();
		updateEnablements();
	}

	public void saveValues() {
		if (enableBackgroundRefresh.getSelection()) {

			// start time
			Calendar cal = Calendar.getInstance();
			cal.set(Calendar.HOUR_OF_DAY, startTime.getHours());
			cal.set(Calendar.MINUTE, startTime.getMinutes());
			cal.set(Calendar.SECOND, startTime.getSeconds());
			schedule.setRefreshStartTime(cal.getTime());

			// repeat interval
			if (repeatEvery.getSelection()) {
				int hours = hoursOrMinutes.getSelectionIndex();
				try {
					long seconds = Long.parseLong(timeInterval.getText());
					if (hours == 0) {
						seconds = seconds * 3600;
					} else {
						seconds = seconds * 60;
					}
					schedule.setRefreshInterval(seconds);
				} catch (NumberFormatException e) {
					// keep old value
				}
			} else {
				schedule.setRunOnce(true);
			}
		}

		if(schedule.isEnabled() != enableBackgroundRefresh.getSelection()) {
			schedule.setEnabled(enableBackgroundRefresh.getSelection(), true /* allow to start */);
		}

		// update schedule
		ISynchronizeParticipant participant = schedule.getParticipant();
		if (!participant.isPinned() && schedule.isEnabled()) {
			participant.setPinned(MessageDialog.openQuestion(getShell(),
					NLS.bind(TeamUIMessages.ConfigureSynchronizeScheduleComposite_0, new String[] { Utils.getTypeName(participant) }),
					NLS.bind(TeamUIMessages.ConfigureSynchronizeScheduleComposite_1, new String[] { Utils.getTypeName(participant) })));
		}
		schedule.getRefreshable().setRefreshSchedule(schedule);
	}

	public void updateEnablements() {
		if (!enableBackgroundRefresh.getSelection()) {
			validator.setComplete(null);
		} else {
			if (repeatEvery.getSelection()) {
				try {
					long number = Long.parseLong(timeInterval.getText());
					if(number <= 0) {
						validator.setComplete(TeamUIMessages.ConfigureRefreshScheduleDialog_7);
					} else {
						validator.setComplete(null);
					}
				} catch (NumberFormatException e) {
					validator.setComplete(TeamUIMessages.ConfigureRefreshScheduleDialog_7);
				}
			}
		}
		synchronizeAt.setEnabled(enableBackgroundRefresh.getSelection());
		startTime.setEnabled(enableBackgroundRefresh.getSelection());
		repeatEvery.setEnabled(enableBackgroundRefresh.getSelection());
		timeInterval.setEnabled(enableBackgroundRefresh.getSelection() && repeatEvery.getSelection());
		hoursOrMinutes.setEnabled(enableBackgroundRefresh.getSelection() && repeatEvery.getSelection());
	}

	private Label createWrappingLabel(Composite parent, String text, int indent, int horizontalSpan) {
		Label label = new Label(parent, SWT.LEFT | SWT.WRAP);
		label.setText(text);
		GridData data = new GridData();
		data.horizontalSpan = horizontalSpan;
		data.horizontalAlignment = GridData.FILL;
		data.horizontalIndent = indent;
		data.grabExcessHorizontalSpace = true;
		data.widthHint = 400;
		label.setLayoutData(data);
		return label;
	}

	private static Label createIndentedLabel(Composite parent, String text, int indent) {
		Label label = new Label(parent, SWT.LEFT);
		label.setText(text);
		GridData data = new GridData();
		data.horizontalSpan = 1;
		data.horizontalAlignment = GridData.FILL;
		data.horizontalIndent = indent;
		label.setLayoutData(data);
		return label;
	}

	private static Button createIndentedButton(Composite parent, String text, int indent) {
		Button label = new Button(parent, SWT.CHECK);
		label.setText(text);
		GridData data = new GridData();
		data.horizontalSpan = 1;
		data.horizontalAlignment = GridData.FILL;
		data.horizontalIndent = indent;
		label.setLayoutData(data);
		return label;
	}
}

Back to the top