/* Copyright 2011 Toby D. Rule
This file is part of CompPad, an OpenOffice extension to provide live
mathematical and engineering calculations within a Writer document.
CompPad is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CompPad 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with CompPad. If not, see <http://www.gnu.org/licenses/>.
*/
package com.CompPad.OOO;
import com.CompPad.model.ComplexAmount;
import com.sun.star.beans.XPropertySet;
import com.sun.star.text.XDependentTextField;
import com.sun.star.text.XTextContent;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextRange;
import com.sun.star.uno.UnoRuntime;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.measure.unit.Unit;
import org.jscience.mathematics.number.Complex;
import org.jscience.mathematics.number.Float64;
import org.jscience.mathematics.vector.DenseMatrix;
import org.jscience.mathematics.vector.DenseVector;
import org.jscience.physics.amount.Amount;
/**
* Openoffice implementation of result field. May be linked to an OOOExpression.
* Implements a Listener that may be added to Expression and used to update
* results output.
* @author brenda
*/
public class OOOResult extends OOOElement {
/* Zero tolerance - should get this from computation engine! */
private String name;
private XPropertySet outputFieldMaster;
private OOODocument oooDocument;
/* Expression to which result is linked */
private OOOExpression oooExpression;
private XDependentTextField xDependentTextField;
/* Need to make sure this is sorted so that sub-types are first in the list
parent types are last */
private Class[] valueTypes={Integer.class, Float.class, Double.class,
String.class, Boolean.class,ArrayList.class, Complex.class,
ComplexAmount.class, LinkedList.class, DenseVector.class, DenseMatrix.class,
Amount.class, com.CompPad.model.Matrix.class,Boolean.class,
groovy.lang.Closure.class};
/**
* Constructor for text field result class. Maybe this class should be
* OOOResultDependentTextField, to distinguish from other possible
* types of result classes.
* @param xTF
*/
public OOOResult(OOODocument oooDocArg, OOOExpression oooExpArg){
xTextDocument = oooDocArg.getTextDocument();
oooDocument=oooDocArg;
oooExpression = oooExpArg;
}
public OOOResult(OOODocument oooDocArg,XDependentTextField xTF){
xTextDocument = oooDocArg.getTextDocument();
xDependentTextField=xTF;
oooDocument=oooDocArg;
}
public OOOResult(){
/* Call without args to get a pattern object - just to get to ValueTypes! */
}
/* Link existing text field to result field */
public void setTextField(XDependentTextField xTF){
xDependentTextField=xTF;
}
/**
* Link to OOOExpression
* @param oooExp
*/
public void setOOOExpression(OOOExpression oooExp){
oooExpression=oooExp;
}
/**
* Link to Expression
* @param exp
*/
// public void setExpression(Expression exp){
// exp.setResultListener(new ResultListener());
// }
@Override
public XTextRange getTextRange() {
return ((XTextContent)UnoRuntime.queryInterface(XTextContent.class,
xDependentTextField)).getAnchor();
}
@Override
public String getName() throws Exception {
return (String)xDependentTextField.getTextFieldMaster().getPropertyValue("Name");
}
/**
* Inner class to implement listener
*
*/
public XPropertySet getOutputFieldMaster() {
/* return the field master for an output field */
return outputFieldMaster;
}
public void dispose(){
/* get rid of OpenOffice representation and destroy this object, or
at least dereference */
if (xDependentTextField!=null){
xDependentTextField.dispose();
}
oooExpression=null;
}
public boolean canHandle(Object e) throws Exception{
for (Class v : valueTypes){
if (v.isInstance(e)){
/* Note that this is satisfied if e is a subclass of v
* eg. Object.isInstance(Integer) would return true,
* but Integer.isInstance(Object would return false,
* so put classes in valueTypes list with subclasses
* (eg Integer) first */
return true;
}
}
return false;
}
/* These output handlers would be best placed in a utility class in the com.CompPad
* package */
/* Return string expressing x in i significant figures */
@Override
public void handle(Object arg) throws java.lang.Exception{
/* Check script engine for formatting parameters */
/* Need to get zero tolerance from scriptengine! */
if (canHandle(arg)){
Logger.getLogger("com.CompPad").log(Level.FINE," arg: "+arg);
/* if output field doesn't exists, then create field */
if (xDependentTextField==null){
/* Create text field */
name = oooDocument.newResultName(); /* Need to create unique ID number */
outputFieldMaster=oooDocument.newFieldMaster(name);
// initOutputField(name);
XDependentTextField xTextField =
oooDocument.createField(outputFieldMaster);
// insert into document. Last argument indicates NOT to replace
// the text range text.
XTextCursor xTextCursor=oooExpression.getTextCursor();
Boolean didGoRight = xTextCursor.goRight((short) 1, false);
xTextCursor.getText().insertTextContent(xTextCursor, xTextField, false);
}
else{
/* get outputFieldMaster */
outputFieldMaster = xDependentTextField.getTextFieldMaster();
}
/* Now set value */
// temporarily commented out. This is not used, but we may eventually use the OOOResult for something
// other than displaying results.
// outputFieldMaster.setPropertyValue("Content","= " + com.CompPad.model.Utility.outputString(arg));
}
else{
if (xDependentTextField!=null){
xDependentTextField.dispose();
}
}
throw new UnsupportedOperationException("Not currently implemented");
}
public void setDependsOn(OOOExpression arg){
oooExpression=arg;
}
}