001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.openide.explorer.view;
042:
043: import org.openide.nodes.Node;
044: import org.openide.util.NbBundle;
045:
046: import java.awt.Component;
047: import java.awt.Point;
048: import java.awt.Rectangle;
049: import java.awt.event.ActionEvent;
050: import java.awt.event.FocusEvent;
051: import java.awt.event.FocusListener;
052: import java.awt.event.KeyEvent;
053: import java.awt.event.MouseEvent;
054: import java.awt.event.MouseMotionListener;
055:
056: import java.util.EventObject;
057:
058: import javax.swing.DefaultCellEditor;
059: import javax.swing.JComponent;
060: import javax.swing.JTextField;
061: import javax.swing.JTree;
062: import javax.swing.KeyStroke;
063: import javax.swing.SwingUtilities;
064: import javax.swing.event.CellEditorListener;
065: import javax.swing.event.ChangeEvent;
066: import javax.swing.tree.DefaultTreeCellEditor;
067: import javax.swing.tree.DefaultTreeCellRenderer;
068: import javax.swing.tree.TreeCellEditor;
069: import javax.swing.tree.TreePath;
070: import org.openide.DialogDisplayer;
071: import org.openide.NotifyDescriptor;
072: import org.openide.util.Exceptions;
073:
074: /** In-place editor in the tree view component.
075: *
076: * @author Petr Hamernik
077: */
078: class TreeViewCellEditor extends DefaultTreeCellEditor implements
079: CellEditorListener, FocusListener, MouseMotionListener {
080: /** generated Serialized Version UID */
081: static final long serialVersionUID = -2171725285964032312L;
082:
083: // Attributes
084:
085: /** Indicates whether is drag and drop currently active or not */
086: boolean dndActive = false;
087:
088: /** True, if the editation was cancelled by the user.
089: */
090: private boolean cancelled = false;
091:
092: /** Stopped is true, if the editation is over (editingStopped is called for the
093: first time). The two variables have virtually the same function, but are kept
094: separate for code clarity.
095: */
096: private boolean stopped = false;
097:
098: /** Construct a cell editor.
099: * @param tree the tree
100: */
101: public TreeViewCellEditor(JTree tree) {
102: //Use a dummy DefaultTreeCellEditor - we'll set up the correct
103: //icon when we fetch the editor component (see EOF). Not sure
104: //it's wildly vaulable to subclass DefaultTreeCellEditor here -
105: //we override most everything
106: super (tree, new DefaultTreeCellRenderer());
107:
108: // deal with selection if already exists
109: if (tree.getSelectionCount() == 1) {
110: lastPath = tree.getSelectionPath();
111: }
112:
113: addCellEditorListener(this );
114: }
115:
116: /** Implements <code>CellEditorListener</code> interface method. */
117: public void editingStopped(ChangeEvent e) {
118: //CellEditor sometimes(probably after stopCellEditing() call) gains one focus but loses two
119: if (stopped) {
120: return;
121: }
122:
123: stopped = true;
124:
125: TreePath lastP = lastPath;
126:
127: if (lastP != null) {
128: Node n = Visualizer.findNode(lastP.getLastPathComponent());
129:
130: if ((n != null) && n.canRename()) {
131: String newStr = (String) getCellEditorValue();
132:
133: try {
134: // bugfix #21589 don't update name if there is not any change
135: if (!n.getName().equals(newStr)) {
136: n.setName(newStr);
137: }
138: } catch (IllegalArgumentException exc) {
139: boolean needToAnnotate = Exceptions
140: .findLocalizedMessage(exc) == null;
141:
142: // annotate new localized message only if there is no localized message yet
143: if (needToAnnotate) {
144: String msg = NbBundle.getMessage(
145: TreeViewCellEditor.class,
146: "RenameFailed", n.getName(), newStr);
147: Exceptions.attachLocalizedMessage(exc, msg);
148: }
149:
150: Exceptions.printStackTrace(exc);
151: }
152: }
153: }
154: }
155:
156: /** Implements <code>CellEditorListener</code> interface method. */
157: public void editingCanceled(ChangeEvent e) {
158: cancelled = true;
159: }
160:
161: /** Overrides superclass method. If the source is a <code>JTextField</code>,
162: * i.e. cell editor, it cancels editing, otherwise it calls superclass method. */
163: public void actionPerformed(ActionEvent evt) {
164: if (evt.getSource() instanceof JTextField) {
165: cancelled = true;
166: cancelCellEditing();
167: } else {
168: super .actionPerformed(evt);
169: }
170: }
171:
172: /** Implements <code>FocusListener</code> interface method. */
173: public void focusLost(FocusEvent evt) {
174: if (stopped || cancelled) {
175: return;
176: }
177:
178: if (!stopCellEditing()) {
179: cancelCellEditing();
180: }
181: }
182:
183: /** Dummy implementation of <code>FocusListener</code> interface method. */
184: public void focusGained(FocusEvent evt) {
185: }
186:
187: /**
188: * This is invoked if a TreeCellEditor is not supplied in the constructor.
189: * It returns a TextField editor.
190: */
191: protected TreeCellEditor createTreeCellEditor() {
192: JTextField tf = new JTextField() {
193: public void addNotify() {
194: stopped = cancelled = false;
195: super .addNotify();
196: requestFocus();
197: }
198: };
199:
200: tf.registerKeyboardAction( //TODO update to use inputMap/actionMap
201: this , KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0,
202: true), JComponent.WHEN_FOCUSED);
203:
204: tf.addFocusListener(this );
205:
206: Ed ed = new Ed(tf);
207: ed.setClickCountToStart(1);
208: ed.getComponent().getAccessibleContext()
209: .setAccessibleDescription(
210: NbBundle.getMessage(TreeViewCellEditor.class,
211: "ACSD_TreeViewCellEditor")); // NOI18N
212: ed.getComponent().getAccessibleContext().setAccessibleName(
213: NbBundle.getMessage(TreeViewCellEditor.class,
214: "ACSN_TreeViewCellEditor")); // NOI18N
215:
216: return ed;
217: }
218:
219: /*
220: * If the realEditor returns true to this message, prepareForEditing
221: * is messaged and true is returned.
222: */
223: public boolean isCellEditable(EventObject event) {
224: if ((event != null) && (event instanceof MouseEvent)) {
225: if (!SwingUtilities.isLeftMouseButton((MouseEvent) event)
226: || ((MouseEvent) event).isPopupTrigger()) {
227: return false;
228: }
229: }
230:
231: if (lastPath != null) {
232: Node n = Visualizer.findNode(lastPath
233: .getLastPathComponent());
234:
235: if ((n == null) || !n.canRename()) {
236: return false;
237: }
238: } else {
239: // Disallow rename when multiple nodes are selected
240: return false;
241: }
242:
243: // disallow editing if we are in DnD operation
244: if (dndActive) {
245: return false;
246: }
247:
248: return super .isCellEditable(event);
249: }
250:
251: protected void determineOffset(JTree tree, Object value,
252: boolean sel, boolean expanded, boolean leaf, int row) {
253: if (renderer != null) {
254: renderer.getTreeCellRendererComponent(tree, value, sel,
255: expanded, leaf, row, true);
256: editingIcon = renderer.getIcon();
257:
258: if (editingIcon != null) {
259: offset = renderer.getIconTextGap()
260: + editingIcon.getIconWidth();
261: } else {
262: offset = 0;
263: }
264: } else {
265: editingIcon = null;
266: offset = 0;
267: }
268: }
269:
270: /*** Sets the state od drag and drop operation.
271: * It's here only because of JTree's bug which allows to
272: * start the editing even if DnD operation occurs
273: * (bug # )
274: */
275: void setDnDActive(boolean dndActive) {
276: if (!dndActive) {
277: tree.removeMouseMotionListener(this );
278: }
279:
280: this .dndActive = dndActive;
281: }
282:
283: protected void setTree(JTree newTree) {
284: if ((newTree != tree) && (timer != null) && timer.isRunning()) {
285: tree.removeMouseMotionListener(this );
286: }
287:
288: super .setTree(newTree);
289: }
290:
291: // bugfix #33765, cancel timer if the mouse leaves a selection rectangle
292: public void mouseDragged(MouseEvent e) {
293: Point p = e.getPoint();
294: boolean b = checkContinueTimer(p);
295:
296: if (!b) {
297: abortTimer();
298: }
299: }
300:
301: public void mouseMoved(MouseEvent e) {
302: Point p = e.getPoint();
303: boolean b = checkContinueTimer(p);
304:
305: if (!b) {
306: abortTimer();
307: }
308: }
309:
310: private void abortTimer() {
311: if ((timer != null) && timer.isRunning()) {
312: timer.stop();
313: tree.removeMouseMotionListener(this );
314: }
315: }
316:
317: protected void startEditingTimer() {
318: tree.addMouseMotionListener(this );
319: super .startEditingTimer();
320: }
321:
322: protected void prepareForEditing() {
323: abortTimer();
324: tree.removeMouseMotionListener(this );
325:
326: super .prepareForEditing();
327: }
328:
329: private boolean checkContinueTimer(Point p) {
330: Rectangle r = tree.getPathBounds(tree.getSelectionPath());
331:
332: if (r == null) {
333: return false;
334: }
335:
336: return (r.contains(p));
337: }
338:
339: /** Redefined default cell editor to convert nodes to name */
340: class Ed extends DefaultCellEditor {
341: /** generated Serialized Version UID */
342: static final long serialVersionUID = -6373058702842751408L;
343:
344: public Ed(JTextField tf) {
345: super (tf);
346: }
347:
348: /** Main method of the editor.
349: * @return component of editor
350: */
351: public Component getTreeCellEditorComponent(JTree tree,
352: Object value, boolean isSelected, boolean expanded,
353: boolean leaf, int row) {
354: Node ren = Visualizer.findNode(value);
355:
356: if ((ren != null) && (ren.canRename())) {
357: delegate.setValue(ren.getName());
358: } else {
359: delegate.setValue(""); // NOI18N
360: }
361:
362: editingIcon = ((VisualizerNode) value).getIcon(expanded,
363: false);
364:
365: ((JTextField) editorComponent).selectAll();
366:
367: return editorComponent;
368: }
369: }
370: }
|