<commit_before>/*
 * Copyright 2010 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.optaplanner.examples.curriculumcourse.swingui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;

import org.optaplanner.core.impl.solution.Solution;
import org.optaplanner.examples.common.swingui.SolutionPanel;
import org.optaplanner.examples.common.swingui.TangoColorFactory;
import org.optaplanner.examples.common.swingui.timetable.TimeTableLayout;
import org.optaplanner.examples.common.swingui.timetable.TimeTableLayoutConstraints;
import org.optaplanner.examples.curriculumcourse.domain.CourseSchedule;
import org.optaplanner.examples.curriculumcourse.domain.Lecture;
import org.optaplanner.examples.curriculumcourse.domain.Period;
import org.optaplanner.examples.curriculumcourse.domain.Room;
import org.optaplanner.examples.curriculumcourse.domain.Teacher;
import org.optaplanner.examples.curriculumcourse.solver.move.PeriodChangeMove;
import org.optaplanner.examples.curriculumcourse.solver.move.RoomChangeMove;

public class CurriculumCoursePanel extends SolutionPanel {

    private static final Color HEADER_COLOR = TangoColorFactory.ALUMINIUM_2;

    private final JPanel roomsPanel;
    private TimeTableLayout roomsTimeTableLayout;
    private final JPanel teachersPanel;
    private TimeTableLayout teachersTimeTableLayout;

    private Map<Period, Integer> periodXMap;
    private Map<Room, Integer> roomYMap;
    private Map<Teacher, Integer> teacherYMap;

    public CurriculumCoursePanel() {
        setLayout(new BorderLayout());
        JTabbedPane tabbedPane = new JTabbedPane();
        roomsTimeTableLayout = new TimeTableLayout();
        roomsPanel = new JPanel(roomsTimeTableLayout);
        tabbedPane.add("Rooms", new JScrollPane(roomsPanel));
        teachersTimeTableLayout = new TimeTableLayout();
        teachersPanel = new JPanel(teachersTimeTableLayout);
        tabbedPane.add("Teachers", new JScrollPane(teachersPanel));
        add(tabbedPane, BorderLayout.CENTER);
        setPreferredSize(PREFERRED_SCROLLABLE_VIEWPORT_SIZE);
    }

    @Override
    public boolean isWrapInScrollPane() {
        return false;
    }

    @Override
    public boolean isRefreshScreenDuringSolving() {
        return true;
    }

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

    public void resetPanel(Solution solution) {
        roomsPanel.removeAll();
        teachersPanel.removeAll();
        CourseSchedule courseSchedule = (CourseSchedule) solution;
        defineGrid(courseSchedule);
        fillCells(courseSchedule);
        repaint(); // Hack to force a repaint of TimeTableLayout during "refresh screen while solving"
    }

    private void defineGrid(CourseSchedule courseSchedule) {
        roomsTimeTableLayout.reset();
        teachersTimeTableLayout.reset();
        JButton footprint = new JButton("1234567");
        int footprintWidth = footprint.getPreferredSize().width;
        int footprintHeight = footprint.getPreferredSize().height;
        roomsTimeTableLayout.addColumn(150); // Header
        teachersTimeTableLayout.addColumn(150); // Header
        periodXMap = new HashMap<Period, Integer>(courseSchedule.getPeriodList().size());
        int unassignedX = roomsTimeTableLayout.addColumn(footprintWidth); // Unassigned period
        teachersTimeTableLayout.addColumn(footprintWidth); // Unassigned period
        periodXMap.put(null, unassignedX);
        for (Period period : courseSchedule.getPeriodList()) {
            int x = roomsTimeTableLayout.addColumn(footprintWidth);
            int otherX = teachersTimeTableLayout.addColumn(footprintWidth);
            if (x != otherX) {
                throw new IllegalStateException("Impossible");
            }
            periodXMap.put(period, x);
        }
        roomsTimeTableLayout.addRow(footprintHeight); // Header
        roomYMap = new HashMap<Room, Integer>(courseSchedule.getRoomList().size());
        roomsTimeTableLayout.addRow(footprintHeight); // Unassigned
        roomYMap.put(null, 1);
        for (Room room : courseSchedule.getRoomList()) {
            int y = roomsTimeTableLayout.addRow(footprintHeight);
            roomYMap.put(room, y);
        }
        teachersTimeTableLayout.addRow(footprintHeight); // Header
        teacherYMap = new HashMap<Teacher, Integer>(courseSchedule.getTeacherList().size());
        for (Teacher teacher : courseSchedule.getTeacherList()) {
            int y = teachersTimeTableLayout.addRow(footprintHeight);
            teacherYMap.put(teacher, y);
        }
    }

    private void fillCells(CourseSchedule courseSchedule) {
        JPanel unassignedPeriodRoomLabel = createHeaderPanel(new JLabel("Unassigned", SwingConstants.CENTER));
        roomsPanel.add(unassignedPeriodRoomLabel, new TimeTableLayoutConstraints(periodXMap.get(null), 0, true));
        JPanel unassignedPeriodTeacherLabel = createHeaderPanel(new JLabel("Unassigned", SwingConstants.CENTER));
        teachersPanel.add(unassignedPeriodTeacherLabel, new TimeTableLayoutConstraints(periodXMap.get(null), 0, true));
        for (Period period : courseSchedule.getPeriodList()) {
            JPanel periodRoomLabel = createHeaderPanel(new JLabel(period.getLabel(), SwingConstants.CENTER));
            roomsPanel.add(periodRoomLabel, new TimeTableLayoutConstraints(periodXMap.get(period), 0, true));
            JPanel periodTeacherLabel = createHeaderPanel(new JLabel(period.getLabel(), SwingConstants.CENTER));
            teachersPanel.add(periodTeacherLabel, new TimeTableLayoutConstraints(periodXMap.get(period), 0, true));
        }

        JPanel unassignedRoomLabel = createHeaderPanel(new JLabel("Unassigned"));
        roomsPanel.add(unassignedRoomLabel, new TimeTableLayoutConstraints(0, roomYMap.get(null), true));
        for (Room room : courseSchedule.getRoomList()) {
            JPanel roomLabel = createHeaderPanel(new JLabel(room.getLabel()));
            roomsPanel.add(roomLabel, new TimeTableLayoutConstraints(0, roomYMap.get(room), true));
        }

        for (Teacher teacher : courseSchedule.getTeacherList()) {
            JPanel teacherLabel = createHeaderPanel(new JLabel(teacher.getLabel()));
            teachersPanel.add(teacherLabel, new TimeTableLayoutConstraints(0, teacherYMap.get(teacher), true));
        }

        TangoColorFactory tangoColorFactory = new TangoColorFactory();
        for (Lecture lecture : courseSchedule.getLectureList()) {
            Color lectureColor = tangoColorFactory.pickColor(lecture);
            int x = periodXMap.get(lecture.getPeriod());
            JButton roomButton = new JButton(new LectureAction(lecture));
            roomButton.setBackground(lectureColor);
            roomsPanel.add(roomButton, new TimeTableLayoutConstraints(x, roomYMap.get(lecture.getRoom())));
            JButton teacherButton = new JButton(new LectureAction(lecture));
            teacherButton.setBackground(lectureColor);
            teachersPanel.add(teacherButton, new TimeTableLayoutConstraints(x, teacherYMap.get(lecture.getTeacher())));
        }
    }

    private JPanel createHeaderPanel(JLabel label) {
        JPanel headerPanel = new JPanel(new BorderLayout());
        headerPanel.add(label, BorderLayout.NORTH);
        headerPanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(TangoColorFactory.ALUMINIUM_5),
                BorderFactory.createEmptyBorder(2, 2, 2, 2)));
        return headerPanel;
    }

    private class LectureAction extends AbstractAction {

        private Lecture lecture;

        public LectureAction(Lecture lecture) {
            super(lecture.getLabel());
            this.lecture = lecture;
        }
<commit_msg>Curriculum course gui: transpose grid<commit_after>/*
 * Copyright 2010 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.optaplanner.examples.curriculumcourse.swingui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;

import org.optaplanner.core.impl.solution.Solution;
import org.optaplanner.examples.common.swingui.SolutionPanel;
import org.optaplanner.examples.common.swingui.TangoColorFactory;
import org.optaplanner.examples.common.swingui.timetable.TimeTableLayout;
import org.optaplanner.examples.common.swingui.timetable.TimeTableLayoutConstraints;
import org.optaplanner.examples.curriculumcourse.domain.CourseSchedule;
import org.optaplanner.examples.curriculumcourse.domain.Lecture;
import org.optaplanner.examples.curriculumcourse.domain.Period;
import org.optaplanner.examples.curriculumcourse.domain.Room;
import org.optaplanner.examples.curriculumcourse.domain.Teacher;
import org.optaplanner.examples.curriculumcourse.solver.move.PeriodChangeMove;
import org.optaplanner.examples.curriculumcourse.solver.move.RoomChangeMove;

public class CurriculumCoursePanel extends SolutionPanel {

    private static final Color HEADER_COLOR = TangoColorFactory.ALUMINIUM_2;

    private final JPanel roomsPanel;
    private TimeTableLayout roomsTimeTableLayout;
    private final JPanel teachersPanel;
    private TimeTableLayout teachersTimeTableLayout;

    private Map<Period, Integer> periodYMap;
    private Map<Room, Integer> roomXMap;
    private Map<Teacher, Integer> teacherXMap;

    public CurriculumCoursePanel() {
        setLayout(new BorderLayout());
        JTabbedPane tabbedPane = new JTabbedPane();
        roomsTimeTableLayout = new TimeTableLayout();
        roomsPanel = new JPanel(roomsTimeTableLayout);
        tabbedPane.add("Rooms", new JScrollPane(roomsPanel));
        teachersTimeTableLayout = new TimeTableLayout();
        teachersPanel = new JPanel(teachersTimeTableLayout);
        tabbedPane.add("Teachers", new JScrollPane(teachersPanel));
        add(tabbedPane, BorderLayout.CENTER);
        setPreferredSize(PREFERRED_SCROLLABLE_VIEWPORT_SIZE);
    }

    @Override
    public boolean isWrapInScrollPane() {
        return false;
    }

    @Override
    public boolean isRefreshScreenDuringSolving() {
        return true;
    }

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

    public void resetPanel(Solution solution) {
        roomsPanel.removeAll();
        teachersPanel.removeAll();
        CourseSchedule courseSchedule = (CourseSchedule) solution;
        defineGrid(courseSchedule);
        fillCells(courseSchedule);
        repaint(); // Hack to force a repaint of TimeTableLayout during "refresh screen while solving"
    }

    private void defineGrid(CourseSchedule courseSchedule) {
        roomsTimeTableLayout.reset();
        teachersTimeTableLayout.reset();
        JButton footprint = new JButton("1234567");
        int footprintWidth = footprint.getPreferredSize().width;
        int footprintHeight = footprint.getPreferredSize().height;
        roomsTimeTableLayout.addColumn(150); // Header
        roomXMap = new HashMap<Room, Integer>(courseSchedule.getRoomList().size());
        int unassignedRoomX = roomsTimeTableLayout.addColumn(footprintWidth); // Unassigned
        roomXMap.put(null, unassignedRoomX);
        for (Room room : courseSchedule.getRoomList()) {
            int x = roomsTimeTableLayout.addColumn(footprintWidth);
            roomXMap.put(room, x);
        }
        teachersTimeTableLayout.addColumn(150); // Header
        teacherXMap = new HashMap<Teacher, Integer>(courseSchedule.getTeacherList().size());
        for (Teacher teacher : courseSchedule.getTeacherList()) {
            int x = teachersTimeTableLayout.addColumn(footprintWidth);
            teacherXMap.put(teacher, x);
        }
        roomsTimeTableLayout.addRow(footprintHeight); // Header
        teachersTimeTableLayout.addRow(footprintHeight); // Header
        periodYMap = new HashMap<Period, Integer>(courseSchedule.getPeriodList().size());
        int unassignedPeriodY = roomsTimeTableLayout.addRow(footprintHeight); // Unassigned period
        int otherUnassignedPeriodY = teachersTimeTableLayout.addRow(footprintHeight); // Unassigned period
        if (unassignedPeriodY != otherUnassignedPeriodY) {
            throw new IllegalStateException("Impossible");
        }
        periodYMap.put(null, unassignedPeriodY);
        for (Period period : courseSchedule.getPeriodList()) {
            int y = roomsTimeTableLayout.addRow(footprintHeight);
            int otherY = teachersTimeTableLayout.addRow(footprintHeight);
            if (y != otherY) {
                throw new IllegalStateException("Impossible");
            }
            periodYMap.put(period, y);
        }
    }

    private void fillCells(CourseSchedule courseSchedule) {
        JPanel unassignedRoomLabel = createHeaderPanel(new JLabel("Unassigned", SwingConstants.CENTER));
        roomsPanel.add(unassignedRoomLabel, new TimeTableLayoutConstraints(roomXMap.get(null), 0, true));
        for (Room room : courseSchedule.getRoomList()) {
            JPanel roomLabel = createHeaderPanel(new JLabel(room.getLabel(), SwingConstants.CENTER));
            roomsPanel.add(roomLabel, new TimeTableLayoutConstraints(roomXMap.get(room), 0, true));
        }

        for (Teacher teacher : courseSchedule.getTeacherList()) {
            JPanel teacherLabel = createHeaderPanel(new JLabel(teacher.getLabel(), SwingConstants.CENTER));
            teachersPanel.add(teacherLabel, new TimeTableLayoutConstraints(teacherXMap.get(teacher), 0, true));
        }

        JPanel unassignedPeriodRoomLabel = createHeaderPanel(new JLabel("Unassigned"));
        roomsPanel.add(unassignedPeriodRoomLabel, new TimeTableLayoutConstraints(0, periodYMap.get(null), true));
        JPanel unassignedPeriodTeacherLabel = createHeaderPanel(new JLabel("Unassigned"));
        teachersPanel.add(unassignedPeriodTeacherLabel, new TimeTableLayoutConstraints(0, periodYMap.get(null), true));
        for (Period period : courseSchedule.getPeriodList()) {
            JPanel periodRoomLabel = createHeaderPanel(new JLabel(period.getLabel()));
            roomsPanel.add(periodRoomLabel, new TimeTableLayoutConstraints(0, periodYMap.get(period), true));
            JPanel periodTeacherLabel = createHeaderPanel(new JLabel(period.getLabel()));
            teachersPanel.add(periodTeacherLabel, new TimeTableLayoutConstraints(0, periodYMap.get(period), true));
        }

        TangoColorFactory tangoColorFactory = new TangoColorFactory();
        for (Lecture lecture : courseSchedule.getLectureList()) {
            Color lectureColor = tangoColorFactory.pickColor(lecture);
            int y = periodYMap.get(lecture.getPeriod());
            JButton roomButton = new JButton(new LectureAction(lecture));
            roomButton.setBackground(lectureColor);
            roomsPanel.add(roomButton, new TimeTableLayoutConstraints(roomXMap.get(lecture.getRoom()), y));
            JButton teacherButton = new JButton(new LectureAction(lecture));
            teacherButton.setBackground(lectureColor);
            teachersPanel.add(teacherButton, new TimeTableLayoutConstraints(teacherXMap.get(lecture.getTeacher()), y));
        }
    }

    private JPanel createHeaderPanel(JLabel label) {
        JPanel headerPanel = new JPanel(new BorderLayout());
        headerPanel.add(label, BorderLayout.NORTH);
        headerPanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(TangoColorFactory.ALUMINIUM_5),
                BorderFactory.createEmptyBorder(2, 2, 2, 2)));
        return headerPanel;
    }

    private class LectureAction extends AbstractAction {

        private Lecture lecture;

        public LectureAction(Lecture lecture) {
            super(lecture.getLabel());
            this.lecture = lecture;
        }

{'fraction_non_alphanumeric': 0.09081667231446967, 'fraction_numerical': 0.003896984073195527, 'mean_word_length': 3.765069967707212, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5457621', 'n_tokens_mistral': 4905, 'n_tokens_neox': 4738, 'n_words': 989}