Statistics
| Revision:

root / trunk / extensions / extArcims / src / es / prodevelop / cit / gvsig / arcims / gui / panels / utils / ServicesTableModel.java @ 8110

History | View | Annotate | Download (5.03 KB)

1
package es.prodevelop.cit.gvsig.arcims.gui.panels.utils;
2

    
3
import javax.swing.JTable;
4
import javax.swing.table.DefaultTableModel;
5

    
6
import org.apache.log4j.Logger;
7
import org.gvsig.remoteClient.arcims.exceptions.ArcImsException;
8

    
9
import com.iver.andami.PluginServices;
10

    
11
import java.util.Vector;
12

    
13
/**
14
* This is a subclass of the JTable's data model.
15
* It simply prevents cells from being edited.
16
* 
17
* @see javax.swing.table.DefaultTableModel
18
* 
19
* @author jldominguez
20
*/
21
public class ServicesTableModel extends DefaultTableModel {
22
        private static Logger logger = Logger.getLogger(ServicesTableModel.class.getName());
23
        private static final long serialVersionUID = 0;
24

    
25

    
26
        public ServicesTableModel(Vector data, Vector cols) {
27
                super(data, cols);
28
                
29
        }
30
        
31
        /**
32
        * Sets to <b>false</b> the possibility to edit any cell.
33
        *  
34
        * @return <b>false</b> (always)
35
        */
36
        public boolean isCellEditable(int x, int y) { return false; }
37
        
38
        /**
39
         * Utility method to leave <tt>n</tt> significant digits in a double
40
         * number.
41
         * 
42
         * @param d the original number
43
         * @param n the number of significant digits desired
44
         * @return the number with n significant digits
45
         */
46
        public static String leaveNDigits(double d, int n) {
47
                if (d == 0.0) return "0.0";
48
                
49
                long integ = Math.round(d);
50
                if ((d - integ) == 0) return Double.toString(d);
51
                
52
                long digitsBeforePoint = Math.round(Math.floor( 1.0 + (Math.log(Math.abs(d)) / Math.log(10.0)) ));
53
                // if (d < 0) sigDigits++;
54
                if (digitsBeforePoint >= n) {
55
                        logger.warn("Unable to round double: " + Double.toString(d));
56
                        return Double.toString(d);
57
                }
58
                
59
                double factor = Math.pow(10.0, 1.0 * (n-digitsBeforePoint));
60
                double newd = d * factor;
61
                integ = Math.round(Math.floor(newd));
62
                newd = 1.0 * integ / factor;
63
                return Double.toString(newd);
64
        }
65
        
66
        /**
67
         * Gets the value stored in a row under a certain column name.
68
         * 
69
         * @param t the JTable
70
         * @param colname the column name
71
         * @param row the row index
72
         * @return the value stored in the row under that column name
73
         */
74
        public static String getColumnValueOfRow(JTable t, String colname, int row) {
75
                for (int i=0; i<t.getColumnCount(); i++) {
76
                        if ((t.getColumnName(i).compareToIgnoreCase(colname)) == 0) {
77
                                return (String) t.getValueAt(row, i);
78
                        }
79
                }
80
                return "Not found";
81
        }
82
        
83
        /**
84
         * Gets the value stored in a row under a certain column index.
85
         * 
86
         * @param t the JTable
87
         * @param colind the column index
88
         * @param row the row index
89
         * @return the value stored in the row under that column name
90
         */
91
        public static String getColumnValueOfRowWithIndex(JTable t, int colind, int row) {
92
                return (String) t.getValueAt(row, colind);
93
//                for (int i=0; i<t.getColumnCount(); i++) {
94
//                        if ((t.getColumnName(i).compareToIgnoreCase(colname)) == 0) {
95
//                                return (String) t.getValueAt(row, i);
96
//                        }
97
//                }
98
                // return "Not found";
99
        }
100
        
101
        public static int getColumnIndex(JTable t, String colName) {
102
                int col_ind = -1;
103
                for (int i=0; i<t.getColumnCount(); i++) {
104
                        if (t.getColumnName(i).compareToIgnoreCase(colName) == 0) {
105
                                col_ind = i;
106
                                break;
107
                        }
108
                }
109
                return col_ind;
110
        }
111

    
112
        /**
113
         * Finds out the index of the first row containing a certain string under
114
         * a certain column name.
115
         * 
116
         * @param t the table
117
         * @param colName the column name
118
         * @param val the value to be searched for
119
         * @return the index of the row that contains the value
120
         * @throws ArcImsException
121
         */
122
        public static int getFirstRowWithValueInColumnName(JTable t, String colName, String val) throws ArcImsException {
123
                ArcImsException aie;
124
                
125
                int col_ind = getColumnIndex(t, colName);
126
                if (col_ind == -1) {
127
                        aie = new ArcImsException(PluginServices.getText(null, "column_not_found") + ": " + colName); 
128
                        logger.error("Column not found. ", aie);
129
                        throw aie; 
130
                }
131
                
132
                for (int i=0; i<t.getRowCount(); i++) {
133
                        if (((String) t.getValueAt(i, col_ind)).compareToIgnoreCase(val) == 0) {
134
                                return i;
135
                        }
136
                }
137
                aie = new ArcImsException(PluginServices.getText(null, "value_not_found") + ": " + val); 
138
                logger.error("Value not found in that column. ", aie);
139
                throw aie; 
140
        }
141
        
142
        /**
143
         * Finds out the index of the first row containing a certain string under
144
         * a certain column index.
145
         * 
146
         * @param t the table
147
         * @param colIndex the column index
148
         * @param val the value to be searched for
149
         * @return the index of the row that contains the value
150
         * @throws ArcImsException
151
         */
152
        public static int getFirstRowWithValueInColumnIndex(JTable t, int colIndex, String val) throws ArcImsException {
153
                ArcImsException aie;
154
                
155
                if ((colIndex < 0) || (colIndex >= t.getColumnCount())) {
156
                        aie = new ArcImsException(PluginServices.getText(null, "column_not_found") + ": " + colIndex); 
157
                        logger.error("Column not found. ", aie);
158
                        throw aie; 
159
                }
160
                
161
                for (int i=0; i<t.getRowCount(); i++) {
162
                        if (((String) t.getValueAt(i, colIndex)).compareToIgnoreCase(val) == 0) {
163
                                return i;
164
                        }
165
                }
166
                aie = new ArcImsException(PluginServices.getText(null, "value_not_found") + ": " + val); 
167
                logger.error("Value not found in that column. ", aie);
168
                throw aie; 
169
        }
170
        
171
        
172
        
173
        public void moveRow(int start,
174
            int end,
175
            int to) {
176
                
177
        }
178
        
179
}