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

« back to all changes in this revision

Viewing changes to freemind/freemind/common/XmlBindingTools.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 23.06.2004
 
21
 */
 
22
/*$Id: XmlBindingTools.java,v 1.1.2.2.2.5 2009/05/20 19:19:11 christianfoltin Exp $*/
 
23
 
 
24
package freemind.common;
 
25
 
 
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;
 
32
 
 
33
import javax.swing.JDialog;
 
34
import javax.swing.JOptionPane;
 
35
 
 
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;
 
41
 
 
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;
 
46
 
 
47
/**
 
48
 * @author foltin
 
49
 * Singleton
 
50
 */
 
51
public class XmlBindingTools {
 
52
 
 
53
        private static XmlBindingTools instance;
 
54
    private static IBindingFactory mBindingFactory;
 
55
 
 
56
 
 
57
    private XmlBindingTools() {
 
58
        }
 
59
 
 
60
        public static XmlBindingTools getInstance() {
 
61
                if(instance == null) {
 
62
                        instance = new XmlBindingTools();
 
63
            try {
 
64
                mBindingFactory = BindingDirectory.getFactory(XmlAction.class);
 
65
            } catch (JiBXException e) {
 
66
                freemind.main.Resources.getInstance().logException(e);
 
67
            }
 
68
 
 
69
                }
 
70
                return instance;
 
71
        }
 
72
 
 
73
        
 
74
        public IMarshallingContext createMarshaller() {
 
75
        try {
 
76
            return mBindingFactory.createMarshallingContext();
 
77
        } catch (JiBXException e) {
 
78
            freemind.main.Resources.getInstance().logException(e);
 
79
            return null;
 
80
        }
 
81
        }
 
82
        
 
83
        public IUnmarshallingContext createUnmarshaller() {
 
84
        try {
 
85
            return  mBindingFactory.createUnmarshallingContext();
 
86
        } catch (JiBXException e) {
 
87
            freemind.main.Resources.getInstance().logException(e);
 
88
            return null;
 
89
        }
 
90
        }
 
91
        
 
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);
 
100
        }
 
101
 
 
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);
 
105
                return result;
 
106
        }
 
107
        
 
108
    public WindowConfigurationStorage decorateDialog(String marshalled, JDialog dialog) {
 
109
//              String unmarshalled = controller.getProperty(
 
110
//                      propertyName);
 
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();
 
119
                                } else {
 
120
                                        screenSize = new Dimension();
 
121
                                        screenSize.height = Integer.MAX_VALUE;
 
122
                                        screenSize.width = Integer.MAX_VALUE;
 
123
                                }
 
124
                                int delta = 20;
 
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()));
 
128
                                return storage;
 
129
                        }
 
130
                }
 
131
                
 
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);
 
138
                return null;
 
139
    }
 
140
 
 
141
 
 
142
        public String marshall(XmlAction action) {
 
143
        // marshall:
 
144
        //marshal to StringBuffer:
 
145
        StringWriter writer = new StringWriter();
 
146
        IMarshallingContext m = XmlBindingTools.getInstance().createMarshaller();
 
147
        try {
 
148
            m.marshalDocument(action, "UTF-8", null,
 
149
                    writer);
 
150
        } catch (JiBXException e) {
 
151
            freemind.main.Resources.getInstance().logException(e);
 
152
            return null;
 
153
        }
 
154
        String result = writer.toString();
 
155
        return result;
 
156
 
 
157
        }
 
158
 
 
159
        public XmlAction unMarshall(String inputString) {
 
160
                return unMarshall(new StringReader( inputString ));
 
161
        }
 
162
 
 
163
    /**
 
164
     */
 
165
    public XmlAction unMarshall(Reader reader) {
 
166
        try {
 
167
                        // unmarshall:
 
168
            IUnmarshallingContext u = XmlBindingTools.getInstance().createUnmarshaller();
 
169
            XmlAction doAction = (XmlAction) u.unmarshalDocument
 
170
            ( reader  , null);
 
171
                        return doAction;
 
172
                } catch (JiBXException e) {
 
173
            freemind.main.Resources.getInstance().logException(e);
 
174
            return null;
 
175
        }
 
176
    }
 
177
 
 
178
 
 
179
}