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.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<T> implements IDIYComponent<T> {
}
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<T> implements IDIYComponent<T> {
public IDIYComponent<T> clone() throws CloneNotSupportedException {
try {
// Instantiate object of the same type
AbstractComponent<T> newInstance = (AbstractComponent<T>) this
.getClass().getConstructors()[0].newInstance();
AbstractComponent<T> newInstance = (AbstractComponent<T>) 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<T> implements IDIYComponent<T> {
&& 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<T> implements IDIYComponent<T> {
}
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);
}
}
@@ -107,8 +107,7 @@ public interface IDIYComponent<T> 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<T> extends Serializable {
* @return
*/
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.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<Integer> 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<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 (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<?>, IDIYComponent<?>> cloneMap = new HashMap<IDIYComponent<?>, 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<IDIYComponent<?>> group : this.groups) {
Set<IDIYComponent<?>> cloneGroup = new HashSet<IDIYComponent<?>>();
for (IDIYComponent<?> component : group) {