Richard S. Hall | 186928b | 2006-09-28 19:54:30 +0000 | [diff] [blame] | 1 | /* |
| 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. Hall | 308c71e | 2006-04-04 12:42:45 +0000 | [diff] [blame] | 9 | * |
Richard S. Hall | 186928b | 2006-09-28 19:54:30 +0000 | [diff] [blame] | 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
Richard S. Hall | 308c71e | 2006-04-04 12:42:45 +0000 | [diff] [blame] | 11 | * |
Richard S. Hall | 186928b | 2006-09-28 19:54:30 +0000 | [diff] [blame] | 12 | * 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. Hall | fb3fe15 | 2006-04-04 13:11:15 +0000 | [diff] [blame] | 18 | */ |
Richard S. Hall | 308c71e | 2006-04-04 12:42:45 +0000 | [diff] [blame] | 19 | package org.apache.felix.shell.gui.impl; |
| 20 | |
| 21 | import java.awt.BorderLayout; |
| 22 | import java.awt.Dimension; |
Richard S. Hall | 308c71e | 2006-04-04 12:42:45 +0000 | [diff] [blame] | 23 | import java.beans.PropertyChangeEvent; |
| 24 | import java.beans.PropertyChangeListener; |
| 25 | |
| 26 | import javax.swing.*; |
| 27 | import javax.swing.event.*; |
| 28 | |
| 29 | import org.apache.felix.shell.gui.Plugin; |
| 30 | |
| 31 | public class ShellPanel extends JPanel implements PropertyChangeListener |
| 32 | { |
| 33 | private Activator m_activator = null; |
| 34 | private JPanel m_emptyPanel = null; |
| 35 | private JList m_pluginList = null; |
| 36 | private Plugin m_selectedPlugin = null; |
| 37 | private Runnable m_runnable = null; |
| 38 | |
| 39 | public ShellPanel(Activator activator) |
| 40 | { |
| 41 | m_activator = activator; |
| 42 | m_activator.addPropertyChangeListener(this); |
| 43 | |
| 44 | setLayout(new BorderLayout()); |
| 45 | JScrollPane scroll = null; |
| 46 | add(scroll = new JScrollPane(m_pluginList = new JList(new SimpleListModel())), BorderLayout.WEST); |
| 47 | scroll.setPreferredSize(new Dimension(150, scroll.getPreferredSize().height)); |
| 48 | add(m_emptyPanel = new JPanel(), BorderLayout.CENTER); |
| 49 | |
| 50 | createEventListeners(); |
| 51 | } |
| 52 | |
| 53 | public void propertyChange(PropertyChangeEvent event) |
| 54 | { |
| 55 | if (event.getPropertyName().equals(Activator.PLUGIN_LIST_PROPERTY)) |
| 56 | { |
| 57 | if (m_runnable == null) |
| 58 | { |
| 59 | m_runnable = new PropertyChangeRunnable(); |
| 60 | } |
| 61 | SwingUtilities.invokeLater(m_runnable); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | private void createEventListeners() |
| 66 | { |
| 67 | m_pluginList.addListSelectionListener(new ListSelectionListener() { |
| 68 | public void valueChanged(ListSelectionEvent event) |
| 69 | { |
| 70 | if (!event.getValueIsAdjusting()) |
| 71 | { |
| 72 | if (m_pluginList.getSelectedIndex() >= 0) |
| 73 | { |
| 74 | // Remove the current GUI. |
| 75 | if (m_selectedPlugin != null) |
| 76 | { |
| 77 | remove(m_selectedPlugin.getGUI()); |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | remove(m_emptyPanel); |
| 82 | } |
| 83 | |
| 84 | // Get the selected plugin GUI. |
| 85 | m_selectedPlugin = |
| 86 | m_activator.getPlugin(m_pluginList.getSelectedIndex()); |
| 87 | if (m_selectedPlugin != null) |
| 88 | { |
| 89 | // Display the selected plugin GUI. |
| 90 | add(m_selectedPlugin.getGUI(), BorderLayout.CENTER); |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | // Display the empty panel. |
| 95 | add(m_emptyPanel, BorderLayout.CENTER); |
| 96 | } |
| 97 | |
| 98 | revalidate(); |
| 99 | repaint(); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | }); |
| 104 | } |
| 105 | |
| 106 | private class SimpleListModel extends AbstractListModel |
| 107 | implements ListDataListener |
| 108 | { |
| 109 | private SimpleListModel() |
| 110 | { |
| 111 | } |
| 112 | |
| 113 | public int getSize() |
| 114 | { |
| 115 | return m_activator.getPluginCount(); |
| 116 | } |
| 117 | |
| 118 | public Object getElementAt(int index) |
| 119 | { |
| 120 | return m_activator.getPlugin(index).getName(); |
| 121 | } |
| 122 | |
| 123 | public void intervalAdded(ListDataEvent event) |
| 124 | { |
| 125 | fireIntervalAdded(this, event.getIndex0(), event.getIndex1()); |
| 126 | } |
| 127 | |
| 128 | public void intervalRemoved(ListDataEvent event) |
| 129 | { |
| 130 | fireIntervalRemoved(this, event.getIndex0(), event.getIndex1()); |
| 131 | } |
| 132 | |
| 133 | public void contentsChanged(ListDataEvent event) |
| 134 | { |
| 135 | fireContentsChanged(this, event.getIndex0(), event.getIndex1()); |
| 136 | } |
| 137 | |
| 138 | public void update() |
| 139 | { |
| 140 | fireContentsChanged(this, 0, -1); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | private class PropertyChangeRunnable implements Runnable |
| 145 | { |
| 146 | public void run() |
| 147 | { |
| 148 | ((SimpleListModel) m_pluginList.getModel()).update(); |
| 149 | |
| 150 | // Check to see if the selected component has been |
| 151 | // removed, if so, then reset the selected component |
| 152 | // to be an empty panel. |
| 153 | if ((m_selectedPlugin != null) && |
| 154 | !m_activator.pluginExists(m_selectedPlugin)) |
| 155 | { |
| 156 | m_pluginList.clearSelection(); |
| 157 | remove(m_selectedPlugin.getGUI()); |
| 158 | m_selectedPlugin = null; |
| 159 | add(m_emptyPanel, BorderLayout.CENTER); |
| 160 | revalidate(); |
| 161 | repaint(); |
| 162 | } |
| 163 | |
| 164 | if ((m_selectedPlugin == null) && (m_activator.getPluginCount() > 0)) |
| 165 | { |
| 166 | m_pluginList.setSelectedIndex(0); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |