001: package snow.sortabletable;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import javax.swing.*;
006: import javax.swing.event.*;
007: import javax.swing.table.*;
008: import java.util.*;
009:
010: public class Test extends JFrame {
011:
012: TestModel mod;
013: SortableTableModel stm;
014: JCheckBox simpleViewCB = new JCheckBox("Simple view", false);
015:
016: public Test() {
017: super ();
018: this .setTitle("Test sortable table");
019:
020: mod = new TestModel();
021: stm = new SortableTableModel(mod, 0, true);
022:
023: final JTable table = new JTable(stm) {
024: public Component _prepareEditor(TableCellEditor editor,
025: int row, int col) {
026: Component comp = editor.getTableCellEditorComponent(
027: this , "", true, row, col);
028: return comp;
029: }
030: };
031: table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
032: stm.installGUI(table);
033:
034: UniversalTableCellRenderer utr = new UniversalTableCellRenderer(
035: stm);
036: table.setDefaultRenderer(Object.class, utr);
037: table.setDefaultRenderer(Double.class, utr);
038: table.setDefaultRenderer(Integer.class, utr);
039:
040: UniversalTableEditor ute = new UniversalTableEditor();
041: table.setDefaultEditor(Object.class, ute);
042: table.setDefaultEditor(Double.class, ute);
043: table.setDefaultEditor(Integer.class, ute);
044:
045: table.setFont(new Font("Dialog", Font.PLAIN, 14));
046: table.setRowHeight(30);
047:
048: JPanel content = new JPanel(new BorderLayout());
049: setContentPane(content);
050: content.add(new JScrollPane(table), BorderLayout.CENTER);
051:
052: JPanel controlPanel = new JPanel();
053: content.add(controlPanel, BorderLayout.SOUTH);
054:
055: JButton addBT = new JButton("Add");
056: controlPanel.add(addBT);
057: addBT.addActionListener(new ActionListener() {
058: public void actionPerformed(ActionEvent e) {
059: mod.addRandom();
060: }
061: });
062:
063: JButton removeBT = new JButton("Remove Last");
064: controlPanel.add(removeBT);
065: removeBT.addActionListener(new ActionListener() {
066: public void actionPerformed(ActionEvent e) {
067: mod.removeLast();
068: }
069: });
070:
071: JButton removeSel = new JButton("Remove Selected");
072: controlPanel.add(removeSel);
073: removeSel.addActionListener(new ActionListener() {
074: public void actionPerformed(ActionEvent e) {
075: int[] sels = table.getSelectedRows();
076: // retrieve the selected items list
077: TestModelItem[] selit = new TestModelItem[sels.length];
078: for (int i = 0; i < sels.length; i++) {
079: int pos = stm
080: .getIndexInUnsortedFromTablePos(sels[i]);
081: selit[i] = mod.getItemAt(pos);
082: }
083:
084: // remove them, per reference, to avoid index iteration conflicts
085: for (int i = 0; i < sels.length; i++) {
086: mod.remove(selit[i]);
087: }
088: }
089: });
090:
091: // search
092: final MultiSearchPanel searchPanel = new MultiSearchPanel(
093: "Filter: ", null, stm);
094: JPanel wrap = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
095: //JPanel wrap = new JPanel();
096: //wrap.setLayout(new BoxLayout(wrap, BoxLayout.X_AXIS)); //new FlowLayout(FlowLayout.LEFT,0,0));
097: wrap.add(searchPanel);
098: wrap.add(Box.createHorizontalStrut(40));
099: wrap.add(simpleViewCB);
100: simpleViewCB.setOpaque(false);
101: simpleViewCB.addActionListener(new ActionListener() {
102: public void actionPerformed(ActionEvent e) {
103: if (!simpleViewCB.isSelected()) {
104: int[] allCols = new int[mod.getColumnCount()];
105: for (int i = 0; i < allCols.length; i++)
106: allCols[i] = i;
107: stm.setVisibleColumns(allCols);
108: } else {
109: stm.setVisibleColumns(new int[] { 0, 3 });
110: }
111: stm.setPreferredColumnSizesFromModel();
112: }
113: });
114:
115: add(wrap, BorderLayout.NORTH);
116:
117: this .addWindowListener(new WindowAdapter() {
118: @Override
119: public void windowClosing(WindowEvent e) {
120: terminateFrame(); // calls System.exit();
121: }
122:
123: @Override
124: public void windowClosed(WindowEvent e) {
125: terminateFrame(); // calls System.exit();
126: }
127: });
128:
129: final Dimension screen = Toolkit.getDefaultToolkit()
130: .getScreenSize();
131: this .setSize(800, 600);
132: this .setLocation(180, 120);
133: this .setVisible(true);
134:
135: // must be visible
136: EventQueue.invokeLater(new Runnable() {
137: public void run() {
138: searchPanel.getFirstTextField().requestFocus();
139: /*
140: table.setRowSelectionInterval(40, 40);
141: table.scrollRectToVisible(table.getCellRect(100, 0, true));*/
142: }
143: });
144:
145: } // Constructor
146:
147: private void terminateFrame() {
148: System.exit(0);
149: }
150:
151: public static void main(String[] arguments) {
152: JFrame.setDefaultLookAndFeelDecorated(true);
153: EventQueue.invokeLater(new Runnable() {
154: public void run() {
155: new Test();
156: }
157: });
158: }
159:
160: public static void numericalTest() {
161: // numerical test described by sun in an article, showing ULP and addition errors.
162: //
163: long t0 = System.currentTimeMillis();
164: double f = 0.0;
165: for (int i = 0; i <= 1000000000; i++) {
166: f += Math.sqrt(i);
167: }
168: System.out.println(f + ", t="
169: + (t0 - System.currentTimeMillis()) / 1000 + " sec");
170:
171: t0 = System.currentTimeMillis();
172: double newsum;
173: double sumerr = 0.0;
174: double err, sum = 0;
175: for (int i = 1; i <= 1000000000; i++) {
176: f = Math.sqrt(i);
177: newsum = sum + f;
178: err = (newsum - sum) - f;
179: sumerr += err;
180: sum = newsum;
181: }
182: System.out.println((sum - sumerr) + ", t="
183: + (t0 - System.currentTimeMillis()) / 1000 + " sec");
184:
185: } // main
186:
187: }
|