1 | /* |
---|
2 | * Jeti, a Java Jabber client, Copyright (C) 2001 E.S. de Boer |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation; either version 2 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | * |
---|
18 | * For questions, comments etc, |
---|
19 | * use the website at http://jeti.jabberstudio.org |
---|
20 | * or mail/im me at jeti@jabberstudio.org |
---|
21 | */ |
---|
22 | |
---|
23 | package nu.fw.jeti.ui; |
---|
24 | |
---|
25 | import java.awt.GridBagConstraints; |
---|
26 | import java.awt.GridBagLayout; |
---|
27 | import java.awt.Insets; |
---|
28 | import java.awt.event.ActionEvent; |
---|
29 | import java.text.MessageFormat; |
---|
30 | import java.util.List; |
---|
31 | |
---|
32 | import javax.swing.*; |
---|
33 | |
---|
34 | import nu.fw.jeti.jabber.Backend; |
---|
35 | import nu.fw.jeti.jabber.JIDStatus; |
---|
36 | import nu.fw.jeti.jabber.elements.IQXRoster; |
---|
37 | import nu.fw.jeti.jabber.elements.InfoQuery; |
---|
38 | import nu.fw.jeti.jabber.elements.RosterItem; |
---|
39 | import nu.fw.jeti.util.I18N; |
---|
40 | import nu.fw.jeti.util.Utils; |
---|
41 | |
---|
42 | /** |
---|
43 | * |
---|
44 | * @author E.S. de Boer |
---|
45 | */ |
---|
46 | |
---|
47 | public class GroupDialog extends JDialog |
---|
48 | { |
---|
49 | private JPanel btnPanel = new JPanel(); |
---|
50 | private JLabel lbltekst = new JLabel(); |
---|
51 | private JComboBox cmbGroup; |
---|
52 | private JButton btnCancel = new JButton(); |
---|
53 | private JButton btnOK = new JButton(); |
---|
54 | private String[] groups; |
---|
55 | //private String operation; |
---|
56 | private Backend backend; |
---|
57 | private String currentGroup; |
---|
58 | private JIDStatus jidStatus; |
---|
59 | |
---|
60 | public GroupDialog(JIDStatus jidStatus, Backend backend) |
---|
61 | { |
---|
62 | super(backend.getMainFrame(), I18N.gettext("main.main.rostermenu.Add_to_Group"), false); |
---|
63 | groups = backend.getAllGroups(); |
---|
64 | String[] temp = new String[groups.length - 1]; |
---|
65 | int i = 0; |
---|
66 | for (int tel = 0; tel < groups.length; tel++) |
---|
67 | {//remove group wich contains jid |
---|
68 | if (!jidStatus.isGroupPresent(groups[tel])) |
---|
69 | { |
---|
70 | temp[i] = groups[tel]; |
---|
71 | i++; |
---|
72 | } |
---|
73 | } |
---|
74 | groups = new String[i]; |
---|
75 | System.arraycopy(temp, 0, groups, 0, i); |
---|
76 | init(jidStatus, backend); |
---|
77 | lbltekst.setText(MessageFormat.format(I18N.gettext("main.popup.Add_{0}_to"), new Object[] { jidStatus.getNick()})); |
---|
78 | pack(); |
---|
79 | setLocationRelativeTo(null); |
---|
80 | } |
---|
81 | |
---|
82 | private void init(JIDStatus jidStatus, Backend backend) |
---|
83 | { |
---|
84 | this.backend = backend; |
---|
85 | this.jidStatus = jidStatus; |
---|
86 | try |
---|
87 | { |
---|
88 | jbInit(); |
---|
89 | } catch (Exception ex) |
---|
90 | { |
---|
91 | ex.printStackTrace(); |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | public GroupDialog(String group, JIDStatus jidStatus, Backend backend) |
---|
96 | { |
---|
97 | super(backend.getMainFrame(), I18N.gettext("main.main.rostermenu.Change_Group"), false); |
---|
98 | groups = backend.getAllGroups(); |
---|
99 | String[] temp = new String[groups.length - 1]; |
---|
100 | int i = 0; |
---|
101 | for (int tel = 0; tel < groups.length; tel++) |
---|
102 | {//remove group wich contains jid |
---|
103 | if (!jidStatus.isGroupPresent(groups[tel])) |
---|
104 | { |
---|
105 | temp[i] = groups[tel]; |
---|
106 | i++; |
---|
107 | } |
---|
108 | } |
---|
109 | groups = new String[i]; |
---|
110 | System.arraycopy(temp, 0, groups, 0, i); |
---|
111 | |
---|
112 | currentGroup = group; |
---|
113 | init(jidStatus, backend); |
---|
114 | lbltekst.setText(MessageFormat.format(I18N.gettext("main.popup.Change_{0}_from_{1}_to"), new Object[] { jidStatus.getNick(), |
---|
115 | group})); |
---|
116 | pack(); |
---|
117 | setLocationRelativeTo(null); |
---|
118 | } |
---|
119 | |
---|
120 | void jbInit() throws Exception |
---|
121 | { |
---|
122 | this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
---|
123 | getRootPane().setDefaultButton(btnOK); |
---|
124 | |
---|
125 | cmbGroup = new JComboBox(groups); |
---|
126 | cmbGroup.setAlignmentX((float) 0.0); |
---|
127 | cmbGroup.setEditable(true); |
---|
128 | |
---|
129 | Action cancelAction = new AbstractAction(I18N.gettext("Cancel")) |
---|
130 | { |
---|
131 | public void actionPerformed(ActionEvent e) |
---|
132 | { |
---|
133 | dispose(); |
---|
134 | } |
---|
135 | }; |
---|
136 | Utils.addCancelButton(this, btnCancel, cancelAction); |
---|
137 | |
---|
138 | btnOK.setText(I18N.gettext("OK")); |
---|
139 | btnOK.addActionListener(new java.awt.event.ActionListener() |
---|
140 | { |
---|
141 | public void actionPerformed(ActionEvent e) |
---|
142 | { |
---|
143 | btnOK_actionPerformed(e); |
---|
144 | } |
---|
145 | }); |
---|
146 | |
---|
147 | getContentPane().setLayout(new GridBagLayout()); |
---|
148 | GridBagConstraints c = new GridBagConstraints(); |
---|
149 | c.gridwidth = GridBagConstraints.REMAINDER; |
---|
150 | c.anchor = GridBagConstraints.LINE_START; |
---|
151 | c.fill = GridBagConstraints.BOTH; |
---|
152 | c.insets = new Insets(3, 3, 0, 3); |
---|
153 | c.weightx = 1.0; |
---|
154 | |
---|
155 | getContentPane().add(lbltekst, c); |
---|
156 | getContentPane().add(cmbGroup, c); |
---|
157 | btnPanel.add(btnOK, null); |
---|
158 | btnPanel.add(btnCancel, null); |
---|
159 | getContentPane().add(btnPanel, c); |
---|
160 | } |
---|
161 | |
---|
162 | void btnOK_actionPerformed(ActionEvent e) |
---|
163 | { |
---|
164 | String group = (String) cmbGroup.getSelectedItem(); |
---|
165 | if (!group.equals(" ")) |
---|
166 | { |
---|
167 | List groups = jidStatus.getGroupsCopy(); |
---|
168 | if (currentGroup != null) |
---|
169 | { |
---|
170 | //backend.changeGroup(jidStatus,currentGroup,group); |
---|
171 | groups.remove(currentGroup); |
---|
172 | groups.add(group); |
---|
173 | IQXRoster roster = new IQXRoster(new RosterItem(jidStatus.getJID(), jidStatus.getNick(), null, null, groups)); |
---|
174 | backend.send(new InfoQuery("set", roster)); |
---|
175 | } else |
---|
176 | { |
---|
177 | if (!jidStatus.isGroupPresent(group)) |
---|
178 | { |
---|
179 | groups.add(group); |
---|
180 | IQXRoster roster = new IQXRoster(new RosterItem(jidStatus.getJID(), jidStatus.getNick(), null, null, groups)); |
---|
181 | backend.send(new InfoQuery("set", roster)); |
---|
182 | //backend.addGroup(jidStatus,group); |
---|
183 | } |
---|
184 | } |
---|
185 | } |
---|
186 | this.dispose(); |
---|
187 | } |
---|
188 | |
---|
189 | } |
---|
190 | /* |
---|
191 | * Overrides for emacs |
---|
192 | * Local variables: |
---|
193 | * tab-width: 4 |
---|
194 | * End: |
---|
195 | */ |
---|