KickJava   Java API By Example, From Geeks To Geeks.

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


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.TreePath JavaDoc;
25 import org.netbeans.modules.tasklist.usertasks.UserTaskListTreeTableNode;
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.model.UserTaskObjectList;
30 import org.netbeans.modules.tasklist.usertasks.treetable.AdvancedTreeTableNode;
31 import org.netbeans.modules.tasklist.usertasks.util.UTUtils;
32 import org.openide.util.NbBundle;
33
34 /**
35  * Moves a task to the left.
36  *
37  * @author tl
38  */

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

47     public MoveLeftAction(UserTaskView utv) {
48         super(utv, NbBundle.getMessage(MoveLeftAction.class,
49                 "MoveLeft")); // NOI18N
50
}
51     
52     public void actionPerformed(ActionEvent JavaDoc event) {
53         // figuring out where we are
54
TreePath JavaDoc sel = utv.getTreeTable().getSelectedPath();
55         UserTaskTreeTableNode n =
56                 (UserTaskTreeTableNode) sel.getLastPathComponent();
57         UserTaskTreeTableNode parent = (UserTaskTreeTableNode) n.getParent();
58         AdvancedTreeTableNode newParent =
59                 (AdvancedTreeTableNode) parent.getParent();
60         TreePath JavaDoc newParentPath = sel.getParentPath().getParentPath();
61         TreePath JavaDoc[] expanded = utv.getTreeTable().getExpandedNodesUnder(
62                 newParentPath);
63         UserTask[] expandedTasks = new UserTask[expanded.length];
64         for (int i = 0; i < expanded.length ; i++) {
65             if (expanded[i].getLastPathComponent() instanceof
66                     UserTaskTreeTableNode) {
67                 expandedTasks[i] = ((UserTaskTreeTableNode)
68                     expanded[i].getLastPathComponent()).getUserTask();
69             }
70         }
71         utv.getTreeTable().clearSelection();
72         int selColumn = utv.getTreeTable().getSelectedColumn();
73
74         // moving the task
75
UserTask ut = n.getUserTask();
76         int index = UTUtils.identityIndexOf(ut.getParentObjectList(), ut);
77         while (ut.getParentObjectList().size() > index + 1) {
78             ut.getSubtasks().add(ut.getParentObjectList().remove(index + 1));
79         }
80         ut.getParentObjectList().remove(ut);
81         UserTaskObjectList newParentList;
82         if (newParent instanceof UserTaskTreeTableNode)
83             newParentList = ((UserTaskTreeTableNode) newParent).
84                     getUserTask().getSubtasks();
85         else
86             newParentList = ((UserTaskListTreeTableNode) newParent).
87                     getUserTaskList().getSubtasks();
88         int parentIndex = UTUtils.identityIndexOf(
89                 newParentList, parent.getUserTask());
90         newParentList.add(parentIndex + 1, ut);
91         
92         // expanding and selecting nodes
93
index = newParent.getIndexOfObject(ut);
94         if (index >= 0) {
95             TreePath JavaDoc newPath = newParentPath.pathByAddingChild(
96                     newParent.getChildAt(index));
97             utv.getTreeTable().expandPath(newPath);
98             for (int i = 0; i < expandedTasks.length; i++) {
99                 if (expandedTasks[i] != null) {
100                     TreePath JavaDoc p = utv.getTreeTable().findPath(expandedTasks[i]);
101                     if (p != null)
102                         utv.getTreeTable().expandPath(p);
103                 }
104             }
105             utv.getTreeTable().select(newPath);
106             utv.getTreeTable().getColumnModel().getSelectionModel().
107                     addSelectionInterval(selColumn, selColumn);
108         }
109     }
110
111     public void valueChanged(ListSelectionEvent JavaDoc e) {
112         TreePath JavaDoc[] sel = utv.getTreeTable().getSelectedPaths();
113         if (utv.getTreeTable().getSortingModel().getSortedColumn() == -1 &&
114                 sel.length == 1) {
115             Object JavaDoc last = sel[0].getLastPathComponent();
116             if (last instanceof UserTaskTreeTableNode) {
117                 UserTaskTreeTableNode n = (UserTaskTreeTableNode) last;
118                 setEnabled(n.getUserTask().getParent() != null);
119             } else {
120                 setEnabled(false);
121             }
122         } else {
123             setEnabled(false);
124         }
125     }
126 }
127
Popular Tags