SystemWindow subclass: #LQCalendarTableWindow instanceVariableNames: 'fixedExtent' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-TableCalendar'! !LQCalendarTableWindow methodsFor: 'tutorial'! fixedExtent: p fixedExtent := p. super extent: p ! ! !LQCalendarTableWindow methodsFor: 'tutorial'! extent: p super extent: (fixedExtent ifNil: [p]) ! ! Object subclass: #LQCalendarTableIO instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-TableCalendar'! !LQCalendarTableIO methodsFor: 'tutorial'! digits: text ^ text notEmpty and: [text allSatisfy: [:ch | ch >= $0 and: [ch <= $9]]] ! ! !LQCalendarTableIO methodsFor: 'tutorial'! pad: number to: width | text | text := number printString. ^ (String new: ((width - text size) max: 0) withAll: $0), text ! ! !LQCalendarTableIO methodsFor: 'tutorial'! dateText: date ^ (self pad: date year to: 4), '-', (self pad: date monthIndex to: 2), '-', (self pad: date dayOfMonth to: 2) ! ! !LQCalendarTableIO methodsFor: 'tutorial'! dateFrom: text | y m d | text size = 10 ifFalse: [^ nil]. ((text at: 5) = $- and: [(text at: 8) = $-]) ifFalse: [^ nil]. ((self digits: (text copyFrom: 1 to: 4)) and: [(self digits: (text copyFrom: 6 to: 7)) and: [self digits: (text copyFrom: 9 to: 10)]]) ifFalse: [^ nil]. y := (text copyFrom: 1 to: 4) asNumber. m := (text copyFrom: 6 to: 7) asNumber. d := (text copyFrom: 9 to: 10) asNumber. ((y between: 1900 and: 2100) and: [m between: 1 and: 12]) ifFalse: [^ nil]. (d between: 1 and: (Date daysInMonth: m forYear: y)) ifFalse: [^ nil]. ^ Date year: y month: m day: d ! ! !LQCalendarTableIO methodsFor: 'tutorial'! oneLine: text max: limit ^ text notEmpty and: [text size <= limit and: [text allSatisfy: [:ch | ch asciiValue >= 32 and: [ch asciiValue <= 126]]]] ! ! !LQCalendarTableIO methodsFor: 'tutorial'! quote: text ^ '"', (String streamContents: [:s | text do: [:ch | s nextPut: ch. ch = $" ifTrue: [s nextPut: ch]]]), '"' ! ! !LQCalendarTableIO methodsFor: 'tutorial'! csvLine: fields ^ String streamContents: [:s | fields withIndexDo: [:field :i | i > 1 ifTrue: [s nextPut: $,]. s nextPutAll: (self quote: field)]] ! ! !LQCalendarTableIO methodsFor: 'tutorial'! parseLine: text | fields field state ch | fields := OrderedCollection new. field := WriteStream on: String new. state := #start. 1 to: text size do: [:i | ch := text at: i. state = #quoted ifTrue: [ch = $" ifTrue: [state := #closed] ifFalse: [field nextPut: ch]] ifFalse: [state = #closed ifTrue: [ch = $" ifTrue: [field nextPut: ch. state := #quoted] ifFalse: [ch = $, ifTrue: [fields add: field contents. field := WriteStream on: String new. state := #start] ifFalse: [^ nil]]] ifFalse: [ch = $, ifTrue: [fields add: field contents. field := WriteStream on: String new. state := #start] ifFalse: [ch = $" ifTrue: [state = #start ifFalse: [^ nil]. state := #quoted] ifFalse: [field nextPut: ch. state := #plain]]]]]. state = #quoted ifTrue: [^ nil]. fields add: field contents. ^ fields asArray ! ! !LQCalendarTableIO methodsFor: 'tutorial'! readText: path | stream | [stream := FileStream readOnlyFileNamed: path. ^ [stream contentsOfEntireFile] ensure: [stream close]] on: Error do: [:e | ^ nil] ! ! !LQCalendarTableIO methodsFor: 'tutorial'! write: text on: path | stream | stream := FileStream newFileNamed: path. [stream nextPutAll: text] ensure: [stream close] ! ! !LQCalendarTableIO methodsFor: 'tutorial'! install: temporary as: local in: directory ((directory fileExists: local) or: [directory directoryExists: local]) ifTrue: [self error: 'Destination already exists']. directory rename: temporary toBe: local ! ! !LQCalendarTableIO methodsFor: 'tutorial'! saveNew: text to: path | directory local temporary | (path isNil or: [path withBlanksTrimmed isEmpty]) ifTrue: [^ false]. directory := FileDirectory forFileName: path. local := FileDirectory localNameFor: path. ((directory fileExists: local) or: [directory directoryExists: local]) ifTrue: [^ false]. temporary := 'lq-stage-', UUID new asString, '.part'. [[self write: text on: (directory fullNameFor: temporary). self install: temporary as: local in: directory] ensure: [(directory fileExists: temporary) ifTrue: [directory deleteFileNamed: temporary]]] on: Error do: [:e | ^ false]. ^ true ! ! Object subclass: #LQCalendarBook instanceVariableNames: 'month selectedDate appointments selectedId nextId dirty status io' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-TableCalendar'! Morph subclass: #LQCalendarApp instanceVariableNames: 'book window grid list monthLabel dayLabel statusLabel' classVariableNames: '' poolDictionaries: '' category: 'LearnSqueak-TableCalendar'! !LQCalendarBook methodsFor: 'tutorial'! initialize super initialize. io := LQCalendarTableIO new. month := Date year: 2024 month: 2 day: 1. selectedDate := Date year: 2024 month: 2 day: 29. appointments := OrderedCollection new. selectedId := nil. nextId := 1. dirty := false. status := 'Dates and times are floating local wall time; no timezone conversion.' ! ! !LQCalendarBook methodsFor: 'tutorial'! month ^ month ! ! !LQCalendarBook methodsFor: 'tutorial'! selectedDate ^ selectedDate ! ! !LQCalendarBook methodsFor: 'tutorial'! appointments ^ appointments deepCopy ! ! !LQCalendarBook methodsFor: 'tutorial'! selectedId ^ selectedId ! ! !LQCalendarBook methodsFor: 'tutorial'! isDirty ^ dirty ! ! !LQCalendarBook methodsFor: 'tutorial'! status ^ status ! ! !LQCalendarBook methodsFor: 'tutorial'! io ^ io ! ! !LQCalendarBook methodsFor: 'tutorial'! status: text status := text ! ! !LQCalendarBook methodsFor: 'tutorial'! io: writer io := writer ! ! !LQCalendarBook methodsFor: 'tutorial'! monthLabel ^ (#('January' 'February' 'March' 'April' 'May' 'June' 'July' 'August' 'September' 'October' 'November' 'December') at: month monthIndex), ' ', month year printString ! ! !LQCalendarBook methodsFor: 'tutorial'! daysInMonth ^ month daysInMonth ! ! !LQCalendarBook methodsFor: 'tutorial'! weekdayOffset ^ month dayOfWeek - 1 ! ! !LQCalendarBook methodsFor: 'tutorial'! moveMonth: delta | index year monthNumber day | index := (month year * 12) + month monthIndex - 1 + delta. year := index // 12. monthNumber := (index \\ 12) + 1. (year between: 1900 and: 2100) ifFalse: [status := 'Supported years are 1900-2100.'. ^ false]. day := selectedDate dayOfMonth min: (Date daysInMonth: monthNumber forYear: year). month := Date year: year month: monthNumber day: 1. selectedDate := Date year: year month: monthNumber day: day. selectedId := nil. status := 'Month changed; selection clamps to the last valid day.'. ^ true ! ! !LQCalendarBook methodsFor: 'tutorial'! selectDate: date date ifNil: [^ false]. (date year between: 1900 and: 2100) ifFalse: [^ false]. selectedDate := date. month := Date year: date year month: date monthIndex day: 1. selectedId := nil. status := 'Selected ', (io dateText: selectedDate), ' (floating local date).'. ^ true ! ! !LQCalendarBook methodsFor: 'tutorial'! selectDay: number (number between: 1 and: self daysInMonth) ifFalse: [^ false]. ^ self selectDate: (Date year: month year month: month monthIndex day: number) ! ! !LQCalendarApp class methodsFor: 'tutorial'! open ^ self new open ! ! !LQCalendarApp methodsFor: 'tutorial'! open window ifNotNil: [window comeToFront. ^ self]. window := LQCalendarTableWindow new model: self; setLabel: 'Floating Local Calendar'. window addMorph: self fullFrame: (LayoutFrame fractions: (0@0 corner: 1@1)). window fixedExtent: 920@650. window openInWorld. ^ self ! ! !LQCalendarApp methodsFor: 'tutorial'! window ^ window ! ! !LQCalendarApp methodsFor: 'tutorial'! book ^ book ! ! !LQCalendarApp methodsFor: 'tutorial'! windowIsClosing window := nil ! ! !LQCalendarApp methodsFor: 'tutorial'! okToClose ^ true ! ! !LQCalendarApp methodsFor: 'tutorial'! label: text at: point size: size | label | label := StringMorph contents: text font: (TextStyle default fontOfSize: size). label color: (Color fromString: '#14334A'); position: self position + point. self addMorph: label. ^ label ! ! !LQCalendarApp methodsFor: 'tutorial'! button: text at: point action: selector args: args | button | button := SimpleButtonMorph new label: text; target: self; actionSelector: selector; arguments: args. button color: Color white; position: self position + point. self addMorph: button. ^ button ! ! !LQCalendarApp methodsFor: 'tutorial'! request: label initial: value ^ UIManager default request: label initialAnswer: value onCancelReturn: nil ! ! !LQCalendarApp methodsFor: 'tutorial'! confirm: text ^ UIManager default confirm: text ! ! !LQCalendarApp methodsFor: 'tutorial'! status: text book status: text. self refresh ! ! !LQCalendarApp methodsFor: 'tutorial'! initialize super initialize. self extent: 910@620; color: (Color fromString: '#EAF1F6'). book := LQCalendarBook new. self label: 'Floating Local Calendar' at: 24@14 size: 24. self label: ('Checkpoint ', self stage printString, ' / 9 - ', self stageTitle) at: 25@49 size: 14. self label: 'Local wall dates and times. No timezone, UTC conversion, reminders, or recurrence.' at: 25@75 size: 12. monthLabel := self label: book monthLabel at: 220@126 size: 21. grid := Morph new color: Color transparent; extent: 478@333; position: self position + (24@170). self addMorph: grid. dayLabel := self label: '2024-02-29 appointments' at: 530@139 size: 15. list := ScrollPane new color: Color white; extent: 350@333; position: self position + (530@172). self addMorph: list. statusLabel := self label: book status at: 25@578 size: 12. self buildToolbar. self refresh ! ! !LQCalendarApp methodsFor: 'tutorial'! refresh | button position dayText | monthLabel contents: book monthLabel. grid submorphs do: [:each | each delete]. #('Sun' 'Mon' 'Tue' 'Wed' 'Thu' 'Fri' 'Sat') withIndexDo: [:name :index | button := StringMorph contents: name. button position: grid position + (((index - 1) * 68 + 18)@0). grid addMorph: button]. 1 to: book daysInMonth do: [:day | position := book weekdayOffset + day - 1. dayText := day printString. self stage >= 4 ifTrue: [(book countOnDay: day) > 0 ifTrue: [dayText := dayText, ' *']]. button := SimpleButtonMorph new label: dayText; target: self; actionSelector: #selectDay:; arguments: {day}. button position: grid position + (((position \\ 7) * 68)@((position // 7) * 48 + 25)); extent: 65@45. button color: (book selectedDate dayOfMonth = day ifTrue: [Color fromString: '#BDE4D7'] ifFalse: [Color white]). grid addMorph: button]. dayLabel contents: (book io dateText: book selectedDate), ' appointments'. list scroller submorphs do: [:each | each delete]. self stage >= 4 ifTrue: [ book dayAppointments withIndexDo: [:each :index | button := SimpleButtonMorph new label: (book timeText: each third), ' ', each fifth; target: self; actionSelector: #selectAppointment:; arguments: {each first}. button position: 4@(6 + ((index - 1) * 42)); extent: 320@38. button color: (each first = book selectedId ifTrue: [Color fromString: '#BDE4D7'] ifFalse: [Color fromString: '#F4F7FA']). list scroller addMorph: button]]. statusLabel contents: book status. ^ self ! ! !LQCalendarApp methodsFor: 'tutorial'! selectDay: day self stage >= 3 ifTrue: [book selectDay: day] ifFalse: [book status: 'Date selection arrives in checkpoint 3.']. self refresh ! ! !LQCalendarApp methodsFor: 'tutorial'! moveMonth: delta book moveMonth: delta. self refresh ! ! !LQCalendarApp methodsFor: 'tutorial'! stage ^ 3 ! ! !LQCalendarApp methodsFor: 'tutorial'! stageTitle ^ 'Select a date' ! ! !LQCalendarApp methodsFor: 'tutorial'! buildToolbar self button: 'Previous' at: 25@120 action: #moveMonth: args: #(-1). self button: 'Next' at: 125@120 action: #moveMonth: args: #(1). ! !