diff dwtx/jface/resource/FontRegistry.d @ 104:04b47443bb01

Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections. These new wrappers now use the tango.util.containers instead of the tango.util.collections.
author Frank Benoit <benoit@tionex.de>
date Thu, 07 Aug 2008 15:01:33 +0200
parents 7ffeace6c47f
children e4589de2eed6
line wrap: on
line diff
--- a/dwtx/jface/resource/FontRegistry.d	Sun Aug 03 17:01:51 2008 +0200
+++ b/dwtx/jface/resource/FontRegistry.d	Thu Aug 07 15:01:33 2008 +0200
@@ -18,13 +18,7 @@
 import dwtx.jface.resource.JFaceResources;
 import dwtx.jface.resource.DataFormatException;
 
-import tango.util.collection.ArraySeq;
 import dwtx.dwtxhelper.JHashMap;
-import tango.util.collection.model.Map;
-import tango.util.collection.model.Seq;
-import tango.util.collection.model.Set;
-import tango.util.collection.model.SetView;
-import tango.util.collection.HashSet;
 // import java.util.Arrays;
 // import java.util.Collections;
 // import java.util.Enumeration;
@@ -44,6 +38,7 @@
 
 import dwt.dwthelper.ResourceBundle;
 import dwt.dwthelper.utils;
+import dwtx.dwtxhelper.Collection;
 import dwt.dwthelper.Runnable;
 version(Windows) import dwt.internal.win32.OS;
 
@@ -181,13 +176,13 @@
             //Return all of the fonts allocated by the receiver.
             //if any of them are the defaultFont then don't bother.
             if (defaultFont !is baseFont && baseFont !is null) {
-                staleFonts.append(baseFont);
+                staleFonts.add(baseFont);
             }
             if (defaultFont !is boldFont && boldFont !is null) {
-                staleFonts.append(boldFont);
+                staleFonts.add(boldFont);
             }
             if (defaultFont !is italicFont && italicFont !is null) {
-                staleFonts.append(italicFont);
+                staleFonts.add(italicFont);
             }
         }
     }
@@ -197,21 +192,21 @@
      * (key type: <code>String</code>,
      *  value type: <code>FontRecord</code>.
      */
-    private JHashMapT!(String,FontRecord) stringToFontRecord;
+    private Map stringToFontRecord;
 
     /**
      * Table of known font data, keyed by symbolic font name
      * (key type: <code>String</code>,
      *  value type: <code>dwt.graphics.FontData[]</code>).
      */
-    private JHashMapT!(String,FontData[]) stringToFontData;
+    private Map stringToFontData;
 
     /**
      * Collection of Fonts that are now stale to be disposed
      * when it is safe to do so (i.e. on shutdown).
      * @see List
      */
-    private Seq!(Font) staleFonts;
+    private List staleFonts;
 
     /**
      * Runnable that cleans up the manager on disposal of the display.
@@ -307,13 +302,9 @@
                 clearCaches();
             }
         };
-        stringToFontRecord = new JHashMapT!(String,FontRecord);
-        //stringToFontRecord.capacity(7);
-
-        stringToFontData = new JHashMapT!(String,FontData[]);
-        //stringToFontData.capacity(7);
-
-        staleFonts = new ArraySeq!(Font);
+        stringToFontRecord = new HashMap(7);
+        stringToFontData = new HashMap(7);
+        staleFonts = new ArrayList();
     }
 
     /**
@@ -497,8 +488,7 @@
      * @since 3.1
      */
     public FontData [] filterData(FontData [] fonts, Display display) {
-        ArraySeq!(FontData) good = new ArraySeq!(FontData);
-        good.capacity(fonts.length);
+        ArrayList good = new ArrayList(fonts.length);
         for (int i = 0; i < fonts.length; i++) {
             FontData fd = fonts[i];
 
@@ -508,26 +498,26 @@
 
             FontData[] fixedFonts = display.getFontList(fd.getName(), false);
             if (isFixedFont(fixedFonts, fd)) {
-                good.append(fd);
+                good.add(fd);
             }
 
             FontData[] scalableFonts = display.getFontList(fd.getName(), true);
             if (scalableFonts.length > 0) {
-                good.append(fd);
+                good.add(fd);
             }
         }
 
 
         //None of the provided datas are valid. Return the
         //first one as it is at least the first choice.
-        if (good.drained() && fonts.length > 0) {
-            good.append(fonts[0]);
+        if (good.isEmpty() && fonts.length > 0) {
+            good.add(fonts[0]);
         }
         else if (fonts.length is 0) {
             return null;
         }
 
-        return good.toArray();
+        return arraycast!(FontData)(good.toArray());
     }
 
 
@@ -602,13 +592,14 @@
      */
     private FontRecord defaultFontRecord() {
 
-        FontRecord record = cast(FontRecord) stringToFontRecord.find(JFaceResources.DEFAULT_FONT);
+        FontRecord record = cast(FontRecord) stringToFontRecord
+                .get(JFaceResources.DEFAULT_FONT);
         if (record is null) {
             Font defaultFont = calculateDefaultFont();
             record = createFont(JFaceResources.DEFAULT_FONT, defaultFont
                     .getFontData());
             defaultFont.dispose();
-            stringToFontRecord.add(JFaceResources.DEFAULT_FONT, record);
+            stringToFontRecord.put(stringcast(JFaceResources.DEFAULT_FONT), record);
         }
         return record;
     }
@@ -631,12 +622,12 @@
     public FontData[] getFontData(String symbolicName) {
 
         Assert.isTrue(symbolicName.length > 0);
-        auto result = stringToFontData.find(symbolicName);
-        if (result.length is 0) {
+        Object result = stringToFontData.get(symbolicName);
+        if (result is null) {
             return defaultFontData();
         }
 
-        return result;
+        return arrayFromObject!(FontData)(result);
     }
 
     /**
@@ -687,26 +678,26 @@
      */
     private FontRecord getFontRecord(String symbolicName) {
         Assert.isNotNull(symbolicName);
-        auto result1 = stringToFontRecord.find(symbolicName);
-        if (result1 !is null) {
-            return cast(FontRecord) result1;
+        auto result = stringToFontRecord.get(symbolicName);
+        if (result !is null) {
+            return cast(FontRecord) result;
         }
 
-        auto result = stringToFontData.find(symbolicName);
+        result = stringToFontData.get(symbolicName);
 
         FontRecord fontRecord;
 
         if (result is null) {
             fontRecord = defaultFontRecord();
         } else {
-            fontRecord = createFont(symbolicName, result);
+            fontRecord = createFont(symbolicName, arrayFromObject!(FontData)(result));
         }
 
         if (fontRecord is null) {
             fontRecord = defaultFontRecord();
         }
 
-        stringToFontRecord.add(symbolicName.dup, fontRecord);
+        stringToFontRecord.put(symbolicName.dup, fontRecord);
         return fontRecord;
 
     }
@@ -714,12 +705,8 @@
     /* (non-Javadoc)
      * @see dwtx.jface.resource.ResourceRegistry#getKeySet()
      */
-    public override SetView!(String) getKeySet() {
-        auto res = new HashSet!(String);
-        foreach( k, v; stringToFontData ){
-            res.add( k );
-        }
-        return res;
+    public override Set getKeySet() {
+        return Collections.unmodifiableSet(stringToFontData.keySet());
     }
 
     /* (non-Javadoc)
@@ -733,20 +720,32 @@
      * @see dwtx.jface.resource.ResourceRegistry#clearCaches()
      */
     protected override void clearCaches() {
-        foreach( k,v; stringToFontRecord ){
-            v.dispose();
-        }
-        foreach( fnt; staleFonts.toArray ){
-            fnt.dispose();
+
+        Iterator iterator = stringToFontRecord.values().iterator();
+        while (iterator.hasNext()) {
+            Object next = iterator.next();
+            (cast(FontRecord) next).dispose();
         }
 
+        disposeFonts(staleFonts.iterator());
         stringToFontRecord.clear();
         staleFonts.clear();
-        
+
         displayDisposeHooked = false;
     }
 
     /**
+     * Dispose of all of the fonts in this iterator.
+     * @param iterator over Collection of Font
+     */
+    private void disposeFonts(Iterator iterator) {
+        while (iterator.hasNext()) {
+            Object next = iterator.next();
+            (cast(Font) next).dispose();
+        }
+    }
+
+    /**
      * Hook a dispose listener on the DWT display.
      */
     private void hookDisplayDispose(Display display) {
@@ -819,16 +818,15 @@
         Assert.isNotNull(symbolicName);
         Assert.isTrue(fontData.length > 0 );
 
-        FontData[] existing = stringToFontData.find(symbolicName);
+        FontData[] existing = arrayFromObject!(FontData)( stringToFontData.get(stringcast(symbolicName)));
         if (ArrayEquals(existing, fontData)) {
             return;
         }
 
-        FontRecord oldFont = stringToFontRecord.find(symbolicName);
-        stringToFontRecord.removeKey(symbolicName);
-        stringToFontData.add(symbolicName.dup, fontData);
+        FontRecord oldFont = cast(FontRecord) stringToFontRecord.remove(stringcast(symbolicName));
+        stringToFontData.put(symbolicName.dup, new ArrayWrapperObject(fontData));
         if (update) {
-            fireMappingChanged(symbolicName, new ArrayWrapperT!(FontData)(existing), new ArrayWrapperT!(FontData)(fontData));
+            fireMappingChanged(symbolicName, new ArrayWrapperT!(FontData)(existing), new ArrayWrapperObject(arraycast!(Object)(fontData)));
         }
 
         if (oldFont !is null) {
@@ -842,10 +840,12 @@
      * real Font objects when requested.
      */
     private void readResourceBundle(ResourceBundle bundle, String bundleName) {
-        foreach( key; bundle.getKeys() ){
+        auto keys = bundle.getKeys();
+        foreach ( key; keys ) {
             int pos = key.lastIndexOf('.');
             if (pos is -1) {
-                stringToFontData.add(key.dup, [ makeFontData(bundle.getString(key)) ]);
+                stringToFontData.put(stringcast(key.dup), new ArrayWrapperObject(arraycast!(Object)([ makeFontData(bundle
+                        .getString(key)) ])));
             } else {
                 String name = key.substring(0, pos);
                 int i = 0;
@@ -856,16 +856,16 @@
                     throw new MissingResourceException(
                             "Wrong key format ", bundleName, key); //$NON-NLS-1$
                 }
-                FontData[] elements = stringToFontData.find(name);
+                FontData[] elements = arrayFromObject!(FontData)( stringToFontData.get(stringcast(name)));
                 if (elements is null) {
                     elements = new FontData[8];
-                    stringToFontData.add(name.dup, elements);
+                    stringToFontData.put(name.dup, new ArrayWrapperObject(elements));
                 }
                 if (i > elements.length) {
                     FontData[] na = new FontData[i + 8];
                     System.arraycopy(elements, 0, na, 0, elements.length);
                     elements = na;
-                    stringToFontData.add(name.dup, elements);
+                    stringToFontData.put(name.dup, new ArrayWrapperObject(elements));
                 }
                 elements[i] = makeFontData(bundle.getString(key));
             }