Statistics
| Revision:

svn-gvsig-desktop / tags / v1_11_0_Build_1306 / extensions / extArcims / src / es / prodevelop / cit / gvsig / arcims / gui / panels / utils / ServicesTableModel.java @ 35731

History | View | Annotate | Download (7.12 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Prodevelop and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *   Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *   +34 963862235
28
 *   gvsig@gva.es
29
 *   www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   Prodevelop Integraci?n de Tecnolog?as SL
34
 *   Conde Salvatierra de ?lava , 34-10
35
 *   46004 Valencia
36
 *   Spain
37
 *
38
 *   +34 963 510 612
39
 *   +34 963 510 968
40
 *   gis@prodevelop.es
41
 *   http://www.prodevelop.es
42
 */
43
package es.prodevelop.cit.gvsig.arcims.gui.panels.utils;
44

    
45
import com.iver.andami.PluginServices;
46

    
47
import org.apache.log4j.Logger;
48

    
49
import org.gvsig.remoteClient.arcims.exceptions.ArcImsException;
50

    
51
import java.util.Vector;
52

    
53
import javax.swing.JTable;
54
import javax.swing.table.DefaultTableModel;
55

    
56

    
57
/**
58
* This is a subclass of the JTable's data model.
59
* It simply prevents cells from being edited.
60
*
61
* @see javax.swing.table.DefaultTableModel
62
*
63
* @author jldominguez
64
*/
65
public class ServicesTableModel extends DefaultTableModel {
66
    private static Logger logger = Logger.getLogger(ServicesTableModel.class.getName());
67
    private static final long serialVersionUID = 0;
68

    
69
    public ServicesTableModel(Vector data, Vector cols) {
70
        super(data, cols);
71
    }
72

    
73
    /**
74
    * Sets to <b>false</b> the possibility to edit any cell.
75
    *
76
    * @return <b>false</b> (always)
77
    */
78
    public boolean isCellEditable(int x, int y) {
79
        return false;
80
    }
81

    
82
    /**
83
     * Utility method to leave <tt>n</tt> significant digits in a double
84
     * number.
85
     *
86
     * @param d the original number
87
     * @param n the number of significant digits desired
88
     * @return the number with n significant digits
89
     */
90
    public static String leaveNDigits(double d, int n) {
91
        if (d == 0.0) {
92
            return "0.0";
93
        }
94

    
95
        long integ = Math.round(d);
96

    
97
        if ((d - integ) == 0) {
98
            return Double.toString(d);
99
        }
100

    
101
        long digitsBeforePoint = Math.round(Math.floor(1.0 +
102
                    (Math.log(Math.abs(d)) / Math.log(10.0))));
103

    
104
        // if (d < 0) sigDigits++;
105
        if (digitsBeforePoint >= n) {
106
            logger.warn("Unable to round double: " + Double.toString(d));
107

    
108
            return Double.toString(d);
109
        }
110

    
111
        double factor = Math.pow(10.0, 1.0 * (n - digitsBeforePoint));
112
        double newd = d * factor;
113
        integ = Math.round(Math.floor(newd));
114
        newd = (1.0 * integ) / factor;
115

    
116
        return Double.toString(newd);
117
    }
118

    
119
    /**
120
     * Gets the value stored in a row under a certain column name.
121
     *
122
     * @param t the JTable
123
     * @param colname the column name
124
     * @param row the row index
125
     * @return the value stored in the row under that column name
126
     */
127
    public static String getColumnValueOfRow(JTable t, String colname, int row) {
128
        for (int i = 0; i < t.getColumnCount(); i++) {
129
            if ((t.getColumnName(i).compareToIgnoreCase(colname)) == 0) {
130
                return (String) t.getValueAt(row, i);
131
            }
132
        }
133

    
134
        return "Not found";
135
    }
136

    
137
    /**
138
     * Gets the value stored in a row under a certain column index.
139
     *
140
     * @param t the JTable
141
     * @param colind the column index
142
     * @param row the row index
143
     * @return the value stored in the row under that column name
144
     */
145
    public static String getColumnValueOfRowWithIndex(JTable t, int colind,
146
        int row) {
147
        return (String) t.getValueAt(row, colind);
148

    
149
        //                for (int i=0; i<t.getColumnCount(); i++) {
150
        //                        if ((t.getColumnName(i).compareToIgnoreCase(colname)) == 0) {
151
        //                                return (String) t.getValueAt(row, i);
152
        //                        }
153
        //                }
154
        // return "Not found";
155
    }
156

    
157
    public static int getColumnIndex(JTable t, String colName) {
158
        int col_ind = -1;
159

    
160
        for (int i = 0; i < t.getColumnCount(); i++) {
161
            if (t.getColumnName(i).compareToIgnoreCase(colName) == 0) {
162
                col_ind = i;
163

    
164
                break;
165
            }
166
        }
167

    
168
        return col_ind;
169
    }
170

    
171
    /**
172
     * Finds out the index of the first row containing a certain string under
173
     * a certain column name.
174
     *
175
     * @param t the table
176
     * @param colName the column name
177
     * @param val the value to be searched for
178
     * @return the index of the row that contains the value
179
     * @throws ArcImsException
180
     */
181
    public static int getFirstRowWithValueInColumnName(JTable t,
182
        String colName, String val) throws ArcImsException {
183
        ArcImsException aie;
184

    
185
        int col_ind = getColumnIndex(t, colName);
186

    
187
        if (col_ind == -1) {
188
            aie = new ArcImsException(PluginServices.getText(null,
189
                        "column_not_found") + ": " + colName);
190
            logger.error("Column not found. ", aie);
191
            throw aie;
192
        }
193

    
194
        for (int i = 0; i < t.getRowCount(); i++) {
195
            if (((String) t.getValueAt(i, col_ind)).compareToIgnoreCase(val) == 0) {
196
                return i;
197
            }
198
        }
199

    
200
        aie = new ArcImsException(PluginServices.getText(null, "value_not_found") +
201
                ": " + val);
202
        logger.error("Value not found in that column. ", aie);
203
        throw aie;
204
    }
205

    
206
    /**
207
     * Finds out the index of the first row containing a certain string under
208
     * a certain column index.
209
     *
210
     * @param t the table
211
     * @param colIndex the column index
212
     * @param val the value to be searched for
213
     * @return the index of the row that contains the value
214
     * @throws ArcImsException
215
     */
216
    public static int getFirstRowWithValueInColumnIndex(JTable t, int colIndex,
217
        String val) throws ArcImsException {
218
        ArcImsException aie;
219

    
220
        if ((colIndex < 0) || (colIndex >= t.getColumnCount())) {
221
            aie = new ArcImsException(PluginServices.getText(null,
222
                        "column_not_found") + ": " + colIndex);
223
            logger.error("Column not found. ", aie);
224
            throw aie;
225
        }
226

    
227
        for (int i = 0; i < t.getRowCount(); i++) {
228
            if (((String) t.getValueAt(i, colIndex)).compareToIgnoreCase(val) == 0) {
229
                return i;
230
            }
231
        }
232

    
233
        aie = new ArcImsException(PluginServices.getText(null, "value_not_found") +
234
                ": " + val);
235
        logger.error("Value not found in that column. ", aie);
236
        throw aie;
237
    }
238

    
239
    public void moveRow(int start, int end, int to) {
240
    }
241
}