'From Squeak6.0alpha of 2 March 2021 [latest update: #20249] on 2 March 2021 at 6:24:28 pm'!

!CodeHolder methodsFor: 'commands' stamp: 'ct 3/2/2021 16:07'!
removeClass
	"Remove the selected class from the system, at interactive user request.  Make certain the user really wants to do this, since it is not reversible.  Answer true if removal actually happened."

	| classToRemove |
	self okToChange ifFalse: [^ false].
	classToRemove := self selectedClassOrMetaClass ifNil: [Beeper beep. ^ false].
	classToRemove := classToRemove theNonMetaClass.
	^ self systemNavigation confirmAndRemoveClass: classToRemove! !


!Browser methodsFor: 'class functions' stamp: 'ct 3/2/2021 16:21'!
removeClass
	"If the user confirms the wish to delete the class, do so"

	super removeClass ifFalse: [^ false].
	
	self classListIndex: 0.
	self changed: #classList.! !


!FileContentsBrowser methodsFor: 'removing' stamp: 'ct 3/2/2021 15:56'!
removeClass

	self hasClassSelected ifFalse: [^ false].
	
	super removeClass ifFalse: [^ false].
	self classListIndex: 0.
	self changed: #classList.! !


!SystemNavigation methodsFor: 'browse' stamp: 'ct 3/2/2021 16:24'!
browseAllReferencesToClass: aClass
	
	self browseAllCallsOnClass: aClass.
	aClass subclasses ifNotEmpty: [
		self browseHierarchy: aClass].! !

!SystemNavigation methodsFor: 'ui' stamp: 'ct 3/2/2021 16:57'!
confirmAndRemoveClass: aClass

	| allCalls subclasses hasForeignCalls browse className numberOfMethods choice remove |
	allCalls := self allCallsOnClass: aClass.
	subclasses := aClass subclasses.
	hasForeignCalls := allCalls anySatisfy: [:ea | ea actualClass ~= aClass].
	
	remove := true.
	browse := false.
	className := aClass name.
	numberOfMethods := aClass methodDict size + aClass class methodDict size.
	choice := UIManager default 
		chooseFromLabeledValues: (OrderedDictionary new in: [:choices |
			choices at: 'Remove it' translated put: [].
			(allCalls notEmpty or: [subclasses notEmpty]) ifTrue: [
				choices at: 'Remove, then browse references' translated put: [browse := true].
				choices at: 'Don''t remove, but show me those references' translated put: [remove := false. browse := true]].
			choices at: 'Forget it -- do nothing -- sorry I asked' translated put: [remove := false]]; yourself)
		title: ('Are you sure that you want to remove\the class\\	{1}{2} Maybe save your image before doing this.{3}' withCRs translated asText format: {
			className asText allBold.
			numberOfMethods > 0 ifFalse: ['?\\' withCRs] ifTrue: [
				'\\and all its {1} methods?' withCRs translated format: {numberOfMethods asString asText allBold}].
			(hasForeignCalls or: [subclasses notEmpty]) ifFalse: [''] ifTrue: [
				'\\(There {2} {1}.)' translated withCRs asText format: {
					(Array streamContents: [:refStream |
						allCalls ifNotEmpty: [refStream nextPut: (
							'{1} {2} to the class' translated withCRs format: {allCalls size asString asText allBold. ('reference' asPluralBasedOn: allCalls) translated})].
						subclasses ifNotEmpty: [refStream nextPut: (
							'{1} {2},\whose superclass must be updated manually after this' translated withCRs format: {subclasses size asString asText allBold. ('subclass' asPluralBasedOn: subclasses) translated})]]) asCommaStringAnd.
					({allCalls. subclasses} detectSum: #size) > 1 ifTrue: ['are' translated] ifFalse: ['is' translated]}]}).
	choice ifNil: [choice := [remove := false]].
	choice value.
	
	remove ifTrue: [
		aClass removeFromSystem].
	browse ifTrue: [
		hasForeignCalls ifTrue: [
			self "browseAllCallsOnClass: aClass"browseAllCallsOn: className].
		subclasses ifNotEmpty: [
			self
				browseMessageList: (subclasses collect: [:subclass | MethodReference class: subclass selector: #Definition environment: self environment])
				name: ('Subclasses of {1}' translated format: {className})
				autoSelect: className]].
	
	^ remove! !

