w2x toolbar configuration dialog

git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@235 f0f2a975-2e09-46c8-9428-3b39399b9f3c
This commit is contained in:
henrikjust 2015-04-05 13:47:15 +00:00
parent c1fecb5252
commit 199616dde6
22 changed files with 794 additions and 178 deletions

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.6 (2015-01-09)
* Version 1.6 (2015-04-05)
*
*/
package org.openoffice.da.comp.w2lcommon.filter;
@ -85,7 +85,7 @@ public class UNOPublisher {
* (5) Post process the document, e.g. displaying the result
*
* @param format the target format
* @return true if the publishing was succesful
* @return true if the publishing was successful
*/
public boolean publish(TargetFormat format) {
if (documentSaved() && updateMediaProperties(format)) {
@ -131,7 +131,7 @@ public class UNOPublisher {
}
xStatus.setValue(7); // Document is converted, that's 70%
postProcess(getTargetURL(format));
postProcess(getTargetURL(format),format);
xStatus.setValue(10); // Export is finished (The user will usually not see this...)
xStatus.end();
@ -161,8 +161,9 @@ public class UNOPublisher {
/** Post process the document after conversion.
*
* @param format URL of the converted document
* @param format the target format
*/
protected void postProcess(String sTargetURL) {
protected void postProcess(String sTargetURL, TargetFormat format) {
}
/** Check that the document is saved in a location, we can use

View file

@ -16,23 +16,23 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2002-2009 by Henrik Just
* Copyright: 2002-2015 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.2 (2009-03-30)
* Version 1.6 (2015-04-05)
*
*/
package org.openoffice.da.comp.writer2latex;
package org.openoffice.da.comp.w2lcommon.helper;
import java.io.*;
class StreamGobbler extends Thread {
public class StreamGobbler extends Thread {
InputStream is;
String type;
StreamGobbler(InputStream is, String type) {
public StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.6 (2015-02-10)
* Version 1.6 (2015-04-05)
*
*/
@ -32,14 +32,10 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.util.Vector;
import com.sun.star.awt.XControl;
import com.sun.star.awt.XControlContainer;
import com.sun.star.awt.XControlModel;
import com.sun.star.awt.XContainerWindowEventHandler;
import com.sun.star.awt.XDialog;
import com.sun.star.awt.XDialogProvider2;
import com.sun.star.awt.XWindow;
import com.sun.star.beans.XPropertySet;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.lang.XServiceInfo;
import com.sun.star.uno.AnyConverter;
@ -50,6 +46,7 @@ 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.StreamGobbler;
/** This class provides a uno component which implements the configuration
* of applications for the Writer2LaTeX toolbar
@ -81,24 +78,27 @@ public final class ApplicationsDialog
// 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(xWindow, event);
return handleExternalEvent(dlg, event);
}
else if (sMethod.equals("ApplicationChange")) {
return changeApplication(xWindow);
return changeApplication(dlg);
}
else if (sMethod.equals("BrowseClick")) {
return browseForExecutable(xWindow);
return browseForExecutable(dlg);
}
else if (sMethod.equals("ExecutableUnfocus")) {
return updateApplication(xWindow);
return updateApplication(dlg);
}
else if (sMethod.equals("OptionsUnfocus")) {
return updateApplication(xWindow);
return updateApplication(dlg);
}
else if (sMethod.equals("AutomaticClick")) {
return autoConfigure(xWindow);
return autoConfigure(dlg);
}
}
catch (com.sun.star.uno.RuntimeException e) {
@ -131,7 +131,7 @@ public final class ApplicationsDialog
// Private stuff
private boolean handleExternalEvent(com.sun.star.awt.XWindow xWindow, Object aEventObject)
private boolean handleExternalEvent(DialogAccess dlg, Object aEventObject)
throws com.sun.star.uno.Exception {
try {
String sMethod = AnyConverter.toString(aEventObject);
@ -140,7 +140,7 @@ public final class ApplicationsDialog
return true;
} else if (sMethod.equals("back") || sMethod.equals("initialize")) {
externalApps.load();
return changeApplication(xWindow);
return changeApplication(dlg);
}
}
catch (com.sun.star.lang.IllegalArgumentException e) {
@ -150,35 +150,35 @@ public final class ApplicationsDialog
return false;
}
private boolean changeApplication(XWindow xWindow) {
String sAppName = getSelectedAppName(xWindow);
private boolean changeApplication(DialogAccess dlg) {
String sAppName = getSelectedAppName(dlg);
if (sAppName!=null) {
String[] s = externalApps.getApplication(sAppName);
setComboBoxText(xWindow, "Executable", s[0]);
setComboBoxText(xWindow, "Options", s[1]);
dlg.setComboBoxText("Executable", s[0]);
dlg.setComboBoxText("Options", s[1]);
}
return true;
}
private boolean browseForExecutable(XWindow xWindow) {
private boolean browseForExecutable(DialogAccess dlg) {
String sPath = filePicker.getPath();
if (sPath!=null) {
try {
setComboBoxText(xWindow, "Executable", new File(new URI(sPath)).getCanonicalPath());
dlg.setComboBoxText("Executable", new File(new URI(sPath)).getCanonicalPath());
}
catch (IOException e) {
}
catch (URISyntaxException e) {
}
updateApplication(xWindow);
updateApplication(dlg);
}
return true;
}
private boolean updateApplication(XWindow xWindow) {
String sAppName = getSelectedAppName(xWindow);
private boolean updateApplication(DialogAccess dlg) {
String sAppName = getSelectedAppName(dlg);
if (sAppName!=null) {
externalApps.setApplication(sAppName, getComboBoxText(xWindow, "Executable"), getComboBoxText(xWindow, "Options"));
externalApps.setApplication(sAppName, dlg.getComboBoxText("Executable"), dlg.getComboBoxText("Options"));
}
return true;
}
@ -281,7 +281,7 @@ public final class ApplicationsDialog
}
// Configure the applications automatically (OS dependent)
private boolean autoConfigure(XWindow xWindow) {
private boolean autoConfigure(DialogAccess dlg) {
String sOsName = System.getProperty("os.name");
String sOsVersion = System.getProperty("os.version");
String sOsArch = System.getProperty("os.arch");
@ -380,12 +380,12 @@ public final class ApplicationsDialog
// sudo apt-get install texlive-latex-extra
// sudo apt-get install tex4ht
displayAutoConfigInfo(info.toString());
changeApplication(xWindow);
changeApplication(dlg);
return true;
}
private String getSelectedAppName(XWindow xWindow) {
short nItem = getListBoxSelectedItem(xWindow, "Application");
private String getSelectedAppName(DialogAccess dlg) {
short nItem = dlg.getListBoxSelectedItem("Application");
//String sAppName = null;
switch (nItem) {
case 0: return ExternalApps.LATEX;
@ -417,7 +417,6 @@ public final class ApplicationsDialog
}
}
private void displayAutoConfigInfo(String sText) {
XDialog xDialog = getDialog("W2LDialogs2.AutoConfigInfo");
if (xDialog!=null) {
@ -428,57 +427,6 @@ public final class ApplicationsDialog
}
}
// Some helpers copied from DialogBase
private XPropertySet getControlProperties(XWindow xWindow, String sControlName) {
XControlContainer xContainer = (XControlContainer)
UnoRuntime.queryInterface(XControlContainer.class, xWindow);
XControl xControl = xContainer.getControl(sControlName);
XControlModel xModel = xControl.getModel();
XPropertySet xPropertySet = (XPropertySet)
UnoRuntime.queryInterface(XPropertySet.class, xModel);
return xPropertySet;
}
private String getComboBoxText(XWindow xWindow, String sControlName) {
// Returns the text of a combobox
XPropertySet xPropertySet = getControlProperties(xWindow, sControlName);
try {
return (String) xPropertySet.getPropertyValue("Text");
}
catch (Exception e) {
// Will fail if the control does not exist or is not a combo
return "";
}
}
private void setComboBoxText(XWindow xWindow, String sControlName, String sText) {
XPropertySet xPropertySet = getControlProperties(xWindow, sControlName);
try {
xPropertySet.setPropertyValue("Text", sText);
}
catch (Exception e) {
// Will fail if the control does not exist or is not a combo box or
// nText is an illegal value
}
}
private short getListBoxSelectedItem(XWindow xWindow, String sControlName) {
// Returns the first selected element in case of a multiselection
XPropertySet xPropertySet = getControlProperties(xWindow, sControlName);
try {
short[] selection = (short[]) xPropertySet.getPropertyValue("SelectedItems");
return selection[0];
}
catch (Exception e) {
// Will fail if the control does not exist or is not a list box
return -1;
}
}
}

View file

@ -35,6 +35,7 @@ import java.util.Map;
import java.util.Vector;
import org.openoffice.da.comp.w2lcommon.helper.RegistryHelper;
import org.openoffice.da.comp.w2lcommon.helper.StreamGobbler;
//import java.util.Map;
import com.sun.star.beans.XMultiHierarchicalPropertySet;

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.6 (2015-02-18)
* Version 1.6 (2015-04-05)
*
*/
package org.openoffice.da.comp.writer2latex;
@ -140,7 +140,7 @@ public class LaTeXUNOPublisher extends UNOPublisher {
/** Postprocess the converted document with LaTeX and display the result
*/
@Override protected void postProcess(String sURL) {
@Override protected void postProcess(String sURL, TargetFormat format) {
if (texify==null) { texify = new TeXify(xContext); }
File file = new File(Misc.urlToFile(getTargetPath()),getTargetFileName());

View file

@ -0,0 +1,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;
}
}

View file

@ -16,11 +16,11 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2002-2014 by Henrik Just
* Copyright: 2002-2015 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.6 (2014-10-06)
* Version 1.6 (2015-04-03)
*
*/
@ -114,6 +114,12 @@ public class W2XRegistration {
multiFactory,
regKey);
}
else if (implName.equals(ToolbarSettingsDialog.__implementationName)) {
xSingleServiceFactory = FactoryHelper.getServiceFactory(ToolbarSettingsDialog.class,
ToolbarSettingsDialog.__serviceName,
multiFactory,
regKey);
}
return xSingleServiceFactory;
}
@ -145,7 +151,9 @@ public class W2XRegistration {
FactoryHelper.writeRegistryServiceInfo(EpubMetadataDialog.__implementationName,
EpubMetadataDialog.__serviceName, regKey) &
FactoryHelper.writeRegistryServiceInfo(ConfigurationDialog.__implementationName,
ConfigurationDialog.__serviceName, regKey);
ConfigurationDialog.__serviceName, regKey) &
FactoryHelper.writeRegistryServiceInfo(ToolbarSettingsDialog.__implementationName,
ToolbarSettingsDialog.__serviceName, regKey);
}
}

View file

@ -20,7 +20,7 @@
*
* All Rights Reserved.
*
* Version 1.6 (2015-01-09)
* Version 1.6 (2015-04-05)
*
*/
@ -28,6 +28,7 @@ package org.openoffice.da.comp.writer2xhtml;
// TODO: Create common base for dispatcher classes
import com.sun.star.beans.XPropertySet;
import com.sun.star.frame.XFrame;
import com.sun.star.lang.XComponent;
import com.sun.star.lib.uno.helper.WeakBase;
@ -36,6 +37,8 @@ import com.sun.star.uno.Exception;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import org.openoffice.da.comp.w2lcommon.filter.UNOPublisher.TargetFormat;
import org.openoffice.da.comp.w2lcommon.helper.RegistryHelper;
import org.openoffice.da.comp.w2lcommon.helper.XPropertySetHelper;
/** This class implements the ui (dispatch) commands provided by Writer2xhtml.
*/
@ -57,10 +60,6 @@ public final class Writer2xhtml extends WeakBase
public static final String __serviceName = "com.sun.star.frame.ProtocolHandler";
private static final String[] m_serviceNames = { __serviceName };
// TODO: These should be configurable
private TargetFormat xhtmlFormat = TargetFormat.html5;
private TargetFormat epubFormat = TargetFormat.epub3;
public Writer2xhtml(XComponentContext xContext) {
m_xContext = xContext;
}
@ -129,11 +128,11 @@ public final class Writer2xhtml extends WeakBase
com.sun.star.beans.PropertyValue[] aArguments ) {
if ( aURL.Protocol.compareTo(PROTOCOL) == 0 ) {
if ( aURL.Path.compareTo("PublishAsXHTML") == 0 ) {
publish(xhtmlFormat);
publishAsXhtml();
return;
}
else if ( aURL.Path.compareTo("PublishAsEPUB") == 0 ) {
publish(epubFormat);
publishAsEpub();
return;
}
else if ( aURL.Path.compareTo("EditEPUBDocumentProperties") == 0 ) {
@ -168,6 +167,39 @@ public final class Writer2xhtml extends WeakBase
}
}
private void publishAsXhtml() {
RegistryHelper registry = new RegistryHelper(m_xContext);
try {
Object view = registry.getRegistryView(ToolbarSettingsDialog.REGISTRY_PATH, false);
XPropertySet xProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,view);
short nXhtmlFormat = XPropertySetHelper.getPropertyValueAsShort(xProps, "XhtmlFormat");
switch (nXhtmlFormat) {
case 0: publish(TargetFormat.xhtml); break;
case 1: publish(TargetFormat.xhtml11); break;
case 2: publish(TargetFormat.xhtml_mathml); break;
case 3: publish(TargetFormat.html5);
}
} catch (Exception e) {
// Failed to get registry view
}
}
private void publishAsEpub() {
RegistryHelper registry = new RegistryHelper(m_xContext);
try {
Object view = registry.getRegistryView(ToolbarSettingsDialog.REGISTRY_PATH, false);
XPropertySet xProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,view);
short nEpubFormat = XPropertySetHelper.getPropertyValueAsShort(xProps, "EpubFormat");
switch (nEpubFormat) {
case 0: publish(TargetFormat.epub); break;
case 1: publish(TargetFormat.epub3);
}
} catch (Exception e) {
// Failed to get registry view
}
}
private void publish(TargetFormat format) {
if (unoPublisher==null) {
unoPublisher = new XhtmlUNOPublisher(m_xContext,m_xFrame,"Writer2xhtml");

View file

@ -16,11 +16,11 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Copyright: 2002-2014 by Henrik Just
* Copyright: 2002-2015 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.6 (2014-11-03)
* Version 1.6 (2015-04-05)
*
*/
package org.openoffice.da.comp.writer2xhtml;
@ -28,13 +28,20 @@ package org.openoffice.da.comp.writer2xhtml;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.util.Vector;
import org.openoffice.da.comp.w2lcommon.filter.UNOPublisher;
import org.openoffice.da.comp.w2lcommon.helper.MessageBox;
import org.openoffice.da.comp.w2lcommon.helper.RegistryHelper;
import org.openoffice.da.comp.w2lcommon.helper.StreamGobbler;
import org.openoffice.da.comp.w2lcommon.helper.XPropertySetHelper;
import writer2latex.util.Misc;
import com.sun.star.beans.XPropertySet;
import com.sun.star.frame.XFrame;
import com.sun.star.uno.Exception;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
public class XhtmlUNOPublisher extends UNOPublisher {
@ -44,27 +51,97 @@ public class XhtmlUNOPublisher extends UNOPublisher {
}
/** Display the converted document in the default application
/** Display the converted document depending on user settings
*
* @param sURL the URL of the converted document
* @param format the target format
*/
@Override protected void postProcess(String sURL) {
@Override protected void postProcess(String sURL, TargetFormat format) {
RegistryHelper registry = new RegistryHelper(xContext);
short nView = 1;
String sExecutable = null;
try {
Object view = registry.getRegistryView(ToolbarSettingsDialog.REGISTRY_PATH, false);
XPropertySet xProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,view);
if (format==TargetFormat.xhtml || format==TargetFormat.xhtml11 || format==TargetFormat.xhtml_mathml || format==TargetFormat.html5) {
nView = XPropertySetHelper.getPropertyValueAsShort(xProps, "XhtmlView");
sExecutable = XPropertySetHelper.getPropertyValueAsString(xProps, "XhtmlExecutable");
}
else { // EPUB
nView = XPropertySetHelper.getPropertyValueAsShort(xProps, "EpubView");
sExecutable = XPropertySetHelper.getPropertyValueAsString(xProps, "EpubExecutable");
}
} catch (Exception e) {
// Failed to get registry view
}
File file = Misc.urlToFile(sURL);
if (file.exists()) {
// Open the file in the default application on this system (if any)
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
try {
desktop.open(file);
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
}
else {
MessageBox msgBox = new MessageBox(xContext, xFrame);
msgBox.showMessage("Writer2xhtml","Error: Failed to open exported document");
if (nView==0) {
return;
}
else if (nView==1) {
if (openWithDefaultApplication(file)) {
return;
}
}
else if (nView==2) {
if (openWithCustomApplication(file, sExecutable)) {
return;
}
}
}
MessageBox msgBox = new MessageBox(xContext, xFrame);
msgBox.showMessage("Writer2xhtml","Error: Failed to open exported document");
}
// Open the file in the default application on this system (if any)
private boolean openWithDefaultApplication(File file) {
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
try {
desktop.open(file);
return true;
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
return false;
}
// Open the file with the user defined application
private boolean openWithCustomApplication(File file, String sExecutable) {
try {
Vector<String> command = new Vector<String>();
command.add(sExecutable);
command.add(file.getPath());
ProcessBuilder pb = new ProcessBuilder(command);
Process proc = pb.start();
// Gobble the error stream of the application
StreamGobbler errorGobbler = new
StreamGobbler(proc.getErrorStream(), "ERROR");
// Gobble the output stream of the application
StreamGobbler outputGobbler = new
StreamGobbler(proc.getInputStream(), "OUTPUT");
errorGobbler.start();
outputGobbler.start();
// The application exists if the process exits with 0
return proc.waitFor()==0;
}
catch (InterruptedException e) {
return false;
}
catch (IOException e) {
return false;
}
}
}