View Javadoc

1   package com.explosion.utilities.preferences.editandrender.table;
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.event.ActionEvent;
26  import java.awt.event.ActionListener;
27  
28  import javax.swing.DefaultCellEditor;
29  import javax.swing.JButton;
30  import javax.swing.JCheckBox;
31  import javax.swing.JDialog;
32  import javax.swing.JFrame;
33  import javax.swing.JTable;
34  
35  import com.explosion.utilities.GeneralUtils;
36  import com.explosion.utilities.exception.ExceptionManagerFactory;
37  import com.explosion.utilities.preferences.Preference;
38  import com.explosion.utilities.preferences.dialogs.EditCollectionPreferenceDialog;
39  
40  public class CollectionPreferenceEditor extends DefaultCellEditor
41  {
42      private Preference preference = null;
43      private final EditCollectionPreferenceDialog dialog = null;
44      
45      public CollectionPreferenceEditor(JButton b, Component owner)
46      {
47          super(new JCheckBox()); //Unfortunately, the constructor expects a
48          // check box, combo box, or text field.
49          editorComponent = b;
50          setClickCountToStart(1); //This is usually 1 or 2.
51  
52          ((JButton) editorComponent).addActionListener(new ButtonActionListener(owner, this));
53      }
54  
55      protected void fireEditingStopped()
56      {
57          super.fireEditingStopped();
58      }
59  
60      public Object getCellEditorValue()
61      {
62          return preference;
63      }
64  
65      public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
66      {
67          preference = (Preference) value;
68          ((JButton) editorComponent).setText("..");
69  
70          return ((JButton) editorComponent);
71      }
72  
73      public Preference getPreference()
74      {
75          return preference;
76      }
77  
78  }
79  
80  class ButtonActionListener implements ActionListener
81  {
82  
83      private EditCollectionPreferenceDialog dialog;
84      private Component owner;
85      private CollectionPreferenceEditor editor;
86  
87      /***
88       *  
89       */
90      public ButtonActionListener(Component owner, CollectionPreferenceEditor editor)
91      {
92          this.owner = owner;
93          this.editor = editor;
94      }
95  
96      public void actionPerformed(ActionEvent e)
97      {
98          try
99          {
100             if (owner instanceof JDialog)
101                 dialog = new EditCollectionPreferenceDialog((JDialog) owner, "Edit values", true, editor.getPreference());
102             else if (owner instanceof JFrame)
103             {
104                 dialog = new EditCollectionPreferenceDialog((JFrame) owner, "Edit values", true, editor.getPreference());
105             }
106             
107             dialog.setSize(new Dimension(400,400));
108             GeneralUtils.centreWindowInParent(dialog);
109             dialog.setVisible(true);
110             
111         }
112         catch (Exception e1)
113         {
114             ExceptionManagerFactory.getExceptionManager().manageException(e1, "Exception caught while managing collection.");
115         }
116         finally
117 		{
118         	editor.fireEditingStopped();
119 		}
120 
121     }
122 
123 }
124