1 package edu.psu.geovista.app.table;
2 import java.awt.*;
3 import java.awt.event.*;
4 import java.util.*;
5 import javax.swing.*;
6 import javax.swing.event.*;
7 import javax.swing.table.*;
8
9
10 public class MxmTableModel extends DefaultTableModel {
11 private int indexes[];
12 private String myDomain=null;
13 private int errorIndex=0;
14
15 public MxmTableModel(Vector data, Vector columnNames)
16 {
17 super(data,columnNames);
18 allocate();
19 }
20
21
22 public MxmTableModel()
23 {
24 super();
25 }
26
27
28 public MxmTableModel(Vector columnNames, int numRows)
29 {
30 super(columnNames,numRows);
31 allocate();
32 }
33
34 public MxmTableModel(Object[] columnNames, int numRows)
35 {
36 super(columnNames,numRows);
37 allocate();
38 }
39
40 public MxmTableModel(Object[][] data, Object[] columnNames)
41 {
42 super(data,columnNames);
43 allocate();
44 }
45 public void insertRow( int row, Vector rowData )
46 {
47 super.insertRow( row, rowData );
48 }
49
50
51
52 /*public Object getValueAt(int row, int column) {
53 return super.getValueAt(indexes[row], column);
54 }*/
55 public void setValueAt(Object aValue, int row, int column) {
56 super.setValueAt(aValue, indexes[row], column);
57 }
58 public void tableChanged(TableModelEvent e) {
59 allocate();
60 }
61 public void sort(int column,String myIndex,String domain) {
62 int rowCount = super.getRowCount();
63 myDomain=domain;
64
65 if(myIndex.equals("ASC"))
66 {
67
68 for(int i=0; i < rowCount; i++) {
69 for(int j = i+1; j < rowCount; j++) {
70
71 if(compare(indexes[i], indexes[j], column) < 0 && (errorIndex==0))
72 {
73 swap(i,j);
74 }
75 if(errorIndex==1) break;
76 }
77 if(errorIndex==1) break;
78 }//end of for loop
79 if(errorIndex==1)
80 {
81 JOptionPane.showMessageDialog(null,"You should choose sort by string");
82 errorIndex=0;
83 }
84
85 }
86 else if(myIndex.equals("DES"))
87 {
88
89 for(int i=0; i < rowCount; i++) {
90 for(int j = i+1; j < rowCount; j++) {
91
92 if(compare(indexes[i], indexes[j], column) > 0) {
93 if(errorIndex==1) break;
94 swap(i,j);
95 }
96 if(errorIndex==1) break;
97 }
98 if(errorIndex==1) break;
99 }//end of for loop
100 if(errorIndex==1)
101 {
102 JOptionPane.showMessageDialog(null,"You should choose sort by string");
103 errorIndex=0;
104
105 }
106
107
108 }
109
110
111 }
112 public void swap(int i, int j) {
113 int tmp = indexes[i];
114 indexes[i] = indexes[j];
115 indexes[j] = tmp;
116 }
117 public int compare(int i, int j, int column) {
118 Object io = super.getValueAt(i,column);
119 Object jo = super.getValueAt(j,column);
120 float c=(float)0.0;
121 if(myDomain.equals("STR"))
122 {
123
124 c = jo.toString().compareTo(io.toString());
125
126 }
127 else if(myDomain.equals("NUM"))
128 {
129 float firstFloat=(float)0.0;
130 float secondFloat=(float)0.0;
131 try
132 {
133 firstFloat=Float.parseFloat(io.toString());
134 secondFloat=Float.parseFloat(jo.toString());
135 }
136 catch(Exception e3)
137 {
138
139 errorIndex=1;
140 }
141 c=secondFloat-firstFloat;
142
143
144 }
145 return (c < 0) ? -1 : ((c > 0) ? 1 : 0);
146 }
147 private void allocate() {
148 indexes = new int[getRowCount()];
149
150 for(int i=0; i < indexes.length; ++i) {
151 indexes[i] = i;
152 }
153 }
154 public Class getColumnClass(int c)
155 {
156 return getValueAt(0, c).getClass();
157 }
158
159 }
This page was automatically generated by Maven