Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.newlayer / org.gvsig.newlayer.lib / org.gvsig.newlayer.lib.impl / src / main / java / org / gvsig / newlayer / impl / DefaultNewLayerService.java @ 40560

History | View | Annotate | Download (7.31 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.newlayer.impl;
25

    
26
import java.util.ArrayList;
27
import java.util.Iterator;
28
import java.util.List;
29

    
30
import org.gvsig.fmap.dal.DALLocator;
31
import org.gvsig.fmap.dal.DataManager;
32
import org.gvsig.fmap.dal.DataServerExplorer;
33
import org.gvsig.fmap.dal.DataStore;
34
import org.gvsig.fmap.dal.DataStoreParameters;
35
import org.gvsig.fmap.dal.exception.DataException;
36
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
37
import org.gvsig.fmap.dal.feature.EditableFeatureType;
38
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
39
import org.gvsig.fmap.mapcontext.MapContext;
40
import org.gvsig.fmap.mapcontext.MapContextLocator;
41
import org.gvsig.fmap.mapcontext.MapContextManager;
42
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
43
import org.gvsig.fmap.mapcontext.layers.FLayer;
44
import org.gvsig.newlayer.NewLayerManager;
45
import org.gvsig.newlayer.NewLayerProvider;
46
import org.gvsig.newlayer.NewLayerProviderFactory;
47
import org.gvsig.newlayer.NewLayerService;
48
import org.gvsig.newlayer.NewLayerServiceException;
49
import org.gvsig.tools.service.ServiceException;
50

    
51
/**
52
 * Default {@link NewLayerService} implementation.
53
 * 
54
 * @author gvSIG Team
55
 * @version $Id$
56
 */
57
public class DefaultNewLayerService implements NewLayerService {
58

    
59
    private NewLayerManager newLayerManager;
60
    private NewLayerProviderFactory type;
61
    private NewLayerProvider provider;
62
    private boolean addLayerToView = true;
63
    private MapContext mapContext;
64

    
65
    /**
66
     * {@link DefaultNewLayerService} constructor
67
     */
68
    public DefaultNewLayerService(NewLayerManager manager, MapContext mapContext) {
69
        this.newLayerManager = manager;
70
        this.mapContext = mapContext;
71
    }
72

    
73
    public NewLayerProvider getProvider() {
74
        return provider;
75
    }
76

    
77
    public void createLayer() throws NewLayerServiceException {
78
        try {
79
            this.getExplorer().add(this.getStoreName(),
80
                this.getNewStoreParameters(), true);
81
        } catch (DataException e) {
82
            throw new CantCreateNewLayerException(e);
83
        }
84
    }
85

    
86
    public void loadLayer() throws NewLayerServiceException {
87
        try {
88
            String storeName = this.getStoreName();
89
            DataStoreParameters storeParameters =
90
                this.getOpenStoreParameters();
91
            DataManager dataManager = DALLocator.getDataManager();
92
            DataStore store = dataManager.openStore(storeName, storeParameters);
93
            String layerName = store.getName();
94

    
95
            MapContextManager mapContextManager =
96
                MapContextLocator.getMapContextManager();
97
            FLayer lyr = mapContextManager.createLayer(layerName, store);
98
            lyr.setActive(true);
99
            lyr.setVisible(true);
100
            mapContext.getLayers().addLayer(lyr);
101

    
102
            lyr.dispose();
103
        } catch (DataException e) {
104
            throw new CantOpenStoreException(e);
105
        } catch (ValidateDataParametersException e) {
106
            throw new CantOpenStoreException(e);
107
        } catch (LoadLayerException e) {
108
            throw new CantLoadNewLayerException(e);
109
        }
110

    
111
    }
112

    
113
    public class CantCreateNewLayerException extends NewLayerServiceException {
114

    
115
        /**
116
                 * 
117
                 */
118
        private static final long serialVersionUID = 4208215791054246118L;
119

    
120
        public CantCreateNewLayerException(Throwable cause) {
121
            super("Can't create the layer", cause, "_cant_create_the_layer",
122
                serialVersionUID);
123
        }
124
    }
125

    
126
    public class CantOpenStoreException extends NewLayerServiceException {
127

    
128
        /**
129
                 * 
130
                 */
131
        private static final long serialVersionUID = -2245228621032918630L;
132

    
133
        public CantOpenStoreException(Throwable cause) {
134
            super("Can't open store", cause, "_cant_open_store",
135
                serialVersionUID);
136
        }
137
    }
138

    
139
    public class CantLoadNewLayerException extends NewLayerServiceException {
140

    
141
        /**
142
                 * 
143
                 */
144
        private static final long serialVersionUID = -1711950651766745963L;
145

    
146
        public CantLoadNewLayerException(Throwable cause) {
147
            super("Can't load the new layer", cause,
148
                "_cant_load_the_new_layer", serialVersionUID);
149
        }
150
    }
151

    
152
    public void setType(String type) {
153
        try {
154
            setProviderFactory(this.newLayerManager
155
                .getNewLayerProviderFactory(type));
156
        } catch (ServiceException e) {
157
            throw new RuntimeException(e);
158
        }
159
    }
160

    
161
    public DataServerExplorer getExplorer() {
162
        return getProvider().getExplorer();
163
    }
164

    
165
    public String getStoreName() {
166
        return getProvider().getStoreName();
167
    }
168

    
169
    public EditableFeatureType getFeatureType() {
170
        return getProvider().getFeatureType();
171
    }
172

    
173
    public NewFeatureStoreParameters getNewStoreParameters() {
174
        return getProvider().getNewStoreParameters();
175
    }
176

    
177
    public DataStoreParameters getOpenStoreParameters() {
178
        return getProvider().getOpenStoreParameters();
179
    }
180

    
181
    public List<String> getTypes() {
182
        List<String> types = new ArrayList<String>();
183
        List<NewLayerProviderFactory> providers = getProviderFactories();
184
        Iterator<NewLayerProviderFactory> it = providers.iterator();
185
        while (it.hasNext()) {
186
            NewLayerProviderFactory newLayerProviderFactory = it.next();
187
            types.add(newLayerProviderFactory.getName());
188
        }
189
        return types;
190
    }
191

    
192
    public String getType() {
193
        return this.type.getName();
194
    }
195

    
196
    public void addLayerToView(boolean b) {
197
        this.addLayerToView = b;
198
    }
199

    
200
    public MapContext getMapContext() {
201
        return mapContext;
202
    }
203

    
204
    public void setProviderFactory(NewLayerProviderFactory type) {
205
        this.type = type;
206
        this.provider = type.create(this);
207
    }
208

    
209
    public NewLayerProviderFactory getProviderFactory() {
210
        return type;
211
    }
212

    
213
    public List<NewLayerProviderFactory> getProviderFactories() {
214

    
215
        List<NewLayerProviderFactory> providers =
216
            new ArrayList<NewLayerProviderFactory>(
217
                this.newLayerManager
218
                    .getProviders(NewLayerManager.STORETYPE.ANY));
219

    
220
        for (Iterator<NewLayerProviderFactory> iterator = providers.iterator(); iterator
221
            .hasNext();) {
222
            NewLayerProviderFactory newLayerProviderFactory = iterator.next();
223
            if (!newLayerProviderFactory.isEnabled()) {
224
                iterator.remove();
225
            }
226
        }
227

    
228
        return providers;
229
    }
230

    
231
}