Skip to main content
summaryrefslogtreecommitdiffstats
blob: c8fc5a9046d3a256cbfe26a4a9316f2a3cad2a2f (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*******************************************************************************
 * Copyright (c) 2004, 2009 Tasktop Technologies 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:
 *     Meghan Allen - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.monitor.usage.editors;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.mylyn.internal.monitor.usage.InteractionEventSummarySorter;
import org.eclipse.mylyn.internal.monitor.usage.UiUsageMonitorPlugin;
import org.eclipse.mylyn.internal.monitor.usage.common.UsageCountContentProvider;
import org.eclipse.mylyn.internal.monitor.usage.common.UsageCountLabelProvider;
import org.eclipse.mylyn.internal.monitor.usage.wizards.UsageSubmissionWizard;
import org.eclipse.mylyn.internal.monitor.usage.wizards.UsageSubmissionWizardDialog;
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.program.Program;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.browser.IWebBrowser;
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.internal.browser.WebBrowserPreference;
import org.eclipse.ui.internal.browser.WorkbenchBrowserSupport;
import org.eclipse.ui.statushandlers.StatusManager;

/**
 * @author Meghan Allen
 */
public class UsageSummaryReportEditorPart extends UsageEditorPart {

	public static final String ID = "org.eclipse.mylyn.monitor.usage.summary.editor"; //$NON-NLS-1$

	private static final long MAX_FILE_LENGTH = 1024 * 1024;

	private static final String DATE_FORMAT_STRING = "MMMMM d, h:mm a"; //$NON-NLS-1$

	// private static final int MAX_NUM_LINES = 1000;

	private Table table;

	private TableViewer tableViewer;

	private final String[] columnNames = new String[] { Messages.UsageSummaryReportEditorPart_Kind,
			Messages.UsageSummaryReportEditorPart_Id, Messages.UsageSummaryReportEditorPart_Count };

	@Override
	public void createPartControl(Composite parent) {
		super.createPartControl(parent);
		sForm.setText(new SimpleDateFormat(DATE_FORMAT_STRING).format(new Date()));
	}

	@Override
	protected void addSections(Composite composite, FormToolkit toolkit) {
		if (editorInput.getReportGenerator().getLastParsedSummary().getSingleSummaries().size() > 0) {
			createUsageSection(editorComposite, toolkit);
		}
	}

	private void createUsageSection(Composite parent, FormToolkit toolkit) {
		Section section = toolkit.createSection(parent, ExpandableComposite.TITLE_BAR);
		section.setText(Messages.UsageSummaryReportEditorPart_Usage_Details);
		section.setLayout(new GridLayout());
		section.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		Composite container = toolkit.createComposite(section);
		section.setClient(container);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		container.setLayout(layout);

		createTable(container, toolkit);
		createTableViewer();
		toolkit.paintBordersFor(container);
	}

	@Override
	protected void createActionSection(Composite parent, FormToolkit toolkit) {
		Section section = toolkit.createSection(parent, ExpandableComposite.TITLE_BAR);
		section.setText(Messages.UsageSummaryReportEditorPart_Actions);
		section.setLayout(new GridLayout());
		section.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		Composite topContainer = toolkit.createComposite(section);
		GridLayout topContainerLayout = new GridLayout();
		topContainerLayout.numColumns = 1;
		topContainer.setLayout(topContainerLayout);
		section.setClient(topContainer);

		Composite buttonContainer = toolkit.createComposite(topContainer);
		GridLayout buttonContainerLayout = new GridLayout();
		buttonContainerLayout.numColumns = 3;
		buttonContainer.setLayout(buttonContainerLayout);

		Button submitData = toolkit.createButton(buttonContainer, Messages.UsageSummaryReportEditorPart_Submit_To
				+ studyParameters.getStudyName(), SWT.PUSH | SWT.CENTER);
		submitData.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				submitData();
			}
		});

		Button viewFile = toolkit.createButton(buttonContainer, Messages.UsageSummaryReportEditorPart_View_File,
				SWT.PUSH | SWT.CENTER);
		viewFile.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				viewFile();
			}
		});

		Button viewStats = toolkit.createButton(buttonContainer,
				Messages.UsageSummaryReportEditorPart_View_Community_Statistics, SWT.PUSH | SWT.CENTER);
		viewStats.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				viewStats();
			}
		});

		if (studyParameters.getUsagePageUrl() == null || studyParameters.getUsagePageUrl().length() == 0) {
			viewStats.setEnabled(false);
		}

		Composite labelContainer = toolkit.createComposite(topContainer);
		GridLayout labelContainerLayout = new GridLayout();
		labelContainerLayout.numColumns = 1;
		labelContainer.setLayout(labelContainerLayout);
		Label submissionLabel = new Label(labelContainer, SWT.NONE);

		submissionLabel.setText(studyParameters.getFilteredIdSubmissionText());

	}

	/**
	 * TODO: Use the coommon api for opening web pages
	 */
	private void viewStats() {
		try {
			if (WebBrowserPreference.getBrowserChoice() == WebBrowserPreference.EXTERNAL) {
				try {
					IWorkbenchBrowserSupport support = PlatformUI.getWorkbench().getBrowserSupport();
					support.getExternalBrowser().openURL(new URL(studyParameters.getUsagePageUrl()));
				} catch (Exception e) {
					StatusManager.getManager()
							.handle(new Status(IStatus.ERROR, UiUsageMonitorPlugin.ID_PLUGIN, "Could not open url", e), StatusManager.SHOW | StatusManager.LOG); //$NON-NLS-1$
				}
			} else {
				IWebBrowser browser = null;
				int flags = 0;
				if (WorkbenchBrowserSupport.getInstance().isInternalWebBrowserAvailable()) {
					flags = IWorkbenchBrowserSupport.AS_EDITOR | IWorkbenchBrowserSupport.LOCATION_BAR
							| IWorkbenchBrowserSupport.NAVIGATION_BAR;

				} else {
					flags = IWorkbenchBrowserSupport.AS_EXTERNAL | IWorkbenchBrowserSupport.LOCATION_BAR
							| IWorkbenchBrowserSupport.NAVIGATION_BAR;
				}

				String generatedId = "org.eclipse.mylyn.web.browser-" + Calendar.getInstance().getTimeInMillis(); //$NON-NLS-1$
				browser = WorkbenchBrowserSupport.getInstance().createBrowser(flags, generatedId, null, null);
				browser.openURL(new URL(studyParameters.getUsagePageUrl()));
			}
		} catch (PartInitException e) {
			MessageDialog.openError(Display.getDefault().getActiveShell(), "Browser init error", //$NON-NLS-1$
					"Browser could not be initiated"); //$NON-NLS-1$
		} catch (MalformedURLException e) {
			MessageDialog.openError(Display.getDefault().getActiveShell(),
					Messages.UsageSummaryReportEditorPart_Url_Not_Found,
					Messages.UsageSummaryReportEditorPart_Url_Could_Not_Be_Opened);
		}
	}

	/**
	 * Only opens in workbench if file is small enough not to blow it up.
	 */
	private void viewFile() {
		File monitorFile = UiUsageMonitorPlugin.getDefault().getMonitorLogFile();

		if (monitorFile.length() <= MAX_FILE_LENGTH) {
			IFileStore fileStore = EFS.getLocalFileSystem().getStore(new Path(monitorFile.getAbsolutePath()));
			if (!fileStore.fetchInfo().isDirectory() && fileStore.fetchInfo().exists()) {
				IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				try {
					IDE.openEditorOnFileStore(page, fileStore);
				} catch (PartInitException e) {
				}
			}
		} else {
			boolean failed = false;
			failed = !Program.launch(monitorFile.getAbsolutePath());
			if (failed) {
				Program p = Program.findProgram(".txt"); //$NON-NLS-1$
				if (p != null) {
					p.execute(monitorFile.getAbsolutePath());
				}
			}
		}
	}

	private void submitData() {

		UsageSubmissionWizard submissionWizard = new UsageSubmissionWizard();

		WizardDialog dialog = new UsageSubmissionWizardDialog(PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow()
				.getShell(), submissionWizard);
		dialog.open();

	}

	private void createTable(Composite parent, FormToolkit toolkit) {
		int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.HIDE_SELECTION;
		table = toolkit.createTable(parent, style);
		TableLayout tlayout = new TableLayout();
		table.setLayout(tlayout);
		GridData wd = new GridData(GridData.FILL_HORIZONTAL);
		wd.heightHint = 300;
		table.setLayoutData(wd);

		table.setLinesVisible(true);
		table.setHeaderVisible(true);

		TableColumn column = new TableColumn(table, SWT.LEFT, 0);
		column.setText(columnNames[0]);
		column.setWidth(60);
		column.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				tableViewer.setSorter(new InteractionEventSummarySorter(InteractionEventSummarySorter.TYPE));

			}
		});

		column = new TableColumn(table, SWT.LEFT, 1);
		column.setText(columnNames[1]);
		column.setWidth(370);
		column.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				tableViewer.setSorter(new InteractionEventSummarySorter(InteractionEventSummarySorter.NAME));
			}
		});

		column = new TableColumn(table, SWT.LEFT, 2);
		column.setText(columnNames[2]);
		column.setWidth(50);
		column.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				tableViewer.setSorter(new InteractionEventSummarySorter(InteractionEventSummarySorter.USAGE_COUNT));
			}
		});

	}

	private void createTableViewer() {
		tableViewer = new TableViewer(table);
		tableViewer.setUseHashlookup(true);
		tableViewer.setColumnProperties(columnNames);

		tableViewer.setContentProvider(new UsageCountContentProvider());
		tableViewer.setLabelProvider(new UsageCountLabelProvider());
		tableViewer.setInput(editorInput.getReportGenerator());
	}

}

Back to the top