Dépôt du be graphe
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.

GraphReaderProgressBar.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package org.insa.graphs.gui;
  2. import java.awt.Component;
  3. import javax.swing.Box;
  4. import javax.swing.BoxLayout;
  5. import javax.swing.JDialog;
  6. import javax.swing.JFrame;
  7. import javax.swing.JLabel;
  8. import javax.swing.JPanel;
  9. import javax.swing.JProgressBar;
  10. import javax.swing.border.EmptyBorder;
  11. import org.insa.graphs.model.Arc;
  12. import org.insa.graphs.model.Node;
  13. import org.insa.graphs.model.RoadInformation;
  14. import org.insa.graphs.model.io.GraphReaderObserver;
  15. /**
  16. * One-time use GraphReaderObserver that display progress in three different
  17. * JProgressBar.
  18. *
  19. * @author Mikael
  20. *
  21. */
  22. public class GraphReaderProgressBar extends JDialog implements GraphReaderObserver {
  23. /**
  24. *
  25. */
  26. private static final long serialVersionUID = -1;
  27. // Index...
  28. private static final int NODE = 0, DESC = 1, ARC = 2;
  29. // Progress bar
  30. private final JProgressBar[] progressBars = new JProgressBar[3];
  31. // Current element read, and modulo.
  32. private int[] counters = new int[]{ 0, 0, 0 };
  33. private int[] modulos = new int[3];
  34. public GraphReaderProgressBar(JFrame owner) {
  35. super(owner);
  36. this.setVisible(false);
  37. setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
  38. final String[] infos = { "nodes", "road informations", "arcs" };
  39. JPanel pane = new JPanel();
  40. pane.setLayout(new BoxLayout(pane, BoxLayout.PAGE_AXIS));
  41. pane.setBorder(new EmptyBorder(15, 15, 15, 15));
  42. pane.add(Box.createVerticalGlue());
  43. for (int i = 0; i < 3; ++i) {
  44. JLabel label = new JLabel("Reading " + infos[i] + "... ");
  45. label.setAlignmentX(Component.LEFT_ALIGNMENT);
  46. progressBars[i] = new JProgressBar();
  47. progressBars[i].setAlignmentX(Component.LEFT_ALIGNMENT);
  48. pane.add(label);
  49. pane.add(progressBars[i]);
  50. }
  51. pane.add(Box.createVerticalGlue());
  52. setContentPane(pane);
  53. pack();
  54. }
  55. @Override
  56. public void notifyStartReading(String mapId) {
  57. setTitle("Reading graph " + mapId + "... ");
  58. setVisible(true);
  59. }
  60. @Override
  61. public void notifyEndReading() {
  62. setVisible(false);
  63. dispose();
  64. }
  65. protected void initProgressBar(int index, int max) {
  66. progressBars[index].setMaximum(max);
  67. modulos[index] = Math.max(max / 100, 1);
  68. }
  69. protected void incCounter(int index) {
  70. counters[index] += 1;
  71. if (counters[index] % modulos[index] == 0) {
  72. progressBars[index].setValue(counters[index]);
  73. }
  74. }
  75. @Override
  76. public void notifyStartReadingNodes(int nNodes) {
  77. initProgressBar(NODE, nNodes);
  78. }
  79. @Override
  80. public void notifyNewNodeRead(Node node) {
  81. incCounter(NODE);
  82. }
  83. @Override
  84. public void notifyStartReadingDescriptors(int nDesc) {
  85. initProgressBar(DESC, nDesc);
  86. }
  87. @Override
  88. public void notifyNewDescriptorRead(RoadInformation desc) {
  89. incCounter(DESC);
  90. }
  91. @Override
  92. public void notifyStartReadingArcs(int nArcs) {
  93. initProgressBar(ARC, nArcs);
  94. }
  95. @Override
  96. public void notifyNewArcRead(Arc arc) {
  97. incCounter(ARC);
  98. }
  99. }