Repository navigation
Expand file tree
/
Copy pathtablemodel.java
More file actions
322 lines (288 loc) · 9 KB
/
Copy pathtablemodel.java
File metadata and controls
322 lines (288 loc) · 9 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
import javax.swing.JFrame;
import javax.swing.table.TableColumn;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
/**
*
* Definition for the tables
*
* @author Jon Arne Westgaard
*
*/
public class tablemodel extends AbstractTableModel {
private String[] columnNames= {"type", "variablename", "text", "row", "columns", "rows", "columns", "fill", "anchor" } ;
private Vector data = new Vector();
private Logger logger = Logger.getLogger("MyLog");
private final String fileHeader = "GridBagLayoutEditor file format v1.2";
tablemodel() {
this.columnNames[0] = prosjekt1.getMessages().getString("type");
this.columnNames[1] = prosjekt1.getMessages().getString("variablename");
this.columnNames[2] = prosjekt1.getMessages().getString("text");
this.columnNames[3] = prosjekt1.getMessages().getString("row");
this.columnNames[4] = prosjekt1.getMessages().getString("column");
this.columnNames[5] = prosjekt1.getMessages().getString("rows");
this.columnNames[6] = prosjekt1.getMessages().getString("columns");
this.columnNames[7] = prosjekt1.getMessages().getString("fill");
this.columnNames[8] = prosjekt1.getMessages().getString("anchor");
}
// Insert a row
/**
* Add row to table
*/
public void insertRow() {
//this.data.addElement("nils"+this.data.size());
//fireTableRowsInserted(this.data.size() - 1, this.data.size() - 1);
this.data.add(new componentJLabel("ny" + this.data.size(), "", 1, 1, 1, 1));
fireTableRowsInserted(this.data.size() - 1, this.data.size() - 1);
logger.log(Level.FINE,"Ny rad på posisjon " + (this.data.size() - 1));
}
// Delete a row
/**
* Delete a row from the table
*
* @param row The row to delete
*/
public void deleteRow (int row) {
this.data.removeElementAt(row);
fireTableRowsDeleted(0,row);
}
// Create a new table, delete everything in the current one
/**
* Add row to table
*/
public void ny() {
this.data.removeAllElements();
fireTableRowsDeleted(0,this.data.size());
}
// Return number of rows
/**
* Count rows from table
*
* @return int with table size
*/
public int getRowCount() {
return this.data.size();
}
// Get number of columns
/**
* Returns number of columns
*
* @return int with columns
*/
public int getColumnCount() {
return this.columnNames.length;
}
/**
* Sets the name for each column
*
* @param col the column to return
* @return the name of a column
*/
public String getColumnName(int col) {
return this.columnNames[col].toString();
}
/**
* Return value at cell
*
* @param row the row to return the cell from
* @param coloumn of the cell to return
*
* @return Object of cell
*/
//
public Object getValueAt(int row, int col) {
//return ((component)this.data.elementAt(row)).getcolumn(col);
//return ((Komponent)this.data.elementAt(paramInt1)).getKolonne(paramInt2);
//return ((component)this.data.elementAt(row)).getcolumn(col);
return this.data.elementAt(row);
}
/**
* Check if the cell is editable
*
* @param row the row to return the cell from
* @param coloumn of the cell to return
*
* @return bool
*/
public boolean isCellEditable(int row, int col) {
return true; // TODO: Dette kan ikke være riktig? Christer
}
/**
* Set the value of a cell
*
* @param row the row to return the cell from
* @param coloumn of the cell to return
*/
public void setValueAt(Object object1, int row, int col)
{
if (col == 0) {
int i = ((Integer)object1).intValue();
switch (i) {
case 0:
this.data.setElementAt(new JLabel("iu"), row);
//this.data.setElementAt(new componentJLabel((component)this.data.elementAt(row)), row);
break;
case 1:
this.data.setElementAt(new componentJButton((component)this.data.elementAt(row)), row);
break;
case 2:
this.data.setElementAt(new componentJTextField((component)this.data.elementAt(row)), row);
break;
case 3:
this.data.setElementAt(new componentJTextArea((component)this.data.elementAt(row)), row);
break;
case 4:
this.data.setElementAt(new componentJCheckBox((component)this.data.elementAt(row)), row);
break;
case 5:
this.data.setElementAt(new componentJList((component)this.data.elementAt(row)), row);
break;
case 6:
this.data.setElementAt(new componentJComboBox((component)this.data.elementAt(row)), row);
break;
case 7:
this.data.setElementAt(new componentJSpinnerList((component)this.data.elementAt(row)), row);
break;
case 8:
this.data.setElementAt(new componentJSpinnerNumber((component)this.data.elementAt(row)), row); }
}
else
{
((component)this.data.elementAt(row)).setcolumn(row, col);
}
fireTableCellUpdated(row, col);
}
/**
* Get the class of a cell
*
* @param row
* @param col
* @return class type
*/
public Class getColumnClass(int row, int col)
{
//return getValueAt(0, i).getClass();
return this.getValueAt(row, col).getClass();
}
/**
* Move a row up
*
* @param row to move
*/
public void moverowup(int row)
{
if ((row >= 0) && (row < this.data.size() - 1)) {
Object localObject = this.data.elementAt(row);
deleteRow(row);
this.data.insertElementAt(localObject, row - 1);
fireTableRowsInserted(row - 1, row - 1);
}
}
/**
* Move row down
*
* @param row to move
*/
public void moverowdown(int row)
{
if (row <= this.data.size() - 1) {
Object localObject = this.data.elementAt(row);
deleteRow(row);
this.data.insertElementAt(localObject, row + 1);
fireTableRowsInserted(row + 1, row + 1);
}
}
/**
* Write table to file
*
* @param randTblFle File to write to
* @throws IOException
*/
public void saveToFile(RandomAccessFile randTblFle) throws IOException{
randTblFle.writeUTF(fileHeader);
randTblFle.writeInt(this.data.size());
for (int i = 0; i < this.data.size(); i++) {
component localKomponent = (component)this.data.elementAt(i);
localKomponent.tilFil(randTblFle);
}
logger.log(Level.FINE, "Written" + this.data.size() + " to file"); //TODO: Internasionalise
}
/**
* Open/load file
*
* @param randAccFile The file to open
* @throws IOException
*/
public void openTableFile(RandomAccessFile randAccFile) throws IOException{
try {
String str = randAccFile.readUTF();
if (!str.equals(fileHeader)) {
logger.warning("Feil p� kontrollstring i filformat, kunne ikke laste inn fil");
JOptionPane.showMessageDialog(null, prosjekt1.getMessages().getString("fileLoad.baddate"), prosjekt1.getMessages().getString("fileLoad.table"), 0);
return;
}
} catch (Exception e) {
logger.log(Level.INFO,"Error with file: " + e.getMessage());
JOptionPane.showMessageDialog(null, prosjekt1.getMessages().getString("fileLoad.baddate"), prosjekt1.getMessages().getString("fileLoad.table"), 0);
return;
}
this.data.removeAllElements(); // Remove all the elements
int i = randAccFile.readInt();
try {
for (int j = 0; j < i; j++) {
component localcomponent = new component();
Object localObject = null;
logger.log(Level.FINE, "Leser inn component " + j);
localcomponent.fraFil(randAccFile);
switch (localcomponent.getType()) { case 0:
localObject = new componentJLabel(localcomponent);
break;
case 1:
//localObject = new componentJButton(localcomponent);
break;
case 2:
//localObject = new componentJTextField(localcomponent);
((component)localObject).fraFil(randAccFile);
break;
case 3:
//localObject = new componentJTextArea(localcomponent);
((component)localObject).fraFil(randAccFile);
break;
case 4:
//localObject = new componentJCheckBox(localcomponent);
break;
case 5:
//localObject = new componentJList(localcomponent);
((component)localObject).fraFil(randAccFile);
break;
case 6:
//localObject = new componentJComboBox(localcomponent);
((component)localObject).fraFil(randAccFile);
break;
case 7:
//localObject = new componentJSpinnerList(localcomponent);
((component)localObject).fraFil(randAccFile);
break;
case 8:
//localObject = new componentJSpinnerNumber(localcomponent);
((component)localObject).fraFil(randAccFile);
}
this.data.add(localObject);
}
} catch (Exception localException2) {
logger.log(Level.FINE, "Feil under lesing fra fil : " + localException2.toString());
}
fireTableRowsInserted(0, this.data.size() - 1);
logger.log(Level.FINE, "Lest inn fil med " + this.data.size() + " rader");
}
} // End of class tablemodel