DropObject.st
author Claus Gittinger <cg@exept.de>
Thu, 09 Dec 1999 12:15:01 +0100
changeset 1292 632e22649e4b
parent 1277 ad8a630c9acd
child 1482 e2cbc5342630
permissions -rw-r--r--
*** empty log message ***

"
 COPYRIGHT (c) 1997 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"



Object subclass:#DropObject
	instanceVariableNames:'theObject displayObject'
	classVariableNames:''
	poolDictionaries:''
	category:'Interface-DragAndDrop'
!

DropObject class instanceVariableNames:'DisplayObject'

"
 The following class instance variables are inherited by this class:

	Object - 
"
!

DropObject subclass:#File
	instanceVariableNames:'info isHtmlFile isImageFile isPrintable'
	classVariableNames:''
	poolDictionaries:''
	privateIn:DropObject
!

DropObject subclass:#Text
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	privateIn:DropObject
!

DropObject subclass:#Color
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	privateIn:DropObject
!

DropObject subclass:#Image
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	privateIn:DropObject
!

!DropObject class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1997 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"


!

documentation
"
    instances of myself and subclasses wrap objects which are being
    dragged (and dropped).
    Any object can be dragged, but for some often encountered types,
    specialized subclasses are provided, which react to certain queries.

    [see also:]
        DragAndDropManager

    [author:]
        Claus Atzkern
"
! !

!DropObject class methodsFor:'instance creation'!

dropObjectClassFor:someThing
    "given something to drag&drop, return an appropriate
     dropObject class for it."

    someThing isColor             ifTrue:[^ DropObject::Color].
    someThing isImageOrForm       ifTrue:[^ DropObject::Image].
    (someThing isKindOf:Filename) ifTrue:[^ DropObject::File].
    someThing isString            ifTrue:[^ DropObject::Text].
    ^ self
!

new:someThing
    "create an instance on something
    "
    |cls|

    cls := self dropObjectClassFor:someThing.
    ^ cls new theObject:someThing

    "Modified: 19.4.1997 / 17:19:55 / cg"
!

newColor:aColor
    "create an instance for a color"

    ^ DropObject::Color new theObject:aColor

    "Modified: 19.4.1997 / 17:20:17 / cg"
!

newFile:aFilename
    "create an instance for a file or directory"

    ^ DropObject::File new theObject:aFilename

    "Modified: 19.4.1997 / 17:20:28 / cg"
!

newImage:anImageOrForm
    "create an instance for an image"

    ^ DropObject::Image new theObject:anImageOrForm

    "Modified: 19.4.1997 / 17:20:37 / cg"
!

newText:aTextOrString
    "create an instance for some text"

    ^ DropObject::Text new theObject:aTextOrString

    "Modified: 19.4.1997 / 17:20:46 / cg"
! !

!DropObject class methodsFor:'defaults'!

displayObject
    "return some object which is shown while dragging;
     here an image based upon my displayObjectName is returned.
     Either this method or #displayObjectName is usually redefined
     in subclasses"

    DisplayObject notNil ifTrue:[
        ^ DisplayObject
    ].
    DisplayObject := Smalltalk imageFromFileNamed:(self displayObjectName) forClass:self.
    ^ DisplayObject

    "Modified: 19.4.1997 / 13:04:05 / cg"
!

displayObject:aDisplayObject
    "set the object which is shown while dragging;
     this object must understand #displayOn:x:y: (i.e. the displayObject
     protocol). The object is kept per class for ALL future drag operations.
     Useful to change the default images in subclasses."

    DisplayObject := aDisplayObject

    "Modified: 19.4.1997 / 15:42:10 / cg"
!

displayObjectName
    "return the name of a bitmap image file, which is used 
     during a drag.
     Here, a dummy file is returned."

    ^ 'bitmaps/xpmBitmaps/plain_pixmaps/balloon3.xpm'

    "Modified: 19.4.1997 / 13:54:13 / cg"
! !

!DropObject methodsFor:'accessing'!

displayObject
    "return my graphical representation - here a default is returned"

    ^ displayObject ? (self class displayObject)


!

displayObject:anObject
    "set my graphical representation"

    displayObject := anObject

!

theObject
    "return the real object represented by the receiver"

    ^ theObject

    "Modified: 19.4.1997 / 12:53:01 / cg"
!

theObject:something
    "set the real object represented by the receiver"

    theObject := something.

    "Modified: 19.4.1997 / 12:53:10 / cg"
! !

!DropObject methodsFor:'testing'!

isColorObject
    "return true, if the dropObject represents a color"

    ^ false

    "Modified: 19.4.1997 / 12:52:36 / cg"
!

isFileObject
    "return true, if the dropObject represents some file or directory"

    ^ false

    "Modified: 19.4.1997 / 12:52:29 / cg"
!

isImageObject
    "return true, if the dropObject represents some image"

    ^ false

    "Modified: 19.4.1997 / 12:52:22 / cg"
!

isTextObject
    "return true, if the dropObject represents some text"

    ^ false

    "Modified: 19.4.1997 / 12:52:17 / cg"
! !

!DropObject::File class methodsFor:'defaults'!

displayObjectName
    "return the name of a bitmap image file, which is used 
     during a drag."

    ^ 'bitmaps/xpmBitmaps/document_images/xfm_file.xpm'

    "Modified: 19.4.1997 / 15:38:07 / cg"
! !

!DropObject::File methodsFor:'accessing'!

theObject:aPathname
    |f path|

    f := aPathname asFilename.
    path := f pathName.
    info := f info.

    super theObject:f

    "Modified: 19.4.1997 / 12:49:17 / cg"
! !

!DropObject::File methodsFor:'queries'!

exists
    "returns true if the file or directory exists
    "
    ^ info notNil

    "Modified: 19.4.1997 / 12:49:30 / cg"
!

isDirectory
    "checks whether file is a directory
    "
    ^ (info notNil and:[info type == #directory])
!

isHtmlFile
    "checks whether file is an html file
    "
    |suffixes pathName|

    isHtmlFile isNil ifTrue:[
        (info isNil or:[self isDirectory]) ifTrue:[
            isHtmlFile := false
        ] ifFalse:[
            pathName   := theObject asString.
            suffixes   := #( '.html' '.htm' '.HTML' '.HTM' ).
            isHtmlFile := (suffixes findFirst:[:el|pathName endsWith:el]) ~~ 0
        ]
    ].
    ^ isHtmlFile

    "Modified: 19.4.1997 / 12:49:37 / cg"
!

isImageFile
    "returns true if file is an image file
    "
    |pathName|

    isImageFile isNil ifTrue:[
        (info isNil or:[self isDirectory]) ifTrue:[
            isImageFile := false
        ] ifFalse:[
            pathName    := theObject asString.
            isImageFile := Image isImageFileSuffix:(pathName asFilename suffix).
        ]
    ].
    ^ isImageFile

    "Modified: 19.4.1997 / 12:50:58 / cg"
!

isPrintable
    "returns false if file contains non printable characters
    "
    |stream buff size|

    isPrintable isNil ifTrue:[
        isPrintable := false.

        (info isNil or:[self isDirectory]) ifFalse:[
            stream := FileStream readonlyFileNamed:(theObject pathName).

            stream isNil ifFalse:[
                buff := String new:300.
                size := stream nextBytes:300 into:buff.
                stream close.
            ].
            1 to:size do:[:i|
                (buff at:i) isPrintable ifFalse:[
                    ^ isPrintable
                ]
            ].
            isPrintable := true
        ]
    ].
    ^ isPrintable
! !

!DropObject::File methodsFor:'testing'!

isFileObject
    "return true, if the dropObject represents a file- or directory"

    ^ true

    "Modified: 19.4.1997 / 12:51:49 / cg"
! !

!DropObject::Text class methodsFor:'defaults'!

displayObjectName
    ^ 'bitmaps/xpmBitmaps/document_images/yellow_file_text_grab.xpm'

    "Modified: 19.4.1997 / 15:40:33 / cg"
! !

!DropObject::Text methodsFor:'testing'!

isTextObject
    "return true, if the dropObject represents some text"

    ^ true

    "Modified: 19.4.1997 / 12:52:08 / cg"
! !

!DropObject::Color class methodsFor:'defaults'!

displayObjectName
    "return the name of a bitmap image file, which is used 
     during a drag."

    ^ 'bitmaps/xpmBitmaps/misc_tools/color_wheel.xpm'

    "Modified: 19.4.1997 / 15:38:12 / cg"
! !

!DropObject::Color methodsFor:'testing'!

isColorObject
    "return true, if the dropObject represents a color"

    ^ true

    "Modified: 19.4.1997 / 12:51:57 / cg"
! !

!DropObject::Image class methodsFor:'defaults'!

displayObjectName
    "return the name of a bitmap image file, which is used 
     during a drag."

    ^ 'bitmaps/xpmBitmaps/misc_tools/picture.xpm'

    "Modified: 19.4.1997 / 15:38:02 / cg"
! !

!DropObject::Image methodsFor:'testing'!

isImageObject
    "return true, if the dropObject represents an image"

    ^ true

    "Modified: 19.4.1997 / 12:51:28 / cg"
! !

!DropObject class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libview2/DropObject.st,v 1.10 1999-12-09 11:15:01 cg Exp $'
! !