-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJTextList.java
332 lines (306 loc) · 11.6 KB
/
JTextList.java
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
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
/**
* Created by abca2 on 9/9/2015.
*/
class JTextList extends JTextPane
{
private StyledDocument doc;
private ArrayList<JCheckBox> check;
private ArrayList<JTextPane> nameList;
private ArrayList<JTextPane> ext;
private Listener listener;
private DocumentFilter documentFilter;
private ArrayList<ArrayList<String>> version;
private ActionListener actionListener;
private Font font;
public JTextList(ActionListener actionListener)
{
font = new Font(Font.SANS_SERIF, 0, 15);
setEditable(false);
this.actionListener = actionListener;
doc = this.getStyledDocument();
check = new ArrayList<>();
nameList = new ArrayList<>();
ext = new ArrayList<>();
version = new ArrayList<>();
listener = new Listener();
documentFilter = new CustomFilter();
}
public int length()
{
return nameList.size();
}
public void replace(int index, String string, int startPos, int endPos)
{
requestFocus();
if (Math.abs(startPos - endPos) < 2)
return;
nameList.get(index).setText(new StringBuilder(nameList.get(index).getText()).replace(startPos, endPos, string).toString());
}
public JCheckBox getCheckBox(int index)
{
try
{
return check.get(index);
}
catch (Exception e)
{
return null;
}
}
@Override
public int getSelectionStart()
{
for (JTextPane a : nameList)
{
if (a.getCaret().isSelectionVisible())
return a.getSelectionStart();
}
return -1;
}
@Override
public int getSelectionEnd()
{
for (JTextPane a : nameList)
{
if (a.getCaret().isSelectionVisible())
return a.getSelectionEnd();
}
return -1;
}
@Override
public String getSelectedText()
{
for (JTextPane a : nameList)
{
if (a.getCaret().isSelectionVisible())
{
return a.getSelectedText();
}
}
return null;
}
public ArrayList<String> getList()
{
ArrayList<String> modifiedList = new ArrayList<>();
for (int i = 0; i < nameList.size(); i++)
modifiedList.add(nameList.get(i).getText() + ext.get(i).getText());
return modifiedList;
}
public void refreshList(ArrayList<String> newList) throws BadLocationException
{
setFont(font);
//Clear lists if smaller size==============================================
doc.remove(0, doc.getLength());
if (newList.size() < nameList.size())
{
if (check.size() > 0)
check.get(0).removeActionListener(actionListener);
for (JTextPane a : nameList)
{
a.removeFocusListener(listener);
a.getActionMap().clear();
((AbstractDocument) a.getDocument()).setDocumentFilter(null);
}
for (JTextPane a : ext)
a.removeFocusListener(listener);
check.clear();
nameList.clear();
ext.clear();
}
//Add check boxes and file names=============================================
for (int i = 0; i < newList.size() + 1; i++)
{
//Check Boxes============================================================
if (i > check.size() - 1)
{
if (i == 0)
{
check.add(new JCheckBox("checkAll"));
check.get(i).addActionListener(actionListener);
}
else
check.add(new JCheckBox());
check.get(i).setSelected(true);
check.get(i).setAlignmentY(0.78f);
check.get(i).setBackground(null);
check.get(i).setFocusable(false);
}
insertComponent(check.get(i));
//TextAreas==============================================================
if (i != 0)
{
//File name
if (i > nameList.size())
{
JTextPane tempName = new JTextPane();
nameList.add(tempName);
version.add(new ArrayList<>());
tempName.setCaret(new CustomCaret());
tempName.addFocusListener(listener);
tempName.setAlignmentY(0.83f);
tempName.setEditable(false);
tempName.getDropTarget().setActive(false);
tempName.getDocument().putProperty("filterNewlines", true);
tempName.setBorder(BorderFactory.createLineBorder(Color.BLACK));
tempName.setBorder(null);
tempName.setFont(font);
}
insertComponent(nameList.get(i - 1));
//Extension
if (ext.size() != newList.size())
{
JTextPane tempExt = new JTextPane();
ext.add(tempExt);
tempExt.addFocusListener(listener);
tempExt.setAlignmentY(0.83f);
tempExt.setEditable(false);
tempExt.getDropTarget().setActive(false);
tempExt.getDocument().putProperty("filterNewlines", true);
tempExt.setBorder(BorderFactory.createLineBorder(Color.BLACK));
tempExt.setBorder(null);
tempExt.setMaximumSize(new Dimension(10,20));
SimpleAttributeSet set = new SimpleAttributeSet();
StyleConstants.setAlignment(set, StyleConstants.ALIGN_RIGHT);
tempExt.getStyledDocument().setParagraphAttributes(0, doc.getLength(), set, false);
tempExt.setFont(font);
}
insertComponent(ext.get(i - 1));
String temp = newList.get(i - 1);
int index = temp.lastIndexOf(".");
nameList.get(i - 1).setText((index >= 0) ? temp.substring(0, index) : temp);
ext.get(i - 1).setText((index >= 0) ? temp.substring(index, temp.length()) : "");
version.get(i - 1).clear();
}
doc.insertString(doc.getLength(), "\n", null);
}
setCaretPosition(0);
}
public void setTextAlignment(int alignment)
{
SimpleAttributeSet set = new SimpleAttributeSet();
StyleConstants.setAlignment(set, alignment);
doc.setParagraphAttributes(0, doc.getLength(), set, false);
}
class CustomFilter extends DocumentFilter
{
@Override
public void remove(FilterBypass fb, int offset, int length) throws BadLocationException
{
String temp;
for (int i = 0; i < nameList.size(); i++)
{
if (!check.get(i + 1).isSelected())
continue;
JTextPane txt = nameList.get(i);
version.get(i).add(txt.getText());
if (txt.getDocument().equals(fb.getDocument()))
continue;
temp = new StringBuilder(txt.getText()).replace(offset, offset + length, "").toString();
txt.setText(temp);
}
super.remove(fb, offset, length);
}
@Override
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException
{
String temp;
for (int i = 0; i < nameList.size(); i++)
{
if (!check.get(i + 1).isSelected())
continue;
JTextPane txt = nameList.get(i);
version.get(i).add(txt.getText());
if (txt.getDocument().equals(fb.getDocument()))
continue;
temp = new StringBuilder(txt.getText()).insert(offset, string).toString();
txt.setText(temp);
}
System.out.println(fb);
super.insertString(fb, offset, string, attr);
}
@Override
public void replace(FilterBypass fb, int offset, int length, String with, AttributeSet attrs) throws BadLocationException
{
String temp;
for (int i = 0; i < nameList.size(); i++)
{
if (!check.get(i + 1).isSelected())
continue;
JTextPane txt = nameList.get(i);
version.get(i).add(txt.getText());
if (txt.getDocument().equals(fb.getDocument()))
continue;
temp = new StringBuilder(txt.getText()).replace(offset, offset + length, with).toString();
txt.setText(temp);
}
super.replace(fb, offset, length, with, attrs);
}
}
class Listener implements FocusListener
{
@Override
public void focusGained(FocusEvent e)
{
JTextPane temp = (JTextPane) e.getComponent();
int index = nameList.indexOf(temp) + 1;
temp.setBorder(BorderFactory.createLineBorder(Color.BLACK));
temp.setEditable(true);
temp.getCaret().setVisible(true);
if (!check.get(0).isSelected() && !check.get(index).isSelected())
{
for (int i = 0; i < check.size(); i++)
{
if (index == i)
continue;
check.get(i).setSelected(false);
}
}
check.get(index).setSelected(true);
if (ext.indexOf(e.getComponent()) > -1)
return;
temp.getActionMap().put("undo", new AbstractAction()
{
@Override
public void actionPerformed(ActionEvent e)
{
((AbstractDocument) temp.getDocument()).setDocumentFilter(null);
for (int i = 0; i < nameList.size(); i++)
{
if (version.get(i).size() > 0)
{
JTextPane tempText = nameList.get(i);
ArrayList<String> tempVersion = version.get(i);
int caretPos = tempText.getCaretPosition() - (tempText.getText().length() - tempVersion.get(tempVersion.size() - 1).length());
tempText.setText(tempVersion.remove(tempVersion.size() - 1));
if (tempText.hasFocus())
tempText.setCaretPosition(caretPos);
}
}
((AbstractDocument) temp.getDocument()).setDocumentFilter(documentFilter);
}
});
temp.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "undo");
((AbstractDocument) temp.getDocument()).setDocumentFilter(documentFilter);
}
@Override
public void focusLost(FocusEvent e)
{
JTextPane temp = (JTextPane) e.getComponent();
if (ext.indexOf(temp) < 0)
((CustomCaret) temp.getCaret()).deselect();
temp.setSelectionEnd(0);
temp.setBorder(null);
temp.setEditable(false);
temp.getActionMap().clear();
((AbstractDocument) temp.getDocument()).setDocumentFilter(null);
}
}
}