From 6e6d70fcd3e0f4e87d9a45f0034fe4f2d1feb4d1 Mon Sep 17 00:00:00 2001 From: bancika Date: Tue, 21 Aug 2012 21:53:21 +0000 Subject: [PATCH] Rectangle selection triggers file (modified) state --- .../diylc/components/AbstractComponent.java | 65 +++++++++++++++---- .../src/org/diylc/core/IDIYComponent.java | 11 +++- .../src/org/diylc/core/Project.java | 29 ++++++--- .../diylc-swing/update.xml | 16 +++++ 4 files changed, 98 insertions(+), 23 deletions(-) diff --git a/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/components/AbstractComponent.java b/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/components/AbstractComponent.java index 1dfb2987..c3269056 100644 --- a/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/components/AbstractComponent.java +++ b/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/components/AbstractComponent.java @@ -12,6 +12,7 @@ import java.awt.font.GlyphVector; import java.lang.reflect.Array; import java.lang.reflect.Field; import java.lang.reflect.Modifier; +import java.util.Arrays; import org.diylc.common.HorizontalAlignment; import org.diylc.common.VerticalAlignment; @@ -88,11 +89,9 @@ public abstract class AbstractComponent implements IDIYComponent { } protected void drawCenteredText(Graphics2D g2d, String text, int x, int y, - HorizontalAlignment horizontalAlignment, - VerticalAlignment verticalAlignment) { + HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment) { FontMetrics fontMetrics = g2d.getFontMetrics(); - Rectangle stringBounds = fontMetrics.getStringBounds(text, g2d) - .getBounds(); + Rectangle stringBounds = fontMetrics.getStringBounds(text, g2d).getBounds(); Font font = g2d.getFont(); FontRenderContext renderContext = g2d.getFontRenderContext(); @@ -132,16 +131,15 @@ public abstract class AbstractComponent implements IDIYComponent { public IDIYComponent clone() throws CloneNotSupportedException { try { // Instantiate object of the same type - AbstractComponent newInstance = (AbstractComponent) this - .getClass().getConstructors()[0].newInstance(); + AbstractComponent newInstance = (AbstractComponent) this.getClass() + .getConstructors()[0].newInstance(); Class clazz = this.getClass(); while (AbstractComponent.class.isAssignableFrom(clazz)) { Field[] fields = clazz.getDeclaredFields(); clazz = clazz.getSuperclass(); // fields = this.getClass().getDeclaredFields(); // Copy over all non-static, non-final fields that are declared - // in - // AbstractComponent or one of it's child classes + // in AbstractComponent or one of it's child classes for (Field field : fields) { if (!Modifier.isStatic(field.getModifiers()) && !Modifier.isFinal(field.getModifiers())) { @@ -154,9 +152,8 @@ public abstract class AbstractComponent implements IDIYComponent { && value.getClass().isArray() && value.getClass().getComponentType() .isAssignableFrom(Point.class)) { - Object newArray = Array.newInstance(value - .getClass().getComponentType(), Array - .getLength(value)); + Object newArray = Array.newInstance( + value.getClass().getComponentType(), Array.getLength(value)); for (int i = 0; i < Array.getLength(value); i++) { Point p = (Point) Array.get(value, i); Array.set(newArray, i, new Point(p)); @@ -175,7 +172,51 @@ public abstract class AbstractComponent implements IDIYComponent { } return newInstance; } catch (Exception e) { - throw new CloneNotSupportedException("Could not clone the component. Reason: " + e.getMessage()); + throw new CloneNotSupportedException("Could not clone the component. Reason: " + + e.getMessage()); } } + + @Override + public boolean equalsTo(IDIYComponent other) { + if (other == null) + return false; + if (!other.getClass().equals(this.getClass())) + return false; + Class clazz = this.getClass(); + while (AbstractComponent.class.isAssignableFrom(clazz)) { + Field[] fields = clazz.getDeclaredFields(); + clazz = clazz.getSuperclass(); + // fields = this.getClass().getDeclaredFields(); + // Copy over all non-static, non-final fields that are declared + // in + // AbstractComponent or one of it's child classes + for (Field field : fields) { + if (!Modifier.isStatic(field.getModifiers()) + && !Modifier.isFinal(field.getModifiers())) { + field.setAccessible(true); + try { + Object value = field.get(this); + Object otherValue = field.get(other); + if (!compareObjects(value, otherValue)) + return false; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + } + } + return true; + } + + private boolean compareObjects(Object o1, Object o2) { + if (o1 == null && o2 == null) + return true; + if (o1 == null || o2 == null) + return false; + if (o1.getClass().isArray()) { + return Arrays.equals((Object[])o1, (Object[])o2); + } + return o1.equals(o2); + } } diff --git a/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/core/IDIYComponent.java b/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/core/IDIYComponent.java index 7a16358e..0f25911c 100644 --- a/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/core/IDIYComponent.java +++ b/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/core/IDIYComponent.java @@ -107,8 +107,7 @@ public interface IDIYComponent extends Serializable { * @param project * @param drawingObserver */ - void draw(Graphics2D g2d, ComponentState componentState, - boolean outlineMode, Project project, + void draw(Graphics2D g2d, ComponentState componentState, boolean outlineMode, Project project, IDrawingObserver drawingObserver); /** @@ -127,4 +126,12 @@ public interface IDIYComponent extends Serializable { * @return */ IDIYComponent clone() throws CloneNotSupportedException; + + /** + * Checks if two components are equal. + * + * @param other + * @return + */ + boolean equalsTo(IDIYComponent other); } diff --git a/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/core/Project.java b/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/core/Project.java index b1f226a9..a4328299 100644 --- a/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/core/Project.java +++ b/ diy-layout-creator --username bancika@gmail.com/diylc-core/src/org/diylc/core/Project.java @@ -4,6 +4,7 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @@ -31,7 +32,7 @@ public class Project implements Serializable { public static Size DEFAULT_GRID_SPACING = new Size(0.1d, SizeUnit.in); private VersionNumber fileVersion; - + private String title; private String author; private String description; @@ -128,11 +129,11 @@ public class Project implements Serializable { public Set getLockedLayers() { return lockedLayers; } - + public VersionNumber getFileVersion() { return fileVersion; } - + public void setFileVersion(VersionNumber fileVersion) { this.fileVersion = fileVersion; } @@ -167,8 +168,18 @@ public class Project implements Serializable { if (components == null) { if (other.components != null) return false; - } else if (!components.equals(other.components)) + } else if (components.size() != other.components.size()) { return false; + } else { + Iterator> i1 = components.iterator(); + Iterator> i2 = other.components.iterator(); + while (i1.hasNext()) { + IDIYComponent c1 = i1.next(); + IDIYComponent c2 = i2.next(); + if (!c1.equalsTo(c2)) + return false; + } + } if (description == null) { if (other.description != null) return false; @@ -213,7 +224,7 @@ public class Project implements Serializable { } @Override - public Project clone() { + public Project clone() { Project project = new Project(); project.setTitle(this.getTitle()); project.setAuthor(this.getAuthor()); @@ -222,10 +233,10 @@ public class Project implements Serializable { project.setGridSpacing(this.getGridSpacing()); project.setHeight(this.getHeight()); project.setWidth(this.getWidth()); - project.getLockedLayers().addAll(this.getLockedLayers()); - + project.getLockedLayers().addAll(this.getLockedLayers()); + Map, IDIYComponent> cloneMap = new HashMap, IDIYComponent>(); - + for (IDIYComponent component : this.components) { try { IDIYComponent clone = component.clone(); @@ -235,7 +246,7 @@ public class Project implements Serializable { throw new RuntimeException(e); } } - + for (Set> group : this.groups) { Set> cloneGroup = new HashSet>(); for (IDIYComponent component : group) { diff --git a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/update.xml b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/update.xml index b93cb67a..e5e1804d 100644 --- a/ diy-layout-creator --username bancika@gmail.com/diylc-swing/update.xml +++ b/ diy-layout-creator --username bancika@gmail.com/diylc-swing/update.xml @@ -717,5 +717,21 @@ http://code.google.com/p/diy-layout-creator/downloads/list + + + 3 + 12 + 0 + + 2012-08-20 21:00:00.000 CET + + + + BUG_FIX + Rectangle selection triggers file (modified) state + + + http://code.google.com/p/diy-layout-creator/downloads/list + \ No newline at end of file