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