Skip to content

Commit

Permalink
generify SolutionPanel
Browse files Browse the repository at this point in the history
  • Loading branch information
ge0ffrey committed Feb 17, 2016
1 parent 235e585 commit 7cb553c
Show file tree
Hide file tree
Showing 25 changed files with 113 additions and 212 deletions.
Expand Up @@ -58,7 +58,7 @@
import org.optaplanner.examples.common.swingui.SolutionPanel;
import org.optaplanner.swing.impl.TangoColorFactory;

public class CheapTimePanel extends SolutionPanel {
public class CheapTimePanel extends SolutionPanel<CheapTimeSolution> {

private StableTaskAssignmentComparator stableTaskAssignmentComparator = new StableTaskAssignmentComparator();
private GroupByMachineTaskAssignmentComparator groupByMachineTaskAssignmentComparator = new GroupByMachineTaskAssignmentComparator();
Expand All @@ -74,7 +74,7 @@ public CheapTimePanel() {
groupByMachineCheckBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updatePanel(solutionBusiness.getSolution());
updatePanel(getSolution());
validate();
}
});
Expand All @@ -85,14 +85,11 @@ public boolean isRefreshScreenDuringSolving() {
return true;
}

private CheapTimeSolution getCheapTimeSolution() {
return (CheapTimeSolution) solutionBusiness.getSolution();
}

public void resetPanel(Solution solution) {
@Override
public void resetPanel(CheapTimeSolution solution) {
removeAll();
add(groupByMachineCheckBox, BorderLayout.NORTH);
ChartPanel chartPanel = new ChartPanel(createChart((CheapTimeSolution) solution));
ChartPanel chartPanel = new ChartPanel(createChart(solution));
add(chartPanel, BorderLayout.CENTER);
}

Expand Down
Expand Up @@ -47,7 +47,7 @@
import org.optaplanner.examples.common.swingui.SolutionPanel;
import org.optaplanner.examples.common.swingui.components.LabeledComboBoxRenderer;

public class CloudBalancingPanel extends SolutionPanel {
public class CloudBalancingPanel extends SolutionPanel<CloudBalance> {

public static final String LOGO_PATH = "/org/optaplanner/examples/cloudbalancing/swingui/cloudBalancingLogo.png";

Expand Down Expand Up @@ -176,12 +176,8 @@ public boolean isRefreshScreenDuringSolving() {
return true;
}

private CloudBalance getCloudBalance() {
return (CloudBalance) solutionBusiness.getSolution();
}

public void resetPanel(Solution solution) {
CloudBalance cloudBalance = (CloudBalance) solution;
@Override
public void resetPanel(CloudBalance cloudBalance) {
maximumComputerCpuPower = 0;
maximumComputerMemory = 0;
maximumComputerNetworkBandwidth = 0;
Expand All @@ -206,12 +202,11 @@ public void resetPanel(Solution solution) {
unassignedPanel = new CloudComputerPanel(this, null);
computersPanel.add(unassignedPanel);
computerToPanelMap.put(null, unassignedPanel);
updatePanel(solution);
updatePanel(cloudBalance);
}

@Override
public void updatePanel(Solution solution) {
CloudBalance cloudBalance = (CloudBalance) solution;
public void updatePanel(CloudBalance cloudBalance) {
Set<CloudComputer> deadCloudComputerSet = new LinkedHashSet<CloudComputer>(computerToPanelMap.keySet());
deadCloudComputerSet.remove(null);
for (CloudComputer computer : cloudBalance.getComputerList()) {
Expand Down Expand Up @@ -354,7 +349,7 @@ public CloudProcessAction(CloudProcess process) {
public void actionPerformed(ActionEvent e) {
JPanel listFieldsPanel = new JPanel(new GridLayout(1, 2));
listFieldsPanel.add(new JLabel("Computer:"));
List<CloudComputer> computerList = getCloudBalance().getComputerList();
List<CloudComputer> computerList = getSolution().getComputerList();
// Add 1 to array size to add null, which makes the entity unassigned
JComboBox computerListField = new JComboBox(
computerList.toArray(new Object[computerList.size() + 1]));
Expand Down
Expand Up @@ -46,7 +46,7 @@
import org.optaplanner.examples.common.swingui.SolutionPanel;
import org.optaplanner.examples.common.swingui.components.LabeledComboBoxRenderer;

public class CoachShuttleGatheringPanel extends SolutionPanel {
public class CoachShuttleGatheringPanel extends SolutionPanel<CoachShuttleGatheringSolution> {

// TODO Create logo
public static final String LOGO_PATH = null;
Expand All @@ -73,18 +73,13 @@ public boolean isRefreshScreenDuringSolving() {
return true;
}

public CoachShuttleGatheringSolution getCoachShuttleGatheringSolution() {
return (CoachShuttleGatheringSolution) solutionBusiness.getSolution();
}

public void resetPanel(Solution s) {
CoachShuttleGatheringSolution solution = (CoachShuttleGatheringSolution) s;
@Override
public void resetPanel(CoachShuttleGatheringSolution solution) {
coachShuttleGatheringWorldPanel.resetPanel(solution);
}

@Override
public void updatePanel(Solution s) {
CoachShuttleGatheringSolution solution = (CoachShuttleGatheringSolution) s;
public void updatePanel(CoachShuttleGatheringSolution solution) {
coachShuttleGatheringWorldPanel.updatePanel(solution);
}

Expand Down
Expand Up @@ -51,7 +51,7 @@ public CoachShuttleGatheringWorldPanel(CoachShuttleGatheringPanel coachShuttleGa
@Override
public void componentResized(ComponentEvent e) {
// TODO Not thread-safe during solving
CoachShuttleGatheringSolution solution = CoachShuttleGatheringWorldPanel.this.coachShuttleGatheringPanel.getCoachShuttleGatheringSolution();
CoachShuttleGatheringSolution solution = CoachShuttleGatheringWorldPanel.this.coachShuttleGatheringPanel.getSolution();
if (solution != null) {
resetPanel(solution);
}
Expand Down
Expand Up @@ -100,7 +100,7 @@ protected Solver<Solution_> createSolver() {
return solverFactory.buildSolver();
}

protected abstract SolutionPanel createSolutionPanel();
protected abstract SolutionPanel<Solution_> createSolutionPanel();

protected abstract SolutionDao createSolutionDao();

Expand Down
Expand Up @@ -28,7 +28,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class SolutionPanel extends JPanel implements Scrollable {
public abstract class SolutionPanel<Solution_ extends Solution> extends JPanel implements Scrollable {

protected static final String USAGE_EXPLANATION_PATH = "/org/optaplanner/examples/common/swingui/exampleUsageExplanation.png";
// Size fits into screen resolution 1024*768
Expand All @@ -37,7 +37,7 @@ public abstract class SolutionPanel extends JPanel implements Scrollable {
protected final transient Logger logger = LoggerFactory.getLogger(getClass());

protected SolverAndPersistenceFrame solverAndPersistenceFrame;
protected SolutionBusiness solutionBusiness;
protected SolutionBusiness<Solution_> solutionBusiness;

public SolverAndPersistenceFrame getSolverAndPersistenceFrame() {
return solverAndPersistenceFrame;
Expand All @@ -47,11 +47,11 @@ public void setSolverAndPersistenceFrame(SolverAndPersistenceFrame solverAndPers
this.solverAndPersistenceFrame = solverAndPersistenceFrame;
}

public SolutionBusiness getSolutionBusiness() {
public SolutionBusiness<Solution_> getSolutionBusiness() {
return solutionBusiness;
}

public void setSolutionBusiness(SolutionBusiness solutionBusiness) {
public void setSolutionBusiness(SolutionBusiness<Solution_> solutionBusiness) {
this.solutionBusiness = solutionBusiness;
}

Expand All @@ -67,12 +67,16 @@ public boolean isRefreshScreenDuringSolving() {
return false;
}

public abstract void resetPanel(Solution solution);
public abstract void resetPanel(Solution_ solution);

public void updatePanel(Solution solution) {
public void updatePanel(Solution_ solution) {
resetPanel(solution);
}

public Solution_ getSolution() {
return (Solution_) solutionBusiness.getSolution();
}

public Dimension getPreferredScrollableViewportSize() {
return PREFERRED_SCROLLABLE_VIEWPORT_SIZE;
}
Expand Down Expand Up @@ -105,7 +109,7 @@ public void doProblemFactChange(ProblemFactChange problemFactChange) {

public void doProblemFactChange(ProblemFactChange problemFactChange, boolean reset) {
solutionBusiness.doProblemFactChange(problemFactChange);
Solution solution = solutionBusiness.getSolution();
Solution_ solution = getSolution();
if (reset) {
resetPanel(solution);
} else {
Expand Down
Expand Up @@ -76,7 +76,7 @@ public class SolverAndPersistenceFrame<Solution_ extends Solution> extends JFram

private final SolutionBusiness<Solution_> solutionBusiness;

private SolutionPanel solutionPanel;
private SolutionPanel<Solution_> solutionPanel;
private ConstraintMatchesDialog constraintMatchesDialog;

private JPanel quickOpenUnsolvedPanel;
Expand All @@ -97,7 +97,8 @@ public class SolverAndPersistenceFrame<Solution_ extends Solution> extends JFram
private JTextField scoreField;
private ShowConstraintMatchesDialogAction showConstraintMatchesDialogAction;

public SolverAndPersistenceFrame(SolutionBusiness<Solution_> solutionBusiness, SolutionPanel solutionPanel) {
public SolverAndPersistenceFrame(SolutionBusiness<Solution_> solutionBusiness,
SolutionPanel<Solution_> solutionPanel) {
super(solutionBusiness.getAppName() + " OptaPlanner example");
this.solutionBusiness = solutionBusiness;
this.solutionPanel = solutionPanel;
Expand All @@ -119,7 +120,7 @@ public void windowClosing(WindowEvent e) {
}

public void bestSolutionChanged() {
Solution solution = solutionBusiness.getSolution();
Solution_ solution = solutionBusiness.getSolution();
if (refreshScreenDuringSolvingCheckBox.isSelected()) {
solutionPanel.updatePanel(solution);
validate(); // TODO remove me?
Expand Down Expand Up @@ -614,7 +615,7 @@ private void setSolvingState(boolean solving) {
}

public void resetScreen() {
Solution solution = solutionBusiness.getSolution();
Solution_ solution = solutionBusiness.getSolution();
solutionPanel.resetPanel(solution);
validate();
refreshScoreField(solution);
Expand Down
Expand Up @@ -51,7 +51,7 @@
import static org.optaplanner.examples.common.swingui.timetable.TimeTablePanel.HeaderColumnKey.*;
import static org.optaplanner.examples.common.swingui.timetable.TimeTablePanel.HeaderRowKey.*;

public class CurriculumCoursePanel extends SolutionPanel {
public class CurriculumCoursePanel extends SolutionPanel<CourseSchedule> {

public static final String LOGO_PATH = "/org/optaplanner/examples/curriculumcourse/swingui/curriculumCourseLogo.png";

Expand Down Expand Up @@ -82,15 +82,11 @@ public boolean isRefreshScreenDuringSolving() {
return true;
}

private CourseSchedule getCourseSchedule() {
return (CourseSchedule) solutionBusiness.getSolution();
}

public void resetPanel(Solution solution) {
@Override
public void resetPanel(CourseSchedule courseSchedule) {
roomsPanel.reset();
teachersPanel.reset();
curriculaPanel.reset();
CourseSchedule courseSchedule = (CourseSchedule) solution;
defineGrid(courseSchedule);
fillCells(courseSchedule);
repaint(); // Hack to force a repaint of TimeTableLayout during "refresh screen while solving"
Expand Down Expand Up @@ -241,15 +237,16 @@ public LectureAction(Lecture lecture) {
public void actionPerformed(ActionEvent e) {
JPanel listFieldsPanel = new JPanel(new GridLayout(3, 2));
listFieldsPanel.add(new JLabel("Period:"));
List<Period> periodList = getCourseSchedule().getPeriodList();
CourseSchedule courseSchedule = getSolution();
List<Period> periodList = courseSchedule.getPeriodList();
// Add 1 to array size to add null, which makes the entity unassigned
JComboBox periodListField = new JComboBox(
periodList.toArray(new Object[periodList.size() + 1]));
LabeledComboBoxRenderer.applyToComboBox(periodListField);
periodListField.setSelectedItem(lecture.getPeriod());
listFieldsPanel.add(periodListField);
listFieldsPanel.add(new JLabel("Room:"));
List<Room> roomList = getCourseSchedule().getRoomList();
List<Room> roomList = courseSchedule.getRoomList();
// Add 1 to array size to add null, which makes the entity unassigned
JComboBox roomListField = new JComboBox(
roomList.toArray(new Object[roomList.size() + 1]));
Expand Down
Expand Up @@ -49,7 +49,7 @@
import org.optaplanner.examples.dinnerparty.domain.SeatDesignation;
import org.optaplanner.examples.dinnerparty.domain.Table;

public class DinnerPartyPanel extends SolutionPanel {
public class DinnerPartyPanel extends SolutionPanel<DinnerParty> {

public static final String LOGO_PATH = "/org/optaplanner/examples/dinnerparty/swingui/dinnerPartyLogo.png";
public static final int MALE_FEMALE_ICON_VARIATION = 5;
Expand Down Expand Up @@ -100,13 +100,9 @@ public boolean isRefreshScreenDuringSolving() {
return true;
}

private DinnerParty getDinnerParty() {
return (DinnerParty) solutionBusiness.getSolution();
}

public void resetPanel(Solution solution) {
@Override
public void resetPanel(DinnerParty dinnerParty) {
removeAll();
DinnerParty dinnerParty = (DinnerParty) solution;
TangoColorFactory tangoColorFactory = new TangoColorFactory();
gridLayout.setColumns((int) Math.ceil(Math.sqrt(dinnerParty.getTableList().size())));
Map<Seat, SeatPanel> seatPanelMap = new HashMap<Seat, SeatPanel>(dinnerParty.getSeatList().size());
Expand Down Expand Up @@ -228,7 +224,7 @@ public SeatDesignationAction(SeatDesignation seatDesignation) {
}

public void actionPerformed(ActionEvent e) {
List<SeatDesignation> seatDesignationList = getDinnerParty().getSeatDesignationList();
List<SeatDesignation> seatDesignationList = getSolution().getSeatDesignationList();
// Add 1 to array size to add null, which makes the entity unassigned
JComboBox seatDesignationListField = new JComboBox(
seatDesignationList.toArray(new Object[seatDesignationList.size() + 1]));
Expand Down
Expand Up @@ -52,7 +52,7 @@
import static org.optaplanner.examples.common.swingui.timetable.TimeTablePanel.HeaderColumnKey.*;
import static org.optaplanner.examples.common.swingui.timetable.TimeTablePanel.HeaderRowKey.*;

public class ExaminationPanel extends SolutionPanel {
public class ExaminationPanel extends SolutionPanel<Examination> {

public static final String LOGO_PATH = "/org/optaplanner/examples/examination/swingui/examinationLogo.png";

Expand Down Expand Up @@ -88,7 +88,7 @@ public void actionPerformed(ActionEvent e) {
return;
}
institutionParametrizationDialog.setInstitutionParametrization(
getExamination().getInstitutionParametrization());
getSolution().getInstitutionParametrization());
institutionParametrizationDialog.setVisible(true);
}
};
Expand All @@ -113,13 +113,9 @@ public boolean isRefreshScreenDuringSolving() {
return true;
}

private Examination getExamination() {
return (Examination) solutionBusiness.getSolution();
}

public void resetPanel(Solution solution) {
@Override
public void resetPanel(Examination examination) {
roomsPanel.reset();
Examination examination = (Examination) solution;
refreshMaximums(examination);
defineGrid(examination);
fillCells(examination);
Expand Down Expand Up @@ -235,15 +231,16 @@ public ExamAction(Exam exam) {
public void actionPerformed(ActionEvent e) {
JPanel listFieldsPanel = new JPanel(new GridLayout(2, 2));
listFieldsPanel.add(new JLabel("Period:"));
List<Period> periodList = getExamination().getPeriodList();
Examination examination = getSolution();
List<Period> periodList = examination.getPeriodList();
// Add 1 to array size to add null, which makes the entity unassigned
JComboBox periodListField = new JComboBox(
periodList.toArray(new Object[periodList.size() + 1]));
LabeledComboBoxRenderer.applyToComboBox(periodListField);
periodListField.setSelectedItem(exam.getPeriod());
listFieldsPanel.add(periodListField);
listFieldsPanel.add(new JLabel("Room:"));
List<Room> roomList = getExamination().getRoomList();
List<Room> roomList = examination.getRoomList();
// Add 1 to array size to add null, which makes the entity unassigned
JComboBox roomListField = new JComboBox(
roomList.toArray(new Object[roomList.size() + 1]));
Expand Down
Expand Up @@ -50,7 +50,7 @@
import static org.optaplanner.examples.common.swingui.timetable.TimeTablePanel.HeaderColumnKey.*;
import static org.optaplanner.examples.common.swingui.timetable.TimeTablePanel.HeaderRowKey.*;

public class InvestmentPanel extends SolutionPanel {
public class InvestmentPanel extends SolutionPanel<InvestmentSolution> {

public static final String LOGO_PATH = "/org/optaplanner/examples/investment/swingui/investmentLogo.png";

Expand Down Expand Up @@ -105,16 +105,12 @@ public boolean isRefreshScreenDuringSolving() {
return true;
}

private InvestmentSolution getInvestmentSolution() {
return (InvestmentSolution) solutionBusiness.getSolution();
}

public void resetPanel(Solution s) {
@Override
public void resetPanel(InvestmentSolution solution) {
ignoreChangeEvents = true;
assetClassPanel.reset();
regionPanel.reset();
sectorPanel.reset();
InvestmentSolution solution = (InvestmentSolution) s;
InvestmentParametrization parametrization = solution.getParametrization();
standardDeviationMaximumField.setValue((double) parametrization.getStandardDeviationMillisMaximum() / 1000.0);
defineGrid(solution);
Expand Down

0 comments on commit 7cb553c

Please sign in to comment.