Hello Aladdin,
On Thursday 02 April 2009, 06:42, Aladdin wrote:
> Moreover, insert color palette has been fixed on the API[2].
> [2] http://openoffice.org/issues/show_bug.cgi?id=94459
it seems there are still issues:
1. where did you get this service from? at least this is completely new to me
[sample code attached], and AFAIK there is no com.sun.star.drawing.ColorTable
in the API reference.
2. Could you make your changes in the color palette persist after your object
dies?
Try the following:
Sub Main
Dim oColorTable as Object
oColorTable = CreateUnoService("com.sun.star.drawing.ColorTable")
Dim sNewName$, bHas as Boolean
sNewName = "My New Color"
If NOT oColorTable.hasByName(sNewName) Then
oColorTable.insertByName(sNewName, RGB(0,124,197))
bHas = oColorTable.hasByName(sNewName)
End If
End Sub
The first time you run the macro, the condition evaluates to false, so you
insert a new color and bHas returns true, saying this went OK.
The second time you run the macro, the condition should evaluate to true, as
you already inserted a new color with that name; but this is not the case,
changes are not made persistent.
This is because the implementation of SvxUnoColorTable
http://svn.services.openoffice.org/opengrok/xref/DEV300_m42/svx/source/unodraw/unoctabl.cxx
misses the point that changes to the XColorTable must be saved for them to
really take place... well, at least this is what the color tab page does
invoking XColorTable::Save().
http://svn.services.openoffice.org/opengrok/xref/DEV300_m42/svx/source/dialog/tpcolor.cxx#757
http://svn.services.openoffice.org/opengrok/xref/DEV300_m42/svx/source/dialog/tpcolor.cxx#915
Regards
--
Ariel Constenla-Haile
La Plata, Argentina
"Aus der Kriegsschule des Lebens
- Was mich nicht umbringt,
macht mich härter."
Nietzsche Götzendämmerung, Sprüche und Pfeile, 8.
/*
* ColorTableDemo.java
*
*/
package org.openoffice.sdk.drawing;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.uno.XComponentContext;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.container.XNameContainer;
import com.sun.star.frame.FrameSearchFlag;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.table.XCell;
import com.sun.star.table.XCellRange;
import com.sun.star.text.ControlCharacter;
import com.sun.star.text.XText;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextTable;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.UnoRuntime;
import java.awt.Color;
/**
*
* @author ariel
*/
public class ColorTableDemo {
private XComponentContext m_xContext;
private XNameContainer m_xColorTable = null;
/** Creates a new instance of ColorTableDemo */
public ColorTableDemo(XComponentContext xContext) {
m_xContext = xContext;
try {
m_xColorTable = (XNameContainer) UnoRuntime.queryInterface(
XNameContainer.class,
m_xContext.getServiceManager().createInstanceWithContext(
"com.sun.star.drawing.ColorTable", m_xContext));
} catch (Exception e) {
e.printStackTrace();
}
}
protected void runDemo(){
printElements();
printColorTableInWriter();
insertColorDemo();
}
private void printElements(){
if (m_xColorTable != null && m_xColorTable.hasElements()){
String[] aColors = m_xColorTable.getElementNames();
for (int n = 0; n < aColors.length; n++) {
String sColorName = aColors[n];
int nColorValue = 0;
try {
nColorValue = AnyConverter.toInt(
m_xColorTable.getByName(sColorName));
} catch (Exception e) {
e.printStackTrace();
}
Color aColor = new Color(nColorValue);
System.out.printf("[%d]\tName: %s\tValue: %d\tRGB(%d,%d,%d)%n",
n, sColorName, nColorValue,
aColor.getRed(), aColor.getGreen(), aColor.getBlue());
}
}
}
private void printColorTableInWriter(){
if (m_xColorTable != null && m_xColorTable.hasElements()){
try {
String[] aColors = m_xColorTable.getElementNames();
XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(
XTextDocument.class, createNewDoc(m_xContext, "writer"));
XMultiServiceFactory xDocFactory = (XMultiServiceFactory)
UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDocument);
XText xDocText = xTextDocument.getText();
XTextCursor xDocTextCursor = xDocText.createTextCursorByRange(
xDocText.getStart());
XPropertySet xDocCursorProps = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xDocTextCursor);
xDocCursorProps.setPropertyValue("ParaStyleName", "Heading 1");
xDocText.insertString(xDocTextCursor, "com.sun.star.drawing.ColorTable", false);
xDocText.insertControlCharacter(xDocTextCursor,
ControlCharacter.PARAGRAPH_BREAK, false);
xDocCursorProps.setPropertyValue("ParaStyleName", "Default");
XTextTable xTextTable = (XTextTable) UnoRuntime.queryInterface(
XTextTable.class,
xDocFactory.createInstance("com.sun.star.text.TextTable"));
xTextTable.initialize(aColors.length + 1, 5);
xDocText.insertTextContent(xDocTextCursor, xTextTable, false);
XCellRange xCellRange = (XCellRange) UnoRuntime.queryInterface(
XCellRange.class, xTextTable);
insertTextIntoCell(xCellRange.getCellByPosition(0, 0),"Color");
insertTextIntoCell(xCellRange.getCellByPosition(1, 0),"UI Name");
insertTextIntoCell(xCellRange.getCellByPosition(2, 0),"css.util.Color");
insertTextIntoCell(xCellRange.getCellByPosition(3, 0),"RGB");
insertTextIntoCell(xCellRange.getCellByPosition(4, 0),"Color.getRGB() & 0x00FFFFFF");
for (int n = 0; n < aColors.length; n++) {
String sColorName = aColors[n];
int nColorValue = 0;
try {
nColorValue = AnyConverter.toInt(
m_xColorTable.getByName(sColorName));
} catch (Exception e) {
e.printStackTrace();
}
Color aColor = new Color(nColorValue);
setCellBackColor(xCellRange.getCellByPosition(0, n + 1), nColorValue);
insertTextIntoCell(xCellRange.getCellByPosition(1, n + 1), sColorName);
insertTextIntoCell(xCellRange.getCellByPosition(2, n + 1), String.valueOf(nColorValue));
insertTextIntoCell(xCellRange.getCellByPosition(3, n + 1), String.format(
"RGB(%d,%d,%d)",
aColor.getRed(), aColor.getGreen(), aColor.getBlue()));
setCellBackColor(xCellRange.getCellByPosition(4, n + 1),
aColor.getRGB() & 0x00FFFFFF);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void insertColorDemo(){
try {
String sName = "My New Color";
Color aColor = new Color(25, 100, 200);
if (m_xColorTable != null && !m_xColorTable.hasByName(sName)){
m_xColorTable.insertByName(sName,
Integer.valueOf(aColor.getRGB() & 0x00FFFFFF));
System.out.printf("%nInserting color \"%s\" : RGB(%d,%d,%d)... %s%n",
sName,
aColor.getRed(), aColor.getGreen(), aColor.getBlue(),
m_xColorTable.hasByName(sName)? "OK" : "FAILURE");
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void insertTextIntoCell(XCell xCell, String sText){
try {
XText xCellText = (XText) UnoRuntime.queryInterface(XText.class, xCell);
xCellText.setString(sText);
} catch (Exception e) {
e.printStackTrace();
}
}
private void setCellBackColor(XCell xCell, int nColor) {
try {
XPropertySet xCellProps = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xCell);
xCellProps.setPropertyValue("BackColor", new Integer(nColor));
} catch (Exception e) {
e.printStackTrace();
}
}
private XComponent createNewDoc(XComponentContext xContext, String docType) {
XComponent xComponent = null;
try {
String loadUrl = "private:factory/s" + docType;
XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(
XComponentLoader.class,
xContext.getServiceManager().createInstanceWithContext(
"com.sun.star.frame.Desktop", xContext));
xComponent = xComponentLoader.loadComponentFromURL(
loadUrl, "_blank",FrameSearchFlag.ALL, new PropertyValue[0]);
} catch (Exception e) {
e.printStackTrace();
} finally {
return xComponent;
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int nStatus = 0;
try {
// get the remote office component context
XComponentContext xContext = Bootstrap.bootstrap();
if (xContext == null) {
System.err.println("ERROR: Could not bootstrap default Office.");
} else {
ColorTableDemo demo = new ColorTableDemo(xContext);
demo.runDemo();
}
}
catch (java.lang.Exception e){
e.printStackTrace();
nStatus = -1;
}
finally {
System.exit( nStatus );
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]