1   /*
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 or later
5    * as published by the Free Software Foundation. This program is distributed
6    * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
7    * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *      http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * © CrossWire Bible Society, 2007 - 2016
18   *
19   */
20  /**
21  <p>Config allows an application to separate the responsibilities of setting
22  itself up, and providing a user interface from the real work that it needs
23  to do.</p>
24  
25  <h2>Introduction</h2>
26  <p>Config is (mostly) all kept in a few packages in the util source tree. The
27   design aims for the following goals:</p>
28  <ul>
29    <li>Application Transparency - It should be possible to add a configuration
30      dialog to an application without adding hundreds of hooks either to your
31      application to read the current state, or to the configuration system to
32      work with the application. This is achieved via an xml config file and a
33      healthy dose of reflection.</li>
34    <li>View Independance - Currently there are a number of Swing front ends -
35      a Mozilla style config dialog with a tree, a more conventional tabbed dialog,
36      and a prototype wizard style interface. There has also been a servlet front-end
37      however the code to do this has suffered bit-rot, and should not be considered
38      useful. It does however prove the view independance concept.</li>
39  </ul>
40  
41  <h3>How To Use Config</h3>
42  <p>There are a number of simple steps. First a config.xml file is needed to tell
43  the config system what to configure and how.</p>
44  
45  <pre>
46  &lt;config&gt;
47  
48    <span style="color: #336600">&lt;!-- A configuration is a set of options ... --&gt;</span>
49    <span style="color: #336600">&lt;!-- The key is a dot separated name - Imaging this in a Mozilla tree or some nested tabs. --&gt;</span>
50    &lt;option key=&quot;Bibles.Sword.Base Directory&quot; type=&quot;string&quot;&gt;
51      <span style="color: #336600">&lt;!-- The type (above) along with the introspect line configures what JavaBean methods will be called --&gt;</span>
52      &lt;introspect class=&quot;org.crosswire.jsword.book.sword.SwordBibleDriver&quot; property=&quot;SwordDir&quot;/&gt;
53      <span style="color: #336600">&lt;!-- The tool-tip (or similar) describing what is going on --&gt;</span>
54      &lt;help&gt;Where is the SWORD Project base directory.&lt;/help&gt;
55    &lt;/option&gt;
56  
57    <span style="color: #336600">&lt;!-- Another option, this time it is a boolean option which will show up as a tickbox --&gt;</span>
58    &lt;option key=&quot;Bibles.Display.Persistent Naming&quot; level=&quot;advanced&quot; type=&quot;boolean&quot;&gt;
59      &lt;introspect class=&quot;org.crosswire.jsword.passage.PassageUtil&quot; property=&quot;PersistentNaming&quot;/&gt;
60      &lt;help&gt;True if the passage editor re-writes the references to conform to its notation.&lt;/help&gt;
61    &lt;/option&gt;
62  
63    <span style="color: #336600">&lt;!-- Another type again this one for the look and feel. --&gt;
64    &lt;!-- The reason for the helper class here is to alter windows that are not currently mapped --&gt;</span>
65      &lt;option key=&quot;Looks.Look and Feel&quot; type=&quot;class&quot;&gt;
66      &lt;introspect class=&quot;org.crosswire.common.swing.LookAndFeelUtil&quot; property=&quot;LookAndFeel&quot;/&gt;
67      &lt;help&gt;The look and feel of the application&lt;/help&gt;
68    &lt;/option&gt;
69  
70    <span style="color: #336600">&lt;!-- When we have have an Enum style config option ... --&gt;</span>
71      &lt;option key=&quot;Bibles.Display.Book Case&quot; level=&quot;advanced&quot; type=&quot;int-options&quot;&gt;
72      &lt;introspect class=&quot;org.crosswire.jsword.passage.Books&quot; property=&quot;Case&quot;/&gt;
73      &lt;help&gt;What case should we use to display the references.&lt;/help&gt;
74      &lt;alternative number=&quot;0&quot; name=&quot;lower&quot;/&gt;
75      &lt;alternative number=&quot;1&quot; name=&quot;Sentence&quot;/&gt;
76      &lt;alternative number=&quot;2&quot; name=&quot;UPPER&quot;/&gt;
77      &lt;alternative number=&quot;3&quot; name=&quot;mIXeD&quot;/&gt;
78    &lt;/option&gt;
79  
80    <span style="color: #336600">&lt;!-- The options here are more complex and need to be provided as a string array by Java code (see below) --&gt;</span>
81      &lt;option key=&quot;Bibles.Default&quot; type=&quot;string-options&quot;&gt;
82      &lt;introspect class=&quot;org.crosswire.jsword.book.Bibles&quot; property=&quot;DefaultName&quot;/&gt;
83      &lt;help&gt;Which of the available Bibles is the default.&lt;/help&gt;
84      &lt;map name=&quot;biblenames&quot;/&gt;
85    &lt;/option&gt;
86  
87    <span style="color: #336600">&lt;!-- This option is 'advanced' which means it is not visible to all users (see below) --&gt;</span>
88      &lt;option key=&quot;Advanced.Source Path&quot; level=&quot;advanced&quot; type=&quot;path&quot;&gt;
89      &lt;introspect class=&quot;org.crosswire.common.swing.DetailedExceptionPane&quot; property=&quot;SourcePath&quot;/&gt;
90      &lt;help&gt;The directories to search for source code in when investigating an exception.&lt;/help&gt;
91    &lt;/option&gt;
92  
93    <span style="color: #336600">&lt;!-- When the choice is very custom you can always do your own implementation --&gt;
94    &lt;!-- This allows us to set users levels so not everyone gets asked hard questions --&gt;</span>
95      &lt;option key=&quot;Advanced.User Level&quot; type=&quot;custom&quot; class=&quot;org.crosswire.common.util.UserLevel$UserLevelChoice&quot;&gt;
96      &lt;help&gt;How advanced is your knowledge of this program.&lt;/help&gt;
97    &lt;/option&gt;
98  
99    <span style="color: #336600">&lt;!-- There are other examples in config.xml --&gt;</span>
100 &lt;/config&gt;
101 </pre>
102 
103 <p>Then you need to add the Java code:</p>
104 <pre><span style="color: #336600">
105 // To load the config.xml file:
106 </span>Config config = new Config(&quot;Tool Shed Options&quot;);
107 Document xmlconfig = Project.resource().getDocument(&quot;config&quot;); <span style="color: #336600">// Or whatever to get a JDOM Document</span>
108 config.add(xmlconfig);
109 
110 <span style="color: #336600">// To load a saved config</span>
111 config.setProperties(Project.resource().getProperties(&quot;desktop&quot;)); <span style="color: #336600">// Or however you get a Properties</span>
112 config.localToApplication(true);
113 
114 <span style="color: #336600">// And display it ...</span>
115 URL configurl = Project.resource().getPropertiesURL(&quot;desktop&quot;); <span style="color: #336600">// URL of the Properties file to save to</span>
116 SwingConfig.showDialog(config, parentWind, configurl);
117 
118 <span style="color: #336600">// The code above needed help in setting up a string choice. This is how ...</span>
119 ChoiceFactory.getDataMap().put(&quot;biblenames&quot;, Bibles.getBibleNames());
120 </pre>
121 <p>There are more examples in <code>org.crosswire.bibledesktop.desktop.OptionsAction.</code></p>
122 
123 */
124 package org.crosswire.common.config;
125