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

Quick Search    Search Deep

Source code: normal/options/NormalOptionSet.java


1   
2   /**************************************************************************
3    *                                                                        *
4    *  Regina - A normal surface theory calculator                           *
5    *  Java user interface                                                   *
6    *                                                                        *
7    *  Copyright (c) 1999-2000, Ben Burton                                   *
8    *  For further details contact Ben Burton (benb@acm.org).                *
9    *                                                                        *
10   *  This program is free software; you can redistribute it and/or         *
11   *  modify it under the terms of the GNU General Public License as        *
12   *  published by the Free Software Foundation; either version 2 of the    *
13   *  License, or (at your option) any later version.                       *
14   *                                                                        *
15   *  This program is distributed in the hope that it will be useful, but   *
16   *  WITHOUT ANY WARRANTY; without even the implied warranty of            *
17   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
18   *  General Public License for more details.                              *
19   *                                                                        *
20   *  You should have received a copy of the GNU General Public             *
21   *  License along with this program; if not, write to the Free            *
22   *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,        *
23   *  MA 02111-1307, USA.                                                   *
24   *                                                                        *
25   **************************************************************************/
26  
27  /* end stub */
28  
29  package normal.options;
30  
31  import java.io.*;
32  import btools.utilities.OptionSet;
33  
34  /**
35   * Provides an option set with a cache of frequently used options for
36   * faster access.  These are accessed through direct variable lookup
37   * as opposed to the usual dictionary search with string comparisons.
38   * <p>
39   * These frequently used options should <i>only</i> be accessed through
40   * the appropriate <tt>get</tt><i>OptionName</i><tt>()</tt>
41   * and <tt>set</tt><i>OptionName</i><tt>()</tt> methods, and never through
42   * the standard methods <tt>getStringOption()</tt>,
43   * <tt>setBooleanOption()</tt>, etc.
44   * <p>
45   * Options that are not explicitly cached by <tt>NormalOptionSet</tt> should,
46   * however, still be accessed through the standard <tt>OptionSet</tt> access
47   * methods.
48   * <p>
49   * Cached options will always provide values.  If they do not appear in the
50   * option set, they will be given default values upon construction of the
51   * option set.
52   */
53  public class NormalOptionSet extends OptionSet {
54      /**
55       * The comment to place at the beginning of the option file.
56       */
57      public static final String normalComment = "User options for " +
58          normal.Application.program;
59      
60      /**
61       * Full option name for a particular cached option.
62       */
63      public static final String optionAutoDock = "AutoDock";
64      
65      /**
66       * Full option name for a particular cached option.
67       */
68      public static final String optionDisplayIcon = "DisplayIcon";
69  
70      /**
71       * The default for a particular cached option.
72       */
73      public static final boolean defaultAutoDock = true;
74      
75      /**
76       * The default for a particular cached option.
77       */
78      public static final boolean defaultDisplayIcon = true;
79      
80      /**
81       * A particular cached option.
82       */
83      private boolean autoDock;
84      
85      /**
86       * A particular cached option.
87       */
88      private boolean displayIcon;
89      
90      /**
91       * Creates a new option set based on the given file.
92       * All options stored in the file will be loaded.  Any errors
93       * that occur when reading from file will be ignored.
94       *
95       * @param optionFile the file to/from which this option set is to
96       * be written/read.
97       * @see #NormalOptionSet(File, boolean)
98       */
99      public NormalOptionSet(File optionFile) {
100         super(optionFile, normalComment);
101     }
102 
103     /**
104      * Creates a new option set based on the given file.
105      * All options stored in the file will be loaded.
106      *
107      * @param optionFile the file to/from which this option set is to
108      * be written/read.
109      * @param forceLoad if set to <tt>false</tt>, we will ignore
110      * any errors in reading from file.  If set to <tt>true</tt>, an
111      * exception will be thrown if an error occurs.
112      * @throws IOException thrown if an error occurs in reading from file.
113      * @see #NormalOptionSet(File)
114      */
115     public NormalOptionSet(File optionFile, boolean forceLoad)
116             throws IOException {
117         super(optionFile, normalComment, forceLoad);
118     }
119     
120     /**
121      * Attempt to read this option set from file.
122      *
123      * @throws IOException thrown if an error occurs in reading from file.
124      */
125     protected void readFromFile() throws IOException {
126         IOException caught = null;
127         try {
128             super.readFromFile();
129         } catch (IOException e) {
130             caught = e;
131         }
132         autoDock = getBooleanOption(optionAutoDock, defaultAutoDock);
133         displayIcon = getBooleanOption(optionDisplayIcon, defaultDisplayIcon);
134         if (caught != null)
135             throw caught;
136     }
137 
138     /**
139      * Attempt to write this option set to file.
140      *
141      * @param forceWrite if set to <tt>false</tt>, we will ignore
142      * any errors in writing to file.  If set to <tt>true</tt>, an
143      * exception will be thrown if an error occurs.
144      * @throws IOException thrown if an error occurs in writing to file.
145      */
146     public void writeToFile(boolean forceWrite) throws IOException {
147         setBooleanOption(optionAutoDock, autoDock);
148         setBooleanOption(optionDisplayIcon, displayIcon);
149         super.writeToFile(forceWrite);
150     }
151 
152     /**
153      * Get a particular cached option.
154      *
155      * @return the current value of the option.
156      */
157     public boolean getAutoDock() {
158         return autoDock;
159     }
160     
161     /**
162      * Set a particular cached option.
163      *
164      * @param value the new value for the option.
165      */
166     public void setAutoDock(boolean value) {
167         autoDock = value;
168     }
169         
170     /**
171      * Get a particular cached option.
172      *
173      * @return the current value of the option.
174      */
175     public boolean getDisplayIcon() {
176         return displayIcon;
177     }
178     
179     /**
180      * Set a particular cached option.
181      *
182      * @param value the new value for the option.
183      */
184     public void setDisplayIcon(boolean value) {
185         displayIcon = value;
186     }
187 }