1585
|
1 |
"{ Package: 'stx:libtool2' }"
|
|
2 |
|
|
3 |
FileOperations subclass:#Rename
|
|
4 |
instanceVariableNames:'renamedFiles'
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'Interface-Support'
|
|
8 |
!
|
|
9 |
|
|
10 |
|
|
11 |
!Rename class methodsFor:'actions'!
|
|
12 |
|
1588
|
13 |
renameFile:oldFile to:newName
|
1585
|
14 |
"move from to
|
|
15 |
"
|
|
16 |
|
|
17 |
|instance|
|
|
18 |
|
|
19 |
instance := self new.
|
1588
|
20 |
instance renameFile:oldFile to:newName.
|
1585
|
21 |
^ instance
|
|
22 |
!
|
|
23 |
|
1588
|
24 |
renameFiles:aColOfFiles
|
1585
|
25 |
"move from to
|
|
26 |
"
|
|
27 |
|
|
28 |
|instance|
|
|
29 |
|
|
30 |
instance := self new.
|
1588
|
31 |
instance renameFiles:aColOfFiles.
|
1585
|
32 |
^ instance
|
|
33 |
! !
|
|
34 |
|
|
35 |
!Rename class methodsFor:'defaults'!
|
|
36 |
|
|
37 |
goodRenameDefaultFor:oldName lastOld:lastOldName lastNew:lastNewName
|
|
38 |
|prefix suffix lastNewSize lastOldSize t
|
|
39 |
lastOldWOSuffix lastNewWOSuffix oldWOSuffix lastOldRest oldRest lastNewRest
|
|
40 |
lastRemoved lastInserted|
|
|
41 |
|
|
42 |
lastNewName isNil ifTrue:[ ^ nil].
|
|
43 |
|
|
44 |
lastNewSize := lastNewName size.
|
|
45 |
lastOldSize := lastOldName size.
|
|
46 |
|
|
47 |
"/ intelligent default ...
|
|
48 |
(lastNewName endsWith:lastOldName) ifTrue:[
|
|
49 |
prefix := lastNewName copyTo:(lastNewSize - lastOldSize).
|
|
50 |
^ (prefix , oldName).
|
|
51 |
].
|
|
52 |
(lastOldName endsWith:lastNewName) ifTrue:[
|
|
53 |
prefix := lastOldName copyTo:(lastOldSize - lastNewSize).
|
|
54 |
^ (oldName copyFrom:prefix size+1).
|
|
55 |
].
|
|
56 |
(lastOldName withoutSeparators = lastNewName) ifTrue:[
|
|
57 |
"/ intelligent default ...
|
|
58 |
^ oldName withoutSeparators.
|
|
59 |
].
|
|
60 |
(lastNewName startsWith:lastOldName) ifTrue:[
|
|
61 |
"/ intelligent default ...
|
|
62 |
suffix := lastNewName copyLast:(lastNewSize - lastOldSize).
|
|
63 |
^ (oldName , suffix).
|
|
64 |
].
|
|
65 |
(lastOldName startsWith:lastNewName) ifTrue:[
|
|
66 |
suffix := lastOldName copyLast:(lastOldSize - lastNewSize).
|
|
67 |
(oldName endsWith:suffix) ifTrue:[
|
|
68 |
^ (oldName copyWithoutLast:suffix size).
|
|
69 |
]
|
|
70 |
].
|
|
71 |
|
|
72 |
lastOldWOSuffix := lastOldName asFilename withoutSuffix name.
|
|
73 |
lastNewWOSuffix := lastNewName asFilename withoutSuffix name.
|
|
74 |
oldWOSuffix := oldName asFilename withoutSuffix name.
|
|
75 |
|
|
76 |
prefix := lastOldWOSuffix commonPrefixWith:oldWOSuffix.
|
|
77 |
(lastNewWOSuffix startsWith:prefix) ifTrue:[
|
|
78 |
lastOldRest := lastOldWOSuffix copyFrom:prefix size + 1.
|
|
79 |
lastNewRest := lastNewWOSuffix copyFrom:prefix size + 1.
|
|
80 |
oldRest := oldWOSuffix copyFrom:prefix size + 1.
|
|
81 |
|
|
82 |
(lastNewRest endsWith:lastOldRest) ifTrue:[
|
|
83 |
t := lastNewRest copyWithoutLast:lastOldRest size.
|
|
84 |
^ ((prefix , t , oldRest) asFilename withSuffix:oldName asFilename suffix) name
|
|
85 |
].
|
|
86 |
].
|
|
87 |
|
|
88 |
suffix := lastOldWOSuffix commonSuffixWith:lastNewWOSuffix.
|
|
89 |
suffix size > 0 ifTrue:[
|
|
90 |
"/ last change changed something at the beginning
|
|
91 |
prefix := oldWOSuffix commonPrefixWith:lastOldWOSuffix.
|
|
92 |
prefix size > 0 ifTrue:[
|
|
93 |
"/ this name starts with the same characters
|
|
94 |
lastRemoved := lastOldWOSuffix copyWithoutLast:suffix size.
|
|
95 |
lastInserted := lastNewWOSuffix copyWithoutLast:suffix size.
|
|
96 |
^ lastInserted , (oldName copyFrom:lastRemoved size + 1)
|
|
97 |
].
|
|
98 |
].
|
|
99 |
|
|
100 |
^ nil
|
|
101 |
! !
|
|
102 |
|
|
103 |
!Rename methodsFor:'accessing'!
|
|
104 |
|
|
105 |
renamedFiles
|
|
106 |
"return the value of the instance variable 'lastRenamedFile' (automatically generated)"
|
|
107 |
|
|
108 |
renamedFiles isNil ifTrue:[
|
|
109 |
renamedFiles := OrderedCollection new.
|
|
110 |
].
|
|
111 |
^ renamedFiles
|
|
112 |
! !
|
|
113 |
|
|
114 |
!Rename methodsFor:'actions'!
|
|
115 |
|
1588
|
116 |
renameFile:oldFile to:newName
|
1585
|
117 |
"rename a file (or directory)"
|
|
118 |
|
|
119 |
|old new msg|
|
|
120 |
|
|
121 |
(oldFile isNil or:[newName isNil]) ifTrue:[
|
|
122 |
result := false
|
|
123 |
].
|
|
124 |
(oldFile asString isBlank or:[newName isBlank]) ifTrue:[
|
|
125 |
result := false
|
|
126 |
].
|
|
127 |
(oldFile baseName = newName) ifTrue:[
|
|
128 |
result := false
|
|
129 |
].
|
|
130 |
|
|
131 |
old := oldFile.
|
|
132 |
new := oldFile directory construct:newName.
|
|
133 |
|
|
134 |
OperatingSystem errorSignal handle:[:ex|
|
|
135 |
msg := ('Cannot rename file ', old baseName,' to ', newName,' !!'), '\\(' ,(OperatingSystem lastErrorString) , ')'.
|
|
136 |
DialogBox warn:msg withCRs.
|
|
137 |
result := false
|
|
138 |
] do:[
|
|
139 |
new exists ifTrue:[
|
|
140 |
(DialogBox confirm:(new baseName allBold, ' already exists - rename (i.e. overwrite) anyway ?'))
|
|
141 |
ifFalse:[
|
|
142 |
result := false.
|
|
143 |
^ self.
|
|
144 |
]
|
|
145 |
].
|
|
146 |
old renameTo:new.
|
|
147 |
self renamedFiles add:new.
|
|
148 |
].
|
|
149 |
result := true.
|
1588
|
150 |
!
|
|
151 |
|
|
152 |
renameFiles:aColOfFiles
|
|
153 |
"rename the selected file(s)"
|
|
154 |
|
|
155 |
|queryBox b lastNewName lastOldName initialText oldName|
|
|
156 |
|
|
157 |
queryBox := FilenameEnterBox new.
|
|
158 |
queryBox okText:'Rename'.
|
|
159 |
aColOfFiles size > 1 ifTrue:[
|
|
160 |
b := queryBox addAbortButtonLabelled:'Cancel All'.
|
|
161 |
b action:[^ self]
|
|
162 |
].
|
|
163 |
aColOfFiles do:[:oldFile |
|
|
164 |
oldName := oldFile baseName asString.
|
|
165 |
queryBox title:('Rename ' , oldName , ' to:').
|
|
166 |
lastNewName notNil ifTrue:[
|
|
167 |
initialText := self class
|
|
168 |
goodRenameDefaultFor:oldName
|
|
169 |
lastOld:lastOldName
|
|
170 |
lastNew:lastNewName
|
|
171 |
].
|
|
172 |
initialText notNil ifTrue:[
|
|
173 |
queryBox initialText:initialText
|
|
174 |
] ifFalse:[
|
|
175 |
queryBox initialText:oldName
|
|
176 |
].
|
|
177 |
queryBox
|
|
178 |
action:[:newName |
|
|
179 |
(self renameFile:oldFile to:newName asString) ifTrue:[
|
|
180 |
result := true
|
|
181 |
].
|
|
182 |
lastOldName := oldName.
|
|
183 |
lastNewName := newName
|
|
184 |
].
|
|
185 |
queryBox showAtPointer
|
|
186 |
]
|
1585
|
187 |
! !
|
|
188 |
|
|
189 |
!Rename class methodsFor:'documentation'!
|
|
190 |
|
|
191 |
version
|
|
192 |
^ '$Header$'
|
|
193 |
! !
|