~ubuntu-branches/ubuntu/utopic/freemind/utopic

« back to all changes in this revision

Viewing changes to freemind/accessories/plugins/ChangeNodeLevelAction.java

  • Committer: Bazaar Package Importer
  • Author(s): Benjamin Drung
  • Date: 2010-01-03 14:19:19 UTC
  • mfrom: (2.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20100103141919-m5az7dkicy21hqop
Tags: 0.9.0~rc6+dfsg-1ubuntu1
* Merge from Debian unstable (LP: #182927), remaining changes:
  - debian/copyright: add license/copyright for
    freemind/freemind/main/ExampleFileFilter.java

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*FreeMind - A Program for creating and viewing Mindmaps
 
2
 *Copyright (C) 2000-2006 by Christian Foltin
 
3
 *See COPYING for Details
 
4
 *
 
5
 *This program is free software; you can redistribute it and/or
 
6
 *modify it under the terms of the GNU General Public License
 
7
 *as published by the Free Software Foundation; either version 2
 
8
 *of the License, or (at your option) any later version.
 
9
 *
 
10
 *This program is distributed in the hope that it will be useful,
 
11
 *but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *GNU General Public License for more details.
 
14
 *
 
15
 *You should have received a copy of the GNU General Public License
 
16
 *along with this program; if not, write to the Free Software
 
17
 *Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
18
 */
 
19
/* $Id: ChangeNodeLevelAction.java,v 1.1.2.2.2.2 2007/04/21 15:11:20 dpolivaev Exp $ */
 
20
 
 
21
/*
 
22
 * Created on 19.02.2006
 
23
 *
 
24
 */
 
25
 
 
26
package accessories.plugins;
 
27
 
 
28
import java.awt.datatransfer.Transferable;
 
29
import java.util.Iterator;
 
30
import java.util.LinkedList;
 
31
import java.util.List;
 
32
import java.util.Vector;
 
33
 
 
34
import freemind.main.Tools;
 
35
import freemind.modes.MindMapNode;
 
36
import freemind.modes.mindmapmode.hooks.MindMapNodeHookAdapter;
 
37
 
 
38
/**
 
39
 * @author foltin
 
40
 */
 
41
public class ChangeNodeLevelAction extends MindMapNodeHookAdapter {
 
42
 
 
43
        /**
 
44
         *
 
45
         */
 
46
        public ChangeNodeLevelAction() {
 
47
                super();
 
48
        }
 
49
 
 
50
        /*
 
51
         * (non-Javadoc)
 
52
         *
 
53
         * @see freemind.extensions.NodeHook#invoke(freemind.modes.MindMapNode,
 
54
         *      java.util.List)
 
55
         */
 
56
        public void invoke(MindMapNode rootNode) {
 
57
                // we dont need node.
 
58
                MindMapNode selectedNode;
 
59
                List selectedNodes;
 
60
                {
 
61
                        MindMapNode focussed = getMindMapController().getSelected();
 
62
                        List selecteds = getMindMapController().getSelecteds();
 
63
                        selectedNode = focussed;
 
64
                        selectedNodes = selecteds;
 
65
                }
 
66
 
 
67
                // bug fix: sort to make independent by user's selection:
 
68
                getMindMapController().sortNodesByDepth(selectedNodes);
 
69
 
 
70
                if (selectedNode.isRoot()) {
 
71
                        getMindMapController().getController().errorMessage(
 
72
                                        getResourceString("cannot_add_parent_to_root"));
 
73
                        return;
 
74
                }
 
75
 
 
76
                boolean upwards = Tools.safeEquals("left",
 
77
                                getResourceString("action_type")) != selectedNode.isLeft();
 
78
                // Make sure the selected nodes all have the same parent
 
79
                // (this restriction is to simplify the action, and could
 
80
                // possibly be removed in the future, when we have undo)
 
81
                // Also make sure that none of the selected nodes are the root node
 
82
                MindMapNode selectedParent = selectedNode.getParentNode();
 
83
                for (Iterator it = selectedNodes.iterator(); it.hasNext();) {
 
84
                        MindMapNode node = (MindMapNode) it.next();
 
85
                        if (node.getParentNode() != selectedParent) {
 
86
                                getMindMapController().getController().errorMessage(
 
87
                                                getResourceString("cannot_add_parent_diff_parents"));
 
88
                                return;
 
89
                        }
 
90
                        if (node == rootNode) {
 
91
                                getMindMapController().getController().errorMessage(
 
92
                                                getResourceString("cannot_add_parent_to_root"));
 
93
                                return;
 
94
                        }
 
95
                }
 
96
 
 
97
        // collect node ids:
 
98
        String selectedNodeId = selectedNode.getObjectId(getController());
 
99
        Vector selectedNodesId = new Vector();
 
100
        for (Iterator iter = selectedNodes.iterator(); iter.hasNext();)
 
101
        {
 
102
            MindMapNode node = (MindMapNode) iter.next();
 
103
            selectedNodesId.add(node.getObjectId(getController()));
 
104
        }
 
105
 
 
106
                if (upwards) {
 
107
                        if (selectedParent.isRoot()) {
 
108
                                // change side of the items:
 
109
                                boolean isLeft = selectedNode.isLeft();
 
110
                                Transferable copy = getMindMapController().cut(selectedNodes);
 
111
                                getMindMapController().paste(copy, selectedParent, false, !isLeft);
 
112
                                select(selectedNodeId, selectedNodesId);
 
113
                                return;
 
114
                        }
 
115
                        // determine child pos of parent
 
116
                        MindMapNode grandParent = selectedParent.getParentNode();
 
117
                        int parentPosition = grandParent.getChildPosition(selectedParent);
 
118
                        boolean isLeft = selectedParent.isLeft();
 
119
                        Transferable copy = getMindMapController().cut(selectedNodes);
 
120
                        if (parentPosition == grandParent.getChildCount() - 1) {
 
121
                                getMindMapController().paste(copy, grandParent, false, isLeft);
 
122
                        } else {
 
123
                                getMindMapController().paste(
 
124
                                                copy,
 
125
                                                (MindMapNode) grandParent
 
126
                                                                .getChildAt(parentPosition + 1), true, isLeft);
 
127
                        }
 
128
                        select(selectedNodeId, selectedNodesId);
 
129
 
 
130
                } else {
 
131
                        int ownPosition = selectedParent.getChildPosition(selectedNode);
 
132
                        // find node above the own nodes:
 
133
                        MindMapNode directSibling = null;
 
134
                        for(int i = ownPosition - 1; i >= 0; --i) {
 
135
                                MindMapNode sibling = (MindMapNode) selectedParent.getChildAt(i);
 
136
                                if((! selectedNodes.contains(sibling)) &&
 
137
                        selectedNode.isLeft() == sibling.isLeft()){
 
138
                                        directSibling = sibling;
 
139
                                        break;
 
140
                                }
 
141
                        }
 
142
            if(directSibling == null) {
 
143
                // start searching for a sibling after the selected block:
 
144
                for(int i = ownPosition + 1; i < selectedParent.getChildCount() ; ++i) {
 
145
                    MindMapNode sibling = (MindMapNode) selectedParent.getChildAt(i);
 
146
                    if((! selectedNodes.contains(sibling)) &&
 
147
                            selectedNode.isLeft() == sibling.isLeft()){
 
148
                        directSibling = sibling;
 
149
                        break;
 
150
                    }
 
151
                }
 
152
            }
 
153
                        if(directSibling != null){
 
154
                                // sibling on the same side found:
 
155
                                Transferable copy = getMindMapController().cut(selectedNodes);
 
156
                                getMindMapController().paste(copy, directSibling, false, directSibling.isLeft());
 
157
                                select(selectedNodeId, selectedNodesId);
 
158
                                return;
 
159
            }
 
160
                }
 
161
        }
 
162
 
 
163
        private void select(String selectedNodeId, List selectedNodesIds) {
 
164
                // get new nodes by object id:
 
165
                MindMapNode newInstanceOfSelectedNode = getMindMapController().getNodeFromID(selectedNodeId);
 
166
                List newSelecteds = new LinkedList();
 
167
                for (Iterator iter = selectedNodesIds.iterator(); iter.hasNext();) {
 
168
                        String nodeId = (String) iter.next();
 
169
                        newSelecteds.add(getMindMapController().getNodeFromID(nodeId));
 
170
                }
 
171
                getMindMapController().selectMultipleNodes(newInstanceOfSelectedNode, newSelecteds);
 
172
        }
 
173
 
 
174
}