-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtendedSubUnitView.java
More file actions
193 lines (133 loc) · 5.78 KB
/
Copy pathExtendedSubUnitView.java
File metadata and controls
193 lines (133 loc) · 5.78 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
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseWheelEvent;
//CURRENTLY OBSOLETE
public class ExtendedSubUnitView extends JPanel {
private JScrollPane scrollPane;
private JTable table;
private SubUnitTableModel subUnitTableModel;
private String header;
public ExtendedSubUnitView(String header) {
//URES KONSTRUKTOR
this.header = header;
this.setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
String[][] data = {{"1", "", "", "", "", "", "", "", "", "", ""}};
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 ExtendedSubUnitView(String header,UnitView parent, int parentIndex, int myIndex) {
//NEM URES KONSTRUKTOR
this.header = header;
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
if(data.length == 0){//ha nincs egy sor sem beszurom az elso sort
data = new String[][]{{"1","","","","","","","","","",""}};
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); ;
adjustSizeAndPadding();
}
public void setupTable(String[][] data){
String[] columns = Finals.EXTENDED_SUB_UNIT_TABLE_HEADER;
table = new JTable(data,columns);
table.getTableHeader().setReorderingAllowed(false);
table.setRowSelectionAllowed(false);
subUnitTableModel = new SubUnitTableModel(data,columns);
table.setModel(subUnitTableModel);
// https://stackoverflow.com/questions/7433602/how-to-center-in-jtable-cell-a-value
JTableUtilities.setCellsAlignment(table, SwingConstants.CENTER,subUnitTableModel);
Action action = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
TableCellListener tcl = (TableCellListener)e.getSource();
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);
}
resizeSubunit();
}
};
TableCellListener tcl = new TableCellListener(table, action);
table.setFillsViewportHeight(true);
table.setPreferredScrollableViewportSize(new Dimension(600,450));
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
TableColumnAdjuster tca = new TableColumnAdjuster(table);
tca.adjustColumns();
}
public void adjustSizeAndPadding(){
//this.setBackground(Color.BLACK);
this.add(new JLabel(header));
this.add(scrollPane);
JLabel pretTotalLabel = new JLabel("PRET TOTAL UNITAR: 256.2569");
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 insertBlankRow(Integer newIndex){
subUnitTableModel.addRowToBooleanMatrix();
subUnitTableModel.addRow(new String[]{newIndex.toString(), "", "", "", "", "", "", "", "", "", ""});
}
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 + 30;
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;
}
}