Skip to main content
summaryrefslogtreecommitdiffstats
blob: f152db28c82a4ff117b73c3cf80777d6b6562d33 (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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*******************************************************************************
 * Copyright (c) 2015 OPCoach 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:
 *     Olivier Prouvost <olivier.prouvost@opcoach.com> - initial API and implementation (bug #451116)
 *     Simon Scholz <simon.scholz@vogella.com> - Bug 466785
 *******************************************************************************/
package org.eclipse.e4.tools.bundles.spy;

import java.util.Iterator;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.eclipse.e4.core.contexts.ContextInjectionFactory;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.internal.tools.bundles.spy.BundleDataFilter;
import org.eclipse.e4.internal.tools.bundles.spy.BundleDataProvider;
import org.eclipse.e4.ui.di.Focus;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
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.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import org.osgi.framework.FrameworkUtil;

/**
 * This class is the main part of the bundle spy. It displays a tableviewer with
 * all bundles
 */
public class BundleSpyPart {

	private static final String ICON_REFRESH = "icons/refresh.png";
	public static final String ICON_STATE_ACTIVE = "icons/state_active.png";
	public static final String ICON_STATE_STARTING = "icons/state_starting.png";
	public static final String ICON_STATE_STOPPING = "icons/state_stopping.png";
	public static final String ICON_STATE_RESOLVED = "icons/state_resolved.png";
	public static final String ICON_STATE_INSTALLED = "icons/state_installed.png";
	public static final String ICON_STATE_UNINSTALLED = "icons/state_uninstalled.png";
	public static final String ICON_START = "icons/start.png";
	public static final String ICON_STOP = "icons/stop.png";

	private TableViewer bundlesTableViewer;

	private Text filterText;

	private Button showOnlyFilteredElements;

	private BundleDataFilter bundleFilter;

	@Inject
	private IEclipseContext ctx;

	/** Store the values to set it when it is reopened */
	private static String lastFilterText = null;
	private static boolean lastShowFiltered = false;

	/**
	 * Create contents of the view part.
	 */
	@PostConstruct
	public void createControls(Composite parent) {
		ImageRegistry imgReg = initializeImageRegistry();

		// Set a filter in context (-> null at the begining).
		bundleFilter = new BundleDataFilter();
		ctx.set(BundleDataFilter.class, bundleFilter);

		parent.setLayout(new GridLayout(1, false));

		final Composite comp = new Composite(parent, SWT.NONE);
		comp.setLayout(new GridLayout(5, false));

		Button refreshButton = new Button(comp, SWT.FLAT);
		refreshButton.setImage(imgReg.get(ICON_REFRESH));
		refreshButton.setToolTipText("Refresh the contexts");
		refreshButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				bundlesTableViewer.refresh(true);
			}
		});

		filterText = new Text(comp, SWT.SEARCH | SWT.ICON_SEARCH | SWT.ICON_CANCEL);
		GridDataFactory.fillDefaults().hint(200, SWT.DEFAULT).applyTo(filterText);
		filterText.setMessage("Search data");
		filterText.setToolTipText(
				"Highlight the bundles where the contained objects contains this string.\n" + "Case is ignored.");
		if (lastFilterText != null)
			filterText.setText(lastFilterText);
		bundleFilter.setPattern(lastFilterText);
		filterText.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				String textToSearch = filterText.getText();
				lastFilterText = textToSearch;
				boolean enableButton = textToSearch.length() > 0;
				// Enable/disable button for filtering
				showOnlyFilteredElements.setEnabled(enableButton);

				// Then update filters and viewers
				bundleFilter.setPattern(textToSearch);
				setFilter();
				bundlesTableViewer.refresh(true);
			}

		});

		showOnlyFilteredElements = new Button(comp, SWT.CHECK);
		showOnlyFilteredElements.setText("Show Only Filtered");
		showOnlyFilteredElements.setToolTipText("Show only the filtered items in the bundle table ");
		showOnlyFilteredElements.setEnabled((lastFilterText != null) && (lastFilterText.length() > 0));
		showOnlyFilteredElements.setSelection(lastShowFiltered);
		showOnlyFilteredElements.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				lastShowFiltered = showOnlyFilteredElements.getSelection();
				setFilter();
			}
		});

		startButton = new Button(comp, SWT.FLAT);
		startButton.setImage(imgReg.get(ICON_START));
		startButton.setToolTipText("Start the selected bundles not yet started");
		startButton.setEnabled(false);
		startButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				IStructuredSelection sel = (IStructuredSelection) bundlesTableViewer.getSelection();
				Iterator<?> iter = sel.iterator();
				while (iter.hasNext()) {
					Bundle b = (Bundle) iter.next();
					try {
						b.start();
					} catch (BundleException e1) {
						e1.printStackTrace();
					}
				}
				bundlesTableViewer.refresh();
				updateButtonStatuses(sel);
			}
		});

		stopButton = new Button(comp, SWT.FLAT);
		stopButton.setImage(imgReg.get(ICON_STOP));
		stopButton.setToolTipText("Stop the selected bundles not yet stopped");
		stopButton.setEnabled(false);
		stopButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (MessageDialog.openConfirm(((Control) e.getSource()).getShell(), "Confirm Bundle Stop",
						"Stopping a bundle may cause problems in your current application.\nUse this button only for your bundles under testing\n\nDo you confirm you want to stop the selected started bundle(s) ? ")) {
					IStructuredSelection sel = (IStructuredSelection) bundlesTableViewer.getSelection();
					Iterator<?> iter = sel.iterator();
					while (iter.hasNext()) {
						Bundle b = (Bundle) iter.next();
						try {
							b.stop();
						} catch (BundleException e1) {
							e1.printStackTrace();
						}
					}
					bundlesTableViewer.refresh();
					updateButtonStatuses(sel);
				}
			}
		});

		// Create the customer table with 2 columns: firstname and name
		bundlesTableViewer = new TableViewer(parent);
		final Table cTable = bundlesTableViewer.getTable();
		cTable.setHeaderVisible(true);
		cTable.setLinesVisible(true);
		GridData gd_cTable = new GridData(SWT.FILL, SWT.FILL, true, true);
		// gd_cTable.verticalAlignment = SWT.TOP;
		cTable.setLayoutData(gd_cTable);

		// Create the first column for bundle name
		addColumn(bundlesTableViewer, 35, "State", BundleDataProvider.COL_STATE);
		addColumn(bundlesTableViewer, 200, "Bundle Name", BundleDataProvider.COL_NAME);
		addColumn(bundlesTableViewer, 200, "Version", BundleDataProvider.COL_VERSION);

		// Set input data and content provider (default ArrayContentProvider)
		bundlesTableViewer.setContentProvider(ArrayContentProvider.getInstance());

		// Get the list of bundles in platform using bundle context...
		BundleContext bc = FrameworkUtil.getBundle(BundleSpyPart.class).getBundleContext();
		bundlesTableViewer.setInput(bc.getBundles());

		bundlesTableViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				updateButtonStatuses((IStructuredSelection) event.getSelection());
			}
		});

		ColumnViewerToolTipSupport.enableFor(bundlesTableViewer);

	}

	/** Update the stop and start buttons depending on current selection */
	protected void updateButtonStatuses(IStructuredSelection selection) {
		// startButton is enabled if at least one bundle is not active
		// stopButton is enabled if at least one bundle is active
		boolean oneBundleIsActive = false;
		boolean oneBundleIsNotActive = false;

		Iterator<?> iter = selection.iterator();
		while (iter.hasNext()) {
			Bundle b = (Bundle) iter.next();
			oneBundleIsActive = oneBundleIsActive || (b.getState() == Bundle.ACTIVE);
			oneBundleIsNotActive = oneBundleIsNotActive || (b.getState() != Bundle.ACTIVE);
		}
		startButton.setEnabled(oneBundleIsNotActive);
		stopButton.setEnabled(oneBundleIsActive);

	}

	private void addColumn(final TableViewer parentTable, int width, String title, final int column) {
		TableViewerColumn col = new TableViewerColumn(bundlesTableViewer, SWT.NONE);
		col.getColumn().setWidth(width);
		col.getColumn().setText(title);

		final BundleDataProvider bdp = ContextInjectionFactory.make(BundleDataProvider.class, ctx);
		bdp.setColumn(column);
		col.setLabelProvider(bdp);

		col.getColumn().addSelectionListener(new SelectionAdapter() {

			private int turnAround = 1;

			@Override
			public void widgetSelected(SelectionEvent e) {
				turnAround *= -1;
				parentTable.setComparator(new ViewerComparator() {
					@Override
					public int compare(Viewer viewer, Object e1, Object e2) {
						if (BundleDataProvider.COL_STATE == column) {
							Bundle b1 = (Bundle) e1;
							Bundle b2 = (Bundle) e2;
							return turnAround(Integer.compare(b1.getState(), b2.getState()));
						}

						return turnAround(bdp.getText(e1).compareTo(bdp.getText(e2)));
					}
				});
			}

			private int turnAround(int compare) {
				return compare * turnAround;
			}
		});

	}

	private static final ViewerFilter[] NO_FILTER = new ViewerFilter[0];
	private Button stopButton;
	private Button startButton;

	/** Set the filter on table */
	public void setFilter() {

		if (showOnlyFilteredElements.isEnabled() && showOnlyFilteredElements.getSelection()) {
			bundlesTableViewer.setFilters(bundleFilter);
		} else {
			bundlesTableViewer.setFilters(NO_FILTER);
		}
	}

	@Focus
	public void setFocus() {
		bundlesTableViewer.getControl().setFocus();
	}

	private ImageRegistry initializeImageRegistry() {
		Bundle b = FrameworkUtil.getBundle(this.getClass());
		ImageRegistry imgReg = new ImageRegistry();
		imgReg.put(ICON_REFRESH, ImageDescriptor.createFromURL(b.getEntry(ICON_REFRESH)));
		imgReg.put(ICON_STATE_ACTIVE, ImageDescriptor.createFromURL(b.getEntry(ICON_STATE_ACTIVE)));
		imgReg.put(ICON_STATE_RESOLVED, ImageDescriptor.createFromURL(b.getEntry(ICON_STATE_RESOLVED)));
		imgReg.put(ICON_STATE_STARTING, ImageDescriptor.createFromURL(b.getEntry(ICON_STATE_STARTING)));
		imgReg.put(ICON_STATE_STOPPING, ImageDescriptor.createFromURL(b.getEntry(ICON_STATE_STOPPING)));
		imgReg.put(ICON_STATE_INSTALLED, ImageDescriptor.createFromURL(b.getEntry(ICON_STATE_INSTALLED)));
		imgReg.put(ICON_STATE_UNINSTALLED, ImageDescriptor.createFromURL(b.getEntry(ICON_STATE_UNINSTALLED)));
		imgReg.put(ICON_START, ImageDescriptor.createFromURL(b.getEntry(ICON_START)));
		imgReg.put(ICON_STOP, ImageDescriptor.createFromURL(b.getEntry(ICON_STOP)));

		ctx.set(ImageRegistry.class, imgReg);

		return imgReg;
	}

}

Back to the top