harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r557138 - in /harmony/enhanced/classlib/trunk/modules/beans/src: main/java/java/beans/ main/java/org/apache/harmony/beans/editors/ test/java/org/apache/harmony/beans/tests/java/beans/
Date Wed, 18 Jul 2007 04:03:46 GMT
Author: pyang
Date: Tue Jul 17 21:03:44 2007
New Revision: 557138

URL: http://svn.apache.org/viewvc?view=rev&rev=557138
Log:
Add test to FontEditor and ColorEditor against RI's behavior, fix bugs; Also fix some trivial
bugs in PropertyEditorManager, add cache mechanism to the findEditor

Modified:
    harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/PropertyEditorManager.java
    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/java/beans/PropertyEditorManager.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/PropertyEditorManager.java?view=diff&rev=557138&r1=557137&r2=557138
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/PropertyEditorManager.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/beans/src/main/java/java/beans/PropertyEditorManager.java
Tue Jul 17 21:03:44 2007
@@ -54,8 +54,14 @@
         PropertyEditor editor = null;
 
         editorClass = registeredEditors.get(targetType);
-
-        if (editorClass == null) {
+        if (editorClass != null) {
+            try {
+                editor = (PropertyEditor) editorClass.newInstance();
+            } catch (Exception e) {
+            }
+        }
+        
+        if (editor == null) {
             String editorClassName = targetType.getName() + "Editor"; //$NON-NLS-1$
             ClassLoader loader = targetType.getClassLoader();
 
@@ -65,6 +71,7 @@
 
             try {
                 editorClass = Class.forName(editorClassName, true, loader);
+                return (PropertyEditor) editorClass.newInstance();
             } catch (ClassNotFoundException cnfe) {
                 String shortEditorClassName = editorClassName
                         .substring(editorClassName.lastIndexOf(".") + 1); //$NON-NLS-1$
@@ -81,21 +88,22 @@
                     try {
                         editorClass = Class.forName(editorClassName, true,
                                 loader);
+                        editorClass.asSubclass(PropertyEditorSupport.class);
                         break;
                     } catch (Exception e) {
                     }
                 }
             } catch (Exception e) {
             }
-        }
-
-        if (editorClass != null) {
-            try {
-                editor = (PropertyEditor) editorClass.newInstance();
-            } catch (Exception e) {
+            if(editorClass != null){
+                try {
+                    //FIXME: cache is still needed, but need more investigation to make tests
pass
+//                    registeredEditors.put(targetType, editorClass);
+                    editor = (PropertyEditor) editorClass.newInstance();
+                } catch (Exception e) {
+                }    
             }
         }
-
         return editor;
     }
 
@@ -113,6 +121,6 @@
     }
 
     public static synchronized String[] getEditorSearchPath() {
-        return path;
+        return path.clone();
     }
 }

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=557138&r1=557137&r2=557138
==============================================================================
--- 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
Tue Jul 17 21:03:44 2007
@@ -68,6 +68,17 @@
         }
     }
     
+    public String getAsText(){
+        Color c = (Color)getValue();
+        StringBuilder sb = new StringBuilder(14);
+        sb.append(c.getRed());
+        sb.append(",");
+        sb.append(c.getGreen());
+        sb.append(",");
+        sb.append(c.getBlue());
+        return sb.toString();
+    }
+    
     @Override
     public void setAsText(String text) {
         if (null == text) {

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=557138&r1=557137&r2=557138
==============================================================================
--- 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
Tue Jul 17 21:03:44 2007
@@ -20,66 +20,126 @@
 import java.awt.Component;
 import java.awt.Font;
 import java.awt.Graphics;
+import java.awt.Panel;
 import java.awt.Rectangle;
-import java.beans.PropertyEditorSupport;
-
-public class FontEditor extends 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 FontEditor extends Panel implements PropertyEditor {
+    
+    List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();
+    
+    private Font value;
+    
+    private Object source;
+    
     public FontEditor(Object source) {
-        super(source);
+        if(source== null){
+            throw new NullPointerException();
+        }
+        this.source = (Font)source;
     }
 
     public FontEditor() {
         super();
     }
 
-    @Override
     public Component getCustomEditor() {
-        return null;
+        return this;
     }
 
-    @Override
     public boolean supportsCustomEditor() {
         return true;
     }
 
-    @Override
     public String getJavaInitializationString() {
         String result = null;
-        Font font = (Font) getValue();
-        if (font != null) {
-            String name = font.getName();
-            int style = font.getStyle();
-            int size = font.getSize();
+        if (value!= null) {
+            String name = value.getName();
+            int style = value.getStyle();
+            int size = value.getSize();
             result = "new Font(" + name + "," + style + "," + size + ")"; //$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 Font) {
-            super.setValue(value);
+    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);
+        PropertyChangeListener[] copy = new PropertyChangeListener[listeners.size()];
+        listeners.toArray(copy);
+        for (PropertyChangeListener listener : copy) {
+            listener.propertyChange(changeAllEvent);
         }
     }
 
-    @Override
     public boolean isPaintable() {
         return true;
     }
 
-    @Override
     public void paintValue(Graphics gfx, Rectangle box) {
         Font font = (Font) getValue();
         if (font != null) {
             gfx.setFont(font);
             gfx.drawBytes("Hello".getBytes(), box.x, box.y, box.x + box.width, //$NON-NLS-1$
                     box.y + box.height);
+        }
+    }
+
+    public String getAsText() {
+        return null;
+    }
+
+    public Object getValue() {
+        return value;
+    }
+
+    public void setAsText(String text) throws IllegalArgumentException {
+        throw new IllegalArgumentException(text==null?text:value.toString());
+    }
+    
+    public synchronized void removePropertyChangeListener(
+            PropertyChangeListener listener) {
+        if (listeners != null) {
+            listeners.remove(listener);
+        }
+    }
+
+    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/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=557138&r1=557137&r2=557138
==============================================================================
--- 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
Tue Jul 17 21:03:44 2007
@@ -17,7 +17,9 @@
 
 package org.apache.harmony.beans.tests.java.beans;
 
+import java.awt.Color;
 import java.awt.Component;
+import java.awt.Font;
 import java.awt.Graphics;
 import java.awt.Rectangle;
 import java.beans.PropertyChangeListener;
@@ -723,6 +725,37 @@
 
         assertNotNull("No property editor found", pe);
         assertTrue(pe instanceof AnotherSamplePropertyEditor);
+    }
+    
+    public void testFontEditor() throws Exception{
+        PropertyEditor e2 = PropertyEditorManager.findEditor(Font.class);
+        Font font = new Font("Helvetica", Font.PLAIN, 12);
+        e2.setValue(font);
+        assertNull(e2.getAsText());
+        assertNull(e2.getTags());
+        assertSame(font, e2.getValue());
+        assertTrue(e2.isPaintable());
+        Component c = (Component) e2.getCustomEditor();
+        assertSame(c, e2);
+    }
+    
+    public void testColorEditor() throws Exception{
+        PropertyEditor e2 = PropertyEditorManager.findEditor(Color.class);
+        e2.setValue(Color.RED);
+        e2.setAsText(e2.getAsText());
+        assertNull(e2.getTags());
+        assertNotSame(Color.RED, e2.getValue());
+        assertEquals(Color.RED, e2.getValue());
+        assertTrue(e2.isPaintable());
+    }
+    
+    public void testGetSetEditorPath() throws Exception{
+      String[] s = new String[]{"path1", "path2"};
+      PropertyEditorManager.setEditorSearchPath(s);
+      s[1] = "path3";
+      String[] s2 = PropertyEditorManager.getEditorSearchPath();
+      assertFalse(s==s2);
+      assertEquals("path1", s2[0]);
     }
     
     String[] defaultSearchPath;



Mime
View raw message