1
/*FreeMind - A Program for creating and viewing Mindmaps
2
*Copyright (C) 2000-2005 Joerg Mueller, Daniel Polansky, Christian Foltin and others.
4
*See COPYING for Details
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.
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.
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.
20
* Created on 11.03.2005
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;
25
import java.awt.event.ActionEvent;
27
import java.io.FileReader;
28
import java.io.FileWriter;
29
import java.io.IOException;
30
import java.io.StringWriter;
32
import javax.swing.JOptionPane;
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;
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.
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.
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.
56
public class RevertAction extends FreemindAction implements ActorXml {
58
private final MindMapController controller;
62
public RevertAction(MindMapController modeController) {
63
super("RevertAction", (String) null, modeController);
64
controller = modeController;
72
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
74
public void actionPerformed(ActionEvent arg0) {
76
File file = controller.getMap().getFile();
78
JOptionPane.showMessageDialog(controller.getView(), controller
79
.getText("map_not_saved"), "FreeMind", JOptionPane.ERROR_MESSAGE);
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);
97
public void openXmlInsteadOfMap(String xmlFileContent) {
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);
112
public RevertXmlAction createRevertXmlAction(File file)
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);
120
return createRevertXmlAction(buffer.toString(), fileName, null);
123
public RevertXmlAction createRevertXmlAction(MindMap map, String fileName, String filePrefix)
125
StringWriter writer = new StringWriter();
127
return createRevertXmlAction(writer.getBuffer().toString(), fileName,
132
* @param filePrefix is used to generate the name of the reverted map in case that fileName is null.
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;
146
* @see freemind.controller.actions.ActorXml#act(freemind.controller.actions.generated.instance.XmlAction)
148
public void act(XmlAction action) {
149
if (action instanceof RevertXmlAction) {
151
RevertXmlAction revertAction = (RevertXmlAction) action;
153
// close the old map.
154
controller.getController().close(true);
155
if (revertAction.getLocalFileName() != null) {
156
controller.load(new File(revertAction.getLocalFileName()));
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();
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());
169
controller.load(tempFile);
171
} catch (Exception e) {
172
freemind.main.Resources.getInstance().logException(e);
180
* @see freemind.controller.actions.ActorXml#getDoActionClass()
182
public Class getDoActionClass() {
183
return RevertXmlAction.class;