resource file handling cleaned up
authorClaus Gittinger <cg@exept.de>
Wed, 03 Dec 2003 12:48:56 +0100
changeset 1869 c6800c75b7d0
parent 1868 0f5605dfc7d5
child 1870 c0475972bf3e
resource file handling cleaned up
MIMETypeIconLibrary.st
--- a/MIMETypeIconLibrary.st	Wed Dec 03 09:40:26 2003 +0100
+++ b/MIMETypeIconLibrary.st	Wed Dec 03 12:48:56 2003 +0100
@@ -14,69 +14,41 @@
     self initializeIcons
 !
 
-initializeFileTypeIcons
-    |resources|
+initializeIcons
+    |defaults resources addMapping fileNameToImageMap|
+
+    Icons := Dictionary new.
+    MatchedIcons := OrderedCollection new.
+    fileNameToImageMap := Dictionary new.
 
     resources := ResourcePack for:self.
 
+    defaults := Dictionary new.
     #(
         "/ internal-type to icon mappings.
-        (#directory             'ICON_DIRECTORY'            'tiny_yellow_dir.xpm'            )
-        (#directoryOpen         'ICON_DIRECTORY_OPEN'       'tiny_yellow_dir_open.xpm'       )
-        (#directoryLocked       'ICON_DIRECTORY_LOCKED'     'tiny_yellow_dir_locked.xpm'     )
-        (#directoryLink         'ICON_DIRECTORY_LINK'       'tiny_yellow_dir_link.xpm'       )
-        (#directoryNetwork      'ICON_DIRECTORY_NETWORK'    'tiny_yellow_dir_network.xpm'    )
-        (#file                  'ICON_FILE'                 'tiny_file_plain.xpm'            )
-        (#fileLink              'ICON_FILE_LINK'            'tiny_file_link.xpm'             )
-        (#fileLocked            'ICON_FILE_LOCKED'          'tiny_file_lock.xpm'             )
-        (#imageFile             'ICON_IMAGE_FILE'           'tiny_file_pix.xpm'              )
-        (#textFile              'ICON_TEXT_FILE'            'tiny_file_text.xpm'             )
-        (#executableFile        'ICON_EXECUTABLEFILE'       'tiny_file_exec.xpm'             )
-        (#deviceFile            'ICON_DEVICE_FILE'          'tiny_file_device.xpm'           )
-        (#binaryFile            'ICON_BINARY_FILE'          'tiny_file_binary.xpm'           )
-        (#specialFile           'ICON_SPECIALFILE'          'tiny_file_special.xpm'          )
-
-        (#addOnLocked           'ICON_ADDON_LOCKED'         'tiny_addOn_lock.xpm'            )
-        (#addOnLinked           'ICON_ADDON_LINKED'         'tiny_addOn_link.xpm'            )
-
-        (#directoryGray         'ICON_DIRECTORY_GRAY'       'tiny_yellow_dir_gray.xpm'       )
-        (#directoryOpenGray     'ICON_DIRECTORY_OPEN_GRAY'  'tiny_yellow_dir_open_gray.xpm'  )
-     ) do:[:entry |
-        |key resource defaultName nm img|
-
-        key := entry at:1.
-        resource := entry at:2.
-        defaultName := entry at:3.
+        #directory             'tiny_yellow_dir.xpm'            
+        #directoryOpen         'tiny_yellow_dir_open.xpm'       
+        #directoryLocked       'tiny_yellow_dir_locked.xpm'     
+        #directoryLink         'tiny_yellow_dir_link.xpm'       
+        #directoryNetwork      'tiny_yellow_dir_network.xpm'    
+        #file                  'tiny_file_plain.xpm'            
+        #fileLink              'tiny_file_link.xpm'             
+        #fileLocked            'tiny_file_lock.xpm'             
 
-        resource notNil ifTrue:[
-            nm := resources at:resource default:nil.
-        ].
-        nm isNil ifTrue:[
-            nm := 'xpmBitmaps/document_images/' , defaultName
-        ].
-        Icons at:key put:(Smalltalk imageFromFileNamed:nm forClass:self).
-    ]
-
-    "
-     self initializeFileTypeIcons
-    "
-!
+        #executableFile        'tiny_file_exec.xpm'             
+        #deviceFile            'tiny_file_device.xpm'           
+        #binaryFile            'tiny_file_binary.xpm'           
+        #specialFile           'tiny_file_special.xpm'          
 
-initializeIcons
-    Icons := Dictionary new.
-    MatchedIcons := OrderedCollection new.
-    self initializeFileTypeIcons.
-    self initializeMimeTypeIcons.
+        #imageFile             'tiny_file_pix.xpm'              
+        #textFile              'tiny_file_text.xpm'             
 
-    "
-     self initializeIcons
-    "
+        #addOnLocked           'tiny_addOn_lock.xpm'            
+        #addOnLinked           'tiny_addOn_link.xpm'            
 
-    "Modified: / 1.8.1998 / 17:39:27 / cg"
-!
+        #directoryGray         'tiny_yellow_dir_gray.xpm'       
+        #directoryOpenGray     'tiny_yellow_dir_open_gray.xpm'  
 
-initializeMimeTypeIcons
-    #(
         "/ mime-type to icon mappings.
 "/        'image/jpeg'                                    'tiny_file_pix.xpm'
 "/        'image/gif'                                     'tiny_file_pix.xpm'
@@ -153,26 +125,50 @@
         'application/x-asn1-source'                     'tiny_file_text.xpm'
 
         "/ compressors / archivers
-        'application/x-tar'                           'tiny_file_archive.xpm'
-        'application/x-gtar'                          'tiny_file_archive.xpm'
-        'application/x-tar-compressed'                'tiny_file_compressed_archive.xpm'
-        'application/x-zip-compressed'                'tiny_file_compressed_archive.xpm'
-        'application/x-gzip-compressed'               'tiny_file_compressed_archive.xpm'
-        'application/x-cpio'                          'tiny_file_archive.xpm'
-        'application/x-shar'                          'tiny_file_archive.xpm'
-        'application/java-archive'                    'tiny_file_archive.xpm'
+        'application/x-tar'                             'tiny_file_archive.xpm'
+        'application/x-gtar'                            'tiny_file_archive.xpm'
+        'application/x-tar-compressed'                  'tiny_file_compressed_archive.xpm'
+        'application/x-zip-compressed'                  'tiny_file_compressed_archive.xpm'
+        'application/x-gzip-compressed'                 'tiny_file_compressed_archive.xpm'
+        'application/x-cpio'                            'tiny_file_archive.xpm'
+        'application/x-shar'                            'tiny_file_archive.xpm'
+        'application/java-archive'                      'tiny_file_archive.xpm'
+
+        '*'                                             'tiny_file_plain.xpm'
+     ) pairWiseDo:[:fileTypeOrMimeTypePattern :baseName |
+        defaults at:fileTypeOrMimeTypePattern put:baseName.
+    ].
+
+    addMapping := [:key :baseNameOrName |
+        |fileName image|
 
-        '*'                                           'tiny_file_plain.xpm'
-     ) pairWiseDo:[:mimeTypePattern :fileName |
-        | nm |
+        baseNameOrName notNil ifTrue:[
+            image := fileNameToImageMap at:baseNameOrName ifAbsent:nil.
+            image isNil ifTrue:[
+                image := Smalltalk imageFromFileNamed:baseNameOrName forClass:self.
+                image isNil ifTrue:[
+                    fileName := 'bitmaps/xpmBitmaps/document_images/' , baseNameOrName.
+                    image := Smalltalk imageFromFileNamed:fileName forClass:self.
+                ].
+            ].
+            fileNameToImageMap at:baseNameOrName put:image.
+            key includesMatchCharacters ifTrue:[
+                MatchedIcons add:(key -> image).
+            ] ifFalse:[    
+                Icons at:key put:image.
+            ]
+        ]
+    ].
 
-        nm := 'xpmBitmaps/document_images/' , fileName.
-        mimeTypePattern includesMatchCharacters ifTrue:[
-            MatchedIcons add:(mimeTypePattern-> (Smalltalk imageFromFileNamed:nm forClass:self)).
-        ] ifFalse:[    
-            Icons at:mimeTypePattern put:(Smalltalk imageFromFileNamed:nm forClass:self).
-        ]
-    ]
+    "all mappings from the resource file"
+    resources keysAndValuesDo:addMapping.
+
+    "missing mappings from above defaults"
+    defaults keysAndValuesDo:[:key :baseName |
+        (resources includesKey:key) ifFalse:[
+            addMapping value:key value:baseName
+        ].
+    ].
 
     "
      self initializeIcons
@@ -251,7 +247,7 @@
 !MIMETypeIconLibrary class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libview2/MIMETypeIconLibrary.st,v 1.6 2003-12-02 10:08:47 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libview2/MIMETypeIconLibrary.st,v 1.7 2003-12-03 11:48:56 cg Exp $'
 ! !
 
 MIMETypeIconLibrary initialize!