Controller first working version

This commit is contained in:
Imbus 2024-06-03 19:33:14 +02:00
parent adbdfd3b99
commit 3b0eb9052d
2 changed files with 119 additions and 0 deletions

View file

@ -0,0 +1,59 @@
package xl.controller;
import xl.model.XLModel;
import xl.gui.Editor;
import xl.gui.SlotLabel;
import xl.gui.StatusLabel;
/**
* The controller for the editor.
*
* The controller is responsible for handling the editor actions.
*/
public class EditorController {
private XLModel xlModel;
private Editor editor;
private SlotLabel currentSlot;
private StatusLabel statusLabel;
public EditorController(XLModel xlModel, StatusLabel statusLabel) {
this.xlModel = xlModel;
this.statusLabel = statusLabel;
}
public void setCurrentSlot(SlotLabel currentSlot) {
this.currentSlot = currentSlot;
}
public void setEditor(Editor editor) {
this.editor = editor;
}
public void handleEditorAction() {
String content = editor.getText();
String address = currentSlot.getAddress();
if (content.isEmpty()) {
boolean isOkRemove = xlModel.remove(address);
if (isOkRemove) {
currentSlot.setText("");
statusLabel.setText("");
} else {
statusLabel.setText(xlModel.getStatus());
}
} else if (content.charAt(0) == '#') {
xlModel.add(address, content);
currentSlot.setText(content.substring(1));
statusLabel.setText("");
} else {
xlModel.add(address, content);
if (xlModel.getStatus().equals("")) {
content = xlModel.getAddressValue(address);
currentSlot.setText(content);
} else {
statusLabel.setText(xlModel.getStatus());
}
}
}
}

View file

@ -0,0 +1,60 @@
package xl.controller;
import java.awt.Color;
import xl.model.XLModel;
import xl.gui.*;
/**
* The controller for the slot labels.
*
* The controller is responsible for handling the slot label actions.
*/
public class SlotLabelController {
private XLModel xlModel;
private Editor editor;
private SlotLabel currentSlot;
private EditorController editorController;
private StatusLabel statusLabel;
private CurrentLabel currentLabel;
public SlotLabelController(XLModel xlModel, Editor editor, StatusLabel statusLabel) {
this.xlModel = xlModel;
this.editor = editor;
this.statusLabel = statusLabel;
}
public void setCurrentLabel(CurrentLabel currentLabel) {
this.currentLabel = currentLabel;
}
public void setCurrentSlot(SlotLabel currentSlot) {
this.currentSlot = currentSlot;
}
public void setEditorController(EditorController editorController) {
this.editorController = editorController;
}
public void handleSlotClick(SlotLabel slotLabel) {
// Retrieve the address of the clicked slot
String address = slotLabel.getAddress();
currentLabel.setText(address);
xlModel.setCurrentAddress(address);
// Clear the status label
statusLabel.setText("");
// Reset the background color of the previous slot
currentSlot.setBackground(Color.WHITE);
// Set the background color of the clicked slot
slotLabel.setBackground(Color.YELLOW);
currentSlot = slotLabel;
editorController.setCurrentSlot(currentSlot);
// Retrieve the content of the clicked slot
String content = xlModel.getAddressContent(address);
// Set the content of the clicked slot in the editor
editor.setText(content);
}
}