KickJava   Java API By Example, From Geeks To Geeks.

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


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 import javax.swing.filechooser.*;
14
15 import java.util.*;
16 import java.io.*;
17
18 public class FileListInput extends SuperInput implements ActionListener, ListSelectionListener
19 {
20    protected InputListModel model;
21    protected JList list;
22    protected StringList data;
23    protected JTextField fullPath;
24
25    protected JButton addme, remme;
26
27    protected String JavaDoc desc;
28  
29    protected JFileChooser chooser;
30
31    public FileListInput(String JavaDoc variable, String JavaDoc _desc, String JavaDoc add, char mn1, String JavaDoc rem, char mn2, int width, int height)
32    {
33       desc = _desc;
34
35       setLayout(new BorderLayout());
36       
37       data = ClientState.getClientState().getStringList(variable);
38       data.load();
39
40       model = new InputListModel();
41       list = new JList(model);
42
43       JPanel temp = new JPanel();
44       temp.setPreferredSize(new Dimension(width, height));
45
46       add(temp, BorderLayout.EAST);
47
48       temp = new JPanel();
49       temp.setPreferredSize(new Dimension(width, height));
50
51       add(temp, BorderLayout.WEST);
52
53       add(new JScrollPane(list), BorderLayout.CENTER);
54    
55       JPanel buttons = new JPanel();
56       buttons.setLayout(new FlowLayout(FlowLayout.CENTER));
57
58       JLabel l_path = new JLabel("Selected: ");
59
60       fullPath = new JTextField("no file selected");
61       fullPath.setBorder(null);
62       fullPath.setEditable(false);
63       fullPath.setOpaque(false);
64
65       JPanel pathDisplay = new JPanel();
66       pathDisplay.setLayout(new BorderLayout());
67       pathDisplay.add(l_path, BorderLayout.WEST);
68       pathDisplay.add(fullPath, BorderLayout.CENTER);
69
70       pathDisplay.setBorder(BorderFactory.createEmptyBorder(0, width, 0, 0));
71
72       addme = new JButton(add);
73       addme.setMnemonic(mn1);
74       addme.addActionListener(this);
75       buttons.add(addme);
76
77       remme = new JButton(rem);
78       remme.setMnemonic(mn2);
79       remme.addActionListener(this);
80       buttons.add(remme);
81
82       JPanel evil = new JPanel();
83       evil.setLayout(new BorderLayout());
84       evil.add(pathDisplay, BorderLayout.NORTH);
85       evil.add(buttons, BorderLayout.SOUTH);
86   
87       list.addListSelectionListener(this);
88
89       add(evil, BorderLayout.SOUTH);
90
91       setMinimumSize(new Dimension(width, height));
92    }
93
94    public void save()
95    {
96       data.save();
97    }
98
99    public int getEstimatedWidth()
100    {
101       return 0;
102    }
103
104    public void valueChanged(ListSelectionEvent ev)
105    {
106       if (!ev.getValueIsAdjusting())
107       {
108          setSelectedCaption();
109       }
110    }
111
112    public void setSelectedCaption()
113    {
114       if (list.getSelectedIndex() > -1 && list.getSelectedIndex() < list.getModel().getSize())
115       {
116          fullPath.setText(data.getList().get(list.getSelectedIndex()).toString());
117       }
118       else
119       {
120          fullPath.setText("no file selected");
121       }
122    }
123
124    public void setAlignWidth(int width)
125    {
126    }
127
128    public JComponent getComponent()
129    {
130       return this;
131    }
132
133    public void refresh()
134    {
135       data.load();
136       model.fireChange();
137    }
138
139    public void actionPerformed(ActionEvent ev)
140    {
141       if (ev.getSource() == remme)
142       {
143           if (list.getSelectedIndex() >= 0)
144           {
145              data.getList().remove(list.getSelectedIndex());
146              model.fireChange();
147           }
148       }
149
150       if (ev.getSource() == addme)
151       {
152           if (chooser == null)
153           {
154              chooser = new JFileChooser();
155              chooser.setApproveButtonText(desc);
156           }
157
158           if (chooser.showDialog(this, null) == JFileChooser.APPROVE_OPTION)
159           {
160              data.getList().add(chooser.getSelectedFile().getAbsolutePath());
161              model.fireChange();
162           }
163       }
164
165       setSelectedCaption();
166       notifyParent();
167    }
168
169    protected class InputListModel extends AbstractListModel
170    {
171       public void fireChange()
172       {
173          model.fireContentsChanged(model, 0, model.getSize());
174       }
175
176       public Object JavaDoc getElementAt(int index)
177       {
178          return new File((String JavaDoc)data.getList().get(index)).getName();
179       }
180
181       public int getSize()
182       {
183          return data.getList().size();
184       }
185    }
186 }
187
188
189
Popular Tags