From adbdfd3b997946ed687414982dc131a45bc1211c Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Mon, 3 Jun 2024 19:32:41 +0200 Subject: [PATCH] Gui/View extensive changes --- app/src/main/java/xl/gui/Editor.java | 16 +++++++-- app/src/main/java/xl/gui/SheetPanel.java | 9 +++-- app/src/main/java/xl/gui/SlotLabel.java | 41 +++++++++++++++++++++-- app/src/main/java/xl/gui/SlotLabels.java | 14 ++++++-- app/src/main/java/xl/gui/StatusLabel.java | 8 +---- app/src/main/java/xl/gui/StatusPanel.java | 11 ++++-- app/src/main/java/xl/gui/XL.java | 30 ++++++++++++----- 7 files changed, 101 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/xl/gui/Editor.java b/app/src/main/java/xl/gui/Editor.java index 1004b4d..fccba39 100644 --- a/app/src/main/java/xl/gui/Editor.java +++ b/app/src/main/java/xl/gui/Editor.java @@ -3,9 +3,21 @@ package xl.gui; import java.awt.Color; import javax.swing.JTextField; -public class Editor extends JTextField { +import xl.controller.EditorController; +import java.awt.event.ActionListener; +import java.awt.event.ActionEvent; - public Editor() { +public class Editor extends JTextField { + + public Editor(EditorController editorController) { setBackground(Color.WHITE); + + // Listen for changes in the text + addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent ae) { + editorController.handleEditorAction(); + } + }); } } diff --git a/app/src/main/java/xl/gui/SheetPanel.java b/app/src/main/java/xl/gui/SheetPanel.java index 74ca92d..83d54a5 100644 --- a/app/src/main/java/xl/gui/SheetPanel.java +++ b/app/src/main/java/xl/gui/SheetPanel.java @@ -3,10 +3,15 @@ package xl.gui; import static java.awt.BorderLayout.CENTER; import static java.awt.BorderLayout.WEST; +import xl.controller.EditorController; +import xl.controller.SlotLabelController; +import xl.model.XLModel; + public class SheetPanel extends BorderPanel { - public SheetPanel(int rows, int columns) { + public SheetPanel(int rows, int columns, XLModel xlModel, SlotLabelController slotLabelController, + EditorController editorController) { add(WEST, new RowLabels(rows)); - add(CENTER, new SlotLabels(rows, columns)); + add(CENTER, new SlotLabels(rows, columns, xlModel, slotLabelController, editorController)); } } diff --git a/app/src/main/java/xl/gui/SlotLabel.java b/app/src/main/java/xl/gui/SlotLabel.java index 5807b19..d93e08d 100644 --- a/app/src/main/java/xl/gui/SlotLabel.java +++ b/app/src/main/java/xl/gui/SlotLabel.java @@ -1,10 +1,45 @@ package xl.gui; import java.awt.Color; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import xl.controller.SlotLabelController; +import java.util.Observer; +import java.util.Observable; +import xl.model.XLModel; -public class SlotLabel extends ColoredLabel { +public class SlotLabel extends ColoredLabel implements Observer { + private String address; - public SlotLabel() { + public SlotLabel(SlotLabelController slotLabelController, int row, char column) { super(" ", Color.WHITE, RIGHT); + this.address = "" + column + row; + + // MouseListener for mouse clicks + addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + slotLabelController.handleSlotClick(SlotLabel.this); + } + }); } -} + + public String getAddress() { + return address; + } + + public void update(Observable o, Object arg) { + if (o instanceof XLModel) { + XLModel xlModel = (XLModel) o; + if (xlModel.containsKey(address)) { + String tempCurrentAdress = xlModel.getAddressContent(address); + xlModel.setCurrentAddress(address); + String content = xlModel.getAddressValue(address); + setText(content); + xlModel.setCurrentAddress(tempCurrentAdress); + } else { + setText(""); + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/xl/gui/SlotLabels.java b/app/src/main/java/xl/gui/SlotLabels.java index e1497d8..56d6548 100644 --- a/app/src/main/java/xl/gui/SlotLabels.java +++ b/app/src/main/java/xl/gui/SlotLabels.java @@ -4,25 +4,35 @@ import java.awt.Color; import java.util.ArrayList; import java.util.List; import javax.swing.SwingConstants; +import xl.model.XLModel; +import xl.controller.EditorController; +import xl.controller.SlotLabelController; public class SlotLabels extends GridPanel { private List labelList; - public SlotLabels(int rows, int cols) { + public SlotLabels(int rows, int cols, XLModel xlModel, SlotLabelController slotLabelController, + EditorController editorController) { super(rows + 1, cols); labelList = new ArrayList(rows * cols); for (char ch = 'A'; ch < 'A' + cols; ch++) { add(new ColoredLabel(Character.toString(ch), Color.LIGHT_GRAY, SwingConstants.CENTER)); } + for (int row = 1; row <= rows; row++) { for (char ch = 'A'; ch < 'A' + cols; ch++) { - SlotLabel label = new SlotLabel(); + SlotLabel label = new SlotLabel(slotLabelController, row, ch); + xlModel.addObserver(label); add(label); labelList.add(label); } } + SlotLabel firstLabel = labelList.get(0); firstLabel.setBackground(Color.YELLOW); + xlModel.setCurrentAddress("A1"); + slotLabelController.setCurrentSlot(firstLabel); + editorController.setCurrentSlot(firstLabel); } } diff --git a/app/src/main/java/xl/gui/StatusLabel.java b/app/src/main/java/xl/gui/StatusLabel.java index 03389eb..cf92cf8 100644 --- a/app/src/main/java/xl/gui/StatusLabel.java +++ b/app/src/main/java/xl/gui/StatusLabel.java @@ -1,16 +1,10 @@ package xl.gui; import java.awt.Color; -import java.util.Observable; -import java.util.Observer; -public class StatusLabel extends ColoredLabel implements Observer { +public class StatusLabel extends ColoredLabel { public StatusLabel() { super("", Color.WHITE); } - - public void update(Observable observable, Object object) { - setText(""); - } } diff --git a/app/src/main/java/xl/gui/StatusPanel.java b/app/src/main/java/xl/gui/StatusPanel.java index a6087bf..3f1ee6e 100644 --- a/app/src/main/java/xl/gui/StatusPanel.java +++ b/app/src/main/java/xl/gui/StatusPanel.java @@ -3,10 +3,15 @@ package xl.gui; import static java.awt.BorderLayout.CENTER; import static java.awt.BorderLayout.WEST; +import xl.controller.SlotLabelController; +import xl.model.XLModel; + public class StatusPanel extends BorderPanel { - protected StatusPanel(StatusLabel statusLabel) { - add(WEST, new CurrentLabel()); + protected StatusPanel(StatusLabel statusLabel, XLModel solver, SlotLabelController slotLabelController) { + CurrentLabel currentLabel = new CurrentLabel(); + slotLabelController.setCurrentLabel(currentLabel); + add(WEST, currentLabel); add(CENTER, statusLabel); } -} +} \ No newline at end of file diff --git a/app/src/main/java/xl/gui/XL.java b/app/src/main/java/xl/gui/XL.java index d4c3e69..c2b232b 100644 --- a/app/src/main/java/xl/gui/XL.java +++ b/app/src/main/java/xl/gui/XL.java @@ -6,7 +6,11 @@ import static java.awt.BorderLayout.SOUTH; import javax.swing.JFrame; import javax.swing.JPanel; + +import xl.controller.EditorController; +import xl.controller.SlotLabelController; import xl.gui.menu.XLMenuBar; +import xl.model.XLModel; public class XL extends JFrame { @@ -15,23 +19,31 @@ public class XL extends JFrame { private StatusLabel statusLabel = new StatusLabel(); private XLList xlList; - public XL(XL oldXL) { - this(oldXL.xlList, oldXL.counter); + public XL(XL oldXL, XLModel XLModel) { + this(oldXL.xlList, oldXL.counter, XLModel); } - public XL(XLList xlList, XLCounter counter) { - super("Untitled-" + counter); + public XL(XLList xlList, XLCounter counter, XLModel XLModel) { + super("Window " + counter); this.xlList = xlList; this.counter = counter; xlList.add(this); counter.increment(); - JPanel statusPanel = new StatusPanel(statusLabel); - JPanel sheetPanel = new SheetPanel(ROWS, COLUMNS); - Editor editor = new Editor(); + + EditorController editorController = new EditorController(XLModel, statusLabel); + Editor editor = new Editor(editorController); + editorController.setEditor(editor); + + SlotLabelController slotLabelController = new SlotLabelController(XLModel, editor, statusLabel); + slotLabelController.setEditorController(editorController); + + JPanel statusPanel = new StatusPanel(statusLabel, XLModel, slotLabelController); + JPanel sheetPanel = new SheetPanel(ROWS, COLUMNS, XLModel, slotLabelController, editorController); + add(NORTH, statusPanel); add(CENTER, editor); add(SOUTH, sheetPanel); - setJMenuBar(new XLMenuBar(this, xlList, statusLabel)); + setJMenuBar(new XLMenuBar(this, xlList, statusLabel, XLModel)); pack(); setDefaultCloseOperation(EXIT_ON_CLOSE); setResizable(false); @@ -44,6 +56,6 @@ public class XL extends JFrame { } public static void main(String[] args) { - new XL(new XLList(), new XLCounter()); + new XL(new XLList(), new XLCounter(), new XLModel()); } }