Rectangle selection triggers file (modified) state

This commit is contained in:
bancika
2012-08-21 21:53:21 +00:00
parent 56fef06b22
commit 6e6d70fcd3
4 changed files with 98 additions and 23 deletions
@@ -12,6 +12,7 @@ import java.awt.font.GlyphVector;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Arrays;
import org.diylc.common.HorizontalAlignment; import org.diylc.common.HorizontalAlignment;
import org.diylc.common.VerticalAlignment; import org.diylc.common.VerticalAlignment;
@@ -88,11 +89,9 @@ public abstract class AbstractComponent<T> implements IDIYComponent<T> {
} }
protected void drawCenteredText(Graphics2D g2d, String text, int x, int y, protected void drawCenteredText(Graphics2D g2d, String text, int x, int y,
HorizontalAlignment horizontalAlignment, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment) {
VerticalAlignment verticalAlignment) {
FontMetrics fontMetrics = g2d.getFontMetrics(); FontMetrics fontMetrics = g2d.getFontMetrics();
Rectangle stringBounds = fontMetrics.getStringBounds(text, g2d) Rectangle stringBounds = fontMetrics.getStringBounds(text, g2d).getBounds();
.getBounds();
Font font = g2d.getFont(); Font font = g2d.getFont();
FontRenderContext renderContext = g2d.getFontRenderContext(); FontRenderContext renderContext = g2d.getFontRenderContext();
@@ -132,16 +131,15 @@ public abstract class AbstractComponent<T> implements IDIYComponent<T> {
public IDIYComponent<T> clone() throws CloneNotSupportedException { public IDIYComponent<T> clone() throws CloneNotSupportedException {
try { try {
// Instantiate object of the same type // Instantiate object of the same type
AbstractComponent<T> newInstance = (AbstractComponent<T>) this AbstractComponent<T> newInstance = (AbstractComponent<T>) this.getClass()
.getClass().getConstructors()[0].newInstance(); .getConstructors()[0].newInstance();
Class<?> clazz = this.getClass(); Class<?> clazz = this.getClass();
while (AbstractComponent.class.isAssignableFrom(clazz)) { while (AbstractComponent.class.isAssignableFrom(clazz)) {
Field[] fields = clazz.getDeclaredFields(); Field[] fields = clazz.getDeclaredFields();
clazz = clazz.getSuperclass(); clazz = clazz.getSuperclass();
// fields = this.getClass().getDeclaredFields(); // fields = this.getClass().getDeclaredFields();
// Copy over all non-static, non-final fields that are declared // Copy over all non-static, non-final fields that are declared
// in // in AbstractComponent or one of it's child classes
// AbstractComponent or one of it's child classes
for (Field field : fields) { for (Field field : fields) {
if (!Modifier.isStatic(field.getModifiers()) if (!Modifier.isStatic(field.getModifiers())
&& !Modifier.isFinal(field.getModifiers())) { && !Modifier.isFinal(field.getModifiers())) {
@@ -154,9 +152,8 @@ public abstract class AbstractComponent<T> implements IDIYComponent<T> {
&& value.getClass().isArray() && value.getClass().isArray()
&& value.getClass().getComponentType() && value.getClass().getComponentType()
.isAssignableFrom(Point.class)) { .isAssignableFrom(Point.class)) {
Object newArray = Array.newInstance(value Object newArray = Array.newInstance(
.getClass().getComponentType(), Array value.getClass().getComponentType(), Array.getLength(value));
.getLength(value));
for (int i = 0; i < Array.getLength(value); i++) { for (int i = 0; i < Array.getLength(value); i++) {
Point p = (Point) Array.get(value, i); Point p = (Point) Array.get(value, i);
Array.set(newArray, i, new Point(p)); Array.set(newArray, i, new Point(p));
@@ -175,7 +172,51 @@ public abstract class AbstractComponent<T> implements IDIYComponent<T> {
} }
return newInstance; return newInstance;
} catch (Exception e) { } 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);
}
} }
@@ -107,8 +107,7 @@ public interface IDIYComponent<T> extends Serializable {
* @param project * @param project
* @param drawingObserver * @param drawingObserver
*/ */
void draw(Graphics2D g2d, ComponentState componentState, void draw(Graphics2D g2d, ComponentState componentState, boolean outlineMode, Project project,
boolean outlineMode, Project project,
IDrawingObserver drawingObserver); IDrawingObserver drawingObserver);
/** /**
@@ -127,4 +126,12 @@ public interface IDIYComponent<T> extends Serializable {
* @return * @return
*/ */
IDIYComponent<T> clone() throws CloneNotSupportedException; IDIYComponent<T> clone() throws CloneNotSupportedException;
/**
* Checks if two components are equal.
*
* @param other
* @return
*/
boolean equalsTo(IDIYComponent<?> other);
} }
@@ -4,6 +4,7 @@ import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@@ -167,8 +168,18 @@ public class Project implements Serializable {
if (components == null) { if (components == null) {
if (other.components != null) if (other.components != null)
return false; return false;
} else if (!components.equals(other.components)) } else if (components.size() != other.components.size()) {
return false; return false;
} else {
Iterator<IDIYComponent<?>> i1 = components.iterator();
Iterator<IDIYComponent<?>> 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 (description == null) {
if (other.description != null) if (other.description != null)
return false; return false;
@@ -213,7 +224,7 @@ public class Project implements Serializable {
} }
@Override @Override
public Project clone() { public Project clone() {
Project project = new Project(); Project project = new Project();
project.setTitle(this.getTitle()); project.setTitle(this.getTitle());
project.setAuthor(this.getAuthor()); project.setAuthor(this.getAuthor());
@@ -717,5 +717,21 @@
</changes> </changes>
<url>http://code.google.com/p/diy-layout-creator/downloads/list</url> <url>http://code.google.com/p/diy-layout-creator/downloads/list</url>
</org.diylc.appframework.update.Version> </org.diylc.appframework.update.Version>
<org.diylc.appframework.update.Version>
<versionNumber>
<major>3</major>
<minor>12</minor>
<build>0</build>
</versionNumber>
<releaseDate>2012-08-20 21:00:00.000 CET</releaseDate>
<name></name>
<changes>
<org.diylc.appframework.update.Change>
<changeType>BUG_FIX</changeType>
<description>Rectangle selection triggers file (modified) state</description>
</org.diylc.appframework.update.Change>
</changes>
<url>http://code.google.com/p/diy-layout-creator/downloads/list</url>
</org.diylc.appframework.update.Version>
</a> </a>
</java.util.Arrays_-ArrayList> </java.util.Arrays_-ArrayList>