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.pkg;
21  
22  import java.awt.GridBagLayout;
23  import java.awt.event.ActionEvent;
24  import java.awt.event.ActionListener;
25  import java.io.File;
26  import java.io.IOException;
27  
28  import javax.swing.ButtonGroup;
29  import javax.swing.JCheckBox;
30  import javax.swing.JPanel;
31  import javax.swing.JRadioButton;
32  import javax.swing.JScrollPane;
33  import javax.swing.JTextArea;
34  
35  import org.xnap.XNap;
36  import org.xnap.gui.util.GUIHelper;
37  import org.xnap.gui.util.GridBagHelper;
38  import org.xnap.pkg.XNapPackageManager;
39  import org.xnap.util.FileHelper;
40  import org.xnap.util.Preferences;
41  
42  public class EditSourcesPanel extends JPanel {
43  
44      //--- Constant(s) ---
45  
46      //--- Data field(s) ---
47  
48  	private JRadioButton automaticRadioButton;
49  	private JCheckBox includeUnstableCheckBox;
50  	private JRadioButton manualRadioButton;
51      private JTextArea sourcesTextArea;
52  
53      //--- Constructor(s) ---
54  
55      public EditSourcesPanel() 
56      {
57  		setLayout(new GridBagLayout());
58  		setBorder(GUIHelper.createDefaultBorder(XNap.tr("Manage Sources")));
59  
60  		ButtonGroup bg = new ButtonGroup();
61  		SelectionListener listener = new SelectionListener();
62  
63  		automaticRadioButton = new JRadioButton(XNap.tr("Automatic"));
64  		automaticRadioButton.addActionListener(listener);
65  		bg.add(automaticRadioButton);
66  		GridBagHelper.addComponent(this, automaticRadioButton);
67  
68  		includeUnstableCheckBox = new JCheckBox
69  			(XNap.tr("Include unstable packages"));
70  		GridBagHelper.add(this, includeUnstableCheckBox);
71  
72  		manualRadioButton = new JRadioButton(XNap.tr("Manual"));
73  		manualRadioButton.addActionListener(listener);
74  		bg.add(manualRadioButton);
75  		GridBagHelper.add(this, manualRadioButton);
76  		
77  		sourcesTextArea = new JTextArea("", 5, 40);
78  		GridBagHelper.add
79  			(this, new JScrollPane(sourcesTextArea));
80  
81  		restore();
82      }
83  
84      //--- Method(s) ---
85  
86  	public void apply() throws IOException
87  	{
88  		Preferences.getInstance().set
89  			("autoSourcesListManagement",
90  			 automaticRadioButton.isSelected());
91  		Preferences.getInstance().set
92  			("includeUnstablePackages",
93  			 includeUnstableCheckBox.isSelected());
94  
95  		String sources;
96  		if (automaticRadioButton.isSelected()) {
97  			sources = XNapPackageManager.getInstance().getDefaultSources
98  				(includeUnstableCheckBox.isSelected());
99  		}
100 		else {
101 			sources = sourcesTextArea.getText();
102 		}
103 
104 		FileHelper.writeText(new File(XNapPackageManager.SOURCES_FILENAME),
105 							 sources);
106 	}
107 
108 	public void restore()
109 	{
110 		if (Preferences.getInstance().getBoolean
111 			("autoSourcesListManagement")) {
112 			automaticRadioButton.setSelected(true);
113 		}
114 		else {
115 			manualRadioButton.setSelected(true);
116 		}
117 
118 		try {
119 			String sources = FileHelper.readText
120 				(new File(XNapPackageManager.SOURCES_FILENAME));
121 			sourcesTextArea.setText(sources);
122 		}
123 		catch (IOException e) {
124 
125 		}
126 		updateComponents();
127 	}
128 
129 	private void updateComponents()
130 	{
131 		includeUnstableCheckBox.setEnabled(automaticRadioButton.isSelected());
132 		sourcesTextArea.setEnabled(manualRadioButton.isSelected());
133 	}
134 
135     // --- Inner Class(es) ---
136 
137     private class SelectionListener implements ActionListener {
138 
139 		public void actionPerformed(ActionEvent event) 
140 		{
141 			updateComponents();
142         }
143 
144     }
145 
146 }