YauhenMardan

Untitled

Apr 2nd, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. package com.company;
  2.  
  3. import org.jgrapht.alg.cycle.CycleDetector;
  4. import org.jgrapht.graph.DefaultEdge;
  5. import org.jgrapht.graph.SimpleDirectedGraph;
  6. import org.jgrapht.traverse.BreadthFirstIterator;
  7. import javax.swing.event.TableModelEvent;
  8. import javax.swing.event.TableModelListener;
  9. import javax.swing.table.DefaultTableModel;
  10. import java.util.Date;
  11.  
  12. public class Model extends DefaultTableModel {
  13.  
  14. public static final Object[] tableHeader = new String[]{"","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
  15.  
  16. SimpleDirectedGraph<Cell, DefaultEdge> cellGraph;
  17.  
  18. public Model() {
  19. super(tableHeader, 0);
  20. cellGraph=new SimpleDirectedGraph<>(DefaultEdge.class);
  21. addTableModelListener(new TableModelListener() {
  22. @Override
  23. public void tableChanged(TableModelEvent e) {
  24. BreadthFirstIterator<Cell, DefaultEdge> iterator = new BreadthFirstIterator<>(cellGraph);
  25. while (iterator.hasNext()) {
  26. Cell c = iterator.next();
  27. Object o = getValueAt(c.getR(), c.getC());
  28. if (o instanceof Formula) {
  29. refresh((Formula) o, c.getR(), c.getC());
  30. }
  31. }
  32. // TopologicalOrderIterator<Cell, DefaultEdge> iterator=new TopologicalOrderIterator<>(cellGraph);
  33. }
  34. });
  35. }
  36.  
  37. @Override
  38. public void setValueAt(Object aValue, int row, int column) {
  39. if(aValue instanceof Formula){
  40. Formula formula=(Formula) aValue;
  41. refresh(formula,row,column);
  42. }
  43. super.setValueAt(aValue, row, column);
  44. }
  45.  
  46. @Override
  47. public Class<?> getColumnClass(int columnIndex) {
  48. return super.getColumnClass(columnIndex);
  49. }
  50.  
  51. void refresh(Formula formula,int row,int column){
  52. if(formula.getFormulaType()==Formula.Type.DATE){
  53. //nothing to do
  54. }
  55. else if(formula.getFormulaType()==Formula.Type.DATE_P_C){
  56. String string=formula.getFormulaString();
  57. String[] ops=string.split("[=+]");
  58. Date date=FormulaUtil.parse(ops[1]);
  59. date=DateUtil.addDays(date,Integer.parseInt(ops[2]));
  60. formula.setShortFormulaString(FormulaUtil.format(date));
  61. }
  62. else if(formula.getFormulaType()==Formula.Type.DATE_M_C){
  63. String string=formula.getFormulaString();
  64. String[] ops=string.split("[=-]");
  65. Date date=FormulaUtil.parse(ops[1]);
  66. try {
  67. date = DateUtil.subDays(date, Integer.parseInt(ops[2]));
  68. formula.setShortFormulaString(FormulaUtil.format(date));
  69. } catch (IllegalArgumentException e){
  70. formula.setShortFormulaString("#ERROR");
  71. }
  72. }
  73. else if(formula.getFormulaType()==Formula.Type.CELL_P_C) {
  74. String string = formula.getFormulaString();
  75. String[] ops = string.split("[=+]");
  76. int c = FormulaUtil.toIndex(ops[1].charAt(0));
  77. int r = Integer.parseInt(Character.toString(ops[1].charAt(1))) - 1;
  78. Object value = getValueAt(r, c);
  79.  
  80. Cell c1, c2;
  81. c1 = new Cell(row, column);
  82. c2 = new Cell(r, c);
  83. cellGraph.addVertex(c1);
  84. cellGraph.addVertex(c2);
  85.  
  86. cellGraph.addEdge(c1, c2);
  87.  
  88. CycleDetector<Cell, DefaultEdge> detector = new CycleDetector<>(cellGraph);
  89. if (detector.detectCycles()) {
  90. cellGraph.removeEdge(c1,c2);
  91. formula.setShortFormulaString("#CYCLE");
  92. } else {
  93. if (value instanceof Formula) {
  94. if (c1.equals(c2)) {
  95. formula.setShortFormulaString("#ERROR");
  96. } else {
  97. Formula f = (Formula) value;
  98. Date date = FormulaUtil.parse(f.getShortFormulaString());
  99. date = DateUtil.addDays(date, Integer.parseInt(ops[2]));
  100. formula.setShortFormulaString(FormulaUtil.format(date));
  101. }
  102. } else {
  103. formula.setShortFormulaString("#ERROR");
  104. }
  105. }
  106. }
  107. else if(formula.getFormulaType()==Formula.Type.CELL_M_C){
  108.  
  109. }
  110.  
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment