No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AlgorithmPanel.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. package org.insa.graphics;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Font;
  5. import java.awt.GridBagConstraints;
  6. import java.awt.GridBagLayout;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.ComponentAdapter;
  10. import java.awt.event.ComponentEvent;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import javax.swing.Box;
  14. import javax.swing.BoxLayout;
  15. import javax.swing.JButton;
  16. import javax.swing.JCheckBox;
  17. import javax.swing.JComboBox;
  18. import javax.swing.JComponent;
  19. import javax.swing.JLabel;
  20. import javax.swing.JPanel;
  21. import javax.swing.border.EmptyBorder;
  22. import org.insa.algo.AbstractAlgorithm;
  23. import org.insa.algo.AlgorithmFactory;
  24. import org.insa.algo.ArcInspector;
  25. import org.insa.algo.ArcInspectorFactory;
  26. import org.insa.graph.Node;
  27. import org.insa.graphics.NodesInputPanel.InputChangedEvent;
  28. import org.insa.graphics.drawing.Drawing;
  29. import org.insa.graphics.drawing.components.MapViewDrawing;
  30. import org.insa.graphics.utils.ColorUtils;
  31. public class AlgorithmPanel extends JPanel implements DrawingChangeListener {
  32. /**
  33. *
  34. */
  35. private static final long serialVersionUID = 1L;
  36. public class StartActionEvent extends ActionEvent {
  37. /**
  38. *
  39. */
  40. private static final long serialVersionUID = 4090710269781229078L;
  41. protected static final String START_EVENT_COMMAND = "allInputFilled";
  42. protected static final int START_EVENT_ID = 0x1;
  43. private final List<Node> nodes;
  44. private final Class<? extends AbstractAlgorithm<?>> algoClass;
  45. private final ArcInspector arcFilter;
  46. private final boolean graphicVisualization;
  47. private final boolean textualVisualization;
  48. public StartActionEvent(Class<? extends AbstractAlgorithm<?>> algoClass, List<Node> nodes,
  49. ArcInspector arcFilter, boolean graphicVisualization,
  50. boolean textualVisualization) {
  51. super(AlgorithmPanel.this, START_EVENT_ID, START_EVENT_COMMAND);
  52. this.nodes = nodes;
  53. this.algoClass = algoClass;
  54. this.graphicVisualization = graphicVisualization;
  55. this.textualVisualization = textualVisualization;
  56. this.arcFilter = arcFilter;
  57. }
  58. /**
  59. * @return Nodes associated with this event.
  60. */
  61. public List<Node> getNodes() {
  62. return this.nodes;
  63. }
  64. /**
  65. * @return Arc filter associated with this event.
  66. */
  67. public ArcInspector getArcFilter() {
  68. return this.arcFilter;
  69. }
  70. /**
  71. * @return Algorithm class associated with this event.
  72. */
  73. public Class<? extends AbstractAlgorithm<?>> getAlgorithmClass() {
  74. return this.algoClass;
  75. }
  76. /**
  77. * @return true if graphic visualization is enabled.
  78. */
  79. public boolean isGraphicVisualizationEnabled() {
  80. return this.graphicVisualization;
  81. }
  82. /**
  83. * @return true if textual visualization is enabled.
  84. */
  85. public boolean isTextualVisualizationEnabled() {
  86. return this.textualVisualization;
  87. }
  88. };
  89. // Input panels for node.
  90. protected NodesInputPanel nodesInputPanel;
  91. // Solution
  92. protected SolutionPanel solutionPanel;
  93. // Component that can be enabled/disabled.
  94. private ArrayList<JComponent> components = new ArrayList<>();
  95. // Graphic / Text checkbox observer
  96. private final JCheckBox graphicObserverCheckbox, textualObserverCheckbox;
  97. // Drawing
  98. private Drawing drawing = null;
  99. private JButton startAlgoButton;
  100. // Start listeners
  101. List<ActionListener> startActionListeners = new ArrayList<>();
  102. /**
  103. * Create a new AlgorithmPanel with the given parameters.
  104. *
  105. * @param parent Parent component for this panel. Only use for centering
  106. * dialogs.
  107. * @param baseAlgorithm Base algorithm for this algorithm panel.
  108. * @param title Title of the panel.
  109. * @param nodeNames Names of the input nodes.
  110. * @param enableArcFilterSelection <code>true</code> to enable
  111. * {@link ArcInspector} selection.
  112. *
  113. * @see ArcInspectorFactory
  114. */
  115. public AlgorithmPanel(Component parent, Class<? extends AbstractAlgorithm<?>> baseAlgorithm,
  116. String title, String[] nodeNames, boolean enableArcFilterSelection) {
  117. super();
  118. setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
  119. setBorder(new EmptyBorder(15, 15, 15, 15));
  120. // Set title.
  121. add(createTitleLabel(title));
  122. add(Box.createVerticalStrut(8));
  123. // Add algorithm selection
  124. JComboBox<String> algoSelect = createAlgoritmSelectComboBox(baseAlgorithm);
  125. if (algoSelect.getItemCount() > 1) {
  126. add(algoSelect);
  127. components.add(algoSelect);
  128. }
  129. // Add inputs for node.
  130. this.nodesInputPanel = createNodesInputPanel(nodeNames);
  131. add(this.nodesInputPanel);
  132. components.add(this.nodesInputPanel);
  133. JComboBox<ArcInspector> arcFilterSelect = new JComboBox<>(
  134. ArcInspectorFactory.getAllFilters().toArray(new ArcInspector[0]));
  135. arcFilterSelect.setBackground(Color.WHITE);
  136. // Add mode selection
  137. JPanel modeAndObserverPanel = new JPanel();
  138. modeAndObserverPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
  139. modeAndObserverPanel.setLayout(new GridBagLayout());
  140. graphicObserverCheckbox = new JCheckBox("Graphic");
  141. graphicObserverCheckbox.setSelected(true);
  142. textualObserverCheckbox = new JCheckBox("Textual");
  143. GridBagConstraints c = new GridBagConstraints();
  144. c.fill = GridBagConstraints.HORIZONTAL;
  145. c.gridy = 2;
  146. c.gridx = 0;
  147. c.weightx = 0;
  148. modeAndObserverPanel.add(new JLabel("Visualization: "), c);
  149. c.gridx = 1;
  150. c.weightx = 1;
  151. modeAndObserverPanel.add(graphicObserverCheckbox, c);
  152. c.gridx = 2;
  153. c.weightx = 1;
  154. modeAndObserverPanel.add(textualObserverCheckbox, c);
  155. if (enableArcFilterSelection) {
  156. c.gridy = 1;
  157. c.gridx = 0;
  158. c.weightx = 0;
  159. modeAndObserverPanel.add(new JLabel("Mode: "), c);
  160. c.gridx = 1;
  161. c.gridwidth = 2;
  162. c.weightx = 1;
  163. modeAndObserverPanel.add(arcFilterSelect, c);
  164. }
  165. components.add(arcFilterSelect);
  166. components.add(textualObserverCheckbox);
  167. add(modeAndObserverPanel);
  168. solutionPanel = new SolutionPanel(parent);
  169. solutionPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
  170. solutionPanel.setVisible(false);
  171. add(Box.createVerticalStrut(10));
  172. add(solutionPanel);
  173. // Bottom panel
  174. JPanel bottomPanel = new JPanel();
  175. bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));
  176. startAlgoButton = new JButton("Start");
  177. startAlgoButton.setEnabled(false);
  178. startAlgoButton.addActionListener(new ActionListener() {
  179. @Override
  180. public void actionPerformed(ActionEvent e) {
  181. for (ActionListener lis: startActionListeners) {
  182. lis.actionPerformed(new StartActionEvent(
  183. AlgorithmFactory.getAlgorithmClass(baseAlgorithm,
  184. (String) algoSelect.getSelectedItem()),
  185. nodesInputPanel.getNodeForInputs(),
  186. (ArcInspector) arcFilterSelect.getSelectedItem(),
  187. graphicObserverCheckbox.isSelected(),
  188. textualObserverCheckbox.isSelected()));
  189. }
  190. }
  191. });
  192. JButton hideButton = new JButton("Hide");
  193. hideButton.addActionListener(new ActionListener() {
  194. @Override
  195. public void actionPerformed(ActionEvent e) {
  196. nodesInputPanel.setEnabled(false);
  197. setVisible(false);
  198. }
  199. });
  200. bottomPanel.add(startAlgoButton);
  201. bottomPanel.add(Box.createHorizontalGlue());
  202. bottomPanel.add(hideButton);
  203. components.add(hideButton);
  204. bottomPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
  205. add(Box.createVerticalStrut(8));
  206. add(bottomPanel);
  207. nodesInputPanel.addInputChangedListener(new ActionListener() {
  208. @Override
  209. public void actionPerformed(ActionEvent e) {
  210. InputChangedEvent evt = (InputChangedEvent) e;
  211. startAlgoButton.setEnabled(allNotNull(evt.getNodes()));
  212. }
  213. });
  214. addComponentListener(new ComponentAdapter() {
  215. @Override
  216. public void componentShown(ComponentEvent e) {
  217. setEnabled(true);
  218. nodesInputPanel.setVisible(true);
  219. }
  220. @Override
  221. public void componentHidden(ComponentEvent e) {
  222. setEnabled(false);
  223. nodesInputPanel.setVisible(false);
  224. }
  225. });
  226. setEnabled(false);
  227. }
  228. /**
  229. * Create the title JLabel for this panel.
  230. *
  231. * @param title Title for the label.
  232. *
  233. * @return A new JLabel containing the given title with proper font.
  234. */
  235. protected JLabel createTitleLabel(String title) {
  236. JLabel titleLabel = new JLabel(title);
  237. titleLabel.setBackground(Color.RED);
  238. titleLabel.setHorizontalAlignment(JLabel.LEFT);
  239. titleLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
  240. Font font = titleLabel.getFont();
  241. font = font.deriveFont(Font.BOLD, 18);
  242. titleLabel.setFont(font);
  243. return titleLabel;
  244. }
  245. /**
  246. * Create the combo box for the algorithm selection.
  247. *
  248. * @param baseAlgorithm Base algorithm for which the select box should be
  249. * created.
  250. *
  251. * @return A new JComboBox containing algorithms for the given base algorithm.
  252. *
  253. * @see AlgorithmFactory
  254. */
  255. protected JComboBox<String> createAlgoritmSelectComboBox(
  256. Class<? extends AbstractAlgorithm<?>> baseAlgorithm) {
  257. JComboBox<String> algoSelect = new JComboBox<>(
  258. AlgorithmFactory.getAlgorithmNames(baseAlgorithm).toArray(new String[0]));
  259. algoSelect.setBackground(Color.WHITE);
  260. algoSelect.setAlignmentX(Component.LEFT_ALIGNMENT);
  261. return algoSelect;
  262. }
  263. /**
  264. * Create a node input panel with the given node input names.
  265. *
  266. * @param nodeNames Field names for the inputs to create.
  267. *
  268. * @return A new NodesInputPanel containing inputs for the given names.
  269. */
  270. protected NodesInputPanel createNodesInputPanel(String[] nodeNames) {
  271. NodesInputPanel panel = new NodesInputPanel();
  272. panel.setAlignmentX(Component.LEFT_ALIGNMENT);
  273. for (int i = 0; i < nodeNames.length; ++i) {
  274. panel.addTextField(nodeNames[i] + ": ", ColorUtils.getColor(i));
  275. }
  276. panel.setEnabled(false);
  277. return panel;
  278. }
  279. /**
  280. * Check if the given list of nodes does not contain any <code>null</code>
  281. * value.
  282. *
  283. * @param nodes List of {@link Node} to check.
  284. *
  285. * @return <code>true</code> if the list does not contain any <code>null</code>
  286. * value, <code>false</code> otherwise.
  287. */
  288. protected boolean allNotNull(List<Node> nodes) {
  289. boolean allNotNull = true;
  290. for (Node node: nodes) {
  291. allNotNull = allNotNull && node != null;
  292. }
  293. return allNotNull;
  294. }
  295. @Override
  296. public void setEnabled(boolean enabled) {
  297. super.setEnabled(enabled);
  298. nodesInputPanel.setEnabled(enabled);
  299. solutionPanel.setEnabled(enabled);
  300. for (JComponent component: components) {
  301. component.setEnabled(enabled);
  302. }
  303. graphicObserverCheckbox.setEnabled(enabled && !(drawing instanceof MapViewDrawing));
  304. enabled = enabled && allNotNull(this.nodesInputPanel.getNodeForInputs());
  305. startAlgoButton.setEnabled(enabled);
  306. }
  307. /**
  308. * Add a new start action listener to this class.
  309. *
  310. * @param listener Listener to add.
  311. */
  312. public void addStartActionListener(ActionListener listener) {
  313. this.startActionListeners.add(listener);
  314. }
  315. @Override
  316. public void onDrawingLoaded(Drawing oldDrawing, Drawing newDrawing) {
  317. if (newDrawing instanceof MapViewDrawing) {
  318. graphicObserverCheckbox.setSelected(false);
  319. graphicObserverCheckbox.setEnabled(false);
  320. }
  321. else {
  322. graphicObserverCheckbox.setSelected(true);
  323. graphicObserverCheckbox.setEnabled(true);
  324. }
  325. this.drawing = newDrawing;
  326. }
  327. @Override
  328. public void onRedrawRequest() {
  329. }
  330. }