-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubUnitView.java
More file actions
302 lines (227 loc) · 10.2 KB
/
Copy pathSubUnitView.java
File metadata and controls
302 lines (227 loc) · 10.2 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseWheelEvent;
public class SubUnitView extends JPanel {
private JScrollPane scrollPane;
private RXTable table;
private SubUnitTableModel subUnitTableModel;
private String header;
private UnitView parent;
private Integer myIndex;
private int type;
private String pretTotalUnitar;
public SubUnitView(String header, int type) {
//URES KONSTRUKTOR
this.type = type;
this.header = header;
this.setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
String[][] data;
switch (type){
case Finals.MATERIAL_SUB_UNIT_TYPE:
case Finals.TRANSPORT_SUB_UNIT_TYPE:
data = new String[][]{{"1", "", "", "", "", "", "", "", ""}};
break;
case Finals.MANOPERA_SUB_UNIT_TYPE:
case Finals.UTILAJ_SUB_UNIT_TYPE:
data = new String[][]{{"1", "", "", "", "", "", "", "", "", ""}};
break;
default:
data = null;//kene valami exception
}
pretTotalUnitar = "0.0";
setupTable(data);
scrollPane = new JScrollPane(table){
@Override
protected void processMouseWheelEvent(MouseWheelEvent e) {
//https://stackoverflow.com/questions/12911506/why-jscrollpane-does-not-react-to-mouse-wheel-events
if (!isWheelScrollingEnabled()) {
if (getParent() != null)
getParent().dispatchEvent(
SwingUtilities.convertMouseEvent(this, e, getParent()));
return;
}
super.processMouseWheelEvent(e);
}
};
scrollPane.setWheelScrollingEnabled(false); ;
adjustSizeAndPadding();
}
public SubUnitView(String header, UnitView parent, int parentIndex, int myIndex, int type ){
//NEM URES KONSTRUKTOR
this.myIndex = myIndex;
this.type = type;
this.header = header;
this.parent = parent;
this.setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
//LOAD DATA
TaskTableCreator ttc = new TaskTableCreator(parent.getParent().getMyModel());
String[][] data = ttc.getSubUnitTs()[parentIndex][myIndex];//kinyerem a sorok matrixat
//pretTotalUnitar = parent.getParent().myController.ITT A PRET TOTAL UNITART BE KELL TOLTENI
if(data.length == 0){//ha nincs egy sor sem beszurom az elso sort
switch (type){
case Finals.MATERIAL_SUB_UNIT_TYPE:
case Finals.TRANSPORT_SUB_UNIT_TYPE:
data = new String[][]{{"1", "", "", "", "", "", "", "", ""}};
break;
case Finals.MANOPERA_SUB_UNIT_TYPE:
case Finals.UTILAJ_SUB_UNIT_TYPE:
data = new String[][]{{"1", "", "", "", "", "", "", "", "", ""}};
break;
default:
data = null;//kene valami exception
}
setupTable(data);
}
else{//ha van sor, betesze oket s beszurok egy ures sort
setupTable(data);
Integer newIndex = data.length + 1;
insertBlankRow(newIndex);
}
//END OF LOAD
scrollPane = new JScrollPane(table){
@Override
protected void processMouseWheelEvent(MouseWheelEvent e) {
//https://stackoverflow.com/questions/12911506/why-jscrollpane-does-not-react-to-mouse-wheel-events
if (!isWheelScrollingEnabled()) {
if (getParent() != null)
getParent().dispatchEvent(
SwingUtilities.convertMouseEvent(this, e, getParent()));
return;
}
super.processMouseWheelEvent(e);
}
};
scrollPane.setWheelScrollingEnabled(false); ;
pretTotalUnitar = ttc.getSumPretTotalUnitars()[parentIndex][myIndex];
adjustSizeAndPadding();
}
public void setupTable(String[][] data){
String[] columns;
switch (type){
case Finals.MATERIAL_SUB_UNIT_TYPE:
columns = Finals.MATERIAL_TABLE_HEADER;
break;
case Finals.TRANSPORT_SUB_UNIT_TYPE:
columns = Finals.TRANSPORT_TABLE_HEADER;
break;
case Finals.MANOPERA_SUB_UNIT_TYPE:
columns = Finals.MANOPERA_TABLE_HEADER;
break;
case Finals.UTILAJ_SUB_UNIT_TYPE:
columns = Finals.UTILAJ_TABLE_HEADER;
break;
default:
columns = null;//kene valami exception
}
//ne lehessen athelyezni az oszlopokat es egy egesz sort kibalasztani
table = new RXTable(data,columns);
table.setSelectAllForEdit(true);
table.getTableHeader().setReorderingAllowed(false);
table.setRowSelectionAllowed(false);
// table.setSelectAllForEdit(true);
// table.setSelectAllForKeyEvent(true);
// table.setSelectAllForActionEvent(true);
// table.setSelectAllForMouseEvent(true);
//lehessen csak bizonyos cellakat modositni
subUnitTableModel = new SubUnitTableModel(data,columns);
table.setModel(subUnitTableModel);
Action action = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
TableCellListener tcl = (TableCellListener)e.getSource();
if(userInputWrong((String)tcl.getNewValue())){
//if input contains the file structure token TOK_D, then revert changes and throw
table.getModel().setValueAt(tcl.getOldValue(),tcl.getRow(),tcl.getColumn());
JOptionPane.showMessageDialog(parent.getParent().myFrame,
"Caracterul " + Finals.TOK_D + " nu este permis!",
"Caracter invalid",
JOptionPane.ERROR_MESSAGE);
return;
}
if(myIndex == null){
table.getModel().setValueAt("",tcl.getRow(),tcl.getColumn());
return;
}
if(subUnitTableModel.getRowCount() - 1 == tcl.getRow()){//last row was edited
//insert new row
String[] oldRow = (String[])getRowAt(tcl.getRow());
Integer newIndex = Integer.parseInt(oldRow[0]) + 1;
insertBlankRow(newIndex);
}
cellChanged(parent.getMyIndex(),myIndex,tcl.getRow(),tcl.getColumn(), (String)tcl.getNewValue());
resizeSubunit();
//table.changeSelection(tcl.getRow(),tcl.getColumn() + 1, false,false);
//table.changeSelection(tcl.getRow(), tcl.getColumn() + 1, false, false);
//table.editCellAt(tcl.getRow(), tcl.getColumn() + 1);
}
};
TableCellListener tcl = new TableCellListener(table, action);
//a szmaok kozepen jelenjenek meg
// https://stackoverflow.com/questions/7433602/how-to-center-in-jtable-cell-a-value
JTableUtilities.setCellsAlignment(table, SwingConstants.CENTER,subUnitTableModel);
table.setFillsViewportHeight(true);
table.setPreferredScrollableViewportSize(new Dimension(600,450));
//a header szerint meretezze az oszlopokat
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
TableColumnAdjuster tca = new TableColumnAdjuster(table);
tca.adjustColumns();
// if((parent != null) && (parent.getMyIndex() == Controller.activeUnitIndex) && (myIndex == Controller.activeSubUnitIndex)){
// table.grabFocus();
// table.changeSelection(Controller.activeRow,Controller.activeColumn,false, false);
// //while(!table.hasFocus())table.grabFocus();
// }
// table.changeSelection(2,2,false,false);
}
private boolean userInputWrong(String data){
return data.contains(Finals.TOK_D);
}
public void insertBlankRow(Integer newIndex){
subUnitTableModel.addRowToBooleanMatrix();
if((type == Finals.MANOPERA_SUB_UNIT_TYPE) ||
(type == Finals.TRANSPORT_SUB_UNIT_TYPE)){
subUnitTableModel.addRow(new String[]{newIndex.toString(), "", "", "", "", "", "", "", ""});
}
else{
subUnitTableModel.addRow(new String[]{newIndex.toString(), "", "", "", "", "", "", "", "", ""});
}
}
public void adjustSizeAndPadding(){
// sok flaska a sizeal kapcsolatosan
//this.setBackground(Color.BLACK);
this.add(new JLabel(header));
//scrollPane.add(new JLabel("PRET TOTAL UNITAR: 263.3"));
this.add(scrollPane);
JLabel pretTotalLabel = new JLabel(Finals.PRET_TOTAL_UNITAR_TEXT + pretTotalUnitar);
pretTotalLabel.setFont(new Font("Arial",Font.PLAIN,10));
this.add(pretTotalLabel);
JPanel paddingPanel = new JPanel();
this.add(paddingPanel);
//this.setBorder(new LineBorder(Color.black));
resizeSubunit();
}
public void resizeSubunit(){
//resize after changes have been made
TableColumnAdjuster tca = new TableColumnAdjuster(table);
tca.adjustColumns();
int numOfRows = table.getRowCount() + 2;
int rowHeight = table.getRowHeight();
int width = 559;
int height = numOfRows * rowHeight + 50;
scrollPane.setPreferredSize(new Dimension(width,height));
this.setPreferredSize(new Dimension(width,height));
this.revalidate();
this.repaint();
}
public Object[] getRowAt(int row) {
Object[] result = new String[Finals.LENGTH_OF_SUB_UNIT_TABLE];
for (int i = 0; i < Finals.LENGTH_OF_SUB_UNIT_TABLE; i++) {
result[i] = table.getModel().getValueAt(row, i);
}
return result;
}
public void cellChanged(Integer unitIndex, Integer subUnitIndex, Integer rowIndex, Integer columnIndex, String data){
parent.cellChanged(unitIndex,subUnitIndex,rowIndex,columnIndex,data);
}
}