1 11 12 package org.objectweb.jac.ide.diagrams; 13 14 import CH.ifa.draw.framework.DrawingEditor; 15 import CH.ifa.draw.framework.Figure; 16 import CH.ifa.draw.standard.TextHolder; 17 import org.objectweb.jac.util.Log; 18 import java.awt.Container ; 19 import java.awt.Dimension ; 20 import java.awt.Rectangle ; 21 import java.awt.event.ActionEvent ; 22 import java.awt.event.ActionListener ; 23 import java.awt.event.KeyEvent ; 24 import java.awt.event.KeyListener ; 25 import java.awt.event.MouseEvent ; 26 27 39 public class TextTool extends CreationTool 40 implements ActionListener , KeyListener 41 { 42 43 private FloatingTextField fTextField; 44 private TextHolder fTypingTarget; 45 46 public TextTool(DrawingEditor newDrawingEditor, Figure prototype) { 47 super(newDrawingEditor, prototype); 48 } 49 50 54 public void mouseDown(MouseEvent e, int x, int y) 55 { 56 TextHolder textHolder = null; 57 Figure pressedFigure = drawing().findFigureInside(x, y); 58 if (pressedFigure instanceof TextHolder) { 59 textHolder = (TextHolder) pressedFigure; 60 if (!textHolder.acceptsTyping()) 61 textHolder = null; 62 } 63 if (textHolder != null) { 64 beginEdit(textHolder); 65 return; 66 } 67 if (getTypingTarget() != null) { 68 endEdit(); 69 editor().toolDone(); 70 } else { 71 super.mouseDown(e, x, y); 72 view().checkDamage(); 76 textHolder = (TextHolder)getCreatedFigure(); 77 beginEdit(textHolder); 78 } 79 } 80 81 public void mouseDrag(MouseEvent e, int x, int y) { 82 } 83 84 public void mouseUp(MouseEvent e, int x, int y) { 85 } 86 87 90 public void deactivate() { 91 endEdit(); 92 super.deactivate(); 93 } 94 95 98 public void activate() { 99 super.activate(); 100 view().clearSelection(); 101 } 104 105 111 public boolean isActivated() { 112 return getTypingTarget() != null; 113 } 114 115 public void actionPerformed(ActionEvent e) { 117 endEdit(); 118 } 119 120 public void keyPressed(KeyEvent e) { 122 int code = e.getKeyCode(); 123 if (code==KeyEvent.VK_ESCAPE) { 124 abortEdit(); 125 } 126 } 127 public void keyReleased(KeyEvent e) {} 128 public void keyTyped(KeyEvent e) {} 129 130 protected void beginEdit(TextHolder figure) { 131 if (fTextField == null) { 132 fTextField = new FloatingTextField(); 133 } 134 135 if (figure != getTypingTarget() && getTypingTarget() != null) { 136 endEdit(); 137 } 138 139 fTextField.addActionListener(this); 140 fTextField.addKeyListener(this); 141 fTextField.createOverlay((Container )view(), figure.getFont()); 142 fTextField.setBounds(fieldBounds(figure), figure.getText()); 143 144 setTypingTarget(figure); 145 } 146 147 protected void endEdit() { 148 Log.trace("diagram","End edit"); 149 if (getTypingTarget() != null) { 150 if (fTextField.getText().length() > 0) { 151 getTypingTarget().setText(fTextField.getText()); 152 } else { 153 drawing().orphan((Figure)getAddedFigure()); 154 } 155 156 setTypingTarget(null); 157 fTextField.endOverlay(); 158 fTextField.removeActionListener(this); 159 fTextField.removeKeyListener(this); 160 } 162 } 163 164 protected void abortEdit() { 165 Log.trace("diagram","Abort edit"); 166 if (getTypingTarget() != null) { 167 setTypingTarget(null); 168 fTextField.endOverlay(); 169 fTextField.removeActionListener(this); 170 fTextField.removeKeyListener(this); 171 } 173 } 174 175 private Rectangle fieldBounds(TextHolder figure) { 176 Rectangle box = figure.textDisplayBox(); 177 int nChars = figure.overlayColumns(); 178 Dimension d = fTextField.getPreferredSize(nChars); 179 return new Rectangle (box.x, box.y, d.width, d.height); 180 } 181 182 protected void setTypingTarget(TextHolder newTypingTarget) { 183 fTypingTarget = newTypingTarget; 184 } 185 186 protected TextHolder getTypingTarget() { 187 return fTypingTarget; 188 } 189 190 } 191 192 | Popular Tags |