/* 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/>.
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.CompPad.OOO;
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;
/**
* Openoffice representation of error field. This class is specifically a text field Error. Could
* just as well implement other classes such as note error.
*
* @author Toby D. Rule
*/
public class OOOError extends OOOElement {
XDependentTextField xDependentTextField;
OOODocument oooDocument;
OOOExpression oooExpression;
XPropertySet errorFieldMaster;
private String name;
private Class[] valueTypes={java.lang.Error.class,
java.lang.Exception.class};
public OOOError(){
}
public OOOError(OOODocument oooDocArg, OOOExpression oooExpArg){
xTextDocument = oooDocArg.getTextDocument();
oooDocument=oooDocArg;
oooExpression = oooExpArg;
}
public OOOError(OOODocument oooDocArg,XDependentTextField xTF){
/* Create the error field */
oooDocument=oooDocArg;
xTextDocument = oooDocArg.getTextDocument();
xDependentTextField = xTF;
}
public void setOOOExpression(OOOExpression oooExpArg){
oooExpression=oooExpArg;
}
@Override
public XTextRange getTextRange() {
return ((XTextContent)UnoRuntime.queryInterface(XTextContent.class,
xDependentTextField)).getAnchor();
}
@Override
public String getName() throws Exception {
try{
XPropertySet xTFM = xDependentTextField.getTextFieldMaster();
return (String)xTFM.getPropertyValue("Name");
}
catch (com.sun.star.uno.RuntimeException e){
return null;
}
}
/* inner classes for listners */
public void dispose(){
if (xDependentTextField!=null){
xDependentTextField.dispose();
}
oooExpression=null;
}
@Override
public void handle(Object arg) throws Exception{
String message;
if (java.lang.Error.class.isInstance(arg)){
message = (((java.lang.Error)arg).getClass()) + ": " + ((java.lang.Error)arg).getMessage() ;
}
else if (java.lang.NullPointerException.class.isInstance(arg)){
message=((java.lang.NullPointerException)arg).getClass().getName();
}
else if (java.lang.Exception.class.isInstance(arg)){
message=((java.lang.Exception)arg).getMessage();
}
else{
message = arg.toString();
}
/* if error field exists, then set error, otherwise create new
* error field */
if (xDependentTextField==null){
/* Create text field */
name = oooDocument.newErrorName(); /* Need to create unique ID number */
errorFieldMaster=oooDocument.newFieldMaster(name);
XDependentTextField xTextField =
oooDocument.createField(errorFieldMaster);
// insert into document. Last argument indicates NOT to replace
// the text range text.
XTextCursor xTextCursor=oooExpression.getTextCursor();
xTextCursor.goRight((short) 1, false);
xTextCursor.getText().insertTextContent(xTextCursor, xTextField, false);
}
else{
/* get outputFieldMaster */
errorFieldMaster = xDependentTextField.getTextFieldMaster();
}
/* Now set value */
errorFieldMaster.setPropertyValue("Content",message);
// else if (xDependentTextField!=null){
// xDependentTextField.dispose();
// }
}
@Override
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;
}
public void setDependsOn(OOOExpression arg){
oooExpression=arg;
}
}