View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
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
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
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.
15   */
16  package org.argeo.cms.e4.monitoring;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import javax.annotation.PostConstruct;
22  
23  import org.argeo.eclipse.ui.TreeParent;
24  import org.eclipse.e4.ui.di.Focus;
25  import org.eclipse.jface.viewers.ITreeContentProvider;
26  import org.eclipse.jface.viewers.TreeViewer;
27  import org.eclipse.jface.viewers.Viewer;
28  import org.eclipse.swt.SWT;
29  import org.eclipse.swt.widgets.Composite;
30  import org.osgi.framework.Bundle;
31  import org.osgi.framework.BundleContext;
32  import org.osgi.framework.FrameworkUtil;
33  
34  /** The OSGi runtime from a module perspective. */
35  public class ModulesView {
36  	private final static BundleContext bc = FrameworkUtil.getBundle(ModulesView.class).getBundleContext();
37  	private TreeViewer viewer;
38  
39  	@PostConstruct
40  	public void createPartControl(Composite parent) {
41  		viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
42  		viewer.setContentProvider(new ModulesContentProvider());
43  		viewer.setLabelProvider(new ModulesLabelProvider());
44  		viewer.setInput(bc);
45  	}
46  
47  	@Focus
48  	public void setFocus() {
49  		viewer.getTree().setFocus();
50  	}
51  
52  	private class ModulesContentProvider implements ITreeContentProvider {
53  		private static final long serialVersionUID = 3819934804640641721L;
54  
55  		public Object[] getElements(Object inputElement) {
56  			return getChildren(inputElement);
57  		}
58  
59  		public Object[] getChildren(Object parentElement) {
60  			if (parentElement instanceof BundleContext) {
61  				BundleContext bundleContext = (BundleContext) parentElement;
62  				Bundle[] bundles = bundleContext.getBundles();
63  
64  				List<BundleNode> modules = new ArrayList<BundleNode>();
65  				for (Bundle bundle : bundles) {
66  					if (bundle.getState() == Bundle.ACTIVE)
67  						modules.add(new BundleNode(bundle, true));
68  				}
69  				return modules.toArray();
70  			} else if (parentElement instanceof TreeParent) {
71  				return ((TreeParent) parentElement).getChildren();
72  			} else {
73  				return null;
74  			}
75  		}
76  
77  		public Object getParent(Object element) {
78  			// TODO Auto-generated method stub
79  			return null;
80  		}
81  
82  		public boolean hasChildren(Object element) {
83  			if (element instanceof TreeParent) {
84  				return ((TreeParent) element).hasChildren();
85  			}
86  			return false;
87  		}
88  
89  		public void dispose() {
90  		}
91  
92  		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
93  		}
94  	}
95  
96  	private class ModulesLabelProvider extends StateLabelProvider {
97  		private static final long serialVersionUID = 5290046145534824722L;
98  
99  		@Override
100 		public String getText(Object element) {
101 			if (element instanceof BundleNode)
102 				return element.toString() + " [" + ((BundleNode) element).getBundle().getBundleId() + "]";
103 			return element.toString();
104 		}
105 	}
106 }