DictSearchPathsDialog.java
8.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package morfeusz;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.*;
@SuppressWarnings("serial")
public class DictSearchPathsDialog extends JPanel
{
private JDialog dialog;
private PathList pathList;
private JButton acceptButton;
private JButton cancelButton;
private JButton addButton;
private JButton removeButton;
private JButton defaultsButton;
private List<String> searchPaths;
private List<String> defaultSearchPaths;
private int ret;
public DictSearchPathsDialog(List<String> searchPaths, List<String> defaultSearchPaths)
{
super(new BorderLayout());
setBorder(BorderFactory.createEmptyBorder(16, 16, 16, 16));
setPreferredSize(new Dimension(600, 200));
this.searchPaths = searchPaths;
this.defaultSearchPaths = defaultSearchPaths;
arrangeComponents();
}
public void arrangeComponents()
{
JLabel label = new JLabel("\u015Acie\u017Cka wyszukiwania s\u0142ownik\u00F3w:");
Box hbox = Box.createHorizontalBox();
JScrollPane sp;
Dimension dim;
label.setBorder(BorderFactory.createEmptyBorder(0, 0, 2, 0));
add(label, BorderLayout.NORTH);
pathList = new PathList();
sp = new JScrollPane(pathList);
sp.setBorder(BorderFactory.createEtchedBorder());
sp.getViewport().setBackground(Color.white);
add(sp, BorderLayout.CENTER);
addButton = new JButton("Dodaj");
removeButton = new JButton("Usu\u0144");
defaultsButton = new JButton("Domy\u015Blna");
cancelButton = new JButton("Anuluj");
acceptButton = new JButton("Akceptuj");
dim = defaultsButton.getPreferredSize();
addButton.setPreferredSize(dim);
removeButton.setPreferredSize(dim);
cancelButton.setPreferredSize(dim);
acceptButton.setPreferredSize(dim);
hbox.setBorder(BorderFactory.createEmptyBorder(16, 0, 0, 0));
hbox.add(addButton);
hbox.add(Box.createHorizontalStrut(8));
hbox.add(removeButton);
hbox.add(Box.createHorizontalStrut(8));
hbox.add(Box.createHorizontalGlue());
hbox.add(defaultsButton);
hbox.add(Box.createHorizontalGlue());
hbox.add(Box.createHorizontalStrut(8));
hbox.add(cancelButton);
hbox.add(Box.createHorizontalStrut(8));
hbox.add(acceptButton);
add(hbox, BorderLayout.SOUTH);
removeButton.setEnabled(false);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event)
{
addPath();
}
});
removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event)
{
removePath();
}
});
defaultsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event)
{
defaults();
}
});
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event)
{
dialog.setVisible(false);
}
});
acceptButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event)
{
modifySearchPaths();
ret = 1;
dialog.setVisible(false);
}
});
}
public void addPath()
{
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Select Files");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setMultiSelectionEnabled(true);
int state = chooser.showOpenDialog(this);
if (state == JFileChooser.APPROVE_OPTION) {
File[] files = chooser.getSelectedFiles();
DefaultTableModel model = (DefaultTableModel)pathList.getModel();
Object[] row = new Object[1];
for (int i = 0; i < files.length; i++) {
row[0] = files[i].getPath();
model.addRow(row);
}
}
}
public void removePath()
{
int[] rows = pathList.getSelectedRows();
DefaultTableModel model = (DefaultTableModel)pathList.getModel();
for (int i = 0; i < rows.length; i++) {
model.removeRow(rows[i] - i);
}
}
public void defaults()
{
DefaultTableModel model = (DefaultTableModel)pathList.getModel();
int count = model.getRowCount();
for (int i = 0; i < count; i++) {
model.removeRow(0);
}
if (defaultSearchPaths != null && !defaultSearchPaths.isEmpty()) {
Object[] row = new Object[1];
for (String path : defaultSearchPaths) {
row[0] = path;
model.addRow(row);
}
}
}
public void modifySearchPaths()
{
DefaultTableModel model = (DefaultTableModel)pathList.getModel();
int count = model.getRowCount();
searchPaths.clear();
for (int i = 0; i < count; i++) {
String path = (String)model.getValueAt(i, 0);
searchPaths.add(path);
}
}
public int doDialog()
{
ret = 0;
dialog = new JDialog();
dialog.setModal(true);
dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
dialog.setTitle("Ustawianie \u015Bcie\u017Cki wyszukiwania s\u0142ownik\u00F3w");
dialog.setResizable(true);
dialog.getContentPane().add(this);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
return ret;
}
public void dispose()
{
if (dialog != null) {
dialog.dispose();
dialog = null;
}
}
private class PathList extends JTable
{
public PathList()
{
super(new PathListModel());
setBorder(BorderFactory.createEmptyBorder());
setBackground(Color.white);
setTableHeader(null);
setRowSelectionAllowed(true);
setDragEnabled(true);
setShowHorizontalLines(false);
setShowVerticalLines(false);
setTransferHandler(new PathListTransferHandler());
getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent event)
{
removeButton.setEnabled(getSelectedRowCount() > 0);
}
});
DefaultTableModel model = (DefaultTableModel)getModel();
Object[] row = new Object[1];
for (String path : searchPaths) {
row[0] = path;
model.addRow(row);
}
}
}
private class PathListModel extends DefaultTableModel
{
public PathListModel()
{
super(0, 1);
}
public Class<? extends Object> getColumnClass(int col)
{
Vector<?> v = (Vector<?>)getDataVector().elementAt(0);
return v.elementAt(col).getClass();
}
public boolean isCellEditable(int row, int column)
{
return false;
}
}
private class PathListTransferHandler extends TransferHandler
{
private boolean canImport;
private boolean accept;
private int[] rows;
private int addIndex;
private int addCount;
public PathListTransferHandler()
{
canImport = false;
accept = false;
rows = null;
addIndex = -1;
addCount = 0;
}
protected Transferable createTransferable(JComponent c)
{
accept = true;
return new StringSelection(exportString(c));
}
protected String exportString(JComponent c)
{
JTable table = (JTable)c;
StringBuffer buff = new StringBuffer();
rows = table.getSelectedRows();
for (int i = 0; i < rows.length; i++) {
String s = (String)table.getValueAt(rows[i], 0);
buff.append(s);
if (i != rows.length - 1) buff.append("\n");
}
return buff.toString();
}
protected void importString(JComponent c, String str)
{
JTable target = (JTable)c;
DefaultTableModel model = (DefaultTableModel)target.getModel();
int index = target.getSelectedRow();
if (rows != null && index >= rows[0] - 1 &&
index <= rows[rows.length - 1])
{
rows = null;
return;
}
int max = model.getRowCount();
if (index < 0) index = max;
else {
index++;
if (index > max) index = max;
}
addIndex = index;
String[] rvalues = str.split("\n");
addCount = rvalues.length;
for (int i = 0; i < rvalues.length; i++) {
Object[] row = new Object[1];
row[0] = rvalues[i];
model.insertRow(index++, row);
}
}
public int getSourceActions(JComponent c)
{
return COPY_OR_MOVE;
}
public boolean importData(JComponent c, Transferable t)
{
if (canImport(c, t.getTransferDataFlavors())) {
canImport = true;
try {
String str = (String)t.getTransferData(DataFlavor.stringFlavor);
importString(c, str);
return true;
}
catch (UnsupportedFlavorException ufe) {
}
catch (IOException ioe) {
}
}
return false;
}
protected void exportDone(JComponent c, Transferable data, int action)
{
if (canImport) {
JTable source = (JTable)c;
if (rows != null) {
DefaultTableModel model = (DefaultTableModel)source.getModel();
if (addCount > 0) {
for (int i = 0; i < rows.length; i++) {
if (rows[i] > addIndex) rows[i] += addCount;
}
}
for (int i = rows.length - 1; i >= 0; i--) model.removeRow(rows[i]);
}
rows = null;
addCount = 0;
addIndex = -1;
canImport = false;
}
accept = false;
}
public boolean canImport(JComponent c, DataFlavor[] flavors)
{
if (accept) {
for (int i = 0; i < flavors.length; i++) {
if (DataFlavor.stringFlavor.equals(flavors[i])) return true;
}
}
return false;
}
}
}