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.

MainWindow.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. package org.insa.base;
  2. import java.awt.BorderLayout;
  3. import java.awt.Color;
  4. import java.awt.Dimension;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.WindowAdapter;
  8. import java.awt.event.WindowEvent;
  9. import java.io.File;
  10. import java.io.IOException;
  11. import java.io.OutputStream;
  12. import java.io.PrintStream;
  13. import java.util.ArrayList;
  14. import javax.swing.BorderFactory;
  15. import javax.swing.BoxLayout;
  16. import javax.swing.JFileChooser;
  17. import javax.swing.JFrame;
  18. import javax.swing.JLabel;
  19. import javax.swing.JMenu;
  20. import javax.swing.JMenuBar;
  21. import javax.swing.JMenuItem;
  22. import javax.swing.JOptionPane;
  23. import javax.swing.JPanel;
  24. import javax.swing.JScrollPane;
  25. import javax.swing.JSplitPane;
  26. import javax.swing.JTextArea;
  27. import javax.swing.KeyStroke;
  28. import javax.swing.SwingConstants;
  29. import javax.swing.filechooser.FileNameExtensionFilter;
  30. import org.insa.algo.weakconnectivity.WeaklyConnectedComponentGraphicObserver;
  31. import org.insa.algo.weakconnectivity.WeaklyConnectedComponentTextObserver;
  32. import org.insa.algo.weakconnectivity.WeaklyConnectedComponentsAlgorithm;
  33. import org.insa.algo.weakconnectivity.WeaklyConnectedComponentsInstance;
  34. import org.insa.drawing.DrawingVisible;
  35. import org.insa.drawing.graph.BlackAndWhiteGraphPalette;
  36. import org.insa.drawing.graph.GraphDrawing;
  37. import org.insa.drawing.graph.PathDrawing;
  38. import org.insa.graph.Graph;
  39. import org.insa.graph.Path;
  40. import org.insa.graph.io.BinaryGraphReader;
  41. import org.insa.graph.io.BinaryPathReader;
  42. import org.insa.graph.io.MapMismatchException;
  43. import org.insa.graph.io.Openfile;
  44. import com.sun.glass.events.KeyEvent;
  45. public class MainWindow extends JFrame {
  46. public class JOutputStream extends OutputStream {
  47. private JTextArea textArea;
  48. public JOutputStream(JTextArea textArea) {
  49. this.textArea = textArea;
  50. }
  51. @Override
  52. public void write(int b) throws IOException {
  53. // redirects data to the text area
  54. textArea.setText(textArea.getText() + String.valueOf((char)b));
  55. // scrolls the text area to the end of data
  56. textArea.setCaretPosition(textArea.getDocument().getLength());
  57. // keeps the textArea up to date
  58. textArea.update(textArea.getGraphics());
  59. }
  60. }
  61. /**
  62. *
  63. */
  64. private static final long serialVersionUID = -527660583705140687L;
  65. /**
  66. *
  67. */
  68. private static final String WINDOW_TITLE = "BE Graphes INSA";
  69. /**
  70. *
  71. */
  72. private static final Dimension DEFAULT_DIMENSION = new Dimension(800, 600);
  73. // Current graph.
  74. private Graph graph;
  75. // Current loaded path.
  76. private Path currentPath;
  77. // List of item for the top menus.
  78. private JMenuItem openMapItem;
  79. // List of items that cannot be used without a graph
  80. private ArrayList<JMenuItem> graphItems = new ArrayList<JMenuItem>();
  81. // Label containing the map ID of the current graph.
  82. private JLabel mapIdPanel;
  83. // Log stream and print stream
  84. private JOutputStream logStream;
  85. private PrintStream printStream;
  86. /**
  87. *
  88. */
  89. private DrawingVisible drawing;
  90. public MainWindow() {
  91. super(WINDOW_TITLE);
  92. setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
  93. setLayout(new BorderLayout());
  94. setSize(DEFAULT_DIMENSION);
  95. setJMenuBar(createMenuBar());
  96. addWindowListener(new WindowAdapter() {
  97. public void windowClosing(WindowEvent e) {
  98. int confirmed = JOptionPane.showConfirmDialog(null,
  99. "Are you sure you want to close the application?", "Exit Confirmation",
  100. JOptionPane.YES_NO_OPTION);
  101. if (confirmed == JOptionPane.YES_OPTION) {
  102. dispose();
  103. System.exit(0);
  104. }
  105. }
  106. });
  107. // Create graph area
  108. JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
  109. drawing = new DrawingVisible();
  110. drawing.setBackground(Color.WHITE);
  111. JTextArea infoPanel = new JTextArea();
  112. infoPanel.setMinimumSize(new Dimension(200, 50));
  113. // infoPanel.setBorder(BorderFactory.createMatteBorder(0, 1, 0, 0, Color.GRAY));
  114. infoPanel.setBackground(Color.WHITE);
  115. infoPanel.setLineWrap(true);
  116. infoPanel.setEditable(false);
  117. this.logStream = new JOutputStream(infoPanel);
  118. this.printStream = new PrintStream(this.logStream);
  119. sp.setResizeWeight(0.8);
  120. // sp.setEnabled(false);
  121. sp.setDividerSize(5);
  122. sp.setBackground(Color.WHITE);
  123. sp.add(drawing);
  124. sp.add(new JScrollPane(infoPanel));
  125. this.add(sp, BorderLayout.CENTER);
  126. this.add(createStatusBar(), BorderLayout.SOUTH);
  127. }
  128. private JMenuBar createMenuBar() {
  129. // Open Map item...
  130. openMapItem = new JMenuItem("Open Map... ",
  131. KeyEvent.VK_O);
  132. openMapItem.setAccelerator(KeyStroke.getKeyStroke(
  133. KeyEvent.VK_O, ActionEvent.ALT_MASK));
  134. openMapItem.addActionListener(new ActionListener() {
  135. @Override
  136. public void actionPerformed(ActionEvent e) {
  137. JFileChooser chooser = new JFileChooser();
  138. FileNameExtensionFilter filter = new FileNameExtensionFilter(
  139. "Map & compressed map files", "map", "map.gz");
  140. chooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
  141. chooser.setFileFilter(filter);
  142. if (chooser.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION) {
  143. BinaryGraphReader reader;
  144. try {
  145. reader = new BinaryGraphReader(
  146. Openfile.open(chooser.getSelectedFile().getAbsolutePath()));
  147. } catch (IOException e1) {
  148. JOptionPane.showMessageDialog(MainWindow.this, "Cannot open the selected file.");
  149. return ;
  150. }
  151. try {
  152. graph = reader.read();
  153. }
  154. catch (Exception exception) {
  155. JOptionPane.showMessageDialog(MainWindow.this, "Unable to read graph from the selected file.");
  156. return ;
  157. }
  158. drawing.clear();
  159. new GraphDrawing(drawing).drawGraph(graph);
  160. for (JMenuItem item: graphItems) {
  161. item.setEnabled(true);
  162. }
  163. mapIdPanel.setText("Map ID: 0x" + Integer.toHexString(graph.getMapId()));
  164. }
  165. }
  166. });
  167. // Open Path item...
  168. JMenuItem openPathItem = new JMenuItem("Open Path... ", KeyEvent.VK_P);
  169. openPathItem.setAccelerator(KeyStroke.getKeyStroke(
  170. KeyEvent.VK_P, ActionEvent.ALT_MASK));
  171. openPathItem.addActionListener(new ActionListener() {
  172. @Override
  173. public void actionPerformed(ActionEvent e) {
  174. JFileChooser chooser = new JFileChooser();
  175. FileNameExtensionFilter filter = new FileNameExtensionFilter(
  176. "Path & compressed path files", "path", "path.gz");
  177. chooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
  178. chooser.setFileFilter(filter);
  179. if (chooser.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION) {
  180. BinaryPathReader reader;
  181. try {
  182. reader = new BinaryPathReader(
  183. Openfile.open(chooser.getSelectedFile().getAbsolutePath()));
  184. } catch (IOException e1) {
  185. JOptionPane.showMessageDialog(MainWindow.this, "Cannot open the selected file.");
  186. return ;
  187. }
  188. try {
  189. currentPath = reader.readPath(graph);
  190. }
  191. catch (MapMismatchException exception) {
  192. JOptionPane.showMessageDialog(MainWindow.this, "The selected file does not contain a path for the current graph.");
  193. return;
  194. }
  195. catch (Exception exception) {
  196. JOptionPane.showMessageDialog(MainWindow.this, "Unable to read path from the selected file.");
  197. return ;
  198. }
  199. new PathDrawing(drawing).drawPath(currentPath);
  200. }
  201. }
  202. });
  203. graphItems.add(openPathItem);
  204. // Close item
  205. JMenuItem closeItem = new JMenuItem("Quit", KeyEvent.VK_Q);
  206. closeItem.setAccelerator(KeyStroke.getKeyStroke(
  207. KeyEvent.VK_Q, ActionEvent.ALT_MASK));
  208. closeItem.addActionListener(new ActionListener() {
  209. @Override
  210. public void actionPerformed(ActionEvent e) {
  211. MainWindow.this.dispatchEvent(new WindowEvent(MainWindow.this, WindowEvent.WINDOW_CLOSING));
  212. }
  213. });
  214. //Build the first menu.
  215. JMenu fileMenu = new JMenu("File");
  216. fileMenu.add(openMapItem);
  217. fileMenu.add(openPathItem);
  218. fileMenu.addSeparator();
  219. fileMenu.add(closeItem);
  220. // Second menu
  221. JMenuItem drawGraphItem = new JMenuItem("Redraw", KeyEvent.VK_R);
  222. drawGraphItem.setAccelerator(KeyStroke.getKeyStroke(
  223. KeyEvent.VK_R, ActionEvent.ALT_MASK));
  224. drawGraphItem.addActionListener(new ActionListener() {
  225. @Override
  226. public void actionPerformed(ActionEvent e) {
  227. drawing.clear();
  228. drawing.setAutoRepaint(true);
  229. new GraphDrawing(drawing).drawGraph(graph);
  230. drawing.setAutoRepaint(false);
  231. }
  232. });
  233. graphItems.add(drawGraphItem);
  234. JMenuItem drawGraphBWItem = new JMenuItem("Redraw (B&W)", KeyEvent.VK_B);
  235. drawGraphBWItem.setAccelerator(KeyStroke.getKeyStroke(
  236. KeyEvent.VK_B, ActionEvent.ALT_MASK));
  237. drawGraphBWItem.addActionListener(new ActionListener() {
  238. @Override
  239. public void actionPerformed(ActionEvent e) {
  240. drawing.clear();
  241. drawing.setAutoRepaint(true);
  242. new GraphDrawing(drawing, new BlackAndWhiteGraphPalette()).drawGraph(graph);
  243. drawing.setAutoRepaint(false);
  244. }
  245. });
  246. graphItems.add(drawGraphBWItem);
  247. JMenu graphMenu = new JMenu("Graph");
  248. graphMenu.add(drawGraphItem);
  249. graphMenu.add(drawGraphBWItem);
  250. // Algo menu
  251. JMenu algoMenu = new JMenu("Algorithms");
  252. JMenuItem wccItem = new JMenuItem("Weakly Connected Components");
  253. wccItem.addActionListener(new ActionListener() {
  254. @Override
  255. public void actionPerformed(ActionEvent e) {
  256. WeaklyConnectedComponentsInstance instance = new WeaklyConnectedComponentsInstance(graph);
  257. WeaklyConnectedComponentsAlgorithm algo = new WeaklyConnectedComponentsAlgorithm(instance);
  258. algo.addObserver(new WeaklyConnectedComponentGraphicObserver(drawing));
  259. (new Thread(algo)).start();
  260. }
  261. });
  262. graphItems.add(wccItem);
  263. algoMenu.add(wccItem);
  264. // Create the menu bar.
  265. JMenuBar menuBar = new JMenuBar();
  266. menuBar.add(fileMenu);
  267. menuBar.add(graphMenu);
  268. menuBar.add(algoMenu);
  269. for (JMenuItem item: graphItems) {
  270. item.setEnabled(false);
  271. }
  272. return menuBar;
  273. }
  274. private JPanel createStatusBar() {
  275. // create the status bar panel and shove it down the bottom of the frame
  276. JPanel statusPanel = new JPanel();
  277. statusPanel.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.GRAY));
  278. statusPanel.setPreferredSize(new Dimension(getWidth(), 20));
  279. statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));
  280. mapIdPanel = new JLabel();
  281. mapIdPanel.setHorizontalAlignment(SwingConstants.LEFT);
  282. statusPanel.add(mapIdPanel);
  283. return statusPanel;
  284. }
  285. public static void main(final String[] args) {
  286. MainWindow w = new MainWindow();
  287. w.setExtendedState(JFrame.MAXIMIZED_BOTH);
  288. w.setVisible(true);
  289. }
  290. }