src/org/math/plot/MatrixTablePanel.java

00001 package org.math.plot;
00002 
00003 import java.awt.*;
00004 import java.awt.datatransfer.*;
00005 import java.io.*;
00006 
00007 import javax.swing.*;
00008 import javax.swing.table.*;
00009 
00010 import org.math.io.files.*;
00011 import org.math.plot.utils.*;
00012 
00018 public class MatrixTablePanel extends DataPanel {
00019 
00020         private static final long serialVersionUID = 1L;
00021 
00022         private JTable table;
00023 
00024         private TableModel model;
00025 
00026         private Object[][] M;
00027 
00028         private boolean viewHeaders = false;
00029 
00030         private String[] headers;
00031 
00032         public MatrixTablePanel(Object[][] m) {
00033                 super();
00034                 M = m;
00035                 if (M.length == 0)
00036                         headers = new String[0];
00037                 else
00038                         headers = new String[M[0].length];
00039                 setModel();
00040                 toWindow();
00041         }
00042 
00043         public void toClipBoard() {
00044                 try {
00045                         Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(Array.cat(M)), null);
00046                 } catch (IllegalStateException e) {
00047                         JOptionPane.showConfirmDialog(null, "Copy to clipboard failed : " + e.getMessage(), "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
00048                 }
00049         }
00050 
00051         public String getText() {
00052                 return M.toString();
00053         }
00054 
00055         public void toASCIIFile(File file) {
00056                 try {
00057                         ASCIIFile.write(file, Array.cat(M));
00058                 } catch (NullPointerException e) {
00059                         // System.out.println("File not saved");
00060                 }
00061         }
00062 
00063         private void setModel() {
00064                 /*Double[][] array = null;
00065                 if (M.length != 0) {
00066                         array = new Double[M.length][M[0].length];
00067                         for (int i = 0; i < array.length; i++) {
00068                                 for (int j = 0; j < array[i].length; j++) {
00069                                         array[i][j] = new Double(M[i][j]);
00070                                 }
00071                         }
00072                 } else
00073                         array = new Double[0][0];*/
00074 
00075                 model = new DefaultTableModel(M, headers) {
00076                         private static final long serialVersionUID = 1L;
00077                         @Override
00078                         public boolean isCellEditable(int row, int column) {
00079                                 return false;
00080                         }
00081                 };
00082 
00083         }
00084 
00085         public void setHeaders(String[] h) {
00086                 if (M.length != 0)
00087                         if (h.length != M[0].length) {
00088                                 throw new IllegalArgumentException("Headers of the table must have " + M[0].length + " elements.");
00089                         }
00090 
00091                 headers = h;
00092                 update();
00093         }
00094 
00095         public void update() {
00096                 setModel();
00097                 super.update();
00098         }
00099 
00100         public void setMatrix(Object[][] m) {
00101                 M = m;
00102 
00103                 if (M.length == 0)
00104                         headers = new String[0];
00105                 else
00106                         headers = new String[M[0].length];
00107 
00108                 update();
00109         }
00110 
00111         protected void toWindow() {
00112                 table = new JTable(model);
00113 
00114                 if (!viewHeaders) {
00115                         table.setTableHeader(null);
00116                 }
00117 
00118                 table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
00119                 // table.setCellSelectionEnabled(true);
00120                 table.setEnabled(false);
00121 
00122                 scrollPane = new JScrollPane(table);
00123 
00124                 /*
00125                  * scrollPane.setPreferredSize(getSize());
00126                  * scrollPane.setSize(getSize());
00127                  */
00128 
00129                 add(scrollPane, BorderLayout.CENTER);
00130         }
00131 
00132         public Object[][] getMatrix() {
00133                 return M;
00134         }
00135 }

Generated on Wed Sep 5 21:44:01 2007 for jmathplot by  doxygen 1.5.1