From fc3cafd2ac98b472f70384d830d5adda94cc1fa0 Mon Sep 17 00:00:00 2001 From: Joshua-Dias-Barreto Date: Tue, 26 Sep 2023 22:20:02 +0530 Subject: [PATCH] Added add/delete row functionality. --- .../AISpAddRowPresenter.class.st | 192 ++++++++++++++++++ .../AISpDataFramePresenter.class.st | 68 ++++++- 2 files changed, 250 insertions(+), 10 deletions(-) create mode 100644 src/AI-DataFrameInspector/AISpAddRowPresenter.class.st diff --git a/src/AI-DataFrameInspector/AISpAddRowPresenter.class.st b/src/AI-DataFrameInspector/AISpAddRowPresenter.class.st new file mode 100644 index 0000000..62886ff --- /dev/null +++ b/src/AI-DataFrameInspector/AISpAddRowPresenter.class.st @@ -0,0 +1,192 @@ +" +Class for handling the window which allows users to add rows. +It provides a user friendly way to add rows to DataFrames. +This window has these fields: +- **Position** : The position at which the new row should be added. +- **Row Name** : The row name of the new row to be added. +- **New Row** : The user can type the new row by adding the necessary items under each column. +- **Add button** : Adds the row to the DataFrame. +- **Reset button** : Clears the items entered under each column of the new row. +After entering each item under the specific column, the 'enter' key must be hit. +" +Class { + #name : #AISpAddRowPresenter, + #superclass : #AISpDataFramePresenter, + #instVars : [ + 'sortType', + 'columnName', + 'emptyRow', + 'resetButton', + 'newRowName', + 'newRow', + 'oneRowPresenter', + 'block', + 'rowInputTable', + 'newRowInputTable', + 'positionInput', + 'addButton', + 'tablePresenter', + 'applyButton', + 'cancelButton', + 'dataRows' + ], + #category : #'AI-DataFrameInspector-sub presenters' +} + +{ #category : #'instance creation' } +AISpAddRowPresenter class >> openOn: aDataFrame [ + + ^ (self on: aDataFrame) open +] + +{ #category : #adding } +AISpAddRowPresenter >> addButtonAction [ + + newRow name: newRowName text. + dataFrame addRow: newRow atPosition: positionInput number. + self window close +] + +{ #category : #initialization } +AISpAddRowPresenter >> connectPresenters [ + + addButton action: [ self addButtonAction ]. + resetButton action: [ self resetButtonAction ] +] + +{ #category : #layout } +AISpAddRowPresenter >> defaultLayout [ + + | topLayout bottomLayout | + topLayout := SpBoxLayout newLeftToRight + spacing: 70; + add: (SpGridLayout new + add: 'Position' at: 1 @ 1; + add: positionInput at: 2 @ 1; + yourself); + add: (SpGridLayout new + add: 'Row Name' at: 1 @ 1; + add: newRowName at: 2 @ 1; + yourself); + yourself. + + bottomLayout := SpBoxLayout newLeftToRight + spacing: 5; + add: addButton width: 65; + add: resetButton width: 65; + yourself. + + ^ SpBoxLayout newTopToBottom + spacing: 15; + add: topLayout height: 40; + add: rowInputTable height: 70; + add: bottomLayout height: 30; + yourself +] + +{ #category : #initialization } +AISpAddRowPresenter >> initializeAddResetButtons [ + + addButton := self newButton + label: 'Add'; + icon: (self iconNamed: #smallAdd). + resetButton := self newButton + label: 'Reset'; + icon: (self iconNamed: #refreshIcon) +] + +{ #category : #initialization } +AISpAddRowPresenter >> initializeNewRowInputTable [ + + emptyRow := dataFrame rowsFrom: 1 to: 1. + 1 to: emptyRow numberOfColumns do: [ :columnIndex | + 1 to: emptyRow numberOfRows do: [ :rowIndex | + emptyRow at: rowIndex at: columnIndex put: '' ] ]. + newRow := DataSeries + withKeys: dataFrame columnNames + values: (emptyRow at: 1) asArray. + newRowInputTable := self newTable. + dataFrame columnNames doWithIndex: [ :headerName :columnIndex | + | column | + column := SpStringTableColumn + title: headerName + evaluated: [ :rowWithName | + (rowWithName at: columnIndex + 1) asString ]. + column beEditable. + column onAcceptEdition: [ :row :newValue | + newRow at: headerName put: newValue ]. + newRowInputTable addColumn: column ]. + newRowInputTable beResizable. + newRowInputTable items: emptyRow asArrayOfRowsWithName +] + +{ #category : #'as yet unclassified' } +AISpAddRowPresenter >> initializeNewRowName [ + + newRowName := self newTextInput + placeholder: 'row name'; + text: ''; + yourself +] + +{ #category : #initialization } +AISpAddRowPresenter >> initializePositionInput [ + + positionInput := self newNumberInput + number: dataFrame numberOfRows + 1; + beInteger; + yourself +] + +{ #category : #initialization } +AISpAddRowPresenter >> initializePresenters [ + + self initializeNewRowName. + self initializeRowInputTable. + self initializePositionInput. + self initializeAddResetButtons +] + +{ #category : #'as yet unclassified' } +AISpAddRowPresenter >> initializeRowInputTable [ + + emptyRow := dataFrame rowsFrom: 1 to: 1. + 1 to: emptyRow numberOfColumns do: [ :columnIndex | + 1 to: emptyRow numberOfRows do: [ :rowIndex | + emptyRow at: rowIndex at: columnIndex put: '' ] ]. + newRow := DataSeries + withKeys: dataFrame columnNames + values: (emptyRow at: 1) asArray. + rowInputTable := self newTable. + dataFrame columnNames doWithIndex: [ :headerName :columnIndex | + | column | + column := SpStringTableColumn + title: headerName + evaluated: [ :rowWithName | + (rowWithName at: columnIndex + 1) asString ]. + column beEditable. + column onAcceptEdition: [ :row :newValue | + newRow at: headerName put: newValue ]. + rowInputTable addColumn: column ]. + rowInputTable beResizable. + rowInputTable items: emptyRow asArrayOfRowsWithName +] + +{ #category : #initialization } +AISpAddRowPresenter >> initializeWindow: aWindowPresenter [ + + aWindowPresenter + initialExtent: 500 @ 250; + title: 'Add Row'; + whenClosedDo: [ + newRowName text: ''. + positionInput number: dataFrame numberOfRows + 1 ] +] + +{ #category : #initialization } +AISpAddRowPresenter >> resetButtonAction [ + + self initializeNewRowInputTable. + self layout replace: rowInputTable with: newRowInputTable. + rowInputTable := newRowInputTable +] diff --git a/src/AI-DataFrameInspector/AISpDataFramePresenter.class.st b/src/AI-DataFrameInspector/AISpDataFramePresenter.class.st index cf95225..6fc0eb6 100644 --- a/src/AI-DataFrameInspector/AISpDataFramePresenter.class.st +++ b/src/AI-DataFrameInspector/AISpDataFramePresenter.class.st @@ -17,8 +17,10 @@ Class { 'editButton', 'searchBar', 'sortButton', + 'addRowButton', 'newdataFramePresenter', 'sortPresenter', + 'addRowPresenter', 'windowPresenter' ], #category : #'AI-DataFrameInspector-Main presenters' @@ -30,12 +32,32 @@ AISpDataFramePresenter class >> openOn: aDataFrame [ ^ (self on: aDataFrame) open ] +{ #category : #adding } +AISpDataFramePresenter >> addRowButtonAction [ + + windowPresenter := addRowPresenter open. + windowPresenter whenClosedDo: [ self refreshLayout ] +] + { #category : #initialization } AISpDataFramePresenter >> connectPresenters [ editButton action: [ self editButtonAction ]. searchBar whenTextChangedDo: [ self searchBarAction ]. - sortButton action: [ self sortButtonAction ] + sortButton action: [ self sortButtonAction ]. + addRowButton action: [ self addRowButtonAction ] +] + +{ #category : #'as yet unclassified' } +AISpDataFramePresenter >> dataFrameContextMenu [ + + ^ self newMenu addItem: [ :item | + item + name: 'delete row'; + icon: (self iconNamed: #remove); + action: [ + dataFrame removeRow: (dataFramePresenter selectedItem at: 1). + self refreshLayout ] ] ] { #category : #layout } @@ -48,6 +70,7 @@ AISpDataFramePresenter >> defaultLayout [ add: editButton width: 60; add: sortButton width: 50; add: searchBar width: 100; + add: addRowButton width: 80; yourself) height: 20; add: dataFramePresenter; @@ -120,7 +143,9 @@ AISpDataFramePresenter >> editDataFrame [ newdataFramePresenter addColumn: column ] ]. newdataFramePresenter beResizable. - newdataFramePresenter items: dataFrame asArrayOfRowsWithName + newdataFramePresenter + items: dataFrame asArrayOfRowsWithName; + contextMenu: self dataFrameContextMenu ] { #category : #searching } @@ -144,6 +169,15 @@ AISpDataFramePresenter >> initialize [ super initialize ] +{ #category : #'as yet unclassified' } +AISpDataFramePresenter >> initializeAddRowPresenter [ + + windowPresenter := SpWindowPresenter new. + addRowPresenter := self + instantiate: AISpAddRowPresenter + on: dataFrame +] + { #category : #initialization } AISpDataFramePresenter >> initializeDataFramePresenter [ @@ -167,7 +201,9 @@ AISpDataFramePresenter >> initializeDataFramePresenter [ evaluated: [ :rowWithName | rowWithName at: columnIndex + 1 ]) ]. dataFramePresenter beResizable. - dataFramePresenter items: dataFrame asArrayOfRowsWithName + dataFramePresenter + items: dataFrame asArrayOfRowsWithName; + contextMenu: self dataFrameContextMenu ] { #category : #initialization } @@ -191,7 +227,9 @@ AISpDataFramePresenter >> initializeNewDataFramePresenter [ evaluated: [ :rowWithName | rowWithName at: columnIndex + 1 ]) ]. newdataFramePresenter beResizable. - newdataFramePresenter items: dataFrame asArrayOfRowsWithName + newdataFramePresenter + items: dataFrame asArrayOfRowsWithName; + contextMenu: self dataFrameContextMenu ] { #category : #initialization } @@ -199,7 +237,8 @@ AISpDataFramePresenter >> initializePresenters [ self initializeDataFramePresenter. self initializeToolbarPresenter. - self initializeSortPresenter + self initializeSortPresenter. + self initializeAddRowPresenter ] { #category : #initialization } @@ -222,7 +261,19 @@ AISpDataFramePresenter >> initializeToolbarPresenter [ sortButton := self newButton label: 'sort'; icon: (self iconNamed: #smallOpen); - yourself + yourself. + addRowButton := self newButton + label: 'Add Row'; + icon: (self iconNamed: #smallAdd); + yourself +] + +{ #category : #'as yet unclassified' } +AISpDataFramePresenter >> refreshLayout [ + + self initializeNewDataFramePresenter. + self layout replace: dataFramePresenter with: newdataFramePresenter. + dataFramePresenter := newdataFramePresenter ] { #category : #searching } @@ -248,8 +299,5 @@ AISpDataFramePresenter >> setModelBeforeInitialization: aModel [ AISpDataFramePresenter >> sortButtonAction [ windowPresenter := sortPresenter open. - windowPresenter whenClosedDo: [ - self initializeNewDataFramePresenter. - self layout replace: dataFramePresenter with: newdataFramePresenter. - dataFramePresenter := newdataFramePresenter ] + windowPresenter whenClosedDo: [ self refreshLayout ] ]