From 4f9cf33bb58099a227a624d2851c4a57426f0085 Mon Sep 17 00:00:00 2001
From: Nils Vreman <nils.vreman@gmail.com>
Date: Mon, 22 Oct 2018 09:52:23 +0200
Subject: [PATCH] Tried introducing empty taskset slots

---
 data/EDFIsWrong.sl                   |   4 +
 data/EDFnotRMS.sl                    |   3 +
 data/harmonic.sl                     |   4 +
 gui/tasksetgui/AbstractGrid.java     | 114 +++++++++++++++++++++++++++
 gui/tasksetgui/AddButton.java        |  20 +++++
 gui/tasksetgui/EmptyGrid.java        |  24 ++++++
 gui/tasksetgui/EmptyLabel.java       |  18 +++++
 gui/tasksetgui/EmptyLabelFilter.java |  21 +++++
 gui/tasksetgui/GridLabel.java        |  86 ++++++++++++++++++++
 gui/tasksetgui/TaskGrid.java         | 101 ++----------------------
 gui/tasksetgui/TaskLabel.java        |  73 +----------------
 gui/tasksetgui/TasksetDisplay.java   |  14 ++++
 12 files changed, 318 insertions(+), 164 deletions(-)
 create mode 100644 data/EDFIsWrong.sl
 create mode 100644 data/EDFnotRMS.sl
 create mode 100644 data/harmonic.sl
 create mode 100644 gui/tasksetgui/AbstractGrid.java
 create mode 100644 gui/tasksetgui/EmptyGrid.java
 create mode 100644 gui/tasksetgui/EmptyLabel.java
 create mode 100644 gui/tasksetgui/EmptyLabelFilter.java
 create mode 100644 gui/tasksetgui/GridLabel.java

diff --git a/data/EDFIsWrong.sl b/data/EDFIsWrong.sl
new file mode 100644
index 0000000..5e8b529
--- /dev/null
+++ b/data/EDFIsWrong.sl
@@ -0,0 +1,4 @@
+1,3,3
+1,4,3
+1,4,4
+1,6,6
diff --git a/data/EDFnotRMS.sl b/data/EDFnotRMS.sl
new file mode 100644
index 0000000..27d1ab4
--- /dev/null
+++ b/data/EDFnotRMS.sl
@@ -0,0 +1,3 @@
+2,5,3
+2,4,4
+2,20,15
diff --git a/data/harmonic.sl b/data/harmonic.sl
new file mode 100644
index 0000000..5d71b7a
--- /dev/null
+++ b/data/harmonic.sl
@@ -0,0 +1,4 @@
+1,2,2
+1,4,4
+1,8,8
+1,16,16
diff --git a/gui/tasksetgui/AbstractGrid.java b/gui/tasksetgui/AbstractGrid.java
new file mode 100644
index 0000000..59f10bb
--- /dev/null
+++ b/gui/tasksetgui/AbstractGrid.java
@@ -0,0 +1,114 @@
+package gui.tasksetgui;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.awt.Color;
+
+import gui.GridPanel;
+import gui.UpdateListener;
+
+public abstract class AbstractGrid extends GridPanel implements UpdateListener, MarkListener {
+
+    private String[] attributes = new String[] {"Pri", "e", "p", "d"};
+    private Map<String, GridLabel> labelMap;
+    private UpdateListener updateListener;
+    private MarkListener markListener;
+
+    // attributes should contain [prio, exectime, period, deadline]
+    public AbstractGrid(String[] taskAttributes) {
+        super(1, 4);
+        labelMap = new HashMap<>();
+        
+        for (int i = 0; i < 4; i++) {
+            GridLabel label = getLabel(taskAttributes[i]); //new GridLabel(taskAttributes[i]);
+            label.addUpdateListener(this);
+            label.addMarkListener(this);
+
+            if (i == 0) label.setEditable(false); // Forbids people from changing priority
+
+            add(label);
+            labelMap.put(attributes[i], label);
+        }
+    }
+    
+    public AbstractGrid(String prio, String exec, String period, String deadline) {
+        this(new String[] {prio, exec, period, deadline});
+    }
+
+    /*
+     * return: attributes from this gridLine
+     */
+    public int[] getAttributes() {
+        int[] attr = new int[4];
+        for (int i = 0; i < 4; i++) {
+            TaskLabel label = (TaskLabel)labelMap.get(attributes[i]);
+            attr[i] = Integer.parseInt(label.getText());
+        }
+        return attr;
+    }
+
+    /*
+     * Get label based on subclass type
+     */
+    protected abstract GridLabel getLabel(String taskAttributes);
+
+    /*
+     * Compares two taskgrid objects and returns the equality between the two
+     */
+    public boolean equals(AbstractGrid ag) {
+        return getAttributes()[0] == ag.getAttributes()[0];
+    }
+
+    /*
+     * The function to be performed when the observable class is updated.
+     * NOTE: TWO INTERFACES ARE NEEDED SUCH THAT THE UI ISN'T UPDATED ONLY WHEN MARKED.
+     */
+    public void update(Object obj) {
+        updateListener.update(this);
+    }
+
+    /*
+     * The function to be performed when the tasklabel class is marked.
+     * NOTE: TWO INTERFACES ARE NEEDED SUCH THAT THE UI ISN'T UPDATED ONLY WHEN MARKED.
+     */
+    public void markUpdate(Object obj) {
+        markListener.markUpdate(this);
+    }
+
+    /*
+     * Sets observer of this class 
+     * (Would have used observable superclass unless I already had a superclass)
+     */
+    public void addUpdateListener(UpdateListener updateListener) {
+        this.updateListener = updateListener;
+    }
+
+    /*
+     * Sets observer of this class 
+     * (Would have used observable superclass unless I already had a superclass)
+     */
+    public void addMarkListener(MarkListener markListener) {
+        this.markListener = markListener;
+    }
+
+    /*
+     * Marks all the tasklabels of this taskgrid.
+     */
+    public void mark() {
+        colorize(Color.YELLOW);
+    }
+
+    /*
+     * Unmarks all the tasklabels of this taskgrid.
+     */
+    public void unmark() {
+        colorize(Color.WHITE);
+    }
+
+    //Private help function to reduce code size
+    private void colorize(Color c) {
+        for (GridLabel tl : labelMap.values()) {
+            tl.setBackground(c);
+        }
+    }
+}
diff --git a/gui/tasksetgui/AddButton.java b/gui/tasksetgui/AddButton.java
index be83bd6..2381d2e 100644
--- a/gui/tasksetgui/AddButton.java
+++ b/gui/tasksetgui/AddButton.java
@@ -30,3 +30,23 @@ public class AddButton extends ActionButton {
     }
 
 }
+//package gui.tasksetgui;
+//
+//import java.awt.event.ActionEvent;
+//import java.awt.event.ActionListener;
+//import javax.swing.JButton;
+//
+//public class AddButton extends JButton {
+//    protected AddButton(TasksetDisplay display) {
+//        super("Add Task");
+//        addEventHandler(display);
+//    }
+//    
+//    protected void addEventHandler(TasksetDisplay display) {
+//        addActionListener(new ActionListener() {
+//            public void actionPerformed(ActionEvent e) {
+//                display.addEmpty();
+//            }
+//        });
+//    }
+//}
diff --git a/gui/tasksetgui/EmptyGrid.java b/gui/tasksetgui/EmptyGrid.java
new file mode 100644
index 0000000..236d62f
--- /dev/null
+++ b/gui/tasksetgui/EmptyGrid.java
@@ -0,0 +1,24 @@
+package gui.tasksetgui;
+
+public class EmptyGrid extends AbstractGrid {
+
+    // attributes should contain [prio, exectime, period, deadline]
+    public EmptyGrid(String[] taskAttributes) {
+        super(taskAttributes);
+    }
+
+    public EmptyGrid() {
+        super(new String[4]);
+    }
+    
+    public EmptyGrid(String prio, String exec, String period, String deadline) {
+        super(new String[] {prio, exec, period, deadline});
+    }
+
+    /*
+     * Get label based on subclass type
+     */
+    protected GridLabel getLabel(String taskAttribute) {
+        return new EmptyLabel(taskAttribute);
+    }
+}
diff --git a/gui/tasksetgui/EmptyLabel.java b/gui/tasksetgui/EmptyLabel.java
new file mode 100644
index 0000000..6921702
--- /dev/null
+++ b/gui/tasksetgui/EmptyLabel.java
@@ -0,0 +1,18 @@
+package gui.tasksetgui;
+
+public class EmptyLabel extends GridLabel {
+
+    public EmptyLabel() {
+        super("");
+    }
+
+    public EmptyLabel(String text) {
+        super("");
+    }
+    /*
+     * Sets the text filter for this label
+     */
+    protected void setFilter() {
+        setDocument(new EmptyLabelFilter());
+    }
+}
diff --git a/gui/tasksetgui/EmptyLabelFilter.java b/gui/tasksetgui/EmptyLabelFilter.java
new file mode 100644
index 0000000..0463840
--- /dev/null
+++ b/gui/tasksetgui/EmptyLabelFilter.java
@@ -0,0 +1,21 @@
+package gui.tasksetgui;
+
+import javax.swing.text.AttributeSet;
+import javax.swing.text.BadLocationException;
+import javax.swing.text.PlainDocument;
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+
+public class EmptyLabelFilter extends PlainDocument {
+    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
+        if (str == null) return;
+
+        String oldString = getText(0, getLength());
+        String newString = oldString.substring(0, offs) + str + oldString.substring(offs);
+        try {
+            Pattern p = Pattern.compile("[^0-9 ]");
+            Matcher m = p.matcher(newString);
+            if (!m.find()) super.insertString(offs, str, a);
+        } catch (NumberFormatException e) {}
+    }
+}
diff --git a/gui/tasksetgui/GridLabel.java b/gui/tasksetgui/GridLabel.java
new file mode 100644
index 0000000..2435930
--- /dev/null
+++ b/gui/tasksetgui/GridLabel.java
@@ -0,0 +1,86 @@
+package gui.tasksetgui;
+
+import java.awt.event.ActionListener;
+import java.awt.event.ActionEvent;
+import java.awt.event.MouseListener;
+import java.awt.event.MouseEvent;
+import java.awt.Color;
+import javax.swing.JTextField;
+
+import gui.UpdateListener;
+
+public abstract class GridLabel extends JTextField implements MouseListener {
+
+    private UpdateListener updateListener;
+    private MarkListener markListener;
+
+    public GridLabel(String text) {
+        // Front-end Characteristics
+        super(text);
+        setBackground(Color.WHITE);
+        setOpaque(true);
+        setFilter(); 
+        setText(text);
+
+        // Add Listener for when mouse does something with object
+        addMouseListener(this);
+
+        // Add Listener that does something with object when <Enter> is pressed
+        addActionListener(new ActionListener() {
+            public void actionPerformed(ActionEvent e) {
+                if (getText().matches("[0-9]+")) {
+                    updateListener.update();
+                }
+            }
+        });
+    }
+
+    /*
+     * Sets the text filter for this label
+     */
+    protected abstract void setFilter();
+
+    /*
+     * Sets observer of this class 
+     * (Would have used observable superclass unless I already had a superclass)
+     */
+    public void addUpdateListener(UpdateListener updateListener) {
+        this.updateListener = updateListener;
+    }
+
+    /*
+     * Sets observer of this class 
+     * (Would have used observable superclass unless I already had a superclass)
+     */
+    public void addMarkListener(MarkListener markListener) {
+        this.markListener = markListener;
+    }
+    
+    // MouseListener Functions.
+    /*
+     * Invoked when the mouse button has been clicked (pressed and released) on a component.
+     */
+    public void mouseClicked(MouseEvent e) {
+        markListener.markUpdate(this);
+    }
+
+    /*
+     * Invoked when a mouse button has been pressed on a component.
+     */
+    public void mousePressed(MouseEvent e) {}
+
+    /*
+     * Invoked when a mouse button has been released on a component.
+     */
+    public void mouseReleased(MouseEvent e) {}
+
+    /*
+     * Invoked when the mouse enters a component.
+     */
+    public void mouseEntered(MouseEvent e) {}
+
+    /*
+     * Invoked when the mouse exits a component.
+     */
+    public void mouseExited(MouseEvent e) {}
+}
diff --git a/gui/tasksetgui/TaskGrid.java b/gui/tasksetgui/TaskGrid.java
index a8770e2..4e2c373 100644
--- a/gui/tasksetgui/TaskGrid.java
+++ b/gui/tasksetgui/TaskGrid.java
@@ -1,109 +1,20 @@
 package gui.tasksetgui;
 
-import java.util.HashMap;
-import java.util.Map;
-import java.awt.Color;
-
-import gui.GridPanel;
-import gui.UpdateListener;
-
-public class TaskGrid extends GridPanel implements UpdateListener, MarkListener {
-
-    private String[] attributes = new String[] {"Pri", "e", "p", "d"};
-    private Map<String, TaskLabel> labelMap;
-    private UpdateListener updateListener;
-    private MarkListener markListener;
+public class TaskGrid extends AbstractGrid {
 
     // attributes should contain [prio, exectime, period, deadline]
     public TaskGrid(String[] taskAttributes) {
-        super(1, 4);
-        labelMap = new HashMap<>();
-        
-        for (int i = 0; i < 4; i++) {
-            TaskLabel label = new TaskLabel(taskAttributes[i]);
-            label.addUpdateListener(this);
-            label.addMarkListener(this);
-
-            if (i == 0) label.setEditable(false); // Forbids people from changing priority
-
-            add(label);
-            labelMap.put(attributes[i], label);
-        }
+        super(taskAttributes);
     }
     
     public TaskGrid(String prio, String exec, String period, String deadline) {
-        this(new String[] {prio, exec, period, deadline});
-    }
-
-    /*
-     * return: attributes from this gridLine
-     */
-    public int[] getAttributes() {
-        int[] attr = new int[4];
-        for (int i = 0; i < 4; i++) {
-            TaskLabel label = labelMap.get(attributes[i]);
-            attr[i] = Integer.parseInt(label.getText());
-        }
-        return attr;
+        super(new String[] {prio, exec, period, deadline});
     }
 
     /*
-     * Compares two taskgrid objects and returns the equality between the two
+     * Get label based on subclass type
      */
-    public boolean equals(TaskGrid tg) {
-        return getAttributes()[0] == tg.getAttributes()[0];
-    }
-
-    /*
-     * The function to be performed when the observable class is updated.
-     * NOTE: TWO INTERFACES ARE NEEDED SUCH THAT THE UI ISN'T UPDATED ONLY WHEN MARKED.
-     */
-    public void update(Object obj) {
-        updateListener.update(this);
-    }
-
-    /*
-     * The function to be performed when the tasklabel class is marked.
-     * NOTE: TWO INTERFACES ARE NEEDED SUCH THAT THE UI ISN'T UPDATED ONLY WHEN MARKED.
-     */
-    public void markUpdate(Object obj) {
-        markListener.markUpdate(this);
-    }
-
-    /*
-     * Sets observer of this class 
-     * (Would have used observable superclass unless I already had a superclass)
-     */
-    public void addUpdateListener(UpdateListener updateListener) {
-        this.updateListener = updateListener;
-    }
-
-    /*
-     * Sets observer of this class 
-     * (Would have used observable superclass unless I already had a superclass)
-     */
-    public void addMarkListener(MarkListener markListener) {
-        this.markListener = markListener;
-    }
-
-    /*
-     * Marks all the tasklabels of this taskgrid.
-     */
-    public void mark() {
-        colorize(Color.YELLOW);
-    }
-
-    /*
-     * Unmarks all the tasklabels of this taskgrid.
-     */
-    public void unmark() {
-        colorize(Color.WHITE);
-    }
-
-    //Private help function to reduce code size
-    private void colorize(Color c) {
-        for (TaskLabel tl : labelMap.values()) {
-            tl.setBackground(c);
-        }
+    protected GridLabel getLabel(String taskAttribute) {
+        return new TaskLabel(taskAttribute);
     }
 }
diff --git a/gui/tasksetgui/TaskLabel.java b/gui/tasksetgui/TaskLabel.java
index 1de7b6d..d837fa2 100644
--- a/gui/tasksetgui/TaskLabel.java
+++ b/gui/tasksetgui/TaskLabel.java
@@ -1,81 +1,16 @@
 package gui.tasksetgui;
 
-import java.awt.event.ActionListener;
-import java.awt.event.ActionEvent;
-import java.awt.event.MouseListener;
-import java.awt.event.MouseEvent;
-import java.awt.Color;
-import javax.swing.JTextField;
-
-import gui.UpdateListener;
-
-public class TaskLabel extends JTextField implements MouseListener {
-
-    private UpdateListener updateListener;
-    private MarkListener markListener;
+public class TaskLabel extends GridLabel {
 
     public TaskLabel(String text) {
         // Front-end Characteristics
         super(text);
-        setBackground(Color.WHITE);
-        setOpaque(true);
-        setDocument(new TaskLabelFilter());
-        setText(text);
-
-        // Add Listener for when mouse does something with object
-        addMouseListener(this);
-
-        // Add Listener that does something with object when <Enter> is pressed
-        addActionListener(new ActionListener() {
-            public void actionPerformed(ActionEvent e) {
-                if (getText().matches("[0-9]+")) {
-                    updateListener.update();
-                }
-            }
-        });
     }
 
     /*
-     * Sets observer of this class 
-     * (Would have used observable superclass unless I already had a superclass)
-     */
-    public void addUpdateListener(UpdateListener updateListener) {
-        this.updateListener = updateListener;
-    }
-
-    /*
-     * Sets observer of this class 
-     * (Would have used observable superclass unless I already had a superclass)
-     */
-    public void addMarkListener(MarkListener markListener) {
-        this.markListener = markListener;
-    }
-    
-    // MouseListener Functions.
-    /*
-     * Invoked when the mouse button has been clicked (pressed and released) on a component.
+     * Sets the text filter for this label
      */
-    public void mouseClicked(MouseEvent e) {
-        markListener.markUpdate(this);
+    protected void setFilter() {
+        setDocument(new TaskLabelFilter());
     }
-
-    /*
-     * Invoked when a mouse button has been pressed on a component.
-     */
-    public void mousePressed(MouseEvent e) {}
-
-    /*
-     * Invoked when a mouse button has been released on a component.
-     */
-    public void mouseReleased(MouseEvent e) {}
-
-    /*
-     * Invoked when the mouse enters a component.
-     */
-    public void mouseEntered(MouseEvent e) {}
-
-    /*
-     * Invoked when the mouse exits a component.
-     */
-    public void mouseExited(MouseEvent e) {}
 }
diff --git a/gui/tasksetgui/TasksetDisplay.java b/gui/tasksetgui/TasksetDisplay.java
index 2dd8672..1d8730c 100644
--- a/gui/tasksetgui/TasksetDisplay.java
+++ b/gui/tasksetgui/TasksetDisplay.java
@@ -23,6 +23,7 @@ public class TasksetDisplay extends BoxPanel implements Observer, UpdateListener
     private TaskGridHeader header = new TaskGridHeader(" Priority", "Exectime", " Period", "Deadline");
     private List<TaskGrid> gridList = new ArrayList<>();
     private Optional<TaskGrid> marked = Optional.empty(); // Used to indicate which taskgrid is marked
+    private Optional<TaskGrid> emptyTask = Optional.empty(); // Used to indicate which taskgrid is marked
     private final int offset = 200;
 
     public TasksetDisplay(Taskset taskset) {
@@ -114,6 +115,19 @@ public class TasksetDisplay extends BoxPanel implements Observer, UpdateListener
         } catch (NoSuchElementException e) {}
     }
 
+    /*
+     * Add empty grid. Not if one already exists
+     */
+    public void addEmpty() {
+        if (!emptyTask.isPresent()) {
+            /*
+            emptyTask = Optional.of(new EmptyGrid());
+            marked = emptyTask;
+            markUpdate(emptyTask.get());
+            */
+        }
+    }
+
     // Private help method.
     private String parse(int i) {
         return String.valueOf(i);
-- 
GitLab