comparison 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
comparison
equal deleted inserted replaced
103:2d6540440fe6 104:04b47443bb01
16 import dwtx.jface.resource.FontDescriptor; 16 import dwtx.jface.resource.FontDescriptor;
17 import dwtx.jface.resource.StringConverter; 17 import dwtx.jface.resource.StringConverter;
18 import dwtx.jface.resource.JFaceResources; 18 import dwtx.jface.resource.JFaceResources;
19 import dwtx.jface.resource.DataFormatException; 19 import dwtx.jface.resource.DataFormatException;
20 20
21 import tango.util.collection.ArraySeq;
22 import dwtx.dwtxhelper.JHashMap; 21 import dwtx.dwtxhelper.JHashMap;
23 import tango.util.collection.model.Map;
24 import tango.util.collection.model.Seq;
25 import tango.util.collection.model.Set;
26 import tango.util.collection.model.SetView;
27 import tango.util.collection.HashSet;
28 // import java.util.Arrays; 22 // import java.util.Arrays;
29 // import java.util.Collections; 23 // import java.util.Collections;
30 // import java.util.Enumeration; 24 // import java.util.Enumeration;
31 // import java.util.HashMap; 25 // import java.util.HashMap;
32 // import java.util.Iterator; 26 // import java.util.Iterator;
42 import dwt.widgets.Shell; 36 import dwt.widgets.Shell;
43 import dwtx.core.runtime.Assert; 37 import dwtx.core.runtime.Assert;
44 38
45 import dwt.dwthelper.ResourceBundle; 39 import dwt.dwthelper.ResourceBundle;
46 import dwt.dwthelper.utils; 40 import dwt.dwthelper.utils;
41 import dwtx.dwtxhelper.Collection;
47 import dwt.dwthelper.Runnable; 42 import dwt.dwthelper.Runnable;
48 version(Windows) import dwt.internal.win32.OS; 43 version(Windows) import dwt.internal.win32.OS;
49 44
50 /** 45 /**
51 * A font registry maintains a mapping between symbolic font names 46 * A font registry maintains a mapping between symbolic font names
179 */ 174 */
180 void addAllocatedFontsToStale(Font defaultFont) { 175 void addAllocatedFontsToStale(Font defaultFont) {
181 //Return all of the fonts allocated by the receiver. 176 //Return all of the fonts allocated by the receiver.
182 //if any of them are the defaultFont then don't bother. 177 //if any of them are the defaultFont then don't bother.
183 if (defaultFont !is baseFont && baseFont !is null) { 178 if (defaultFont !is baseFont && baseFont !is null) {
184 staleFonts.append(baseFont); 179 staleFonts.add(baseFont);
185 } 180 }
186 if (defaultFont !is boldFont && boldFont !is null) { 181 if (defaultFont !is boldFont && boldFont !is null) {
187 staleFonts.append(boldFont); 182 staleFonts.add(boldFont);
188 } 183 }
189 if (defaultFont !is italicFont && italicFont !is null) { 184 if (defaultFont !is italicFont && italicFont !is null) {
190 staleFonts.append(italicFont); 185 staleFonts.add(italicFont);
191 } 186 }
192 } 187 }
193 } 188 }
194 189
195 /** 190 /**
196 * Table of known fonts, keyed by symbolic font name 191 * Table of known fonts, keyed by symbolic font name
197 * (key type: <code>String</code>, 192 * (key type: <code>String</code>,
198 * value type: <code>FontRecord</code>. 193 * value type: <code>FontRecord</code>.
199 */ 194 */
200 private JHashMapT!(String,FontRecord) stringToFontRecord; 195 private Map stringToFontRecord;
201 196
202 /** 197 /**
203 * Table of known font data, keyed by symbolic font name 198 * Table of known font data, keyed by symbolic font name
204 * (key type: <code>String</code>, 199 * (key type: <code>String</code>,
205 * value type: <code>dwt.graphics.FontData[]</code>). 200 * value type: <code>dwt.graphics.FontData[]</code>).
206 */ 201 */
207 private JHashMapT!(String,FontData[]) stringToFontData; 202 private Map stringToFontData;
208 203
209 /** 204 /**
210 * Collection of Fonts that are now stale to be disposed 205 * Collection of Fonts that are now stale to be disposed
211 * when it is safe to do so (i.e. on shutdown). 206 * when it is safe to do so (i.e. on shutdown).
212 * @see List 207 * @see List
213 */ 208 */
214 private Seq!(Font) staleFonts; 209 private List staleFonts;
215 210
216 /** 211 /**
217 * Runnable that cleans up the manager on disposal of the display. 212 * Runnable that cleans up the manager on disposal of the display.
218 */ 213 */
219 protected Runnable displayRunnable; 214 protected Runnable displayRunnable;
305 displayRunnable = new class Runnable { 300 displayRunnable = new class Runnable {
306 public void run() { 301 public void run() {
307 clearCaches(); 302 clearCaches();
308 } 303 }
309 }; 304 };
310 stringToFontRecord = new JHashMapT!(String,FontRecord); 305 stringToFontRecord = new HashMap(7);
311 //stringToFontRecord.capacity(7); 306 stringToFontData = new HashMap(7);
312 307 staleFonts = new ArrayList();
313 stringToFontData = new JHashMapT!(String,FontData[]);
314 //stringToFontData.capacity(7);
315
316 staleFonts = new ArraySeq!(Font);
317 } 308 }
318 309
319 /** 310 /**
320 * Load the FontRegistry using the ClassLoader from the PlatformUI 311 * Load the FontRegistry using the ClassLoader from the PlatformUI
321 * plug-in 312 * plug-in
495 * @param display the display to check against 486 * @param display the display to check against
496 * @return the list of fonts that have been found on this system 487 * @return the list of fonts that have been found on this system
497 * @since 3.1 488 * @since 3.1
498 */ 489 */
499 public FontData [] filterData(FontData [] fonts, Display display) { 490 public FontData [] filterData(FontData [] fonts, Display display) {
500 ArraySeq!(FontData) good = new ArraySeq!(FontData); 491 ArrayList good = new ArrayList(fonts.length);
501 good.capacity(fonts.length);
502 for (int i = 0; i < fonts.length; i++) { 492 for (int i = 0; i < fonts.length; i++) {
503 FontData fd = fonts[i]; 493 FontData fd = fonts[i];
504 494
505 if (fd is null) { 495 if (fd is null) {
506 continue; 496 continue;
507 } 497 }
508 498
509 FontData[] fixedFonts = display.getFontList(fd.getName(), false); 499 FontData[] fixedFonts = display.getFontList(fd.getName(), false);
510 if (isFixedFont(fixedFonts, fd)) { 500 if (isFixedFont(fixedFonts, fd)) {
511 good.append(fd); 501 good.add(fd);
512 } 502 }
513 503
514 FontData[] scalableFonts = display.getFontList(fd.getName(), true); 504 FontData[] scalableFonts = display.getFontList(fd.getName(), true);
515 if (scalableFonts.length > 0) { 505 if (scalableFonts.length > 0) {
516 good.append(fd); 506 good.add(fd);
517 } 507 }
518 } 508 }
519 509
520 510
521 //None of the provided datas are valid. Return the 511 //None of the provided datas are valid. Return the
522 //first one as it is at least the first choice. 512 //first one as it is at least the first choice.
523 if (good.drained() && fonts.length > 0) { 513 if (good.isEmpty() && fonts.length > 0) {
524 good.append(fonts[0]); 514 good.add(fonts[0]);
525 } 515 }
526 else if (fonts.length is 0) { 516 else if (fonts.length is 0) {
527 return null; 517 return null;
528 } 518 }
529 519
530 return good.toArray(); 520 return arraycast!(FontData)(good.toArray());
531 } 521 }
532 522
533 523
534 /** 524 /**
535 * Creates a new font with the given font datas or <code>null</code> 525 * Creates a new font with the given font datas or <code>null</code>
600 /** 590 /**
601 * Returns the default font record. 591 * Returns the default font record.
602 */ 592 */
603 private FontRecord defaultFontRecord() { 593 private FontRecord defaultFontRecord() {
604 594
605 FontRecord record = cast(FontRecord) stringToFontRecord.find(JFaceResources.DEFAULT_FONT); 595 FontRecord record = cast(FontRecord) stringToFontRecord
596 .get(JFaceResources.DEFAULT_FONT);
606 if (record is null) { 597 if (record is null) {
607 Font defaultFont = calculateDefaultFont(); 598 Font defaultFont = calculateDefaultFont();
608 record = createFont(JFaceResources.DEFAULT_FONT, defaultFont 599 record = createFont(JFaceResources.DEFAULT_FONT, defaultFont
609 .getFontData()); 600 .getFontData());
610 defaultFont.dispose(); 601 defaultFont.dispose();
611 stringToFontRecord.add(JFaceResources.DEFAULT_FONT, record); 602 stringToFontRecord.put(stringcast(JFaceResources.DEFAULT_FONT), record);
612 } 603 }
613 return record; 604 return record;
614 } 605 }
615 606
616 /** 607 /**
629 * @return the font 620 * @return the font
630 */ 621 */
631 public FontData[] getFontData(String symbolicName) { 622 public FontData[] getFontData(String symbolicName) {
632 623
633 Assert.isTrue(symbolicName.length > 0); 624 Assert.isTrue(symbolicName.length > 0);
634 auto result = stringToFontData.find(symbolicName); 625 Object result = stringToFontData.get(symbolicName);
635 if (result.length is 0) { 626 if (result is null) {
636 return defaultFontData(); 627 return defaultFontData();
637 } 628 }
638 629
639 return result; 630 return arrayFromObject!(FontData)(result);
640 } 631 }
641 632
642 /** 633 /**
643 * Returns the font associated with the given symbolic font name. 634 * Returns the font associated with the given symbolic font name.
644 * Returns the default font if there is no special value associated 635 * Returns the default font if there is no special value associated
685 * @param symbolicName The key for the record. 676 * @param symbolicName The key for the record.
686 * @return FontRecird 677 * @return FontRecird
687 */ 678 */
688 private FontRecord getFontRecord(String symbolicName) { 679 private FontRecord getFontRecord(String symbolicName) {
689 Assert.isNotNull(symbolicName); 680 Assert.isNotNull(symbolicName);
690 auto result1 = stringToFontRecord.find(symbolicName); 681 auto result = stringToFontRecord.get(symbolicName);
691 if (result1 !is null) { 682 if (result !is null) {
692 return cast(FontRecord) result1; 683 return cast(FontRecord) result;
693 } 684 }
694 685
695 auto result = stringToFontData.find(symbolicName); 686 result = stringToFontData.get(symbolicName);
696 687
697 FontRecord fontRecord; 688 FontRecord fontRecord;
698 689
699 if (result is null) { 690 if (result is null) {
700 fontRecord = defaultFontRecord(); 691 fontRecord = defaultFontRecord();
701 } else { 692 } else {
702 fontRecord = createFont(symbolicName, result); 693 fontRecord = createFont(symbolicName, arrayFromObject!(FontData)(result));
703 } 694 }
704 695
705 if (fontRecord is null) { 696 if (fontRecord is null) {
706 fontRecord = defaultFontRecord(); 697 fontRecord = defaultFontRecord();
707 } 698 }
708 699
709 stringToFontRecord.add(symbolicName.dup, fontRecord); 700 stringToFontRecord.put(symbolicName.dup, fontRecord);
710 return fontRecord; 701 return fontRecord;
711 702
712 } 703 }
713 704
714 /* (non-Javadoc) 705 /* (non-Javadoc)
715 * @see dwtx.jface.resource.ResourceRegistry#getKeySet() 706 * @see dwtx.jface.resource.ResourceRegistry#getKeySet()
716 */ 707 */
717 public override SetView!(String) getKeySet() { 708 public override Set getKeySet() {
718 auto res = new HashSet!(String); 709 return Collections.unmodifiableSet(stringToFontData.keySet());
719 foreach( k, v; stringToFontData ){
720 res.add( k );
721 }
722 return res;
723 } 710 }
724 711
725 /* (non-Javadoc) 712 /* (non-Javadoc)
726 * @see dwtx.jface.resource.ResourceRegistry#hasValueFor(java.lang.String) 713 * @see dwtx.jface.resource.ResourceRegistry#hasValueFor(java.lang.String)
727 */ 714 */
731 718
732 /* (non-Javadoc) 719 /* (non-Javadoc)
733 * @see dwtx.jface.resource.ResourceRegistry#clearCaches() 720 * @see dwtx.jface.resource.ResourceRegistry#clearCaches()
734 */ 721 */
735 protected override void clearCaches() { 722 protected override void clearCaches() {
736 foreach( k,v; stringToFontRecord ){ 723
737 v.dispose(); 724 Iterator iterator = stringToFontRecord.values().iterator();
738 } 725 while (iterator.hasNext()) {
739 foreach( fnt; staleFonts.toArray ){ 726 Object next = iterator.next();
740 fnt.dispose(); 727 (cast(FontRecord) next).dispose();
741 } 728 }
742 729
730 disposeFonts(staleFonts.iterator());
743 stringToFontRecord.clear(); 731 stringToFontRecord.clear();
744 staleFonts.clear(); 732 staleFonts.clear();
745 733
746 displayDisposeHooked = false; 734 displayDisposeHooked = false;
735 }
736
737 /**
738 * Dispose of all of the fonts in this iterator.
739 * @param iterator over Collection of Font
740 */
741 private void disposeFonts(Iterator iterator) {
742 while (iterator.hasNext()) {
743 Object next = iterator.next();
744 (cast(Font) next).dispose();
745 }
747 } 746 }
748 747
749 /** 748 /**
750 * Hook a dispose listener on the DWT display. 749 * Hook a dispose listener on the DWT display.
751 */ 750 */
817 private void put(String symbolicName, FontData[] fontData, bool update) { 816 private void put(String symbolicName, FontData[] fontData, bool update) {
818 817
819 Assert.isNotNull(symbolicName); 818 Assert.isNotNull(symbolicName);
820 Assert.isTrue(fontData.length > 0 ); 819 Assert.isTrue(fontData.length > 0 );
821 820
822 FontData[] existing = stringToFontData.find(symbolicName); 821 FontData[] existing = arrayFromObject!(FontData)( stringToFontData.get(stringcast(symbolicName)));
823 if (ArrayEquals(existing, fontData)) { 822 if (ArrayEquals(existing, fontData)) {
824 return; 823 return;
825 } 824 }
826 825
827 FontRecord oldFont = stringToFontRecord.find(symbolicName); 826 FontRecord oldFont = cast(FontRecord) stringToFontRecord.remove(stringcast(symbolicName));
828 stringToFontRecord.removeKey(symbolicName); 827 stringToFontData.put(symbolicName.dup, new ArrayWrapperObject(fontData));
829 stringToFontData.add(symbolicName.dup, fontData);
830 if (update) { 828 if (update) {
831 fireMappingChanged(symbolicName, new ArrayWrapperT!(FontData)(existing), new ArrayWrapperT!(FontData)(fontData)); 829 fireMappingChanged(symbolicName, new ArrayWrapperT!(FontData)(existing), new ArrayWrapperObject(arraycast!(Object)(fontData)));
832 } 830 }
833 831
834 if (oldFont !is null) { 832 if (oldFont !is null) {
835 oldFont.addAllocatedFontsToStale(defaultFontRecord().getBaseFont()); 833 oldFont.addAllocatedFontsToStale(defaultFontRecord().getBaseFont());
836 } 834 }
840 * Reads the resource bundle. This puts FontData[] objects 838 * Reads the resource bundle. This puts FontData[] objects
841 * in the mapping table. These will lazily be turned into 839 * in the mapping table. These will lazily be turned into
842 * real Font objects when requested. 840 * real Font objects when requested.
843 */ 841 */
844 private void readResourceBundle(ResourceBundle bundle, String bundleName) { 842 private void readResourceBundle(ResourceBundle bundle, String bundleName) {
845 foreach( key; bundle.getKeys() ){ 843 auto keys = bundle.getKeys();
844 foreach ( key; keys ) {
846 int pos = key.lastIndexOf('.'); 845 int pos = key.lastIndexOf('.');
847 if (pos is -1) { 846 if (pos is -1) {
848 stringToFontData.add(key.dup, [ makeFontData(bundle.getString(key)) ]); 847 stringToFontData.put(stringcast(key.dup), new ArrayWrapperObject(arraycast!(Object)([ makeFontData(bundle
848 .getString(key)) ])));
849 } else { 849 } else {
850 String name = key.substring(0, pos); 850 String name = key.substring(0, pos);
851 int i = 0; 851 int i = 0;
852 try { 852 try {
853 i = tango.text.convert.Integer.toInt(key.substring(pos + 1)); 853 i = tango.text.convert.Integer.toInt(key.substring(pos + 1));
854 } catch (IllegalArgumentException e) { 854 } catch (IllegalArgumentException e) {
855 //Panic the file can not be parsed. 855 //Panic the file can not be parsed.
856 throw new MissingResourceException( 856 throw new MissingResourceException(
857 "Wrong key format ", bundleName, key); //$NON-NLS-1$ 857 "Wrong key format ", bundleName, key); //$NON-NLS-1$
858 } 858 }
859 FontData[] elements = stringToFontData.find(name); 859 FontData[] elements = arrayFromObject!(FontData)( stringToFontData.get(stringcast(name)));
860 if (elements is null) { 860 if (elements is null) {
861 elements = new FontData[8]; 861 elements = new FontData[8];
862 stringToFontData.add(name.dup, elements); 862 stringToFontData.put(name.dup, new ArrayWrapperObject(elements));
863 } 863 }
864 if (i > elements.length) { 864 if (i > elements.length) {
865 FontData[] na = new FontData[i + 8]; 865 FontData[] na = new FontData[i + 8];
866 System.arraycopy(elements, 0, na, 0, elements.length); 866 System.arraycopy(elements, 0, na, 0, elements.length);
867 elements = na; 867 elements = na;
868 stringToFontData.add(name.dup, elements); 868 stringToFontData.put(name.dup, new ArrayWrapperObject(elements));
869 } 869 }
870 elements[i] = makeFontData(bundle.getString(key)); 870 elements[i] = makeFontData(bundle.getString(key));
871 } 871 }
872 } 872 }
873 } 873 }