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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.regex.PatternSyntaxException;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableRowSorter;
public class DataDisplay extends JFrame{
private static final long serialVersionUID = 1L;
private JButton saveButton = null;
private JButton closeButton = null;
private JButton deleteButton = null;
private JTextField filterField = null;
private JPanel pan = null;
private DataStocked datasheet = null;
private JTable table = null;
private TableRowSorter<AbstractTableModel> sorter;
protected DataDisplay() {
super();
this.setLayout(new GridBagLayout());
this.setVisible(true);
this.setPreferredSize(new Dimension(600,600));
this.setMinimumSize(new Dimension(400,400));
this.setLocationRelativeTo(null);
this.filterField = new JTextField(25);
this.filterField.setEditable(true);
this.filterField.getDocument().addDocumentListener(
new DocumentListener (){
public void changedUpdate(DocumentEvent e){
newFilter();
}
public void insertUpdate(DocumentEvent e){
newFilter();
}
public void removeUpdate(DocumentEvent e){
newFilter();
}
});
this.saveButton = new JButton("Save");
this.closeButton = new JButton ("Close");
this.deleteButton = new JButton ("Delete");
this.saveButton.setPreferredSize(deleteButton.getPreferredSize());
this.closeButton.setPreferredSize(deleteButton.getPreferredSize());
this.saveButton.addActionListener(new ButtonAction());
this.closeButton.addActionListener(new ButtonAction());
this.deleteButton.addActionListener(new ButtonAction());
pan = new JPanel();
this.datasheet = DataStocked.getDataStocked();
this.table = new JTable(this.datasheet.getData(), datasheet.getTitle());
this.table.setAutoCreateRowSorter(true);
sorter = new TableRowSorter<AbstractTableModel>((AbstractTableModel)this.table.getModel());
this.table.setRowSorter(sorter);
this.table.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent event){
if(event.getButton() == MouseEvent.BUTTON3){
Point p = new Point(event.getX(), event.getY());
if(table.columnAtPoint(p)!= -1){
JMenuItem addColumn = new JMenuItem("Add Column");
addColumn.addActionListener(new AddColumnListener(p));
JMenuItem removeColumn = new JMenuItem("Remove this Column");
removeColumn.addActionListener(new RemoveColumnListener(p));
JMenuItem change = new JMenuItem("Change Column's name");
change.addActionListener(new ChangeListener(p));
JMenuItem addRow = new JMenuItem("Add Row");
addRow.addActionListener(new AddRowListener(p));
JMenuItem removeRow = new JMenuItem("Remove this Row");
removeRow.addActionListener(new RemoveRowListener(p));
JPopupMenu menu = new JPopupMenu();
menu.add(addColumn);
menu.add(removeColumn);
menu.add(change);
menu.add(addRow);
menu.add(removeRow);
menu.show(table, event.getX(), event.getY());
}
}
}
});
}
protected void display(){
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 1;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1.;
gbc.weighty = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5, 5, 10, 5);
this.add(new JScrollPane(this.table), gbc);
pan.add(new JLabel("Filter Text"));
pan.add(filterField);
pan.add(deleteButton);
pan.add(saveButton);
pan.add(closeButton);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridheight = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1.;
gbc.weighty = 0;
gbc.fill = GridBagConstraints.NONE;
gbc.insets = new Insets(5, 5, 10, 5);
this.add(pan,gbc);
}
protected void newFilterExactMatches(String s){
RowFilter<AbstractTableModel, Object> rf = null;
try{
this.filterField.setText("^"+s+"$");
rf = RowFilter.regexFilter(this.filterField.getText(),0);
}catch (PatternSyntaxException e){
e.printStackTrace();
}
sorter.setRowFilter(rf);
}
protected void newFilter(){
RowFilter<AbstractTableModel, Object> rf = null;
try{
rf = RowFilter.regexFilter(this.filterField.getText(),0);
}catch (PatternSyntaxException e){
e.printStackTrace();
}
sorter.setRowFilter(rf);
}
protected Vector<Vector<Object>> getDataFromTable(JTable t){
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
for(int i = 0; i < t.getRowCount(); i++){
Vector<Object> row = new Vector<Object>();
for(int j = 0; j < t.getColumnCount(); j++){
if (t.getValueAt(i,j) != null)row.add(t.getValueAt(i,j));
}
data.add(row);
}
return data;
}
protected Vector<Object> getTitleFromTable(JTable t){
Vector<Object> title = new Vector<Object>();
int i=0;
while (i<table.getColumnCount()){
title.add(table.getColumnName(i));
i++;
}
return title;
}
protected class RemoveColumnListener implements ActionListener{
private Point point = null;
private Object[][] data;
private Object[] title;
public RemoveColumnListener(Point col){
this.point = col;
}
public void actionPerformed(ActionEvent event) {
int col = table.columnAtPoint(this.point);
TableColumn column = table.getColumn(table.getColumnName(col));
initNewData(column);
((DefaultTableModel)table.getModel()).setDataVector(this.data, this.title);
}
private void initNewData(TableColumn column){
this.data = new Object[table.getRowCount()][table.getColumnCount()-1];
this.title = new Object[table.getColumnCount()-1];
for(int i = 0; i < table.getRowCount(); i++){
for(int j = 0, k = 0; j < table.getColumnCount(); j++){
if(!((DefaultTableModel)table.getModel()).getColumnName(j).equals(column.getHeaderValue())){
if(i == 0)this.title[k] = ((DefaultTableModel)table.getModel()).getColumnName(j);
this.data[i][k] = table.getValueAt(i, j);
k++;
}
}
}
}
}
protected class AddColumnListener implements ActionListener{
private Point point = null;
public AddColumnListener (Point p){
point=p;
}
public void actionPerformed(ActionEvent event) {
JOptionPane jop = new JOptionPane();
@SuppressWarnings("static-access")
String name = jop.showInputDialog(null, "Saisissez le nom de la nouvelle colonne", "Ajout d'une colonne", JOptionPane.QUESTION_MESSAGE);
if(name != null){
if(!name.trim().equals("")){
((DefaultTableModel) table.getModel()).addColumn(name);
}
}
}
}
protected class AddRowListener implements ActionListener{
private Point point = null;
public AddRowListener(Point p){
point = p;
}
public void actionPerformed(ActionEvent event) {
int row = table.rowAtPoint(point);
((DefaultTableModel)table.getModel()).insertRow(row, new Vector<Object>());
}
}
protected class RemoveRowListener implements ActionListener {
private Point point = null;
public RemoveRowListener (Point p){
point = p;
}
public void actionPerformed(ActionEvent event){
int row = table.rowAtPoint(point);
((DefaultTableModel)table.getModel()).removeRow(row);
}
}
protected class ChangeListener implements ActionListener{
private Point point = null;
public ChangeListener(Point p){
this.point=p;
}
public void actionPerformed(ActionEvent event) {
int col = table.columnAtPoint(point);
JOptionPane jop = new JOptionPane();
@SuppressWarnings("static-access")
String name = jop.showInputDialog(null, "Saisissez le nouveau nom de la colonne", "Changement de Nom", JOptionPane.QUESTION_MESSAGE);
if(name != null){
if(!name.trim().equals("")){
table.getColumnModel().getColumn(col).setHeaderValue(name);
table.getTableHeader().resizeAndRepaint();
}
}
}
}
protected class ButtonAction extends AbstractAction{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==saveButton){
datasheet.setDataAndTitle(getDataFromTable(table), getTitleFromTable(table));
}else if (e.getSource()==closeButton) {
System.out.println("close");
dispose();
}else if (e.getSource()==deleteButton){
if(DataStocked.remove(datasheet)){
dispose();
}else{
System.out.println("Remove DataSheet failed");
}
}
}
}
} |
Partager