01: // Copyright 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.internal.services;
16:
17: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newCaseInsensitiveMap;
18:
19: import java.util.Collection;
20: import java.util.Map;
21:
22: import org.apache.tapestry.Block;
23: import org.apache.tapestry.internal.structure.Page;
24: import org.apache.tapestry.services.BeanBlockContribution;
25: import org.apache.tapestry.services.BeanBlockSource;
26:
27: public class BeanBlockSourceImpl implements BeanBlockSource {
28: private final RequestPageCache _pageCache;
29:
30: private final Map<String, BeanBlockContribution> _display = newCaseInsensitiveMap();
31:
32: private final Map<String, BeanBlockContribution> _edit = newCaseInsensitiveMap();
33:
34: public BeanBlockSourceImpl(RequestPageCache pageCache,
35: Collection<BeanBlockContribution> configuration) {
36: _pageCache = pageCache;
37:
38: for (BeanBlockContribution contribution : configuration) {
39: Map<String, BeanBlockContribution> map = contribution
40: .isEdit() ? _edit : _display;
41:
42: // TODO: Check for conflicts?
43:
44: map.put(contribution.getDataType(), contribution);
45: }
46: }
47:
48: public boolean hasDisplayBlock(String datatype) {
49: return _display.containsKey(datatype);
50: }
51:
52: public Block getDisplayBlock(String datatype) {
53: BeanBlockContribution contribution = _display.get(datatype);
54:
55: if (contribution == null)
56: throw new RuntimeException(ServicesMessages
57: .noDisplayForDataType(datatype));
58:
59: return toBlock(contribution);
60: }
61:
62: private Block toBlock(BeanBlockContribution contribution) {
63: Page page = _pageCache.get(contribution.getPageName());
64:
65: return page.getRootElement()
66: .getBlock(contribution.getBlockId());
67: }
68:
69: public Block getEditBlock(String datatype) {
70: BeanBlockContribution contribution = _edit.get(datatype);
71:
72: if (contribution == null)
73: throw new RuntimeException(ServicesMessages
74: .noEditForDataType(datatype));
75:
76: return toBlock(contribution);
77: }
78:
79: }
|