View Javadoc
1   //package org.argeo.eclipse.ui.workbench.osgi;
2   //public class BundlesView {}
3   
4   /*
5    * Copyright (C) 2007-2012 Argeo GmbH
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *         http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.argeo.cms.e4.monitoring;
20  
21  import javax.annotation.PostConstruct;
22  
23  import org.argeo.eclipse.ui.ColumnViewerComparator;
24  import org.argeo.eclipse.ui.specific.EclipseUiSpecificUtils;
25  import org.eclipse.e4.ui.di.Focus;
26  import org.eclipse.jface.viewers.ColumnLabelProvider;
27  import org.eclipse.jface.viewers.IStructuredContentProvider;
28  import org.eclipse.jface.viewers.TableViewer;
29  import org.eclipse.jface.viewers.TableViewerColumn;
30  import org.eclipse.jface.viewers.Viewer;
31  import org.eclipse.swt.SWT;
32  import org.eclipse.swt.widgets.Composite;
33  import org.osgi.framework.Bundle;
34  import org.osgi.framework.BundleContext;
35  import org.osgi.framework.FrameworkUtil;
36  
37  /**
38   * Overview of the bundles as a table. Equivalent to Equinox 'ss' console
39   * command.
40   */
41  public class BundlesView {
42  	private final static BundleContext bc = FrameworkUtil.getBundle(BundlesView.class).getBundleContext();
43  	private TableViewer viewer;
44  
45  	@PostConstruct
46  	public void createPartControl(Composite parent) {
47  		viewer = new TableViewer(parent);
48  		viewer.setContentProvider(new BundleContentProvider());
49  		viewer.getTable().setHeaderVisible(true);
50  
51  		EclipseUiSpecificUtils.enableToolTipSupport(viewer);
52  
53  		// ID
54  		TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
55  		column.getColumn().setWidth(30);
56  		column.getColumn().setText("ID");
57  		column.getColumn().setAlignment(SWT.RIGHT);
58  		column.setLabelProvider(new ColumnLabelProvider() {
59  			private static final long serialVersionUID = -3122136344359358605L;
60  
61  			public String getText(Object element) {
62  				return Long.toString(((Bundle) element).getBundleId());
63  			}
64  		});
65  		new ColumnViewerComparator(column);
66  
67  		// State
68  		column = new TableViewerColumn(viewer, SWT.NONE);
69  		column.getColumn().setWidth(18);
70  		column.getColumn().setText("State");
71  		column.setLabelProvider(new StateLabelProvider());
72  		new ColumnViewerComparator(column);
73  
74  		// Symbolic name
75  		column = new TableViewerColumn(viewer, SWT.NONE);
76  		column.getColumn().setWidth(250);
77  		column.getColumn().setText("Symbolic Name");
78  		column.setLabelProvider(new ColumnLabelProvider() {
79  			private static final long serialVersionUID = -4280840684440451080L;
80  
81  			public String getText(Object element) {
82  				return ((Bundle) element).getSymbolicName();
83  			}
84  		});
85  		new ColumnViewerComparator(column);
86  
87  		// Version
88  		column = new TableViewerColumn(viewer, SWT.NONE);
89  		column.getColumn().setWidth(250);
90  		column.getColumn().setText("Version");
91  		column.setLabelProvider(new ColumnLabelProvider() {
92  			private static final long serialVersionUID = 6871926308708629989L;
93  
94  			public String getText(Object element) {
95  				Bundle bundle = (org.osgi.framework.Bundle) element;
96  				return bundle.getVersion().toString();
97  			}
98  		});
99  		new ColumnViewerComparator(column);
100 
101 		viewer.setInput(bc);
102 
103 	}
104 
105 	@Focus
106 	public void setFocus() {
107 		if (viewer != null)
108 			viewer.getControl().setFocus();
109 	}
110 
111 	/** Content provider managing the array of bundles */
112 	private static class BundleContentProvider implements IStructuredContentProvider {
113 		private static final long serialVersionUID = -8533792785725875977L;
114 
115 		public Object[] getElements(Object inputElement) {
116 			if (inputElement instanceof BundleContext) {
117 				BundleContext bc = (BundleContext) inputElement;
118 				return bc.getBundles();
119 			}
120 			return null;
121 		}
122 
123 		public void dispose() {
124 		}
125 
126 		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
127 		}
128 	}
129 }