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

« back to all changes in this revision

Viewing changes to freemind/freemind/modes/mindmapmode/actions/RevertAction.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-2005  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 11.03.2005
 
21
 */
 
22
/* $Id: RevertAction.java,v 1.1.2.2.2.10 2009/01/01 21:33:48 christianfoltin Exp $ */
 
23
package freemind.modes.mindmapmode.actions;
 
24
 
 
25
import java.awt.event.ActionEvent;
 
26
import java.io.File;
 
27
import java.io.FileReader;
 
28
import java.io.FileWriter;
 
29
import java.io.IOException;
 
30
import java.io.StringWriter;
 
31
 
 
32
import javax.swing.JOptionPane;
 
33
 
 
34
import freemind.controller.actions.generated.instance.RevertXmlAction;
 
35
import freemind.controller.actions.generated.instance.XmlAction;
 
36
import freemind.main.Tools;
 
37
import freemind.modes.MindMap;
 
38
import freemind.modes.mindmapmode.MindMapController;
 
39
import freemind.modes.mindmapmode.actions.xml.ActionPair;
 
40
import freemind.modes.mindmapmode.actions.xml.ActorXml;
 
41
 
 
42
/**
 
43
 * Reverts the map to the saved version. In Xml, the old map is stored as xml
 
44
 * and as an undo action, the new map is stored, too.
 
45
 *
 
46
 * Moreover, the filename of the doAction is set to the appropriate map file's
 
47
 * name. The undo action has no file name associated.
 
48
 *
 
49
 * The action goes like this: close the actual map and open the given Xml/File.
 
50
 * If only a Xml string is given, a temporary file name is created, the xml
 
51
 * stored into and this map is opened instead of the actual.
 
52
 *
 
53
 * @author foltin
 
54
 *
 
55
 */
 
56
public class RevertAction extends FreemindAction implements ActorXml {
 
57
 
 
58
        private final MindMapController controller;
 
59
 
 
60
        /**
 
61
         */
 
62
        public RevertAction(MindMapController modeController) {
 
63
                super("RevertAction", (String) null, modeController);
 
64
                controller = modeController;
 
65
                addActor(this);
 
66
 
 
67
        }
 
68
 
 
69
        /*
 
70
         * (non-Javadoc)
 
71
         *
 
72
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 
73
         */
 
74
        public void actionPerformed(ActionEvent arg0) {
 
75
                try {
 
76
                        File file = controller.getMap().getFile();
 
77
            if(file == null) {
 
78
                JOptionPane.showMessageDialog(controller.getView(), controller
 
79
                        .getText("map_not_saved"), "FreeMind", JOptionPane.ERROR_MESSAGE);
 
80
                return;
 
81
            }
 
82
                        RevertXmlAction doAction = createRevertXmlAction(file);
 
83
                        RevertXmlAction undoAction = createRevertXmlAction(controller
 
84
                                        .getMap(), null, file.getName());
 
85
                        controller.getActionFactory().startTransaction(
 
86
                                        this.getClass().getName());
 
87
                        controller.getActionFactory().executeAction(
 
88
                                        new ActionPair(doAction, undoAction));
 
89
                        controller.getActionFactory().endTransaction(
 
90
                                        this.getClass().getName());
 
91
                } catch (IOException e) {
 
92
freemind.main.Resources.getInstance().logException(                     e);
 
93
                }
 
94
 
 
95
        }
 
96
 
 
97
    public void openXmlInsteadOfMap(String xmlFileContent) {
 
98
        try {
 
99
            RevertXmlAction doAction = createRevertXmlAction(xmlFileContent, null, null);
 
100
            RevertXmlAction undoAction = createRevertXmlAction(controller
 
101
                    .getMap(), null, null);
 
102
            controller.getActionFactory().startTransaction(
 
103
                    this.getClass().getName());
 
104
            controller.getActionFactory().executeAction(
 
105
                    new ActionPair(doAction, undoAction));
 
106
            controller.getActionFactory().endTransaction(
 
107
                    this.getClass().getName());
 
108
        } catch (IOException e) {
 
109
            freemind.main.Resources.getInstance().logException(e);
 
110
        }
 
111
    }
 
112
        public RevertXmlAction createRevertXmlAction(File file)
 
113
                        throws IOException {
 
114
                String fileName = file.getAbsolutePath();
 
115
                FileReader f = new FileReader(file);
 
116
                StringBuffer buffer = new StringBuffer();
 
117
                for (int c; (c = f.read()) != -1;)
 
118
                        buffer.append((char) c);
 
119
                f.close();
 
120
                return createRevertXmlAction(buffer.toString(), fileName, null);
 
121
        }
 
122
 
 
123
        public RevertXmlAction createRevertXmlAction(MindMap map, String fileName, String filePrefix)
 
124
                        throws IOException {
 
125
                StringWriter writer = new StringWriter();
 
126
                map.getXml(writer);
 
127
                return createRevertXmlAction(writer.getBuffer().toString(), fileName,
 
128
                                filePrefix);
 
129
        }
 
130
 
 
131
        /**
 
132
         * @param filePrefix is used to generate the name of the reverted map in case that fileName is null.
 
133
         */
 
134
        public RevertXmlAction createRevertXmlAction(String xmlPackedFile,
 
135
                        String fileName, String filePrefix)  {
 
136
                RevertXmlAction revertXmlAction = new RevertXmlAction();
 
137
                revertXmlAction.setLocalFileName(fileName);
 
138
                revertXmlAction.setMap(xmlPackedFile);
 
139
                revertXmlAction.setFilePrefix(filePrefix);
 
140
                return revertXmlAction;
 
141
        }
 
142
 
 
143
        /*
 
144
         * (non-Javadoc)
 
145
         *
 
146
         * @see freemind.controller.actions.ActorXml#act(freemind.controller.actions.generated.instance.XmlAction)
 
147
         */
 
148
        public void act(XmlAction action) {
 
149
                if (action instanceof RevertXmlAction) {
 
150
                        try {
 
151
                                RevertXmlAction revertAction = (RevertXmlAction) action;
 
152
 
 
153
                                // close the old map.
 
154
                                controller.getController().close(true);
 
155
                                if (revertAction.getLocalFileName() != null) {
 
156
                                        controller.load(new File(revertAction.getLocalFileName()));
 
157
                                } else {
 
158
                                        // the map is given by xml. we store it and open it.
 
159
                                        String filePrefix = controller.getText("freemind_reverted");
 
160
                                        if (revertAction.getFilePrefix() != null) {
 
161
                                                filePrefix = revertAction.getFilePrefix();
 
162
                                        }
 
163
                                        File tempFile = File.createTempFile(filePrefix, freemind.main.FreeMindCommon.FREEMIND_FILE_EXTENSION,
 
164
                                                        new File(controller.getFrame()
 
165
                                                                        .getFreemindDirectory()));
 
166
                                        FileWriter fw = new FileWriter(tempFile);
 
167
                                        fw.write(revertAction.getMap());
 
168
                                        fw.close();
 
169
                                        controller.load(tempFile);
 
170
                                }
 
171
                        } catch (Exception e) {
 
172
                                freemind.main.Resources.getInstance().logException(e);
 
173
                        }
 
174
                }
 
175
        }
 
176
 
 
177
        /*
 
178
         * (non-Javadoc)
 
179
         *
 
180
         * @see freemind.controller.actions.ActorXml#getDoActionClass()
 
181
         */
 
182
        public Class getDoActionClass() {
 
183
                return RevertXmlAction.class;
 
184
        }
 
185
 
 
186
}