forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.java
More file actions
executable file
·134 lines (132 loc) · 3.58 KB
/
Copy pathTable.java
File metadata and controls
executable file
·134 lines (132 loc) · 3.58 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
package javaforce.webui;
/** Table
*
* @author pquiring
*/
public class Table extends Container {
private int cellWidth, cellHeight, cols, rows;
private boolean border;
public Table(int cellWidth, int cellHeight, int cols, int rows) {
this.cellWidth = cellWidth; //X
this.cellHeight = cellHeight; //Y
this.cols = cols; //X
this.rows = rows; //Y
setClass("table");
setSize();
}
private class Cell extends Container {
public int x,y,spanx,spany;
public Cell(Component comp) {
setClass("cell");
add(comp);
}
public String html() {
setSize(spanx * cellWidth, spany * cellHeight);
//setPosition(x * width, y * height);
setStyle("left", Integer.toString(x * cellWidth));
setStyle("top", Integer.toString(y * cellHeight));
StringBuffer sb = new StringBuffer();
sb.append("<div" + getAttrs() + ">");
sb.append(get(0).html());
sb.append("</div>");
return sb.toString();
}
}
private void setSize() {
setSize(cellWidth * cols, cellHeight * rows);
sendEvent("setsize", new String [] {"w=" + (cellWidth * cols), "h=" + (cellHeight * rows)});
}
public void setBorder(boolean state) {
border = state;
if (border) {
addClass("border");
} else {
removeClass("border");
}
}
public String html() {
StringBuffer sb = new StringBuffer();
//using an actualy <table> proved to be too difficult once spans where implemented
sb.append("<div" + getAttrs() + ">");
for(int y=0;y<rows;y++) {
for(int x=0;x<cols;x++) {
Cell cell = getCell(x,y,false);
if (cell == null) continue;
sb.append(cell.html());
}
}
sb.append("</div>");
return sb.toString();
}
public void add(Component comp, int x, int y) {
add(comp,x,y,1,1);
}
public void add(Component comp, int x, int y, int spanx, int spany) {
String html;
Cell cell = new Cell(comp);
cell.x = x;
cell.y = y;
cell.spanx = spanx;
cell.spany = spany;
add(cell);
}
public void addRow() {
rows++;
setSize();
}
public void addColumn() {
cols++;
setSize();
}
/** Sets number of columns and rows. */
public void setTableSize(int cols, int rows) {
this.cols = cols;
this.rows = rows;
setSize();
}
public Component remove(int x,int y) {
Cell cell = getCell(x,y,false);
if (cell != null) {
remove(cell);
sendEvent("remove", new String[] {"child=" + cell.id});
return cell.get(0);
}
return null;
}
public void setSpans(int x,int y,int spanx, int spany) {
Cell cell = getCell(x,y,false);
if (cell == null) return;
cell.spanx = spanx;
cell.spany = spany;
cell.sendEvent("setsize", new String[] {"w=" + spanx * cellWidth, "h=" + spany * cellHeight});
}
private Cell getCell(int x,int y,boolean checkSpans) {
int cnt = count();
for(int a=0;a<cnt;a++) {
Cell cell = (Cell)get(a);
int x1 = cell.x;
int y1 = cell.y;
int x2 = x1;
int y2 = y1;
if (checkSpans) {
x2 += cell.spanx - 1;
y2 += cell.spany - 1;
}
if (x >= x1 && x <= x2 && y >= y1 && y <= y2) {
return cell;
}
}
return null;
}
public Component get(int x,int y,boolean checkSpans) {
Cell cell = getCell(x,y,checkSpans);
if (cell == null) return null;
return cell.get(0);
}
public int getRows() {
return rows;
}
public int getColumns() {
return cols;
}
}