Search in sources :

Example 26 with AUndoableEdit

use of edu.cmu.cs.hcii.cogtool.util.AUndoableEdit in project cogtool by cogtool.

the class DictionaryEditorController method createSetAlgorithmAction.

protected IListenerAction createSetAlgorithmAction() {
    return new IListenerAction() {

        public Class<?> getParameterClass() {
            return DictionaryEditorUI.SetAlgorithmParms.class;
        }

        public boolean performAction(Object actionParms) {
            DictionaryEditorUI.SetAlgorithmParms parms = (DictionaryEditorUI.SetAlgorithmParms) actionParms;
            double similarity;
            if (parms.rowIndices.length == 0) {
                interaction.reportProblem(ERROR_TITLE, NO_SELECTION);
                return false;
            }
            CompoundUndoableEdit editSeq = new CompoundUndoableEdit(SET_ALGORITHM, DictionaryEditorLID.SetAlgorithm);
            for (int rowIndice : parms.rowIndices) {
                DictEntry entry = dictionary.getEntry(rowIndice);
                if (entry == null) {
                    final ITermSimilarity oldAlg = pendingEntry.getDictEntry().algorithm;
                    final double oldSimilarity = pendingEntry.getSimilarity();
                    final ITermSimilarity newAlg = parms.algorithm;
                    boolean equal = (newAlg == null) ? oldAlg == null : newAlg.equals(oldAlg);
                    if (equal) {
                        interaction.setStatusMessage(ALG_UNCHANGED);
                        continue;
                    }
                    pendingEntry.setAlgorithm(newAlg);
                    pendingEntry.setSimilarity(ITermSimilarity.UNKNOWN);
                    editSeq.addEdit(new AUndoableEdit(DictionaryEditorLID.SetAlgorithm) {

                        @Override
                        public String getPresentationName() {
                            return SET_ALGORITHM;
                        }

                        @Override
                        public void redo() {
                            super.redo();
                            pendingEntry.setAlgorithm(newAlg);
                            pendingEntry.setSimilarity(ITermSimilarity.UNKNOWN);
                        }

                        @Override
                        public void undo() {
                            super.undo();
                            pendingEntry.setAlgorithm(oldAlg);
                            pendingEntry.setSimilarity(oldSimilarity);
                        }
                    });
                    interaction.setStatusMessage(ALG_CHANGED);
                    continue;
                }
                if (parms.algorithm.equals(entry.algorithm)) {
                    interaction.setStatusMessage(ALG_UNCHANGED);
                    continue;
                }
                if (checkNewEntry(entry.goalWord, entry.searchWord, parms.algorithm)) {
                    interaction.reportProblem(ERROR_TITLE, ENTRY_EXISTS);
                    continue;
                }
                if (parms.algorithm != ITermSimilarity.MANUAL) {
                    similarity = computeSimilarity(entry.goalWord, entry.searchWord, parms.algorithm);
                } else {
                    similarity = dictionary.getValue(entry).similarity;
                }
                setSimilarity(rowIndice, similarity, parms.algorithm, editSeq);
            }
            editSeq.end();
            if (editSeq.isSignificant()) {
                undoMgr.addEdit(editSeq);
                interaction.setStatusMessage(ALG_CHANGED);
            }
            return true;
        }
    };
}
Also used : DictionaryEditorUI(edu.cmu.cs.hcii.cogtool.ui.DictionaryEditorUI) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) ITermSimilarity(edu.cmu.cs.hcii.cogtool.model.ITermSimilarity) DictEntry(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry) PendingDictEntry(edu.cmu.cs.hcii.cogtool.ui.PendingDictEntry) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)

Example 27 with AUndoableEdit

use of edu.cmu.cs.hcii.cogtool.util.AUndoableEdit in project cogtool by cogtool.

the class DictionaryEditorController method createDeleteEntryAction.

protected IListenerAction createDeleteEntryAction() {
    return new IListenerAction() {

        public Class<?> getParameterClass() {
            return int[].class;
        }

        public boolean performAction(Object actionParms) {
            final int[] rows = (int[]) actionParms;
            final DictEntry[] deletedEntries = new DictEntry[rows.length];
            final DictValue[] deletedValues = new DictValue[rows.length];
            for (int i = rows.length - 1; i >= 0; i--) {
                deletedEntries[i] = dictionary.getEntry(rows[i]);
                if (deletedEntries[i] == null) {
                    interaction.reportProblem(ERROR_TITLE, NO_ENTRY);
                    continue;
                }
                deletedValues[i] = dictionary.getValue(deletedEntries[i]);
                dictionary.removeEntry(rows[i]);
            }
            undoMgr.addEdit(new AUndoableEdit(DictionaryEditorLID.Delete) {

                @Override
                public String getPresentationName() {
                    return (rows.length == 1) ? DELETE_ROW : DELETE_ROWS;
                }

                @Override
                public void redo() {
                    super.redo();
                    for (int i = rows.length - 1; i >= 0; i--) {
                        dictionary.removeEntry(rows[i]);
                    }
                }

                @Override
                public void undo() {
                    super.undo();
                    for (int i = 0; i < rows.length; i++) {
                        dictionary.insertEntry(deletedEntries[i], deletedValues[i], rows[i]);
                    }
                }
            });
            return true;
        }
    };
}
Also used : IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) DictValue(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictValue) DictEntry(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry) PendingDictEntry(edu.cmu.cs.hcii.cogtool.ui.PendingDictEntry) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)

Example 28 with AUndoableEdit

use of edu.cmu.cs.hcii.cogtool.util.AUndoableEdit in project cogtool by cogtool.

the class DictionaryEditorController method createSetStringAction.

protected IListenerAction createSetStringAction(final int column) {
    return new IListenerAction() {

        public Class<?> getParameterClass() {
            return DictionaryEditorUI.SetStringParms.class;
        }

        public boolean performAction(Object actionParms) {
            DictionaryEditorUI.SetStringParms p = (DictionaryEditorUI.SetStringParms) actionParms;
            if (p != null) {
                final int rowIndex = p.rowIndex;
                final String newString = p.string;
                IUndoableEdit edit = null;
                DictEntry entry = dictionary.getEntry(rowIndex);
                if (entry == null) {
                    interaction.reportProblem(ERROR_TITLE, NO_ENTRY);
                    return true;
                }
                CogToolLID lid;
                final String goal;
                final String search;
                final String oldGoal;
                final String oldSearch;
                if (column == 0) {
                    goal = newString;
                    oldGoal = entry.goalWord;
                    if (goal.equals(oldGoal)) {
                        interaction.setStatusMessage(STRING_UNCHANGED);
                        return true;
                    }
                    search = entry.searchWord;
                    oldSearch = search;
                    lid = DictionaryEditorLID.SetGoalString;
                } else {
                    goal = entry.goalWord;
                    oldGoal = goal;
                    search = newString;
                    oldSearch = entry.searchWord;
                    if (search.equals(oldSearch)) {
                        interaction.setStatusMessage(STRING_UNCHANGED);
                        return true;
                    }
                    lid = DictionaryEditorLID.SetSearchString;
                }
                if (checkNewEntry(goal, search, entry.algorithm)) {
                    interaction.reportProblem(ERROR_TITLE, ENTRY_EXISTS);
                    return false;
                }
                dictionary.updateEntry(rowIndex, goal, search);
                CompoundUndoableEdit editSeq = new CompoundUndoableEdit(MODIFY_TERM, lid);
                edit = new AUndoableEdit(lid) {

                    @Override
                    public String getPresentationName() {
                        return MODIFY_TERM;
                    }

                    @Override
                    public void redo() {
                        super.redo();
                        dictionary.updateEntry(rowIndex, goal, search);
                    }

                    @Override
                    public void undo() {
                        super.undo();
                        dictionary.updateEntry(rowIndex, oldGoal, oldSearch);
                    }
                };
                editSeq.addEdit(edit);
                if (entry.algorithm != ITermSimilarity.MANUAL) {
                    double similarity = computeSimilarity(goal, search, entry.algorithm);
                    setSimilarity(rowIndex, similarity, entry.algorithm, editSeq);
                }
                editSeq.end();
                undoMgr.addEdit(editSeq);
            }
            return true;
        }
    };
}
Also used : DictionaryEditorUI(edu.cmu.cs.hcii.cogtool.ui.DictionaryEditorUI) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) CogToolLID(edu.cmu.cs.hcii.cogtool.CogToolLID) DictEntry(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry) PendingDictEntry(edu.cmu.cs.hcii.cogtool.ui.PendingDictEntry) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)

Example 29 with AUndoableEdit

use of edu.cmu.cs.hcii.cogtool.util.AUndoableEdit in project cogtool by cogtool.

the class FrameEditorController method updateFrameName.

// renameEltGroup
/**
     * Update the frame's name.
     * The name must be unique.
     *
     * @param frame
     * @param newName
     * @return
     */
private boolean updateFrameName(final Frame frame, String tryName) {
    final String oldName = frame.getName();
    boolean notDone = true;
    do {
        if (tryName.length() == 0) {
            tryName = interaction.protestNameCannotBeEmpty("Frame");
            // If canceled, indicate so; otherwise, test again
            if (tryName == null) {
                return false;
            }
        } else {
            Frame frameForName = model.getDesign().getFrame(tryName);
            // then no change is necessary!
            if (frameForName == frame) {
                notDone = false;
            } else if (frameForName != null) {
                // A non-null widget for the tryName indicates a collision
                tryName = interaction.protestNameCollision("Frame");
                // If canceled, indicate so; otherwise, test again
                if (tryName == null) {
                    return false;
                }
            } else {
                // Not canceled, not empty, and not a collision
                notDone = false;
                final String newName = tryName;
                frame.setName(newName);
                IUndoableEdit edit = new AUndoableEdit(DesignEditorLID.RenameFrame) {

                    @Override
                    public String getPresentationName() {
                        return CHG_FRAME_NAME;
                    }

                    @Override
                    public void redo() {
                        super.redo();
                        Frame testFrame = design.getFrame(newName);
                        frame.setName(newName);
                        if (testFrame != null) {
                            makeFrameNameUnique(frame);
                        }
                    }

                    @Override
                    public void undo() {
                        super.undo();
                        Frame testFrame = design.getFrame(oldName);
                        frame.setName(oldName);
                        if (testFrame != null) {
                            makeFrameNameUnique(frame);
                        }
                    }
                };
                undoMgr.addEdit(edit);
            }
        }
    } while (notDone);
    return true;
}
Also used : Frame(edu.cmu.cs.hcii.cogtool.model.Frame) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)

Example 30 with AUndoableEdit

use of edu.cmu.cs.hcii.cogtool.util.AUndoableEdit in project cogtool by cogtool.

the class FrameEditorController method createGroupElementsAction.

private IListenerAction createGroupElementsAction() {
    return new IListenerAction() {

        public Class<?> getParameterClass() {
            return FrameEditorSelectionState.class;
        }

        public boolean performAction(Object prms) {
            FrameEditorSelectionState selection = (FrameEditorSelectionState) prms;
            int selectedEltCount = selection.getElementSelectionCount();
            if (selectedEltCount > 1) {
                final FrameElementGroup newGroup = new FrameElementGroup();
                assignUniqueEltGroupName(newGroup);
                Iterator<FrameElement> selectedElts = selection.getSelectedElementsIterator();
                while (selectedElts.hasNext()) {
                    FrameElement rootElt = selectedElts.next().getRootElement();
                    // that the element is a member of the group
                    if (!newGroup.contains(rootElt)) {
                        newGroup.add(rootElt);
                    }
                }
                model.addEltGroup(newGroup);
                IUndoableEdit edit = new AUndoableEdit(FrameEditorLID.Group) {

                    @Override
                    public String getPresentationName() {
                        return GROUP_ELEMENTS;
                    }

                    @Override
                    public void redo() {
                        super.redo();
                        Iterator<FrameElement> groupElts = newGroup.iterator();
                        // back to the frame
                        while (groupElts.hasNext()) {
                            groupElts.next().addToEltGroup(newGroup);
                        }
                        model.addEltGroup(newGroup);
                    }

                    @Override
                    public void undo() {
                        super.undo();
                        model.removeEltGroup(newGroup);
                        Iterator<FrameElement> groupElts = newGroup.iterator();
                        // association from the element.
                        while (groupElts.hasNext()) {
                            groupElts.next().removeFromEltGroup(newGroup);
                        }
                    }
                };
                undoMgr.addEdit(edit);
                return true;
            }
            if (selectedEltCount == 1) {
                interaction.protestTooFewWidgets();
            } else {
                interaction.protestNoSelection();
            }
            return false;
        }
    };
}
Also used : IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) FrameEditorSelectionState(edu.cmu.cs.hcii.cogtool.ui.FrameEditorSelectionState) FrameElementGroup(edu.cmu.cs.hcii.cogtool.model.FrameElementGroup) FrameElement(edu.cmu.cs.hcii.cogtool.model.FrameElement) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint)

Aggregations

AUndoableEdit (edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)66 IUndoableEdit (edu.cmu.cs.hcii.cogtool.util.IUndoableEdit)34 IListenerAction (edu.cmu.cs.hcii.cogtool.util.IListenerAction)21 DoublePoint (edu.cmu.cs.hcii.cogtool.model.DoublePoint)16 AUndertaking (edu.cmu.cs.hcii.cogtool.model.AUndertaking)14 TaskApplication (edu.cmu.cs.hcii.cogtool.model.TaskApplication)14 ITaskDesign (edu.cmu.cs.hcii.cogtool.model.Project.ITaskDesign)12 CompoundUndoableEdit (edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit)10 Demonstration (edu.cmu.cs.hcii.cogtool.model.Demonstration)9 ComputationUndoRedo (edu.cmu.cs.hcii.cogtool.controller.DemoScriptCmd.ComputationUndoRedo)7 DefaultModelGeneratorState (edu.cmu.cs.hcii.cogtool.model.DefaultModelGeneratorState)7 Design (edu.cmu.cs.hcii.cogtool.model.Design)7 Frame (edu.cmu.cs.hcii.cogtool.model.Frame)7 TaskGroup (edu.cmu.cs.hcii.cogtool.model.TaskGroup)7 TaskParent (edu.cmu.cs.hcii.cogtool.model.TaskParent)7 DictEntry (edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry)6 ProjectSelectionState (edu.cmu.cs.hcii.cogtool.ui.ProjectSelectionState)6 UndoManager (edu.cmu.cs.hcii.cogtool.util.UndoManager)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6