AbstractFileBrowser.st
changeset 4721 2fe2500194e6
parent 4708 abd269f1d39f
child 4722 62f29a42dded
equal deleted inserted replaced
4720:fb924a6ecd39 4721:2fe2500194e6
  1946                   #enabled: #hasFileSelection
  1946                   #enabled: #hasFileSelection
  1947                   #label: 'Convert to PNG'
  1947                   #label: 'Convert to PNG'
  1948                   #itemValue: #convertImageToPNG
  1948                   #itemValue: #convertImageToPNG
  1949                   #translateLabel: true
  1949                   #translateLabel: true
  1950                 )
  1950                 )
       
  1951                #(#MenuItem
       
  1952                   #enabled: #hasFileSelection
       
  1953                   #label: 'Convert to XPM'
       
  1954                   #itemValue: #convertImageToXPM
       
  1955                   #translateLabel: true
       
  1956                 )
       
  1957                #(#MenuItem
       
  1958                   #enabled: #hasFileSelection
       
  1959                   #label: 'Convert to JPG'
       
  1960                   #itemValue: #convertImageToJPG
       
  1961                   #translateLabel: true
       
  1962                 )
  1951                )
  1963                )
  1952               nil
  1964               nil
  1953               nil
  1965               nil
  1954             )
  1966             )
  1955           )
  1967           )
  3988     ].
  4000     ].
  3989 ! !
  4001 ! !
  3990 
  4002 
  3991 !AbstractFileBrowser methodsFor:'menu actions tools'!
  4003 !AbstractFileBrowser methodsFor:'menu actions tools'!
  3992 
  4004 
       
  4005 conversionChainFrom:inSuffix to:outSuffix
       
  4006     |conv|
       
  4007 
       
  4008     inSuffix = outSuffix ifTrue:[
       
  4009         ^ nil
       
  4010     ].
       
  4011 
       
  4012     "/ q&d hack to get my images converted for old html-browsers...
       
  4013     "/ this should come from somewhere else (do we need an ImageConverter class ?).
       
  4014 
       
  4015     conv := OrderedCollection new.
       
  4016     conv add:('anytopnm' -> 'pnm').
       
  4017 
       
  4018     outSuffix = 'png' ifTrue:[
       
  4019         conv add:('pnmtopng' -> 'png').
       
  4020         ^ conv.
       
  4021     ].
       
  4022     outSuffix = 'gif' ifTrue:[
       
  4023         conv add:('ppmtogif' -> 'gif').
       
  4024         ^ conv.
       
  4025     ].
       
  4026     outSuffix = 'xpm' ifTrue:[
       
  4027         conv add:('ppmtoxpm' -> 'xpm').
       
  4028         ^ conv.
       
  4029     ].
       
  4030     outSuffix = 'jpg' ifTrue:[
       
  4031         conv add:('ppmtojpeg' -> 'jpg').
       
  4032         ^ conv.
       
  4033     ].
       
  4034     self error:'unimplemented conversion'.
       
  4035 !
       
  4036 
  3993 convertImageToGIF
  4037 convertImageToGIF
  3994     self 
  4038     self convertImageToSuffix:'gif'
  3995         convertImageUsingChain:
  4039 !
  3996             (Array
  4040 
  3997                 with:('ppm' -> 'xpmtoppm')
  4041 convertImageToJPG
  3998                 with:('gif' -> 'ppmtogif')
  4042     self convertImageToSuffix:'jpg'
  3999             )
       
  4000 !
  4043 !
  4001 
  4044 
  4002 convertImageToPNG
  4045 convertImageToPNG
  4003     self 
  4046     self convertImageToSuffix:'png'
  4004         convertImageUsingChain:
  4047 !
  4005             (Array
  4048 
  4006                 with:('ppm' -> 'xpmtoppm')
  4049 convertImageToSuffix:outSuffix
  4007                 with:('png' -> 'ppmtopng')
  4050     |image writer tempFileXPM chainOfConversions conversionStream skipSignal |
  4008             )
  4051 
  4009 !
  4052 "/    [
  4010 
  4053         skipSignal := Signal new.
  4011 convertImageUsingChain:chainOfConversions
  4054 
  4012     |img tempFileXPM|
       
  4013 
       
  4014     [
       
  4015         self withActivityIndicationDo:[
  4055         self withActivityIndicationDo:[
  4016             self currentFileNameHolder value do:[:fileName |
  4056             self currentFileNameHolder value do:[:fileName |
  4017                 |inFile outFile|
  4057                 |imageOrNil inFile outFile eachConversionSuffixCommandPair doneWithThisFile
  4018 
  4058                  eachConversionSuffix eachConversionCommand tempFileTemplate|
  4019                 self notify:('Converting:',  fileName baseName).
  4059 
  4020                 fileName isDirectory ifFalse:[
  4060                 skipSignal handle:[:ex | ] 
  4021                     img := Image fromFile:(fileName pathName).
  4061                 do:[
  4022                     img notNil ifTrue:[
  4062                     self notify:('Converting:',  fileName baseName).
  4023                         tempFileXPM  := Filename newTemporary withSuffix:'xpm'.
  4063 
       
  4064                     fileName isDirectory ifFalse:[
       
  4065                         outFile := fileName withSuffix:outSuffix.
       
  4066                         outFile exists ifTrue:[
       
  4067                             (Dialog confirm:(resources string:'Overwrite existing %1 ?' with:outFile baseName allBold))
       
  4068                             ifFalse:[
       
  4069                                 skipSignal raise.
       
  4070                             ].
       
  4071                         ].
       
  4072 
       
  4073                         image := Image fromFile:fileName.
       
  4074                         image isNil ifTrue:[
       
  4075                             chainOfConversions := self conversionChainFrom:(fileName suffix) to:outSuffix.
       
  4076                         ] ifFalse:[
       
  4077                             writer := MIMETypes imageReaderForSuffix:outSuffix.
       
  4078                             (writer notNil and:[writer canRepresent:image]) ifTrue:[
       
  4079                                 writer save:image onFile:outFile.
       
  4080                                 skipSignal raise.
       
  4081                             ].
       
  4082                         ].
       
  4083 
       
  4084                         chainOfConversions isNil ifTrue:[ skipSignal raise ].
       
  4085                         conversionStream := chainOfConversions readStream.
       
  4086 
       
  4087                         doneWithThisFile := false.
       
  4088 
       
  4089                         tempFileTemplate  := Filename newTemporary.
       
  4090 
       
  4091                         inFile := fileName.
       
  4092                         eachConversionSuffixCommandPair := conversionStream next.
       
  4093                         eachConversionCommand := eachConversionSuffixCommandPair key.
       
  4094 
       
  4095                         eachConversionCommand == #readToXPM ifTrue:[
       
  4096                             image := Image fromFile:(inFile pathName).
       
  4097                             image isNil ifTrue:[
       
  4098                                 self warn:'Unknown format/not an image: ' , inFile baseName.
       
  4099                                 skipSignal raise.
       
  4100                             ] ifFalse:[
       
  4101                                 tempFileXPM  := tempFileTemplate withSuffix:'xpm'.
       
  4102                                 image saveOn:tempFileXPM using:XPMReader.
       
  4103                                 inFile := tempFileXPM.
       
  4104                             ].
       
  4105                             eachConversionSuffixCommandPair := conversionStream next.
       
  4106                         ].
  4024 
  4107 
  4025                         [
  4108                         [
  4026                             img saveOn:tempFileXPM using:XPMReader.
  4109                             |command|
  4027 
  4110 
  4028                             inFile := tempFileXPM.
  4111                             [eachConversionSuffixCommandPair notNil] whileTrue:[
  4029                             chainOfConversions do:[:eachConversionSuffixCommandPair |
  4112                                 eachConversionCommand := eachConversionSuffixCommandPair key.
  4030                                 |eachConversionSuffix eachConversionCommand|
  4113                                 eachConversionSuffix  := eachConversionSuffixCommandPair value.
  4031 
       
  4032                                 eachConversionSuffix := eachConversionSuffixCommandPair key.
       
  4033                                 eachConversionCommand := eachConversionSuffixCommandPair value.
       
  4034 
  4114 
  4035                                 self notify:('Converting to ',  eachConversionSuffix , '...').
  4115                                 self notify:('Converting to ',  eachConversionSuffix , '...').
  4036                                 outFile := Filename newTemporary withSuffix:eachConversionSuffix.
  4116                                 outFile := tempFileTemplate withSuffix:eachConversionSuffix.
  4037                                 (OperatingSystem 
  4117                                 command := '%1 %2 > %3' 
  4038                                         executeCommand:('%1 %2 > %3' 
  4118                                                 bindWith:eachConversionCommand
  4039                                                             bindWith:eachConversionCommand
  4119                                                 with:inFile pathName
  4040                                                             with:inFile pathName
  4120                                                 with:outFile pathName.
  4041                                                             with:outFile pathName))
  4121 
       
  4122                                 (OperatingSystem executeCommand:command)
  4042                                 ifFalse:[
  4123                                 ifFalse:[
  4043                                     inFile delete.
       
  4044                                     outFile delete.
       
  4045                                     self warn:('Conversion to %1 using %2 failed.' 
  4124                                     self warn:('Conversion to %1 using %2 failed.' 
  4046                                             bindWith:eachConversionSuffix
  4125                                             bindWith:eachConversionSuffix
  4047                                             with:eachConversionCommand allBold).
  4126                                             with:eachConversionCommand allBold).
  4048                                     ^ self.
  4127 
       
  4128                                     (inFile ~= fileName) ifTrue:[inFile delete].
       
  4129                                     outFile delete.
       
  4130                                     skipSignal raise
  4049                                 ].
  4131                                 ].
  4050                                 inFile delete.
  4132                                 (inFile ~= fileName) ifTrue:[inFile delete].
  4051                                 inFile := outFile.
  4133                                 inFile := outFile.
       
  4134                                 eachConversionSuffixCommandPair := conversionStream next
  4052                             ].
  4135                             ].
  4053                         ] ensure:[
  4136                         ] ifCurtailed:[
  4054                             inFile delete.
  4137                             (inFile ~= fileName) ifTrue:[inFile delete].
  4055                             outFile delete.
  4138                             outFile delete.
  4056                         ].
  4139                         ].
  4057 
  4140 
  4058                         outFile moveTo:(fileName withSuffix:(chainOfConversions last key)).
  4141                         outFile moveTo:(fileName withSuffix:(chainOfConversions last value)).
  4059                         self updateAndSelect:nil.
  4142                         self updateAndSelect:nil.
  4060                     ] ifFalse:[
       
  4061                         self warn:'Unknown format/not an image: ' , fileName asString
       
  4062                     ]
  4143                     ]
  4063                 ]
  4144                 ]
  4064             ].
  4145             ].
  4065             self notify:''.
  4146             self notify:''.
  4066         ]
  4147         ]
  4067     ] fork.
  4148 "/    ] fork.
       
  4149 !
       
  4150 
       
  4151 convertImageToXPM
       
  4152     self convertImageToSuffix:'xpm'
  4068 !
  4153 !
  4069 
  4154 
  4070 createProjectAndOpenProjectBrowser
  4155 createProjectAndOpenProjectBrowser
  4071     |nm f s directory|
  4156     |nm f s directory|
  4072 
  4157 
  5995 ! !
  6080 ! !
  5996 
  6081 
  5997 !AbstractFileBrowser class methodsFor:'documentation'!
  6082 !AbstractFileBrowser class methodsFor:'documentation'!
  5998 
  6083 
  5999 version
  6084 version
  6000     ^ '$Header: /cvs/stx/stx/libtool/AbstractFileBrowser.st,v 1.115 2003-03-21 17:53:34 cg Exp $'
  6085     ^ '$Header: /cvs/stx/stx/libtool/AbstractFileBrowser.st,v 1.116 2003-03-26 12:20:09 cg Exp $'
  6001 ! !
  6086 ! !