2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.cms
.ui
.workbench
.osgi
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
21 import org
.argeo
.cms
.ui
.workbench
.WorkbenchUiPlugin
;
22 import org
.argeo
.eclipse
.ui
.TreeParent
;
23 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
24 import org
.eclipse
.jface
.viewers
.TreeViewer
;
25 import org
.eclipse
.jface
.viewers
.Viewer
;
26 import org
.eclipse
.swt
.SWT
;
27 import org
.eclipse
.swt
.widgets
.Composite
;
28 import org
.eclipse
.ui
.part
.ViewPart
;
29 import org
.osgi
.framework
.Bundle
;
30 import org
.osgi
.framework
.BundleContext
;
32 /** The OSGi runtime from a module perspective. */
33 public class ModulesView
extends ViewPart
{
34 private TreeViewer viewer
;
37 public void createPartControl(Composite parent
) {
38 viewer
= new TreeViewer(parent
, SWT
.MULTI
| SWT
.H_SCROLL
| SWT
.V_SCROLL
);
39 viewer
.setContentProvider(new ModulesContentProvider());
40 viewer
.setLabelProvider(new ModulesLabelProvider());
41 viewer
.setInput(WorkbenchUiPlugin
.getDefault().getBundle()
46 public void setFocus() {
47 viewer
.getTree().setFocus();
50 private class ModulesContentProvider
implements ITreeContentProvider
{
51 private static final long serialVersionUID
= 3819934804640641721L;
53 public Object
[] getElements(Object inputElement
) {
54 return getChildren(inputElement
);
57 public Object
[] getChildren(Object parentElement
) {
58 if (parentElement
instanceof BundleContext
) {
59 BundleContext bundleContext
= (BundleContext
) parentElement
;
60 Bundle
[] bundles
= bundleContext
.getBundles();
62 List
<BundleNode
> modules
= new ArrayList
<BundleNode
>();
63 for (Bundle bundle
: bundles
) {
64 if (bundle
.getState() == Bundle
.ACTIVE
)
65 modules
.add(new BundleNode(bundle
, true));
67 return modules
.toArray();
68 } else if (parentElement
instanceof TreeParent
) {
69 return ((TreeParent
) parentElement
).getChildren();
75 public Object
getParent(Object element
) {
76 // TODO Auto-generated method stub
80 public boolean hasChildren(Object element
) {
81 if (element
instanceof TreeParent
) {
82 return ((TreeParent
) element
).hasChildren();
87 public void dispose() {
90 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
94 private class ModulesLabelProvider
extends StateLabelProvider
{
95 private static final long serialVersionUID
= 5290046145534824722L;
98 public String
getText(Object element
) {
99 if (element
instanceof BundleNode
)
100 return element
.toString() + " ["
101 + ((BundleNode
) element
).getBundle().getBundleId()
103 return element
.toString();