-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostWork.java
More file actions
374 lines (326 loc) · 15.6 KB
/
PostWork.java
File metadata and controls
374 lines (326 loc) · 15.6 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package main;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
public class PostWork extends javax.swing.JFrame {
JTable algoTable = new JTable();
JTable listTable = new JTable();
public PostWork() throws IOException{
initComponents();
initMainComponents();
}
private void initMainComponents()throws IOException{
initMainFrame();
buttonRun();
initHelp();
createListTable();
createAlgoTable();
}
private void initMainFrame(){
setLocationRelativeTo(null);
setTitle("Машина Поста");
setResizable(false);
JLabel head = new JLabel("МАШИНА ПОСТА ДЛЯ АЛГОРИТМУ N1 + N2 - N3 - N4 + N5 + N6");
head.setFont(new Font("Courier New", Font.BOLD, 16));
head.setBounds(this.getWidth()/3, 10, 600, 15);
add(head);
}
private void buttonRun(){
JButton runButton = new JButton("RUN");
runButton.setFont(new Font("Courier New", Font.BOLD, 20));
add(runButton);
runButton.setBounds(800,90,100,100);
runButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent evt){
try {
runPostMachine();
} catch (IOException ex) {
Logger.getLogger(PostWork.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
private void initHelp(){
String text = "A - ABORT\n\n"
+ "E - EXCHANGE\n\n"
+ "R - RIGHT MOVE\n\n"
+ "L - LEFT MOVE\n\n"
+ "S - SET SYMBOL\n\n"
+ "D - DELETE SYMBOL";
JTextArea helpLabel = new JTextArea();
helpLabel.append(text);
helpLabel.setEditable(false);
helpLabel.setFont(new Font("Courier New", Font.BOLD, 12));
helpLabel.setBounds(800, 220, 170, 180);
add(helpLabel);
}
private JTable createListTable(){
DefaultTableModel listTableModel = new ListModel(1,60);//Таблица ЛЕНТА
listTable.setModel(listTableModel);
listTable.setRowHeight(30);
listTable.setShowGrid(true);
listTable.setRowSelectionAllowed(false);
listTable.setBounds(10, 50, super.getWidth()-20, 31);//отступы и размеры
add(listTable);
//визуальное выделение отдельной клеточки
listTable.setCellSelectionEnabled(true);
listTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//заполнение V при нажатии мышкой
listTable.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
int row = listTable.getSelectedRow();
int col = listTable.getSelectedColumn();
if (listTable.getValueAt(row, col) == null){
listTable.setValueAt("V", row, col);
}else if (listTable.getValueAt(row, col) == "V"){
listTable.setValueAt(null, row, col);
}
}
});
return listTable;
}
private JTable createAlgoTable() throws IOException{
Vector<String> newRow = new Vector<>();
Scanner scanner = new Scanner(new File("src\\main\\dataHead.txt"));
while(scanner.hasNextLine()){//запишем строку заста
newRow.add(scanner.nextLine());
}
DefaultTableModel algoTableModel = new MyModel(newRow,0);//таблица АЛГОРИТМ
algoTable.setModel(algoTableModel);
algoTable.setRowHeight(20);
algoTable.setShowGrid(true);
int colCount = algoTable.getColumnCount();//запрет resize
for (int i=0; i < colCount; i++){
algoTable.getColumnModel().getColumn(i).setResizable(false);
}
algoTable.getColumnModel().getColumn(0).setMaxWidth(60);//установка ширины столбцов
algoTable.getColumnModel().getColumn(1).setMaxWidth(60);
algoTable.getColumnModel().getColumn(2).setMaxWidth(80);
algoTable.getColumnModel().getColumn(3).setMaxWidth(80);
algoTable.getTableHeader().setReorderingAllowed(false);
JScrollPane scr = new JScrollPane(algoTable);//помещаем таблицу в ScrollPane
scr.setBounds(20, 90, 700, super.getHeight()-120);
add(scr);
algoTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
//считываем таблицу из файла
Vector<String> newR;
scanner = new Scanner(new File("src\\main\\testData.txt"));
System.out.println(scanner.hasNext());
int i = 0;
while(scanner.hasNextLine()){
newR = new Vector<>();
newR.add(scanner.nextLine());
newR.add(scanner.nextLine());
newR.add(scanner.nextLine());
newR.add(scanner.nextLine());
newR.add(scanner.nextLine());
algoTableModel.addRow(newR);
}
return algoTable;
}
private void runPostMachine() throws IOException{
Thread thread = new Thread(new Runnable(){
JTable list= listTable;
JTable algo = algoTable;
int currNumber = 1;
String currAction = null;
int currListPos = 0;
@Override
public void run() {
boolean flag = true;
while (flag){
currAction = algo.getValueAt(currNumber-1, 1).toString();
System.out.println(currAction);
switch(currAction){
case "D":
list.setValueAt(null, 0, currListPos);
currNumber = Integer.parseInt(algo.getValueAt(currNumber-1, 2).toString());
break;
case "R":
currListPos++;
currNumber = Integer.parseInt(algo.getValueAt(currNumber-1, 2).toString());
break;
case "L":
currListPos--;
currNumber = Integer.parseInt(algo.getValueAt(currNumber-1, 2).toString());
break;
case "S":
list.setValueAt("V", 0, currListPos);
currNumber = Integer.parseInt(algo.getValueAt(currNumber-1, 2).toString());
break;
case "E":
if (list.getValueAt(0, currListPos) == null){
currNumber = Integer.parseInt(algo.getValueAt(currNumber-1, 2).toString());
}else /*if(list.getValueAt(0, currListPos).equals("V"))*/{
currNumber = Integer.parseInt(algo.getValueAt(currNumber-1, 3).toString());
}
break;
case "A":
JOptionPane.showMessageDialog(algo, "Программа завершила работу!",
"Конец выполнения", JOptionPane.PLAIN_MESSAGE);
flag = false;
break;
}
updateListTable(currListPos);
if (!flag)
break;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void updateListTable(int currListPos){
TableModel mod = list.getModel();
((DefaultTableModel)mod).fireTableDataChanged();
for (int i = 0; i < list.getColumnCount(); i++)
list.getColumnModel().getColumn(i).setCellRenderer(new DefaultRender());
list.getColumnModel().getColumn(currListPos).setCellRenderer(new MyRender());
}
// private void updateAlgoTable(int currListPos){
// TableModel mod = algo.getModel();
// ((DefaultTableModel)mod).fireTableDataChanged();
// for (int i = 0; i < algo.getRowCount(); i++)
// list.getColumnModel().getColumn(i).setCellRenderer(new DefaultRender());
// algo.getr
// list.getColumnModel().getColumn(currListPos).setCellRenderer(new MyRender());
// }
});
thread.start();
}
public class ListModel extends DefaultTableModel{
ListModel(int row, int column){
super(row,column);
}
ListModel(Vector list, int row){
super(list,row);
}
boolean[] canEdit = new boolean [] {
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
};
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
}
public class MyModel extends DefaultTableModel{
MyModel(int row, int column){
super(row,column);
}
MyModel(Vector list, int row){
super(list,row);
}
boolean[] canEdit = new boolean [] {
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false
};
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setUndecorated(true);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1140, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 530, Short.MAX_VALUE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(PostWork.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(PostWork.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(PostWork.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(PostWork.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new PostWork().setVisible(true);
} catch (IOException ex) {
Logger.getLogger(PostWork.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
}