KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > dck > items > NetworkSelect


1 package rero.dck.items;
2
3 import java.awt.*;
4 import java.awt.event.*;
5
6 import javax.swing.*;
7 import javax.swing.event.*;
8
9 import java.io.*;
10
11 import rero.dck.*;
12 import rero.config.*;
13
14 import rero.dialogs.toolkit.*;
15 import rero.dialogs.server.*;
16
17 import java.util.*;
18
19 public class NetworkSelect extends SuperInput implements ItemListener
20 {
21    public static final String JavaDoc ALL_NETWORKS = "All Networks";
22
23    protected JComboBox networks;
24    protected JButton add;
25    protected JButton delete;
26
27    protected String JavaDoc networkV;
28    protected String JavaDoc currentV;
29    protected StringList data;
30
31    public NetworkSelect(String JavaDoc _variableNetworks, String JavaDoc _variableCurrent)
32    {
33       networkV = _variableNetworks;
34       currentV = _variableCurrent;
35
36       setLayout(new BorderLayout());
37
38       add(new JLabel(" Network:"), BorderLayout.NORTH);
39
40       networks = new JComboBox();
41       networks.setPrototypeDisplayValue("SuperLamerNet");
42
43       networks.addItemListener(this);
44
45       JPanel bottom = new JPanel();
46       bottom.setLayout(new FlowLayout(FlowLayout.LEFT));
47
48       bottom.add(networks);
49       
50       add = new JButton("Add");
51       add.setMnemonic('A');
52       add.addActionListener(new ActionListener()
53       {
54          public void actionPerformed(ActionEvent ev)
55          {
56             showNetworkDialog();
57          }
58       });
59
60       delete = new JButton("Delete");
61       delete.setMnemonic('D');
62
63       delete.addActionListener(new ActionListener()
64       {
65          public void actionPerformed(ActionEvent ev)
66          {
67             if (networks.getSelectedIndex() > 0) // we don't want to remove the first element, so > 0...
68
{
69                ClientState.getClientState().setString(getVariable(), "");
70                ClientState.getClientState().sync();
71
72                int zz = networks.getSelectedIndex() - 1;
73                data.remove(networks.getSelectedItem().toString());
74                refresh();
75                networks.setSelectedIndex(zz);
76             }
77          }
78       });
79  
80       bottom.add(add);
81       bottom.add(delete);
82
83       add(bottom, BorderLayout.CENTER);
84    }
85
86    public void showNetworkDialog()
87    {
88        NetworkPanel panel = new NetworkPanel();
89        panel.setupDialog(null);
90
91        ADialog temp = new ADialog(this, "Select a network", panel, null);
92        temp.pack();
93
94        String JavaDoc network = (String JavaDoc)temp.showDialog(null);
95
96        if (network != null && !data.isValue(network))
97        {
98           data.add(network);
99           refresh();
100           networks.setSelectedIndex(networks.getItemCount() - 1);
101        }
102    }
103
104    protected static class NetworkPanel extends APanel
105    {
106       private JList list;
107
108       public void setupDialog(Object JavaDoc value)
109       {
110          DefaultListModel model = new DefaultListModel();
111
112          Iterator i = ServerData.getServerData().getGroups().iterator();
113          i.next(); // skip "All Servers"
114
i.next(); // skip "Random Servers"
115

116          while (i.hasNext())
117          {
118             ServerGroup group = (ServerGroup)i.next();
119             model.addElement(group.getName());
120          }
121
122          list = new JList(model);
123
124          addComponent(new JLabel("Select a network:"));
125          addComponent(new JScrollPane(list));
126       }
127  
128       public void processParent(final ADialog dialog)
129       {
130          list.addMouseListener(new MouseAdapter()
131          {
132             public void mouseClicked(MouseEvent ev)
133             {
134                if (ev.getClickCount() > 1)
135                {
136                   dialog.closeAndReturn();
137                }
138             }
139          });
140       }
141
142       public Object JavaDoc getValue(Object JavaDoc value)
143       {
144          return list.getSelectedValue();
145       }
146    }
147
148    public void save()
149    {
150       if (data != null)
151          data.save();
152    }
153
154    public int getEstimatedWidth()
155    {
156       return 0;
157    }
158
159    public void setAlignWidth(int width)
160    {
161    }
162
163    public void itemStateChanged(ItemEvent ev)
164    {
165       if (ev.getStateChange() == ItemEvent.SELECTED)
166       {
167          // fire an action listener event with the action command being the "network"
168
fireEvent(networks.getSelectedItem().toString());
169          notifyParent();
170       }
171    }
172
173    public JComponent getComponent()
174    {
175       return this;
176    }
177
178    public void refresh()
179    {
180       networks.removeAllItems();
181       networks.addItem(ALL_NETWORKS);
182       
183       if (data == null)
184       {
185          data = ClientState.getClientState().getStringList(networkV);
186       }
187
188       LinkedList temp = data.getList();
189       Iterator i = temp.iterator();
190       while (i.hasNext())
191       {
192          networks.addItem(i.next().toString());
193       }
194
195       networks.setSelectedIndex(0);
196    }
197
198    protected LinkedList listeners = new LinkedList();
199
200    protected void fireEvent(String JavaDoc command)
201    {
202       ActionEvent ev = new ActionEvent(this, 0, command);
203
204       Iterator i = listeners.iterator();
205       while (i.hasNext())
206       {
207          ActionListener temp = (ActionListener)i.next();
208          temp.actionPerformed(ev);
209       }
210    }
211
212    public void addActionListener(ActionListener l) { listeners.add(l); }
213    public void addDeleteListener(ActionListener l) { delete.addActionListener(l); }
214 }
215
216
217
Popular Tags