123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- package org.insa.graphs.gui;
-
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.ArrayList;
- import java.util.List;
-
- import javax.swing.BorderFactory;
- import javax.swing.Box;
- import javax.swing.BoxLayout;
- import javax.swing.JButton;
- import javax.swing.JComboBox;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.border.CompoundBorder;
- import javax.swing.border.EmptyBorder;
-
- import org.insa.graphs.algorithm.AbstractInputData;
- import org.insa.graphs.algorithm.AbstractSolution;
- import org.insa.graphs.gui.drawing.Drawing;
- import org.insa.graphs.gui.drawing.overlays.PathOverlay;
- import org.insa.graphs.model.Graph;
- import org.insa.graphs.model.Path;
-
- public class SolutionPanel extends JPanel implements DrawingChangeListener, GraphChangeListener {
-
- /**
- *
- */
- private static final long serialVersionUID = 1L;
-
- private class SolutionBundle {
-
- // Solution
- private final AbstractSolution solution;
-
- // Path Overlay (not final due to redraw)
- private List<PathOverlay> overlays = new ArrayList<>();
-
- /**
- * Create a new bundle with the given solution and create a new overlay
- * corresponding to the solution (if the solution is feasible).
- *
- * @param solution Solution for this bundle, must not be null.
- *
- */
- public SolutionBundle(AbstractSolution solution, boolean createOverlays) {
- this.solution = solution;
- if (createOverlays) {
- this.overlays = createOverlaysFromSolution();
- }
- }
-
- /**
- * @return Solution associated with this bundle.
- */
- public AbstractSolution getSolution() {
- return this.solution;
- }
-
- /**
- * @return Data assocaited with this bundle.
- */
- public AbstractInputData getData() {
- return this.solution.getInputData();
- }
-
- /**
- * @return Overlays associated with this bundle, or null.
- */
- public List<PathOverlay> getOverlays() {
- return this.overlays;
- }
-
- /**
- * @return true if this bundle has overlays.
- */
- public boolean hasOverlays() {
- return !this.overlays.isEmpty();
- }
-
- /**
- * Re-draw the current overlay (if any) on the new drawing.
- *
- */
- public void updateOverlays() {
- if (this.overlays.isEmpty()) {
- return; // This bundle has no overlay.
- }
- List<PathOverlay> oldOverlays = this.overlays;
- this.overlays = createOverlaysFromSolution();
- for (int i = 0; i < oldOverlays.size(); ++i) {
- oldOverlays.get(i).delete();
- }
- }
-
- private List<PathOverlay> createOverlaysFromSolution() {
- List<PathOverlay> overlays = new ArrayList<>();
- if (solution.isFeasible()) {
- Method[] methods = this.solution.getClass().getDeclaredMethods();
- for (Method method: methods) {
- if (method.getReturnType().equals(Path.class)
- && method.getParameterCount() == 0) {
- try {
- Path path = (Path) method.invoke(this.solution);
- overlays.add(drawing.drawPath(path));
- }
- catch (IllegalAccessException | IllegalArgumentException
- | InvocationTargetException e) {
- // This has been check before, so should never happen...
- e.printStackTrace();
- }
- }
- }
- }
- return overlays;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see java.lang.Object#toString()
- */
- public String toString() {
- return getData().toString();
- }
-
- }
-
- // Solution
- private Drawing drawing;
-
- // Solution selector
- private final JComboBox<SolutionBundle> solutionSelect;
-
- private final JLabel informationPanel;
-
- // Current bundle
- private SolutionBundle currentBundle = null;
-
- public SolutionPanel(Component parent) {
- super();
- setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
- setBorder(new CompoundBorder(BorderFactory.createMatteBorder(1, 0, 1, 0, Color.LIGHT_GRAY),
- new EmptyBorder(10, 0, 10, 0)));
-
- solutionSelect = new JComboBox<>();
- solutionSelect.setBackground(Color.WHITE);
- solutionSelect.setAlignmentX(Component.LEFT_ALIGNMENT);
- add(solutionSelect);
-
- informationPanel = new JLabel();
- informationPanel.setOpaque(true);
- informationPanel.setFocusable(false);
- // informationPanel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
- informationPanel.setHorizontalAlignment(JLabel.LEFT);
-
- add(Box.createVerticalStrut(8));
- add(informationPanel);
-
- JButton clearButton = new JButton("Hide");
- clearButton.addActionListener(new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- for (PathOverlay overlay: currentBundle.getOverlays()) {
- if (overlay.isVisible()) {
- overlay.setVisible(false);
- clearButton.setText("Show");
- }
- else {
- overlay.setVisible(true);
- clearButton.setText("Hide");
- }
- }
- }
- });
-
- JPanel buttonPanel = new JPanel();
- buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
- buttonPanel.add(Box.createHorizontalGlue());
- buttonPanel.add(clearButton);
- buttonPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
-
- add(Box.createVerticalStrut(4));
- add(buttonPanel);
-
- solutionSelect.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
-
- if (currentBundle != null) {
- for (PathOverlay overlay: currentBundle.getOverlays()) {
- overlay.setVisible(false);
- }
- }
-
- SolutionBundle bundle = (SolutionBundle) solutionSelect.getSelectedItem();
-
- if (bundle != null) {
-
- updateInformationLabel(bundle);
- buttonPanel
- .setVisible(bundle.getSolution().isFeasible() && bundle.hasOverlays());
- clearButton.setText(bundle.getSolution().isFeasible() ? "Hide" : "Show");
-
- for (PathOverlay overlay: bundle.getOverlays()) {
- overlay.setVisible(true);
- }
- }
-
- currentBundle = bundle;
- }
- });
-
- }
-
- public void addSolution(AbstractSolution solution) {
- addSolution(solution, true);
- }
-
- /**
- * Add the given solution to the panel.
- *
- * @param solution the solution to add to the panel
- * @param createOverlays Whether or not overlay should be created for this
- * solution.
- */
- public void addSolution(AbstractSolution solution, boolean createOverlays) {
- SolutionBundle bundle = new SolutionBundle(solution, createOverlays);
- solutionSelect.addItem(bundle);
- solutionSelect.setSelectedItem(bundle);
- }
-
- protected void updateInformationLabel(SolutionBundle bundle) {
- informationPanel.setText(bundle.getSolution().toString());
- revalidate();
- repaint();
- }
-
- @Override
- public void setEnabled(boolean enabled) {
- super.setEnabled(enabled);
- solutionSelect.setEnabled(enabled);
-
- if (enabled) {
- // Trigger event
- solutionSelect.setSelectedItem(currentBundle);
- }
- else {
- SolutionBundle bundle = (SolutionBundle) this.solutionSelect.getSelectedItem();
- if (bundle != null) {
- for (PathOverlay overlay: bundle.getOverlays()) {
- overlay.setVisible(false);
- }
- }
- }
- }
-
- @Override
- public void newGraphLoaded(Graph graph) {
- for (int i = 0; i < this.solutionSelect.getItemCount(); ++i) {
- for (PathOverlay overlay: this.solutionSelect.getItemAt(i).getOverlays()) {
- overlay.delete();
- }
- }
- this.solutionSelect.removeAllItems();
- this.currentBundle = null;
- this.setVisible(false);
- }
-
- @Override
- public void onDrawingLoaded(Drawing oldDrawing, Drawing newDrawing) {
- if (newDrawing != drawing) {
- drawing = newDrawing;
- }
- }
-
- @Override
- public void onRedrawRequest() {
- for (int i = 0; i < this.solutionSelect.getItemCount(); ++i) {
- this.solutionSelect.getItemAt(i).updateOverlays();
- }
- }
-
- }
|