Skip to content

Commit

Permalink
PLANNER-423 Fix Combobox selection rollover effect
Browse files Browse the repository at this point in the history
  • Loading branch information
ge0ffrey committed Oct 23, 2015
1 parent f08a5b1 commit df1587a
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ public void actionPerformed(ActionEvent e) {
// Add 1 to array size to add null, which makes the entity unassigned
JComboBox computerListField = new JComboBox(
computerList.toArray(new Object[computerList.size() + 1]));
computerListField.setRenderer(new LabeledComboBoxRenderer());
LabeledComboBoxRenderer.applyToComboBox(computerListField);
computerListField.setSelectedItem(process.getComputer());
listFieldsPanel.add(computerListField);
int result = JOptionPane.showConfirmDialog(CloudBalancingPanel.this.getRootPane(), listFieldsPanel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,33 @@
package org.optaplanner.examples.common.swingui.components;

import java.awt.Component;
import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.plaf.basic.BasicComboBoxRenderer;

/**
* Display the user-friendly {@link Labeled#getLabel()} instead of the developer-friendly {@link Object#toString()}.
*/
public class LabeledComboBoxRenderer extends BasicComboBoxRenderer {
public class LabeledComboBoxRenderer implements ListCellRenderer {

public static void applyToComboBox(JComboBox comboBox) {
comboBox.setRenderer(new LabeledComboBoxRenderer(comboBox.getRenderer()));
}

private final ListCellRenderer originalListCellRenderer;

public LabeledComboBoxRenderer(ListCellRenderer originalListCellRenderer) {
this.originalListCellRenderer = originalListCellRenderer;
}

@Override
public Component getListCellRendererComponent(JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
if (isSelected) {
setBackground(list.getSelectionBackground());
setForeground(list.getSelectionForeground());
} else {
setBackground(list.getBackground());
setForeground(list.getForeground());
}
setFont(list.getFont());

setText((value == null) ? "" : ((Labeled) value).getLabel());
return this;
String label = (value == null) ? "" : ((Labeled) value).getLabel();
return originalListCellRenderer.getListCellRendererComponent(list, label, index, isSelected, cellHasFocus);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,15 @@ public void actionPerformed(ActionEvent e) {
// Add 1 to array size to add null, which makes the entity unassigned
JComboBox periodListField = new JComboBox(
periodList.toArray(new Object[periodList.size() + 1]));
periodListField.setRenderer(new LabeledComboBoxRenderer());
LabeledComboBoxRenderer.applyToComboBox(periodListField);
periodListField.setSelectedItem(lecture.getPeriod());
listFieldsPanel.add(periodListField);
listFieldsPanel.add(new JLabel("Room:"));
List<Room> roomList = getCourseSchedule().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]));
roomListField.setRenderer(new LabeledComboBoxRenderer());
LabeledComboBoxRenderer.applyToComboBox(roomListField);
roomListField.setSelectedItem(lecture.getRoom());
listFieldsPanel.add(roomListField);
listFieldsPanel.add(new JLabel("Locked:"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public void actionPerformed(ActionEvent e) {
// Add 1 to array size to add null, which makes the entity unassigned
JComboBox seatDesignationListField = new JComboBox(
seatDesignationList.toArray(new Object[seatDesignationList.size() + 1]));
seatDesignationListField.setRenderer(new LabeledComboBoxRenderer());
LabeledComboBoxRenderer.applyToComboBox(seatDesignationListField);
seatDesignationListField.setSelectedItem(seatDesignation);
int result = JOptionPane.showConfirmDialog(DinnerPartyPanel.this.getRootPane(), seatDesignationListField,
"Select seat designation to switch with", JOptionPane.OK_CANCEL_OPTION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,15 @@ public void actionPerformed(ActionEvent e) {
// Add 1 to array size to add null, which makes the entity unassigned
JComboBox periodListField = new JComboBox(
periodList.toArray(new Object[periodList.size() + 1]));
periodListField.setRenderer(new LabeledComboBoxRenderer());
LabeledComboBoxRenderer.applyToComboBox(periodListField);
periodListField.setSelectedItem(exam.getPeriod());
listFieldsPanel.add(periodListField);
listFieldsPanel.add(new JLabel("Room:"));
List<Room> roomList = getExamination().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]));
roomListField.setRenderer(new LabeledComboBoxRenderer());
LabeledComboBoxRenderer.applyToComboBox(roomListField);
roomListField.setSelectedItem(exam.getRoom());
listFieldsPanel.add(roomListField);
int result = JOptionPane.showConfirmDialog(ExaminationPanel.this.getRootPane(), listFieldsPanel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public void actionPerformed(ActionEvent e) {
// Add 1 to array size to add null, which makes the entity unassigned
JComboBox machineListField = new JComboBox(
machineList.toArray(new Object[machineList.size() + 1]));
machineListField.setRenderer(new LabeledComboBoxRenderer());
LabeledComboBoxRenderer.applyToComboBox(machineListField);
machineListField.setSelectedItem(processAssignment.getMachine());
int result = JOptionPane.showConfirmDialog(MachineReassignmentPanel.this.getRootPane(), machineListField,
"Select machine", JOptionPane.OK_CANCEL_OPTION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void actionPerformed(ActionEvent e) {
// Add 1 to array size to add null, which makes the entity unassigned
JComboBox rowListField = new JComboBox(
rowList.toArray(new Object[rowList.size() + 1]));
rowListField.setRenderer(new LabeledComboBoxRenderer());
LabeledComboBoxRenderer.applyToComboBox(rowListField);
rowListField.setSelectedItem(queen.getRow());
messagePanel.add(rowListField, BorderLayout.CENTER);
int result = JOptionPane.showConfirmDialog(NQueensPanel.this.getRootPane(), messagePanel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public void actionPerformed(ActionEvent e) {
// Add 1 to array size to add null, which makes the entity unassigned
JComboBox employeeListField = new JComboBox(
employeeList.toArray(new Object[employeeList.size() + 1]));
employeeListField.setRenderer(new LabeledComboBoxRenderer());
LabeledComboBoxRenderer.applyToComboBox(employeeListField);
employeeListField.setSelectedItem(shiftAssignment.getEmployee());
int result = JOptionPane.showConfirmDialog(EmployeePanel.this.getRootPane(), employeeListField,
"Select employee", JOptionPane.OK_CANCEL_OPTION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public void actionPerformed(ActionEvent e) {
// Add 1 to array size to add null, which makes the entity unassigned
JComboBox bedListField = new JComboBox(
bedList.toArray(new Object[bedList.size() + 1]));
bedListField.setRenderer(new LabeledComboBoxRenderer());
LabeledComboBoxRenderer.applyToComboBox(bedListField);
bedListField.setSelectedItem(bedDesignation.getBed());
listFieldsPanel.add(bedListField);
int result = JOptionPane.showConfirmDialog(PatientAdmissionSchedulePanel.this.getRootPane(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public void actionPerformed(ActionEvent e) {
// Add 1 to array size to add null, which makes the entity unassigned
JComboBox teamListField = new JComboBox(
teamList.toArray(new Object[teamList.size() + 1]));
teamListField.setRenderer(new LabeledComboBoxRenderer());
LabeledComboBoxRenderer.applyToComboBox(teamListField);
teamListField.setSelectedItem(teamAssignment.getTeam());
listFieldsPanel.add(teamListField);
listFieldsPanel.add(new JLabel("Locked:"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void actionPerformed(ActionEvent e) {
// Add 1 to array size to add null, which makes the entity unassigned
JComboBox dayListField = new JComboBox(
dayList.toArray(new Object[dayList.size() + 1]));
dayListField.setRenderer(new LabeledComboBoxRenderer());
LabeledComboBoxRenderer.applyToComboBox(dayListField);
dayListField.setSelectedItem(match.getDay());
int result = JOptionPane.showConfirmDialog(TravelingTournamentPanel.this.getRootPane(), dayListField,
"Select day", JOptionPane.OK_CANCEL_OPTION);
Expand Down

0 comments on commit df1587a

Please sign in to comment.