from pathlib import Path
p=Path('C:/MCSJAgent/SwingBridge/src/SwingBridgeAgentV15.java')
s=p.read_text(encoding='utf-8')
s=s.replace('public class SwingBridgeAgentV15','public class SwingBridgeAgentV16')
helper=r'''
    private static String fxCommitTableCell(Object table, Object column, int row, String newValue) {
        try {
            try { table.getClass().getMethod("applyCss").invoke(table); } catch (Throwable ignore) {}
            try { table.getClass().getMethod("layout").invoke(table); } catch (Throwable ignore) {}
            List<Object> nodes = new ArrayList<Object>();
            collectFxDedup(table, nodes, new IdentityHashMap<Object, Boolean>());
            Object cell = null;
            for (Object node : nodes) {
                if (!isInstanceOfFx(node, "javafx.scene.control.TableCell")) continue;
                int index = ((Number) node.getClass().getMethod("getIndex").invoke(node)).intValue();
                Object nodeColumn = node.getClass().getMethod("getTableColumn").invoke(node);
                Object nodeTable = node.getClass().getMethod("getTableView").invoke(node);
                if (index == row && nodeColumn == column && nodeTable == table) { cell = node; break; }
            }
            if (cell == null) return "resolved table cell is not realized after scroll/layout";
            boolean editing = ((Boolean) cell.getClass().getMethod("isEditing").invoke(cell)).booleanValue();
            if (!editing) return "resolved table cell is not editing";
            Method commitEdit = cell.getClass().getMethod("commitEdit", Object.class);
            commitEdit.invoke(cell, newValue);
            return null;
        } catch (Throwable t) {
            Throwable cause = t instanceof InvocationTargetException && ((InvocationTargetException) t).getCause() != null
                ? ((InvocationTargetException) t).getCause() : t;
            return "table-cell commit failed: " + cause;
        }
    }

'''
needle='    private static String fxTableCell(List<Component> panels, String panelClass, int panelOccurrence,'
assert needle in s
s=s.replace(needle,helper+needle,1)
old='''        if (!("select".equals(action) || "edit".equals(action) || "doubleclick".equals(action))) {
            return errorJson("action must be select, edit, or doubleclick");
        }'''
new='''        String commitValue = null;
        if (action.startsWith("commit|")) { commitValue = action.substring(7); action = "commit"; }
        if (!("select".equals(action) || "edit".equals(action) || "doubleclick".equals(action) || "commit".equals(action))) {
            return errorJson("action must be select, edit, doubleclick, or commit|value");
        }'''
assert old in s
s=s.replace(old,new,1)
old='''        } else if ("doubleclick".equals(action)) {
            String failure = fxFireTableCellDoubleClick(table, col, row);
            if (failure != null) return errorJson(failure);
        }
        int selectedRow'''
new='''        } else if ("doubleclick".equals(action)) {
            String failure = fxFireTableCellDoubleClick(table, col, row);
            if (failure != null) return errorJson(failure);
        } else if ("commit".equals(action)) {
            Object editingCell = table.getClass().getMethod("getEditingCell").invoke(table);
            if (editingCell == null) return errorJson("TableView has no active editing cell");
            int editingRow = ((Number) editingCell.getClass().getMethod("getRow").invoke(editingCell)).intValue();
            Object editingColumn = editingCell.getClass().getMethod("getTableColumn").invoke(editingCell);
            if (editingRow != row || editingColumn != col) return errorJson("TableView editing-cell guard mismatch");
            String failure = fxCommitTableCell(table, col, row, commitValue);
            if (failure != null) return errorJson(failure);
            String committed = fxTableCellText(col, row);
            if (!commitValue.equals(committed)) return errorJson("committed backing-row readback mismatch: " + committed);
            actual = committed;
        }
        int selectedRow'''
assert old in s
s=s.replace(old,new,1)
old='''        String verification = "doubleclick".equals(action) ? "event-dispatched-to-resolved-cell"
            : ("edit".equals(action) ? "editing-cell-readback" : "selection-focus-readback");'''
new='''        String verification = "doubleclick".equals(action) ? "event-dispatched-to-resolved-cell"
            : ("edit".equals(action) ? "editing-cell-readback" : ("commit".equals(action) ? "backing-row-commit-readback" : "selection-focus-readback"));'''
assert old in s
s=s.replace(old,new,1)
out=Path('C:/MCSJAgent/SwingBridge/src/SwingBridgeAgentV16.java')
out.write_text(s,encoding='utf-8')
print(out, len(s))
