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.plugin.opennap.gui;
21  
22  import java.awt.Component;
23  import java.awt.GridBagLayout;
24  import java.util.Enumeration;
25  
26  import javax.swing.AbstractButton;
27  import javax.swing.ButtonGroup;
28  import javax.swing.JCheckBox;
29  import javax.swing.JPanel;
30  import javax.swing.JRadioButton;
31  import javax.swing.JTextField;
32  
33  import org.xnap.XNap;
34  import org.xnap.gui.component.DefaultDialog;
35  import org.xnap.gui.util.GUIHelper;
36  import org.xnap.gui.util.GridBagHelper;
37  import org.xnap.plugin.opennap.user.OpenNapGlobalUser;
38  
39  public class GlobalOpenNapUserEditorDialog extends DefaultDialog {
40      
41      //--- Data field(s) ---
42  
43      private OpenNapGlobalUser user;
44  
45      private JTextField jtName;
46      private JTextField jtComment;
47      private JTextField jtCategory;
48  
49      private JCheckBox jcbIgnoreChat;
50  
51      private JRadioButton jrbUploadDefault;
52      private JRadioButton jrbUploadNever;
53      private JRadioButton jrbUploadAlways;
54      private ButtonGroup bgUpload;
55  
56      private JRadioButton jrbDownloadDefault;
57      private JRadioButton jrbDownloadNever;
58      private ButtonGroup bgDownload;
59  
60      //--- Constructor(s) ---
61      
62      public GlobalOpenNapUserEditorDialog(OpenNapGlobalUser user)
63      {
64  		initialize();
65  		setUser(user);
66  
67  		pack();
68      }
69  
70      // --- Methods ---
71  
72      public static void showDialog(Component c, OpenNapGlobalUser user)
73      {
74  		GlobalOpenNapUserEditorDialog me
75  			= new GlobalOpenNapUserEditorDialog(user);
76  		me.show(c);
77  		me.pack();
78      }
79  
80      private void initialize()
81      {
82  		getMainPanel().setLayout(new GridBagLayout());
83  
84  		GridBagHelper.addLabel(getMainPanel(), XNap.tr("Name"));
85  		jtName = new JTextField("", 20);
86  		jtName.setEditable(false);
87  		GridBagHelper.add(getMainPanel(), jtName);
88  
89  		GridBagHelper.addLabel(getMainPanel(), XNap.tr("Comment"));
90  		jtComment = new JTextField("", 20);
91  		GridBagHelper.add(getMainPanel(), jtComment);
92  
93  		GridBagHelper.addLabel(getMainPanel(), XNap.tr("Category"));
94  		jtCategory = new JTextField("", 20);
95  		GridBagHelper.add(getMainPanel(), jtCategory);
96  
97  		GridBagHelper.addLabel(getMainPanel(), "");
98  		jcbIgnoreChat = new JCheckBox(XNap.tr("Ignore Chat"));
99  		GridBagHelper.add(getMainPanel(), jcbIgnoreChat);
100 
101 		// uploads
102 		JPanel jpUpload = new JPanel(new GridBagLayout());
103 		jpUpload.setBorder
104 			(GUIHelper.createDefaultBorder(XNap.tr("Allow Uploads To User")));
105 		GridBagHelper.add(getMainPanel(), jpUpload);
106 	
107 		jrbUploadDefault = new JRadioButton(XNap.tr("Default"));
108 		jrbUploadDefault.setActionCommand(OpenNapGlobalUser.TRANSFER_DEFAULT + "");
109 		GridBagHelper.add(jpUpload, jrbUploadDefault);
110 
111 		jrbUploadNever = new JRadioButton(XNap.tr("Never"));
112 		jrbUploadNever.setActionCommand(OpenNapGlobalUser.TRANSFER_NEVER + "");
113 		GridBagHelper.add(jpUpload, jrbUploadNever);
114 
115 		jrbUploadAlways = new JRadioButton(XNap.tr("Always"));
116 		jrbUploadAlways.setActionCommand
117 			(OpenNapGlobalUser.TRANSFER_UNLIMITED + "");
118 		GridBagHelper.add(jpUpload, jrbUploadAlways);
119 
120 		bgUpload = new ButtonGroup();
121 		bgUpload.add(jrbUploadDefault);
122 		bgUpload.add(jrbUploadNever);
123 		bgUpload.add(jrbUploadAlways);
124 
125 		// downloads
126 		JPanel jpDownload = new JPanel(new GridBagLayout());
127 		jpDownload.setBorder
128 			(GUIHelper.createDefaultBorder(XNap.tr("Allow Downloads From User")));
129 		GridBagHelper.add(getMainPanel(), jpDownload);
130 
131 		jrbDownloadDefault = new JRadioButton(XNap.tr("Default"));
132 		jrbDownloadDefault.setActionCommand(OpenNapGlobalUser.TRANSFER_DEFAULT + "");
133 		GridBagHelper.add(jpDownload, jrbDownloadDefault);
134 
135 		jrbDownloadNever = new JRadioButton(XNap.tr("Never"));
136 		jrbDownloadNever.setActionCommand(OpenNapGlobalUser.TRANSFER_NEVER + "");
137 		GridBagHelper.add(jpDownload, jrbDownloadNever);
138 
139 		bgDownload = new ButtonGroup();
140 		bgDownload.add(jrbDownloadDefault);
141 		bgDownload.add(jrbDownloadNever);
142     }
143 
144 
145     public boolean apply()
146     {
147 		user.setComment(jtComment.getText());
148 		user.setCategory(jtCategory.getText());
149 
150 		user.setChatIgnored(jcbIgnoreChat.isSelected());
151 
152 		user.setMaxUploads(getValue(bgUpload));
153 
154 		user.setMaxDownloads(getValue(bgDownload));
155 		return true;
156     }
157 
158     public OpenNapGlobalUser getUser()
159     {
160 		return user;
161     }
162 
163     public void setUser(OpenNapGlobalUser user)
164     {
165 		this.user = user;
166 
167 		jtName.setText(user.getName());
168 		jtComment.setText(user.getComment());
169 		jtCategory.setText(user.getCategory());
170 
171 		jcbIgnoreChat.setSelected(user.isChatIgnored());
172 
173 		setValue(bgDownload, user.getMaxDownloads());
174 
175 		setValue(bgUpload, user.getMaxUploads());
176     }
177 
178     private int getValue(ButtonGroup bg)
179     {
180 		return Integer.parseInt(bg.getSelection().getActionCommand());
181     }
182 
183     private void setValue(ButtonGroup bg, int value)
184     {
185 		for (Enumeration e = bg.getElements(); e.hasMoreElements();) {
186 			AbstractButton b = (AbstractButton)e.nextElement();
187 			if (b.getActionCommand().equals(value + "")) {
188 				b.setSelected(true);
189 				return;
190 			}
191 		}
192 
193     }
194 
195 }
196