Skip to content

Commit

Permalink
Write quantities backward compatible when performing export
Browse files Browse the repository at this point in the history
  • Loading branch information
dpolivaev committed Feb 27, 2016
1 parent 32f06f9 commit fab6733
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 22 deletions.
@@ -0,0 +1,37 @@
package org.freeplane.core.io;

import org.freeplane.core.ui.LengthUnits;
import org.freeplane.core.util.Quantity;
import org.freeplane.features.map.MapWriter;

public class BackwardCompatibleQuantityWriter {

final private ITreeWriter writer;
final private boolean makeCompatible;

static public BackwardCompatibleQuantityWriter forWriter(ITreeWriter writer){
final Object hint = writer.getHint(BackwardCompatibleQuantityWriter.class);
if(Boolean.FALSE.equals(hint)){
final boolean makeCompatible = Boolean.TRUE.equals(writer.getHint(MapWriter.WriterHint.FORCE_FORMATTING));
final BackwardCompatibleQuantityWriter quantityWriter = new BackwardCompatibleQuantityWriter(writer, makeCompatible);
writer.setHint(BackwardCompatibleQuantityWriter.class, quantityWriter);
return quantityWriter;
}
return (BackwardCompatibleQuantityWriter) hint;
}

public BackwardCompatibleQuantityWriter(ITreeWriter writer, boolean makeCompatible) {
this.writer = writer;
this.makeCompatible = makeCompatible;
}

public void writeQuantity(String name, final Quantity<LengthUnits> value) {
if(makeCompatible){
writer.addAttribute(name, value.toBaseUnitsRounded());
writer.addAttribute(name+"_QUANTITY", value.toString());
}
else
writer.addAttribute(name, value.toString());
}

}
Expand Up @@ -24,10 +24,12 @@

import org.freeplane.core.extension.IExtension;
import org.freeplane.core.io.IAttributeHandler;
import org.freeplane.core.io.IAttributeWriter;
import org.freeplane.core.io.IElementDOMHandler;
import org.freeplane.core.io.IElementHandler;
import org.freeplane.core.io.IExtensionElementWriter;
import org.freeplane.core.io.ITreeWriter;
import org.freeplane.core.io.BackwardCompatibleQuantityWriter;
import org.freeplane.core.io.ReadManager;
import org.freeplane.core.io.WriteManager;
import org.freeplane.core.ui.LengthUnits;
Expand Down Expand Up @@ -244,6 +246,7 @@ public void setAttribute(final Object userObject, final String value) {
}
});
}


/**
*/
Expand All @@ -266,6 +269,7 @@ public void writeContent(final ITreeWriter writer, final Object node, final IExt
attributes.write(writer);
}
});
writer.addAttributeWriter(XML_NODE_ATTRIBUTE_LAYOUT, AttributeWriter.INSTANCE);
registerAttributeHandlers(reader);
}

Expand All @@ -280,30 +284,37 @@ void save(NodeModel node, NodeAttributeTableModel table, final ITreeWriter write
}

private static final Quantity<LengthUnits> DEFAULT_COLUMN_WIDTH = new Quantity<LengthUnits>(60, LengthUnits.pt);
private void saveLayout(AttributeTableLayoutModel layout, final ITreeWriter writer) throws IOException {
if (layout != null) {
XMLElement attributeElement = null;
if (!DEFAULT_COLUMN_WIDTH.equals(layout.getColumnWidth(0))) {
attributeElement = initializeNodeAttributeLayoutXMLElement(attributeElement);
attributeElement.setAttribute("NAME_WIDTH", layout.getColumnWidth(0).toString());
}
if (!DEFAULT_COLUMN_WIDTH.equals(layout.getColumnWidth(1))) {
attributeElement = initializeNodeAttributeLayoutXMLElement(attributeElement);
attributeElement.setAttribute("VALUE_WIDTH", layout.getColumnWidth(1).toString());
static class AttributeWriter implements IAttributeWriter{
static AttributeWriter INSTANCE = new AttributeWriter();

@Override
public void writeAttributes(ITreeWriter writer, Object userObject, String tag) {
AttributeTableLayoutModel layout = (AttributeTableLayoutModel) userObject;
final Quantity<LengthUnits> firstColumnWidth = layout.getColumnWidth(0);
final Quantity<LengthUnits> secondColumnWidth = layout.getColumnWidth(1);
final boolean firstColumnHasOwnWidth = !DEFAULT_COLUMN_WIDTH.equals(firstColumnWidth);
final boolean secondColumnHasOwnWidth = !DEFAULT_COLUMN_WIDTH.equals(secondColumnWidth);
if (firstColumnHasOwnWidth) {
BackwardCompatibleQuantityWriter.forWriter(writer).writeQuantity("NAME_WIDTH", firstColumnWidth);
}
if (attributeElement != null) {
writer.addElement(layout, attributeElement);
if (secondColumnHasOwnWidth) {
BackwardCompatibleQuantityWriter.forWriter(writer).writeQuantity("VALUE_WIDTH", secondColumnWidth);
}
}

}
private XMLElement initializeNodeAttributeLayoutXMLElement(XMLElement attributeElement) {
if (attributeElement == null) {
attributeElement = new XMLElement();
attributeElement.setName(AttributeBuilder.XML_NODE_ATTRIBUTE_LAYOUT);
private void saveLayout(AttributeTableLayoutModel layout, final ITreeWriter writer) throws IOException {
if (layout != null) {
final Quantity<LengthUnits> firstColumnWidth = layout.getColumnWidth(0);
final Quantity<LengthUnits> secondColumnWidth = layout.getColumnWidth(1);
final boolean firstColumnHasOwnWidth = !DEFAULT_COLUMN_WIDTH.equals(firstColumnWidth);
final boolean secondColumnHasOwnWidth = !DEFAULT_COLUMN_WIDTH.equals(secondColumnWidth);
if (firstColumnHasOwnWidth || secondColumnHasOwnWidth ) {
writer.addElement(layout, AttributeBuilder.XML_NODE_ATTRIBUTE_LAYOUT);
}
}
return attributeElement;
}

private void saveAttribute(NodeModel node, final ITreeWriter writer, final Attribute attr) throws IOException {
final XMLElement attributeElement = new XMLElement();
attributeElement.setName(AttributeBuilder.XML_NODE_ATTRIBUTE);
Expand Down
Expand Up @@ -30,6 +30,7 @@
import org.freeplane.core.io.IExtensionAttributeWriter;
import org.freeplane.core.io.IExtensionElementWriter;
import org.freeplane.core.io.ITreeWriter;
import org.freeplane.core.io.BackwardCompatibleQuantityWriter;
import org.freeplane.core.io.ReadManager;
import org.freeplane.core.io.WriteManager;
import org.freeplane.core.ui.LengthUnits;
Expand Down Expand Up @@ -305,11 +306,11 @@ private void writeAttributes(final ITreeWriter writer, final NodeModel node, fin
}
final Quantity<LengthUnits> shapeHorizontalMargin = shapeConfiguration.getHorizontalMargin();
if (! shapeHorizontalMargin.equals(ShapeConfigurationModel.DEFAULT_MARGIN)) {
writer.addAttribute("SHAPE_HORIZONTAL_MARGIN", shapeHorizontalMargin.toString());
BackwardCompatibleQuantityWriter.forWriter(writer).writeQuantity("SHAPE_HORIZONTAL_MARGIN", shapeHorizontalMargin);
}
final Quantity<LengthUnits> shapeVerticalMargin = shapeConfiguration.getVerticalMargin();
if (! shapeVerticalMargin.equals(ShapeConfigurationModel.DEFAULT_MARGIN)) {
writer.addAttribute("SHAPE_VERTICAL_MARGIN", shapeVerticalMargin.toString());
BackwardCompatibleQuantityWriter.forWriter(writer).writeQuantity("SHAPE_VERTICAL_MARGIN", shapeVerticalMargin);
}
final boolean uniformShape = shapeConfiguration.isUniform();
if (uniformShape) {
Expand All @@ -333,12 +334,12 @@ private void writeAttributes(final ITreeWriter writer, final NodeModel node, fin
final boolean forceFormatting) {
final Quantity<LengthUnits> maxTextWidth = forceFormatting ? nsc.getMaxWidth(node) : size.getMaxNodeWidth();
if (maxTextWidth != null) {
writer.addAttribute("MAX_WIDTH", maxTextWidth.toString());
BackwardCompatibleQuantityWriter.forWriter(writer).writeQuantity("MAX_WIDTH", maxTextWidth);
}

final Quantity<LengthUnits> minTextWidth = forceFormatting ? nsc.getMinWidth(node) : size.getMinNodeWidth();
if (minTextWidth != null) {
writer.addAttribute("MIN_WIDTH", minTextWidth.toString());
BackwardCompatibleQuantityWriter.forWriter(writer).writeQuantity("MIN_WIDTH", minTextWidth);
}
}
public void writeContent(final ITreeWriter writer, final Object userObject, final String tag) throws IOException {
Expand Down

0 comments on commit fab6733

Please sign in to comment.