-
Notifications
You must be signed in to change notification settings - Fork 63
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
src/Spec2-CommonWidgets/SpSimpleFileEditPresenter.class.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
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 | ||
] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.