Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.image.extension / src / main / java / org / gvsig / arcims / image / gui / panels / utils / ServicesTableModel.java @ 32321

History | View | Annotate | Download (6.85 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2010 Prodevelop S.L. main development
26
 * http://www.prodevelop.es
27
 */
28

    
29
package org.gvsig.arcims.image.gui.panels.utils;
30

    
31

    
32
import java.util.Vector;
33

    
34
import javax.swing.JTable;
35
import javax.swing.table.DefaultTableModel;
36

    
37
import org.slf4j.Logger;
38
import org.slf4j.LoggerFactory;
39
import org.gvsig.andami.PluginServices;
40
import org.gvsig.remoteclient.arcims.exceptions.ArcImsException;
41

    
42

    
43
/**
44
* This is a subclass of the JTable's data model.
45
* It simply prevents cells from being edited.
46
*
47
* @see javax.swing.table.DefaultTableModel
48
*
49
* @author jldominguez
50
*/
51
public class ServicesTableModel extends DefaultTableModel {
52
    private static Logger logger = LoggerFactory.getLogger(ServicesTableModel.class.getName());
53
    private static final long serialVersionUID = 0;
54

    
55
        public ServicesTableModel(Vector data, Vector cols) {
56
        super(data, cols);
57
    }
58

    
59
    /**
60
    * Sets to <b>false</b> the possibility to edit any cell.
61
    *
62
    * @return <b>false</b> (always)
63
    */
64
    public boolean isCellEditable(int x, int y) {
65
        return false;
66
    }
67

    
68
    /**
69
     * Utility method to leave <tt>n</tt> significant digits in a double
70
     * number.
71
     *
72
     * @param d the original number
73
     * @param n the number of significant digits desired
74
     * @return the number with n significant digits
75
     */
76
    public static String leaveNDigits(double d, int n) {
77
        if (d == 0.0) {
78
            return "0.0";
79
        }
80

    
81
        long integ = Math.round(d);
82

    
83
        if ((d - integ) == 0) {
84
            return Double.toString(d);
85
        }
86

    
87
        long digitsBeforePoint = Math.round(Math.floor(1.0 +
88
                    (Math.log(Math.abs(d)) / Math.log(10.0))));
89

    
90
        // if (d < 0) sigDigits++;
91
        if (digitsBeforePoint >= n) {
92
            logger.warn("Unable to round double: " + Double.toString(d));
93

    
94
            return Double.toString(d);
95
        }
96

    
97
        double factor = Math.pow(10.0, 1.0 * (n - digitsBeforePoint));
98
        double newd = d * factor;
99
        integ = Math.round(Math.floor(newd));
100
        newd = (1.0 * integ) / factor;
101

    
102
        return Double.toString(newd);
103
    }
104

    
105
    /**
106
     * Gets the value stored in a row under a certain column name.
107
     *
108
     * @param t the JTable
109
     * @param colname the column name
110
     * @param row the row index
111
     * @return the value stored in the row under that column name
112
     */
113
    public static String getColumnValueOfRow(JTable t, String colname, int row) {
114
        for (int i = 0; i < t.getColumnCount(); i++) {
115
            if ((t.getColumnName(i).compareToIgnoreCase(colname)) == 0) {
116
                return (String) t.getValueAt(row, i);
117
            }
118
        }
119

    
120
        return "Not found";
121
    }
122

    
123
    /**
124
     * Gets the value stored in a row under a certain column index.
125
     *
126
     * @param t the JTable
127
     * @param colind the column index
128
     * @param row the row index
129
     * @return the value stored in the row under that column name
130
     */
131
    public static String getColumnValueOfRowWithIndex(JTable t, int colind,
132
        int row) {
133
        return (String) t.getValueAt(row, colind);
134

    
135
        //                for (int i=0; i<t.getColumnCount(); i++) {
136
        //                        if ((t.getColumnName(i).compareToIgnoreCase(colname)) == 0) {
137
        //                                return (String) t.getValueAt(row, i);
138
        //                        }
139
        //                }
140
        // return "Not found";
141
    }
142

    
143
    public static int getColumnIndex(JTable t, String colName) {
144
        int col_ind = -1;
145

    
146
        for (int i = 0; i < t.getColumnCount(); i++) {
147
            if (t.getColumnName(i).compareToIgnoreCase(colName) == 0) {
148
                col_ind = i;
149

    
150
                break;
151
            }
152
        }
153

    
154
        return col_ind;
155
    }
156

    
157
    /**
158
     * Finds out the index of the first row containing a certain string under
159
     * a certain column name.
160
     *
161
     * @param t the table
162
     * @param colName the column name
163
     * @param val the value to be searched for
164
     * @return the index of the row that contains the value
165
     * @throws ArcImsException
166
     */
167
    public static int getFirstRowWithValueInColumnName(JTable t,
168
        String colName, String val) throws ArcImsException {
169
        ArcImsException aie;
170

    
171
        int col_ind = getColumnIndex(t, colName);
172

    
173
        if (col_ind == -1) {
174
            aie = new ArcImsException(PluginServices.getText(null,
175
                        "column_not_found") + ": " + colName);
176
            logger.error("Column not found. ", aie);
177
            throw aie;
178
        }
179

    
180
        for (int i = 0; i < t.getRowCount(); i++) {
181
            if (((String) t.getValueAt(i, col_ind)).compareToIgnoreCase(val) == 0) {
182
                return i;
183
            }
184
        }
185

    
186
        aie = new ArcImsException(PluginServices.getText(null, "value_not_found") +
187
                ": " + val);
188
        logger.error("Value not found in that column. ", aie);
189
        throw aie;
190
    }
191

    
192
    /**
193
     * Finds out the index of the first row containing a certain string under
194
     * a certain column index.
195
     *
196
     * @param t the table
197
     * @param colIndex the column index
198
     * @param val the value to be searched for
199
     * @return the index of the row that contains the value
200
     * @throws ArcImsException
201
     */
202
    public static int getFirstRowWithValueInColumnIndex(JTable t, int colIndex,
203
        String val) throws ArcImsException {
204
        ArcImsException aie;
205

    
206
        if ((colIndex < 0) || (colIndex >= t.getColumnCount())) {
207
            aie = new ArcImsException(PluginServices.getText(null,
208
                        "column_not_found") + ": " + colIndex);
209
            logger.error("Column not found. ", aie);
210
            throw aie;
211
        }
212

    
213
        for (int i = 0; i < t.getRowCount(); i++) {
214
            if (((String) t.getValueAt(i, colIndex)).compareToIgnoreCase(val) == 0) {
215
                return i;
216
            }
217
        }
218

    
219
        aie = new ArcImsException(PluginServices.getText(null, "value_not_found") +
220
                ": " + val);
221
        logger.error("Value not found in that column. ", aie);
222
        throw aie;
223
    }
224

    
225
    public void moveRow(int start, int end, int to) {
226
    }
227
}