Yearly Archives: 2012

4 posts

Eclipse Draw2D Border Magic

While using Eclipse Draw2D I stumbled over some quite interesting behaviour on win32 using rectangles and scaling.

We have a scalable layer and added a rectangle. When the layer was actually scaling (so not 1.0) strange artifacts appeared on win32 platforms. Finally it turned out that it was a negative line size with the outline flag set to true. Which renders on Linux as if outline would be set to false. Yet interesting was that setting the lineSize to 0.0 (zero) while keeping the outline flag true causes a hairline to be drawn. So a line which is 1 pixel, independent of the scale factor.

Settings Screenshot
[code language=”Java”]
figure.setOutline ( false );
[/code]
[code language=”Java”]
figure.setOutline ( true );
[/code]

Which is the same as:
[code language=”Java”]
figure.setOutline ( true );
figure.setLineWidthFloat ( 1.0f );
[/code]

[code language=”Java”]
figure.setOutline ( true );
figure.setLineWidthFloat ( 0.0f );
[/code]

This is the actual hairline variant.

[code language=”Java”]
figure.setOutline ( true );
figure.setLineWidthFloat ( 2.0f );
[/code]
[code language=”Java”]
figure.setOutline ( true );
figure.setLineWidthFloat ( -1.0f );
[/code]

Note the small artefacts at on the sides. These are invisible if run under Linux.

The full code to create the canvas was:

[code language=”Java”]
ScalableLayeredPane pane = new ScalableLayeredPane();

Layer layer = new Layer();
layer.setLayoutManager(new StackLayout());

pane.add ( layer );

Figure figure = new Figure ();
figure.setLayoutManager(new XYLayout());
figure.setBackgroundColor(Display.getDefault().getSystemColor(SWT.COLOR_DARK_GRAY));
figure.setOpaque(true);

RectangleFigure r = new RectangleFigure();
r.setBackgroundColor(Display.getDefault().getSystemColor(SWT.COLOR_YELLOW));
figure.add(r, new Rectangle(10,10,20,20));
layer.add ( figure );

pane.setScale(4.0);
r.setLineWidthFloat(0.0f);
r.setOutline(true);
[/code]

Setting the page size and orientation with ODFDOM for tables aka spreadsheets

Most tasks that seem to be quite easy tend to turn out as a desaster. Setting the page size and orientation with ODFDOM was such a thing. While styling with ODFDOM is powerfull and a horror at the same time. You actually never know where the attribute have to be placed or which attributes to use.

After I finished an export to “ods” (aka spreadsheet) it simply wanted to set the format to “A4 landscape”. Here is how to do that:

First create your spreadsheet and sheet ( you might already have done that):

[code language=”Java”]
OdfSpreadsheetDocument output = OdfSpreadsheetDocument.newSpreadsheetDocument ();
final OdfTable sheet = OdfTable.newTable ( output );
[/code]

Now change the styles to “A4 landscape”:

First we need to get the “master page” named “Default”:
[code language=”Java”]
StyleMasterPageElement defaultPage = output.getOfficeMasterStyles ().getMasterPage ( "Default" );
[/code]

The master page tells us the name of the page style:
[code language=”Java”]
String pageLayoutName = defaultPage.getStylePageLayoutNameAttribute ();
[/code]

Which gives us the page layout object:
[code language=”Java”]
OdfStylePageLayout pageLayout = defaultPage.getAutomaticStyles ().getPageLayout ( pageLayoutName );
[/code]

Finally we can set “A4 landscape”:
[code language=”Java”]
pageLayout.setProperty ( OdfPageLayoutProperties.PrintOrientation, "landscape" );
pageLayout.setProperty ( OdfPageLayoutProperties.PageHeight, "210.01mm" );
pageLayout.setProperty ( OdfPageLayoutProperties.PageWidth, "297mm" );
pageLayout.setProperty ( OdfPageLayoutProperties.NumFormat, "1" );
[/code]

All four properties seem to be required. Also the width and height have to be rotatate according to “landscape”.

Extending OpenWRT

We recently switched from IPfire to OpenWRT in our office. Not only because we let OpenWRT run on an ALIX board (which also IPfire could do), but mostly due to the fact that if you want to extend the system you really can do it. Development of IPfire seems to be quite difficutly, not only technically.

On the other hand OpenWRT quickly provides help if you want to contribute. Also too the “noob” questions you will post in the beginning ;-)

My initial idea was to create a small plugin for OpenWRT which allows one to configure “pptpd” and VPN users using the web interface LUCI. While OpenWRTs configuration system (called UCI) handles the configuraton model in the background, LuCI brings this models to a Lua driven web user interface. UCI and LuCI play well together, you only need to know how it works together.

Continue reading

Working with ODFDOM

While working a little bit with ODFDOM to automatically generate some documentation I found some tasks rather difficult to accomplish due to the fact that they were hardly documented. There is a lot of ODF documentation, but how to use it with ODFDOM is another story.

So first a list of documentation I found:

Adding a new paragraph with a pre-defined style

To create a new paragraph with a style I used the following static function:
[code language=”Java”]
public static OdfTextParagraph newStyledParagraph ( final OdfTextDocument odt, final String style, final String content ) throws Exception
{
final OdfTextParagraph p;
if ( content != null ) {
p = odt.newParagraph ( content );
} else {
p = odt.newParagraph ();
}
p.setStyleName ( style );
return p;
}
[/code]

Adding fields (like title and subtitle)

The following snippet will create a new paragraph

[code language=”Java”]
OdfTextParagraph p;
p = OdfHelper.newStyledParagraph ( odt, "Title", null );
p.newTextTitleElement ();

p = OdfHelper.newStyledParagraph ( odt, "Subtitle", null );
p.newTextSubjectElement ();
[/code]

Creating a new parapgraph style

The next snippet creates a new a new style called “Source Text” dereived from “Text Boby”. The setFontFamily method is a helper to set all font properties at once.

[code language=”Java”]
protected void setFontFamily ( final OdfStyleBase style, final String value )
{
style.setProperty ( OdfTextProperties.FontFamily, value );
style.setProperty ( OdfTextProperties.FontFamilyAsian, value );
style.setProperty ( OdfTextProperties.FontFamilyComplex, value );
}

private void createStyles ( final OdfTextDocument odt )
{
final OdfOfficeStyles styles = odt.getOrCreateDocumentStyles ();

final OdfStyle style = styles.newStyle ( "Source Text", OdfStyleFamily.Paragraph );
style.setStyleParentStyleNameAttribute ( "Text Body" );
style.setProperty ( OdfParagraphProperties.Margin, "1cm" );
style.setProperty ( OdfParagraphProperties.BackgroundColor, "#DDDDDD" );
setFontFamily ( style, "Courier New" );
}
[/code]

Copy content from one to another ODF File

While most of the document I made was created some static part a the beginning was needed. Instead of creating it manually I created a new ODT File in LibreOffice and imported the content to the generated document.

The following snipped does that:
[code language=”Java”]
private void createStaticContent ( final OdfTextDocument odt, final File file ) throws Exception
{
// check if file is readable and is a file
if ( !file.canRead () || !file.isFile () )
{
return;
}

// Load the document to import

final OdfTextDocument staticOdt = OdfTextDocument.loadDocument ( file );

// Create a new text section which will receive the content. This is optional.
// You could also load the content directly on the root of the document

final TextSectionElement section = new TextSectionElement ( odt.getContentDom () );
odt.getContentRoot ().appendChild ( section );
section.setTextProtectedAttribute ( true );
section.setTextNameAttribute ( "Static Content" ); //$NON-NLS-1$

// iterate over all nodes
final NodeList childNodes = staticOdt.getContentRoot ().getChildNodes ();
for ( int i = 0; i < childNodes.getLength (); i++ )
{
// clone node from source
final Node newNode = childNodes.item ( i ).cloneNode ( true );
// import to target DOM
final Node adoptedNode = odt.getContentDom ().adoptNode ( newNode );
// append to section
section.appendChild ( adoptedNode );
}
}
Since only content is imported the styles of the target document will be used for the content that was imported.
[/code]