KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > actions > MoveDownAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.actions;
20
21 import org.openide.nodes.Index;
22 import org.openide.nodes.Node;
23 import org.openide.util.HelpCtx;
24 import org.openide.util.NbBundle;
25 import org.openide.util.actions.*;
26
27 import java.lang.ref.Reference JavaDoc;
28 import java.lang.ref.WeakReference JavaDoc;
29
30 import javax.swing.event.ChangeEvent JavaDoc;
31 import javax.swing.event.ChangeListener JavaDoc;
32
33
34 /** Move an item down in a list.
35 * This action is final only for performance reasons.
36 * @see Index
37 *
38 * @author Ian Formanek, Dafe Simonek
39 */

40 public final class MoveDownAction extends NodeAction {
41     /** the key to listener to reorder of selected nodes */
42     private static final String JavaDoc PROP_ORDER_LISTENER = "sellistener"; // NOI18N
43

44     /** Holds index cookie on which we are listening */
45     private Reference JavaDoc curIndexCookie;
46
47     /* Initilizes the set of properties.
48     */

49     protected void initialize() {
50         super.initialize();
51
52         // initializes the listener
53
OrderingListener sl = new OrderingListener();
54         putProperty(PROP_ORDER_LISTENER, sl);
55     }
56
57     /** Getter for curIndexCookie */
58     private Index getCurIndexCookie() {
59         return ((curIndexCookie == null) ? null : (Index) curIndexCookie.get());
60     }
61
62     protected void performAction(Node[] activatedNodes) {
63         // we need to check activatedNodes, because there's no
64
// guarantee that they not changed between enable() and
65
// performAction calls
66
Index cookie = getIndexCookie(activatedNodes);
67
68         if (cookie == null) {
69             return;
70         }
71
72         int nodeIndex = cookie.indexOf(activatedNodes[0]);
73
74         if ((nodeIndex >= 0) && (nodeIndex < (cookie.getNodesCount() - 1))) {
75             cookie.moveDown(nodeIndex);
76         }
77     }
78
79     protected boolean asynchronous() {
80         return false;
81     }
82
83     protected boolean enable(Node[] activatedNodes) {
84         // remove old listener, if any
85
Index idx = getCurIndexCookie();
86
87         if (idx != null) {
88             idx.removeChangeListener((ChangeListener JavaDoc) getProperty(PROP_ORDER_LISTENER));
89             idx = null;
90         }
91
92         Index cookie = getIndexCookie(activatedNodes);
93
94         if (cookie == null) {
95             return false;
96         }
97
98         int nodeIndex = cookie.indexOf(activatedNodes[0]);
99
100         // now start listening to reordering changes
101
cookie.addChangeListener((OrderingListener) getProperty(PROP_ORDER_LISTENER));
102         curIndexCookie = new WeakReference JavaDoc(cookie);
103
104         return (nodeIndex >= 0) && (nodeIndex < (cookie.getNodesCount() - 1));
105     }
106
107     public String JavaDoc getName() {
108         return NbBundle.getMessage(MoveDownAction.class, "MoveDown");
109     }
110
111     public HelpCtx getHelpCtx() {
112         return new HelpCtx(MoveDownAction.class);
113     }
114
115     /** Helper method. Returns index cookie or null, if some
116     * conditions aren't satisfied */

117     private Index getIndexCookie(Node[] activatedNodes) {
118         if ((activatedNodes == null) || (activatedNodes.length != 1)) {
119             return null;
120         }
121
122         Node parent = activatedNodes[0].getParentNode();
123
124         if (parent == null) {
125             return null;
126         }
127
128         return (Index) parent.getCookie(Index.class);
129     }
130
131     /** Listens to the ordering changes and enables/disables the
132     * action if appropriate */

133     private final class OrderingListener implements ChangeListener JavaDoc {
134         OrderingListener() {
135         }
136
137         public void stateChanged(ChangeEvent JavaDoc e) {
138             Node[] activatedNodes = getActivatedNodes();
139             Index cookie = getIndexCookie(activatedNodes);
140
141             if (cookie == null) {
142                 setEnabled(false);
143             } else {
144                 int nodeIndex = cookie.indexOf(activatedNodes[0]);
145                 setEnabled((nodeIndex >= 0) && (nodeIndex < (cookie.getNodesCount() - 1)));
146             }
147         }
148     }
149 }
150
Popular Tags