-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathCTable.java
More file actions
191 lines (182 loc) · 6.31 KB
/
Copy pathCTable.java
File metadata and controls
191 lines (182 loc) · 6.31 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
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the MIT License https://raw.github.com/mit-cml/app-inventor/master/mitlicense.txt
package openblocks.codeblockutil;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import openblocks.codeblockutil.CScrollPane.ScrollPolicy;
/**
* CTable is a generic table with a look and feel that matches the
* rest of codeblocks.
*/
public class CTable extends JPanel{
private static final long serialVersionUID = 328149080251L;
private static final int COLUMN_WIDTH = 50;
private static final int ROW_HEIGHT = 15;
private static final Color foreground = Color.white;
private static final Font font = new Font("Ariel", Font.BOLD, 12);
private final List<double[]> data;
private final JComponent view;
private final JComponent scroll;
private String[] columns;
private JLabel[] columnLabels;
public CTable(){
this(9);
}
/**
* Create a new Table instance with an empty domain
* @param i - thumb width
*/
public CTable(int i){
super(new BorderLayout());
this.columns = new String[] {};
this.columnLabels = new JLabel[] {};
this.data = new ArrayList<double[]>();
view = new JPanel();
view.setBackground(foreground);
scroll = new CTracklessScrollPane(
view,
ScrollPolicy.VERTICAL_BAR_AS_NEEDED,
ScrollPolicy.HORIZONTAL_BAR_AS_NEEDED,
i, CGraphite.darkgreen, new Color(100,100,100));
this.add(scroll, BorderLayout.CENTER);
}
public void addMouseListener(MouseListener l){
this.view.addMouseListener(l);
}
/**
* rests this table with the new column identities
*
*/
public void setColumns(String[] columns){
data.clear();
view.removeAll();
view.setLayout(new GridLayout(0,columns.length));
columnLabels = new JLabel[columns.length];
int i = 0;
for(String name : columns){
JLabel label = new JLabel(name, SwingConstants.CENTER);
label.setFont(font);
label.setForeground(foreground);
label.setOpaque(true);
label.setBackground(CGraphite.darkgreen);
label.setBorder(BorderFactory.createMatteBorder(1,1,1,1, Color.BLUE));
view.add(label);
columnLabels[i] = label;
i++;
}
this.columns = columns;
view.setPreferredSize(new Dimension(columns.length*COLUMN_WIDTH,ROW_HEIGHT*2));
scroll.revalidate();
}
/**
* Updates the column names of this with the specified String array of column names
* @param columns the desired column names to update this with
*/
public void updateColumns(String[] columns) {
this.columns = columns;
for(int i = 0; i < columns.length; i++) {
columnLabels[i].setText(columns[i]);
}
scroll.revalidate();
scroll.repaint();
}
/**
* Clears the table of all data, but keeps the current column names
*/
public void clearTable(){
this.setColumns(this.columns);
}
/**
* Adds a row of data to this using the double array of specified datum
* @param datum desired row of data to add to this
*/
public void addRow(double[] datum){
for(int i=0; i<columns.length; i++){
JLabel label;
if (i<datum.length){
label = new JLabel(Double.toString(datum[i]),SwingConstants.CENTER);
}else{
label = new JLabel();
}
label.setOpaque(true);
if(i == 0){
label.setFont(font);
label.setBackground(CGraphite.darkgreen);
label.setForeground(foreground);
label.setBorder(BorderFactory.createMatteBorder(1,1,1,1, Color.BLUE));
}else{
label.setFont(font);
label.setBackground(CGraphite.gray);
label.setForeground(foreground);
label.setBorder(BorderFactory.createMatteBorder(1,1,1,1, Color.BLUE));
}
view.add(label);
}
data.add(datum);
view.setPreferredSize(new Dimension(columns.length*COLUMN_WIDTH,ROW_HEIGHT*(1+data.size())));
scroll.revalidate();
scroll.repaint();
}
public String getCSV(){
StringBuilder output = new StringBuilder();
for(int i=0; i<columns.length; i++){
output.append(columns[i]+",");
}
output.append("\n");
for(double[] datum : data){
for(int i=0; i<datum.length; i++){
output.append(datum[i]+",");
}
output.append("\n");
}
output.append("\n");
return output.toString();
}
public Insets getInsets(){
return new Insets(10,10,35,10);
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new BorderLayout());
f.setSize(300, 200);
final CTable c = new CTable();
c.setColumns(new String[] {"a","b","c"});
//c.setPreferredSize(new Dimension(400,50));
f.add(c, BorderLayout.CENTER);
JButton button = new JButton("add data");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
c.addRow(new double[] {1,2,3});
}
});
f.add(button, BorderLayout.SOUTH);
JButton button2 = new JButton("save data");
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.out.println(c.getCSV());
}
});
f.add(button2, BorderLayout.NORTH);
f.setVisible(true);
f.repaint();
}
}