w2phtml/source/java/org/openoffice/da/comp/writer4latex/ConfigurationDialog.java
henrikjust 9241a44f6c Java 5 + Writer4LaTeX + bugfixes
git-svn-id: svn://svn.code.sf.net/p/writer2latex/code/trunk@11 f0f2a975-2e09-46c8-9428-3b39399b9f3c
2009-03-30 07:38:37 +00:00

304 lines
11 KiB
Java

/************************************************************************
*
* ConfigurationDialog.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-2009 by Henrik Just
*
* All Rights Reserved.
*
* Version 1.2 (2009-03-30)
*
*/
package org.openoffice.da.comp.writer4latex;
import java.io.File;
import java.net.URI;
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.XWindow;
import com.sun.star.beans.XPropertySet;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XServiceInfo;
import com.sun.star.ui.dialogs.ExecutableDialogResults;
import com.sun.star.ui.dialogs.XExecutableDialog;
import com.sun.star.ui.dialogs.XFilePicker;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.lib.uno.helper.WeakBase;
/** This class provides a uno component which implements the configuration
* of Writer4LaTeX
*/
public final class ConfigurationDialog
extends WeakBase
implements XServiceInfo, XContainerWindowEventHandler {
private XComponentContext xContext;
private ExternalApps externalApps;
/** The component will be registered under this name.
*/
public static String __serviceName = "org.openoffice.da.writer4latex.ConfigurationDialog";
/** The component should also have an implementation name.
*/
public static String __implementationName = "org.openoffice.da.comp.writer4latex.ConfigurationDialog";
/** Create a new ConfigurationDialog */
public ConfigurationDialog(XComponentContext xContext) {
this.xContext = xContext;
externalApps = new ExternalApps(xContext);
}
// Implement XContainerWindowEventHandler
public boolean callHandlerMethod(XWindow xWindow, Object event, String sMethod)
throws com.sun.star.lang.WrappedTargetException {
try {
if (sMethod.equals("external_event") ){
return handleExternalEvent(xWindow, event);
}
else if (sMethod.equals("ApplicationChange")) {
return changeApplication(xWindow);
}
else if (sMethod.equals("BrowseClick")) {
return browseForExecutable(xWindow);
}
else if (sMethod.equals("ExecutableUnfocus")) {
return updateApplication(xWindow);
}
else if (sMethod.equals("OptionsUnfocus")) {
return updateApplication(xWindow);
}
else if (sMethod.equals("AutomaticClick")) {
return autoConfigure(xWindow);
}
}
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", "ApplicationChange", "BrowseClick", "ExecutableUnfocus", "OptionsUnfocus", "AutomaticClick" };
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(com.sun.star.awt.XWindow xWindow, Object aEventObject)
throws com.sun.star.uno.Exception {
try {
String sMethod = AnyConverter.toString(aEventObject);
if (sMethod.equals("ok")) {
externalApps.save();
return true;
} else if (sMethod.equals("back") || sMethod.equals("initialize")) {
externalApps.load();
return changeApplication(xWindow);
}
}
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 boolean changeApplication(XWindow xWindow) {
String sAppName = getSelectedAppName(xWindow);
if (sAppName!=null) {
String[] s = externalApps.getApplication(sAppName);
setComboBoxText(xWindow, "Executable", s[0]);
setComboBoxText(xWindow, "Options", s[1]);
}
return true;
}
private boolean browseForExecutable(XWindow xWindow) {
XComponent xComponent = null;
try {
// Create FilePicker
Object filePicker = xContext.getServiceManager()
.createInstanceWithContext("com.sun.star.ui.dialogs.FilePicker", xContext);
XFilePicker xFilePicker = (XFilePicker)
UnoRuntime.queryInterface(XFilePicker.class, filePicker);
xComponent = (XComponent)
UnoRuntime.queryInterface(XComponent.class, xFilePicker);
// Display the FilePicker
XExecutableDialog xExecutable = (XExecutableDialog)
UnoRuntime.queryInterface(XExecutableDialog.class, xFilePicker);
// Get the path
if (xExecutable.execute() == ExecutableDialogResults.OK) {
String[] sPathList = xFilePicker.getFiles();
if (sPathList.length > 0) {
setComboBoxText(xWindow, "Executable", new File(new URI(sPathList[0])).getCanonicalPath());
updateApplication(xWindow);
}
}
}
catch (com.sun.star.uno.Exception e) {
}
catch (java.net.URISyntaxException e) {
}
catch (java.io.IOException e) {
}
finally{
// Always dispose the FilePicker component
if (xComponent!=null) {
xComponent.dispose();
}
}
return true;
}
private boolean updateApplication(XWindow xWindow) {
String sAppName = getSelectedAppName(xWindow);
if (sAppName!=null) {
externalApps.setApplication(sAppName, getComboBoxText(xWindow, "Executable"), getComboBoxText(xWindow, "Options"));
}
return true;
}
private boolean autoConfigure(XWindow xWindow) {
externalApps.setApplication(ExternalApps.LATEX, "latex", "--interaction=batchmode %s");
externalApps.setApplication(ExternalApps.PDFLATEX, "pdflatex", "--interaction=batchmode %s");
externalApps.setApplication(ExternalApps.XELATEX, "xelatex", "--interaction=batchmode %s");
externalApps.setApplication(ExternalApps.DVIPS, "dvips", "%s");
externalApps.setApplication(ExternalApps.BIBTEX, "bibtex", "%s");
externalApps.setApplication(ExternalApps.MAKEINDEX, "makeindex", "%s");
externalApps.setApplication(ExternalApps.OOLATEX, "oolatex", "%s");
String sOsName = System.getProperty("os.name");
if ("Linux".equals(sOsName)) {
// TODO: Search for applications (which...)
// Viewers may be evince, okular, xdvi, xpdf, ghostview or...
externalApps.setApplication(ExternalApps.DVIVIEWER, "evince", "%s");
externalApps.setApplication(ExternalApps.PDFVIEWER, "evince", "%s");
externalApps.setApplication(ExternalApps.POSTSCRIPTVIEWER, "evince", "%s");
}
else if ("Windows".equals(sOsName)) {
// Assume MikTeX
externalApps.setApplication(ExternalApps.DVIVIEWER, "yap", "--single-instance %s");
// And assume gsview for pdf and ps
// gsview32 may not be in the path, but at least this helps a bit
externalApps.setApplication(ExternalApps.PDFVIEWER, "gsview32.exe", "-e \"%s\"");
externalApps.setApplication(ExternalApps.POSTSCRIPTVIEWER, "gsview32.exe", "-e \"%s\"");
}
changeApplication(xWindow);
return true;
}
private String getSelectedAppName(XWindow xWindow) {
short nItem = getListBoxSelectedItem(xWindow, "Application");
//String sAppName = null;
switch (nItem) {
case 0: return ExternalApps.LATEX;
case 1: return ExternalApps.PDFLATEX;
case 2: return ExternalApps.XELATEX;
case 3: return ExternalApps.DVIPS;
case 4: return ExternalApps.BIBTEX;
case 5: return ExternalApps.MAKEINDEX;
case 6: return ExternalApps.OOLATEX;
case 7: return ExternalApps.DVIVIEWER;
case 8: return ExternalApps.PDFVIEWER;
case 9: return ExternalApps.POSTSCRIPTVIEWER;
}
return "???";
}
// 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;
}
}
}