Back to index

moin  1.9.0~rc2
URLTool.java
Go to the documentation of this file.
00001 /*
00002  * @(#)URLTool.java 5.1
00003  *
00004  */
00005 
00006 package CH.ifa.draw.figures;
00007 
00008 import java.awt.*;
00009 import java.awt.event.*;
00010 import java.net.URLEncoder;
00011 import CH.ifa.draw.framework.*;
00012 import CH.ifa.draw.standard.*;
00013 import CH.ifa.draw.util.FloatingTextField;
00014 
00025 public class URLTool extends CreationTool {
00026 
00027     private FloatingTextField   fTextField;
00028     private Figure  fTypingTarget;
00029     private static Font dialogFont = Font.decode("dialog-PLAIN-12");
00030 
00031     public URLTool(DrawingView view, Figure prototype) {
00032         super(view, prototype);
00033     }
00034 
00039     public void mouseDown(MouseEvent e, int x, int y)
00040     {
00041        Figure pressedFigure;
00042 
00043        pressedFigure = drawing().findFigureInside(x, y);
00044        if (pressedFigure != null) {
00045            beginEdit(pressedFigure);
00046        } else if (fTypingTarget != null) {
00047            editor().toolDone();
00048            endEdit();
00049        }
00050     }
00051 
00052     public void mouseDrag(MouseEvent e, int x, int y) {
00053     }
00054 
00055     public void mouseUp(MouseEvent e, int x, int y) {
00056     }
00057 
00061     public void deactivate() {
00062         super.deactivate();
00063         endEdit();
00064     }
00065 
00069     public void activate() {
00070         super.activate();
00071         view().clearSelection();
00072         // JDK1.1 TEXT_CURSOR has an incorrect hot spot
00073         //view.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
00074     }
00075 
00076     protected void beginEdit(Figure figure) {
00077         if (fTextField == null)
00078             fTextField = new FloatingTextField();
00079 
00080        if (figure != fTypingTarget && fTypingTarget != null)
00081            endEdit();
00082 
00083         fTextField.createOverlay((Container)view(), dialogFont);
00084        fTextField.setBounds(fieldBounds(figure),
00085                           (String)figure.getAttribute("Sensitive"));
00086        fTypingTarget = figure;
00087     }
00088 
00089     protected void endEdit() {
00090        if (fTypingTarget != null) {
00091            String s = URLEncoder.encode(fTextField.getText());
00092            fTypingTarget.setAttribute("Sensitive", s);
00093            fTypingTarget = null;
00094            fTextField.endOverlay();
00095            view().checkDamage();
00096        }
00097     }
00098 
00099     private Rectangle fieldBounds(Figure figure) {
00100        Rectangle box = figure.displayBox();
00101         Dimension d = fTextField.getPreferredSize(1, 20);
00102         return new Rectangle(box.x, box.y, d.width, d.height);
00103     }
00104 }
00105