EMMA Coverage Report (generated Wed Feb 13 07:49:24 ICT 2008)
[all classes][net.sourceforge.hiveboard.drawing]

COVERAGE SUMMARY FOR SOURCE FILE [ToolDisplayHandler.java]

nameclass, %method, %block, %line, %
ToolDisplayHandler.java67%  (2/3)100% (9/9)93%  (227/243)91%  (51/56)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class ToolDisplayHandler100% (1/1)100% (6/6)100% (211/211)100% (47/47)
<static initializer> 100% (1/1)100% (4/4)100% (1/1)
ToolDisplayHandler (ToolHandler, List): void 100% (1/1)100% (96/96)100% (22/22)
addTool (ToolContribution): void 100% (1/1)100% (47/47)100% (9/9)
clearSelection (): void 100% (1/1)100% (16/16)100% (5/5)
getToolBar (): JToolBar 100% (1/1)100% (3/3)100% (1/1)
setEnabled (boolean): void 100% (1/1)100% (45/45)100% (9/9)
     
class ToolDisplayHandler$10%   (0/1)100% (0/0)100% (0/0)100% (0/0)
     
class ToolDisplayHandler$ToolContribComparator100% (1/1)100% (3/3)50%  (16/32)44%  (4/9)
compare (ToolContribution, ToolContribution): int 100% (1/1)38%  (10/26)38%  (3/8)
ToolDisplayHandler$ToolContribComparator (): void 100% (1/1)100% (3/3)100% (1/1)
ToolDisplayHandler$ToolContribComparator (ToolDisplayHandler$1): void 100% (1/1)100% (3/3)100% (1/1)

1//  Copyright 2004-2007 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// 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 
15package net.sourceforge.hiveboard.drawing;
16 
17import java.awt.Component;
18import java.util.Arrays;
19import java.util.Comparator;
20import java.util.HashMap;
21import java.util.List;
22import java.util.Map;
23 
24import javax.swing.ButtonGroup;
25import javax.swing.ButtonModel;
26import javax.swing.JComponent;
27import javax.swing.JToggleButton;
28import javax.swing.JToolBar;
29 
30import org.apache.commons.logging.Log;
31import org.apache.commons.logging.LogFactory;
32 
33public class ToolDisplayHandler
34{
35        static final private Log        _logger = LogFactory.getLog(ToolDisplayHandler.class);
36        
37        public ToolDisplayHandler(ToolHandler handler, List<ToolContribution> config)
38        {
39                _handler = handler;
40 
41                // Parse Configuration
42                ToolContribution[] contribs = config.toArray(new ToolContribution[config.size()]);
43                // Sort it by order and name
44                Arrays.sort(contribs, new ToolContribComparator());
45                
46                // Create toolbar
47                _toolbar = new JToolBar();
48                //#### JToolBar.setName() must be passed the TITLE of the -floating- palette
49                // and thus cannot be used with code names...
50//                _toolbar.setName(NAME + "all-tools-palette");
51                _toolbar.setName("Drawing Tools");
52                _toolbar.setBorderPainted(false);
53 
54                // Add fake button to exclusive group of tools (to allow unselection of all tools)
55                _group.add(_resetGroup);
56 
57                // Create tools, commands & toolbar buttons
58                for (int i = 0; i < contribs.length; i++)
59                {
60                        ToolContribution contrib = contribs[i];
61                        if (_logger.isDebugEnabled())
62                        {
63                                _logger.debug("<init> contribs[" + i + "] = " + contrib);
64                        }
65                        String id = contrib.getName();
66                        if (id == null)
67                        {
68                                // Add separator to the toolbar
69                                _toolbar.addSeparator();
70                                continue;
71                        }
72                        // Create the Toobar button & command
73                        addTool(contrib);
74                }
75 
76                // Initially the toolbar must be disabled
77                setEnabled(false);
78        }
79        
80        public JToolBar        getToolBar()
81        {
82                return _toolbar;
83        }
84        
85        public void                clearSelection()
86        {
87                _resetGroup.setSelected(true);
88                ButtonModel selection = _group.getSelection();
89                if (selection != null)
90                {
91                        _group.setSelected(selection, false);
92                }
93        }
94 
95        public void                setEnabled(boolean enabled)
96        {
97                _logger.debug("setEnabled(" + enabled + ")");
98                // Enable/Disable all tools
99                Component[] comps = _toolbar.getComponents();
100                for (int i = 0; i < comps.length; i++)
101                {
102                        if (comps[i] instanceof JComponent)
103                        {
104                                JComponent comp = (JComponent) comps[i];
105                                // Check if the enabling state of that tool must be handled here
106                                if (comp.getClientProperty(ToolHandler.NOT_TOOL_CLIENT_PROPERTY) != null)
107                                {
108                                        // The state is handled by the tool itself
109                                        continue;
110                                }
111                        }
112                        comps[i].setEnabled(enabled);
113                }
114        }
115 
116        private void        addTool(ToolContribution contrib)
117        {
118                // Initialize tool
119                Tool tool = contrib.getObject();
120                tool.init(contrib.getName(), _handler);
121                tool.setPropertyMap(_properties);
122 
123                JComponent[] components = tool.getToolBarComponents();
124                for (int i = 0; i < components.length; i++)
125                {
126                        _toolbar.add(components[i]);
127                        if (        tool instanceof ExclusiveOptionsTool
128                                &&        components[i] instanceof JToggleButton)
129                        {
130                                _group.add((JToggleButton) components[i]);
131                        }
132                }
133        }
134 
135        // Sorts tool configuration contributions by their given "order"
136        static private class ToolContribComparator implements Comparator<ToolContribution>
137        {
138                public int        compare(ToolContribution c1, ToolContribution c2)
139                {
140                        int diff = c1.getOrder() - c2.getOrder();
141                        if (diff != 0)
142                        {
143                                return diff;
144                        }
145                        else if (c1.getName() != null)
146                        {
147                                return c1.getName().compareTo(c2.getName());
148                        }
149                        else if (c2.getName() != null)
150                        {
151                                return -1;
152                        }
153                        else
154                        {
155                                return 0;
156                        }
157                }
158        }
159 
160        // Reference to the ToolHandler
161        final private ToolHandler                        _handler;
162        
163        final private ButtonGroup                        _group = new ButtonGroup();
164        // This invisible button is used to unselect ALL tools from the exclusive group
165        final private JToggleButton                        _resetGroup = new JToggleButton();
166        final private JToolBar                                _toolbar;
167        final private Map<String, Object>        _properties = new HashMap<String, Object>();
168}

[all classes][net.sourceforge.hiveboard.drawing]
EMMA 2.0.5312 (C) Vladimir Roubtsov