1 package siouxsie.desktop.option.impl;
2
3 import java.awt.BorderLayout;
4 import java.awt.CardLayout;
5 import java.awt.Component;
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.List;
9
10 import javax.swing.DefaultListCellRenderer;
11 import javax.swing.DefaultListModel;
12 import javax.swing.JComponent;
13 import javax.swing.JLabel;
14 import javax.swing.JList;
15 import javax.swing.JPanel;
16 import javax.swing.JScrollPane;
17 import javax.swing.event.ListSelectionEvent;
18 import javax.swing.event.ListSelectionListener;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import siouxsie.desktop.option.CategoryDescription;
24 import siouxsie.desktop.option.IOptionPane;
25 import siouxsie.desktop.option.IOptionPaneInitializer;
26 import siouxsie.desktop.option.IPreferencesRenderer;
27 import siouxsie.desktop.option.OptionPaneDescription;
28
29 /**
30 * JList menu based preferences renderer.
31 *
32 * @author Arnaud Cogoluegnes
33 * @version $Id: ListPreferencesRenderer.java 143 2008-06-03 20:37:37Z acogo $
34 */
35 public class ListPreferencesRenderer implements IPreferencesRenderer {
36
37 private List<OptionPaneDescription> optionPaneDescriptions;
38
39 private List<CategoryDescription> categoryDescriptions;
40
41 private IOptionPaneInitializer initializerService;
42
43 private Collection<IOptionPane> optionPanes = new ArrayList<IOptionPane>();
44
45 private static final Log LOG = LogFactory
46 .getLog(ListPreferencesRenderer.class);
47
48 private JPanel mainPanel;
49
50 private JList list;
51
52
53
54
55
56
57 public JComponent getComponent() {
58 return mainPanel;
59 }
60
61
62
63
64
65
66 public void save() throws Exception {
67
68 for (IOptionPane pane : optionPanes) {
69 pane.save();
70 }
71 }
72
73
74
75
76
77
78 public void cancel() throws Exception {
79
80
81 }
82
83 public void init() {
84
85 final JPanel panel = new JPanel();
86 final CardLayout cardLayout = new CardLayout();
87 panel.setLayout(cardLayout);
88
89
90 panel.add(new JPanel(), "");
91
92 DefaultListModel listModel = new DefaultListModel();
93
94 for (OptionPaneDescription desc : optionPaneDescriptions) {
95 IOptionPane optionPane;
96 try {
97 optionPane = desc.getOptionPane() != null ? desc
98 .getOptionPane() : (IOptionPane) desc.getClass()
99 .newInstance();
100 initializerService.initialize(optionPane);
101 } catch (InstantiationException e) {
102 LOG.error("could not create option pane", e);
103 continue;
104 } catch (IllegalAccessException e) {
105 LOG.error("could not create option pane", e);
106 continue;
107 }
108
109 optionPane.init();
110
111 listModel.addElement(optionPane);
112
113 panel.add(optionPane.getComponent(), optionPane.getName());
114 optionPanes.add(optionPane);
115
116 }
117
118 list = new JList(listModel);
119
120
121 list.setCellRenderer(new DefaultListCellRenderer() {
122 @Override
123 public Component getListCellRendererComponent(JList list,
124 Object value, int index, boolean isSelected,
125 boolean cellHasFocus) {
126 Component comp = super.getListCellRendererComponent(list,
127 value, index, isSelected, cellHasFocus);
128 if (comp instanceof JLabel) {
129 ((JLabel) comp).setText(((IOptionPane) value).getName());
130 }
131 return comp;
132 }
133 });
134
135 list.addListSelectionListener(new ListSelectionListener() {
136 public void valueChanged(ListSelectionEvent e) {
137 if (!e.getValueIsAdjusting()) {
138 IOptionPane selection = (IOptionPane) list
139 .getSelectedValue();
140 cardLayout.show(panel, selection.getName());
141 }
142 }
143 });
144
145 mainPanel = new JPanel(new BorderLayout());
146 mainPanel.add(new JScrollPane(list), BorderLayout.WEST);
147 mainPanel.add(panel, BorderLayout.CENTER);
148 }
149
150 public List<CategoryDescription> getCategoryDescriptions() {
151 return categoryDescriptions;
152 }
153
154 public void setCategoryDescriptions(
155 List<CategoryDescription> categoryDescriptions) {
156 this.categoryDescriptions = categoryDescriptions;
157 }
158
159 public IOptionPaneInitializer getInitializerService() {
160 return initializerService;
161 }
162
163 public void setInitializerService(IOptionPaneInitializer initializerService) {
164 this.initializerService = initializerService;
165 }
166
167 public List<OptionPaneDescription> getOptionPaneDescriptions() {
168 return optionPaneDescriptions;
169 }
170
171 public void setOptionPaneDescriptions(
172 List<OptionPaneDescription> optionPaneDescriptions) {
173 this.optionPaneDescriptions = optionPaneDescriptions;
174 }
175
176 }