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.BorderLayout;
23  import java.awt.Font;
24  import java.util.Iterator;
25  import java.util.LinkedList;
26  
27  import javax.swing.Icon;
28  import javax.swing.JComponent;
29  import javax.swing.JLabel;
30  import javax.swing.JPanel;
31  import javax.swing.JScrollPane;
32  import javax.swing.JTabbedPane;
33  import javax.swing.border.EmptyBorder;
34  
35  import org.xnap.gui.component.DefaultDialog;
36  import org.xnap.gui.component.IconSplitPane;
37  import org.xnap.gui.util.GUIHelper;
38  import org.xnap.gui.util.IconHelper;
39  
40  /***
41   * Provides an abstract preferences dialog.
42   */
43  public abstract class AbstractPreferencesDialog extends DefaultDialog {
44      
45      //--- Data field(s) ---
46  
47      private IconSplitPane pane;
48      private LinkedList panels = new LinkedList();
49  
50      private boolean canClose;
51      private boolean needToRestartXNap = false;
52  
53      //--- Constructor(s) ---
54  
55      public AbstractPreferencesDialog(String title) 
56      {
57  		super(BUTTON_HELP | BUTTON_CONTEXT_HELP| BUTTON_OKAY | BUTTON_APPLY 
58  			  | BUTTON_CANCEL);
59  	
60  		// icon panel
61  		pane = new IconSplitPane();
62  		pane.setBorder(GUIHelper.createEmptyBorder());
63  		pane.setDividerSize(2);
64  		pane.setOneTouchExpandable(false);
65  
66  		// content
67          setTitle(title);
68  		getMainPanel().setLayout(new BorderLayout());
69  		getMainPanel().add(pane, BorderLayout.CENTER);
70      }
71  
72      // --- Methods ---
73  
74      /***
75       * Adds <code>tab</code> to the list. Sets an
76       * appropriate border for <code>tab</code> and wraps it into a
77       * {@link JScrollPane}.
78       */
79      public JPanel addPanel(SettingsPanel tab)
80      {
81  		return addPanel(tab, tab.getTitle(), tab.getIcon(), 
82  						tab.getDescription());
83      }
84  
85      /***
86       * Adds <code>tab</code> to the list. Sets an
87       * appropriate border for <code>tab</code> and wraps it into a
88       * {@link JScrollPane}.
89       */
90      public JPanel addPanel(SettingsPanel tab, String icon)
91      {
92  		return addPanel(tab, tab.getTitle(), IconHelper.getTabTitleIcon(icon), 
93  						tab.getDescription());
94      }
95  
96      /***
97       * Adds <code>tab</code> to the list. Sets an
98       * appropriate border for <code>tab</code> and wraps it into a
99       * {@link JScrollPane}.
100      */
101     public JPanel addPanel(SettingsPanel tab, String title, Icon icon, 
102 						   String info)
103     {
104 		panels.add(tab.getPanel());
105 		tab.getPanel().setBorder(new EmptyBorder(10, 10, 10, 10));
106 		JScrollPane jsp = new JScrollPane(tab.getPanel());
107 		jsp.setBorder(GUIHelper.createEmptyBorder());
108 
109 		return addPanel(jsp, title, icon, info);
110     }
111 
112     /***
113      * Wraps <code>jc</code> in an info panel and adds it to the list.
114      */
115     public JPanel addPanel(JComponent jc, String title, String icon, 
116 						   String info)
117     {
118 		return addPanel(jc, title, IconHelper.getTabTitleIcon(icon), info);
119     }
120 
121     /***
122      * Wraps <code>jc</code> in an info panel and adds it to the list.
123      */
124     public JPanel addPanel(JComponent jc, String title, Icon icon, String info)
125     {
126 		JPanel jp = new JPanel(new BorderLayout());
127 		pane.addTab(title, icon, jp);
128 
129 		JLabel jlInfo = new JLabel(info);
130 		jlInfo.setFont(new Font("Dialog", Font.BOLD, 12));
131 		jlInfo.setBorder(new EmptyBorder(5, 5, 5, 5));
132 		jp.add(jlInfo, BorderLayout.NORTH);
133 
134 		jp.add(jc, BorderLayout.CENTER);
135 
136 		return jp;
137     }
138 
139     /***
140      * Adds <code>tab</code> to <code>parent</code>. Sets an
141      * appropriate border for <code>tab</code> and wraps it into a
142      * {@link JScrollPane}.
143      */
144     public JScrollPane addTab(JTabbedPane parent, SettingsPanel tab)
145     {
146 		panels.add(tab.getPanel());
147 		tab.getPanel().setBorder(new EmptyBorder(10, 10, 10, 10));
148 		JScrollPane jsp = new JScrollPane(tab.getPanel());
149 		jsp.setBorder(GUIHelper.createEmptyBorder());
150 		parent.addTab(tab.getTitle(), jsp);
151 
152 		return jsp;
153     }
154 
155 
156     public boolean apply()
157     {
158 		// panels
159 		boolean canClose = true;
160 		for (Iterator i = panels.iterator(); i.hasNext();) {
161 			try {
162 				((SettingsPanel)i.next()).apply();
163 			}
164 			catch (Exception e) {
165 				canClose = false;
166 				Dialogs.error(this, e.getLocalizedMessage());
167 			}
168 		}
169 
170 		return canClose;
171     }
172 
173     /***
174      * Removes <code>jp</code> from the list.
175      */
176     public void remove(JPanel jp)
177     {
178 		pane.remove(jp);
179     }
180 
181     /***
182      * Removes <code>tab</code> from the list of {@link SettingsPanel} 
183      * objects. After this method has been invoked, <code>tab.apply()</code>
184      * is not called when the dialog is closed.
185      */
186     public void remove(SettingsPanel tab)
187     {
188 		panels.remove(tab);
189     }
190 
191 }