Docsity

String Morph Subclass: Clickable String Morph - Notes | CS 2340, Papers of Computer Science

Toggle sidebar
bg1
StringMorph subclass: #ClickableStringMorph
instanceVariableNames: 'model normalColor clickAction doubleClickAction
contentSelector stateSelector '
classVariableNames: ''
poolDictionaries: ''
category: 'Newspaper'!
!ClickableStringMorph commentStamp: '<historical>' prior: 0!
Authors: Galina Yeremenchuk ([email protected]), Albert Urshanskiy
Class: ClickableStringMorph
Description: Represents a String Morph capable of responding to single-clicks
and double-clicks.
!
!ClickableStringMorph methodsFor: 'event handling' stamp: 'npm 2/13/2000 22:01'!
click: evt
"Alternates the color of the text string upon each single-click and
performs the action associated with single-clicks."
self color: ((color = normalColor) ifTrue: [(normalColor muchLighter)]
ifFalse: [normalColor.]).
clickAction value: model.
! !
!ClickableStringMorph methodsFor: 'event handling' stamp: 'npm 2/14/2000 18:12'!
doubleClick: evt
"Performs the action associated with double-clicks."
doubleClickAction value: model.
! !
!ClickableStringMorph methodsFor: 'event handling' stamp: 'npm 2/12/2000 23:52'!
handlesMouseDown: evt
"Indicates that this ClickableStringMorph handles MouseDown events."
^true.
! !
!ClickableStringMorph methodsFor: 'event handling' stamp: 'npm 2/13/2000 19:21'!
mouseDown: evt
"Dispatch MouseDown events to this ClickableStringMorph's handlers for
single-clicks and double-clicks."
evt hand waitForClicksOrDrag: self event: evt.
! !
!ClickableStringMorph methodsFor: 'event handling' stamp: 'npm 2/14/2000 18:13'!
update: anEvent
"Have this ClickableStringMorph take appropriate action when its model
updates."
(anEvent = contentSelector) ifTrue: [self contents: ((model perform:
contentSelector) asString).].
(anEvent = stateSelector) ifTrue: [self isSelected: (model perform:
stateSelector).].
! !
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37

Partial preview of the text

Download String Morph Subclass: Clickable String Morph - Notes | CS 2340 and more Papers Computer Science in PDF only on Docsity!

StringMorph subclass: #ClickableStringMorph instanceVariableNames: 'model normalColor clickAction doubleClickAction contentSelector stateSelector ' classVariableNames: '' poolDictionaries: '' category: 'Newspaper'! !ClickableStringMorph commentStamp: '' prior: 0! Authors: Galina Yeremenchuk ([email protected]), Albert Urshanskiy ([email protected]), Nick Michalko ([email protected]) Class: ClickableStringMorph Description: Represents a String Morph capable of responding to single-clicks and double-clicks. ! !ClickableStringMorph methodsFor: 'event handling' stamp: 'npm 2/13/2000 22:01'! click: evt "Alternates the color of the text string upon each single-click and performs the action associated with single-clicks." self color: ((color = normalColor) ifTrue: [(normalColor muchLighter)] ifFalse: [normalColor.]). clickAction value: model. !! !ClickableStringMorph methodsFor: 'event handling' stamp: 'npm 2/14/2000 18:12'! doubleClick: evt "Performs the action associated with double-clicks." doubleClickAction value: model. !! !ClickableStringMorph methodsFor: 'event handling' stamp: 'npm 2/12/2000 23:52'! handlesMouseDown: evt "Indicates that this ClickableStringMorph handles MouseDown events." ^true. !! !ClickableStringMorph methodsFor: 'event handling' stamp: 'npm 2/13/2000 19:21'! mouseDown: evt "Dispatch MouseDown events to this ClickableStringMorph's handlers for single-clicks and double-clicks." evt hand waitForClicksOrDrag: self event: evt. !! !ClickableStringMorph methodsFor: 'event handling' stamp: 'npm 2/14/2000 18:13'! update: anEvent "Have this ClickableStringMorph take appropriate action when its model updates." (anEvent = contentSelector) ifTrue: [self contents: ((model perform: contentSelector) asString).]. (anEvent = stateSelector) ifTrue: [self isSelected: (model perform: stateSelector).]. !!

!ClickableStringMorph methodsFor: 'accessing' stamp: 'npm 2/14/2000 18:14'! clickAction: aBlock "Set the action associated with a single-click. The provided block must accept an argument for the model of this ClickableStringMorph." clickAction := aBlock. !! !ClickableStringMorph methodsFor: 'accessing' stamp: 'npm 2/13/2000 19:56'! contentSelector: aContentSelector "What message should the model be sent for this ClickableStringMorph to retrieve its contents?" contentSelector := aContentSelector. !! !ClickableStringMorph methodsFor: 'accessing' stamp: 'npm 2/14/2000 18:14'! doubleClickAction: aBlock "Set the action associated with a double-click. The provided block must accept an argument for the model of this ClickableStringMorph." doubleClickAction := aBlock.!! !ClickableStringMorph methodsFor: 'accessing' stamp: 'npm 2/14/2000 18:15'! isSelected: aBoolean "Adjusts the display properties of this ClickableStringMorph as necessary." (aBoolean) ifTrue: [self color: normalColor.] ifFalse: [self color: (normalColor muchLighter).].!! !ClickableStringMorph methodsFor: 'accessing' stamp: 'npm 2/14/2000 18:15'! model: aModel "Set the model attribute of this ClickableStringMorph." model := aModel. model addDependent: self. !! !ClickableStringMorph methodsFor: 'accessing' stamp: 'npm 2/13/2000 19:57'! stateSelector: aStateSelector "What message should the model be sent in order for this ClickableStringMorph to determine its select state?" stateSelector := aStateSelector.!! !ClickableStringMorph methodsFor: 'initialization' stamp: 'npm 2/14/2000 18:03'! initialize "Set the instance variables of this ClickableStringMorph to their default values." normalColor := (Color blue). clickAction := []. doubleClickAction := [].

EmbeddedServerAction subclass: #DeselectAction instanceVariableNames: 'webpaper ' classVariableNames: '' poolDictionaries: '' category: 'Newspaper'! !DeselectAction commentStamp: '' prior: 0! This class subclasses EmbeddedServerAction. It handles the request for the de- selection of the newssources.! !DeselectAction methodsFor: 'URL processing' stamp: 'gy 4/5/2000 12:49'! process: request |fp doc paper cat sources name allSources | Transcript show:'in deselect process'.Transcript cr. fp:=FileStream fileNamed: 'ss.html'. doc := HTMLformatter startPage:'Deselect page'. doc :=doc,'

'. allSources := Set new. paper := self webpaper. paper loadFromFile: 'newssources.txt'. cat := paper newsCategories. cat do:[ :aCategor | sources :=aCategor newsSources. sources do:[:aSource| (aSource isSelected) ifTrue:[ name := (aSource sourceURL) asString. allSources add:name. ]]]. Transcript cr. Transcript show: allSources asString.Transcript cr. doc := doc, '

', 'The following sources were previously selected via the Squeak based interface','

'. Transcript show: doc asString. allSources do:[:aSource| doc := doc, aSource. doc := doc, (HTMLformatter checkbox: aSource value:true). doc :=doc, '

'.].

doc := doc, ''. doc := doc,'

'. Transcript show: doc asString. fp nextPutAll:doc. fp close. request reply: PWS success; reply: PWS contentHTML; reply: PWS crlf. request reply: (FileStream fileNamed: 'ss.html')contentsOfEntireFile. ! ]style[(16 374 262 289 11 1 308 1)f1b,f1,f2,f1,f2,f3,f1,f1b!! !DeselectAction methodsFor: 'accessors' stamp: 'gy 4/4/2000 15:03'! webpaper "return the model for this class" ^webpaper.!! !DeselectAction methodsFor: 'accessors' stamp: 'gy 4/2/2000 14:22'! webpaper: aPaper "set the model for this class" webpaper := aPaper.!! "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "! DeselectAction class instanceVariableNames: ''! !DeselectAction class methodsFor: 'instance creation' stamp: 'gy 4/2/ 15:29'! openOn: aPaper "create an instance of a newscategory action and set its model to the passed in paper" | deselection | deselection := DeselectAction new. Transcript show: 'webpaper should be set'. deselection webpaper: aPaper. Transcript show: 'in deselect openon'. PWS link:'deselect' to: deselection. !!

name := aName. self changed: #name. !! !NewsCategory methodsFor: 'accessing' stamp: 'npm 2/13/2000 20:57'! newsSources "What are the NewsSources associated with this NewsCategory?" ^sources.!! !NewsCategory methodsFor: 'accessing' stamp: 'npm 2/14/2000 18:23'! removeNewsSource: aNewsSource "Remove the specified NewsSource from this NewsCategory." sources remove: aNewsSource. self changed: #newsSources.!! !NewsCategory methodsFor: 'accessing' stamp: 'gy 3/22/2000 09:48'! selected:aBoolean "comment stating purpose of message" isSelected := aBoolean.!! !NewsCategory methodsFor: 'accessing' stamp: 'npm 2/13/2000 21:52'! toggle "Toggle the select Boolean of this NewsCategory." isSelected := isSelected not. self changed: #isSelected. !! !NewsCategory methodsFor: 'initializing' stamp: 'npm 3/1/2000 20:34'! initialize "Initialize the instance variables of this NewsCategory to their default values." isPrimary := false. isSelected := false. name := nil. sources := OrderedCollection new. !! !NewsCategory methodsFor: 'file storage' stamp: 'npm 2/14/2000 15:48'! stringSummary "Returns a string summary of this NewsCategory (string format: NewsCategory name followed by a dollar sign followed by the select boolean followed by NewsSource summaries on separate lines)." | summary | summary := name , '$' , (isSelected asString).

sources do: [:source | summary := summary , (String cr) , (source stringSummary).]. ^(summary asString).!! "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "! NewsCategory class instanceVariableNames: ''! !NewsCategory class methodsFor: 'instance creation' stamp: 'npm 2/14/ 16:01'! fromString: aString "Instantiates a NewsCategory object based upon the provided string (string format: NewsCategory name followed by a dollar sign followed by the select boolean followed by NewsSource summaries on separate lines). " | aNewsCategory tokens categoryInfo categoryName categoryIsSelected | tokens := aString findTokens: (String cr). "Create a NewsCategory object with appropriate name and select values." categoryInfo := (tokens at: 1) findTokens: '$'. categoryName := (categoryInfo at: 1). categoryIsSelected := ((categoryInfo at: 2) = 'true'). aNewsCategory := NewsCategory named: categoryName isSelected: categoryIsSelected. "Add the associated NewsSources to this NewsCategory." 2 to: (tokens size) do: [:index | aNewsCategory addNewsSource: (NewsSource fromString: (tokens at: index)).]. ^aNewsCategory. !! !NewsCategory class methodsFor: 'instance creation' stamp: 'npm 2/9/2000 21:12'! named: aName "Instantiates a NewsCategory object with the provided name and performs initialization." | aNewsCategory | aNewsCategory := super new initialize. aNewsCategory name: aName. ^aNewsCategory. !! !NewsCategory class methodsFor: 'instance creation' stamp: 'npm 2/10/ 17:37'! named: aName isSelected: aBoolean "Instantiates a NewsCategory object with the provided name & select value." | aNewsCategory |

EmbeddedServerAction subclass: #NewsCategoryAction instanceVariableNames: 'webpaper ' classVariableNames: '' poolDictionaries: '' category: 'Newspaper'! !NewsCategoryAction commentStamp: '' prior: 0! Respond to the PWS requests of the newscategory class.! !NewsCategoryAction methodsFor: 'accessors' stamp: 'gy 3/23/2000 12:16'! webpaper: aPaper "set the model for this class" webpaper := aPaper.!! !NewsCategoryAction methodsFor: 'URL processing' stamp: 'gyu 4/21/2000 12:54'! process: request "Process the PWS request, and respond appropriately" Transcript show: request url asString. Transcript cr. request reply: PWS success; reply: PWS contentHTML; reply: PWS crlf. request reply: (FileStream fileNamed: 'newscategories.html') contentsOfEntireFile. Transcript show: 'in news category process method'!! "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "! NewsCategoryAction class instanceVariableNames: ''! !NewsCategoryAction class methodsFor: 'instance creation' stamp: 'gy 3/23/ 12:17'! openOn: aPaper "create an instance of a newscategory action and set its model to the passed in paper" | webcategory| webcategory := NewsCategoryAction new. Transcript show: 'in newscategory openon'. PWS link:'newscategory' to: webcategory. webcategory webpaper: aPaper.!!

Object subclass: #NewsCategoryDisplay instanceVariableNames: 'newsPaper newsCategories ' classVariableNames: '' poolDictionaries: '' category: 'Newspaper'! !NewsCategoryDisplay commentStamp: '' prior: 0! Authors: Galina Yeremenchuk ([email protected]), Albert Urshanskiy ([email protected]), Nick Michalko ([email protected]) Class: NewsCategoryDisplay Description: Provides a user interface to the NewsCategories. ! !NewsCategoryDisplay methodsFor: 'accessing' stamp: 'gy 4/3/2000 20:45'! newsPaper "Return the newspaper model of this class" ^newsPaper.!! !NewsCategoryDisplay methodsFor: 'accessing' stamp: 'npm 2/14/2000 19:21'! newsPaper: aNewspaper "Set the Newspaper model of this NewsCategoryDisplay." newsPaper := aNewspaper. newsCategories := aNewspaper newsCategories. newsPaper addDependent: self. !! !NewsCategoryDisplay methodsFor: 'display' stamp: 'npm 2/23/2000 18:21'! display "Open a Window allowing the user to select which NewsCategories to include in the Newspaper." | window windowColor categoryAlignMorph categoryScrollPane categoryTransform clickString buttonPanel saveAsButton primButton closeButton spacer | "First create a Window to house our subsequent components." window := SystemWindow labelled: 'News Categories'. windowColor := Color r: 0.376 g: 0.972 b: 0.694. "Next create the NewsCategory ScrollPane." categoryScrollPane := ScrollPane new initialize. categoryTransform := TransformMorph new initialize. categoryTransform color: (windowColor).

primButton color: (windowColor slightlyDarker). closeButton := SimpleButtonMorph new. closeButton target: window. closeButton actionSelector: #delete. closeButton label: 'Close'. closeButton borderWidth: 1. closeButton borderColor: (Color blue). closeButton color: (windowColor slightlyDarker). buttonPanel addMorph: closeButton. buttonPanel addMorph: (spacer fullCopy). buttonPanel addMorph: saveAsButton. buttonPanel addMorph: (spacer fullCopy). buttonPanel addMorph: primButton. "Finally add the button panel to the bottom 1/4 of the Window." window addMorph: buttonPanel frame: ([email protected] extent: [email protected]). "Open the Window once all the components have been added." window visible: false. window openInWorld. window newBounds: ((window position) extent: 400@300). window visible: true. !! !NewsCategoryDisplay methodsFor: 'event handling' stamp: 'npm 2/23/2000 19:08'! saveAs "Prompt the user for a file path and save the current Newspaper configuration to that file." | fileName | "Have the user input the file name via a FillInTheBlankMorph. Then have the Newspaper save the current configuration." fileName := FillInTheBlankMorph request: 'Configuration File Name' initialAnswer: (newsPaper fileLastUsed). (fileName isEmpty) ifFalse: [newsPaper saveToFile: fileName.]. !! !NewsCategoryDisplay methodsFor: 'event handling' stamp: 'gy 4/4/2000 22:28'! selectPrimaryNewsCategory "Have the user select the primary NewsCategory of the Newspaper." | primaryCategory categories | "Retrieve the name of the primary NewsCategory via a FillInTheBlankMorph."

primaryCategory :=FillInTheBlankMorph request: 'Primary Category Name'. Transcript show: 'primary is ', primaryCategory asString.Transcript cr. categories := (self newsPaper) newsCategories. categories do:[ :aCat| ((aCat name asString) sameAs:(primaryCategory asString))ifTrue:[ Transcript show:'setting ', (aCat name) asString, ' to primary'.Transcript cr. aCat isPrimaryCategory:true. Transcript show:(aCat isPrimaryCategory) asString.]]. !! "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "! NewsCategoryDisplay class instanceVariableNames: ''! !NewsCategoryDisplay class methodsFor: 'instance creation' stamp: 'npm 2/14/ 19:21'! openOn: aNewspaper "Instantiates & displays a NewsCategoryDisplay on the provided Newspaper." | aNewsCategoryDisplay | aNewsCategoryDisplay := super new. aNewsCategoryDisplay newsPaper: aNewspaper. aNewsCategoryDisplay display. ^aNewsCategoryDisplay. !!

"The below code was found in the Scamper method for generating a plain-text HTML page." "document := (HtmlParser parse: (ReadStream on: anHTMLArticle))." " formatter := HtmlFormatter new. document addToFormatter: formatter. formattedPage := formatter text." "Add some delimiter strings to the formatted article to distinguish it from other artcles." topDelimeter := '

','-------------------------------------------------------------------' ,'

'. topDelimeter := topDelimeter , '','

', ((aNewsReporter newsCategory) name) , ' - ' , (((aNewsReporter newsSource) sourceURL) asString),'

'. topDelimeter := topDelimeter , '

' , '------------------------------------------------------------------' , '

' , '

','

'. bottomDelimeter:= '

', '

' , '

'. "Add the article to the newspaper file." formattedPage := topDelimeter , anHTMLArticle, bottomDelimeter. temp2 nextPutAll: formattedPage.]. temp2 close.] ifFalse:[ articles do: [:anHTMLArticle | "The below code was found in the Scamper method for generating a plain-text HTML page." "document := (HtmlParser parse: (ReadStream on: anHTMLArticle))." " formatter := HtmlFormatter new. document addToFormatter: formatter. formattedPage := formatter text." "Add some delimiter strings to the formatted article to distinguish it from other artcles." topDelimeter := '

','-------------------------------------------------------------------' ,'

'. topDelimeter := topDelimeter , '','

', ((aNewsReporter newsCategory) name) , ' - ' , (((aNewsReporter newsSource) sourceURL) asString),'

'. topDelimeter := topDelimeter , '

' , '------------------------------------------------------------------' , '

' , '

','

'.

bottomDelimeter:= '

', '

' , '

'. "Add the article to the newspaper file." formattedPage := topDelimeter , anHTMLArticle, bottomDelimeter. aFileStream nextPutAll: formattedPage.]. ]!! !NewsEditor methodsFor: 'generating' stamp: 'npm 3/1/2000 21:39'! generateNewspaper "Generates a viewable Newspaper based upon the preferences of the user." "First, create the requisite NewsReporters based upon the user's selections." self hireNewsReporters. "Next dispatch each NewsReporter to collect news articles. I may implement this with multiple threads/processes at a later date." self dispatchNewsReporters. "Produce a Newspaper file containing the reported articles." self generateNewspaperFile. !! !NewsEditor methodsFor: 'generating' stamp: 'gy 4/4/2000 22:38'! generateNewspaperFile "Produces a crude plain-text file containing the news articles collected by the NewsReporters." | fs fd fileName temp2 temp1 | "The NewsEditor will store the generated newspaper in a file named 'newspaper.txt'." fileName := 'temp1.html'. "Delete the old newspaper file." fd := FileDirectory default. fd deleteFileNamed: fileName. "Create a new newspaper file containing the collected articles." fs := FileStream fileNamed: fileName. newsReporters do: [:aNewsReporter | self addArticlesFrom: aNewsReporter toFile: fs.]. fs close. (FileStream isAFileNamed:'temp2.html') ifTrue:[ temp2:=FileStream fileNamed:'temp2.html'] ifFalse:[temp2:=nil.]. temp1 := FileStream fileNamed:'temp1.html'. fs := FileStream fileNamed:'newspaper.html'.

^newsEditor. !!

Object subclass: #NewsReporter instanceVariableNames: 'newsSource newsCategory newsEditor newsArticles maximumNumberOfArticles ' classVariableNames: '' poolDictionaries: '' category: 'Newspaper'! !NewsReporter commentStamp: '' prior: 0! Author: Nick Michalko ([email protected]) Category: NewsReporter Description: Retrieves articles relevant to a specified NewsCategory from a provided NewsSource.! !NewsReporter methodsFor: 'processing' stamp: 'npm 3/1/2000 23:48'! collectLinksExiting: sourceURL "Collects the links/anchors departing from a given URL." | pageContents | "Get the HTML contents of the specified URL and extract the anchors." pageContents := (HTTPSocket httpGet: (sourceURL asString)) contents. ^(self collectLinksOnPage: pageContents pageURL: sourceURL). !! !NewsReporter methodsFor: 'processing' stamp: 'gy 3/23/2000 11:53'! collectLinksOnPage: HTMLString pageURL: sourceURL "Tokenizes the HTML contents of the specified HTML page and preserves the departing links as URLs." | tokens nextToken tags links graphicLinks urls url | "Tokenize the HTML." tokens := HtmlTokenizer on: (ReadStream on: HTMLString). "Extract the tags from the tokenized HTML." tags := OrderedCollection new. [(nextToken := tokens next) isNil.] whileFalse: [(nextToken isTag) ifTrue: [tags add: nextToken.]]. "Extract the anchors/links from the collection of tags." links := tags select: [:aTag | (((aTag source) asLowercase) beginsWith: '<a href').]. "graphicLinks := tags select:[:aTag | (((aTag source) asLowercase) beginsWith:'<img src').]." "Now convert the links into valid URLs. The following code should generate valid URLs for absolute and relative anchors." urls := OrderedCollection new. links do: [:aLink | url := self contentsFrom: '=' to: '>' onString: (aLink source).