KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.logging.Logger JavaDoc;
22 import org.openide.nodes.Index;
23 import org.openide.nodes.Node;
24 import org.openide.util.HelpCtx;
25 import org.openide.util.NbBundle;
26 import org.openide.util.actions.*;
27
28 import java.lang.ref.Reference JavaDoc;
29 import java.lang.ref.WeakReference JavaDoc;
30
31 import java.util.Arrays JavaDoc;
32
33 import javax.swing.event.ChangeEvent JavaDoc;
34 import javax.swing.event.ChangeListener JavaDoc;
35
36
37 /** Move an item up in a list.
38 *
39 * @see Index
40 * @author Ian Formanek, Jan Jancura, Dafe Simonek
41 */

42 public final class MoveUpAction extends NodeAction {
43     /** the key to listener to reorder of selected nodes */
44     private static final String JavaDoc PROP_ORDER_LISTENER = "sellistener"; // NOI18N
45
private static Logger JavaDoc err = Logger.getLogger("org.openide.actions.MoveUpAction"); // NOI18N
46

47     /** Holds index cookie on which we are listening */
48     private Reference JavaDoc curIndexCookie;
49
50     /* Initilizes the set of properties.
51     */

52     protected void initialize() {
53         err.fine("initialize");
54
55         super.initialize();
56
57         // initializes the listener
58
OrderingListener sl = new OrderingListener();
59         putProperty(PROP_ORDER_LISTENER, sl);
60     }
61
62     /** Getter for curIndexCookie */
63     private Index getCurIndexCookie() {
64         return ((curIndexCookie == null) ? null : (Index) curIndexCookie.get());
65     }
66
67     protected void performAction(Node[] activatedNodes) {
68         // we need to check activatedNodes, because there's no
69
// guarantee that they not changed between enable() and
70
// performAction calls
71
Index cookie = getIndexCookie(activatedNodes);
72
73         if (cookie == null) {
74             return;
75         }
76
77         int nodeIndex = cookie.indexOf(activatedNodes[0]);
78
79         if (nodeIndex > 0) {
80             cookie.moveUp(nodeIndex);
81         }
82     }
83
84     protected boolean asynchronous() {
85         return false;
86     }
87
88     protected boolean enable(Node[] activatedNodes) {
89         err.fine(
90             "enable; activatedNodes=" + ((activatedNodes == null) ? null : Arrays.asList(activatedNodes))
91         );
92
93         // remove old listener, if any
94
Index idx = getCurIndexCookie();
95
96         if (idx != null) {
97             idx.removeChangeListener((ChangeListener JavaDoc) getProperty(PROP_ORDER_LISTENER));
98         }
99
100         Index cookie = getIndexCookie(activatedNodes);
101
102         if (err != null) {
103             err.fine("enable; cookie=" + cookie);
104         }
105
106         if (cookie == null) {
107             return false;
108         }
109
110         // now start listening to reordering changes
111
cookie.addChangeListener((OrderingListener) getProperty(PROP_ORDER_LISTENER));
112         curIndexCookie = new WeakReference JavaDoc(cookie);
113
114         int index = cookie.indexOf(activatedNodes[0]);
115
116         if (err != null) {
117             err.fine("enable; index=" + index);
118
119             if (index == -1) {
120                 Node parent = activatedNodes[0].getParentNode();
121                 err.fine(
122                     "enable; parent=" + parent + "; parent.children=" + Arrays.asList(parent.getChildren().getNodes())
123                 );
124             }
125         }
126
127         return index > 0;
128     }
129
130     public String JavaDoc getName() {
131         return NbBundle.getMessage(MoveUpAction.class, "MoveUp");
132     }
133
134     public HelpCtx getHelpCtx() {
135         return new HelpCtx(MoveUpAction.class);
136     }
137
138     /** Helper method. Returns index cookie or null, if some
139     * conditions weren't satisfied */

140     private Index getIndexCookie(Node[] activatedNodes) {
141         if ((activatedNodes == null) || (activatedNodes.length != 1)) {
142             return null;
143         }
144
145         Node parent = activatedNodes[0].getParentNode();
146
147         if (parent == null) {
148             return null;
149         }
150
151         return (Index) parent.getCookie(Index.class);
152     }
153
154     /** Listens to the ordering changes and enables/disables the
155     * action if appropriate */

156     private final class OrderingListener implements ChangeListener JavaDoc {
157         OrderingListener() {
158         }
159
160         public void stateChanged(ChangeEvent JavaDoc e) {
161             Node[] activatedNodes = getActivatedNodes();
162
163             err.fine(
164                 "stateChanged; activatedNodes=" + ((activatedNodes == null) ? null : Arrays.asList(activatedNodes))
165             );
166
167             Index cookie = getIndexCookie(activatedNodes);
168
169             if (err != null) {
170                 err.fine("stateChanged; cookie=" + cookie);
171             }
172
173             if (cookie == null) {
174                 setEnabled(false);
175             } else {
176                 int index = cookie.indexOf(activatedNodes[0]);
177
178                 if (err != null) {
179                     err.fine("stateChanged; index=" + index);
180                 }
181
182                 setEnabled(index > 0);
183             }
184         }
185     }
186 }
187
Popular Tags