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