1 package com.explosion.utilities.preferences.dialogs;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.awt.Component;
24 import java.awt.Dimension;
25 import java.awt.GridBagConstraints;
26 import java.awt.GridBagLayout;
27 import java.awt.Insets;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.MouseEvent;
30 import java.util.Arrays;
31 import java.util.Iterator;
32 import java.util.Vector;
33
34 import javax.swing.BorderFactory;
35 import javax.swing.JButton;
36 import javax.swing.JPanel;
37 import javax.swing.JScrollPane;
38 import javax.swing.table.DefaultTableModel;
39 import javax.swing.table.TableModel;
40
41 import org.apache.log4j.LogManager;
42 import org.apache.log4j.Logger;
43
44 import com.explosion.utilities.RowHeaderUtil;
45 import com.explosion.utilities.exception.ExceptionManagerFactory;
46 import com.explosion.utilities.preferences.Preference;
47 import com.explosion.utilities.preferences.PreferenceChangeEvent;
48 import com.explosion.utilities.preferences.PreferenceChangeListener;
49
50 /***
51 * @author Stephen Cowx Date created:@25-Jan-2003
52 * This JPanel is specifically for editing preferences of type Collection.
53 * In other words, if a preference is of typoe collection and the user selects
54 * to edit the prefrence, this JPanel will be used to allow
55 * the user to add and remove items from their collection.
56 */
57 public class EditCollectionPreferenceJPanel extends JPanel implements PreferenceChangeListener
58 {
59 private static Logger log = LogManager
60 .getLogger(EditCollectionPreferenceJPanel.class);
61
62 private Component owner;
63
64 private JPanel buttonPanel = new JPanel();
65
66 private CollectionPreferenceTable table;
67
68 private JScrollPane scrollPane = new JScrollPane();
69
70 private JButton removeButton = new JButton("Delete");
71
72 private JButton addButton = new JButton("Add");
73
74 private Preference preference;
75
76 private String initialSelection = null;
77
78 private Vector trackedButtons = new Vector();
79
80 private Vector buttons = new Vector();
81
82 /***
83 * Constructor
84 */
85 public EditCollectionPreferenceJPanel(Preference preference) throws Exception
86 {
87 this.preference = preference;
88 this.preference.addPreferenceChangeListener(this);
89 init();
90 }
91
92 /***
93 * This method sets out the look and feel for this component
94 */
95 public void init() throws Exception
96 {
97 this.setSize(new Dimension(300, 200));
98 this.setLayout(new GridBagLayout());
99
100 addButton.addActionListener(new java.awt.event.ActionListener()
101 {
102
103 public void actionPerformed(ActionEvent e)
104 {
105 addButton_actionPerformed(e);
106 }
107 });
108
109 removeButton.addActionListener(new java.awt.event.ActionListener()
110 {
111
112 public void actionPerformed(ActionEvent e)
113 {
114 removeButton_actionPerformed(e);
115 }
116 });
117
118 trackedButtons.addElement(removeButton);
119
120 buttons.addElement(addButton);
121 buttons.addElement(removeButton);
122
123 buttonPanel.setLayout(new GridBagLayout());
124 buttonPanel.setBorder(BorderFactory.createEtchedBorder());
125 for (int i = 0; i < buttons.size(); i++)
126 buttonPanel.add(((JButton) buttons.elementAt(i)), new GridBagConstraints(0, i, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0));
127
128 table = new CollectionPreferenceTable(preference, this);
129 scrollPane.getViewport().add(table);
130 RowHeaderUtil util= new RowHeaderUtil();
131 util.setRowHeader(table, true);
132
133 refresh();
134
135 table.addMouseListener(new java.awt.event.MouseAdapter()
136 {
137
138 public void mouseClicked(MouseEvent e)
139 {
140 if ( e.getClickCount() > 1)
141 list_mouseClicked(e);
142 }
143 });
144
145 table.setRowSelectionAllowed(true);
146 table.setColumnSelectionAllowed(false);
147
148 this.add(scrollPane, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 5, 5, 5), 10, 10));
149 this.add(buttonPanel, new GridBagConstraints(1, 0, 1, 1, 0.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 10, 10));
150
151 checkEnabled();
152
153 }
154
155 void addButton_actionPerformed(ActionEvent e)
156 {
157 try
158 {
159 preference.addValue(null);
160 } catch (Exception ex)
161 {
162 com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(ex, null);
163 }
164 }
165
166 void list_mouseClicked(MouseEvent e)
167 {
168 checkEnabled();
169 }
170
171 void removeButton_actionPerformed(ActionEvent e)
172 {
173 try
174 {
175 int selectedRows[] = table.getSelectedRows();
176 Arrays.sort(selectedRows);
177 for (int i = 0; i < selectedRows.length; i++) {
178 this.preference.removeValue(table.getValueAt(selectedRows[i],0));
179 }
180 } catch (Exception ex)
181 {
182 com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(ex,"Exception caught while removing value.");
183 }
184 }
185
186 public void preferenceChanged(PreferenceChangeEvent preferenceChangeEvent)
187 {
188 try
189 {
190 refresh();
191 } catch (Exception e)
192 {
193 ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while updating preference.");
194 }
195 }
196
197 public void refresh()
198 {
199 log.debug("refreshing values");
200 Vector rows = new Vector();
201 Vector columns = new Vector();
202 columns.add("Values");
203
204 Vector row;
205 for (Iterator it = preference.getValues().iterator(); it.hasNext();)
206 {
207 Object object = it.next();
208 row = new Vector();
209 row.add(object);
210 rows.add(row);
211 }
212
213 TableModel model = new DefaultTableModel(rows, columns);
214 table.setModel(model);
215
216 checkEnabled();
217
218 repaint();
219
220 }
221
222 private void checkEnabled()
223 {
224 if (table.getModel().getRowCount() < 1)
225 {
226 this.removeButton.setEnabled(false);
227
228 for (int t = 0; t < trackedButtons.size(); t++)
229 ((JButton) trackedButtons.elementAt(t)).setEnabled(false);
230
231 return;
232 }
233
234 if (table.getSelectedRow() == -1)
235 {
236 this.removeButton.setEnabled(false);
237
238 for (int t = 0; t < trackedButtons.size(); t++)
239 ((JButton) trackedButtons.elementAt(t)).setEnabled(false);
240 }
241
242 if (preference.isEditable())
243 {
244 this.removeButton.setEnabled(true);
245
246 for (int t = 0; t < trackedButtons.size(); t++)
247 ((JButton) trackedButtons.elementAt(t)).setEnabled(true);
248
249 } else
250 {
251 this.removeButton.setEnabled(false);
252
253 for (int t = 0; t < trackedButtons.size(); t++)
254 ((JButton) trackedButtons.elementAt(t)).setEnabled(false);
255
256 }
257 }
258
259
260 }