Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6acb56d60001988856d8d1002c79437f7b78579b (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
/*******************************************************************************
 * Copyright (c) 2007 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.equinox.internal.p2.ui.sdk.updates;

import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.Preferences.IPropertyChangeListener;
import org.eclipse.core.runtime.Preferences.PropertyChangeEvent;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.equinox.internal.p2.ui.sdk.*;
import org.eclipse.equinox.internal.p2.ui.sdk.prefs.PreferenceConstants;
import org.eclipse.equinox.p2.engine.Profile;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.ui.ProvUIImages;
import org.eclipse.equinox.p2.ui.dialogs.UpdateDialog;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.PopupDialog;
import org.eclipse.jface.preference.PreferenceDialog;
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.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.*;
import org.eclipse.ui.dialogs.PreferencesUtil;
import org.eclipse.ui.progress.WorkbenchJob;

/**
 * AutomaticUpdatesPopup is an async popup dialog for notifying
 * the user of updates.
 * 
 * @since 3.4
 */
public class AutomaticUpdatesPopup extends PopupDialog {
	public static final String[] ELAPSED = {ProvSDKMessages.AutomaticUpdateScheduler_5Minutes, ProvSDKMessages.AutomaticUpdateScheduler_15Minutes, ProvSDKMessages.AutomaticUpdateScheduler_30Minutes, ProvSDKMessages.AutomaticUpdateScheduler_60Minutes};
	private static final long MINUTE = 60 * 1000L;
	private static final String REMIND_HREF = "RMD"; //$NON-NLS-1$
	private static final String PREFS_HREF = "PREFS"; //$NON-NLS-1$
	public static final String AUTO_UPDATE_STATUS_ITEM = "AutomaticUpdatesPopup"; //$NON-NLS-1$
	private static final String DIALOG_SETTINGS_SECTION = "AutomaticUpdatesPopup"; //$NON-NLS-1$

	Preferences prefs;
	long remindDelay = -1L;
	IPropertyChangeListener listener;
	WorkbenchJob remindJob;
	IInstallableUnit[] toUpdate;
	Profile profile;
	boolean downloaded;
	boolean hidden = false;
	StatusLineCLabelContribution item;

	public AutomaticUpdatesPopup(IInstallableUnit[] toUpdate, Profile profile, boolean alreadyDownloaded, Preferences prefs) {
		super((Shell) null, PopupDialog.INFOPOPUPRESIZE_SHELLSTYLE | SWT.MODELESS, true, true, false, false, ProvSDKMessages.AutomaticUpdatesDialog_UpdatesAvailableTitle, null);
		downloaded = alreadyDownloaded;
		this.profile = profile;
		this.toUpdate = toUpdate;
		this.prefs = prefs;
		remindDelay = computeRemindDelay();
		listener = new IPropertyChangeListener() {

			public void propertyChange(PropertyChangeEvent event) {
				if (PreferenceConstants.PREF_REMIND_SCHEDULE.equals(event.getProperty()) || PreferenceConstants.PREF_REMIND_ELAPSED.equals(event.getProperty())) {
					computeRemindDelay();
					scheduleRemindJob();
				}
			}
		};
		prefs.addPropertyChangeListener(listener);
		createStatusLineItem();

	}

	protected Control createDialogArea(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		composite.setLayout(layout);
		Link infoLink = new Link(parent, SWT.MULTI | SWT.WRAP);
		if (downloaded)
			infoLink.setText(ProvSDKMessages.AutomaticUpdatesDialog_ClickToReviewDownloaded);
		else
			infoLink.setText(ProvSDKMessages.AutomaticUpdatesDialog_ClickToReviewNotDownloaded);
		infoLink.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				new UpdateDialog(null, toUpdate, profile).open();
			}
		});
		infoLink.setLayoutData(new GridData(GridData.FILL_BOTH));

		// spacer
		new Label(parent, SWT.NONE);

		Link remindLink = new Link(parent, SWT.MULTI | SWT.WRAP | SWT.RIGHT);
		remindLink.setText(NLS.bind(ProvSDKMessages.AutomaticUpdatesPopup_RemindAndPrefLink, new String[] {REMIND_HREF, prefs.getString(PreferenceConstants.PREF_REMIND_ELAPSED), PREFS_HREF}));
		remindLink.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (REMIND_HREF.equals(e.text)) {
					if (prefs.getBoolean(PreferenceConstants.PREF_REMIND_SCHEDULE)) {
						// We are already on a remind schedule, so just set up the reminder
						hide();
						scheduleRemindJob();
					} else {
						// We were not on a schedule.  Setting the pref value
						// will activate our listener and start the remind job
						hide();
						prefs.setValue(PreferenceConstants.PREF_REMIND_SCHEDULE, true);
					}
				}
				if (PREFS_HREF.equals(e.text)) {
					PreferenceDialog dialog = PreferencesUtil.createPreferenceDialogOn(getShell(), PreferenceConstants.PREF_PAGE_AUTO_UPDATES, null, null);
					dialog.open();
				}
			}
		});
		infoLink.setLayoutData(new GridData(GridData.FILL_BOTH));

		return composite;

	}

	protected IDialogSettings getDialogBoundsSettings() {
		IDialogSettings settings = ProvSDKUIActivator.getDefault().getDialogSettings();
		IDialogSettings section = settings.getSection(DIALOG_SETTINGS_SECTION);
		if (section == null) {
			section = settings.addNewSection(DIALOG_SETTINGS_SECTION);
		}
		return section;
	}

	public boolean close() {
		if (hidden)
			return false;
		prefs.removePropertyChangeListener(listener);
		cancelRemindJob();
		remindJob = null;
		listener = null;
		IStatusLineManager manager = getStatusLineManager();
		if (manager != null) {
			manager.remove(item);
		}
		manager.update(true);
		item.dispose();
		item = null;
		return super.close();
	}

	void scheduleRemindJob() {
		// Cancel any pending remind job if there is one
		if (remindJob != null)
			remindJob.cancel();
		// If no updates have been found, there is nothing to remind
		if (toUpdate == null)
			return;
		if (remindDelay < 0)
			return;
		remindJob = new WorkbenchJob(ProvSDKMessages.AutomaticUpdatesPopup_ReminderJobTitle) {
			public IStatus runInUIThread(IProgressMonitor monitor) {
				Shell shell = getShell();
				if (shell != null && !shell.isDisposed()) {
					show();
				}
				return Status.OK_STATUS;
			}
		};
		remindJob.setSystem(true);
		remindJob.setPriority(Job.INTERACTIVE);
		remindJob.schedule(remindDelay);

	}

	/*
	 * Computes the number of milliseconds for the delay
	 * in reminding the user of updates
	 */
	long computeRemindDelay() {
		if (prefs.getBoolean(PreferenceConstants.PREF_REMIND_SCHEDULE)) {
			String elapsed = prefs.getString(PreferenceConstants.PREF_REMIND_ELAPSED);
			for (int d = 0; d < ELAPSED.length; d++)
				if (ELAPSED[d].equals(elapsed))
					switch (d) {
						case 0 :
							// 5 minutes
							return 5 * MINUTE;
						case 1 :
							// 15 minutes
							return 15 * MINUTE;
						case 2 :
							// 30 minutes
							return 30 * MINUTE;
						case 3 :
							// 1 hour
							return 60 * MINUTE;
					}
		}
		return -1L;
	}

	private void cancelRemindJob() {
		if (remindJob != null) {
			remindJob.cancel();
		}
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(ProvSDKMessages.AutomaticUpdatesDialog_UpdatesAvailableTitle);
	}

	private void createStatusLineItem() {
		item = new StatusLineCLabelContribution(AUTO_UPDATE_STATUS_ITEM, 5);
		item.addListener(SWT.MouseDown, new Listener() {
			public void handleEvent(Event event) {
				show();
			}
		});
		item.setTooltip(ProvSDKMessages.AutomaticUpdatesDialog_UpdatesAvailableTitle);
		item.setImage(ProvUIImages.getImage(ProvUIImages.IMG_TOOL_UPDATE));
		IStatusLineManager manager = getStatusLineManager();
		if (manager != null) {
			manager.add(item);
		}
		item.setVisible(false);

	}

	IStatusLineManager getStatusLineManager() {
		// TODO  YUCK!  Am I missing an easier way to do this??
		IWorkbenchPartSite site = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart().getSite();
		if (site instanceof IViewSite) {
			return ((IViewSite) site).getActionBars().getStatusLineManager();
		} else if (site instanceof IEditorSite) {
			return ((IViewSite) site).getActionBars().getStatusLineManager();
		}
		return null;
	}

	public void hide() {
		hidden = true;
		Shell shell = getShell();
		if (shell != null && !shell.isDisposed())
			shell.setVisible(false);
		if (item != null) {
			item.setVisible(true);
			updateStatusLine();
		}
	}

	public void show() {
		Shell shell = getShell();
		if (shell != null && !shell.isDisposed()) {
			shell.setVisible(true);
			hidden = false;
			if (item != null) {
				item.setVisible(false);
				updateStatusLine();
			}

		}
	}

	void updateStatusLine() {
		IStatusLineManager manager = getStatusLineManager();
		if (manager != null)
			manager.update(true);
	}

}

Back to the top