1   /**
2    * Distribution License:
3    * BibleDesktop is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU General Public License, version 2 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 General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/gpl.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: SwordSiteEditor.java 2091 2011-03-07 04:15:31Z dmsmith $
21   */
22  package org.crosswire.bibledesktop.book.install;
23  
24  import java.awt.GridBagConstraints;
25  import java.awt.GridBagLayout;
26  import java.awt.Insets;
27  import java.io.IOException;
28  import java.io.ObjectInputStream;
29  
30  import javax.swing.JLabel;
31  import javax.swing.JPanel;
32  import javax.swing.JTextField;
33  
34  import org.crosswire.bibledesktop.BDMsg;
35  import org.crosswire.jsword.book.install.Installer;
36  import org.crosswire.jsword.book.install.sword.AbstractSwordInstaller;
37  
38  /**
39   * A representation of a Sword SiteEditor.
40   * 
41   * @see gnu.gpl.License for license details.<br>
42   *      The copyright to this program is held by it's authors.
43   * @author DM Smith [dmsmith555 at yahoo dot com]
44   */
45  public class SwordSiteEditor extends JPanel implements SiteEditor {
46      public void initialize() {
47          host = new JTextField();
48          // TRANSLATOR: Label for the host field.
49          // This is something like www.crosswire.org.
50          JLabel hostLabel = getLabelForText(BDMsg.gettext("Host:"), host);
51  
52          catalogDir = new JTextField();
53          // TRANSLATOR: Label for the catalog directory field.
54          // This is the folder that contains mods.d.tar.gz.
55          JLabel catalogDirLabel = getLabelForText(BDMsg.gettext("Catalog Directory:"), catalogDir);
56  
57          packageDir = new JTextField();
58          // TRANSLATOR: Label for the zip directory field.
59          // SWORD modules are cached as zip files in this directory.
60          JLabel packageDirLabel = getLabelForText(BDMsg.gettext("Zip Directory:"), packageDir);
61  
62          proxyHost = new JTextField();
63          // TRANSLATOR: Label for the proxy host field.
64          // Sometimes users have their internet access proxied. This field allows the user to enter the proxy host.
65          JLabel proxyHostLabel = getLabelForText(BDMsg.gettext("Proxy Host:"), proxyHost);
66  
67          proxyPort = new JTextField();
68          // TRANSLATOR: Label for the proxy port field.
69          // Sometimes users have their internet access proxied. This field allows the user to enter the proxy port, if not 80.
70          JLabel proxyPortLabel = getLabelForText(BDMsg.gettext("Proxy Port:"), proxyPort);
71  
72          setLayout(new GridBagLayout());
73          add(hostLabel,       new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(2, 10, 2, 2), 0, 0));
74          add(host,            new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 10), 0, 0));
75          add(catalogDirLabel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(2, 10, 2, 2), 0, 0));
76          add(catalogDir,      new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 10), 0, 0));
77          add(packageDirLabel, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(2, 10, 2, 2), 0, 0));
78          add(packageDir,      new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 10), 0, 0));
79          add(proxyHostLabel,  new GridBagConstraints(0, 3, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(2, 10, 2, 2), 0, 0));
80          add(proxyHost,       new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 10), 0, 0));
81          add(proxyPortLabel,  new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.LINE_END, GridBagConstraints.NONE, new Insets(2, 10, 2, 2), 0, 0));
82          add(proxyPort,       new GridBagConstraints(1, 4, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2, 10), 0, 0));
83  
84          reset();
85      }
86  
87      /* (non-Javadoc)
88       * @see org.crosswire.bibledesktop.book.install.SiteEditor#save()
89       */
90      public void save() {
91          if (installer == null) {
92              return;
93          }
94  
95          installer.setHost(host.getText());
96          installer.setCatalogDirectory(catalogDir.getText());
97          installer.setPackageDirectory(packageDir.getText());
98          installer.setProxyHost(proxyHost.getText());
99          Integer pport = null;
100         try {
101             pport = Integer.valueOf(proxyPort.getText());
102         } catch (NumberFormatException e) {
103             pport = null; // or -1
104         }
105         installer.setProxyPort(pport);
106     }
107 
108     /* (non-Javadoc)
109      * @see org.crosswire.bibledesktop.book.install.SiteEditor#reset()
110      */
111     public void reset() {
112         if (installer == null) {
113             return;
114         }
115 
116         host.setText(installer.getHost());
117         catalogDir.setText(installer.getCatalogDirectory());
118         packageDir.setText(installer.getPackageDirectory());
119         proxyHost.setText(installer.getProxyHost());
120         Integer port = installer.getProxyPort();
121         proxyPort.setText(port == null ? null : port.toString());
122     }
123 
124     /* (non-Javadoc)
125      * @see org.crosswire.bibledesktop.book.install.SiteEditor#setEditable(boolean)
126      */
127     public void setEditable(boolean editable) {
128         if (host != null) {
129             host.setEditable(editable);
130         }
131 
132         if (catalogDir != null) {
133             catalogDir.setEditable(editable);
134         }
135 
136         if (packageDir != null) {
137             packageDir.setEditable(editable);
138         }
139 
140         if (proxyHost != null) {
141             proxyHost.setEditable(editable);
142         }
143 
144         if (proxyPort != null) {
145             proxyPort.setEditable(editable);
146         }
147     }
148 
149     /* (non-Javadoc)
150      * @see org.crosswire.bibledesktop.book.install.SiteEditor#getInstaller()
151      */
152     public Installer getInstaller() {
153         return installer;
154     }
155 
156     /* (non-Javadoc)
157      * @see org.crosswire.bibledesktop.book.install.SiteEditor#setInstaller()
158      */
159     public void setInstaller(Installer newInstaller) {
160         assert newInstaller == null || newInstaller instanceof AbstractSwordInstaller;
161         Installer old = installer;
162         installer = (AbstractSwordInstaller) newInstaller;
163         if (newInstaller == null) {
164             removeAll();
165         } else if (!newInstaller.equals(old)) {
166             removeAll();
167             initialize();
168         }
169     }
170 
171     private JLabel getLabelForText(String title, JTextField field) {
172         JLabel label = new JLabel();
173         label.setText(title);
174         label.setLabelFor(field);
175         return label;
176     }
177 
178     /**
179      * Serialization support.
180      * 
181      * @param is
182      * @throws IOException
183      * @throws ClassNotFoundException
184      */
185     private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException {
186         installer = null;
187         is.defaultReadObject();
188     }
189 
190     private transient AbstractSwordInstaller installer;
191     private JTextField host;
192     private JTextField catalogDir;
193     private JTextField packageDir;
194     private JTextField proxyHost;
195     private JTextField proxyPort;
196 
197     /**
198      * Serialization ID
199      */
200     private static final long serialVersionUID = 3834589894202175795L;
201 
202 }
203