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.BorderLayout;
24  import java.awt.Component;
25  import java.awt.Dialog;
26  import java.awt.Dimension;
27  import java.awt.Frame;
28  import java.awt.event.ActionEvent;
29  import java.util.Vector;
30  
31  import javax.swing.JButton;
32  import javax.swing.JDialog;
33  import javax.swing.JLabel;
34  import javax.swing.JPanel;
35  import javax.swing.JTabbedPane;
36  
37  import com.explosion.utilities.GeneralUtils;
38  import com.explosion.utilities.preferences.PreferenceChangeEvent;
39  import com.explosion.utilities.preferences.PreferenceChangeListener;
40  
41  /***
42   * @author Stephen Cowx Date created:@31-Jan-2003
43   * This is a JDialog with an ok cancel and apply button set that
44   * is sensitive to the items being edited.
45   * 
46   * The items being edited should notify this dialog when they have been editied
47   * via the preferenceChangedListener interface and it will update it's buttons
48   * accordingly.
49   */
50  
51  public abstract class TableBasedPreferenceEditorDialog extends JDialog implements PreferenceChangeListener
52  {
53  
54      public JTabbedPane configTabbedPane = new JTabbedPane();
55  
56      public JPanel configPanel = new JPanel();
57  
58      private JPanel okCancelApplyPanel = new JPanel();
59  
60      private JButton applyButton = new JButton("Apply");
61  
62      private JButton cancelButton = new JButton("Cancel");
63  
64      private JButton okButton = new JButton("Ok");
65  
66      private boolean dirty = false;
67  
68      private boolean useTabbedPanes = true;
69  
70      private Dimension theSize = new Dimension(450, 400);
71  
72      private Vector dataTables = new Vector();
73      
74      private boolean editable = true;
75  
76      Component owner;
77  
78      public TableBasedPreferenceEditorDialog(Frame owner, String title)
79      {
80          super(owner, title);
81          try
82          {
83              init();
84              this.owner = owner;
85          } catch (Exception e)
86          {
87              com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, null);
88          }
89      }
90  
91      public TableBasedPreferenceEditorDialog(Frame owner, String title, boolean useTabbedPanes, boolean editable)
92      {
93          super(owner, title);
94          try
95          {
96              this.editable = editable;
97              this.useTabbedPanes = useTabbedPanes;
98              this.owner = owner;
99              init();
100         } catch (Exception e)
101         {
102             com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, null);
103         }
104     }
105 
106     public TableBasedPreferenceEditorDialog(Dialog owner, String title, boolean useTabbedPanes, boolean editable)
107     {
108         super(owner, title);
109         try
110         {
111             this.editable = editable;
112             this.useTabbedPanes = useTabbedPanes;
113             this.owner = owner;
114             init();
115         } catch (Exception e)
116         {
117             com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, null);
118         }
119     }
120 
121     public void setDataTables(Vector dataTables)
122     {
123         this.dataTables = dataTables;
124     }
125 
126     /***
127      * This is themethod which initialises
128      */
129     private void init() throws Exception
130     {
131 
132         okButton.setMnemonic('o');
133         applyButton.setMnemonic('a');
134         cancelButton.setMnemonic('c');
135 
136         if (this.editable)
137         {
138             okCancelApplyPanel.add(okButton, null); 
139             okCancelApplyPanel.add(applyButton, null);
140         }
141         else
142         {
143             cancelButton.setText("Ok");
144         }
145         
146         okCancelApplyPanel.add(cancelButton, null);
147         
148 
149         okButton.addActionListener(new java.awt.event.ActionListener()
150         {
151 
152             public void actionPerformed(ActionEvent e)
153             {
154                 okButton_actionPerformed(e);
155             }
156         });
157         applyButton.addActionListener(new java.awt.event.ActionListener()
158         {
159 
160             public void actionPerformed(ActionEvent e)
161             {
162                 applyButton_actionPerformed(e);
163             }
164         });
165         cancelButton.addActionListener(new java.awt.event.ActionListener()
166         {
167 
168             public void actionPerformed(ActionEvent e)
169             {
170                 cancelButton_actionPerformed(e);
171             }
172         });
173 
174         this.getContentPane().setLayout(new BorderLayout());
175         if (!editable)
176         {
177            this.getContentPane().add(new JLabel("Note: These values are not editable."), BorderLayout.NORTH);   
178         }
179         if (useTabbedPanes)
180             this.getContentPane().add(configTabbedPane, BorderLayout.CENTER);
181         else
182         {
183             configPanel.setLayout(new BorderLayout());
184             this.getContentPane().add(configPanel, BorderLayout.CENTER);
185         }
186 
187         this.getContentPane().add(okCancelApplyPanel, BorderLayout.SOUTH);
188 
189         this.setModal(true);
190         this.setSize(theSize);
191                 
192         GeneralUtils.centreWindowInParent(this);
193         this.applyButton.setEnabled(false);
194     }
195 
196     void okButton_actionPerformed(ActionEvent e)
197     {
198         if (dirty) apply();
199 
200         this.dispose();
201     }
202 
203     public JButton getOKButton()
204     {
205         return okButton;
206     }
207 
208     void applyButton_actionPerformed(ActionEvent e)
209     {
210         apply();
211 
212         /* Update the apply button */
213         applyButton.setEnabled(false);
214         dirty = false;
215 
216     }
217 
218     void cancelButton_actionPerformed(ActionEvent e)
219     {
220         this.dispose();
221     }
222 
223     public abstract void loadPreferences(Object preferences);
224 
225     /***
226      * This method saves the properties to persistent storage and calls the
227      * application.applyPreferences method
228      */
229     public abstract void apply();
230 
231     public void preferenceChanged(PreferenceChangeEvent preferenceChangeEvent)
232     {
233         dirty = true;
234         this.applyButton.setEnabled(true);
235     }
236     /***
237      * @return Returns the okCancelApplyPanel.
238      */
239     public JPanel getOkCancelApplyPanel()
240     {
241         return okCancelApplyPanel;
242     }
243 }