-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkListEventHandler.java
More file actions
308 lines (261 loc) · 9.53 KB
/
WorkListEventHandler.java
File metadata and controls
308 lines (261 loc) · 9.53 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
package com.enventek.plant.worklist.newlist;
import com.enventek.plant.datasource.DataSource;
import com.enventek.plant.datasource.FileObject;
import com.enventek.plant.hibernate.objects.Board;
import com.enventek.plant.hibernate.objects.Cutting;
import com.enventek.plant.hibernate.objects.Truss;
import com.enventek.plant.utils.Globals;
import com.enventek.plant.utils.Item;
import com.enventek.plant.utils.savedState.FileMetadataManager;
import com.enventek.plant.utils.savedState.SavedStateUtils;
import com.enventek.plant.worklist.WorkOrderUtils;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.awt.event.*;
public class WorkListEventHandler {
static final Logger logger = LogManager.getLogger(WorkListEventHandler.class);
private final WorkListUI ui;
private final WorkListController ctl;
private final WorkListState state;
private final WorkListBackupManager wlBackupManager;
private WorkOrderUtils workOrderUtils;
private WorkListUtilities utils;
public WorkListEventHandler(WorkListUI ui, WorkListController controller, WorkListState state,
WorkListBackupManager backupManager, WorkListUtilities utils) {
this.ui = ui;
this.ctl = controller;
this.state = state;
this.wlBackupManager = backupManager;
this.utils = utils;
}
// Setter for workOrderUtils since it might be initialized later
public void setWorkOrderUtils(WorkOrderUtils workOrderUtils) {
this.workOrderUtils = workOrderUtils;
}
public void registerListeners() {
// List selection listener (main brain) - MOVED HERE
ui.itemList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
handleListSelection(e);
}
});
// Keyboard (simple mode)
ui.itemList.addKeyListener(new KeyAdapter() {
private int selectedIndex = 0; // local index for simple mode
@Override
public void keyReleased(KeyEvent e) {
if (!state.isSimple())
return;
if (e.isControlDown() || e.isAltDown() || e.isShiftDown())
return;
int size = ui.itemList.getModel().getSize();
if (size <= 0)
return;
switch (e.getKeyCode()) {
case KeyEvent.VK_S, KeyEvent.VK_DOWN -> {
selectedIndex = ui.itemList.getSelectedIndex() >= 0 ? ui.itemList.getSelectedIndex()
: selectedIndex;
selectedIndex = (selectedIndex >= size - 1) ? 0 : selectedIndex + 1;
ui.itemList.ensureIndexIsVisible(selectedIndex);
ui.itemList.repaint();
}
case KeyEvent.VK_W, KeyEvent.VK_UP -> {
selectedIndex = ui.itemList.getSelectedIndex() >= 0 ? ui.itemList.getSelectedIndex()
: selectedIndex;
selectedIndex = (selectedIndex <= 0) ? size - 1 : selectedIndex - 1;
ui.itemList.ensureIndexIsVisible(selectedIndex);
ui.itemList.repaint();
}
case KeyEvent.VK_D, KeyEvent.VK_ENTER, KeyEvent.VK_RIGHT -> {
ui.itemList.setSelectedIndex(selectedIndex);
selectedIndex = 0;
}
case KeyEvent.VK_A, KeyEvent.VK_LEFT -> {
if (ui.backButton.getText().contains("Search") || e.getKeyCode() == KeyEvent.VK_LEFT) {
ctl.refresh();
selectedIndex = 0;
} else {
selectedIndex = 0;
String currentName = ui.backButton.getText();
DataSource dataSource = ctl.getDataSource();
if (dataSource != null) {
DefaultListModel model = dataSource.getModel(null);
ui.itemList.setModel(model);
ui.backButton.setText("Search");
ui.backButton.setIcon(ui.searchIcon);
for (int i = 0; i < model.size(); i++) {
Object m = model.get(i);
if (String.valueOf(m).equalsIgnoreCase(currentName)) {
selectedIndex = i;
break;
}
}
}
}
}
}
}
});
// Mouse drag (swipe)
ui.itemList.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent evt) {
if (state.isProcessInProgress()) {
evt.consume();
return;
}
if (evt == null || state.dragStart == null)
return;
if (!ui.itemList.contains(evt.getPoint()))
ui.itemList.clearSelection();
if (state.valueChangedFlag) {
state.dragStart = evt.getPoint();
state.valueChangedFlag = false;
ui.itemList.repaint();
}
int index = ui.itemList.getSelectedIndex();
if (index == ui.itemList.getSelectedIndex() && evt.getX() > state.dragStart.getX()
&& evt.getX() > (ui.itemList.getWidth() - (ui.itemList.getWidth() / 3))) {
state.deleteCandidate = true;
if (state.isSwipeEnabled() && ctl.getDataSource() != null
&& ui.itemList.getSelectedValue() != null) {
ctl.handleSwipePopup();
}
state.deleteCandidate = false;
} else {
state.deleteCandidate = false;
ui.itemList.repaint();
}
}
});
ui.itemList.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent evt) {
if (state.isProcessInProgress()) {
evt.consume();
return;
}
if (state.dragStart == null) {
state.dragStart = evt.getPoint();
state.deleteCandidate = false;
ui.itemList.repaint();
}
}
@Override
public void mouseReleased(MouseEvent evt) {
state.dragStart = null;
if (state.deleteCandidate)
evt.consume();
ui.itemList.repaint();
}
});
}
// Extracted method - NOW IN EVENT HANDLER
private void handleListSelection(ListSelectionEvent e) {
if (!isWorkOrderStreamActive() && ctl.getDelayTimer().isRunning()) {
return;
}
if (!e.getValueIsAdjusting()) {
ui.itemList.setEnabled(false);
ctl.getDelayTimer().restart();
}
if (!state.itemListEventHandlingEnabled) {
return;
}
state.valueChangedFlag = true;
if (e.getValueIsAdjusting() && !state.isIgnoreAdjusting()) {
return;
}
Object selectedValue = ui.itemList.getSelectedValue();
if (selectedValue == null) {
return;
}
processSelection(selectedValue);
}
void processSelection(Object selectedValue) {
ctl.notifySelectionListeners(selectedValue);
String name = selectedValue.toString();
Object unwrapped = (selectedValue instanceof Item i) ? i.getValue() : selectedValue;
// Work order stream rendering
if (isWorkOrderStreamActive()) {
workOrderUtils.workStreamProcess(selectedValue);
if (unwrapped instanceof Board b) {
String jname = b.getJobName();
name = "<html>" + b.getCutting() + "<br>#" + jname + " - " + ctl.getDataSource().getName();
} else if (unwrapped instanceof Truss t) {
String jname = t.getJobName();
name = "<html>" + t + "<br>#" + jname + " - " + ctl.getDataSource().getName();
}
workOrderUtils.setSelectedByWO(false);
ui.backButton.setText(name);
ui.backButton.setEnabled(false);
}
// Save selection index unless navigating into FileObject or Cutting
if (!(unwrapped instanceof FileObject) && !(unwrapped instanceof Cutting)) {
int idx = ui.itemList.getSelectedIndex();
if (idx != -1 && SavedStateUtils.isInitialized()) {
SavedStateUtils.setItemIndex(idx);
}
}
//web update on selection
utils.sendItemUpdate();
// Traverse into sublist
DefaultListModel<Item> model = ctl.getDataSource().getModel(selectedValue);
if (model == null) {
ctl.finalizeBackHeader(name);
return;
}
// Pre-select cutting if it's the only element
if (model.getSize() == 1 && model.getElementAt(0).getValue() instanceof Cutting) {
SwingUtilities.invokeLater(() -> ui.itemList.setSelectedIndex(0));
}
ctl.setUnfilteredList(null); // clear filter
// Push into new list view
ctl.enterFolder(model);
// Restore selection
if (model.getSize() > 0) {
SwingUtilities.invokeLater(() -> ui.itemList.setSelectedIndex(0));
}
// Update file hash
if (unwrapped instanceof FileObject fo) {
Object val = ctl.getDataSource().getDataFromSelection(fo);
if (val instanceof DefaultListModel) {
ctl.enterFolder((DefaultListModel<Item>) val);
}
if (SavedStateUtils.isInitialized()) {
String hash = FileMetadataManager.getFileHash(fo);
logger.debug("Selected file hash: {}", hash);
SavedStateUtils.setCurrentFileHash(hash, fo.getName());
}
} else if (unwrapped instanceof Cutting ctt) {
String hash = FileMetadataManager.getFileHash(ctt.getFile());
logger.debug("Selected file hash: {}", hash);
String jname = (ctt.getBoards() != null && ctt.getBoards().length > 0)
? ctt.getBoards()[0].getCutting().getFileName()
: "";
if (SavedStateUtils.isInitialized()) {
SavedStateUtils.setCurrentFileHash(hash, jname);
}
name = "<html>" + name + "<br>#" + jname + " - " + ctl.getDataSource().getName();
} else {
name = "<html>" + ctl.getDataSource().getName() + "<br>" + name;
}
// DO THIS TO RESTORE THE SAVED STATE DONE ITEMS WHEN USER CLICKS ON A
// JOB
if (!state.isBackupSelection() && SavedStateUtils.isInitialized() && FileMetadataManager.isInitialized()) {
if (!(isWorkOrderStreamActive() && workOrderUtils.hasWorkStreamSaved())) {
long t0 = System.currentTimeMillis();
wlBackupManager.restoreCompletedItemsOnlyAsync();
logger.info("loadWorkListSaved finished in {} ms", System.currentTimeMillis() - t0);
}
}
ctl.finalizeBackHeader(name);
}
private boolean isWorkOrderStreamActive() {
return workOrderUtils != null && workOrderUtils.isActiveWorkStream();
}
}