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

History | View | Annotate | Download (5.7 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.net.URL;
27
import java.util.ArrayList;
28
import java.util.HashMap;
29
import java.util.Iterator;
30
import java.util.List;
31
import java.util.Map;
32

    
33
import javax.swing.ImageIcon;
34

    
35
import org.slf4j.Logger;
36
import org.slf4j.LoggerFactory;
37

    
38
import org.gvsig.fmap.mapcontext.MapContext;
39
import org.gvsig.newlayer.NewLayerManager;
40
import org.gvsig.newlayer.NewLayerProviderFactory;
41
import org.gvsig.newlayer.NewLayerService;
42
import org.gvsig.newlayer.NewLayerWizard;
43
import org.gvsig.newlayer.impl.preferences.DefaultNewLayerPreferencesComponent;
44
import org.gvsig.newlayer.preferences.NewLayerPreferencesComponent;
45
import org.gvsig.tools.ToolsLocator;
46
import org.gvsig.tools.extensionpoint.ExtensionPoint;
47
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension;
48
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
49
import org.gvsig.tools.service.ServiceException;
50

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

    
59
    private static final Logger LOG = LoggerFactory
60
        .getLogger(DefaultNewLayerManager.class);
61

    
62
    final static private String EP_NEWLAYER_NAME = "NewLayer.manager.providers";
63
    final static private String EP_NEWLAYER_DESCRIPTION = "NewLayer providers";
64

    
65
    private Map<NewLayerProviderFactory, Boolean> providerStatus;
66

    
67
    /**
68
     * Empty constructor.
69
     */
70
    public DefaultNewLayerManager() {
71
        providerStatus = new HashMap<NewLayerProviderFactory, Boolean>();
72
    }
73

    
74
    public NewLayerService createNewLayerService(MapContext mapContext) {
75
        return new DefaultNewLayerService(this, mapContext);
76
    }
77

    
78
    public void registerProvider(NewLayerProviderFactory factory) {
79
        ExtensionPoint ep = getNewLayerProvidersExtensionPoint();
80
        ep.append(factory.getName(), factory.getDescription(), factory);
81
    }
82

    
83
    public NewLayerWizard createNewLayerWizard(NewLayerService service) {
84
        URL iconURL =
85
            getClass().getClassLoader().getResource(
86
                "org/gvsig/newlayer/lib/impl/images/newlayericon.png");
87
        ImageIcon logo = null;
88
        if (iconURL != null) {
89
            logo = new ImageIcon(iconURL);
90
        }
91

    
92
        return new DefaultNewLayerWizard(logo, service);
93
    }
94

    
95
    public NewLayerProviderFactory getNewLayerProviderFactory(
96
        String providerName) throws ServiceException {
97
        ExtensionPoint ep = getNewLayerProvidersExtensionPoint();
98
        try {
99
            return (NewLayerProviderFactory) ep.create(providerName);
100
        } catch (InstantiationException e) {
101
            throw new RuntimeException(e);
102
        } catch (IllegalAccessException e) {
103
            throw new RuntimeException(e);
104
        }
105
    }
106

    
107
    public void enableProvider(NewLayerProviderFactory factory, Boolean value) {
108
        providerStatus.put(factory, Boolean.valueOf(value));
109
    }
110

    
111
    public NewLayerPreferencesComponent createNewLayerProvidersPreferences() {
112
        return new DefaultNewLayerPreferencesComponent();
113
    }
114

    
115
    public List<NewLayerProviderFactory> getProviders() {
116
        ExtensionPoint ep = getNewLayerProvidersExtensionPoint();
117
        List<NewLayerProviderFactory> providers =
118
            new ArrayList<NewLayerProviderFactory>();
119
        @SuppressWarnings("unchecked")
120
        Iterator<Extension> it = ep.iterator();
121

    
122
        while (it.hasNext()) {
123
            Extension extension = (Extension) it.next();
124
            NewLayerProviderFactory factory;
125
            try {
126
                factory = (NewLayerProviderFactory) extension.create();
127
            } catch (InstantiationException e) {
128
                LOG.warn(
129
                    "Can't create NewLayerProviderFactory "
130
                        + extension.getName(), e);
131
                continue;
132
            } catch (IllegalAccessException e) {
133
                LOG.warn(
134
                    "Can't create NewLayerProviderFactory "
135
                        + extension.getName(), e);
136
                continue;
137
            }
138
            providers.add(factory);
139
        }
140

    
141
        return providers;
142
    }
143

    
144
    private ExtensionPoint getNewLayerProvidersExtensionPoint() {
145
        ExtensionPointManager epmgr = ToolsLocator.getExtensionPointManager();
146
        return
147
            epmgr.add(EP_NEWLAYER_NAME, EP_NEWLAYER_DESCRIPTION);
148
    }
149

    
150
    public List<NewLayerProviderFactory> getProviders(STORETYPE filter) {
151
        // TODO use filters
152
        // if (filter == STORETYPE.TABULAR){
153
        // if (!factory.isSpatial()){
154
        // providers.add(factory);
155
        // }
156
        // } else {
157
        // }
158
        return getProviders();
159
    }
160

    
161
    public boolean isProviderEnabled(NewLayerProviderFactory factory) {
162
        Boolean status = providerStatus.get(factory);
163
        return status == null ? true : status.booleanValue();
164
    }
165

    
166
}