Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/virtuosotechnologies/asaph/standardgui/EditorPrefs.java


1   /*
2   ================================================================================
3   
4     FILE:  EditorPrefs.java
5     
6     PROJECT:
7     
8       Asaph
9     
10    CONTENTS:
11    
12      Prefs pane for the editor
13    
14    PROGRAMMERS:
15    
16      Daniel Azuma (DA)  <dazuma@kagi.com>
17    
18    COPYRIGHT:
19    
20      Copyright (C) 2003  Daniel Azuma  (dazuma@kagi.com)
21      
22      This program is free software; you can redistribute it and/or
23      modify it under the terms of the GNU General Public License as
24      published by the Free Software Foundation; either version 2
25      of the License, or (at your option) any later version.
26      
27      This program is distributed in the hope that it will be useful,
28      but WITHOUT ANY WARRANTY; without even the implied warranty of
29      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30      GNU General Public License for more details.
31      
32      You should have received a copy of the GNU General Public
33      License along with this program; if not, write to
34        Free Software Foundation, Inc.
35        59 Temple Place, Suite 330
36        Boston, MA 02111-1307 USA
37  
38  ================================================================================
39  */
40  
41  
42  package com.virtuosotechnologies.asaph.standardgui;
43  
44  
45  import java.lang.reflect.Method;
46  import java.awt.Insets;
47  import java.awt.BorderLayout;
48  import java.awt.GridBagLayout;
49  import java.awt.GridBagConstraints;
50  import java.awt.event.ItemListener;
51  import java.awt.event.ItemEvent;
52  import java.util.EventObject;
53  import java.util.EventListener;
54  import java.util.prefs.Preferences;
55  import javax.swing.JComponent;
56  import javax.swing.JPanel;
57  import javax.swing.JScrollPane;
58  import javax.swing.JCheckBox;
59  import javax.swing.JSpinner;
60  import javax.swing.JLabel;
61  import javax.swing.BorderFactory;
62  import javax.swing.SpinnerNumberModel;
63  import javax.swing.event.ChangeListener;
64  import javax.swing.event.ChangeEvent;
65  
66  import com.virtuosotechnologies.lib.util.EventBroadcastHelper;
67  import com.virtuosotechnologies.lib.swing.ScrollablePanel;
68  
69  import com.virtuosotechnologies.asaph.maingui.PrefsPaneProvider;
70  
71  
72  /**
73   * Prefs pane for the editor 
74   */
75  /*package*/ class EditorPrefs
76  implements PrefsPaneProvider
77  {
78    private static final String STR_EditorPrefs_UndoLevelsLabel =
79      ResourceAccess.Strings.buildString("EditorPrefs_UndoLevelsLabel");
80    private static final String STR_EditorPrefs_SmartQuotesCheck =
81      ResourceAccess.Strings.buildString("EditorPrefs_SmartQuotesCheck");
82    
83    private static final String EDITOR_PREFSNODE = "editor";
84    private static final String UNDOLEVELS_PREF = "undoLevels";
85    private static final String AUTO_SMARTQUOTES_PREF = "autoSmartQuotes";
86    
87    
88    public static class PrefsChangedEvent
89    extends EventObject
90    {
91      public PrefsChangedEvent(
92        Object source)
93      {
94        super(source);
95      }
96    }
97    
98    
99    public static interface PrefsListener
100   extends EventListener
101   {
102     public static final Method PREFS_CHANGED_METHOD =
103       EventBroadcastHelper.getUniqueListenerMethod(PrefsListener.class);
104     
105     public void prefsChanged(
106       PrefsChangedEvent ev);
107   }
108   
109   
110   private Preferences prefs_;
111   
112   private int undoLevels_;
113   private boolean autoSmartQuotes_;
114   
115   private EventBroadcastHelper broadcaster_;
116   
117   
118   /**
119    * Constructor
120    */
121   /*package*/ EditorPrefs()
122   {
123     Preferences rootPrefs = Preferences.userNodeForPackage(EditorPrefs.class);
124     prefs_ = rootPrefs.node(EDITOR_PREFSNODE);
125     undoLevels_ = prefs_.getInt(UNDOLEVELS_PREF, 20);
126     autoSmartQuotes_ = prefs_.getBoolean(AUTO_SMARTQUOTES_PREF, true);
127     broadcaster_ = new EventBroadcastHelper(PrefsListener.class);
128   }
129   
130   
131   /*package*/ int getUndoLevels()
132   {
133     return undoLevels_;
134   }
135   
136   
137   /*package*/ boolean isAutoSmartQuoting()
138   {
139     return autoSmartQuotes_;
140   }
141   
142   
143   /*package*/ void addPrefsListener(
144     PrefsListener listener)
145   {
146     broadcaster_.addListenerWeak(listener);
147   }
148   
149   
150   /*package*/ void removePrefsListener(
151     PrefsListener listener)
152   {
153     broadcaster_.removeListener(listener);
154   }
155   
156   
157   /**
158    * This method is called when the prefs window is opened. The provider
159    * should return a JComponent implementing the prefs pane, and ensure that it
160    * reflects the current settings.
161    *
162    * @param a JComponent to use as a dialog parent
163    * @return the JComponent to add to the prefs window
164    */
165   public JComponent createPane(
166     JComponent dialogParent)
167   {
168     JPanel mainPanel = new JPanel(new GridBagLayout());
169     mainPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
170     GridBagConstraints gbc = new GridBagConstraints();
171     gbc.weighty = 1;
172     gbc.fill = GridBagConstraints.NONE;
173     gbc.anchor = GridBagConstraints.WEST;
174     
175     JLabel undoLevelLabel = new JLabel(STR_EditorPrefs_UndoLevelsLabel);
176     gbc.gridwidth = 1;
177     gbc.gridx = 0;
178     gbc.weightx = 0;
179     gbc.insets = new Insets(10, 10, 0, 5);
180     mainPanel.add(undoLevelLabel, gbc);
181     
182     final JSpinner undoLevelSpinner = new JSpinner(
183       new SpinnerNumberModel(undoLevels_, 0, 100, 1));
184     undoLevelSpinner.addChangeListener(
185       new ChangeListener()
186       {
187         public void stateChanged(
188           ChangeEvent ev)
189         {
190           undoLevels_ = ((Number)undoLevelSpinner.getValue()).intValue();
191           broadcaster_.fireEvent(PrefsListener.PREFS_CHANGED_METHOD,
192             new PrefsChangedEvent(EditorPrefs.this));
193         }
194       });
195     gbc.gridwidth = 1;
196     gbc.gridx = 1;
197     gbc.weightx = 1;
198     gbc.insets = new Insets(10, 0, 0, 10);
199     mainPanel.add(undoLevelSpinner, gbc);
200     
201     JCheckBox checkBox = new JCheckBox(STR_EditorPrefs_SmartQuotesCheck,
202       autoSmartQuotes_);
203     checkBox.addItemListener(
204       new ItemListener()
205       {
206         public void itemStateChanged(
207           ItemEvent ev)
208         {
209           if (ev.getStateChange() == ItemEvent.SELECTED)
210           {
211             autoSmartQuotes_ = true;
212           }
213           else if (ev.getStateChange() == ItemEvent.DESELECTED)
214           {
215             autoSmartQuotes_ = false;
216           }
217         }
218       });
219     gbc.gridwidth = 2;
220     gbc.gridx = 0;
221     gbc.weightx = 1;
222     gbc.insets = new Insets(10, 10, 0, 10);
223     mainPanel.add(checkBox, gbc);
224     
225     ScrollablePanel viewport = new ScrollablePanel(new BorderLayout());
226     viewport.add(mainPanel, BorderLayout.NORTH);
227     JScrollPane scroller = new JScrollPane(viewport);
228     return scroller;
229   }
230   
231   
232   /**
233    * This method is called when the pane is brought into focus.
234    */
235   public void paneFocused()
236   {
237   }
238   
239   
240   /**
241    * This method is called when the pane is unfocused.
242    */
243   public void paneUnfocused()
244   {
245   }
246   
247   
248   /**
249    * This method is called when the prefs window is closed. The provider should
250    * generally respond by defuncting the pane.
251    */
252   public void paneDisposed()
253   {
254     prefs_.putInt(UNDOLEVELS_PREF, undoLevels_);
255     prefs_.putBoolean(AUTO_SMARTQUOTES_PREF, autoSmartQuotes_);
256   }
257 }