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

« back to all changes in this revision

Viewing changes to freemind/freemind/modes/mindmapmode/actions/IconAction.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-2004  Joerg Mueller, Daniel Polansky, Christian Foltin and others.
 
3
 *
 
4
 *See COPYING for Details
 
5
 *
 
6
 *This program is free software; you can redistribute it and/or
 
7
 *modify it under the terms of the GNU General Public License
 
8
 *as published by the Free Software Foundation; either version 2
 
9
 *of the License, or (at your option) any later version.
 
10
 *
 
11
 *This program is distributed in the hope that it will be useful,
 
12
 *but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *GNU General Public License for more details.
 
15
 *
 
16
 *You should have received a copy of the GNU General Public License
 
17
 *along with this program; if not, write to the Free Software
 
18
 *Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
 *
 
20
 * Created on 29.09.2004
 
21
 */
 
22
/* $Id: IconAction.java,v 1.1.2.2.2.8 2008/04/10 20:49:21 dpolivaev Exp $ */
 
23
 
 
24
package freemind.modes.mindmapmode.actions;
 
25
 
 
26
import java.awt.event.ActionEvent;
 
27
import java.awt.event.MouseEvent;
 
28
import java.util.ListIterator;
 
29
 
 
30
import javax.swing.Action;
 
31
import javax.swing.ImageIcon;
 
32
import javax.swing.KeyStroke;
 
33
import javax.swing.SwingConstants;
 
34
 
 
35
import freemind.controller.actions.generated.instance.AddIconAction;
 
36
import freemind.controller.actions.generated.instance.XmlAction;
 
37
import freemind.controller.filter.condition.IconContainedCondition;
 
38
import freemind.main.Tools;
 
39
import freemind.modes.IconInformation;
 
40
import freemind.modes.MindIcon;
 
41
import freemind.modes.MindMapNode;
 
42
import freemind.modes.mindmapmode.MindMapController;
 
43
import freemind.modes.mindmapmode.MindMapNodeModel;
 
44
import freemind.modes.mindmapmode.actions.xml.ActionPair;
 
45
import freemind.modes.mindmapmode.actions.xml.ActorXml;
 
46
 
 
47
 
 
48
public class IconAction extends FreemindAction  implements ActorXml, IconInformation{
 
49
    public MindIcon icon;
 
50
    private final MindMapController modeController;
 
51
    private final RemoveIconAction removeLastIconAction;
 
52
    public IconAction(MindMapController controller, MindIcon _icon, RemoveIconAction removeLastIconAction) {
 
53
        super(_icon.getDescription(), _icon.getIcon(), controller);
 
54
        this.modeController = controller;
 
55
        this.removeLastIconAction = removeLastIconAction;
 
56
        putValue(Action.SHORT_DESCRIPTION, _icon.getDescription());
 
57
        this.icon = _icon;
 
58
        controller.getActionFactory().registerActor(this, getDoActionClass());
 
59
    }
 
60
 
 
61
    public void actionPerformed(ActionEvent e) {
 
62
           if(e.getID() == ActionEvent.ACTION_FIRST && (e.getModifiers() & ActionEvent.SHIFT_MASK & ~ActionEvent.CTRL_MASK & ~ActionEvent.ALT_MASK) != 0) 
 
63
           {
 
64
                   removeAllIcons();
 
65
                   addLastIcon();
 
66
                   return;
 
67
           }
 
68
           if(e == null || (e.getModifiers() & (ActionEvent.CTRL_MASK | ActionEvent.ALT_MASK)) == 0){
 
69
                   addLastIcon();
 
70
                   return;
 
71
           }
 
72
           // e != null
 
73
           if((e.getModifiers() & ~ActionEvent.SHIFT_MASK & ~ActionEvent.CTRL_MASK & ActionEvent.ALT_MASK) != 0) 
 
74
           {
 
75
                  removeIcon(false);
 
76
                   return;
 
77
           }
 
78
           if((e.getModifiers() & ~ActionEvent.SHIFT_MASK &  ActionEvent.CTRL_MASK & ~ActionEvent.ALT_MASK) != 0) 
 
79
           {
 
80
                   removeIcon(true);
 
81
                   return;
 
82
           }
 
83
    }
 
84
 
 
85
        private void addLastIcon() {
 
86
                for (ListIterator it = modeController.getSelecteds().listIterator();it.hasNext();) {
 
87
               MindMapNodeModel selected = (MindMapNodeModel)it.next();
 
88
               addIcon(selected, icon);
 
89
           }
 
90
        }
 
91
 
 
92
        private void removeIcon(boolean removeFirst) {
 
93
                for (ListIterator it = modeController.getSelecteds().listIterator();it.hasNext();) {
 
94
               MindMapNodeModel selected = (MindMapNodeModel)it.next();
 
95
               removeIcon(selected, icon, removeFirst);
 
96
           }
 
97
        }
 
98
 
 
99
        private void toggleIcon() {
 
100
                for (ListIterator it = modeController.getSelecteds().listIterator();it.hasNext();) {
 
101
               MindMapNodeModel selected = (MindMapNodeModel)it.next();
 
102
               toggleIcon(selected, icon);
 
103
           }
 
104
        }
 
105
 
 
106
        private void removeAllIcons() {
 
107
                for (ListIterator it = modeController.getSelecteds().listIterator();it.hasNext();) {
 
108
               MindMapNodeModel selected = (MindMapNodeModel)it.next();
 
109
               if(selected.getIcons().size() > 0) {
 
110
                        modeController.removeAllIcons(selected);
 
111
               }
 
112
             }
 
113
        }
 
114
 
 
115
    public void addIcon(MindMapNode node, MindIcon icon) {
 
116
        modeController.getActionFactory().startTransaction(
 
117
                (String) getValue(NAME));
 
118
        modeController.getActionFactory().executeAction(
 
119
                getAddLastIconActionPair(node, icon));
 
120
        modeController.getActionFactory().endTransaction(
 
121
                (String) getValue(NAME));
 
122
    }
 
123
 
 
124
    private void toggleIcon(MindMapNode node, MindIcon icon) {
 
125
        modeController.getActionFactory().startTransaction(
 
126
                (String) getValue(NAME));
 
127
        modeController.getActionFactory().executeAction(
 
128
                getToggleIconActionPair(node, icon));
 
129
        modeController.getActionFactory().endTransaction(
 
130
                (String) getValue(NAME));
 
131
    }
 
132
 
 
133
    private void removeIcon(MindMapNode node, MindIcon icon, boolean removeFirst) {
 
134
        final ActionPair removeIconActionPair = getRemoveIconActionPair(node, icon, removeFirst);
 
135
        if(removeIconActionPair == null){
 
136
                return;
 
137
        }
 
138
        modeController.getActionFactory().startTransaction(
 
139
                (String) getValue(NAME));
 
140
                modeController.getActionFactory().executeAction(
 
141
                removeIconActionPair);
 
142
        modeController.getActionFactory().endTransaction(
 
143
                (String) getValue(NAME));
 
144
    }
 
145
 
 
146
    /**
 
147
     */
 
148
    private ActionPair getAddLastIconActionPair(MindMapNode node, MindIcon icon)  {
 
149
        int iconIndex = MindIcon.LAST;
 
150
        return getAddIconActionPair(node, icon, iconIndex);
 
151
    }
 
152
 
 
153
        private ActionPair getAddIconActionPair(MindMapNode node, MindIcon icon,
 
154
                        int iconIndex) {
 
155
                AddIconAction doAction = createAddIconAction(node, icon, iconIndex);
 
156
        XmlAction undoAction = removeLastIconAction.createRemoveIconXmlAction(node,
 
157
                        iconIndex);
 
158
        return new ActionPair(doAction, undoAction);
 
159
        }
 
160
 
 
161
    /**
 
162
     */
 
163
    private ActionPair getToggleIconActionPair(MindMapNode node, MindIcon icon)  {
 
164
        int iconIndex = IconContainedCondition.iconFirstIndex(node, icon.getName());
 
165
        if(iconIndex == -1){
 
166
                return getAddLastIconActionPair(node, icon);
 
167
        }
 
168
        else{
 
169
                return getRemoveIconActionPair(node, icon, iconIndex);
 
170
        }
 
171
    }
 
172
 
 
173
    /**
 
174
     * @param removeFirst 
 
175
     */
 
176
    private ActionPair getRemoveIconActionPair(MindMapNode node, MindIcon icon, boolean removeFirst)  {
 
177
        int iconIndex = removeFirst ? 
 
178
                        IconContainedCondition.iconFirstIndex(node, icon.getName())
 
179
                        :IconContainedCondition.iconLastIndex(node, icon.getName());
 
180
        return iconIndex >= 0 ? getRemoveIconActionPair(node, icon, iconIndex) : null;
 
181
    }
 
182
 
 
183
        private ActionPair getRemoveIconActionPair(MindMapNode node,
 
184
                        MindIcon icon, int iconIndex) {
 
185
                XmlAction doAction = removeLastIconAction.createRemoveIconXmlAction(node, iconIndex);
 
186
        XmlAction undoAction =  createAddIconAction(node, icon, iconIndex);
 
187
        return new ActionPair(doAction, undoAction);
 
188
        }
 
189
 
 
190
    public void act(XmlAction action) {
 
191
        if (action instanceof AddIconAction) {
 
192
            AddIconAction iconAction = (AddIconAction) action;
 
193
            MindMapNode node = modeController.getNodeFromID(iconAction.getNode());
 
194
            String iconName = iconAction.getIconName();
 
195
            int position = iconAction.getIconPosition();
 
196
            MindIcon icon = MindIcon.factory(iconName);
 
197
            node.addIcon(icon, position);
 
198
            modeController.nodeChanged(node);
 
199
        }
 
200
    }
 
201
 
 
202
    public Class getDoActionClass() {
 
203
        return AddIconAction.class;
 
204
    }
 
205
    public AddIconAction createAddIconAction(MindMapNode node, MindIcon icon, int iconPosition) {
 
206
        AddIconAction action = new AddIconAction();
 
207
        action.setNode(node.getObjectId(modeController));
 
208
        action.setIconName(icon.getName());
 
209
        action.setIconPosition(iconPosition);
 
210
        return action;
 
211
    }
 
212
 
 
213
        public MindIcon getMindIcon() {
 
214
                return icon;
 
215
        }
 
216
 
 
217
        public KeyStroke getKeyStroke() {
 
218
                final String keystrokeResourceName = icon.getKeystrokeResourceName();
 
219
                final String keyStrokeDescription = getMindMapController().getFrame().getAdjustableProperty(keystrokeResourceName);
 
220
                return Tools.getKeyStroke(keyStrokeDescription);
 
221
        }
 
222
 
 
223
        public String getDescription() {
 
224
                return icon.getDescription();
 
225
        }
 
226
 
 
227
        public ImageIcon getIcon() {
 
228
                return icon.getIcon();
 
229
        }
 
230
 
 
231
        public String getKeystrokeResourceName() {
 
232
                return icon.getKeystrokeResourceName();
 
233
        }
 
234
 
 
235
}