View Javadoc

1   package com.explosion.utilities.preferences.dialogs;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.awt.Component;
24  import java.awt.Dimension;
25  import java.awt.Frame;
26  import java.awt.GridBagConstraints;
27  import java.awt.GridBagLayout;
28  import java.awt.Insets;
29  import java.awt.event.ActionEvent;
30  import java.awt.event.MouseEvent;
31  import java.util.Arrays;
32  import java.util.Iterator;
33  import java.util.Vector;
34  
35  import javax.swing.BorderFactory;
36  import javax.swing.JButton;
37  import javax.swing.JDialog;
38  import javax.swing.JFrame;
39  import javax.swing.JPanel;
40  import javax.swing.JScrollPane;
41  import javax.swing.table.DefaultTableModel;
42  import javax.swing.table.TableModel;
43  
44  import org.apache.log4j.LogManager;
45  import org.apache.log4j.Logger;
46  
47  import com.explosion.utilities.RowHeaderUtil;
48  import com.explosion.utilities.exception.ExceptionManagerFactory;
49  import com.explosion.utilities.preferences.Preference;
50  import com.explosion.utilities.preferences.PreferenceChangeEvent;
51  import com.explosion.utilities.preferences.PreferenceChangeListener;
52  
53  /***
54   * @author Stephen Cowx Date created:@25-Jan-2003
55   * This dialog is specifically for editing preferences of type Collection.
56   * In other words, if a preference is of typoe collection and the user selects
57   * to edit the prefrence, this dialog box will be used to allow
58   * the user to add and remove items from their collection.
59   */
60  public class EditCollectionPreferenceDialog extends JDialog implements PreferenceChangeListener
61  {
62  	private static Logger log = LogManager
63  	.getLogger(EditCollectionPreferenceDialog.class);
64  	
65      private Component owner;
66  
67      private JPanel buttonPanel = new JPanel();
68  
69      private CollectionPreferenceTable table;
70  
71      private JScrollPane scrollPane = new JScrollPane();
72  
73      private JButton removeButton = new JButton("Delete");
74  
75      private JButton addButton = new JButton("Add");
76  
77      private JButton okButton = new JButton("Done");
78  
79      private Preference preference;
80      
81      private String initialSelection = null;
82  
83      private Vector trackedButtons = new Vector();
84  
85      private Vector buttons = new Vector();
86  
87      /***
88       * Constructor
89       */
90      public EditCollectionPreferenceDialog(JFrame owner, String title, boolean modal, Preference preference) throws Exception
91      {
92          super((Frame )owner, title, modal);
93          this.owner = owner;
94          this.preference = preference;
95          this.preference.addPreferenceChangeListener(this);
96          init();
97      }
98  
99      /***
100      * Constructor
101      */
102     public EditCollectionPreferenceDialog(JDialog owner, String title, boolean modal, Preference preference) throws Exception
103     {
104         super(owner, title, modal);
105         this.owner = owner;
106         this.preference = preference;
107         this.preference.addPreferenceChangeListener(this);
108         init();
109     }
110 
111     /***
112      * This method sets out the look and feel for this component
113      */
114     public void init() throws Exception
115     {
116         this.setSize(new Dimension(300, 200));
117         this.getContentPane().setLayout(new GridBagLayout());
118 
119         addButton.addActionListener(new java.awt.event.ActionListener()
120         {
121 
122             public void actionPerformed(ActionEvent e)
123             {
124                 addButton_actionPerformed(e);
125             }
126         });
127 
128         removeButton.addActionListener(new java.awt.event.ActionListener()
129         {
130 
131             public void actionPerformed(ActionEvent e)
132             {
133                 removeButton_actionPerformed(e);
134             }
135         });
136 
137         okButton.addActionListener(new java.awt.event.ActionListener()
138         {
139 
140             public void actionPerformed(ActionEvent e)
141             {
142                 okButton_actionPerformed(e);
143             }
144         });
145 
146         trackedButtons.addElement(removeButton);
147         
148         buttons.addElement(addButton);
149         buttons.addElement(removeButton);
150         buttons.addElement(okButton);
151 
152         buttonPanel.setLayout(new GridBagLayout());
153         buttonPanel.setBorder(BorderFactory.createEtchedBorder());
154         for (int i = 0; i < buttons.size(); i++)
155             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));
156 
157         table = new CollectionPreferenceTable(preference, this);
158         scrollPane.getViewport().add(table);
159         RowHeaderUtil util= new RowHeaderUtil();
160         util.setRowHeader(table, true);
161         
162         refresh();
163 
164         table.addMouseListener(new java.awt.event.MouseAdapter()
165         {
166 
167             public void mouseClicked(MouseEvent e)
168             {
169                 if ( e.getClickCount() > 1)
170                    list_mouseClicked(e);                 
171             }
172         });
173         
174         table.setRowSelectionAllowed(true);
175         table.setColumnSelectionAllowed(false);
176         
177         this.getContentPane().add(scrollPane, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 5, 5, 5), 10, 10));
178         this.getContentPane().add(buttonPanel, new GridBagConstraints(1, 0, 1, 1, 0.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 10, 10));
179 
180         checkEnabled();
181 
182     }
183 
184     void addButton_actionPerformed(ActionEvent e)
185     {
186         try
187         {
188             preference.addValue(null);
189         } catch (Exception ex)
190         {
191             com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(ex, null);
192         }
193     }
194 
195     void okButton_actionPerformed(ActionEvent e)
196     {
197         try
198         {
199             preference.save();
200             this.preference.removePreferenceChangeListener(this);
201             this.setVisible(false);
202             preference.firePreferenceChangedEvent(PreferenceChangeEvent.PROPERTY_VALUE_CHANGED);
203         } catch (Exception ex)
204         {
205             com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(ex, null);
206         }
207     }
208 
209     void list_mouseClicked(MouseEvent e)
210     {
211         checkEnabled();
212     }
213 
214     void removeButton_actionPerformed(ActionEvent e)
215     {
216         try
217         {
218             int selectedRows[] = table.getSelectedRows();
219     		Arrays.sort(selectedRows);
220     		for (int i = 0; i < selectedRows.length; i++) {
221     			this.preference.removeValue(table.getValueAt(selectedRows[i],0));
222     		}
223     	} catch (Exception ex)
224         {
225             com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(ex,"Exception caught while removing value.");
226         }
227     }
228     
229     public void preferenceChanged(PreferenceChangeEvent preferenceChangeEvent)
230     {
231         try
232         {
233            refresh();
234         } catch (Exception e)
235         {
236             ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while updating preference.");
237         }
238     }    
239 
240     public void refresh()
241     {
242     	log.debug("refreshing values");
243         Vector rows = new Vector();
244         Vector columns = new Vector();
245         columns.add("Values");
246     	
247         Vector row;
248         for (Iterator it = preference.getValues().iterator(); it.hasNext();)
249         {
250             Object object = it.next();
251         	row = new Vector();
252             row.add(object);
253             rows.add(row);
254         }
255         
256         TableModel model = new DefaultTableModel(rows, columns);
257         table.setModel(model);
258         
259         checkEnabled();
260         
261         repaint();
262         
263     }
264 
265     private void checkEnabled()
266     {
267         if (table.getModel().getRowCount() < 1)
268         {
269             this.removeButton.setEnabled(false);
270             
271             for (int t = 0; t < trackedButtons.size(); t++)
272                 ((JButton) trackedButtons.elementAt(t)).setEnabled(false);
273 
274             return;
275         }
276         
277         if (table.getSelectedRow() == -1)
278         {
279             this.removeButton.setEnabled(false);
280            
281             for (int t = 0; t < trackedButtons.size(); t++)
282                 ((JButton) trackedButtons.elementAt(t)).setEnabled(false);
283         }
284 
285         if (preference.isEditable())
286         {
287             this.removeButton.setEnabled(true);
288             
289             for (int t = 0; t < trackedButtons.size(); t++)
290                 ((JButton) trackedButtons.elementAt(t)).setEnabled(true);
291 
292         } else
293         {
294             this.removeButton.setEnabled(false);
295             
296             for (int t = 0; t < trackedButtons.size(); t++)
297                 ((JButton) trackedButtons.elementAt(t)).setEnabled(false);
298 
299         }
300     }
301 
302 
303 }