KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > actions > MoveRightAction


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
20 package org.netbeans.modules.tasklist.usertasks.actions;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import javax.swing.event.ListSelectionEvent JavaDoc;
24 import javax.swing.tree.TreeNode JavaDoc;
25 import javax.swing.tree.TreePath JavaDoc;
26 import org.netbeans.modules.tasklist.usertasks.UserTaskTreeTableNode;
27 import org.netbeans.modules.tasklist.usertasks.UserTaskView;
28 import org.netbeans.modules.tasklist.usertasks.model.UserTask;
29 import org.netbeans.modules.tasklist.usertasks.util.UTUtils;
30 import org.openide.util.NbBundle;
31
32 /**
33  * Moves a task to the left.
34  *
35  * @author tl
36  */

37 public class MoveRightAction extends UTViewAction {
38     private static final long serialVersionUID = 1;
39
40     /**
41      * Creates a new instance.
42      *
43      * @param utv a view
44      */

45     public MoveRightAction(UserTaskView utv) {
46         super(utv, NbBundle.getMessage(MoveRightAction.class,
47                 "MoveRight")); // NOI18N
48
}
49     
50     public void actionPerformed(ActionEvent JavaDoc event) {
51         // figuring out where we are
52
TreePath JavaDoc sel = utv.getTreeTable().getSelectedPath();
53         UserTaskTreeTableNode n =
54                 (UserTaskTreeTableNode) sel.getLastPathComponent();
55         TreeNode JavaDoc parent = n.getParent();
56         int index = parent.getIndex(n);
57         UserTaskTreeTableNode newParent =
58                 (UserTaskTreeTableNode) parent.getChildAt(index - 1);
59         TreePath JavaDoc newParentPath = sel.getParentPath().
60                 pathByAddingChild(newParent);
61         TreePath JavaDoc[] expanded = utv.getTreeTable().getExpandedNodesUnder(sel);
62         UserTask[] expandedTasks = new UserTask[expanded.length];
63         for (int i = 0; i < expanded.length ; i++) {
64             expandedTasks[i] = ((UserTaskTreeTableNode)
65                 expanded[i].getLastPathComponent()).getUserTask();
66         }
67         utv.getTreeTable().clearSelection();
68
69         // moving the task
70
UserTask ut = n.getUserTask();
71         ut.getParentObjectList().remove(ut);
72         newParent.getUserTask().getSubtasks().add(ut);
73         
74         // expanding and selecting nodes
75
utv.getTreeTable().expandPath(newParentPath);
76         index = newParent.getIndexOfObject(ut);
77         if (index >= 0) {
78             for (int i = 0; i < expandedTasks.length; i++) {
79                 TreePath JavaDoc p = utv.getTreeTable().findPath(expandedTasks[i]);
80                 if (p != null)
81                     utv.getTreeTable().expandPath(p);
82             }
83             utv.getTreeTable().select(newParentPath.pathByAddingChild(
84                     newParent.getChildAt(index)));
85         }
86     }
87
88     public void valueChanged(ListSelectionEvent JavaDoc e) {
89         TreePath JavaDoc[] sel = utv.getTreeTable().getSelectedPaths();
90         if (utv.getTreeTable().getSortingModel().getSortedColumn() == -1 &&
91                 sel.length == 1) {
92             Object JavaDoc last = sel[0].getLastPathComponent();
93             if (last instanceof UserTaskTreeTableNode) {
94                 UserTaskTreeTableNode n = (UserTaskTreeTableNode) last;
95                 TreeNode JavaDoc parent = n.getParent();
96                 if (parent == null)
97                     UTUtils.LOGGER.fine(n.getUserTask() +
98                             " does not have a parent"); // NOI18N
99
int index = parent.getIndex(n);
100                 setEnabled(index != 0);
101             } else {
102                 setEnabled(false);
103             }
104         } else {
105             setEnabled(false);
106         }
107     }
108 }
109
Popular Tags