1
/*FreeMind - A Program for creating and viewing Mindmaps
2
*Copyright (C) 2000-2004 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 23.06.2004
22
/*$Id: XmlBindingTools.java,v 1.1.2.2.2.5 2009/05/20 19:19:11 christianfoltin Exp $*/
24
package freemind.common;
26
import java.awt.Dimension;
27
import java.awt.Frame;
28
import java.awt.Toolkit;
29
import java.io.Reader;
30
import java.io.StringReader;
31
import java.io.StringWriter;
33
import javax.swing.JDialog;
34
import javax.swing.JOptionPane;
36
import org.jibx.runtime.BindingDirectory;
37
import org.jibx.runtime.IBindingFactory;
38
import org.jibx.runtime.IMarshallingContext;
39
import org.jibx.runtime.IUnmarshallingContext;
40
import org.jibx.runtime.JiBXException;
42
import freemind.controller.Controller;
43
import freemind.controller.actions.generated.instance.WindowConfigurationStorage;
44
import freemind.controller.actions.generated.instance.XmlAction;
45
import freemind.main.Resources;
51
public class XmlBindingTools {
53
private static XmlBindingTools instance;
54
private static IBindingFactory mBindingFactory;
57
private XmlBindingTools() {
60
public static XmlBindingTools getInstance() {
61
if(instance == null) {
62
instance = new XmlBindingTools();
64
mBindingFactory = BindingDirectory.getFactory(XmlAction.class);
65
} catch (JiBXException e) {
66
freemind.main.Resources.getInstance().logException(e);
74
public IMarshallingContext createMarshaller() {
76
return mBindingFactory.createMarshallingContext();
77
} catch (JiBXException e) {
78
freemind.main.Resources.getInstance().logException(e);
83
public IUnmarshallingContext createUnmarshaller() {
85
return mBindingFactory.createUnmarshallingContext();
86
} catch (JiBXException e) {
87
freemind.main.Resources.getInstance().logException(e);
92
public void storeDialogPositions(Controller controller, JDialog dialog, WindowConfigurationStorage storage, String window_preference_storage_property) {
93
storage.setX((dialog.getX()));
94
storage.setY((dialog.getY()));
95
storage.setWidth((dialog.getWidth()));
96
storage.setHeight((dialog.getHeight()));
97
String marshalled = marshall(storage);
98
String result = marshalled;
99
controller.setProperty(window_preference_storage_property, result);
102
public WindowConfigurationStorage decorateDialog(Controller controller, JDialog dialog, String window_preference_storage_property) {
103
String marshalled = controller.getProperty(window_preference_storage_property);
104
WindowConfigurationStorage result = decorateDialog(marshalled, dialog);
108
public WindowConfigurationStorage decorateDialog(String marshalled, JDialog dialog) {
109
// String unmarshalled = controller.getProperty(
111
if (marshalled != null) {
112
WindowConfigurationStorage storage = (WindowConfigurationStorage) unMarshall(marshalled);
113
if (storage != null) {
114
// Check that location is on current screen.
115
Dimension screenSize;
116
if(Resources.getInstance().getBoolProperty("place_dialogs_on_first_screen")) {
117
Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
118
screenSize = defaultToolkit.getScreenSize();
120
screenSize = new Dimension();
121
screenSize.height = Integer.MAX_VALUE;
122
screenSize.width = Integer.MAX_VALUE;
125
dialog.setLocation(Math.min(storage.getX(), screenSize.width-delta),
126
Math.min(storage.getY(), screenSize.height-delta));
127
dialog.setSize(new Dimension(storage.getWidth(), storage.getHeight()));
132
// set standard dialog size of no size is stored
133
final Frame rootFrame = JOptionPane.getFrameForComponent(dialog);
134
final Dimension prefSize = rootFrame.getSize();
135
prefSize.width = prefSize.width * 3 / 4;
136
prefSize.height = prefSize.height * 3 / 4;
137
dialog.setSize(prefSize);
142
public String marshall(XmlAction action) {
144
//marshal to StringBuffer:
145
StringWriter writer = new StringWriter();
146
IMarshallingContext m = XmlBindingTools.getInstance().createMarshaller();
148
m.marshalDocument(action, "UTF-8", null,
150
} catch (JiBXException e) {
151
freemind.main.Resources.getInstance().logException(e);
154
String result = writer.toString();
159
public XmlAction unMarshall(String inputString) {
160
return unMarshall(new StringReader( inputString ));
165
public XmlAction unMarshall(Reader reader) {
168
IUnmarshallingContext u = XmlBindingTools.getInstance().createUnmarshaller();
169
XmlAction doAction = (XmlAction) u.unmarshalDocument
172
} catch (JiBXException e) {
173
freemind.main.Resources.getInstance().logException(e);