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.ui.workbench.osgi;
20  
21  import org.argeo.cms.ui.workbench.WorkbenchUiPlugin;
22  import org.argeo.eclipse.ui.ColumnViewerComparator;
23  import org.argeo.eclipse.ui.specific.EclipseUiSpecificUtils;
24  import org.eclipse.jface.viewers.ColumnLabelProvider;
25  import org.eclipse.jface.viewers.IStructuredContentProvider;
26  import org.eclipse.jface.viewers.TableViewer;
27  import org.eclipse.jface.viewers.TableViewerColumn;
28  import org.eclipse.jface.viewers.Viewer;
29  import org.eclipse.swt.SWT;
30  import org.eclipse.swt.widgets.Composite;
31  import org.eclipse.ui.part.ViewPart;
32  import org.osgi.framework.Bundle;
33  import org.osgi.framework.BundleContext;
34  
35  /**
36   * Overview of the bundles as a table. Equivalent to Equinox 'ss' console
37   * command.
38   */
39  public class BundlesView extends ViewPart {
40  	private TableViewer viewer;
41  
42  	@Override
43  	public void createPartControl(Composite parent) {
44  		viewer = new TableViewer(parent);
45  		viewer.setContentProvider(new BundleContentProvider());
46  		viewer.getTable().setHeaderVisible(true);
47  
48  		EclipseUiSpecificUtils.enableToolTipSupport(viewer);
49  
50  		// ID
51  		TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
52  		column.getColumn().setWidth(30);
53  		column.getColumn().setText("ID");
54  		column.getColumn().setAlignment(SWT.RIGHT);
55  		column.setLabelProvider(new ColumnLabelProvider() {
56  			private static final long serialVersionUID = -3122136344359358605L;
57  
58  			public String getText(Object element) {
59  				return Long.toString(((Bundle) element).getBundleId());
60  			}
61  		});
62  		new ColumnViewerComparator(column);
63  
64  		// State
65  		column = new TableViewerColumn(viewer, SWT.NONE);
66  		column.getColumn().setWidth(18);
67  		column.getColumn().setText("State");
68  		column.setLabelProvider(new StateLabelProvider());
69  		new ColumnViewerComparator(column);
70  
71  		// Symbolic name
72  		column = new TableViewerColumn(viewer, SWT.NONE);
73  		column.getColumn().setWidth(250);
74  		column.getColumn().setText("Symbolic Name");
75  		column.setLabelProvider(new ColumnLabelProvider() {
76  			private static final long serialVersionUID = -4280840684440451080L;
77  
78  			public String getText(Object element) {
79  				return ((Bundle) element).getSymbolicName();
80  			}
81  		});
82  		new ColumnViewerComparator(column);
83  
84  		// Version
85  		column = new TableViewerColumn(viewer, SWT.NONE);
86  		column.getColumn().setWidth(150);
87  		column.getColumn().setText("Version");
88  		column.setLabelProvider(new ColumnLabelProvider() {
89  			private static final long serialVersionUID = 6871926308708629989L;
90  
91  			public String getText(Object element) {
92  				Bundle bundle = (org.osgi.framework.Bundle) element;
93  				return bundle.getVersion().toString();
94  			}
95  		});
96  		new ColumnViewerComparator(column);
97  
98  		viewer.setInput(WorkbenchUiPlugin.getDefault().getBundle().getBundleContext());
99  
100 	}
101 
102 	@Override
103 	public void setFocus() {
104 		if (viewer != null)
105 			viewer.getControl().setFocus();
106 	}
107 
108 	/** Content provider managing the array of bundles */
109 	private static class BundleContentProvider implements IStructuredContentProvider {
110 		private static final long serialVersionUID = -8533792785725875977L;
111 
112 		public Object[] getElements(Object inputElement) {
113 			if (inputElement instanceof BundleContext) {
114 				BundleContext bc = (BundleContext) inputElement;
115 				return bc.getBundles();
116 			}
117 			return null;
118 		}
119 
120 		public void dispose() {
121 		}
122 
123 		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
124 		}
125 	}
126 }