blob: e39d89c53e1a3c64b3c1fd97d5754f05d7c8ffa8 [file] [log] [blame]
Richard S. Hall5c23fb92006-09-28 19:55:37 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
Richard S. Hall54d1e2e2006-04-05 14:19:38 +00009 *
Richard S. Hall5c23fb92006-09-28 19:55:37 +000010 * http://www.apache.org/licenses/LICENSE-2.0
Richard S. Hall54d1e2e2006-04-05 14:19:38 +000011 *
Richard S. Hall5c23fb92006-09-28 19:55:37 +000012 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
Richard S. Hall54d1e2e2006-04-05 14:19:38 +000018 */
19package org.apache.felix.shell.gui.plugin;
20
21import java.awt.BorderLayout;
22import java.awt.Component;
23import java.awt.event.ActionEvent;
24import java.awt.event.ActionListener;
25
26import javax.swing.*;
27import javax.swing.table.AbstractTableModel;
28
29import org.apache.felix.shell.gui.Plugin;
30import org.osgi.framework.*;
31import org.osgi.service.packageadmin.PackageAdmin;
32
33public class BundleListPlugin extends JPanel implements Plugin
34{
35 private BundleContext m_context = null;
36 private JTextField m_urlField = null;
37 private JButton m_installButton = null;
38 private JTable m_bundleTable = null;
39 private JButton m_startButton = null;
40 private JButton m_stopButton = null;
41 private JButton m_updateButton = null;
42 private JButton m_refreshButton = null;
43 private JButton m_uninstallButton = null;
Richard S. Hall82c55822007-03-08 14:34:15 +000044 private JButton m_shutdownButton = null;
Richard S. Hall54d1e2e2006-04-05 14:19:38 +000045
46 // Plugin interface methods.
47
48 public String getName()
49 {
50 return "Bundle List";
51 }
52
53 public Component getGUI()
54 {
55 return this;
56 }
57
58 // Implementation.
59
60 public BundleListPlugin(BundleContext context)
61 {
62 m_context = context;
63
64 // Create user interface components.
65 setLayout(new BorderLayout());
Richard S. Hall54d1e2e2006-04-05 14:19:38 +000066 add(createURLPanel(), BorderLayout.NORTH);
Richard S. Hall82c55822007-03-08 14:34:15 +000067 add(new JScrollPane(m_bundleTable = new JTable()), BorderLayout.CENTER);
Richard S. Hall54d1e2e2006-04-05 14:19:38 +000068 add(createButtonPanel(), BorderLayout.SOUTH);
69
70 // Set table model to display bundles.
71 m_bundleTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
72 m_bundleTable.setModel(new SimpleTableModel());
73 m_bundleTable.getColumnModel().getColumn(0).setPreferredWidth(75);
74 m_bundleTable.getColumnModel().getColumn(1).setPreferredWidth(100);
75 m_bundleTable.getColumnModel().getColumn(2).setPreferredWidth(350);
76
77 createEventListeners();
78 }
79
80 private JPanel createURLPanel()
81 {
82 JPanel panel = new JPanel(new BorderLayout());
83 panel.add(new JLabel("URL"), BorderLayout.WEST);
84 panel.add(m_urlField = new JTextField(20), BorderLayout.CENTER);
85 panel.add(m_installButton = new JButton("Install"), BorderLayout.EAST);
86 m_installButton.setMnemonic('I');
87 return panel;
88 }
89
90 private JPanel createButtonPanel()
91 {
92 JPanel panel = new JPanel();
93 panel.add(m_startButton = new JButton("Start"));
94 panel.add(m_stopButton = new JButton("Stop"));
95 panel.add(m_updateButton = new JButton("Update"));
96 panel.add(m_refreshButton = new JButton("Refresh"));
97 panel.add(m_uninstallButton = new JButton("Uninstall"));
Richard S. Hall82c55822007-03-08 14:34:15 +000098 panel.add(m_shutdownButton = new JButton("Shutdown"));
Richard S. Hall54d1e2e2006-04-05 14:19:38 +000099 m_startButton.setMnemonic('S');
100 m_stopButton.setMnemonic('p');
101 m_updateButton.setMnemonic('a');
102 m_refreshButton.setMnemonic('R');
103 m_uninstallButton.setMnemonic('U');
Richard S. Hall82c55822007-03-08 14:34:15 +0000104 m_shutdownButton.setMnemonic('d');
Richard S. Hall54d1e2e2006-04-05 14:19:38 +0000105 return panel;
106 }
107
108 private void createEventListeners()
109 {
110 // Listen for bundle events in order to update
111 // the GUI bundle list.
112 BundleListener bl = new BundleListener() {
113 public void bundleChanged(BundleEvent event)
114 {
115 ((SimpleTableModel) m_bundleTable.getModel()).update();
116 }
117 };
118 m_context.addBundleListener(bl);
119
120 // Create action listeners.
121 m_installButton.addActionListener(new ActionListener() {
122 public void actionPerformed(ActionEvent event)
123 {
124 if (m_urlField.getText().length() > 0)
125 {
126 try
127 {
128 m_context.installBundle(m_urlField.getText(), null);
129 }
130 catch (BundleException ex)
131 {
132 JOptionPane.showMessageDialog(
133 JOptionPane.getFrameForComponent(BundleListPlugin.this),
134 ex.getMessage(), "Error",
135 JOptionPane.ERROR_MESSAGE);
136 }
137 }
138 }
139 });
140
141 m_startButton.addActionListener(new ActionListener() {
142 public void actionPerformed(ActionEvent event)
143 {
144 int[] rows = m_bundleTable.getSelectedRows();
145 for (int i = 0; i < rows.length; i++)
146 {
147 try
148 {
149 m_context.getBundles()[rows[i]].start();
150 }
151 catch (BundleException ex)
152 {
153 JOptionPane.showMessageDialog(
154 JOptionPane.getFrameForComponent(BundleListPlugin.this),
155 ex.getMessage(), "Error",
156 JOptionPane.ERROR_MESSAGE);
157 }
158 }
159 }
160 });
161
162 m_stopButton.addActionListener(new ActionListener() {
163 public void actionPerformed(ActionEvent event)
164 {
165 int[] rows = m_bundleTable.getSelectedRows();
166 for (int i = 0; i < rows.length; i++)
167 {
168 try
169 {
170 m_context.getBundles()[rows[i]].stop();
171 }
172 catch (BundleException ex)
173 {
174 JOptionPane.showMessageDialog(
175 JOptionPane.getFrameForComponent(BundleListPlugin.this),
176 ex.getMessage(), "Error",
177 JOptionPane.ERROR_MESSAGE);
178 }
179 }
180 }
181 });
182
183 m_updateButton.addActionListener(new ActionListener() {
184 public void actionPerformed(ActionEvent event)
185 {
186 int[] rows = m_bundleTable.getSelectedRows();
187 for (int i = 0; i < rows.length; i++)
188 {
189 try
190 {
191 m_context.getBundles()[rows[i]].update();
192 }
193 catch (BundleException ex)
194 {
195 JOptionPane.showMessageDialog(
196 JOptionPane.getFrameForComponent(BundleListPlugin.this),
197 ex.getMessage(), "Error",
198 JOptionPane.ERROR_MESSAGE);
199 }
200 }
201 }
202 });
203
204 m_refreshButton.addActionListener(new ActionListener() {
205 public void actionPerformed(ActionEvent event)
206 {
207 // Get package admin service.
208 ServiceReference ref = m_context.getServiceReference(
209 PackageAdmin.class.getName());
210 if (ref == null)
211 {
212 JOptionPane.showMessageDialog(
213 JOptionPane.getFrameForComponent(BundleListPlugin.this),
214 "Unable to obtain PackageAdmin service.", "Error",
215 JOptionPane.ERROR_MESSAGE);
216 return;
217 }
218
219 PackageAdmin pa = (PackageAdmin) m_context.getService(ref);
220 if (pa == null)
221 {
222 JOptionPane.showMessageDialog(
223 JOptionPane.getFrameForComponent(BundleListPlugin.this),
224 "Unable to obtain PackageAdmin service.", "Error",
225 JOptionPane.ERROR_MESSAGE);
226 return;
227 }
228
229 pa.refreshPackages(null);
230 }
231 });
232
233 m_uninstallButton.addActionListener(new ActionListener() {
234 public void actionPerformed(ActionEvent event)
235 {
236 int[] rows = m_bundleTable.getSelectedRows();
237 // We need to uninstall in reverse order, otherwise
238 // the index will get messed up.
239 for (int i = rows.length - 1; i >= 0; i--)
240 {
241 try
242 {
243 m_context.getBundles()[rows[i]].uninstall();
244 }
245 catch (BundleException ex)
246 {
247 JOptionPane.showMessageDialog(
248 JOptionPane.getFrameForComponent(BundleListPlugin.this),
249 ex.getMessage(), "Error",
250 JOptionPane.ERROR_MESSAGE);
251 }
252 }
253 }
254 });
Richard S. Hall82c55822007-03-08 14:34:15 +0000255
256 m_shutdownButton.addActionListener(new ActionListener() {
257 public void actionPerformed(ActionEvent event)
258 {
259 Bundle systembundle = m_context.getBundle(0);
260 try
261 {
262 systembundle.stop();
263 }
264 catch (Exception ex)
265 {
266 System.out.println(ex.toString());
267 ex.printStackTrace(System.out);
268 }
269 }
270 });
Richard S. Hall54d1e2e2006-04-05 14:19:38 +0000271 }
272
273 private class SimpleTableModel extends AbstractTableModel
274 {
275 public int getRowCount()
276 {
277 return (m_context.getBundles() == null)
278 ? 0 : m_context.getBundles().length;
279 }
280
281 public int getColumnCount()
282 {
283 return 3;
284 }
285
286 public String getColumnName(int column)
287 {
288 if (column == 0)
289 {
290 return "Id";
291 }
292 else if (column == 1)
293 {
294 return "State";
295 }
296 else if (column == 2)
297 {
298 return "Location";
299 }
300 return "";
301 }
302
303 public Class getColumnClass(int column)
304 {
305 if (column == 0)
306 {
307 return Long.class;
308 }
309
310 return String.class;
311 }
312
313 public boolean isCellEditable(int row, int column)
314 {
315 return false;
316 }
317
318 public Object getValueAt(int row, int column)
319 {
320 if (column == 0)
321 {
322 return new Long(m_context.getBundles()[row].getBundleId());
323 }
324 else if (column == 1)
325 {
326 return getStateString(m_context.getBundles()[row].getState());
327 }
328 else if (column == 2)
329 {
330 String name = (String)
331 m_context.getBundles()[row].getHeaders().get(Constants.BUNDLE_NAME);
332 name = (name == null)
333 ? m_context.getBundles()[row].getLocation() : name;
334 return name;
335 }
336 return null;
337 }
338
339 public void update()
340 {
341 fireTableDataChanged();
342 }
343
344 private String getStateString(int state)
345 {
346 switch (state)
347 {
348 case Bundle.INSTALLED:
349 return "Installed";
350 case Bundle.RESOLVED:
351 return "Resolved";
352 case Bundle.STARTING:
353 return "Starting";
354 case Bundle.ACTIVE:
355 return "Active";
356 case Bundle.STOPPING:
357 return "Stopping";
358 }
359 return "[unknown]";
360 }
361 }
362}