|
| 1 | +Class { |
| 2 | + #name : #PyramidShowCodePlugin, |
| 3 | + #superclass : #Object, |
| 4 | + #traits : 'TPyramidPlugin', |
| 5 | + #classTraits : 'TPyramidPlugin classTrait', |
| 6 | + #instVars : [ |
| 7 | + 'codePresenterForAll', |
| 8 | + 'codePresenterForSelection', |
| 9 | + 'projectModel' |
| 10 | + ], |
| 11 | + #category : #'Pyramid-Bloc-plugin-showcode' |
| 12 | +} |
| 13 | + |
| 14 | +{ #category : #adding } |
| 15 | +PyramidShowCodePlugin >> addPanelsOn: aPyramidSimpleWindow [ |
| 16 | + |
| 17 | + aPyramidSimpleWindow at: #tabLeft addItem: [ :builder | |
| 18 | + builder |
| 19 | + makeTab: self codePresenterForAll |
| 20 | + label: 'Code for all' |
| 21 | + icon: (Smalltalk ui icons iconNamed: #page) |
| 22 | + order: 5 ]. |
| 23 | + aPyramidSimpleWindow at: #tabRight addItem: [ :builder | |
| 24 | + builder |
| 25 | + makeTab: self codePresenterForSelection |
| 26 | + label: 'Code for selection' |
| 27 | + icon: (Smalltalk ui icons iconNamed: #page) |
| 28 | + order: 999 ] |
| 29 | +] |
| 30 | + |
| 31 | +{ #category : #accessing } |
| 32 | +PyramidShowCodePlugin >> codePresenterForAll [ |
| 33 | + |
| 34 | + ^ codePresenterForAll |
| 35 | +] |
| 36 | + |
| 37 | +{ #category : #accessing } |
| 38 | +PyramidShowCodePlugin >> codePresenterForSelection [ |
| 39 | + |
| 40 | + ^ codePresenterForSelection |
| 41 | +] |
| 42 | + |
| 43 | +{ #category : #connecting } |
| 44 | +PyramidShowCodePlugin >> connectOn: aPyramidEditor [ |
| 45 | + |
| 46 | + projectModel := aPyramidEditor projectModel. |
| 47 | + aPyramidEditor projectModel announcer |
| 48 | + when: PyramidElementsChangedEvent |
| 49 | + do: [ :evt | self elementsChanged: evt ] |
| 50 | + for: self. |
| 51 | + aPyramidEditor projectModel announcer |
| 52 | + when: PyramidFirstLevelElementsChangedEvent |
| 53 | + do: [ :evt | self firstLevelElementsChanged: evt ] |
| 54 | + for: self. |
| 55 | + aPyramidEditor projectModel announcer |
| 56 | + when: PyramidSelectionChangedEvent |
| 57 | + do: [ :evt | self selectionChanged: evt ] |
| 58 | + for: self |
| 59 | +] |
| 60 | + |
| 61 | +{ #category : #'as yet unclassified' } |
| 62 | +PyramidShowCodePlugin >> elementsChanged: aPyramidElementsChangedEvent [ |
| 63 | + |
| 64 | + self firstLevelElementsChanged: aPyramidElementsChangedEvent. |
| 65 | + self selectionChanged: aPyramidElementsChangedEvent |
| 66 | +] |
| 67 | + |
| 68 | +{ #category : #'as yet unclassified' } |
| 69 | +PyramidShowCodePlugin >> firstLevelElementsChanged: aPyramidFirstLevelElementsChangedEvent [ |
| 70 | + |
| 71 | + self codePresenterForAll text: (Stash new serialize: |
| 72 | + aPyramidFirstLevelElementsChangedEvent firstLevelElements) |
| 73 | +] |
| 74 | + |
| 75 | +{ #category : #initialization } |
| 76 | +PyramidShowCodePlugin >> initialize [ |
| 77 | + |
| 78 | + super initialize. |
| 79 | + codePresenterForAll := SpCodePresenter new |
| 80 | + beForScripting; |
| 81 | + whenSubmitDo: [ :text | |
| 82 | + self validateCodeForAll: text ]; |
| 83 | + yourself. |
| 84 | + codePresenterForSelection := SpCodePresenter new |
| 85 | + beForScripting; |
| 86 | + whenSubmitDo: [ :text | |
| 87 | + self validateCodeForSelection: text ]; |
| 88 | + yourself |
| 89 | +] |
| 90 | + |
| 91 | +{ #category : #accessing } |
| 92 | +PyramidShowCodePlugin >> projectModel [ |
| 93 | + |
| 94 | + ^ projectModel |
| 95 | +] |
| 96 | + |
| 97 | +{ #category : #'event handling' } |
| 98 | +PyramidShowCodePlugin >> selectionChanged: aPyramidSelectionChangedEvent [ |
| 99 | + |
| 100 | + self codePresenterForSelection text: ''. |
| 101 | + aPyramidSelectionChangedEvent selection ifEmpty: [ |
| 102 | + self codePresenterForSelection text: '"Select one element."'. |
| 103 | + ^ self ]. |
| 104 | + aPyramidSelectionChangedEvent selection size > 2 ifTrue: [ |
| 105 | + self codePresenterForSelection text: '"Select only one element."'. |
| 106 | + ^ self ]. |
| 107 | + self codePresenterForSelection text: (Stash new serialize: |
| 108 | + aPyramidSelectionChangedEvent selection first) |
| 109 | +] |
| 110 | + |
| 111 | +{ #category : #'as yet unclassified' } |
| 112 | +PyramidShowCodePlugin >> validateCodeForAll: aString [ |
| 113 | + |
| 114 | + | collection | |
| 115 | + collection := Stash new materialize: aString. |
| 116 | + collection isCollection ifFalse: [ |
| 117 | + ^ self inform: 'Could not serialize: The code is not a collection.' ]. |
| 118 | + (collection allSatisfy: [ :each | each isKindOf: BlElement ]) |
| 119 | + ifFalse: [ |
| 120 | + ^ self inform: |
| 121 | + 'Could not serialize: Not BlElement found in the code.' ]. |
| 122 | + self projectModel firstLevelElements replaceAll: collection. |
| 123 | + self projectModel updateSelection |
| 124 | +] |
| 125 | + |
| 126 | +{ #category : #'as yet unclassified' } |
| 127 | +PyramidShowCodePlugin >> validateCodeForSelection: aString [ |
| 128 | + |
| 129 | + | newElement currentSelection | |
| 130 | + newElement := Stash new materialize: aString. |
| 131 | + (newElement isKindOf: BlElement) ifFalse: [ |
| 132 | + ^ self inform: 'Could not serialize: The code is not a BlElement.' ]. |
| 133 | + self projectModel selection size = 1 ifFalse: [ |
| 134 | + ^ self inform: |
| 135 | + 'Could not serialize: The selection should only contains one element.' ]. |
| 136 | + currentSelection := self projectModel selection first. |
| 137 | + (self projectModel firstLevelElements includes: currentSelection) |
| 138 | + ifTrue: [ |
| 139 | + self projectModel firstLevelElements replaceAll: |
| 140 | + (self projectModel firstLevelElements asArray |
| 141 | + copyReplaceAll: { currentSelection } |
| 142 | + with: { newElement }) ]. |
| 143 | + currentSelection parent ifNotNil: [ :p | |
| 144 | + p replaceChild: currentSelection with: newElement ]. |
| 145 | + |
| 146 | + self projectModel selection replaceAll: { newElement } |
| 147 | +] |
0 commit comments