17
17
import java .awt .event .WindowAdapter ;
18
18
import java .awt .event .WindowEvent ;
19
19
import java .util .ArrayList ;
20
+ import java .util .function .Function ;
20
21
21
22
/**
22
23
* @author christianfranzke
@@ -26,11 +27,16 @@ public class EditHistoryDialog extends JDialog {
26
27
private static final String CONFIG_Y = "edit_history.y" ;
27
28
private static final String CONFIG_HEIGHT = "edit_history.height" ;
28
29
private static final String CONFIG_WIDTH = "edit_history.width" ;
30
+ private final EventList <String > eventList ;
31
+ private final Function <Integer ,Integer > inc_op = f -> f + 1 ;
32
+ private final Function <Integer ,Integer > dec_op = f -> f - 1 ;
29
33
30
34
public EditHistoryDialog (Window owner , JMenuItem menuItem , EventList <String > eventList ) {
31
35
super (owner );
32
36
initComponents ();
33
37
38
+ this .eventList = eventList ;
39
+
34
40
menuItem .setEnabled (false );
35
41
addWindowListener (new WindowAdapter () {
36
42
@ Override
@@ -45,14 +51,15 @@ public void windowClosed(WindowEvent e) {
45
51
list .getSelectionModel ().addListSelectionListener (l -> {
46
52
if (l .getValueIsAdjusting ())
47
53
return ;
48
- adjustDeleteButton ();
54
+ adjustButtons ();
49
55
});
50
- adjustDeleteButton ();
56
+ adjustButtons ();
51
57
52
58
btnDeleteEntries .addActionListener (l -> {
53
59
var changeList = new ArrayList <String >();
60
+ var listModel = list .getModel ();
54
61
for (var idx : list .getSelectedIndices ()) {
55
- changeList .add (list . getModel () .getElementAt (idx ));
62
+ changeList .add (listModel .getElementAt (idx ));
56
63
}
57
64
58
65
var lock = eventList .getReadWriteLock ().writeLock ();
@@ -66,9 +73,37 @@ public void windowClosed(WindowEvent e) {
66
73
changeList .clear ();
67
74
});
68
75
76
+ btnUp .addActionListener (l -> {
77
+ var idx = list .getSelectedIndex ();
78
+ idx = moveEntry (idx , dec_op );
79
+ list .setSelectedIndex (idx );
80
+ });
81
+
82
+ btnDown .addActionListener (l -> {
83
+ var idx = list .getSelectedIndex ();
84
+ idx = moveEntry (idx , inc_op );
85
+ list .setSelectedIndex (idx );
86
+ });
87
+
69
88
restorePosition ();
70
89
}
71
90
91
+ private int moveEntry (int idx , Function <Integer ,Integer > operator ) {
92
+ var lock = eventList .getReadWriteLock ().writeLock ();
93
+ lock .lock ();
94
+ try {
95
+ var obj = eventList .get (idx );
96
+ eventList .remove (idx );
97
+ idx = operator .apply (idx );
98
+ eventList .add (idx , obj );
99
+ }
100
+ finally {
101
+ lock .unlock ();
102
+ }
103
+
104
+ return idx ;
105
+ }
106
+
72
107
private void restorePosition () {
73
108
var config = ApplicationConfiguration .getConfiguration ();
74
109
try {
@@ -102,8 +137,20 @@ private void savePosition() {
102
137
}
103
138
}
104
139
105
- private void adjustDeleteButton () {
106
- btnDeleteEntries .setEnabled (list .getSelectionModel ().getSelectedItemsCount () > 0 );
140
+ private void adjustButtons () {
141
+ final var itemCount = list .getSelectionModel ().getSelectedItemsCount ();
142
+ final var singleSelection = itemCount == 1 ;
143
+ btnDeleteEntries .setEnabled (itemCount > 0 );
144
+ btnUp .setEnabled (singleSelection );
145
+ btnDown .setEnabled (singleSelection );
146
+ if (singleSelection ) {
147
+ //check if entry is either first or last entry
148
+ var idx = list .getSelectionModel ().getLeadSelectionIndex ();
149
+ if (idx == 0 ) //first
150
+ btnUp .setEnabled (false );
151
+ if (idx == list .getModel ().getSize () - 1 ) //last
152
+ btnDown .setEnabled (false );
153
+ }
107
154
}
108
155
109
156
private void initComponents () {
@@ -116,6 +163,10 @@ private void initComponents() {
116
163
var toolBar1 = new JToolBar ();
117
164
btnDeleteEntries = new JButton ();
118
165
btnDeleteEntries .setIcon (SVGIconUtilities .createSVGIcon ("icons/fontawesome/trash-can.svg" )); //NON-NLS
166
+ btnUp = new JButton ();
167
+ btnUp .setIcon (SVGIconUtilities .createSVGIcon ("icons/fontawesome/arrow-up.svg" )); //NON-NLS
168
+ btnDown = new JButton ();
169
+ btnDown .setIcon (SVGIconUtilities .createSVGIcon ("icons/fontawesome/arrow-down.svg" )); //NON-NLS
119
170
120
171
//======== this ========
121
172
setTitle ("Suchhistorie bearbeiten" ); //NON-NLS
@@ -146,6 +197,14 @@ private void initComponents() {
146
197
//---- btnDeleteEntries ----
147
198
btnDeleteEntries .setToolTipText ("Ausgew\u00e4 hlte Eintr\u00e4 ge l\u00f6 schen" ); //NON-NLS
148
199
toolBar1 .add (btnDeleteEntries );
200
+
201
+ //---- btnUp ----
202
+ btnUp .setToolTipText ("Element nach oben verschieben" ); //NON-NLS
203
+ toolBar1 .add (btnUp );
204
+
205
+ //---- btnDown ----
206
+ btnDown .setToolTipText ("Element nach unten verschieben" ); //NON-NLS
207
+ toolBar1 .add (btnDown );
149
208
}
150
209
contentPanel .add (toolBar1 , BorderLayout .NORTH );
151
210
}
@@ -161,5 +220,7 @@ private void initComponents() {
161
220
// Generated using JFormDesigner non-commercial license
162
221
private JList <String > list ;
163
222
private JButton btnDeleteEntries ;
223
+ private JButton btnUp ;
224
+ private JButton btnDown ;
164
225
// JFormDesigner - End of variables declaration //GEN-END:variables @formatter:on
165
226
}
0 commit comments