Update src/main/java/dev/sillyangel/calc/utils.java
Some checks failed
Build the Jar / build (push) Failing after 9s
Some checks failed
Build the Jar / build (push) Failing after 9s
This commit is contained in:
@@ -1,6 +1,11 @@
|
|||||||
utils.java
|
|
||||||
package dev.sillyangel.calc;
|
package dev.sillyangel.calc;
|
||||||
|
|
||||||
|
import javax.swing.JOptionPane;
|
||||||
|
import java.awt.datatransfer.Clipboard;
|
||||||
|
import java.awt.datatransfer.DataFlavor;
|
||||||
|
import java.awt.datatransfer.StringSelection;
|
||||||
|
import java.awt.Toolkit;
|
||||||
|
|
||||||
public class utils {
|
public class utils {
|
||||||
// private double result;
|
// private double result;
|
||||||
// private boolean resultVisible;
|
// private boolean resultVisible;
|
||||||
@@ -97,8 +102,89 @@ public class utils {
|
|||||||
ci.operand1 = "";
|
ci.operand1 = "";
|
||||||
ci.operand2 = "";
|
ci.operand2 = "";
|
||||||
|
|
||||||
resultVisible = true;
|
ci.resultVisible = true;
|
||||||
display.setText(""+result);
|
ci.display.setText(""+ci.result);
|
||||||
saveToUndoStack(""+result);
|
saveToUndoStack(""+ci.result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void saveToUndoStack(String value) {
|
||||||
|
// Remove any states after current index (if user undid and then made a new action)
|
||||||
|
if (ci.undoIndex < ci.undoStack.size() - 1) {
|
||||||
|
ci.undoStack.subList(ci.undoIndex + 1, ci.undoStack.size()).clear();
|
||||||
|
}
|
||||||
|
ci.undoStack.add(value);
|
||||||
|
ci.undoIndex = ci.undoStack.size() - 1;
|
||||||
|
}
|
||||||
|
public void undoAction() {
|
||||||
|
if (ci.undoIndex > 0) {
|
||||||
|
ci.undoIndex--;
|
||||||
|
ci.display.setText(ci.undoStack.get(ci.undoIndex));
|
||||||
|
} else {
|
||||||
|
System.out.println("Nothing to undo");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void redoAction() {
|
||||||
|
if (ci.undoIndex < ci.undoStack.size() - 1) {
|
||||||
|
ci.undoIndex++;
|
||||||
|
ci.display.setText(ci.undoStack.get(ci.undoIndex));
|
||||||
|
} else {
|
||||||
|
System.out.println("Nothing to redo");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clipboard operations
|
||||||
|
public void cutAction() {
|
||||||
|
copyAction();
|
||||||
|
ci.display.setText("");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void copyAction() {
|
||||||
|
String text = ci.display.getText();
|
||||||
|
if (text != null && !text.isEmpty()) {
|
||||||
|
StringSelection selection = new StringSelection(text);
|
||||||
|
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
|
||||||
|
clipboard.setContents(selection, null);
|
||||||
|
System.out.println("Copied: " + text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void pasteAction() {
|
||||||
|
try {
|
||||||
|
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
|
||||||
|
String text = (String) clipboard.getData(DataFlavor.stringFlavor);
|
||||||
|
if (text != null && !text.isEmpty()) {
|
||||||
|
// Validate that pasted content is a valid number
|
||||||
|
try {
|
||||||
|
Double.parseDouble(text);
|
||||||
|
ci.display.setText(text);
|
||||||
|
saveToUndoStack(text);
|
||||||
|
} catch (NumberFormatException ex) {
|
||||||
|
JOptionPane.showMessageDialog(ci,
|
||||||
|
"Pasted content is not a valid number",
|
||||||
|
"Invalid Input", JOptionPane.ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception ex) {
|
||||||
|
System.err.println("Error pasting: " + ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void processDigit(String actionCommand) {
|
||||||
|
if (ci.resultVisible) {
|
||||||
|
ci.display.setText("");
|
||||||
|
ci.resultVisible = false;
|
||||||
|
}
|
||||||
|
String newValue = ci.display.getText() + actionCommand;
|
||||||
|
ci.display.setText(newValue);
|
||||||
|
saveToUndoStack(newValue);
|
||||||
|
}
|
||||||
|
public void setOperator(String daop) {
|
||||||
|
ci.operand1 = ci.display.getText();
|
||||||
|
ci.operator = daop;
|
||||||
|
ci.display.setText("");
|
||||||
|
}
|
||||||
|
public void clearEntry() {
|
||||||
|
ci.display.setText("");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user