root / tags / v2_0_0_Build_2050 / libraries / org.gvsig.newlayer / org.gvsig.newlayer.lib / org.gvsig.newlayer.lib.impl / src / main / java / org / gvsig / newlayer / impl / DefaultNewLayerService.java @ 38695
History | View | Annotate | Download (7.26 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 |
package org.gvsig.newlayer.impl; |
23 |
|
24 |
import java.util.ArrayList; |
25 |
import java.util.Iterator; |
26 |
import java.util.List; |
27 |
|
28 |
import org.gvsig.fmap.dal.DALLocator; |
29 |
import org.gvsig.fmap.dal.DataManager; |
30 |
import org.gvsig.fmap.dal.DataServerExplorer; |
31 |
import org.gvsig.fmap.dal.DataStore; |
32 |
import org.gvsig.fmap.dal.DataStoreParameters; |
33 |
import org.gvsig.fmap.dal.exception.DataException; |
34 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
35 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
36 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
37 |
import org.gvsig.fmap.mapcontext.MapContext; |
38 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
39 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
40 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
41 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
42 |
import org.gvsig.newlayer.NewLayerManager; |
43 |
import org.gvsig.newlayer.NewLayerProvider; |
44 |
import org.gvsig.newlayer.NewLayerProviderFactory; |
45 |
import org.gvsig.newlayer.NewLayerService; |
46 |
import org.gvsig.newlayer.NewLayerServiceException; |
47 |
import org.gvsig.tools.service.ServiceException; |
48 |
|
49 |
/**
|
50 |
* Default {@link NewLayerService} implementation.
|
51 |
*
|
52 |
* @author gvSIG Team
|
53 |
* @version $Id$
|
54 |
*/
|
55 |
public class DefaultNewLayerService implements NewLayerService { |
56 |
|
57 |
private NewLayerManager newLayerManager;
|
58 |
private NewLayerProviderFactory type;
|
59 |
private NewLayerProvider provider;
|
60 |
private boolean addLayerToView = true; |
61 |
private MapContext mapContext;
|
62 |
|
63 |
/**
|
64 |
* {@link DefaultNewLayerService} constructor
|
65 |
*/
|
66 |
public DefaultNewLayerService(NewLayerManager manager, MapContext mapContext) {
|
67 |
this.newLayerManager = manager;
|
68 |
this.mapContext = mapContext;
|
69 |
} |
70 |
|
71 |
public NewLayerProvider getProvider() {
|
72 |
return provider;
|
73 |
} |
74 |
|
75 |
public void createLayer() throws NewLayerServiceException { |
76 |
try {
|
77 |
this.getExplorer().add(this.getStoreName(), |
78 |
this.getNewStoreParameters(), true); |
79 |
} catch (DataException e) {
|
80 |
throw new CantCreateNewLayerException(e); |
81 |
} |
82 |
} |
83 |
|
84 |
public void loadLayer() throws NewLayerServiceException { |
85 |
try {
|
86 |
String storeName = this.getStoreName(); |
87 |
DataStoreParameters storeParameters = |
88 |
this.getOpenStoreParameters();
|
89 |
DataManager dataManager = DALLocator.getDataManager(); |
90 |
DataStore store = dataManager.openStore(storeName, storeParameters); |
91 |
String layerName = store.getName();
|
92 |
|
93 |
MapContextManager mapContextManager = |
94 |
MapContextLocator.getMapContextManager(); |
95 |
FLayer lyr = mapContextManager.createLayer(layerName, store); |
96 |
lyr.setActive(true);
|
97 |
lyr.setVisible(true);
|
98 |
mapContext.getLayers().addLayer(lyr); |
99 |
|
100 |
lyr.dispose(); |
101 |
} catch (DataException e) {
|
102 |
throw new CantOpenStoreException(e); |
103 |
} catch (ValidateDataParametersException e) {
|
104 |
throw new CantOpenStoreException(e); |
105 |
} catch (LoadLayerException e) {
|
106 |
throw new CantLoadNewLayerException(e); |
107 |
} |
108 |
|
109 |
} |
110 |
|
111 |
public class CantCreateNewLayerException extends NewLayerServiceException { |
112 |
|
113 |
/**
|
114 |
*
|
115 |
*/
|
116 |
private static final long serialVersionUID = 4208215791054246118L; |
117 |
|
118 |
public CantCreateNewLayerException(Throwable cause) { |
119 |
super("Can't create the layer", cause, "_cant_create_the_layer", |
120 |
serialVersionUID); |
121 |
} |
122 |
} |
123 |
|
124 |
public class CantOpenStoreException extends NewLayerServiceException { |
125 |
|
126 |
/**
|
127 |
*
|
128 |
*/
|
129 |
private static final long serialVersionUID = -2245228621032918630L; |
130 |
|
131 |
public CantOpenStoreException(Throwable cause) { |
132 |
super("Can't open store", cause, "_cant_open_store", |
133 |
serialVersionUID); |
134 |
} |
135 |
} |
136 |
|
137 |
public class CantLoadNewLayerException extends NewLayerServiceException { |
138 |
|
139 |
/**
|
140 |
*
|
141 |
*/
|
142 |
private static final long serialVersionUID = -1711950651766745963L; |
143 |
|
144 |
public CantLoadNewLayerException(Throwable cause) { |
145 |
super("Can't load the new layer", cause, |
146 |
"_cant_load_the_new_layer", serialVersionUID);
|
147 |
} |
148 |
} |
149 |
|
150 |
public void setType(String type) { |
151 |
try {
|
152 |
setProviderFactory(this.newLayerManager
|
153 |
.getNewLayerProviderFactory(type)); |
154 |
} catch (ServiceException e) {
|
155 |
throw new RuntimeException(e); |
156 |
} |
157 |
} |
158 |
|
159 |
public DataServerExplorer getExplorer() {
|
160 |
return getProvider().getExplorer();
|
161 |
} |
162 |
|
163 |
public String getStoreName() { |
164 |
return getProvider().getStoreName();
|
165 |
} |
166 |
|
167 |
public EditableFeatureType getFeatureType() {
|
168 |
return getProvider().getFeatureType();
|
169 |
} |
170 |
|
171 |
public NewFeatureStoreParameters getNewStoreParameters() {
|
172 |
return getProvider().getNewStoreParameters();
|
173 |
} |
174 |
|
175 |
public DataStoreParameters getOpenStoreParameters() {
|
176 |
return getProvider().getOpenStoreParameters();
|
177 |
} |
178 |
|
179 |
public List<String> getTypes() { |
180 |
List<String> types = new ArrayList<String>(); |
181 |
List<NewLayerProviderFactory> providers = getProviderFactories();
|
182 |
Iterator<NewLayerProviderFactory> it = providers.iterator();
|
183 |
while (it.hasNext()) {
|
184 |
NewLayerProviderFactory newLayerProviderFactory = it.next(); |
185 |
types.add(newLayerProviderFactory.getName()); |
186 |
} |
187 |
return types;
|
188 |
} |
189 |
|
190 |
public String getType() { |
191 |
return this.type.getName(); |
192 |
} |
193 |
|
194 |
public void addLayerToView(boolean b) { |
195 |
this.addLayerToView = b;
|
196 |
} |
197 |
|
198 |
public MapContext getMapContext() {
|
199 |
return mapContext;
|
200 |
} |
201 |
|
202 |
public void setProviderFactory(NewLayerProviderFactory type) { |
203 |
this.type = type;
|
204 |
this.provider = type.create(this); |
205 |
} |
206 |
|
207 |
public NewLayerProviderFactory getProviderFactory() {
|
208 |
return type;
|
209 |
} |
210 |
|
211 |
public List<NewLayerProviderFactory> getProviderFactories() { |
212 |
|
213 |
List<NewLayerProviderFactory> providers =
|
214 |
new ArrayList<NewLayerProviderFactory>( |
215 |
this.newLayerManager
|
216 |
.getProviders(NewLayerManager.STORETYPE.ANY)); |
217 |
|
218 |
for (Iterator<NewLayerProviderFactory> iterator = providers.iterator(); iterator |
219 |
.hasNext();) { |
220 |
NewLayerProviderFactory newLayerProviderFactory = iterator.next(); |
221 |
if (!newLayerProviderFactory.isEnabled()) {
|
222 |
iterator.remove(); |
223 |
} |
224 |
} |
225 |
|
226 |
return providers;
|
227 |
} |
228 |
|
229 |
} |