1 11 12 package org.objectweb.jac.ide.diagrams; 13 14 import java.awt.Container ; 15 import java.awt.Dimension ; 16 import java.awt.Font ; 17 import java.awt.Rectangle ; 18 import java.awt.event.ActionListener ; 19 import java.awt.event.KeyListener ; 20 import javax.swing.JTextField ; 21 22 32 public class FloatingTextField { 33 34 private JTextField fEditWidget; 35 private Container fContainer; 36 37 public FloatingTextField() { 38 fEditWidget = new JTextField (20); 39 } 40 41 44 public void createOverlay(Container container) { 45 createOverlay(container, null); 46 } 47 48 52 public void createOverlay(Container container, Font font) { 53 container.add(fEditWidget, 0); 54 if (font != null) { 55 fEditWidget.setFont(font); 56 } 57 fContainer = container; 58 } 59 60 63 public void addActionListener(ActionListener listener) { 64 fEditWidget.addActionListener(listener); 65 } 66 67 70 public void removeActionListener(ActionListener listener) { 71 fEditWidget.removeActionListener(listener); 72 } 73 74 77 public void addKeyListener(KeyListener listener) { 78 fEditWidget.addKeyListener(listener); 79 } 80 81 84 public void removeKeyListener(KeyListener listener) { 85 fEditWidget.removeKeyListener(listener); 86 } 87 88 91 public void setBounds(Rectangle r, String text) { 92 fEditWidget.setText(text); 93 fEditWidget.setBounds(r.x, r.y, r.width, r.height); 94 fEditWidget.setVisible(true); 95 fEditWidget.selectAll(); 96 fEditWidget.requestFocus(); 97 } 98 99 102 public String getText() { 103 return fEditWidget.getText(); 104 } 105 106 109 public Dimension getPreferredSize(int cols) { 110 fEditWidget.setColumns(cols); 111 return fEditWidget.getPreferredSize(); 112 } 113 114 117 public void endOverlay() { 118 fContainer.requestFocus(); 119 if (fEditWidget != null) { 120 fEditWidget.setVisible(false); 121 fContainer.remove(fEditWidget); 122 123 Rectangle bounds = fEditWidget.getBounds(); 124 fContainer.repaint(bounds.x, bounds.y, bounds.width, bounds.height); 125 } 126 } 127 } 128 129 | Popular Tags |