1 package jimm.datavision.gui; 2 import jimm.datavision.field.Field; 3 import jimm.datavision.gui.cmd.TypingCommand; 4 import jimm.util.I18N; 5 import java.awt.Cursor ; 6 import java.awt.Font ; 7 import java.awt.FontMetrics ; 8 import java.awt.event.MouseEvent ; 9 import java.awt.event.KeyEvent ; 10 import java.awt.event.ActionEvent ; 11 import javax.swing.JTextPane ; 12 import javax.swing.text.BadLocationException ; 13 14 20 public class TextFieldWidget extends FieldWidget { 21 22 protected int origHeight; 23 protected int lineHeight; 24 protected boolean changingEditState; 25 protected TypingCommand typingCommand; 26 27 33 public TextFieldWidget(SectionWidget sw, Field field) { 34 super(sw, field); 35 36 Font f = field.getFormat().getFont(); 38 FontMetrics fm = swingField.getComponent().getFontMetrics(f); 39 lineHeight = fm.getHeight(); 40 } 41 42 protected String getPopupNameText() { 43 return I18N.get(I18N.MENU_FILE_PREFIX, "TextFieldWidget.popup_name"); 45 } 46 47 protected void addCustomPopupItems() { 48 MenuUtils.addToMenu(this, popup, "TextFieldWidget.popup_edit", POPUP_FONT); 49 popup.addSeparator(); 50 } 51 52 56 public void actionPerformed(ActionEvent e) { 57 String command = e.getActionCommand(); 58 if (command == null) return; 59 60 if ("edit".equals(command)) 61 startEditing(); 62 else 63 super.actionPerformed(e); 64 } 65 66 72 public void mouseClicked(MouseEvent e) { 73 if (e.getClickCount() == 2) 74 startEditing(); 75 else 76 super.mouseClicked(e); 77 } 78 79 82 public void startEditing() { 83 synchronized(this) { 85 if (changingEditState) 86 return; 87 changingEditState = true; 88 } 89 90 if (!selected) 91 select(true); 92 93 JTextPane textPane = (JTextPane )getComponent(); 94 textPane.setEditable(true); 95 textPane.getCaret().setVisible(true); 96 textPane.requestFocus(); 97 origHeight = textPane.getBounds().height; 98 99 sectionWidget.designer.setIgnoreKeys(true); 100 101 textPane.removeMouseListener(this); 102 textPane.removeMouseMotionListener(this); 103 104 changingEditState = false; 105 106 typingCommand = new TypingCommand(this, origHeight); 107 } 108 109 113 protected void endEditing() { 114 synchronized(this) { 116 if (changingEditState) 117 return; 118 changingEditState = true; 119 } 120 121 if (typingCommand != null) { 122 getSectionWidget().performCommand(typingCommand); 123 typingCommand = null; 124 } 125 126 changingEditState = false; 127 } 128 129 134 protected void doSelect(boolean makeSelected) { 135 if (makeSelected == false) 136 endEditing(); 137 super.doSelect(makeSelected); 138 } 139 140 146 protected void cursorForPosition(MouseEvent e) { 147 JTextPane textPane = (JTextPane )getComponent(); 148 if (textPane.isEditable()) 149 textPane.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR)); 150 else 151 super.cursorForPosition(e); 152 } 153 154 160 public void keyTyped(KeyEvent e) { 161 char c = e.getKeyChar(); 162 int pos; 163 java.awt.Rectangle bounds; 164 JTextPane textPane = (JTextPane )getComponent(); 165 166 switch (c) { 167 case KeyEvent.VK_ENTER: 168 bounds = getComponent().getBounds(); 170 bounds.height += lineHeight; 171 getComponent().setBounds(bounds); 172 173 sectionWidget.growToFit(); 175 break; 176 case KeyEvent.VK_BACK_SPACE: 177 try { 178 pos = textPane.getCaretPosition(); 179 if (pos > 0 && textPane.getText(pos - 1, 1).equals("\n")) { 180 bounds = textPane.getBounds(); 181 bounds.height -= lineHeight; 182 textPane.setBounds(bounds); 183 } 184 } 185 catch (BadLocationException ex) {} 186 break; 187 case KeyEvent.VK_DELETE: 188 try { 189 pos = textPane.getCaretPosition(); 190 if (pos < textPane.getText().length() 191 && textPane.getText(pos, 1).equals("\n")) 192 { 193 bounds = textPane.getBounds(); 194 bounds.height -= lineHeight; 195 textPane.setBounds(bounds); 196 } 197 } 198 catch (BadLocationException ex2) {} 199 break; 200 } 201 } 202 203 } 204 | Popular Tags |