You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2007/08/01 13:50:50 UTC
svn commit: r561769 - in /harmony/enhanced/classlib/trunk/modules/beans/src:
main/java/org/apache/harmony/beans/editors/
test/java/org/apache/harmony/beans/tests/java/beans/
Author: tellison
Date: Wed Aug 1 04:50:49 2007
New Revision: 561769
URL: http://svn.apache.org/viewvc?view=rev&rev=561769
Log:
Apply patch HARMONY-4578 ([classlib][beans] o.a.h.b.editors.ColorEditor & FontEditor do not have correct behaviors)
Modified:
harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/editors/ColorEditor.java
harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/editors/FontEditor.java
harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PropertyEditorManagerTest.java
Modified: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/editors/ColorEditor.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/editors/ColorEditor.java?view=diff&rev=561769&r1=561768&r2=561769
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/editors/ColorEditor.java (original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/editors/ColorEditor.java Wed Aug 1 04:50:49 2007
@@ -20,30 +20,43 @@
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
+import java.awt.Panel;
import java.awt.Rectangle;
-import java.beans.PropertyEditorSupport;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.beans.PropertyEditor;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
-public class ColorEditor extends PropertyEditorSupport {
+@SuppressWarnings("serial")
+public class ColorEditor extends Panel implements PropertyEditor {
+List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();
+
+ private Color value;
+
+ private Object source;
+
public ColorEditor(Object source) {
- super(source);
+ if(null == source) {
+ throw new NullPointerException();
+ }
+ this.source = source;
}
public ColorEditor() {
super();
}
- @Override
public Component getCustomEditor() {
- return null;
+ return this;
}
- @Override
public boolean supportsCustomEditor() {
return true;
}
- @Override
public String getJavaInitializationString() {
String result = null;
Color color = (Color) getValue();
@@ -51,23 +64,31 @@
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
- result = "new Color(" + red + "," + green + "," + blue + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ result = "new java.awt.Color(" + red + "," + green + "," + blue + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
}
return result;
}
- @Override
public String[] getTags() {
return null;
}
- @Override
public void setValue(Object value) {
- if (value instanceof Color) {
- super.setValue(value);
+ if( null == value) {
+ return;
+ }
+ Object oldValue = this.value;
+ this.value = (Color) value;
+ PropertyChangeEvent changeAllEvent = new PropertyChangeEvent(this,
+ "value", oldValue, value); //$NON-NLS-1$
+ PropertyChangeListener[] copy = new PropertyChangeListener[listeners.size()];
+ listeners.toArray(copy);
+ for (PropertyChangeListener listener : copy) {
+ listener.propertyChange(changeAllEvent);
}
}
+ @SuppressWarnings("nls")
public String getAsText(){
Color c = (Color)getValue();
StringBuilder sb = new StringBuilder(14);
@@ -79,7 +100,7 @@
return sb.toString();
}
- @Override
+ @SuppressWarnings("nls")
public void setAsText(String text) {
if (null == text) {
throw new NullPointerException();
@@ -88,33 +109,69 @@
int r = 0;
int g = 0;
int b = 0;
-
+ String aText = text;
try {
int index = text.indexOf(",");
r = Integer.parseInt(text.substring(0, index));
- text = text.substring(index + 1);
- index = text.indexOf(",");
- g = Integer.parseInt(text.substring(0, index));
- text = text.substring(index + 1);
- b = Integer.parseInt(text);
+ aText = text.substring(index + 1);
+ index = aText.indexOf(",");
+ g = Integer.parseInt(aText.substring(0, index));
+ aText = aText.substring(index + 1);
+ b = Integer.parseInt(aText);
setValue(new Color(r, g, b));
} catch (Exception e) {
- throw new IllegalArgumentException(text);
+ throw new IllegalArgumentException(aText);
}
}
- @Override
public boolean isPaintable() {
return true;
}
- @Override
public void paintValue(Graphics gfx, Rectangle box) {
Color color = (Color) getValue();
if (color != null) {
gfx.setColor(color);
gfx.drawRect(box.x, box.y, box.x + box.width, box.y + box.height);
+ }
+ }
+
+ public Object getValue() {
+ return value;
+ }
+
+ @Override
+ public synchronized void removePropertyChangeListener(
+ PropertyChangeListener listener) {
+ if (listeners != null) {
+ listeners.remove(listener);
+ }
+ }
+
+ @Override
+ public synchronized void addPropertyChangeListener(
+ PropertyChangeListener listener) {
+ listeners.add(listener);
+ }
+
+ public void firePropertyChange() {
+ if (listeners.isEmpty()) {
+ return;
+ }
+
+ List<PropertyChangeListener> copy = new ArrayList<PropertyChangeListener>(
+ listeners.size());
+ synchronized (listeners) {
+ copy.addAll(listeners);
+ }
+
+ PropertyChangeEvent changeAllEvent = new PropertyChangeEvent(source,
+ null, null, null);
+ for (Iterator<PropertyChangeListener> listenersItr = copy.iterator(); listenersItr
+ .hasNext();) {
+ PropertyChangeListener listna = listenersItr.next();
+ listna.propertyChange(changeAllEvent);
}
}
}
Modified: harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/editors/FontEditor.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/editors/FontEditor.java?view=diff&rev=561769&r1=561768&r2=561769
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/editors/FontEditor.java (original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/org/apache/harmony/beans/editors/FontEditor.java Wed Aug 1 04:50:49 2007
@@ -42,7 +42,7 @@
if(source== null){
throw new NullPointerException();
}
- this.source = (Font)source;
+ this.source = source;
}
public FontEditor() {
@@ -73,13 +73,10 @@
}
public void setValue(Object newValue) {
- if(newValue == null){
- throw new NullPointerException();
- }
Object oldValue = value;
value = (Font)newValue;
PropertyChangeEvent changeAllEvent = new PropertyChangeEvent(this,
- "value", oldValue, value);
+ "value", oldValue, value); //$NON-NLS-1$
PropertyChangeListener[] copy = new PropertyChangeListener[listeners.size()];
listeners.toArray(copy);
for (PropertyChangeListener listener : copy) {
@@ -112,6 +109,7 @@
throw new IllegalArgumentException(text==null?text:value.toString());
}
+ @Override
public synchronized void removePropertyChangeListener(
PropertyChangeListener listener) {
if (listeners != null) {
@@ -119,6 +117,7 @@
}
}
+ @Override
public synchronized void addPropertyChangeListener(
PropertyChangeListener listener) {
listeners.add(listener);
Modified: harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PropertyEditorManagerTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PropertyEditorManagerTest.java?view=diff&rev=561769&r1=561768&r2=561769
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PropertyEditorManagerTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/test/java/org/apache/harmony/beans/tests/java/beans/PropertyEditorManagerTest.java Wed Aug 1 04:50:49 2007
@@ -21,7 +21,9 @@
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
+import java.awt.Panel;
import java.awt.Rectangle;
+import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
@@ -735,10 +737,39 @@
assertNull(e2.getTags());
assertSame(font, e2.getValue());
assertTrue(e2.isPaintable());
- Component c = (Component) e2.getCustomEditor();
+ Component c = e2.getCustomEditor();
assertSame(c, e2);
+ e2.addPropertyChangeListener(new ExceptionPropertyChangeListener());
+
+ try {
+ e2.setValue(null);
+ fail("Should throw an error");
+ } catch (MockError e) {
+ // expected
+ assertNull(e2.getValue());
+ }
+
+ try {
+ e2.setValue(new Font("Arial", Font.BOLD, 10));
+ fail("Should throw an error");
+ } catch (MockError e) {
+ // expected
+ }
}
+ @SuppressWarnings("serial")
+ public static class MockError extends Error {
+ public MockError(String msg) {
+ super(msg);
+ }
+ }
+ public static class ExceptionPropertyChangeListener implements PropertyChangeListener {
+
+ public void propertyChange(PropertyChangeEvent event) {
+ throw new MockError("property changed");
+ }
+
+ }
public void testColorEditor() throws Exception{
PropertyEditor e2 = PropertyEditorManager.findEditor(Color.class);
e2.setValue(Color.RED);
@@ -747,6 +778,47 @@
assertNotSame(Color.RED, e2.getValue());
assertEquals(Color.RED, e2.getValue());
assertTrue(e2.isPaintable());
+ assertTrue(e2 instanceof Panel);
+ assertTrue(e2 instanceof PropertyEditor);
+ assertTrue(e2.supportsCustomEditor());
+ assertSame(e2, e2.getCustomEditor());
+ assertEquals("new java.awt.Color(255,0,0)", e2
+ .getJavaInitializationString());
+ assertNull(e2.getTags());
+
+ ExceptionPropertyChangeListener listener = new ExceptionPropertyChangeListener();
+ e2.addPropertyChangeListener(listener);
+
+ e2.setValue(null);
+ assertEquals(Color.RED.getRed(), ((Color) e2.getValue()).getRed());
+
+ try {
+ e2.setValue(Color.yellow);
+ fail("Should throw an error");
+ } catch (MockError e) {
+ // expected
+ }
+
+ assertEquals("255,255,0", e2.getAsText());
+
+ try {
+ e2.setAsText(null);
+ fail("Should throw NPE");
+ } catch (NullPointerException e) {
+ // expected
+ }
+
+ try {
+ e2.setAsText("text");
+ fail("Should throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+
+ e2.removePropertyChangeListener(listener);
+ e2.setAsText("255,255,255");
+ assertEquals("java.awt.Color[r=255,g=255,b=255]", ((Color) e2
+ .getValue()).toString());
}
public void testGetSetEditorPath() throws Exception{