| ConfigEditorFactory.java |
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 as published by
5 * the Free Software Foundation. This program is distributed in the hope
6 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7 * 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 * Copyright: 2005
18 * The copyright to this program is held by it's authors.
19 *
20 * ID: $Id: ConfigEditorFactory.java 2091 2011-03-07 04:15:31Z dmsmith $
21 */
22 package org.crosswire.common.config.swing;
23
24 import java.awt.Component;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27 import java.io.IOException;
28 import java.net.URI;
29
30 import org.crosswire.common.config.Config;
31 import org.crosswire.common.swing.CWMsg;
32 import org.crosswire.common.util.LucidRuntimeException;
33 import org.crosswire.common.util.PluginUtil;
34 import org.crosswire.common.util.Reporter;
35
36 /**
37 * Allow a swing program to display a Dialog box displaying a set of config
38 * options.
39 *
40 * @see gnu.lgpl.License for license details.<br>
41 * The copyright to this program is held by it's authors.
42 * @author Joe Walker [joe at eireneh dot com]
43 */
44 public final class ConfigEditorFactory {
45 /**
46 * Prevent instantiation
47 */
48 private ConfigEditorFactory() {
49 }
50
51 /**
52 * Create a dialog to house a TreeConfig component using the default set of
53 * Fields
54 *
55 * @param config
56 * The set of Choices to display
57 * @param parent
58 * A component to use to find a frame to use as a dialog parent
59 * @param al
60 * The action when the user clicks on ok or apply
61 */
62 public static void showDialog(Config config, Component parent, ActionListener al) {
63 Exception ex = null;
64 try {
65 ConfigEditor base = PluginUtil.getImplementation(ConfigEditor.class);
66 base.construct(config);
67 base.showDialog(parent, al);
68 } catch (ClassCastException e) {
69 ex = e;
70 } catch (IOException e) {
71 ex = e;
72 } catch (ClassNotFoundException e) {
73 ex = e;
74 } catch (InstantiationException e) {
75 ex = e;
76 } catch (IllegalAccessException e) {
77 ex = e;
78 }
79
80 if (ex != null) {
81 Reporter.informUser(parent, ex);
82 }
83 }
84
85 /**
86 * Create a dialog to house a TreeConfig component using the default set of
87 * Fields, with the default accept action of config.localToApplication and
88 * config,localToPermanent
89 *
90 * @param config
91 * The set of Choices to display
92 * @param parent
93 * A component to use to find a frame to use as a dialog parent
94 */
95 public static void showDialog(Config config, Component parent, URI uri) {
96 showDialog(config, parent, new URIActionListener(config, uri));
97 }
98
99 /**
100 * A quick class to save a config to a uri
101 */
102 static class URIActionListener implements ActionListener {
103 /**
104 * To save to a URI
105 */
106 public URIActionListener(Config config, URI uri) {
107 this.config = config;
108 this.uri = uri;
109 }
110
111 /**
112 * The save action
113 */
114 public void actionPerformed(ActionEvent ev) {
115 try {
116 config.localToApplication();
117 config.localToPermanent(uri);
118 } catch (IOException ex) {
119 // TRANSLATOR: Common error condition: Preferences could not be saved. {0} is a placeholder for the preference file.
120 throw new LucidRuntimeException(CWMsg.gettext("Could not save preferences: {0}", uri), ex);
121 }
122 }
123
124 /**
125 * The config to save if needed
126 */
127 private Config config;
128
129 /**
130 * The URI to save to if needed
131 */
132 private URI uri;
133 }
134 }
135