Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import org.jgrapht.alg.cycle.CycleDetector;
- import org.jgrapht.graph.DefaultEdge;
- import org.jgrapht.graph.SimpleDirectedGraph;
- import org.jgrapht.traverse.BreadthFirstIterator;
- import javax.swing.event.TableModelEvent;
- import javax.swing.event.TableModelListener;
- import javax.swing.table.DefaultTableModel;
- import java.util.Date;
- public class Model extends DefaultTableModel {
- 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"};
- SimpleDirectedGraph<Cell, DefaultEdge> cellGraph;
- public Model() {
- super(tableHeader, 0);
- cellGraph=new SimpleDirectedGraph<>(DefaultEdge.class);
- addTableModelListener(new TableModelListener() {
- @Override
- public void tableChanged(TableModelEvent e) {
- BreadthFirstIterator<Cell, DefaultEdge> iterator = new BreadthFirstIterator<>(cellGraph);
- while (iterator.hasNext()) {
- Cell c = iterator.next();
- Object o = getValueAt(c.getR(), c.getC());
- if (o instanceof Formula) {
- refresh((Formula) o, c.getR(), c.getC());
- }
- }
- // TopologicalOrderIterator<Cell, DefaultEdge> iterator=new TopologicalOrderIterator<>(cellGraph);
- }
- });
- }
- @Override
- public void setValueAt(Object aValue, int row, int column) {
- if(aValue instanceof Formula){
- Formula formula=(Formula) aValue;
- refresh(formula,row,column);
- }
- super.setValueAt(aValue, row, column);
- }
- @Override
- public Class<?> getColumnClass(int columnIndex) {
- return super.getColumnClass(columnIndex);
- }
- void refresh(Formula formula,int row,int column){
- if(formula.getFormulaType()==Formula.Type.DATE){
- //nothing to do
- }
- else if(formula.getFormulaType()==Formula.Type.DATE_P_C){
- String string=formula.getFormulaString();
- String[] ops=string.split("[=+]");
- Date date=FormulaUtil.parse(ops[1]);
- date=DateUtil.addDays(date,Integer.parseInt(ops[2]));
- formula.setShortFormulaString(FormulaUtil.format(date));
- }
- else if(formula.getFormulaType()==Formula.Type.DATE_M_C){
- String string=formula.getFormulaString();
- String[] ops=string.split("[=-]");
- Date date=FormulaUtil.parse(ops[1]);
- try {
- date = DateUtil.subDays(date, Integer.parseInt(ops[2]));
- formula.setShortFormulaString(FormulaUtil.format(date));
- } catch (IllegalArgumentException e){
- formula.setShortFormulaString("#ERROR");
- }
- }
- else if(formula.getFormulaType()==Formula.Type.CELL_P_C) {
- String string = formula.getFormulaString();
- String[] ops = string.split("[=+]");
- int c = FormulaUtil.toIndex(ops[1].charAt(0));
- int r = Integer.parseInt(Character.toString(ops[1].charAt(1))) - 1;
- Object value = getValueAt(r, c);
- Cell c1, c2;
- c1 = new Cell(row, column);
- c2 = new Cell(r, c);
- cellGraph.addVertex(c1);
- cellGraph.addVertex(c2);
- cellGraph.addEdge(c1, c2);
- CycleDetector<Cell, DefaultEdge> detector = new CycleDetector<>(cellGraph);
- if (detector.detectCycles()) {
- cellGraph.removeEdge(c1,c2);
- formula.setShortFormulaString("#CYCLE");
- } else {
- if (value instanceof Formula) {
- if (c1.equals(c2)) {
- formula.setShortFormulaString("#ERROR");
- } else {
- Formula f = (Formula) value;
- Date date = FormulaUtil.parse(f.getShortFormulaString());
- date = DateUtil.addDays(date, Integer.parseInt(ops[2]));
- formula.setShortFormulaString(FormulaUtil.format(date));
- }
- } else {
- formula.setShortFormulaString("#ERROR");
- }
- }
- }
- else if(formula.getFormulaType()==Formula.Type.CELL_M_C){
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment