View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui;
21  
22  import java.awt.Component;
23  
24  import javax.swing.JOptionPane;
25  import javax.swing.JPanel;
26  
27  import org.xnap.XNap;
28  import org.xnap.gui.action.*;
29  import org.xnap.gui.event.DialogListener;
30  import org.xnap.gui.event.DialogSupport;
31  import org.xnap.util.Preferences;
32  
33  /***
34   * Provides the plugin preferences dialog.
35   */
36  public class PluginPreferencesDialog extends AbstractPreferencesDialog {
37      
38      //--- Data field(s) ---
39  
40      /***
41       * Used to store dialog listeners.
42       */
43      private static DialogSupport listeners = new DialogSupport();
44      private static PluginPreferencesDialog me = null;
45  
46      private Preferences prefs = Preferences.getInstance();
47  
48      //--- Constructor(s) ---
49  
50      private PluginPreferencesDialog() 
51      {
52  		super(XNap.tr("XNap - Plugin Settings"));
53  	
54  		listeners.fireDialogWillBecomeVisible(this);
55  
56  		setSize(prefs.getPrefsWindowWidth(), prefs.getPrefsWindowHeight());
57      }
58  
59      // --- Methods ---
60  
61      public static void addDialogListener(DialogListener listener)
62      {
63  		listeners.addDialogListener(listener);
64  		ActionHelper.pluginPreferencesDialogAction.setEnabled(true);
65      }
66  
67      public void close()
68      {
69  		// save dialog prefs
70  		prefs.setPrefsWindowHeight(getBounds().getSize().height);
71  		prefs.setPrefsWindowWidth(getBounds().getSize().width);
72  
73  		// save the preferences to file
74  		if (isOkay() && !prefs.write()) {
75  			String msg = XNap.tr("Could not write {0}.", prefs.getFilename());
76  			JOptionPane.showMessageDialog
77  				(this, msg, XNap.tr("Preferences"), 
78  				 JOptionPane.ERROR_MESSAGE);
79  			return;
80  		}
81  
82  		listeners.fireDialogWillBecomeInvisible(this);
83  
84  		dispose();
85  		me = null;		    
86      }
87  
88      public static void removeDialogListener(DialogListener listener)
89      {
90  		listeners.removeDialogListener(listener);
91  		if (listeners.size() == 0) {
92  			ActionHelper.pluginPreferencesDialogAction.setEnabled(false);
93  		}
94      }
95  
96      /***
97       * Removes <code>jp</code> from the list.
98       */
99      public static void removePanel(JPanel jp)
100     {
101 		if (me != null) {
102 			me.remove(jp);
103 		}
104     }
105 
106     /***
107      * Removes <code>tab</code> from the list of {@link SettingsPanel} 
108      * objects. After this method has been invoked, <code>tab.apply()</code>
109      * is not called when the dialog is closed.
110      */
111     public static void removePanel(SettingsPanel tab)
112     {
113 		if (me != null) {
114 			me.remove(tab);
115 		}
116     }
117 
118     public static void showDialog(Component c)
119     {
120         if (me == null) {
121             me = new PluginPreferencesDialog();
122         }
123 		me.show(c);
124     }
125 
126 }