| 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.Color; |
| 18 | import java.awt.Font; |
| 19 | import java.awt.FontMetrics; |
| 20 | import java.awt.Point; |
| 21 | |
| 22 | import javax.swing.JComponent; |
| 23 | import javax.swing.JTextArea; |
| 24 | import javax.swing.border.EmptyBorder; |
| 25 | |
| 26 | // DrawingTool can be seen as any "tool" from the graphics toolbar |
| 27 | public class TextTool extends AbstractToggleButtonTool implements MouseClickTool |
| 28 | { |
| 29 | public TextTool() |
| 30 | { |
| 31 | _panel = new TextToolPanel(); |
| 32 | _field.setBorder(new EmptyBorder(0, 0 , 0, 0)); |
| 33 | _field.setOpaque(false); |
| 34 | _field.setName("toolbar-text-field"); |
| 35 | _field.setSize(VIRTUAL_FIELD_SIZE, VIRTUAL_FIELD_SIZE); |
| 36 | _field.setDocument(_panel.getFieldDocument()); |
| 37 | } |
| 38 | |
| 39 | public DrawingAction execute(JComponent area, Point location) |
| 40 | { |
| 41 | Color penColor = (Color) _properties.get(ColorPropertyTool.TOOL_PROPERTY_COLOR); |
| 42 | Font font = (Font) _properties.get(FontPropertyTool.TOOL_PROPERTY_FONT); |
| 43 | |
| 44 | // Create a text field in the drawing area to show immediate feedback |
| 45 | _field.setFont(font); |
| 46 | _field.setForeground(penColor); |
| 47 | area.add(_field); |
| 48 | FontMetrics metrics = _field.getFontMetrics(font); |
| 49 | _field.setLocation(location.x, location.y - metrics.getAscent()); |
| 50 | // Display dialog to enter text content |
| 51 | boolean accepted = _application.showDialog(_panel); |
| 52 | // Don't forget to remove feedback field from drawing are |
| 53 | area.remove(_field); |
| 54 | if (!accepted) |
| 55 | { |
| 56 | area.repaint(); |
| 57 | return null; |
| 58 | } |
| 59 | TextAreaInfo info = new TextAreaInfo(_field); |
| 60 | return new TextDrawingAction( location.x, |
| 61 | location.y, |
| 62 | info.getLines(), |
| 63 | font, |
| 64 | penColor, |
| 65 | info.getLineHeight()); |
| 66 | } |
| 67 | |
| 68 | static final private int VIRTUAL_FIELD_SIZE = 10000; |
| 69 | |
| 70 | final private TextToolPanel _panel; |
| 71 | final private JTextArea _field = new TextPaintingComponent(); |
| 72 | } |