-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEDC16C34.java
421 lines (347 loc) · 13.8 KB
/
EDC16C34.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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package DTCController;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;
/**
* Class to edit the DTC table on EDC16C34 platform
*/
public class EDC16C34 extends JFrame implements ActionListener {
public static final Font bigFont = new Font("Dialog",Font.BOLD,18);
private JMenuBar menuBar;
private JMenu fileMenu;
private JMenuItem openMenuItem;
private JMenuItem saveMenuItem;
private JMenuItem exitMenuItem;
private JMenu findMenu;
private JMenuItem findMenuItem;
private JMenuItem showAllMenuItem;
private JMenu sortMenu;
private JMenuItem sortClassMenuItem;
private JMenuItem noSortMenuItem;
private JMenu classMenu;
private JMenu infoMenu;
private JMenuItem infoMenuItem;
private JTextField softwareText;
private JTextField projectText;
private JTextField fileText;
private JTable dtcTable;
private DefaultTableModel dtcModel;
private String[] classList;
private JComboBox classComboBox;
private String lastOpenedDir = ".";
private String filter = "";
private Dump dump = null;
private DTCInfo dtcInfo = null;
private Vector<DTC> allDTC = new Vector<>();
private Vector<DTC> filteredDTC = new Vector<>();
private int sortBy = 0;
private String APP_RELEASE = "v0.2";
// Class Cell Renderer/Editor
class DTCClassCellRenderer extends DefaultTableCellRenderer {
JComboBox cb = new JComboBox(classList);
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
cb.setSelectedIndex((Integer)value);
return cb;
}
}
class DTCClassCellEditor extends DefaultCellEditor
{
int row;
public DTCClassCellEditor()
{
super(classComboBox);
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column)
{
this.row = row;
classComboBox.setSelectedIndex((Integer)value);
return classComboBox;
}
@Override
public Object getCellEditorValue() {
return new Integer(classComboBox.getSelectedIndex());
}
}
// Env Cell Renderer / Editor
class EnvClassCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
return new JButton("Edit");
}
}
class EnvClassCellEditor extends DefaultCellEditor
{
JButton editButton;
int row;
public EnvClassCellEditor()
{
super(new JCheckBox());
editButton = new JButton("Edit");
editButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new DTCEnvPanel(new DTCEnv(filteredDTC.get(row),dtcInfo,dump.memory));
}
});
}
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column)
{
this.row = row;
return editButton;
}
}
public EDC16C34() {
// Create GUI
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
menuBar.add(fileMenu);
openMenuItem = new JMenuItem("Open DUMP");
openMenuItem.addActionListener(this);
fileMenu.add(openMenuItem);
saveMenuItem = new JMenuItem("Save DUMP");
saveMenuItem.addActionListener(this);
fileMenu.add(saveMenuItem);
fileMenu.add(new JSeparator());
exitMenuItem = new JMenuItem("Exit");
exitMenuItem.addActionListener(e -> System.exit(0));
fileMenu.add(exitMenuItem);
findMenu = new JMenu("Find DTC");
findMenuItem = new JMenuItem("Find code");
findMenuItem.addActionListener(this);
findMenu.add(findMenuItem);
showAllMenuItem = new JMenuItem("View all");
showAllMenuItem.addActionListener(this);
findMenu.add(showAllMenuItem);
menuBar.add(findMenu);
sortMenu = new JMenu("Sort");
noSortMenuItem = new JMenuItem("None");
noSortMenuItem.addActionListener(this);
sortMenu.add(noSortMenuItem);
sortClassMenuItem = new JMenuItem("By class");
sortClassMenuItem.addActionListener(this);
sortMenu.add(sortClassMenuItem);
menuBar.add(sortMenu);
classMenu = new JMenu("DTC Class");
menuBar.add(classMenu);
for(int i=1;i<=Dump.CLASS_NUMBER;i++) {
JMenuItem clItem = new JMenuItem("Class #"+i);
final int classID = i;
clItem.addActionListener(e -> {
if(dump != null ) {
new DTCClassPanel(new DTCClass(classID,dtcInfo,dump.memory));
}
});
classMenu.add(clItem);
}
infoMenu = new JMenu("Info");
infoMenuItem = new JMenuItem("View infos");
infoMenuItem.addActionListener(this);
infoMenuItem.addActionListener(e -> {
if(dump != null )
new InfoPanel(dump);
});
infoMenu.add(infoMenuItem);
menuBar.add(infoMenu);
setJMenuBar(menuBar);
setLayout(new BorderLayout());
JPanel refPanel = new JPanel();
refPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.insets.top = 2;
gbc.insets.bottom = 2;
gbc.insets.left = 2;
gbc.insets.right = 2;
gbc.weightx = 1.0;
fileText = new JTextField("");
fileText.setEditable(false);
refPanel.add(fileText,gbc);
gbc.weightx = 0.0;
JLabel softwareLabel = new JLabel("Software");
refPanel.add(softwareLabel,gbc);
softwareText = new JTextField("0000000000");
softwareText.setFont(bigFont);
softwareText.setEditable(false);
refPanel.add(softwareText,gbc);
JLabel projectLabel = new JLabel("Project");
refPanel.add(projectLabel,gbc);
projectText = new JTextField("XXXXXXXX");
projectText.setFont(bigFont);
projectText.setEditable(false);
refPanel.add(projectText,gbc);
add(refPanel,BorderLayout.NORTH);
// Table
dtcModel = new DefaultTableModel() {
public Class getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0: // Idx
case 1: // Main DTC code
case 2: // Code path #1
case 3: // Code path #2
case 4: // Code path #3
case 5: // Code path #4
return String.class;
case 6: // DTC Class
// Class
return Integer.class;
case 7: // DTC Env
// Class
return JButton.class;
}
return String.class;
}
public boolean isCellEditable(int row, int column) {
return column>=6;
}
public void setValueAt(Object aValue, int row, int column) {
if(column==6) {
filteredDTC.get(row).setDTCClass((Integer)aValue);
updateTable();
}
}
};
classList = new String[Dump.CLASS_NUMBER+1];
classList[0] = "Disabled";
for(int i=1;i<=Dump.CLASS_NUMBER;i++)
classList[i] = "Class " + i;
classComboBox = new JComboBox(classList);
dtcTable = new JTable(dtcModel);
dtcTable.setDefaultRenderer(Integer.class, new DTCClassCellRenderer());
dtcTable.setDefaultEditor(Integer.class, new DTCClassCellEditor());
dtcTable.setDefaultRenderer(JButton.class, new EnvClassCellRenderer());
dtcTable.setDefaultEditor(JButton.class, new EnvClassCellEditor());
dtcTable.setRowHeight(25);
JScrollPane dtcView = new JScrollPane(dtcTable);
add(dtcView,BorderLayout.CENTER);
setTitle("EDC16C34 DTC Controller [" + APP_RELEASE + "]");
setPreferredSize(new Dimension(1000,800));
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new EDC16C34();
}
@Override
public void actionPerformed(ActionEvent e) {
if( e.getSource()==openMenuItem ) {
JFileChooser chooser = new JFileChooser(lastOpenedDir);
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File f = chooser.getSelectedFile();
if (f != null) {
try {
loadDUMP(f.getAbsolutePath());
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
lastOpenedDir = f.getAbsolutePath();
}
}
} else if (e.getSource()==findMenuItem) {
String newFilter = JOptionPane.showInputDialog(this,"Find",filter);
if(newFilter!=null) {
filter = newFilter;
updateTable();
}
} else if (e.getSource()==sortClassMenuItem) {
sortBy = 1;
updateTable();
} else if (e.getSource()==noSortMenuItem) {
sortBy = 0;
updateTable();
} if (e.getSource()==showAllMenuItem) {
filter = "";
updateTable();
} else if (e.getSource()==saveMenuItem) {
JFileChooser chooser = new JFileChooser(lastOpenedDir);
int returnVal = chooser.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File f = chooser.getSelectedFile();
if (f != null) {
try {
if( f.exists() ) {
if (JOptionPane.showConfirmDialog(this,
"The file already exists, Do you want to overwrite ?","Warning",
JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
dump.write(f.getAbsolutePath());
} else {
if(JOptionPane.showConfirmDialog(this,
"Do you want to correct the checksum ?",
"Question",JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
ChecksumHelper.correct(dump.memory);
dump.write(f.getAbsolutePath());
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, ex.getMessage());
}
lastOpenedDir = f.getAbsolutePath();
}
}
}
}
private void loadDUMP(String fileName) throws IOException {
try {
dump = new Dump(fileName);
} catch (IOException e) {
throw new IOException("Cannot read: " + e.getMessage());
}
dtcInfo = dump.findDTCInfo();
fileText.setText(fileName);
softwareText.setText(dump.software);
projectText.setText(dump.project);
allDTC.clear();
for(int i = 0; i< this.dtcInfo.nbDTC; i++)
allDTC.add(new DTC(i,dtcInfo,dump.memory));
updateTable();
}
private void updateTable() {
filteredDTC = new Vector<>();
for(int i = 0; i< allDTC.size(); i++) {
if(allDTC.get(i).match(filter))
filteredDTC.add(allDTC.get(i));
}
if(sortBy>0) {
Collections.sort(filteredDTC, new Comparator<DTC>() {
@Override
public int compare(DTC o1, DTC o2) {
switch (sortBy) {
case 1: // By Class
return Integer.compare(o1.getDTCClass(), o2.getDTCClass());
}
return 0;
}
});
}
String colName[] = {"" , "Internal code" , "Code #1" , "Code #2" , "Code #3" , "Code #4" , "Class", "Env"};
Object[][] dtcInfo = new Object[filteredDTC.size()][8];
for(int i = 0; i< filteredDTC.size(); i++) {
dtcInfo[i][0] = "#" + filteredDTC.get(i).idx;
dtcInfo[i][1] = filteredDTC.get(i).getbaseCode();
dtcInfo[i][2] = filteredDTC.get(i).getCode(0);
dtcInfo[i][3] = filteredDTC.get(i).getCode(1);
dtcInfo[i][4] = filteredDTC.get(i).getCode(2);
dtcInfo[i][5] = filteredDTC.get(i).getCode(3);
dtcInfo[i][6] = new Integer(filteredDTC.get(i).getDTCClass());
dtcInfo[i][7] = "";
}
dtcModel.setDataVector(dtcInfo, colName);
dtcTable.getColumnModel().getColumn(0).setMaxWidth(40);
dtcTable.getColumnModel().getColumn(7).setMaxWidth(50);
}
}