]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/osgi/BundlesView.java
68e7d54df2cd65add7ffa72448142dd9021f4368
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui.workbench / src / org / argeo / eclipse / ui / workbench / osgi / BundlesView.java
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.eclipse.ui.workbench.osgi;
20
21 import java.util.Comparator;
22
23 import org.argeo.eclipse.ui.ColumnViewerComparator;
24 import org.argeo.eclipse.ui.specific.EclipseUiSpecificUtils;
25 import org.argeo.eclipse.ui.workbench.WorkbenchUiPlugin;
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.graphics.Image;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.ui.part.ViewPart;
35 import org.osgi.framework.Bundle;
36 import org.osgi.framework.BundleContext;
37 import org.osgi.framework.Constants;
38
39 /**
40 * Overview of the bundles as a table. Equivalent to Equinox 'ss' console
41 * command.
42 */
43
44 // public class BundlesView {}
45
46 public class BundlesView extends ViewPart {
47 private TableViewer viewer;
48
49 @Override
50 public void createPartControl(Composite parent) {
51 viewer = new TableViewer(parent);
52 viewer.setContentProvider(new BundleContentProvider());
53 viewer.getTable().setHeaderVisible(true);
54
55 EclipseUiSpecificUtils.enableToolTipSupport(viewer);
56
57 // ID
58 TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
59 column.getColumn().setWidth(30);
60 column.getColumn().setText("ID");
61 column.getColumn().setAlignment(SWT.RIGHT);
62 column.setLabelProvider(new ColumnLabelProvider() {
63 private static final long serialVersionUID = -3122136344359358605L;
64
65 public String getText(Object element) {
66 return Long.toString(((Bundle) element).getBundleId());
67 }
68 });
69 new ColumnViewerComparator<Bundle>(column, new Comparator<Bundle>() {
70 public int compare(Bundle o1, Bundle o2) {
71 return (int) (o1.getBundleId() - o2.getBundleId());
72 }
73 });
74
75 // State
76 column = new TableViewerColumn(viewer, SWT.NONE);
77 column.getColumn().setWidth(18);
78 column.getColumn().setText("State");
79 column.setLabelProvider(new StateLabelProvider());
80 new ColumnViewerComparator<Bundle>(column, new Comparator<Bundle>() {
81 public int compare(Bundle o1, Bundle o2) {
82 return o1.getState() - o2.getState();
83 }
84 });
85
86 // Symbolic name
87 column = new TableViewerColumn(viewer, SWT.NONE);
88 column.getColumn().setWidth(300);
89 column.getColumn().setText("Symbolic Name");
90 column.setLabelProvider(new ColumnLabelProvider() {
91 private static final long serialVersionUID = -4280840684440451080L;
92
93 public String getText(Object element) {
94 return ((Bundle) element).getSymbolicName();
95 }
96 });
97 new ColumnViewerComparator<Bundle>(column, new Comparator<Bundle>() {
98 public int compare(Bundle o1, Bundle o2) {
99 return o1.getSymbolicName().compareTo(o2.getSymbolicName());
100 }
101 });
102
103 // Version
104 column = new TableViewerColumn(viewer, SWT.NONE);
105 column.getColumn().setWidth(150);
106 column.getColumn().setText("Version");
107 column.setLabelProvider(new ColumnLabelProvider() {
108 private static final long serialVersionUID = 6871926308708629989L;
109
110 public String getText(Object element) {
111 Bundle bundle = (org.osgi.framework.Bundle) element;
112 return bundle.getVersion().toString();
113 }
114 });
115 new ColumnViewerComparator<Bundle>(column, new Comparator<Bundle>() {
116 public int compare(Bundle o1, Bundle o2) {
117 return o1.getVersion().compareTo(o2.getVersion());
118 }
119 });
120
121 viewer.setInput(WorkbenchUiPlugin.getDefault().getBundle()
122 .getBundleContext());
123
124 }
125
126 @Override
127 public void setFocus() {
128 if (viewer != null)
129 viewer.getControl().setFocus();
130 }
131
132 /** Content provider managing the array of bundles */
133 private static class BundleContentProvider implements
134 IStructuredContentProvider {
135 private static final long serialVersionUID = -8533792785725875977L;
136
137 public Object[] getElements(Object inputElement) {
138 if (inputElement instanceof BundleContext) {
139 BundleContext bc = (BundleContext) inputElement;
140 return bc.getBundles();
141 }
142 return null;
143 }
144
145 public void dispose() {
146 }
147
148 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
149 }
150
151 }
152
153 /** Label provider for the state column */
154 private static class StateLabelProvider extends ColumnLabelProvider {
155 private static final long serialVersionUID = -7885583135316000733L;
156
157 @Override
158 public Image getImage(Object element) {
159 Integer state = ((Bundle) element).getState();
160 switch (state) {
161 case Bundle.UNINSTALLED:
162 return OsgiExplorerImages.INSTALLED;
163 case Bundle.INSTALLED:
164 return OsgiExplorerImages.INSTALLED;
165 case Bundle.RESOLVED:
166 return OsgiExplorerImages.RESOLVED;
167 case Bundle.STARTING:
168 return OsgiExplorerImages.STARTING;
169 case Bundle.STOPPING:
170 return OsgiExplorerImages.STARTING;
171 case Bundle.ACTIVE:
172 return OsgiExplorerImages.ACTIVE;
173 default:
174 return null;
175 }
176 }
177
178 @Override
179 public String getText(Object element) {
180 return null;
181 }
182
183 @Override
184 public String getToolTipText(Object element) {
185 Bundle bundle = (Bundle) element;
186 Integer state = bundle.getState();
187 switch (state) {
188 case Bundle.UNINSTALLED:
189 return "UNINSTALLED";
190 case Bundle.INSTALLED:
191 return "INSTALLED";
192 case Bundle.RESOLVED:
193 return "RESOLVED";
194 case Bundle.STARTING:
195 String activationPolicy = bundle.getHeaders()
196 .get(Constants.BUNDLE_ACTIVATIONPOLICY).toString();
197
198 // .get("Bundle-ActivationPolicy").toString();
199 // FIXME constant triggers the compilation failure
200 if (activationPolicy != null
201 && activationPolicy.equals(Constants.ACTIVATION_LAZY))
202 // && activationPolicy.equals("lazy"))
203 // FIXME constant triggers the compilation failure
204 // && activationPolicy.equals(Constants.ACTIVATION_LAZY))
205 return "<<LAZY>>";
206 return "STARTING";
207 case Bundle.STOPPING:
208 return "STOPPING";
209 case Bundle.ACTIVE:
210 return "ACTIVE";
211 default:
212 return null;
213 }
214 }
215 }
216 }