Statistics
| Revision:

root / 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 / DefaultNewLayerManager.java @ 38549

History | View | Annotate | Download (5.65 KB)

1 36022 fdiaz
/* 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.net.URL;
25
import java.util.ArrayList;
26 37891 cordinyana
import java.util.HashMap;
27 36022 fdiaz
import java.util.Iterator;
28
import java.util.List;
29 37891 cordinyana
import java.util.Map;
30 36022 fdiaz
31
import javax.swing.ImageIcon;
32
33 37891 cordinyana
import org.slf4j.Logger;
34
import org.slf4j.LoggerFactory;
35
36 36220 fdiaz
import org.gvsig.fmap.mapcontext.MapContext;
37 36022 fdiaz
import org.gvsig.newlayer.NewLayerManager;
38
import org.gvsig.newlayer.NewLayerProviderFactory;
39
import org.gvsig.newlayer.NewLayerService;
40
import org.gvsig.newlayer.NewLayerWizard;
41 37891 cordinyana
import org.gvsig.newlayer.impl.preferences.DefaultNewLayerPreferencesComponent;
42
import org.gvsig.newlayer.preferences.NewLayerPreferencesComponent;
43 36022 fdiaz
import org.gvsig.tools.ToolsLocator;
44
import org.gvsig.tools.extensionpoint.ExtensionPoint;
45
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension;
46
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
47 37891 cordinyana
import org.gvsig.tools.service.ServiceException;
48 36022 fdiaz
49
/**
50
 * Default {@link NewLayerManager} implementation.
51
 *
52
 * @author gvSIG Team
53
 * @version $Id$
54
 */
55
public class DefaultNewLayerManager implements NewLayerManager {
56
57 37891 cordinyana
    private static final Logger LOG = LoggerFactory
58
        .getLogger(DefaultNewLayerManager.class);
59 36022 fdiaz
60 37891 cordinyana
    final static private String EP_NEWLAYER_NAME = "NewLayer.manager.providers";
61
    final static private String EP_NEWLAYER_DESCRIPTION = "NewLayer providers";
62 36022 fdiaz
63 37891 cordinyana
    private Map<NewLayerProviderFactory, Boolean> providerStatus;
64 36022 fdiaz
65 37891 cordinyana
    /**
66
     * Empty constructor.
67
     */
68
    public DefaultNewLayerManager() {
69
        providerStatus = new HashMap<NewLayerProviderFactory, Boolean>();
70
    }
71 36022 fdiaz
72 37891 cordinyana
    public NewLayerService createNewLayerService(MapContext mapContext) {
73
        return new DefaultNewLayerService(this, mapContext);
74
    }
75 36022 fdiaz
76 37891 cordinyana
    public void registerProvider(NewLayerProviderFactory factory) {
77
        ExtensionPoint ep = getNewLayerProvidersExtensionPoint();
78
        ep.append(factory.getName(), factory.getDescription(), factory);
79
    }
80
81
    public NewLayerWizard createNewLayerWizard(NewLayerService service) {
82
        URL iconURL =
83
            getClass().getClassLoader().getResource(
84
                "org/gvsig/newlayer/lib/impl/images/newlayericon.png");
85 36022 fdiaz
        ImageIcon logo = null;
86 37891 cordinyana
        if (iconURL != null) {
87
            logo = new ImageIcon(iconURL);
88 36022 fdiaz
        }
89
90 37891 cordinyana
        return new DefaultNewLayerWizard(logo, service);
91
    }
92
93
    public NewLayerProviderFactory getNewLayerProviderFactory(
94
        String providerName) throws ServiceException {
95
        ExtensionPoint ep = getNewLayerProvidersExtensionPoint();
96
        try {
97
            return (NewLayerProviderFactory) ep.create(providerName);
98
        } catch (InstantiationException e) {
99
            throw new RuntimeException(e);
100
        } catch (IllegalAccessException e) {
101
            throw new RuntimeException(e);
102
        }
103
    }
104
105
    public void enableProvider(NewLayerProviderFactory factory, Boolean value) {
106
        providerStatus.put(factory, Boolean.valueOf(value));
107
    }
108
109
    public NewLayerPreferencesComponent createNewLayerProvidersPreferences() {
110
        return new DefaultNewLayerPreferencesComponent();
111
    }
112
113
    public List<NewLayerProviderFactory> getProviders() {
114
        ExtensionPoint ep = getNewLayerProvidersExtensionPoint();
115
        List<NewLayerProviderFactory> providers =
116
            new ArrayList<NewLayerProviderFactory>();
117
        @SuppressWarnings("unchecked")
118
        Iterator<Extension> it = ep.iterator();
119
120
        while (it.hasNext()) {
121
            Extension extension = (Extension) it.next();
122
            NewLayerProviderFactory factory;
123
            try {
124
                factory = (NewLayerProviderFactory) extension.create();
125
            } catch (InstantiationException e) {
126
                LOG.warn(
127
                    "Can't create NewLayerProviderFactory "
128
                        + extension.getName(), e);
129
                continue;
130
            } catch (IllegalAccessException e) {
131
                LOG.warn(
132
                    "Can't create NewLayerProviderFactory "
133
                        + extension.getName(), e);
134
                continue;
135
            }
136
            providers.add(factory);
137
        }
138
139
        return providers;
140
    }
141
142
    private ExtensionPoint getNewLayerProvidersExtensionPoint() {
143
        ExtensionPointManager epmgr = ToolsLocator.getExtensionPointManager();
144
        return
145
            epmgr.add(EP_NEWLAYER_NAME, EP_NEWLAYER_DESCRIPTION);
146
    }
147
148
    public List<NewLayerProviderFactory> getProviders(STORETYPE filter) {
149
        // TODO use filters
150
        // if (filter == STORETYPE.TABULAR){
151
        // if (!factory.isSpatial()){
152
        // providers.add(factory);
153
        // }
154
        // } else {
155
        // }
156
        return getProviders();
157
    }
158
159
    public boolean isProviderEnabled(NewLayerProviderFactory factory) {
160
        Boolean status = providerStatus.get(factory);
161
        return status == null ? true : status.booleanValue();
162
    }
163
164 36022 fdiaz
}