| 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.BasicStroke; |
| 18 | import java.awt.event.ActionEvent; |
| 19 | import java.awt.event.ActionListener; |
| 20 | |
| 21 | import javax.swing.ButtonGroup; |
| 22 | import javax.swing.Icon; |
| 23 | import javax.swing.JCheckBoxMenuItem; |
| 24 | import javax.swing.JPopupMenu; |
| 25 | import javax.swing.SwingConstants; |
| 26 | |
| 27 | import org.jdesktop.application.Resource; |
| 28 | import org.jdesktop.application.ResourceMap; |
| 29 | |
| 30 | // DrawingEditor can be seen as any "tool" from the graphics toolbar |
| 31 | public class LinePropertyTool extends AbstractPushButtonTool |
| 32 | { |
| 33 | static final public String TOOL_PROPERTY_LINE = "Line"; |
| 34 | |
| 35 | @Override protected void initProperty() |
| 36 | { |
| 37 | _properties.put(TOOL_PROPERTY_LINE, _stroke); |
| 38 | } |
| 39 | |
| 40 | @Override protected void initResources(ResourceMap map) |
| 41 | { |
| 42 | super.initResources(map); |
| 43 | // Now create the popup menu |
| 44 | _popup = new JPopupMenu(); |
| 45 | _popup.setName("line-thickness-popup"); |
| 46 | for (int i = 0; i < ITEMS_COUNT; i++) |
| 47 | { |
| 48 | addLineItem(i); |
| 49 | } |
| 50 | ((JCheckBoxMenuItem) _popup.getComponent(0)).setSelected(true); |
| 51 | } |
| 52 | |
| 53 | private void addLineItem(int index) |
| 54 | { |
| 55 | final int thickness = _linesThickness[index]; |
| 56 | final JCheckBoxMenuItem item = new JCheckBoxMenuItem(); |
| 57 | item.setName("line-thickness-item-" + thickness); |
| 58 | item.setHorizontalAlignment(SwingConstants.LEFT); |
| 59 | if (thickness <= 1) |
| 60 | { |
| 61 | item.setText(_onePixelText); |
| 62 | } |
| 63 | else |
| 64 | { |
| 65 | item.setText(String.format(_manyPixelsFormat, thickness)); |
| 66 | } |
| 67 | item.setIcon(_icons[index]); |
| 68 | item.addActionListener(new ActionListener() |
| 69 | { |
| 70 | public void actionPerformed(ActionEvent e) |
| 71 | { |
| 72 | updateStroke(thickness, item.getIcon()); |
| 73 | } |
| 74 | }); |
| 75 | _popup.add(item); |
| 76 | _group.add(item); |
| 77 | } |
| 78 | |
| 79 | @Override public void accept() |
| 80 | { |
| 81 | // Open popup menu to choose line thickness |
| 82 | _popup.show(_button, 0, _button.getHeight()); |
| 83 | _button.setSelected(false); |
| 84 | } |
| 85 | |
| 86 | private void updateStroke(float thickness, Icon icon) |
| 87 | { |
| 88 | _thickness = thickness; |
| 89 | updateStroke(); |
| 90 | } |
| 91 | |
| 92 | private void updateStroke() |
| 93 | { |
| 94 | _stroke = new BasicStroke( _thickness, |
| 95 | BasicStroke.CAP_ROUND, |
| 96 | BasicStroke.JOIN_ROUND, |
| 97 | 0.0f, |
| 98 | _dash, |
| 99 | _dashPhase); |
| 100 | initProperty(); |
| 101 | } |
| 102 | |
| 103 | static private final int ITEMS_COUNT = 9; |
| 104 | |
| 105 | @Resource(key = "thickness") |
| 106 | private int[] _linesThickness = new int[ITEMS_COUNT]; |
| 107 | @Resource(key = "icon") |
| 108 | private Icon[] _icons = new Icon[ITEMS_COUNT]; |
| 109 | @Resource(key = "textOne") |
| 110 | private String _onePixelText; |
| 111 | @Resource(key = "textMany") |
| 112 | private String _manyPixelsFormat; |
| 113 | |
| 114 | private float _thickness = 1.0f; |
| 115 | private float[] _dash = null; |
| 116 | private float _dashPhase = 0.0f; |
| 117 | private JPopupMenu _popup = new JPopupMenu(); |
| 118 | private ButtonGroup _group = new ButtonGroup(); |
| 119 | private BasicStroke _stroke = new BasicStroke( _thickness, |
| 120 | BasicStroke.CAP_ROUND, |
| 121 | BasicStroke.JOIN_ROUND, |
| 122 | 0.0f, |
| 123 | _dash, |
| 124 | _dashPhase); |
| 125 | } |