FilenameEditField.st
changeset 72 f17df5ea35ed
parent 71 9f9243f5813b
child 75 903e595b4cec
equal deleted inserted replaced
71:9f9243f5813b 72:f17df5ea35ed
     9  other person.  No title to or ownership of the software is
     9  other person.  No title to or ownership of the software is
    10  hereby transferred.
    10  hereby transferred.
    11 "
    11 "
    12 
    12 
    13 EditField subclass:#FilenameEditField 
    13 EditField subclass:#FilenameEditField 
    14        instanceVariableNames:'directoriesOnly filesOnly'
    14        instanceVariableNames:'directoriesOnly filesOnly directory'
    15        classVariableNames:   ''
    15        classVariableNames:   ''
    16        poolDictionaries:''
    16        poolDictionaries:''
    17        category:'Views-Text'
    17        category:'Views-Text'
    18 !
    18 !
    19 
    19 
    33 "
    33 "
    34 !
    34 !
    35 
    35 
    36 version
    36 version
    37 "
    37 "
    38 $Header: /cvs/stx/stx/libwidg2/FilenameEditField.st,v 1.13 1995-08-30 18:06:13 claus Exp $
    38 $Header: /cvs/stx/stx/libwidg2/FilenameEditField.st,v 1.14 1995-09-07 12:47:59 claus Exp $
    39 "
    39 "
    40 !
    40 !
    41 
    41 
    42 documentation
    42 documentation
    43 "
    43 "
    46     Filename completion ignores regular files if directoriesOnly is true,
    46     Filename completion ignores regular files if directoriesOnly is true,
    47     and ignores directories, if filesOnly is true. Both default to false.
    47     and ignores directories, if filesOnly is true. Both default to false.
    48 "
    48 "
    49 ! !
    49 ! !
    50 
    50 
    51 !FilenameEditField methodsFor:'initialization'!
    51 !FilenameEditField class methodsFor:'defaults'!
    52 
    52 
    53 initialize
    53 filenameCompletionBlock 
    54     super initialize.
    54     "return a block which can be used for fileName completion.
    55     directoriesOnly := filesOnly := false.
    55      This has been extracted into a separate method, to allow reuse of
    56 
    56      this code (for example, for use with regular editFields ..)"
    57     entryCompletionBlock := [
    57 
       
    58     ^ [:field :inDirectory :directoriesOnly :filesOnly |
    58 	|s f matchSet nMatch name words dir|
    59 	|s f matchSet nMatch name words dir|
    59 
    60 
    60 	s := self contents.
    61 	s := field contents.
    61 	"
    62 	"
    62 	 find the last word ...
    63 	 find the last word ...
    63 	"
    64 	"
    64 	words := s asCollectionOfWords.
    65 	words := s asCollectionOfWords.
    65 	f := words last asFilename.
    66 	f := words last asFilename.
       
    67 	f isAbsolute ifFalse:[
       
    68 	     inDirectory asString ~= '.' ifTrue:[
       
    69 		 f := (inDirectory name asFilename construct:f) name asFilename
       
    70 	     ].
       
    71 	].
       
    72 	f isAbsolute ifTrue:[
       
    73 	    f := f pathName asFilename.
       
    74 	].
       
    75 
    66 	matchSet := f filenameCompletion.
    76 	matchSet := f filenameCompletion.
    67 	dir := f directory.
    77 	dir := f directory.
       
    78 
    68 	directoriesOnly ifTrue:[
    79 	directoriesOnly ifTrue:[
    69 	    matchSet := matchSet select:[:aFilename |
    80 	    matchSet := matchSet select:[:aFilename |
    70 		(dir construct:aFilename) isDirectory
    81 		(dir construct:aFilename) isDirectory
    71 	    ].
    82 	    ].
    72 	] ifFalse:[
    83 	] ifFalse:[
    79 
    90 
    80 	(nMatch := matchSet size) ~~ 1 ifTrue:[
    91 	(nMatch := matchSet size) ~~ 1 ifTrue:[
    81 	    "
    92 	    "
    82 	     more than one possible completion -
    93 	     more than one possible completion -
    83 	    "
    94 	    "
    84 	    self changed:#directory with:f directoryName.
    95 	    field changed:#directory with:f directoryName.
    85 	    device beep
    96 	    field flash.
       
    97 	    field device beep.
    86 	].
    98 	].
    87 	"
    99 	"
    88 	 even with more than one possible completion,
   100 	 even with more than one possible completion,
    89 	 f's name is now common prefix
   101 	 f's name is now common prefix
    90 	"
   102 	"
    91 	name := f asString.
   103 	name := f asString.
    92 	nMatch == 1 ifTrue:[
   104 	nMatch == 1 ifTrue:[
    93 	    "
   105 	    "
    94 	     exactly one possible completion -
   106 	     exactly one possible completion -
    95 	    "
   107 	    "
    96 	    f := dir construct:matchSet first.
   108 "/            f := dir construct:matchSet first.
       
   109 	    f := matchSet first asFilename.
    97 
   110 
    98 	    directoriesOnly ifTrue:[
   111 	    directoriesOnly ifTrue:[
    99 		name := f asString
   112 		name := f asString
   100 	    ] ifFalse:[
   113 	    ] ifFalse:[
   101 		f isDirectory ifTrue:[
   114 		f isDirectory ifTrue:[
   102 		    (name endsWith:(Filename separator)) ifFalse:[
   115 		    (name endsWith:(Filename separator)) ifFalse:[
   103 			name := (f construct:'') asString
   116 			name := f asString , '/'
   104 		    ].
   117 		    ].
   105 		].
   118 		].
   106 	    ]
   119 	    ]
   107 	].
   120 	].
   108 	"
   121 	"
   112 	s := ''.
   125 	s := ''.
   113 	1 to:(words size - 1) do:[:idx |
   126 	1 to:(words size - 1) do:[:idx |
   114 	    s := s , (words at:idx) , ' '
   127 	    s := s , (words at:idx) , ' '
   115 	].
   128 	].
   116 	s := s , name.
   129 	s := s , name.
   117 	self contents:s.
   130 	field contents:s.
   118 	self cursorToEndOfLine.
   131 	field cursorToEndOfLine.
   119     ].
   132     ].
       
   133 ! !
       
   134 
       
   135 !FilenameEditField methodsFor:'initialization'!
       
   136 
       
   137 initialize
       
   138     super initialize.
       
   139     directoriesOnly := filesOnly := false.
       
   140     directory := '.' asFilename.
       
   141 
       
   142     entryCompletionBlock := [
       
   143 	self class 
       
   144 	    filenameCompletionBlock 
       
   145 		value:self 
       
   146 		value:directory
       
   147 		value:directoriesOnly
       
   148 		value:filesOnly
       
   149     ].
       
   150 
       
   151     "Modified: 7.9.1995 / 10:20:46 / claus"
   120 !
   152 !
   121 
   153 
   122 realize
   154 realize
   123     "move the cursor to the end - thats the most interresting part of
   155     "move the cursor to the end - thats the most interresting part of
   124      a filename
   156      a filename
   126     super realize.
   158     super realize.
   127     self cursorToEndOfLine.
   159     self cursorToEndOfLine.
   128 ! !
   160 ! !
   129 
   161 
   130 !FilenameEditField methodsFor:'accessing'!
   162 !FilenameEditField methodsFor:'accessing'!
       
   163 
       
   164 directory
       
   165     ^ directory
       
   166 
       
   167     "Modified: 7.9.1995 / 10:12:40 / claus"
       
   168 !
       
   169 
       
   170 directory:aFilename
       
   171     directory := aFilename asFilename
       
   172 
       
   173     "Modified: 7.9.1995 / 10:12:55 / claus"
       
   174 !
       
   175 
       
   176 showsDirectoriesOnly
       
   177     "return if expanding names for directories only"
       
   178 
       
   179     ^ directoriesOnly
       
   180 
       
   181     "Modified: 6.9.1995 / 20:35:30 / claus"
       
   182 !
       
   183 
       
   184 showsFilesOnly
       
   185     "return if expanding names for files only"
       
   186 
       
   187     ^ filesOnly
       
   188 
       
   189     "Modified: 6.9.1995 / 20:34:57 / claus"
       
   190 !
   131 
   191 
   132 directoriesOnly
   192 directoriesOnly
   133     "set to expand names for directories only"
   193     "set to expand names for directories only"
   134 
   194 
   135     directoriesOnly := true.
   195     directoriesOnly := true.