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: FontChooser.java 1605 2007-08-03 21:34:46Z dmsmith $
21   */
22  package org.crosswire.bibledesktop.book.install;
23  
24  import java.awt.BorderLayout;
25  import java.awt.Component;
26  import java.awt.Container;
27  import java.awt.GridLayout;
28  import java.awt.event.ItemEvent;
29  import java.awt.event.ItemListener;
30  import java.io.IOException;
31  import java.io.ObjectInputStream;
32  
33  import javax.swing.JButton;
34  import javax.swing.JCheckBox;
35  import javax.swing.JDialog;
36  import javax.swing.JFrame;
37  import javax.swing.JLabel;
38  import javax.swing.JPanel;
39  import javax.swing.SwingUtilities;
40  
41  import org.crosswire.bibledesktop.BDMsg;
42  import org.crosswire.common.swing.ActionFactory;
43  import org.crosswire.common.swing.GuiUtil;
44  import org.crosswire.jsword.util.WebWarning;
45  
46  /**
47   * InternetWarning is used to request permission of the user to access the
48   * Internet. An option allows them to request that they are not asked again. The
49   * default for the option is to be asked every time.
50   * 
51   * @see gnu.lgpl.License for license details.<br>
52   *      The copyright to this program is held by it's authors.
53   * @author DM Smith [dmsmith555 at yahoo dot com]
54   */
55  public class InternetWarning extends JPanel {
56      /**
57       * Create a WebWarningDialog.
58       */
59      public InternetWarning() {
60          actions = new ActionFactory(this);
61  
62          ItemListener changer = new ItemListener() {
63              public void itemStateChanged(ItemEvent ev) {
64                  fireStateChange();
65              }
66          };
67  
68          setLayout(new GridLayout(2, 1, 5, 5));
69  
70          add(new JLabel(WebWarning.instance().getWarning()));
71  
72          showWarning = new JCheckBox(WebWarning.instance().getShownWarningLabel());
73          showWarning.setSelected(true);
74          showWarning.addItemListener(changer);
75  
76          add(showWarning);
77          GuiUtil.applyDefaultOrientation(this);
78      }
79  
80      /**
81       * Display a InternetWarning as a dialog
82       */
83      public static int showDialog(Component parent, String title) {
84          final InternetWarning webWarning = new InternetWarning();
85  
86          JPanel buttons = new JPanel();
87          // TRANSLATOR: This is the text on a "Yes" button.
88          JButton yesButton = new JButton(webWarning.actions.addAction("Yes", BDMsg.gettext("Yes")));
89          // TRANSLATOR: This is the text on a "No" button.
90          JButton noButton = new JButton(webWarning.actions.addAction("No", BDMsg.gettext("No")));
91          buttons.add(yesButton);
92          buttons.add(noButton);
93  
94          Component root = SwingUtilities.getRoot(parent);
95  
96          JDialog dialog = (root instanceof JFrame) ? new JDialog((JFrame) root, title, true) : new JDialog((JDialog) root, title, true);
97  
98          webWarning.dialog = dialog;
99          webWarning.choice = InternetWarning.DENIED;
100 
101         dialog.getRootPane().setDefaultButton(yesButton);
102 
103         Container content = dialog.getContentPane();
104         content.setLayout(new BorderLayout());
105         content.add(webWarning, BorderLayout.NORTH);
106         content.add(buttons, BorderLayout.SOUTH);
107         dialog.pack();
108         GuiUtil.centerOnScreen(dialog);
109         GuiUtil.applyDefaultOrientation(dialog);
110         dialog.setVisible(true);
111 
112         dialog.dispose();
113 
114         return webWarning.choice;
115     }
116 
117     public void doYes() {
118         dialog.setVisible(false);
119         choice = InternetWarning.GRANTED;
120     }
121 
122     public void doNo() {
123         dialog.setVisible(false);
124         choice = InternetWarning.DENIED;
125     }
126 
127     /**
128      * When something changes we must inform out listeners.
129      */
130     protected void fireStateChange() {
131         WebWarning.instance().setShown(showWarning.isSelected());
132     }
133 
134     /**
135      * Serialization support.
136      * 
137      * @param is
138      * @throws IOException
139      * @throws ClassNotFoundException
140      */
141     private void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException {
142         actions = new ActionFactory(this);
143         is.defaultReadObject();
144     }
145 
146     /**
147      * Access to the Internet is granted.
148      */
149     public static final int GRANTED = 0;
150 
151     /**
152      * Access to the Internet is denied.
153      */
154     public static final int DENIED = 1;
155 
156     /*
157      * The ActionFactory holding the actions used by this
158      * EditSite.
159      */
160     private transient ActionFactory actions;
161 
162     /**
163      * The user's choice.
164      */
165     protected int choice;
166 
167     /**
168      * The dialog box
169      */
170     protected JDialog dialog;
171 
172     /**
173      * Bold font?
174      */
175     protected JCheckBox showWarning;
176 
177     /**
178      * Serialization ID
179      */
180     private static final long serialVersionUID = 3978992071925250097L;
181 }
182