1 19 package org.openide.explorer.view; 20 21 import org.openide.nodes.Node; 22 import org.openide.util.NbBundle; 23 24 import java.awt.Component ; 25 import java.awt.Point ; 26 import java.awt.Rectangle ; 27 import java.awt.event.ActionEvent ; 28 import java.awt.event.FocusEvent ; 29 import java.awt.event.FocusListener ; 30 import java.awt.event.KeyEvent ; 31 import java.awt.event.MouseEvent ; 32 import java.awt.event.MouseMotionListener ; 33 34 import java.util.EventObject ; 35 36 import javax.swing.DefaultCellEditor ; 37 import javax.swing.JComponent ; 38 import javax.swing.JTextField ; 39 import javax.swing.JTree ; 40 import javax.swing.KeyStroke ; 41 import javax.swing.SwingUtilities ; 42 import javax.swing.event.CellEditorListener ; 43 import javax.swing.event.ChangeEvent ; 44 import javax.swing.tree.DefaultTreeCellEditor ; 45 import javax.swing.tree.DefaultTreeCellRenderer ; 46 import javax.swing.tree.TreeCellEditor ; 47 import javax.swing.tree.TreePath ; 48 import org.openide.DialogDisplayer; 49 import org.openide.NotifyDescriptor; 50 import org.openide.util.Exceptions; 51 52 53 57 class TreeViewCellEditor extends DefaultTreeCellEditor implements CellEditorListener , FocusListener , 58 MouseMotionListener { 59 60 static final long serialVersionUID = -2171725285964032312L; 61 62 64 65 boolean dndActive = false; 66 67 69 private boolean cancelled = false; 70 71 75 private boolean stopped = false; 76 77 80 public TreeViewCellEditor(JTree tree) { 81 super(tree, new DefaultTreeCellRenderer ()); 86 87 if (tree.getSelectionCount() == 1) { 89 lastPath = tree.getSelectionPath(); 90 } 91 92 addCellEditorListener(this); 93 } 94 95 96 public void editingStopped(ChangeEvent e) { 97 if (stopped) { 99 return; 100 } 101 102 stopped = true; 103 104 TreePath lastP = lastPath; 105 106 if (lastP != null) { 107 Node n = Visualizer.findNode(lastP.getLastPathComponent()); 108 109 if ((n != null) && n.canRename()) { 110 String newStr = (String ) getCellEditorValue(); 111 112 try { 113 if (!n.getName().equals(newStr)) { 115 n.setName(newStr); 116 } 117 } catch (IllegalArgumentException exc) { 118 boolean needToAnnotate = Exceptions.findLocalizedMessage(exc) == null; 119 120 if (needToAnnotate) { 122 String msg = NbBundle.getMessage(TreeViewCellEditor.class, "RenameFailed", n.getName(), newStr); 123 Exceptions.attachLocalizedMessage(exc, msg); 124 } 125 126 DialogDisplayer.getDefault().notifyLater(new NotifyDescriptor.Exception(exc)); 127 } 128 } 129 } 130 } 131 132 133 public void editingCanceled(ChangeEvent e) { 134 cancelled = true; 135 } 136 137 139 public void actionPerformed(ActionEvent evt) { 140 if (evt.getSource() instanceof JTextField ) { 141 cancelled = true; 142 cancelCellEditing(); 143 } else { 144 super.actionPerformed(evt); 145 } 146 } 147 148 149 public void focusLost(FocusEvent evt) { 150 if (stopped || cancelled) { 151 return; 152 } 153 154 if (!stopCellEditing()) { 155 cancelCellEditing(); 156 } 157 } 158 159 160 public void focusGained(FocusEvent evt) { 161 } 162 163 167 protected TreeCellEditor createTreeCellEditor() { 168 JTextField tf = new JTextField () { 169 public void addNotify() { 170 stopped = cancelled = false; 171 super.addNotify(); 172 requestFocus(); 173 } 174 }; 175 176 tf.registerKeyboardAction( this, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true), JComponent.WHEN_FOCUSED); 178 179 tf.addFocusListener(this); 180 181 Ed ed = new Ed(tf); 182 ed.setClickCountToStart(1); 183 ed.getComponent().getAccessibleContext().setAccessibleDescription( 184 NbBundle.getMessage(TreeViewCellEditor.class, "ACSD_TreeViewCellEditor") 185 ); ed.getComponent().getAccessibleContext().setAccessibleName( 187 NbBundle.getMessage(TreeViewCellEditor.class, "ACSN_TreeViewCellEditor") 188 ); 190 return ed; 191 } 192 193 197 public boolean isCellEditable(EventObject event) { 198 if ((event != null) && (event instanceof MouseEvent )) { 199 if (!SwingUtilities.isLeftMouseButton((MouseEvent ) event) || ((MouseEvent ) event).isPopupTrigger()) { 200 return false; 201 } 202 } 203 204 if (lastPath != null) { 205 Node n = Visualizer.findNode(lastPath.getLastPathComponent()); 206 207 if ((n == null) || !n.canRename()) { 208 return false; 209 } 210 } else { 211 return false; 213 } 214 215 if (dndActive) { 217 return false; 218 } 219 220 return super.isCellEditable(event); 221 } 222 223 protected void determineOffset(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row) { 224 if (renderer != null) { 225 renderer.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, true); 226 editingIcon = renderer.getIcon(); 227 228 if (editingIcon != null) { 229 offset = renderer.getIconTextGap() + editingIcon.getIconWidth(); 230 } else { 231 offset = 0; 232 } 233 } else { 234 editingIcon = null; 235 offset = 0; 236 } 237 } 238 239 244 void setDnDActive(boolean dndActive) { 245 if (!dndActive) { 246 tree.removeMouseMotionListener(this); 247 } 248 249 this.dndActive = dndActive; 250 } 251 252 protected void setTree(JTree newTree) { 253 if ((newTree != tree) && (timer != null) && timer.isRunning()) { 254 tree.removeMouseMotionListener(this); 255 } 256 257 super.setTree(newTree); 258 } 259 260 public void mouseDragged(MouseEvent e) { 262 Point p = e.getPoint(); 263 boolean b = checkContinueTimer(p); 264 265 if (!b) { 266 abortTimer(); 267 } 268 } 269 270 public void mouseMoved(MouseEvent e) { 271 Point p = e.getPoint(); 272 boolean b = checkContinueTimer(p); 273 274 if (!b) { 275 abortTimer(); 276 } 277 } 278 279 private void abortTimer() { 280 if ((timer != null) && timer.isRunning()) { 281 timer.stop(); 282 tree.removeMouseMotionListener(this); 283 } 284 } 285 286 protected void startEditingTimer() { 287 tree.addMouseMotionListener(this); 288 super.startEditingTimer(); 289 } 290 291 protected void prepareForEditing() { 292 abortTimer(); 293 tree.removeMouseMotionListener(this); 294 295 super.prepareForEditing(); 296 } 297 298 private boolean checkContinueTimer(Point p) { 299 Rectangle r = tree.getPathBounds(tree.getSelectionPath()); 300 301 if (r == null) { 302 return false; 303 } 304 305 return (r.contains(p)); 306 } 307 308 309 class Ed extends DefaultCellEditor { 310 311 static final long serialVersionUID = -6373058702842751408L; 312 313 public Ed(JTextField tf) { 314 super(tf); 315 } 316 317 320 public Component getTreeCellEditorComponent( 321 JTree tree, Object value, boolean isSelected, boolean expanded, boolean leaf, int row 322 ) { 323 Node ren = Visualizer.findNode(value); 324 325 if ((ren != null) && (ren.canRename())) { 326 delegate.setValue(ren.getName()); 327 } else { 328 delegate.setValue(""); } 330 331 editingIcon = ((VisualizerNode) value).getIcon(expanded, false); 332 333 ((JTextField ) editorComponent).selectAll(); 334 335 return editorComponent; 336 } 337 } 338 } 339 | Popular Tags |