Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a simple code file editor so that we can use it for the preferences. #1612

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions src/Spec2-CommonWidgets/SpSimpleFileEditPresenter.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
"
I define a simple code editor.
Before saving I create a backup of the edited file.


Here is a typical use:

```
SpSimpleFileEditPresenter new
onFileReference: '/Library/Preferences/pharo/6.0/settings.st' asFileReference;
openDialog
```
"
Class {
#name : 'SpSimpleFileEditPresenter',
#superclass : 'SpPresenter',
#instVars : [
'label',
'fileReference',
'codeText'
],
#category : 'Spec2-CommonWidgets',
#package : 'Spec2-CommonWidgets'
}

{ #category : 'examples' }
SpSimpleFileEditPresenter class >> example [
<script>

| presenter fm |
fm := FileSystem memory / 'unknow.st'.
fm writeStreamDo: [ :each | each nextPutAll: 'Please edit me and save me!' ].
presenter := self new.
presenter
label: 'The meaning of life?';
onFileReference: fm ;
openDialog
]

{ #category : 'layout' }
SpSimpleFileEditPresenter >> backupFileReference [

| basename nameWithoutExtension |
basename := fileReference basename.
nameWithoutExtension := fileReference basenameWithoutExtension.
^ fileReference parent / (nameWithoutExtension, 'Old.txt')
]

{ #category : 'layout' }
SpSimpleFileEditPresenter >> calculateLabelHeight [

^ 16
]

{ #category : 'layout' }
SpSimpleFileEditPresenter >> defaultLayout [

^ SpBoxLayout newTopToBottom
spacing: 5;
add: (SpBoxLayout newLeftToRight
add: label expand: true;
yourself)
height: self calculateLabelHeight;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is not recommanded to hardcode the height in Spec.
Using expand: false on the TopToBottom layout should be enough.

^ SpBoxLayout newTopToBottom
		spacing: 5;
	  	add: (SpBoxLayout newLeftToRight
				add: label expand: true;
				yourself)
	  		expand: false;
	add: codeText;
	yourself

add: codeText expand: true;
yourself
]

{ #category : 'initialization' }
SpSimpleFileEditPresenter >> initializeDialogWindow: aDialog [

super initializeDialogWindow: aDialog.
aDialog
okAction: [ :presenter | self save ];
cancelAction: [ :presenter | self inform: 'Did not save' ]
]

{ #category : 'layout' }
SpSimpleFileEditPresenter >> initializePresenters [

label := self newLabel.
codeText := self newCode.
codeText takeKeyboardFocus.
]

{ #category : 'layout' }
SpSimpleFileEditPresenter >> initializeWindow: aWindowPresenter [

aWindowPresenter
title: 'File edit';
initialExtent: 600@600
]

{ #category : 'accessing' }
SpSimpleFileEditPresenter >> label [

^ label
]

{ #category : 'accessing' }
SpSimpleFileEditPresenter >> label: anObject [

label := anObject
]

{ #category : 'layout' }
SpSimpleFileEditPresenter >> onFileReference: aFileReference [

fileReference := aFileReference.
codeText text: aFileReference contents.
label label: 'File: ...', (aFileReference fullName last: 45)
]

{ #category : 'layout' }
SpSimpleFileEditPresenter >> save [

self backupFileReference ensureDelete.
self saveBackup.
fileReference ensureDelete.
fileReference writeStreamDo: [ :stream | stream nextPutAll: (codeText selectAll; text) ].



]

{ #category : 'layout' }
SpSimpleFileEditPresenter >> saveBackup [

fileReference copyTo: self backupFileReference.

]

{ #category : 'accessing' }
SpSimpleFileEditPresenter >> textInput [

^ codeText
]

{ #category : 'accessing' }
SpSimpleFileEditPresenter >> textInput: anObject [

codeText := anObject
]
Loading