add file version info

improve image serialization
serialize points referenced in inches
This commit is contained in:
bancika
2011-02-27 23:23:51 +00:00
parent 209e970c00
commit 5ae8a3477e
7 changed files with 81 additions and 5 deletions
@@ -59,7 +59,7 @@
</entry>
<entry>
<string>default.org.diylc.components.misc.Label:Font Bold</string>
<boolean>true</boolean>
<boolean>false</boolean>
</entry>
<entry>
<string>autoCreatePads</string>
@@ -11,6 +11,8 @@ import org.diylc.core.annotations.PositiveMeasureValidator;
import org.diylc.core.measures.Size;
import org.diylc.core.measures.SizeUnit;
import com.diyfever.gui.update.VersionNumber;
/**
* Entity class that defines a project. Contains project properties and a
* collection of components.This class is serialized to file. Some filed getters
@@ -27,6 +29,8 @@ public class Project implements Serializable {
public static Size DEFAULT_HEIGHT = new Size(21d, SizeUnit.cm);
public static Size DEFAULT_GRID_SPACING = new Size(0.1d, SizeUnit.in);
private VersionNumber fileVersion;
private String title;
private String author;
private String description;
@@ -123,6 +127,14 @@ public class Project implements Serializable {
public Set<Integer> getLockedLayers() {
return lockedLayers;
}
public VersionNumber getFileVersion() {
return fileVersion;
}
public void setFileVersion(VersionNumber fileVersion) {
this.fileVersion = fileVersion;
}
@Override
public int hashCode() {
@@ -0,0 +1,41 @@
package org.diylc.presenter;
import java.awt.Point;
import org.diylc.utils.Constants;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
/**
* Serializes {@link Point} objects by converting coordinates from pixels to
* inches, thus avoiding the resolution to affect point placement.
*
* @author Branislav Stojkovic
*/
public class PointConverter implements Converter {
@Override
public void marshal(Object object, HierarchicalStreamWriter writer, MarshallingContext context) {
Point point = (Point) object;
writer.addAttribute("x", Double.toString(1d * point.x / Constants.PIXELS_PER_INCH));
writer.addAttribute("y", Double.toString(1d * point.y / Constants.PIXELS_PER_INCH));
}
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
double x = Double.parseDouble(reader.getAttribute("x"));
double y = Double.parseDouble(reader.getAttribute("y"));
return new Point((int) Math.round(x * Constants.PIXELS_PER_INCH), (int) Math.round(y
* Constants.PIXELS_PER_INCH));
}
@SuppressWarnings("unchecked")
@Override
public boolean canConvert(Class clazz) {
return Point.class.isAssignableFrom(clazz);
}
}
@@ -245,6 +245,7 @@ public class Presenter implements IPlugInPort {
public void saveProjectToFile(String fileName, boolean isBackup) {
LOG.info(String.format("saveProjectToFile(%s)", fileName));
try {
currentProject.setFileVersion(CURRENT_VERSION);
projectFileManager.serializeProjectToFile(currentProject, fileName, isBackup);
} catch (Exception ex) {
LOG.error("Could not save file", ex);
@@ -68,6 +68,9 @@ public class ProjectFileManager {
}
private XStream xStream;
// Legacy deserializer for 3.0.1 through 3.0.7, loads Points referenced in
// pixels.
private XStream xStreamOld;
private String currentFileName = null;
private boolean modified = false;
@@ -78,6 +81,9 @@ public class ProjectFileManager {
super();
this.xStream = new XStream(new DomDriver());
xStream.autodetectAnnotations(true);
xStream.registerConverter(new PointConverter());
this.xStreamOld = new XStream(new DomDriver());
xStreamOld.autodetectAnnotations(true);
this.messageDispatcher = messageDispatcher;
}
@@ -436,7 +442,14 @@ public class ProjectFileManager {
private Project parseV3File(String fileName) throws IOException {
Project project;
FileInputStream fis = new FileInputStream(fileName);
project = (Project) xStream.fromXML(fis);
try {
project = (Project) xStream.fromXML(fis);
} catch (Exception e) {
LOG.warn("Could not open with the new xStream, trying the old one");
fis.close();
fis = new FileInputStream(fileName);
project = (Project) xStreamOld.fromXML(fis);
}
fis.close();
return project;
}
@@ -48,9 +48,14 @@ public class IconImageConverter implements Converter {
writer.addAttribute("width", Integer.toString(width));
writer.addAttribute("height", Integer.toString(height));
StringBuilder dataBuilder = new StringBuilder();
int n = 0;
for (int pixel : pixels) {
dataBuilder.append(Integer.toString(pixel, 16));
dataBuilder.append(",");
n++;
if (n % 16 == 0) {
dataBuilder.append("\n");
}
}
writer.addAttribute("data", dataBuilder.toString());
}
@@ -61,11 +66,15 @@ public class IconImageConverter implements Converter {
int height = Integer.parseInt(reader.getAttribute("height"));
LOG.debug("Reading image from file: " + width + "x" + height);
int[] pixels = new int[width * height];
String data = reader.getAttribute("data");
String data = reader.getAttribute("data").replace("\n", "");
String[] splitData = data.split(",");
for (int i = 0; i < splitData.length; i++) {
for (int i = 0; i < Math.min(splitData.length, pixels.length); i++) {
if (!splitData[i].isEmpty()) {
pixels[i] = Integer.parseInt(splitData[i], 16);
try {
pixels[i] = Integer.parseInt(splitData[i].trim(), 16);
} catch (Exception e) {
pixels[i] = 0;
}
}
}