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.ui.workbench.osgi;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
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;
31  
32  /** The OSGi runtime from a module perspective. */
33  public class ModulesView extends ViewPart {
34  	private TreeViewer viewer;
35  
36  	@Override
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()
42  				.getBundleContext());
43  	}
44  
45  	@Override
46  	public void setFocus() {
47  		viewer.getTree().setFocus();
48  	}
49  
50  	private class ModulesContentProvider implements ITreeContentProvider {
51  		private static final long serialVersionUID = 3819934804640641721L;
52  
53  		public Object[] getElements(Object inputElement) {
54  			return getChildren(inputElement);
55  		}
56  
57  		public Object[] getChildren(Object parentElement) {
58  			if (parentElement instanceof BundleContext) {
59  				BundleContext bundleContext = (BundleContext) parentElement;
60  				Bundle[] bundles = bundleContext.getBundles();
61  
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));
66  				}
67  				return modules.toArray();
68  			} else if (parentElement instanceof TreeParent) {
69  				return ((TreeParent) parentElement).getChildren();
70  			} else {
71  				return null;
72  			}
73  		}
74  
75  		public Object getParent(Object element) {
76  			// TODO Auto-generated method stub
77  			return null;
78  		}
79  
80  		public boolean hasChildren(Object element) {
81  			if (element instanceof TreeParent) {
82  				return ((TreeParent) element).hasChildren();
83  			}
84  			return false;
85  		}
86  
87  		public void dispose() {
88  		}
89  
90  		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
91  		}
92  	}
93  
94  	private class ModulesLabelProvider extends StateLabelProvider {
95  		private static final long serialVersionUID = 5290046145534824722L;
96  
97  		@Override
98  		public String getText(Object element) {
99  			if (element instanceof BundleNode)
100 				return element.toString() + " ["
101 						+ ((BundleNode) element).getBundle().getBundleId()
102 						+ "]";
103 			return element.toString();
104 		}
105 	}
106 }