Commit 91a440f6 authored by Nils Vreman's avatar Nils Vreman
Browse files

Added ScheduLearn project to my project folder

parents
package gui.menu.help;
public class ScheduleTab extends TabPanel {
private static final String[] listOptions = new String[] {
"Schedule",
"Solution",
"Checking Solution",
"Clearing Cells",
"Clearing Strategy"};
private static final String[] helpTexts = new String[] {
"Schedule",
"Solution",
"Checking Solution",
"Clearing Cells",
"Clearing Strategy"};
public ScheduleTab() {
super(listOptions, helpTexts);
}
}
package gui.menu.help;
public class StrategyTab extends TabPanel {
private static final String[] listOptions = new String[] {
"EDF",
"RMS"};
private static final String[] helpTexts = new String[] {
"EDF",
"RMS"};
public StrategyTab() {
super(listOptions, helpTexts);
}
}
package gui.menu.help;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.util.Map;
import java.util.HashMap;
public abstract class TabPanel extends JPanel implements EventListener {
private final String[] listOptions;
private Map<String, JTextField> helpTexts = new HashMap<>();
public TabPanel(String[] listOptions, String[] fieldTexts) {
super(new BorderLayout());
this.listOptions = listOptions;
OptionList list = new OptionList(listOptions);
list.addListener(this);
for (int i = 0; i < listOptions.length; i++) {
JTextField htf = new HelpTextField(fieldTexts[i]);
helpTexts.put(listOptions[i], htf);
}
add(BorderLayout.WEST, list);
add(BorderLayout.CENTER, new HelpTextField(""));
}
public void update(String tab) {
if (helpTexts.containsKey(tab)) {
add(BorderLayout.CENTER, helpTexts.get(tab));
revalidate();
repaint();
}
}
}
package gui.menu.help;
public class TasksetTab extends TabPanel {
private static final String[] listOptions = new String[] {
"Add Tasks",
"Remove Tasks",
"Change Tasks",
"Clear Taskset",
"Priorities",
"Load Taskset",
"Save Taskset"};
private static final String[] fieldTexts = new String[] {
"Add Tasks",
"Remove Tasks",
"Change Tasks",
"Clear Taskset",
"Priorities",
"Load Taskset",
"Save Taskset"};
public TasksetTab() {
super(listOptions, fieldTexts);
}
}
package gui.schedulegui;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class ColoredLabel extends JLabel {
public ColoredLabel(String text) {
this(text, Color.WHITE, SwingConstants.LEFT);
}
public ColoredLabel(String text, Color color) {
this(text, color, SwingConstants.LEFT);
}
public ColoredLabel(String text, Color color, int alignment) {
super(text, alignment);
setBackground(color);
setOpaque(true);
}
}
package gui.schedulegui;
import java.util.Observable;
import java.util.Observer;
import java.util.HashMap;
import java.util.Map;
import java.awt.Color;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.SwingConstants;
import javax.swing.BorderFactory;
import model.SchedulingModel;
import gui.GridPanel;
public abstract class Grid extends GridPanel implements Observer {
protected Map<String, SlotLabel> labelMap, activeLabels;
private SchedulingModel scheduleMdl;
public Grid(int rows, int cols, SchedulingModel scheduleMdl, String title) {
super(rows + 1, cols);
setBorder(BorderFactory.createTitledBorder(title));
this.scheduleMdl = scheduleMdl;
scheduleMdl.addObserver(this);
labelMap = new HashMap<>();
activeLabels = new HashMap<>();
for (int i = 1; i <= cols; i++) {
add(new ColoredLabel(String.valueOf(i), Color.LIGHT_GRAY, SwingConstants.CENTER));
}
for (int row = 1; row <= rows; row++) {
for (int col = 1; col <= cols; col++) {
SlotLabel label = new SlotLabel();
addListener(scheduleMdl, label); // Add mouse listener to this slot
add(label);
labelMap.put("(" + Integer.toString(row) + "," + Integer.toString(col) + ")", label);
}
}
}
/*
* Adds MouseListener to each label
*/
protected abstract void addListener(SchedulingModel scheduleMdl, SlotLabel s);
/*
* The action to perform in subclass when update happens
*/
protected abstract void updateAction(SchedulingModel scheudleMdl);
public void update(Observable obs, Object obj) { updateAction(scheduleMdl); }
/*
* Change activeLabels from subclass
*/
protected void putActiveLabel(String id, SlotLabel s) { activeLabels.put(id, s); }
protected void removeActiveLabel(String id) { activeLabels.remove(id); }
/*
* Return: string adress of slotlabel s
*/
protected String getAdress(SlotLabel s) {
for (String adress : labelMap.keySet()) {
if(labelMap.get(adress).equals(s)) return adress;
}
return "";
}
/*
* Parse task and slot to String id
*/
protected String parse(int task, int slot) {
return "(" + task + "," + slot + ")";
}
/*
* Parse String to task
*/
protected int parse(String id) {
String[] str = id.replaceAll("\\D+"," ").trim().split(" "); //Extracts all numbers in string form
return Integer.parseInt(str[0]);
}
/*
* Clears all the active labels from the map
*/
protected void clearActive() {
for (Map.Entry<String, SlotLabel> entry : activeLabels.entrySet()) {
int task = parse(entry.getKey());
entry.getValue().toggle(task);
}
activeLabels.clear();
}
}
package gui.schedulegui;
import static java.awt.Color.LIGHT_GRAY;
import static javax.swing.SwingConstants.RIGHT;
import gui.GridPanel;
public class RowLabels extends GridPanel {
public RowLabels(int rows) {
super(rows+2, 1);
add(new ColoredLabel("", LIGHT_GRAY, RIGHT));
add(new ColoredLabel("", LIGHT_GRAY, RIGHT));
for (int i = 1; i <= rows; i++) {
add(new ColoredLabel(String.valueOf(i), LIGHT_GRAY, RIGHT));
}
}
}
package gui.schedulegui;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import model.SchedulingModel;
import gui.CurrentSlot;
public class ScheduleGrid extends Grid {
private CurrentSlot currentSlot;
private SlotLabel currentSlotLabel;
public ScheduleGrid(int rows, int cols, SchedulingModel scheduleMdl, CurrentSlot currentSlot) {
super(rows, cols, scheduleMdl, "Schedule");
this.currentSlot = currentSlot;
}
/*
* Adds MouseListener to each label
*/
protected void addListener(SchedulingModel scheduleMdl, SlotLabel label) {
// Add mouseListener to label.
label.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// toggle color of current slot label
currentSlotLabel = label;
currentSlot.setCurrentSlot(getAdress(currentSlotLabel));
boolean activated = currentSlotLabel.toggle(currentSlot.getCurrentTask());
// If active: Add to activeLabels; else: remove
if (activated) {
putActiveLabel(currentSlot.toString(), currentSlotLabel);
} else {
removeActiveLabel(currentSlot.toString());
}
// Activate cell in scheduleMdl
scheduleMdl.activateCell(currentSlot.toString(), currentSlot.getCurrentTask(), activated);
}
});
}
/*
* If scheduleMdl is cleared: clear active cells.
*/
protected void updateAction(SchedulingModel scheduleMdl) {
if (scheduleMdl.isCleared()) {
clearActive();
}
}
}
package gui.schedulegui;
import javax.swing.JPanel;
import static java.awt.BorderLayout.CENTER;
import static java.awt.BorderLayout.WEST;
import static java.awt.BorderLayout.SOUTH;
import gui.BorderPanel;
import gui.CurrentSlot;
import model.SchedulingModel;
public class SchedulePanel extends BorderPanel {
// GridPanel, RowLabels, SlotLabels
public SchedulePanel(SchedulingModel scheduleMdl, CurrentSlot currentSlot, int rows, int cols) {
JPanel rowLabelsUp = new RowLabels(rows);
JPanel rowLabelsLow = new RowLabels(rows);
JPanel scheduleGrid = new ScheduleGrid(rows, cols, scheduleMdl, currentSlot);
JPanel solutionGrid = new SolutionGrid(rows, cols, scheduleMdl);
JPanel solutionPanel = new SolutionPanel(solutionGrid, rowLabelsLow);
add(WEST, rowLabelsUp);
add(CENTER, scheduleGrid);
add(SOUTH, solutionPanel);
}
}
package gui.schedulegui;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ShowSolutionButton extends JButton {
private boolean visible = false;
protected ShowSolutionButton(SolutionPanel solutionPanel) {
super("Show Solution");
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
solutionPanel.toggle();
if (visible) {
setText("Hide Solution");
visible = true;
} else {
setText("Show Solution");
visible = false;
}
}
});
}
}
package gui.schedulegui;
import java.awt.Color;
import model.ColorEnum;
public class SlotLabel extends ColoredLabel {
public SlotLabel() {
super(" ", Color.WHITE, RIGHT);
}
/*
* Toggles background color of this slot label based on white and color
*/
public boolean toggle(int task) {
if (this.getBackground() != Color.WHITE) {
this.setBackground(Color.WHITE);
return false;
} else {
this.setBackground(ColorEnum.valueOf(task).color());
return true;
}
}
/*
* Toggles background color of this slot label based on white and light grey
*/
public boolean toggle() {
if (this.getBackground() != Color.WHITE) {
this.setBackground(Color.WHITE);
return false;
} else {
this.setBackground(Color.LIGHT_GRAY);
return true;
}
}
}
package gui.schedulegui;
import java.awt.GridLayout;
import model.SchedulingModel;
public class SolutionGrid extends Grid {
public SolutionGrid(int rows, int cols, SchedulingModel scheduleMdl) {
super(rows, cols, scheduleMdl, "Solution");
updateAction(scheduleMdl);
}
/*
* No listener needed
*/
protected void addListener(SchedulingModel scheduleModel, SlotLabel s) {}
/*
* When scheduleMdl is updated, we update solution.
*/
protected void updateAction(SchedulingModel scheduleMdl) {
//Clears the solution panel before new solution
clearActive();
if (scheduleMdl.solutionIsValid()) {
//Add the solution
int[] solution = scheduleMdl.getSolution();
for (int col = 0; col < ((GridLayout)getLayout()).getColumns() && col < solution.length; col++) {
if (solution[col] != 0) {
String id = parse(solution[col], col + 1);
labelMap.get(id).toggle(solution[col]);
activeLabels.put(id, labelMap.get(id));
}
}
//Mark the hyperperiod
for (int row = 1;
row < ((GridLayout)getLayout()).getRows() &&
solution.length + 1 <= ((GridLayout)getLayout()).getColumns();
row++) {
String id = parse(row, solution.length + 1);
labelMap.get(id).toggle();
activeLabels.put(id, labelMap.get(id));
}
}
}
}
package gui.schedulegui;
import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import static java.awt.BorderLayout.NORTH;
import static java.awt.BorderLayout.CENTER;
import static java.awt.BorderLayout.WEST;
import model.SchedulingModel;
import model.Schedule;
import gui.BorderPanel;
public class SolutionPanel extends BorderPanel {
private JPanel solutionGrid, rowLabels;
private JButton button = new ShowSolutionButton(this);
private JTextField field;
private boolean visible = false;
public SolutionPanel(JPanel solutionGrid, JPanel rowLabels) {
this.solutionGrid = solutionGrid;
this.rowLabels = rowLabels;
field = new JTextField("Solution Hidden");
field.setHorizontalAlignment(JTextField.CENTER);
field.setFont(field.getFont().deriveFont(Font.BOLD));
field.setBackground(Color.LIGHT_GRAY);
add(NORTH, button);
add(CENTER, field);
setPreferredSize(new Dimension(400, 160));
}
/*
* Toggle solution panel so it is visible or invisible
*/
public void toggle() {
if (visible) {
hideSolution();
visible = false;
} else {
viewSolution();
visible = true;
}
}
private void hideSolution() {
BorderLayout layout = (BorderLayout) getLayout();
remove(layout.getLayoutComponent(CENTER));
remove(layout.getLayoutComponent(WEST));
add(CENTER, field);
revalidate();
repaint();
}
private void viewSolution() {
BorderLayout layout = (BorderLayout) getLayout();
remove(layout.getLayoutComponent(CENTER));
add(WEST, rowLabels);
add(CENTER, solutionGrid);
revalidate();
repaint();
}
}
package gui.tasksetgui;
import javax.swing.JButton;
import tasks.Taskset;
import model.Status;
public abstract class ActionButton extends JButton {
protected ActionButton(Taskset taskset, Status status, String title) {
super(title);
addEventHandler(taskset, status);
}
protected abstract void addEventHandler(Taskset taskset, Status status);
}
package gui.tasksetgui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Optional;
import tasks.Taskset;
import model.Status;
public class AddButton extends ActionButton {
protected AddButton(Taskset taskset, Status status) {
super(taskset, status, "Add Task");
}
protected void addEventHandler(Taskset taskset, Status status) {
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Optional<int[]> opValues = new AddDialog().generateDialog();
if (opValues.isPresent()) {
int[] values = opValues.get();
taskset.addTask(values[0], values[1], values[2]);
status.setStatus("Task Added");
}
} catch (NumberFormatException error) {
status.setStatus("Invalid Input");
}
}
});
}
}
package gui.tasksetgui;
public class AddDialog extends MultiInputPane {
public AddDialog() {
super(3);
}
protected String[] labelTitles() {
return new String[] {"ExecTime", "Period", "Deadline"};
}
protected String generateDialogMessage() {
return "Please Enter Attributes For New Task";
}
}
package gui.tasksetgui;
import tasks.Taskset;
import model.Status;
import gui.GridPanel;
public class ButtonPanel extends GridPanel {
public ButtonPanel(Taskset taskset, TasksetDisplay display, Status status) {
super(1, 2);
add(new AddButton(taskset, status));
add(new RemoveButton(display));
}
}
package gui.tasksetgui;
/*
* NOTE: TWO INTERFACES ARE NEEDED SUCH THAT THE UI ISN'T UPDATED WHEN MARKED.
*/
public interface MarkListener {