2 |
26 Feb 07 |
jari |
1 |
/* |
2 |
26 Feb 07 |
jari |
Copyright @ 1999-2003, The Institute for Genomic Research (TIGR). |
2 |
26 Feb 07 |
jari |
All rights reserved. |
2 |
26 Feb 07 |
jari |
4 |
*/ |
2 |
26 Feb 07 |
jari |
5 |
/* |
2 |
26 Feb 07 |
jari |
* $RCSfile: FileTransferPanel.java,v $ |
2 |
26 Feb 07 |
jari |
* $Revision: 1.3 $ |
2 |
26 Feb 07 |
jari |
* $Date: 2005/03/10 15:27:49 $ |
2 |
26 Feb 07 |
jari |
* $Author: braistedj $ |
2 |
26 Feb 07 |
jari |
* $State: Exp $ |
2 |
26 Feb 07 |
jari |
11 |
*/ |
2 |
26 Feb 07 |
jari |
12 |
package org.tigr.util.swing; |
2 |
26 Feb 07 |
jari |
13 |
|
2 |
26 Feb 07 |
jari |
14 |
import java.awt.Component; |
2 |
26 Feb 07 |
jari |
15 |
import java.awt.Dimension; |
2 |
26 Feb 07 |
jari |
16 |
import java.awt.GridBagConstraints; |
2 |
26 Feb 07 |
jari |
17 |
import java.awt.GridBagLayout; |
2 |
26 Feb 07 |
jari |
18 |
import java.awt.Insets; |
2 |
26 Feb 07 |
jari |
19 |
import java.awt.event.ActionEvent; |
2 |
26 Feb 07 |
jari |
20 |
import java.awt.event.ActionListener; |
2 |
26 Feb 07 |
jari |
21 |
import java.awt.event.MouseAdapter; |
2 |
26 Feb 07 |
jari |
22 |
import java.awt.event.MouseEvent; |
2 |
26 Feb 07 |
jari |
23 |
import java.io.File; |
2 |
26 Feb 07 |
jari |
24 |
|
2 |
26 Feb 07 |
jari |
25 |
import javax.swing.DefaultListCellRenderer; |
2 |
26 Feb 07 |
jari |
26 |
import javax.swing.DefaultListModel; |
2 |
26 Feb 07 |
jari |
27 |
import javax.swing.JButton; |
2 |
26 Feb 07 |
jari |
28 |
import javax.swing.JFileChooser; |
2 |
26 Feb 07 |
jari |
29 |
import javax.swing.JLabel; |
2 |
26 Feb 07 |
jari |
30 |
import javax.swing.JList; |
2 |
26 Feb 07 |
jari |
31 |
import javax.swing.JPanel; |
2 |
26 Feb 07 |
jari |
32 |
import javax.swing.JScrollPane; |
2 |
26 Feb 07 |
jari |
33 |
import javax.swing.JTextField; |
2 |
26 Feb 07 |
jari |
34 |
import javax.swing.border.BevelBorder; |
2 |
26 Feb 07 |
jari |
35 |
import javax.swing.event.ListSelectionEvent; |
2 |
26 Feb 07 |
jari |
36 |
import javax.swing.event.ListSelectionListener; |
2 |
26 Feb 07 |
jari |
37 |
import javax.swing.filechooser.FileFilter; |
2 |
26 Feb 07 |
jari |
38 |
import javax.swing.filechooser.FileSystemView; |
2 |
26 Feb 07 |
jari |
39 |
|
2 |
26 Feb 07 |
jari |
40 |
public class FileTransferPanel extends JPanel { |
2 |
26 Feb 07 |
jari |
41 |
|
2 |
26 Feb 07 |
jari |
42 |
private static final String FILE_ADD_CMD = "file-add"; |
2 |
26 Feb 07 |
jari |
43 |
private static final String FILE_DEL_CMD = "file-del"; |
2 |
26 Feb 07 |
jari |
44 |
private static final String FILE_ALL_ADD_CMD = "file-all-add"; |
2 |
26 Feb 07 |
jari |
45 |
private static final String FILE_ALL_DEL_CMD = "file-all-del"; |
2 |
26 Feb 07 |
jari |
46 |
private static final String FILE_CHOOSE = "file-choose"; |
2 |
26 Feb 07 |
jari |
47 |
|
2 |
26 Feb 07 |
jari |
48 |
private JList availList; |
2 |
26 Feb 07 |
jari |
49 |
private JList choosedList; |
2 |
26 Feb 07 |
jari |
50 |
private DefaultListModel availModel; |
2 |
26 Feb 07 |
jari |
51 |
private DefaultListModel choosedModel; |
2 |
26 Feb 07 |
jari |
52 |
|
2 |
26 Feb 07 |
jari |
53 |
private JButton addButton; |
2 |
26 Feb 07 |
jari |
54 |
private JButton delButton; |
2 |
26 Feb 07 |
jari |
55 |
private JButton addAllButton; |
2 |
26 Feb 07 |
jari |
56 |
private JButton delAllButton; |
2 |
26 Feb 07 |
jari |
57 |
|
2 |
26 Feb 07 |
jari |
58 |
private FileFilter[] fileFilters; |
2 |
26 Feb 07 |
jari |
59 |
private File directory; |
2 |
26 Feb 07 |
jari |
60 |
|
2 |
26 Feb 07 |
jari |
61 |
/** |
2 |
26 Feb 07 |
jari |
* Constructs a <code>FileTransferPanel</code> with specified initial |
2 |
26 Feb 07 |
jari |
* directory and set of file filters. |
2 |
26 Feb 07 |
jari |
64 |
*/ |
2 |
26 Feb 07 |
jari |
65 |
public FileTransferPanel(String currentDirectory, FileFilter[] fileFilters) { |
2 |
26 Feb 07 |
jari |
66 |
setLayout(new GridBagLayout()); |
2 |
26 Feb 07 |
jari |
67 |
setBorder(new BevelBorder(BevelBorder.RAISED)); |
2 |
26 Feb 07 |
jari |
68 |
|
2 |
26 Feb 07 |
jari |
69 |
this.fileFilters = fileFilters; |
2 |
26 Feb 07 |
jari |
70 |
|
2 |
26 Feb 07 |
jari |
71 |
Listener listener = new Listener(); |
2 |
26 Feb 07 |
jari |
72 |
|
2 |
26 Feb 07 |
jari |
73 |
GridBagConstraints gbc = new GridBagConstraints(); |
2 |
26 Feb 07 |
jari |
74 |
gbc.insets = new Insets(5, 5, 5, 5); |
2 |
26 Feb 07 |
jari |
75 |
|
2 |
26 Feb 07 |
jari |
// directory panel |
2 |
26 Feb 07 |
jari |
77 |
gbc.gridx = 0; |
2 |
26 Feb 07 |
jari |
78 |
gbc.gridy = 0; |
2 |
26 Feb 07 |
jari |
79 |
gbc.weightx = 1.0; |
2 |
26 Feb 07 |
jari |
80 |
gbc.gridwidth = 3; |
2 |
26 Feb 07 |
jari |
81 |
gbc.fill = GridBagConstraints.HORIZONTAL; |
2 |
26 Feb 07 |
jari |
82 |
|
2 |
26 Feb 07 |
jari |
83 |
JPanel dirPanel = createDirPanel(currentDirectory); |
2 |
26 Feb 07 |
jari |
84 |
add(dirPanel, gbc); |
2 |
26 Feb 07 |
jari |
85 |
|
2 |
26 Feb 07 |
jari |
// labels... |
2 |
26 Feb 07 |
jari |
87 |
gbc.gridx = 0; |
2 |
26 Feb 07 |
jari |
88 |
gbc.gridy = 1; |
2 |
26 Feb 07 |
jari |
89 |
gbc.weightx = 0.0; |
2 |
26 Feb 07 |
jari |
90 |
gbc.gridwidth = 1; |
2 |
26 Feb 07 |
jari |
91 |
gbc.fill = GridBagConstraints.NONE; |
2 |
26 Feb 07 |
jari |
92 |
gbc.anchor = GridBagConstraints.WEST; |
2 |
26 Feb 07 |
jari |
93 |
|
2 |
26 Feb 07 |
jari |
94 |
add(new JLabel("Available:"), gbc); |
2 |
26 Feb 07 |
jari |
95 |
|
2 |
26 Feb 07 |
jari |
96 |
gbc.gridx = 2; |
2 |
26 Feb 07 |
jari |
97 |
|
2 |
26 Feb 07 |
jari |
98 |
add(new JLabel("Chosen:"), gbc); |
2 |
26 Feb 07 |
jari |
99 |
|
2 |
26 Feb 07 |
jari |
// lists |
2 |
26 Feb 07 |
jari |
101 |
JScrollPane scroller; |
2 |
26 Feb 07 |
jari |
102 |
|
2 |
26 Feb 07 |
jari |
103 |
gbc.gridx = 0; |
2 |
26 Feb 07 |
jari |
104 |
gbc.gridy = 2; |
2 |
26 Feb 07 |
jari |
105 |
gbc.weightx = 0.5; |
2 |
26 Feb 07 |
jari |
106 |
gbc.weighty = 1.0; |
2 |
26 Feb 07 |
jari |
107 |
gbc.fill = GridBagConstraints.BOTH; |
2 |
26 Feb 07 |
jari |
108 |
gbc.anchor = GridBagConstraints.CENTER; |
2 |
26 Feb 07 |
jari |
109 |
|
2 |
26 Feb 07 |
jari |
110 |
availList = createList(listener); |
2 |
26 Feb 07 |
jari |
111 |
availModel = (DefaultListModel)availList.getModel(); |
2 |
26 Feb 07 |
jari |
112 |
scroller = new JScrollPane(availList); |
2 |
26 Feb 07 |
jari |
113 |
scroller.setPreferredSize(new Dimension(100, 100)); |
2 |
26 Feb 07 |
jari |
114 |
add(scroller, gbc); |
2 |
26 Feb 07 |
jari |
115 |
|
2 |
26 Feb 07 |
jari |
116 |
gbc.gridx = 2; |
2 |
26 Feb 07 |
jari |
117 |
|
2 |
26 Feb 07 |
jari |
118 |
choosedList = createList(listener); |
2 |
26 Feb 07 |
jari |
119 |
choosedModel = (DefaultListModel)choosedList.getModel(); |
2 |
26 Feb 07 |
jari |
120 |
scroller = new JScrollPane(choosedList); |
2 |
26 Feb 07 |
jari |
121 |
scroller.setPreferredSize(new Dimension(100, 100)); |
2 |
26 Feb 07 |
jari |
122 |
add(scroller, gbc); |
2 |
26 Feb 07 |
jari |
123 |
|
2 |
26 Feb 07 |
jari |
// buttons |
2 |
26 Feb 07 |
jari |
125 |
gbc.gridx = 1; |
2 |
26 Feb 07 |
jari |
126 |
gbc.weightx = 0.0; |
2 |
26 Feb 07 |
jari |
127 |
gbc.weighty = 0.0; |
2 |
26 Feb 07 |
jari |
128 |
gbc.fill = GridBagConstraints.NONE; |
2 |
26 Feb 07 |
jari |
129 |
|
2 |
26 Feb 07 |
jari |
130 |
JPanel btnsPanel = createBtnsPanel(listener); |
2 |
26 Feb 07 |
jari |
131 |
add(btnsPanel, gbc); |
2 |
26 Feb 07 |
jari |
132 |
|
2 |
26 Feb 07 |
jari |
133 |
updateDirectory(new File(currentDirectory)); |
2 |
26 Feb 07 |
jari |
134 |
} |
2 |
26 Feb 07 |
jari |
135 |
|
2 |
26 Feb 07 |
jari |
136 |
/** |
2 |
26 Feb 07 |
jari |
* Returns an array of choosed files. |
2 |
26 Feb 07 |
jari |
138 |
*/ |
2 |
26 Feb 07 |
jari |
139 |
public File[] getFiles() { |
2 |
26 Feb 07 |
jari |
140 |
File[] list = new File[choosedModel.size()]; |
2 |
26 Feb 07 |
jari |
141 |
choosedModel.copyInto(list); |
2 |
26 Feb 07 |
jari |
142 |
return list; |
2 |
26 Feb 07 |
jari |
143 |
} |
2 |
26 Feb 07 |
jari |
144 |
|
2 |
26 Feb 07 |
jari |
145 |
/** |
2 |
26 Feb 07 |
jari |
* Returns number of choosed files. |
2 |
26 Feb 07 |
jari |
147 |
*/ |
2 |
26 Feb 07 |
jari |
148 |
public int getFilesNumber() { |
2 |
26 Feb 07 |
jari |
149 |
return choosedModel.size(); |
2 |
26 Feb 07 |
jari |
150 |
} |
2 |
26 Feb 07 |
jari |
151 |
|
2 |
26 Feb 07 |
jari |
152 |
/** |
2 |
26 Feb 07 |
jari |
* Updates set of avaiable files from the specified directory. |
2 |
26 Feb 07 |
jari |
154 |
*/ |
2 |
26 Feb 07 |
jari |
155 |
private void updateDirectory(File directory) { |
2 |
26 Feb 07 |
jari |
// if not a folder |
2 |
26 Feb 07 |
jari |
157 |
if (!directory.isDirectory()) { |
2 |
26 Feb 07 |
jari |
158 |
return; |
2 |
26 Feb 07 |
jari |
159 |
} |
2 |
26 Feb 07 |
jari |
// if the same folder |
2 |
26 Feb 07 |
jari |
161 |
if (this.directory != null && this.directory.equals(directory)) { |
2 |
26 Feb 07 |
jari |
162 |
return; |
2 |
26 Feb 07 |
jari |
163 |
} |
2 |
26 Feb 07 |
jari |
164 |
this.directory = directory; |
2 |
26 Feb 07 |
jari |
165 |
|
2 |
26 Feb 07 |
jari |
166 |
availModel.clear(); |
2 |
26 Feb 07 |
jari |
167 |
choosedModel.clear(); |
2 |
26 Feb 07 |
jari |
168 |
|
2 |
26 Feb 07 |
jari |
169 |
FileSystemView fileSystem = FileSystemView.getFileSystemView(); |
2 |
26 Feb 07 |
jari |
170 |
File[] list = fileSystem.getFiles(directory, true); |
2 |
26 Feb 07 |
jari |
171 |
for (int i=0; i<list.length; i++) { |
2 |
26 Feb 07 |
jari |
172 |
if (accept(list[i])) { |
2 |
26 Feb 07 |
jari |
173 |
availModel.addElement(list[i]); |
2 |
26 Feb 07 |
jari |
174 |
} |
2 |
26 Feb 07 |
jari |
175 |
} |
2 |
26 Feb 07 |
jari |
176 |
updateButtons(); |
2 |
26 Feb 07 |
jari |
177 |
} |
2 |
26 Feb 07 |
jari |
178 |
|
2 |
26 Feb 07 |
jari |
179 |
/** |
2 |
26 Feb 07 |
jari |
* Check if passed file is suitable. |
2 |
26 Feb 07 |
jari |
181 |
*/ |
2 |
26 Feb 07 |
jari |
182 |
private boolean accept(File file) { |
2 |
26 Feb 07 |
jari |
183 |
if (file.isDirectory()) { |
2 |
26 Feb 07 |
jari |
184 |
return false; |
2 |
26 Feb 07 |
jari |
185 |
} |
2 |
26 Feb 07 |
jari |
186 |
if (fileFilters == null) { |
2 |
26 Feb 07 |
jari |
187 |
return true; |
2 |
26 Feb 07 |
jari |
188 |
} |
2 |
26 Feb 07 |
jari |
189 |
for (int i=0; i<fileFilters.length; i++) { |
2 |
26 Feb 07 |
jari |
190 |
if (fileFilters[i].accept(file)) { |
2 |
26 Feb 07 |
jari |
191 |
return true; |
2 |
26 Feb 07 |
jari |
192 |
} |
2 |
26 Feb 07 |
jari |
193 |
} |
2 |
26 Feb 07 |
jari |
194 |
return false; |
2 |
26 Feb 07 |
jari |
195 |
} |
2 |
26 Feb 07 |
jari |
196 |
|
2 |
26 Feb 07 |
jari |
197 |
/** |
2 |
26 Feb 07 |
jari |
* Adds selected files. |
2 |
26 Feb 07 |
jari |
199 |
*/ |
2 |
26 Feb 07 |
jari |
200 |
private void addAction() { |
2 |
26 Feb 07 |
jari |
201 |
int[] indices = availList.getSelectedIndices(); |
2 |
26 Feb 07 |
jari |
202 |
for (int i=0; i<indices.length; i++) { |
2 |
26 Feb 07 |
jari |
203 |
choosedModel.addElement(availModel.elementAt(indices[i])); |
2 |
26 Feb 07 |
jari |
204 |
} |
2 |
26 Feb 07 |
jari |
205 |
for (int i=0; i<indices.length; i++) { |
2 |
26 Feb 07 |
jari |
206 |
availModel.remove(indices[i]-i); |
2 |
26 Feb 07 |
jari |
207 |
} |
2 |
26 Feb 07 |
jari |
208 |
} |
2 |
26 Feb 07 |
jari |
209 |
|
2 |
26 Feb 07 |
jari |
210 |
/** |
2 |
26 Feb 07 |
jari |
* Removes selected files from the choosed files list. |
2 |
26 Feb 07 |
jari |
212 |
*/ |
2 |
26 Feb 07 |
jari |
213 |
private void delAction() { |
2 |
26 Feb 07 |
jari |
214 |
int[] indices = choosedList.getSelectedIndices(); |
2 |
26 Feb 07 |
jari |
215 |
for (int i=0; i<indices.length; i++) { |
2 |
26 Feb 07 |
jari |
216 |
availModel.addElement(choosedModel.elementAt(indices[i])); |
2 |
26 Feb 07 |
jari |
217 |
} |
2 |
26 Feb 07 |
jari |
218 |
for (int i=0; i<indices.length; i++) { |
2 |
26 Feb 07 |
jari |
219 |
choosedModel.remove(indices[i]-i); |
2 |
26 Feb 07 |
jari |
220 |
} |
2 |
26 Feb 07 |
jari |
221 |
} |
2 |
26 Feb 07 |
jari |
222 |
|
2 |
26 Feb 07 |
jari |
223 |
/** |
2 |
26 Feb 07 |
jari |
* Adds all avaiable file to choosed files list. |
2 |
26 Feb 07 |
jari |
225 |
*/ |
2 |
26 Feb 07 |
jari |
226 |
private void addAllAction() { |
2 |
26 Feb 07 |
jari |
227 |
final int SIZE = availModel.size(); |
2 |
26 Feb 07 |
jari |
228 |
for (int i=0; i<SIZE; i++) { |
2 |
26 Feb 07 |
jari |
229 |
choosedModel.addElement(availModel.elementAt(i)); |
2 |
26 Feb 07 |
jari |
230 |
} |
2 |
26 Feb 07 |
jari |
231 |
availModel.clear(); |
2 |
26 Feb 07 |
jari |
232 |
} |
2 |
26 Feb 07 |
jari |
233 |
|
2 |
26 Feb 07 |
jari |
234 |
/** |
2 |
26 Feb 07 |
jari |
* Adds all choosed file from the choosed files list. |
2 |
26 Feb 07 |
jari |
236 |
*/ |
2 |
26 Feb 07 |
jari |
237 |
private void delAllAction() { |
2 |
26 Feb 07 |
jari |
238 |
final int SIZE = choosedModel.size(); |
2 |
26 Feb 07 |
jari |
239 |
for (int i=0; i<SIZE; i++) { |
2 |
26 Feb 07 |
jari |
240 |
availModel.addElement(choosedModel.elementAt(i)); |
2 |
26 Feb 07 |
jari |
241 |
} |
2 |
26 Feb 07 |
jari |
242 |
choosedModel.clear(); |
2 |
26 Feb 07 |
jari |
243 |
} |
2 |
26 Feb 07 |
jari |
244 |
|
2 |
26 Feb 07 |
jari |
245 |
/** |
2 |
26 Feb 07 |
jari |
* Creates a JList. |
2 |
26 Feb 07 |
jari |
247 |
*/ |
2 |
26 Feb 07 |
jari |
248 |
private JList createList(Listener listener) { |
2 |
26 Feb 07 |
jari |
249 |
JList list = new JList(new DefaultListModel()); |
2 |
26 Feb 07 |
jari |
250 |
list.setCellRenderer(new ListRenderer()); |
2 |
26 Feb 07 |
jari |
251 |
list.addListSelectionListener(listener); |
2 |
26 Feb 07 |
jari |
252 |
list.addMouseListener(listener); |
2 |
26 Feb 07 |
jari |
253 |
return list; |
2 |
26 Feb 07 |
jari |
254 |
} |
2 |
26 Feb 07 |
jari |
255 |
|
2 |
26 Feb 07 |
jari |
256 |
/** |
2 |
26 Feb 07 |
jari |
* Creates a panel to display current directory. |
2 |
26 Feb 07 |
jari |
258 |
*/ |
2 |
26 Feb 07 |
jari |
259 |
private JPanel createDirPanel(String currentDirectory) { |
2 |
26 Feb 07 |
jari |
260 |
return new DirectoryPanel(currentDirectory); |
2 |
26 Feb 07 |
jari |
261 |
} |
2 |
26 Feb 07 |
jari |
262 |
|
2 |
26 Feb 07 |
jari |
263 |
/** |
2 |
26 Feb 07 |
jari |
* Creates a panel with 'add', 'del', 'add all' and 'del all' buttons. |
2 |
26 Feb 07 |
jari |
265 |
*/ |
2 |
26 Feb 07 |
jari |
266 |
private JPanel createBtnsPanel(ActionListener listener) { |
2 |
26 Feb 07 |
jari |
267 |
|
2 |
26 Feb 07 |
jari |
268 |
addButton = createButton("Add" , FILE_ADD_CMD , listener); |
2 |
26 Feb 07 |
jari |
269 |
delButton = createButton("Del" , FILE_DEL_CMD , listener); |
2 |
26 Feb 07 |
jari |
270 |
addAllButton = createButton("Add All", FILE_ALL_ADD_CMD, listener); |
2 |
26 Feb 07 |
jari |
271 |
delAllButton = createButton("Del All", FILE_ALL_DEL_CMD, listener); |
2 |
26 Feb 07 |
jari |
272 |
|
2 |
26 Feb 07 |
jari |
273 |
JPanel panel = new JPanel(new GridBagLayout()); |
2 |
26 Feb 07 |
jari |
274 |
GridBagConstraints gbc = new GridBagConstraints(); |
2 |
26 Feb 07 |
jari |
275 |
gbc.fill = GridBagConstraints.HORIZONTAL; |
2 |
26 Feb 07 |
jari |
276 |
gbc.gridx = 0; |
2 |
26 Feb 07 |
jari |
277 |
gbc.gridy = 0; |
2 |
26 Feb 07 |
jari |
278 |
|
2 |
26 Feb 07 |
jari |
279 |
panel.add(addButton, gbc); |
2 |
26 Feb 07 |
jari |
280 |
gbc.gridy++; |
2 |
26 Feb 07 |
jari |
281 |
panel.add(delButton, gbc); |
2 |
26 Feb 07 |
jari |
282 |
gbc.gridy++; |
2 |
26 Feb 07 |
jari |
283 |
panel.add(addAllButton, gbc); |
2 |
26 Feb 07 |
jari |
284 |
gbc.gridy++; |
2 |
26 Feb 07 |
jari |
285 |
panel.add(delAllButton, gbc); |
2 |
26 Feb 07 |
jari |
286 |
return panel; |
2 |
26 Feb 07 |
jari |
287 |
} |
2 |
26 Feb 07 |
jari |
288 |
|
2 |
26 Feb 07 |
jari |
289 |
/** |
2 |
26 Feb 07 |
jari |
* Creates a button with specified text and action command. |
2 |
26 Feb 07 |
jari |
291 |
*/ |
2 |
26 Feb 07 |
jari |
292 |
private JButton createButton(String text, String command, ActionListener listener) { |
2 |
26 Feb 07 |
jari |
293 |
JButton button = new JButton(text); |
2 |
26 Feb 07 |
jari |
294 |
button.setEnabled(false); |
2 |
26 Feb 07 |
jari |
295 |
button.setActionCommand(command); |
2 |
26 Feb 07 |
jari |
296 |
button.addActionListener(listener); |
2 |
26 Feb 07 |
jari |
297 |
return button; |
2 |
26 Feb 07 |
jari |
298 |
} |
2 |
26 Feb 07 |
jari |
299 |
|
2 |
26 Feb 07 |
jari |
300 |
/** |
2 |
26 Feb 07 |
jari |
* Updates the dialog buttons states. |
2 |
26 Feb 07 |
jari |
302 |
*/ |
2 |
26 Feb 07 |
jari |
303 |
private void updateButtons() { |
2 |
26 Feb 07 |
jari |
304 |
if (availModel.isEmpty()) { |
2 |
26 Feb 07 |
jari |
305 |
addButton.setEnabled(false); |
2 |
26 Feb 07 |
jari |
306 |
addAllButton.setEnabled(false); |
2 |
26 Feb 07 |
jari |
307 |
} else { |
2 |
26 Feb 07 |
jari |
308 |
addAllButton.setEnabled(true); |
2 |
26 Feb 07 |
jari |
309 |
if (availList.isSelectionEmpty()) { |
2 |
26 Feb 07 |
jari |
310 |
addButton.setEnabled(false); |
2 |
26 Feb 07 |
jari |
311 |
} else { |
2 |
26 Feb 07 |
jari |
312 |
addButton.setEnabled(true); |
2 |
26 Feb 07 |
jari |
313 |
} |
2 |
26 Feb 07 |
jari |
314 |
} |
2 |
26 Feb 07 |
jari |
315 |
if (choosedModel.isEmpty()) { |
2 |
26 Feb 07 |
jari |
316 |
delButton.setEnabled(false); |
2 |
26 Feb 07 |
jari |
317 |
delAllButton.setEnabled(false); |
2 |
26 Feb 07 |
jari |
318 |
} else { |
2 |
26 Feb 07 |
jari |
319 |
delAllButton.setEnabled(true); |
2 |
26 Feb 07 |
jari |
320 |
if (choosedList.isSelectionEmpty()) { |
2 |
26 Feb 07 |
jari |
321 |
delButton.setEnabled(false); |
2 |
26 Feb 07 |
jari |
322 |
} else { |
2 |
26 Feb 07 |
jari |
323 |
delButton.setEnabled(true); |
2 |
26 Feb 07 |
jari |
324 |
} |
2 |
26 Feb 07 |
jari |
325 |
} |
2 |
26 Feb 07 |
jari |
326 |
} |
2 |
26 Feb 07 |
jari |
327 |
|
2 |
26 Feb 07 |
jari |
328 |
/** |
2 |
26 Feb 07 |
jari |
* The class to listen to mouse, action and list selection events. |
2 |
26 Feb 07 |
jari |
330 |
*/ |
2 |
26 Feb 07 |
jari |
331 |
private class Listener extends MouseAdapter implements ActionListener, ListSelectionListener { |
2 |
26 Feb 07 |
jari |
332 |
|
2 |
26 Feb 07 |
jari |
333 |
public void mouseClicked(MouseEvent e) { |
2 |
26 Feb 07 |
jari |
334 |
if (e.getClickCount() < 2) |
2 |
26 Feb 07 |
jari |
335 |
return; |
2 |
26 Feb 07 |
jari |
336 |
Object source = e.getSource(); |
2 |
26 Feb 07 |
jari |
337 |
if (source == availList) { |
2 |
26 Feb 07 |
jari |
338 |
addAction(); |
2 |
26 Feb 07 |
jari |
339 |
} else if (source == choosedList) { |
2 |
26 Feb 07 |
jari |
340 |
delAction(); |
2 |
26 Feb 07 |
jari |
341 |
} |
2 |
26 Feb 07 |
jari |
342 |
updateButtons(); |
2 |
26 Feb 07 |
jari |
343 |
} |
2 |
26 Feb 07 |
jari |
344 |
|
2 |
26 Feb 07 |
jari |
345 |
public void actionPerformed(ActionEvent e) { |
2 |
26 Feb 07 |
jari |
346 |
String command = e.getActionCommand(); |
2 |
26 Feb 07 |
jari |
347 |
if (command.equals(FILE_ADD_CMD)) { |
2 |
26 Feb 07 |
jari |
348 |
addAction(); |
2 |
26 Feb 07 |
jari |
349 |
} else if (command.equals(FILE_DEL_CMD)) { |
2 |
26 Feb 07 |
jari |
350 |
delAction(); |
2 |
26 Feb 07 |
jari |
351 |
} else if (command.equals(FILE_ALL_ADD_CMD)) { |
2 |
26 Feb 07 |
jari |
352 |
addAllAction(); |
2 |
26 Feb 07 |
jari |
353 |
} else if (command.equals(FILE_ALL_DEL_CMD)) { |
2 |
26 Feb 07 |
jari |
354 |
delAllAction(); |
2 |
26 Feb 07 |
jari |
355 |
} |
2 |
26 Feb 07 |
jari |
356 |
updateButtons(); |
2 |
26 Feb 07 |
jari |
357 |
} |
2 |
26 Feb 07 |
jari |
358 |
|
2 |
26 Feb 07 |
jari |
359 |
public void valueChanged(ListSelectionEvent e) { |
2 |
26 Feb 07 |
jari |
360 |
updateButtons(); |
2 |
26 Feb 07 |
jari |
361 |
} |
2 |
26 Feb 07 |
jari |
362 |
} |
2 |
26 Feb 07 |
jari |
363 |
|
2 |
26 Feb 07 |
jari |
364 |
/** |
2 |
26 Feb 07 |
jari |
* Class to render a list of files. |
2 |
26 Feb 07 |
jari |
366 |
*/ |
2 |
26 Feb 07 |
jari |
367 |
private class ListRenderer extends DefaultListCellRenderer { |
2 |
26 Feb 07 |
jari |
368 |
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
2 |
26 Feb 07 |
jari |
369 |
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
2 |
26 Feb 07 |
jari |
370 |
File file = (File)value; |
2 |
26 Feb 07 |
jari |
371 |
setText(file.getName()); |
2 |
26 Feb 07 |
jari |
372 |
return this; |
2 |
26 Feb 07 |
jari |
373 |
} |
2 |
26 Feb 07 |
jari |
374 |
} |
2 |
26 Feb 07 |
jari |
375 |
|
2 |
26 Feb 07 |
jari |
376 |
/** |
2 |
26 Feb 07 |
jari |
* The class to display and choose current directory. |
2 |
26 Feb 07 |
jari |
378 |
*/ |
2 |
26 Feb 07 |
jari |
379 |
private class DirectoryPanel extends JPanel implements ActionListener { |
2 |
26 Feb 07 |
jari |
380 |
|
2 |
26 Feb 07 |
jari |
381 |
private JTextField textField; |
2 |
26 Feb 07 |
jari |
382 |
|
2 |
26 Feb 07 |
jari |
383 |
/** |
2 |
26 Feb 07 |
jari |
* Constructs a <code>DirectoryPanel</code> with specified current |
2 |
26 Feb 07 |
jari |
* directory. |
2 |
26 Feb 07 |
jari |
386 |
*/ |
2 |
26 Feb 07 |
jari |
387 |
public DirectoryPanel(String currentDirectory) { |
2 |
26 Feb 07 |
jari |
388 |
setLayout(new GridBagLayout()); |
2 |
26 Feb 07 |
jari |
389 |
|
2 |
26 Feb 07 |
jari |
390 |
GridBagConstraints gbc = new GridBagConstraints(); |
2 |
26 Feb 07 |
jari |
391 |
gbc.insets = new Insets(5, 5, 5, 5); |
2 |
26 Feb 07 |
jari |
392 |
|
2 |
26 Feb 07 |
jari |
393 |
gbc.gridx = 0; |
2 |
26 Feb 07 |
jari |
394 |
gbc.gridy = 0; |
2 |
26 Feb 07 |
jari |
395 |
gbc.anchor = GridBagConstraints.WEST; |
2 |
26 Feb 07 |
jari |
396 |
|
2 |
26 Feb 07 |
jari |
397 |
add(new JLabel("Directory:"), gbc); |
2 |
26 Feb 07 |
jari |
398 |
|
2 |
26 Feb 07 |
jari |
399 |
gbc.gridx = 1; |
2 |
26 Feb 07 |
jari |
400 |
gbc.weightx = 1.0; |
2 |
26 Feb 07 |
jari |
401 |
gbc.fill = GridBagConstraints.HORIZONTAL; |
2 |
26 Feb 07 |
jari |
402 |
gbc.anchor = GridBagConstraints.CENTER; |
2 |
26 Feb 07 |
jari |
403 |
|
2 |
26 Feb 07 |
jari |
404 |
textField = new JTextField(currentDirectory, 30); |
2 |
26 Feb 07 |
jari |
405 |
textField.setEditable(false); |
2 |
26 Feb 07 |
jari |
406 |
add(textField, gbc); |
2 |
26 Feb 07 |
jari |
407 |
|
2 |
26 Feb 07 |
jari |
408 |
gbc.gridx = 2; |
2 |
26 Feb 07 |
jari |
409 |
gbc.weightx = 0.0; |
2 |
26 Feb 07 |
jari |
410 |
gbc.anchor = GridBagConstraints.EAST; |
2 |
26 Feb 07 |
jari |
411 |
gbc.fill = GridBagConstraints.NONE; |
2 |
26 Feb 07 |
jari |
412 |
|
2 |
26 Feb 07 |
jari |
413 |
JButton chooseBtn = new JButton("Choose..."); |
2 |
26 Feb 07 |
jari |
414 |
chooseBtn.setActionCommand(FILE_CHOOSE); |
2 |
26 Feb 07 |
jari |
415 |
chooseBtn.addActionListener(this); |
2 |
26 Feb 07 |
jari |
416 |
add(chooseBtn, gbc); |
2 |
26 Feb 07 |
jari |
417 |
} |
2 |
26 Feb 07 |
jari |
418 |
|
2 |
26 Feb 07 |
jari |
419 |
public void actionPerformed(ActionEvent e) { |
2 |
26 Feb 07 |
jari |
420 |
String command = e.getActionCommand(); |
2 |
26 Feb 07 |
jari |
421 |
if (command.equals(FILE_CHOOSE)) { |
2 |
26 Feb 07 |
jari |
422 |
chooseDirectory(); |
2 |
26 Feb 07 |
jari |
423 |
} |
2 |
26 Feb 07 |
jari |
424 |
} |
2 |
26 Feb 07 |
jari |
425 |
|
2 |
26 Feb 07 |
jari |
426 |
/** |
2 |
26 Feb 07 |
jari |
* Lets the user choose a directory. |
2 |
26 Feb 07 |
jari |
428 |
*/ |
2 |
26 Feb 07 |
jari |
429 |
private void chooseDirectory() { |
2 |
26 Feb 07 |
jari |
430 |
JFileChooser filechooser = new JFileChooser(); |
2 |
26 Feb 07 |
jari |
431 |
filechooser.setCurrentDirectory(new File(textField.getText())); |
2 |
26 Feb 07 |
jari |
432 |
filechooser.setDialogTitle("Choose a directory"); |
2 |
26 Feb 07 |
jari |
433 |
filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); |
2 |
26 Feb 07 |
jari |
434 |
|
2 |
26 Feb 07 |
jari |
435 |
int ret = filechooser.showOpenDialog(this); |
2 |
26 Feb 07 |
jari |
436 |
if (ret == JFileChooser.APPROVE_OPTION) { |
2 |
26 Feb 07 |
jari |
437 |
File dir = filechooser.getSelectedFile(); |
2 |
26 Feb 07 |
jari |
438 |
textField.setText(dir.getPath()); |
2 |
26 Feb 07 |
jari |
439 |
updateDirectory(dir); |
2 |
26 Feb 07 |
jari |
440 |
} |
2 |
26 Feb 07 |
jari |
441 |
} |
2 |
26 Feb 07 |
jari |
442 |
} |
2 |
26 Feb 07 |
jari |
443 |
} |