Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.spi / src / main / java / org / gvsig / fmap / dal / resource / spi / MultiResource.java @ 40767

History | View | Annotate | Download (7.39 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.fmap.dal.resource.spi;
25

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

    
29
import org.gvsig.fmap.dal.DALLocator;
30
import org.gvsig.fmap.dal.exception.InitializeException;
31
import org.gvsig.fmap.dal.resource.ResourceParameters;
32
import org.gvsig.fmap.dal.resource.exception.AccessResourceException;
33
import org.gvsig.fmap.dal.resource.exception.PrepareResourceException;
34
import org.gvsig.fmap.dal.resource.exception.ResourceException;
35
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyChangesException;
36
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyCloseException;
37
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyDisposeException;
38
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyOpenException;
39
import org.slf4j.Logger;
40
import org.slf4j.LoggerFactory;
41

    
42
/**
43
 * Resource implementation which is able to show an unique interface to a group
44
 * of Resources.
45
 * 
46
 * @author 2009- <a href="cordinyana@gvsig.org">C?sar Ordi?ana</a> - gvSIG team
47
 */
48
public class MultiResource extends AbstractResource {
49

    
50
        public static final String TYPE_NAME = "multi";
51

    
52
        public static final String DESCRIPTION = "Group of multiple resources";
53

    
54
        private static final Logger LOG =
55
                        LoggerFactory.getLogger(MultiResource.class);
56

    
57
        private List resources = new ArrayList();
58

    
59
        // Initially, the first one
60
        private int defaultResourcePos = 0;
61

    
62
        /**
63
         * Creates a new {@link MultiResource} from the given parameters.
64
         * 
65
         * @param parameters
66
         *            to create the resource from
67
         * @throws InitializeException
68
         *             if there is an error creating the resource
69
         */
70
        public MultiResource(MultiResourceParameters parameters)
71
                        throws InitializeException {
72
                super(parameters);
73
        }
74

    
75
        private List getResources() {
76
                return resources;
77
        }
78

    
79
        /**
80
         * Adds a new {@link ResourceProvider} to the list of Resources managed by
81
         * this group.
82
         * 
83
         * @param parameters
84
         *            to create the resource from
85
         * @param defaultResource
86
         *            if the added resource is to be used as the multi resource
87
         *            default one
88
         * @throws InitializeException
89
         *             if there is an error adding the resource
90
         */
91
        public void addResource(ResourceParameters parameters,
92
                        boolean defaultResource) throws InitializeException {
93
                ResourceProvider resourceProvider =
94
                                ((ResourceManagerProviderServices) DALLocator.getResourceManager()).createResource(parameters);
95
                resources.add(resourceProvider);
96
                if (defaultResource) {
97
                        defaultResourcePos = resources.size() - 1;
98
                }
99
        }
100

    
101
        /**
102
         * Adds a new {@link ResourceProvider} to the list of Resources managed by
103
         * this group.
104
         * 
105
         * @param name
106
         *            of the resource to add
107
         * @param params
108
         *            parameters to create the resource with
109
         * @param defaultResource
110
         *            if the added resource is to be used as the multi resource
111
         *            default one
112
         * @throws InitializeException
113
         *             if there is an error adding the resource
114
         */
115
        public void addResource(String name, Object[] params,
116
                        boolean defaultResource) throws InitializeException {
117
                ResourceProvider resourceProvider =
118
                                ((ResourceManagerProviderServices) DALLocator.getResourceManager()).createResource(
119
                                                name, params);
120
                resources.add(resourceProvider);
121
                if (defaultResource) {
122
                        defaultResourcePos = resources.size() - 1;
123
                }
124
        }
125

    
126
        public boolean isThis(ResourceParameters parameters)
127
                        throws ResourceException {
128
                if (parameters instanceof MultiResourceParameters) {
129
                        MultiResourceParameters multiParams =
130
                                        (MultiResourceParameters) parameters;
131
                        return multiParams.getName() != null
132
                                        && multiParams.getName().equals(
133
                                                        getMultiResourceParameters().getName());
134
                }
135

    
136
                return false;
137
        }
138

    
139
        public void notifyChanges() throws ResourceNotifyChangesException {
140
                List resources = getResources();
141
                for (int i = 0; i < resources.size(); i++) {
142
                        ((ResourceProvider) getResources().get(i)).notifyChanges();
143
                }
144
        }
145

    
146
        public void notifyClose() throws ResourceNotifyCloseException {
147
                List resources = getResources();
148
                for (int i = 0; i < resources.size(); i++) {
149
                        ((ResourceProvider) getResources().get(i)).notifyClose();
150
                }
151
        }
152

    
153
        public void notifyDispose() throws ResourceNotifyDisposeException {
154
                List resources = getResources();
155
                for (int i = 0; i < resources.size(); i++) {
156
                        ((ResourceProvider) getResources().get(i)).notifyDispose();
157
                }
158
        }
159

    
160
        public void notifyOpen() throws ResourceNotifyOpenException {
161
                List resources = getResources();
162
                for (int i = 0; i < resources.size(); i++) {
163
                        ((ResourceProvider) getResources().get(i)).notifyOpen();
164
                }
165
        }
166

    
167
        public void prepare() throws PrepareResourceException {
168
                List resources = getResources();
169
                for (int i = 0; i < resources.size(); i++) {
170
                        ((ResourceProvider) getResources().get(i)).prepare();
171
                }
172
        }
173

    
174
        public void closeRequest() throws ResourceException {
175
                List resources = getResources();
176
                for (int i = 0; i < resources.size(); i++) {
177
                        ((ResourceProvider) getResources().get(i)).closeRequest();
178
                }
179
        }
180

    
181
        public Object get() throws AccessResourceException {
182
                ResourceProvider defaultResource = getDefaultResource();
183
                if (defaultResource == null) {
184
                        return null;
185
                } else {
186
                        return defaultResource.get();
187
                }
188
        }
189

    
190
        public Object getData() {
191
                ResourceProvider defaultResource = getDefaultResource();
192
                if (defaultResource == null) {
193
                        return null;
194
                } else {
195
                        return defaultResource.getData();
196
                }
197
        }
198

    
199
        private MultiResourceParameters getMultiResourceParameters() {
200
                return (MultiResourceParameters) getParameters();
201
        }
202

    
203
        public String getName() throws AccessResourceException {
204
                StringBuffer buffer =
205
                                new StringBuffer().append("MultiResource ").append(
206
                                                getMultiResourceParameters().getName()).append(":: ");
207
                List resources = getResources();
208
                for (int i = 0; i < resources.size(); i++) {
209
                        buffer.append(((ResourceProvider) resources.get(i)).getName());
210
                        if (defaultResourcePos == i) {
211
                                buffer.append(" (default)");
212
                        }
213
                        if (i < resources.size() - 1) {
214
                                buffer.append(", ");
215
                        }
216
                }
217
                return buffer.toString();
218
        }
219

    
220
        public String toString() {
221
                try {
222
                        return getName();
223
                } catch (AccessResourceException e) {
224
                        LOG.error("Error getting the resource name", e);
225
                }
226
                return super.toString();
227
        }
228

    
229
        private ResourceProvider getDefaultResource() {
230
                if (getResources().size() > 0) {
231
                        return (ResourceProvider) getResources().get(defaultResourcePos);
232
                } else {
233
                        return null;
234
                }
235
        }
236

    
237
        public void setData(Object data) {
238
                ResourceProvider defaultResource = getDefaultResource();
239
                if (defaultResource != null) {
240
                        defaultResource.setData(data);
241
                }
242
        }
243
}