Skip to content

Commit ced43b7

Browse files
committed
Add Edit Item functionality
1 parent 37ddfc9 commit ced43b7

File tree

8 files changed

+78
-44
lines changed

8 files changed

+78
-44
lines changed

src/main/java/com/sankdev/App.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
*/
3838
public class App extends Application {
3939

40+
public static final String PRIMARY_VIEW = "primary";
41+
public static final String EDIT_ITEM_VIEW = "editItem";
42+
4043
private static final ResourceBundle theGuiResourceBundle = ResourceBundle.getBundle(
4144
"com.sankdev.locale.GuiResourceBundle", Locale.getDefault());
4245

src/main/java/com/sankdev/controller/AddItemController.java

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.sankdev.controller;
2+
3+
import com.sankdev.App;
4+
import com.sankdev.model.ItemHolder;
5+
import com.sankdev.portfolio.Item;
6+
import com.sankdev.portfolio.Publication;
7+
import com.sankdev.service.PortfolioService;
8+
import java.io.IOException;
9+
import javafx.fxml.FXML;
10+
import javafx.scene.control.TextField;
11+
12+
/**
13+
* Controller for adding a new portfolio item.
14+
*/
15+
public class EditItemController {
16+
17+
// Model
18+
private final ItemHolder itemHolder = ItemHolder.getInstance();
19+
private static final PortfolioService portfolioService = new PortfolioService();
20+
21+
@FXML
22+
public TextField itemId;
23+
24+
@FXML
25+
public TextField itemName;
26+
27+
// Automatically called by FXML Loader
28+
// NOTE: Good place to hook onto the model
29+
public void initialize() {
30+
if (itemHolder.getItem() != null) {
31+
this.itemId.setText(itemHolder.getItem().getId());
32+
this.itemName.setText(itemHolder.getItem().getName());
33+
}
34+
}
35+
36+
@FXML
37+
private void switchToPrimary() throws IOException {
38+
App.setRoot(App.PRIMARY_VIEW);
39+
}
40+
41+
@FXML
42+
private void saveItem() throws IOException {
43+
if (itemHolder.getItem() != null) {
44+
Item editedItem = itemHolder.getItem();
45+
editedItem.setId(this.itemId.getText());
46+
editedItem.setName(this.itemName.getText());
47+
} else {
48+
Item newItem = new Publication(this.itemId.getText(), this.itemName.getText());
49+
portfolioService.addPortfolioItem(newItem);
50+
}
51+
App.setRoot(App.PRIMARY_VIEW);
52+
}
53+
}

src/main/java/com/sankdev/controller/PrimarySceneController.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.sankdev.controller;
22

33
import com.sankdev.App;
4+
import com.sankdev.model.ItemHolder;
45
import com.sankdev.portfolio.Item;
56
import com.sankdev.service.PortfolioService;
67
import java.io.IOException;
@@ -15,7 +16,8 @@
1516
* The Primary controller for the primary scene, i.e. the Application Main Window layout.
1617
*/
1718
public class PrimarySceneController {
18-
19+
// Model
20+
private final ItemHolder itemHolder = ItemHolder.getInstance();
1921
private final PortfolioService portfolioService = new PortfolioService();
2022

2123
// Primary scene control bindings
@@ -38,8 +40,9 @@ public void initialize() {
3840
}
3941

4042
@FXML
41-
private void switchToAddItem() throws IOException {
42-
App.setRoot("addItem");
43+
private void onAdd() throws IOException {
44+
itemHolder.setItem(null);
45+
App.setRoot(App.EDIT_ITEM_VIEW);
4346
}
4447

4548
@FXML
@@ -62,4 +65,12 @@ private void readPortfolio() throws IOException, ClassNotFoundException {
6265
// when deserialized
6366
tableView.setItems(portfolioService.getPortfolioItems());
6467
}
68+
69+
public void onEdit() throws IOException {
70+
if (tableView.getSelectionModel().getSelectedItem() != null) {
71+
Item selectedItem = tableView.getSelectionModel().getSelectedItem();
72+
itemHolder.setItem(selectedItem);
73+
App.setRoot(App.EDIT_ITEM_VIEW);
74+
}
75+
}
6576
}

src/main/java/com/sankdev/controller/ItemHolder.java renamed to src/main/java/com/sankdev/model/ItemHolder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.sankdev.controller;
1+
package com.sankdev.model;
22

33
import com.sankdev.portfolio.Item;
44

src/main/java/module-info.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
opens com.sankdev.dao to javafx.base;
88
exports com.sankdev.controller;
99
opens com.sankdev.controller to javafx.fxml;
10+
exports com.sankdev.model;
11+
opens com.sankdev.model to javafx.fxml;
1012
}

src/main/resources/com/sankdev/addItem.fxml renamed to src/main/resources/com/sankdev/editItem.fxml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
<?import javafx.scene.control.TextField?>
1010
<VBox alignment="TOP_LEFT" spacing="20.0" xmlns="http://javafx.com/javafx/8.0.171"
1111
xmlns:fx="http://javafx.com/fxml/1"
12-
fx:controller="com.sankdev.controller.AddItemController"
12+
fx:controller="com.sankdev.controller.EditItemController"
1313
stylesheets="@css/base.css">
1414
<children>
1515
<Label text="%addItemText" />
1616
<Label text="ID: " />
17-
<TextField fx:id="newItemId" />
17+
<TextField fx:id="itemId" />
1818
<Label text="NAME: " />
19-
<TextField fx:id="newItemName" />
19+
<TextField fx:id="itemName" />
2020
<HBox>
2121
<Button fx:id="okButton" text="%okKey" onAction="#saveItem"/>
2222
<Button fx:id="cancelButton" text="%cancelKey" onAction="#switchToPrimary" />

src/main/resources/com/sankdev/primary.fxml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
<top>
1313
<HBox styleClass="h-box" xmlns:fx="http://javafx.com/fxml">
1414
<children>
15-
<Button fx:id="editButton" text="%editButton">
15+
<Button fx:id="editButton" text="%editButton" onAction="#onEdit">
1616
</Button>
1717
<Button fx:id="deleteButton" text="%deleteButton">
1818
</Button>
19-
<Button fx:id="addButton" text="%addButton" onAction="#switchToAddItem">
19+
<Button fx:id="addButton" text="%addButton" onAction="#onAdd">
2020
</Button>
2121
<Button fx:id="saveButton" text="%saveButton" onAction="#writePortfolio">
2222
</Button>

0 commit comments

Comments
 (0)