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 12KB

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