KickJava   Java API By Example, From Geeks To Geeks.

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


1 package rero.dck.items;
2
3 import java.awt.*;
4 import java.awt.event.*;
5
6 import javax.swing.*;
7 import javax.swing.table.*;
8 import javax.swing.event.*;
9
10 import rero.config.*;
11 import rero.dck.*;
12
13 public class ListInput extends SuperInput implements ActionListener
14 {
15    protected InputListModel model;
16    protected JList list;
17    protected StringList data;
18
19    protected String JavaDoc desc;
20    protected String JavaDoc title;
21  
22    public ListInput(String JavaDoc variable, String JavaDoc _title, String JavaDoc _desc, int width, int height)
23    {
24       title = _title;
25       desc = _desc;
26
27       setLayout(new BorderLayout());
28       
29       data = ClientState.getClientState().getStringList(variable);
30       data.load();
31
32       model = new InputListModel();
33       list = new JList(model);
34
35       JPanel temp = new JPanel();
36       temp.setPreferredSize(new Dimension(width, height));
37
38       add(temp, BorderLayout.EAST);
39
40       temp = new JPanel();
41       temp.setPreferredSize(new Dimension(width, height));
42
43       add(temp, BorderLayout.WEST);
44
45       add(new JScrollPane(list), BorderLayout.CENTER);
46    
47       JPanel buttons = new JPanel();
48       buttons.setLayout(new FlowLayout(FlowLayout.CENTER));
49
50       JButton addme = new JButton("Add");
51       addme.setMnemonic('A');
52       addme.addActionListener(this);
53       buttons.add(addme);
54
55       JButton remme = new JButton("Remove");
56       remme.setMnemonic('R');
57       remme.addActionListener(this);
58       buttons.add(remme);
59
60       add(buttons, BorderLayout.SOUTH);
61
62       setMinimumSize(new Dimension(width, height));
63    }
64
65    public void save()
66    {
67       data.save();
68    }
69
70    public int getEstimatedWidth()
71    {
72       return 0;
73    }
74
75    public void setAlignWidth(int width)
76    {
77    }
78
79    public JComponent getComponent()
80    {
81       return this;
82    }
83
84    public void refresh()
85    {
86       data.load();
87       model.fireChange();
88    }
89
90    public void actionPerformed(ActionEvent ev)
91    {
92       if (ev.getActionCommand().equals("Remove"))
93       {
94           if (list.getSelectedIndex() >= 0)
95           {
96              data.getList().remove(list.getSelectedIndex());
97              model.fireChange();
98           }
99       }
100
101       if (ev.getActionCommand().equals("Add"))
102       {
103           String JavaDoc input = JOptionPane.showInputDialog(this, title, desc, JOptionPane.QUESTION_MESSAGE);
104           if (input != null)
105           {
106              data.getList().add(input);
107              model.fireChange();
108           }
109       }
110
111       notifyParent();
112    }
113
114    protected class InputListModel extends AbstractListModel
115    {
116       public void fireChange()
117       {
118          model.fireContentsChanged(model, 0, model.getSize());
119       }
120
121       public Object JavaDoc getElementAt(int index)
122       {
123          return (String JavaDoc)data.getList().get(index);
124       }
125
126       public int getSize()
127       {
128          return data.getList().size();
129       }
130    }
131 }
132
133
134
Popular Tags