svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.newlayer / org.gvsig.newlayer.prov / org.gvsig.newlayer.prov.file / src / main / java / org / gvsig / newlayer / prov / file / AbstractNewLayerFileProvider.java @ 40560
History | View | Annotate | Download (7.28 KB)
1 | 40560 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40560 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | *
|
||
6 | 40435 | jjdelcerro | * This program is free software; you can redistribute it and/or
|
7 | * modify it under the terms of the GNU General Public License
|
||
8 | 40560 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * of the License, or (at your option) any later version.
|
10 | 40560 | jjdelcerro | *
|
11 | 40435 | jjdelcerro | * 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 | 40560 | jjdelcerro | *
|
16 | 40435 | jjdelcerro | * 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 | 40560 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | 40435 | jjdelcerro | * MA 02110-1301, USA.
|
20 | 40560 | jjdelcerro | *
|
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | package org.gvsig.newlayer.prov.file; |
||
25 | |||
26 | import java.io.File; |
||
27 | import java.util.ArrayList; |
||
28 | import java.util.List; |
||
29 | |||
30 | import org.slf4j.Logger; |
||
31 | import org.slf4j.LoggerFactory; |
||
32 | |||
33 | import org.gvsig.fmap.dal.DALLocator; |
||
34 | import org.gvsig.fmap.dal.DataManager; |
||
35 | import org.gvsig.fmap.dal.DataServerExplorerParameters; |
||
36 | import org.gvsig.fmap.dal.DataStoreParameters; |
||
37 | import org.gvsig.fmap.dal.exception.DataException; |
||
38 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
39 | import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
||
40 | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
||
41 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
42 | import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
||
43 | import org.gvsig.newlayer.NewLayerProviderPanel; |
||
44 | import org.gvsig.newlayer.NewLayerService; |
||
45 | import org.gvsig.newlayer.spi.AbstractNewLayerProvider; |
||
46 | import org.gvsig.tools.exception.BaseRuntimeException; |
||
47 | |||
48 | /**
|
||
49 | * NewLayer provider to create a shape file.
|
||
50 | *
|
||
51 | * @author gvSIG Team
|
||
52 | * @version $Id$
|
||
53 | */
|
||
54 | public abstract class AbstractNewLayerFileProvider extends AbstractNewLayerProvider { |
||
55 | protected static final Logger LOG = |
||
56 | LoggerFactory.getLogger(AbstractNewLayerFileProvider.class); |
||
57 | |||
58 | protected List<NewLayerProviderPanel> panels = new ArrayList(); |
||
59 | protected NewLayerFileProviderPanel fileProviderPanel = null; |
||
60 | protected NewFeatureStoreParameters storeParameters = null; |
||
61 | |||
62 | protected AbstractNewLayerFileProvider(NewLayerService service) {
|
||
63 | super(service);
|
||
64 | fileProviderPanel = new NewLayerFileProviderPanel(this); |
||
65 | panels.add(fileProviderPanel); |
||
66 | } |
||
67 | |||
68 | public List<NewLayerProviderPanel> getPanels() { |
||
69 | return panels;
|
||
70 | } |
||
71 | |||
72 | public EditableFeatureType getFeatureType() {
|
||
73 | return this.getNewStoreParameters().getDefaultFeatureType(); |
||
74 | } |
||
75 | |||
76 | protected abstract String getFileParameterName(); |
||
77 | |||
78 | protected abstract String getFileExtension(); |
||
79 | |||
80 | public NewFeatureStoreParameters getNewStoreParameters() {
|
||
81 | DataManager dataManager = DALLocator.getDataManager(); |
||
82 | |||
83 | try {
|
||
84 | if (this.explorer == null){ |
||
85 | DataServerExplorerParameters explorerParameters = |
||
86 | dataManager.createServerExplorerParameters("FilesystemExplorer");
|
||
87 | this.explorer =
|
||
88 | dataManager.openServerExplorer("FilesystemExplorer", explorerParameters);
|
||
89 | } |
||
90 | |||
91 | File selectedFile = fileProviderPanel.getSelectedFile();
|
||
92 | String fileName = selectedFile.getName();
|
||
93 | if (fileName.indexOf(".") < 0){ |
||
94 | fileName = fileName + "." + this.getFileExtension(); |
||
95 | selectedFile = new File(selectedFile.getParent() + File.separator + fileName); |
||
96 | } |
||
97 | |||
98 | //If the file has not been updated, returns the same parameters
|
||
99 | if (this.storeParameters != null){ |
||
100 | File previousFile = (File)storeParameters.getDynValue(getFileParameterName()); |
||
101 | if (previousFile.equals(selectedFile)){
|
||
102 | return storeParameters;
|
||
103 | } |
||
104 | } |
||
105 | |||
106 | storeParameters = |
||
107 | (NewFeatureStoreParameters) explorer.getAddParameters(this.getStoreName());
|
||
108 | storeParameters.setDynValue( |
||
109 | getFileParameterName(), selectedFile); |
||
110 | return storeParameters;
|
||
111 | } catch (InitializeException e) {
|
||
112 | LOG.error("Can't initialize store parameters.");
|
||
113 | throw new CantInitializeStoreParametersException(e); |
||
114 | } catch (ProviderNotRegisteredException e) {
|
||
115 | LOG.error("Can't create store parameters, provider not registered.");
|
||
116 | throw new CantCreateStoreParametersException(e); |
||
117 | } catch (DataException e) {
|
||
118 | LOG.error("Can't create store parameters, provider not registered.");
|
||
119 | throw new CantCreateStoreParametersException(e); |
||
120 | } catch (ValidateDataParametersException e) {
|
||
121 | LOG.error("Can't create the explorter.");
|
||
122 | throw new CantCreateExplorerException(e); |
||
123 | } |
||
124 | } |
||
125 | |||
126 | public DataStoreParameters getOpenStoreParameters() {
|
||
127 | DataManager dataManager = DALLocator.getDataManager(); |
||
128 | |||
129 | try {
|
||
130 | File selectedFile = fileProviderPanel.getSelectedFile();
|
||
131 | String fileName = selectedFile.getName();
|
||
132 | if (fileName.indexOf(".") < 0) { |
||
133 | fileName = fileName + "." + this.getFileExtension(); |
||
134 | selectedFile = new File(selectedFile.getParent() |
||
135 | + File.separator + fileName);
|
||
136 | } |
||
137 | |||
138 | DataStoreParameters storeParameters = (DataStoreParameters) dataManager |
||
139 | .createStoreParameters(this.getStoreName());
|
||
140 | storeParameters.setDynValue(getFileParameterName(), selectedFile); |
||
141 | return storeParameters;
|
||
142 | } catch (InitializeException e) {
|
||
143 | throw new CantInitializeStoreParametersException(e); |
||
144 | } catch (ProviderNotRegisteredException e) {
|
||
145 | throw new CantCreateStoreParametersException(e); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | class CantCreateExplorerException extends BaseRuntimeException { |
||
150 | private static final long serialVersionUID = -3487961532247258393L; |
||
151 | private final static String MESSAGE_FORMAT = "Can't create explorer."; |
||
152 | private final static String MESSAGE_KEY = "_Cant_create_explorer"; |
||
153 | |||
154 | public CantCreateExplorerException(Throwable cause) { |
||
155 | super(MESSAGE_FORMAT, cause, MESSAGE_KEY, serialVersionUID);
|
||
156 | } |
||
157 | } |
||
158 | |||
159 | class CantCreateStoreParametersException extends BaseRuntimeException { |
||
160 | private static final long serialVersionUID = -3487961532247258393L; |
||
161 | private final static String MESSAGE_FORMAT = "Can't create store parameters, provider not registered."; |
||
162 | private final static String MESSAGE_KEY = "_Cant_create_store_parameters_provider_not_registered"; |
||
163 | |||
164 | public CantCreateStoreParametersException(Throwable cause) { |
||
165 | super(MESSAGE_FORMAT, cause, MESSAGE_KEY, serialVersionUID);
|
||
166 | } |
||
167 | } |
||
168 | |||
169 | class CantInitializeStoreParametersException extends BaseRuntimeException { |
||
170 | private static final long serialVersionUID = -5263149018209261288L; |
||
171 | private final static String MESSAGE_FORMAT = "Can't initialize store parameters."; |
||
172 | private final static String MESSAGE_KEY = "_Cant_initialize_store_parameters"; |
||
173 | |||
174 | public CantInitializeStoreParametersException(Throwable cause) { |
||
175 | super(MESSAGE_FORMAT, cause, MESSAGE_KEY, serialVersionUID);
|
||
176 | } |
||
177 | } |
||
178 | |||
179 | } |