Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.geocoding.extension / src / org / gvsig / geocoding / utils / GeocodingUtils.java @ 32526

History | View | Annotate | Download (5.22 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
 * 2009 Prodevelop S.L  vsanjaime   programador
26
 */
27

    
28
package org.gvsig.geocoding.utils;
29

    
30
import java.awt.Font;
31
import java.io.File;
32

    
33
import javax.swing.border.TitledBorder;
34

    
35
import org.gvsig.andami.PluginServices;
36
import org.gvsig.andami.ui.mdiManager.IWindow;
37
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel;
38
import org.gvsig.app.project.documents.view.gui.IView;
39
import org.gvsig.fmap.dal.DataStore;
40
import org.gvsig.fmap.mapcontext.layers.FLayer;
41
import org.gvsig.fmap.mapcontext.layers.FLayers;
42
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
43
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer;
44
import org.gvsig.geocoding.pattern.GeocodingPattern;
45

    
46
/**
47
 * Geocoding utilities
48
 * 
49
 * @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
50
 * @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
51
 */
52
public class GeocodingUtils {
53

    
54
        /**
55
         * Add to the file name the suffix .xml
56
         * 
57
         * @param file
58
         * @return
59
         */
60
        public static File addXMLSuffix(File file) {
61
                if (!(file.getPath().toLowerCase().endsWith(".xml"))) {
62
                        return new File(file.getPath() + ".xml");
63
                }
64
                return file;
65
        }
66

    
67
        /**
68
         * Add to the file name the suffix .dbf
69
         * 
70
         * @param file
71
         * @return
72
         */
73
        public static File addDBFSuffix(File file) {
74
                if (!(file.getPath().toLowerCase().endsWith(".dbf"))) {
75
                        return new File(file.getPath() + ".dbf");
76
                }
77
                return file;
78
        }
79

    
80
        /**
81
         * Add to the file name the suffix .dbf
82
         * 
83
         * @param file
84
         * @return
85
         */
86
        public static File addSuffixFile(File file, String suffix) {
87
                File nfile = new File(file.getPath() + "." + suffix);
88
                return nfile;
89
        }
90

    
91
        /**
92
         * Add to the file name the suffix .dbf
93
         * 
94
         * @param file
95
         * @return
96
         */
97
        public static File addSHPSuffix(File file) {
98
                if (!(file.getPath().toLowerCase().endsWith(".shp"))) {
99
                        return new File(file.getPath() + ".shp");
100
                }
101
                return file;
102
        }
103

    
104
        /**
105
         * Delete to the file name the suffix .dbf
106
         * 
107
         * @param file
108
         * @return
109
         */
110
        public static File delSHPSuffix(File file) {
111
                if ((file.getPath().toLowerCase().endsWith(".shp"))) {
112
                        String path = file.getPath();
113
                        int npath = path.length();
114
                        String subfile = file.getPath().substring(0, npath - 3);
115
                        return new File(subfile);
116
                }
117
                return file;
118
        }
119

    
120
        /**
121
         * @param name
122
         *            of the layer
123
         * @param font
124
         *            for the border title
125
         * @return a titled border for the layer name
126
         */
127
        public static TitledBorder getTitledBorder(String name, Font titleFont) {
128
                return javax.swing.BorderFactory.createTitledBorder(null, name,
129
                                javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
130
                                javax.swing.border.TitledBorder.DEFAULT_POSITION, titleFont,
131
                                null);
132
        }
133

    
134
        /**
135
         * @param name
136
         *            of the layer
137
         * @return a titled border for the layer name
138
         */
139
        public static TitledBorder getTitledBorder(String name) {
140
                return GeocodingUtils.getTitledBorder(name, null);
141
        }
142

    
143
        // /**
144
        // * Clear all rows of the TableModel
145
        // *
146
        // * @param model
147
        // */
148
        // public static void clearTableModel(DefaultTableModel model) {
149
        // int n = model.getRowCount();
150
        // for (int i = n - 1; i > -1; i--) {
151
        // model.removeRow(i);
152
        // }
153
        // }
154
        
155
        /**
156
         * Get layer datastore from view
157
         * @param pat
158
         * @return layer datastore
159
         */
160
        public static DataStore searchLayerInCurrentView(GeocodingPattern pat)
161
                        throws GeocodingNoDataStoreException {
162
                String name = pat.getDataSource().getLayerName();
163
                String provi = pat.getDataSource().getLayerProvider();
164

    
165
                boolean ok = true;
166

    
167
                IWindow[] wins = PluginServices.getMDIManager().getOrderedWindows();
168
                for (IWindow win : wins) {
169
                        if (win instanceof IView) {
170
                                FLayers lays = ((DefaultViewPanel) win).getMapControl().getMapContext()
171
                                                .getLayers();
172
                                for (int i = 0; i < lays.getLayersCount(); i++) {
173
                                        FLayer ly = lays.getLayer(i);
174
                                        if (ly instanceof FLyrVect
175
                                                        && ly.getName().compareTo(name) == 0) {
176
                                                DataStore store = ((FLyrVect) ly).getDataStore();
177
                                                if (provi.compareTo(store.getParameters()
178
                                                                .getDescription()) == 0) {
179
                                                        return store;
180
                                                }
181
                                        }
182
                                }
183
                        }
184
                }
185
                ok = false;
186
                if (!ok) {
187
                        throw new GeocodingNoDataStoreException();
188
                }
189

    
190
                return null;
191
        }
192
        
193

    
194

    
195
}