File: ToolbarSettingsDialog.java

package info (click to toggle)
writer2latex 1.6.1-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,456 kB
  • sloc: java: 33,474; xml: 4,710; javascript: 3,067; sh: 67; makefile: 29
file content (262 lines) | stat: -rw-r--r-- 9,546 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/************************************************************************
 *
 *  ToolbarSettingsDialog.java
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *  Copyright: 2002-2015 by Henrik Just
 *
 *  All Rights Reserved.
 * 
 *  Version 1.6 (2015-04-05)
 *
 */ 
 
package org.openoffice.da.comp.writer2xhtml;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import com.sun.star.awt.XContainerWindowEventHandler;
import com.sun.star.awt.XDialog;
import com.sun.star.awt.XWindow;
import com.sun.star.beans.XPropertySet;
import com.sun.star.lang.XServiceInfo;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.Exception;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.util.XChangesBatch;

import com.sun.star.lib.uno.helper.WeakBase;

import org.openoffice.da.comp.w2lcommon.helper.DialogAccess;
import org.openoffice.da.comp.w2lcommon.helper.FilePicker;
import org.openoffice.da.comp.w2lcommon.helper.RegistryHelper;
import org.openoffice.da.comp.w2lcommon.helper.XPropertySetHelper;

/** This class provides a uno component which implements the configuration
 *  of the writer2xhtml toolbar
 */
public final class ToolbarSettingsDialog
    extends WeakBase
    implements XServiceInfo, XContainerWindowEventHandler {
	
	public static final String REGISTRY_PATH = "/org.openoffice.da.Writer2xhtml.toolbar.ToolbarOptions/Settings";

    private XComponentContext xContext;
    private FilePicker filePicker;
    
    /** The component will be registered under this name.
     */
    public static String __serviceName = "org.openoffice.da.writer2xhtml.ToolbarSettingsDialog";

    /** The component should also have an implementation name.
     */
    public static String __implementationName = "org.openoffice.da.comp.writer2xhtml.ToolbarSettingsDialog";

    /** Create a new ToolbarSettingsDialog */
    public ToolbarSettingsDialog(XComponentContext xContext) {
        this.xContext = xContext;
        filePicker = new FilePicker(xContext);
    }
	
    // Implement XContainerWindowEventHandler
    public boolean callHandlerMethod(XWindow xWindow, Object event, String sMethod)
        throws com.sun.star.lang.WrappedTargetException {
		XDialog xDialog = (XDialog)UnoRuntime.queryInterface(XDialog.class, xWindow);
		DialogAccess dlg = new DialogAccess(xDialog);

        try {
            if (sMethod.equals("external_event") ){
                return handleExternalEvent(dlg, event);
            }
            else if (sMethod.equals("XhtmlFormatChange")) {
            	return true;
            }
            else if (sMethod.equals("XhtmlViewChange")) {
            	return xhtmlViewChange(dlg);
            }
            else if (sMethod.equals("XhtmlBrowseClick")) {
            	return xhtmlBrowseClick(dlg);
            }
            else if (sMethod.equals("EpubFormatChange")) {
            	return true;
            }
            else if (sMethod.equals("EpubViewChange")) {
            	return epubViewChange(dlg);
            }
            else if (sMethod.equals("EpubBrowseClick")) {
            	return epubBrowseClick(dlg);
            }
        }
        catch (com.sun.star.uno.RuntimeException e) {
            throw e;
        }
        catch (com.sun.star.uno.Exception e) {
            throw new com.sun.star.lang.WrappedTargetException(sMethod, this, e);
        }
        return false;
    }
	
    public String[] getSupportedMethodNames() {
        String[] sNames = { "external_event", "XhtmlFormatChange", "XhtmlViewChange", "XhtmlBrowseClick",
        		"EpupFormatChange", "EpubViewChange", "EpubBrowseClick" };
        return sNames;
    }
    
    // Implement the interface XServiceInfo
    public boolean supportsService(String sServiceName) {
        return sServiceName.equals(__serviceName);
    }

    public String getImplementationName() {
        return __implementationName;
    }
    
    public String[] getSupportedServiceNames() {
        String[] sSupportedServiceNames = { __serviceName };
        return sSupportedServiceNames;
    }
	
    // Private stuff
    
    private boolean handleExternalEvent(DialogAccess dlg, Object aEventObject)
        throws com.sun.star.uno.Exception {
        try {
            String sMethod = AnyConverter.toString(aEventObject);
            if (sMethod.equals("ok")) {
                saveConfiguration(dlg);
                return true;
            } else if (sMethod.equals("back") || sMethod.equals("initialize")) {
                loadConfiguration(dlg);
                enableXhtmlExecutable(dlg);
                enableEpubExecutable(dlg);
                return true;
            }
        }
        catch (com.sun.star.lang.IllegalArgumentException e) {
            throw new com.sun.star.lang.IllegalArgumentException(
            "Method external_event requires a string in the event object argument.", this,(short) -1);
        }
        return false;
    }
    
    private void loadConfiguration(DialogAccess dlg) {
    	RegistryHelper registry = new RegistryHelper(xContext);
    	
		try {
			Object view = registry.getRegistryView(REGISTRY_PATH, false);
			XPropertySet xProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,view);
			
        	dlg.setListBoxSelectedItem("XhtmlFormat",
        			XPropertySetHelper.getPropertyValueAsShort(xProps, "XhtmlFormat"));
        	dlg.setListBoxSelectedItem("XhtmlView",
        			XPropertySetHelper.getPropertyValueAsShort(xProps, "XhtmlView"));
        	dlg.setTextFieldText("XhtmlExecutable",
        			XPropertySetHelper.getPropertyValueAsString(xProps, "XhtmlExecutable"));
        	dlg.setListBoxSelectedItem("EpubFormat",
        			XPropertySetHelper.getPropertyValueAsShort(xProps, "EpubFormat"));
        	dlg.setListBoxSelectedItem("EpubView",
        			XPropertySetHelper.getPropertyValueAsShort(xProps, "EpubView"));
        	dlg.setTextFieldText("EpubExecutable",
        			XPropertySetHelper.getPropertyValueAsString(xProps, "EpubExecutable"));
		} catch (Exception e) {
    		// Failed to get registry view
		}
    }
    
    private void saveConfiguration(DialogAccess dlg) {
		RegistryHelper registry = new RegistryHelper(xContext);
    	try {
    		Object view = registry.getRegistryView(REGISTRY_PATH, true);
    		XPropertySet xProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,view);
   			XPropertySetHelper.setPropertyValue(xProps, "XhtmlFormat", dlg.getListBoxSelectedItem("XhtmlFormat"));
   			XPropertySetHelper.setPropertyValue(xProps, "XhtmlView", dlg.getListBoxSelectedItem("XhtmlView"));
   			XPropertySetHelper.setPropertyValue(xProps, "XhtmlExecutable", dlg.getTextFieldText("XhtmlExecutable"));
   			XPropertySetHelper.setPropertyValue(xProps, "EpubFormat", dlg.getListBoxSelectedItem("EpubFormat"));
   			XPropertySetHelper.setPropertyValue(xProps, "EpubView", dlg.getListBoxSelectedItem("EpubView"));
   			XPropertySetHelper.setPropertyValue(xProps, "EpubExecutable", dlg.getTextFieldText("EpubExecutable"));
   			
            // Commit registry changes
            XChangesBatch  xUpdateContext = (XChangesBatch)
                UnoRuntime.queryInterface(XChangesBatch.class,view);
            try {
                xUpdateContext.commitChanges();
            }
            catch (Exception e) {
                // ignore
            }
                        
        	registry.disposeRegistryView(view);
    	}
    	catch (Exception e) {
    		// Failed to get registry view
    	}		    	
    }
    
    private boolean xhtmlViewChange(DialogAccess dlg) {
    	enableXhtmlExecutable(dlg);
    	return true;
    }

    private void enableXhtmlExecutable(DialogAccess dlg) {
    	int nItem = dlg.getListBoxSelectedItem("XhtmlView");
    	dlg.setControlEnabled("XhtmlExecutable", nItem==2);
    	dlg.setControlEnabled("XhtmlBrowseButton", nItem==2);
    }
    
    private boolean xhtmlBrowseClick(DialogAccess dlg) {
    	browseForExecutable(dlg,"XhtmlExecutable");
    	return true;
    }
	
    private boolean epubViewChange(DialogAccess dlg) {
    	enableEpubExecutable(dlg);
    	return true;
    }
	
    private void enableEpubExecutable(DialogAccess dlg) {
    	int nItem = dlg.getListBoxSelectedItem("EpubView");
    	dlg.setControlEnabled("EpubExecutable", nItem==2);
    	dlg.setControlEnabled("EpubBrowseButton", nItem==2);
    }
    
    private boolean epubBrowseClick(DialogAccess dlg) {
    	browseForExecutable(dlg,"EpubExecutable");
    	return true;
    }
    
    private boolean browseForExecutable(DialogAccess dlg, String sControlName) {
    	String sPath = filePicker.getPath();
    	if (sPath!=null) {
    		try {
				dlg.setComboBoxText(sControlName, new File(new URI(sPath)).getCanonicalPath());
			}
    		catch (IOException e) {
			}
    		catch (URISyntaxException e) {
			}
    	}     
    	return true;
    }

	
}