| 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 | |
| 15 | package net.sourceforge.hiveboard.drawing; |
| 16 | |
| 17 | import java.awt.Component; |
| 18 | import java.util.Arrays; |
| 19 | import java.util.Comparator; |
| 20 | import java.util.HashMap; |
| 21 | import java.util.List; |
| 22 | import java.util.Map; |
| 23 | |
| 24 | import javax.swing.ButtonGroup; |
| 25 | import javax.swing.ButtonModel; |
| 26 | import javax.swing.JComponent; |
| 27 | import javax.swing.JToggleButton; |
| 28 | import javax.swing.JToolBar; |
| 29 | |
| 30 | import org.apache.commons.logging.Log; |
| 31 | import org.apache.commons.logging.LogFactory; |
| 32 | |
| 33 | public 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 | } |