root / tags / v2_0_0_Build_2035 / libraries / org.gvsig.newlayer / org.gvsig.newlayer.lib / org.gvsig.newlayer.lib.impl / src / main / java / org / gvsig / newlayer / impl / DefaultNewLayerService.java @ 36338
History | View | Annotate | Download (5.93 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 |
|
47 |
/**
|
48 |
* Default {@link NewLayerService} implementation.
|
49 |
*
|
50 |
* @author gvSIG Team
|
51 |
* @version $Id$
|
52 |
*/
|
53 |
public class DefaultNewLayerService implements NewLayerService { |
54 |
|
55 |
private NewLayerManager newLayerManager;
|
56 |
private NewLayerProviderFactory type;
|
57 |
private NewLayerProvider provider;
|
58 |
private boolean addLayerToView = true; |
59 |
private MapContext mapContext;
|
60 |
|
61 |
/**
|
62 |
* {@link DefaultNewLayerService} constructor
|
63 |
|
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() |
75 |
throws NewLayerServiceException {
|
76 |
try {
|
77 |
this.getExplorer().add(this.getStoreName(), 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 = this.getNewStoreParameters();
|
87 |
DataManager dataManager=DALLocator.getDataManager(); |
88 |
DataStore store = dataManager.openStore(storeName, storeParameters); |
89 |
String layerName = store.getName();
|
90 |
|
91 |
MapContextManager mapContextManager = MapContextLocator.getMapContextManager(); |
92 |
FLayer lyr = mapContextManager.createLayer(layerName, store); |
93 |
lyr.setActive(true);
|
94 |
lyr.setVisible(true);
|
95 |
mapContext.getLayers().addLayer(lyr); |
96 |
|
97 |
lyr.dispose(); |
98 |
} catch (DataException e) {
|
99 |
throw new CantOpenStoreException(e); |
100 |
} catch (ValidateDataParametersException e) {
|
101 |
throw new CantOpenStoreException(e); |
102 |
} catch (LoadLayerException e) {
|
103 |
throw new CantLoadNewLayerException(e); |
104 |
} |
105 |
|
106 |
} |
107 |
|
108 |
|
109 |
public class CantCreateNewLayerException extends NewLayerServiceException { |
110 |
/**
|
111 |
*
|
112 |
*/
|
113 |
private static final long serialVersionUID = 4208215791054246118L; |
114 |
|
115 |
public CantCreateNewLayerException(Throwable cause) { |
116 |
super("Can't create the layer", cause, "_cant_create_the_layer", serialVersionUID); |
117 |
} |
118 |
} |
119 |
|
120 |
public class CantOpenStoreException extends NewLayerServiceException { |
121 |
|
122 |
/**
|
123 |
*
|
124 |
*/
|
125 |
private static final long serialVersionUID = -2245228621032918630L; |
126 |
|
127 |
public CantOpenStoreException(Throwable cause) { |
128 |
super("Can't open store", cause, "_cant_open_store", serialVersionUID); |
129 |
} |
130 |
} |
131 |
|
132 |
public class CantLoadNewLayerException extends NewLayerServiceException { |
133 |
|
134 |
/**
|
135 |
*
|
136 |
*/
|
137 |
private static final long serialVersionUID = -1711950651766745963L; |
138 |
|
139 |
public CantLoadNewLayerException(Throwable cause) { |
140 |
super("Can't load the new layer", cause, "_cant_load_the_new_layer", serialVersionUID); |
141 |
} |
142 |
} |
143 |
|
144 |
public void setType(String type) { |
145 |
List<NewLayerProviderFactory> providers = this.newLayerManager.getProviders(NewLayerManager.STORETYPE.ANY); |
146 |
Iterator<NewLayerProviderFactory> it = providers.iterator();
|
147 |
while (it.hasNext()) {
|
148 |
NewLayerProviderFactory newLayerProviderFactory = it.next(); |
149 |
if (newLayerProviderFactory.getName().equalsIgnoreCase(type)){
|
150 |
this.type = newLayerProviderFactory;
|
151 |
this.provider = newLayerProviderFactory.create(this); |
152 |
return;
|
153 |
} |
154 |
} |
155 |
throw new IllegalArgumentException(type); |
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 = this.newLayerManager.getProviders(NewLayerManager.STORETYPE.ANY); |
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 |
} |