Search in sources :

Example 46 with AUndoableEdit

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

the class FrameEditorController method captureImageAction.

// createChangeAuxTextPropertyAction
/**
     * Create a ListenerAction which will handle capturing a background image.
     * @return
     */
private IListenerAction captureImageAction() {
    return new IListenerAction() {

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

        public boolean performAction(Object prms) {
            CompoundUndoableEdit editSequence = new CompoundUndoableEdit(CAPTURE_BKG_IMG, FrameEditorLID.CaptureImageProperty);
            // Get the selection.
            FrameEditorSelectionState selection = (FrameEditorSelectionState) prms;
            Iterator<IWidget> selected = selection.getSelectedWidgetsIterator();
            // Iterate over every selected widget
            while (selected.hasNext()) {
                final IWidget w = selected.next();
                DoubleRectangle bounds = w.getEltBounds();
                // Get the image from the background, and crop to shape
                final byte[] bg = GraphicsUtil.cropImage(model.getBackgroundImage(), bounds.x, bounds.y, bounds.width, bounds.height);
                // Get the old image, could be null.
                final byte[] old = w.getImage();
                final String previousImagePath = (String) w.getAttribute(WidgetAttributes.IMAGE_PATH_ATTR);
                w.setImage(bg);
                w.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, WidgetAttributes.NO_IMAGE);
                editSequence.addEdit(new AUndoableEdit(FrameEditorLID.CaptureImageProperty) {

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

                    @Override
                    public void redo() {
                        super.redo();
                        w.setImage(bg);
                        w.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, WidgetAttributes.NO_IMAGE);
                    }

                    @Override
                    public void undo() {
                        super.undo();
                        w.setImage(old);
                        w.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, previousImagePath);
                    }
                });
            }
            editSequence.end();
            // Only add this edit if it is significant
            if (editSequence.isSignificant()) {
                undoMgr.addEdit(editSequence);
            }
            return true;
        }
    };
}
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) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) DoubleRectangle(edu.cmu.cs.hcii.cogtool.model.DoubleRectangle) IWidget(edu.cmu.cs.hcii.cogtool.model.IWidget)

Example 47 with AUndoableEdit

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

the class DictionaryEditorCmd method importDictionary.

/**
     * Replace prevDict with dict after reading the entries from the file.
     * @param csvFile TODO
     */
// I (dfm) believe the above is a lie. I think prevDict was already replaced 
// by dict by the caller, and prevDict is simply a vestigal bit of data that
// is passed around and encapsulated in undo and redo forms for no reason
// at all. But I'm not sufficiently confident of that diagnosis to zap it
// just yet....
public static boolean importDictionary(final Design design, final ISimilarityDictionary dict, final ISimilarityDictionary prevDict, Interaction interaction, IUndoableEditSequence editSeq, String csvFile) {
    File dataFile = (csvFile != null ? new File(csvFile) : interaction.selectCSVFile());
    if (dataFile == null) {
        return false;
    }
    FileReader reader = null;
    List<String> outputLines = new ArrayList<String>();
    try {
        reader = new FileReader(dataFile);
        FileUtil.readLines(reader, outputLines);
    } catch (IOException e) {
        interaction.reportProblem("File I/O Error", e.getMessage());
        return false;
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            interaction.reportProblem("File I/O Error on Close", e.getMessage());
            return false;
        }
    }
    if (outputLines.size() == 0) {
        interaction.reportProblem("Empty file", "Requested .csv file is empty");
        return false;
    }
    if (outputLines.size() < 3) {
        interaction.reportProblem("Invalid file", "Requested file is not a valid dictionary file");
        return false;
    }
    String[] headerCol = CSVSupport.getCells(outputLines.get(0));
    if ((headerCol.length != 2) || !(ISimilarityDictionary.COGTOOL_DICT_HEADER.equals(headerCol[0])) || !headerCol[1].equals(DICT_CSV_VERSION_0)) {
        interaction.reportProblem("Invalid file", "Requested file is not a valid dictionary file");
        return false;
    }
    final Map<DictEntry, DictValue> newEntries = new LinkedHashMap<DictEntry, DictValue>();
    for (int i = 2; i < outputLines.size(); i++) {
        String newLine = outputLines.get(i);
        String[] cols = CSVSupport.getCells(newLine);
        // [required] 0 is goal string
        // [required] 1 is search string
        // [required] 2 is algorithm name
        // [optional] 3 is site (for Google algs) or url (for LSA)
        // [optional] 4 is term space (for LSA)
        String site = (cols.length > 3) ? cols[3] : "";
        String space = (cols.length > 4) ? cols[4] : "";
        DictEntry entry = new DictEntry(cols[0], cols[1], getNameAlgorithm(cols[2], site, space));
        double similarity;
        Date editedDate;
        try {
            similarity = parseDouble(cols[4]);
            editedDate = new PersistentDate(PersistentDate.DATE_FORMAT.parse(cols[5]));
        } catch (Exception e) {
            throw new RcvrParsingException("Parsing error", e);
        }
        DictValue value = new DictValue(similarity, editedDate);
        newEntries.put(entry, value);
    }
    dict.insertEntries(newEntries, false);
    if (editSeq != null) {
        editSeq.addEdit(new AUndoableEdit(ProjectLID.ImportDict) {

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

            @Override
            public void redo() {
                super.redo();
                if (!NullSafe.equals(prevDict, WidgetAttributes.NO_DICTIONARY)) {
                    design.setAttribute(WidgetAttributes.DICTIONARY_ATTR, dict);
                } else {
                    dict.insertEntries(newEntries, false);
                }
            }

            @Override
            public void undo() {
                super.undo();
                if (!NullSafe.equals(prevDict, WidgetAttributes.NO_DICTIONARY)) {
                    design.setAttribute(WidgetAttributes.DICTIONARY_ATTR, prevDict);
                } else {
                    dict.removeEntries(newEntries);
                }
            }
        });
    }
    if (interaction != null) {
        interaction.setStatusMessage("Import successful");
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) RcvrParsingException(edu.cmu.cs.hcii.cogtool.util.RcvrParsingException) IOException(java.io.IOException) PersistentDate(edu.cmu.cs.hcii.cogtool.util.PersistentDate) Date(java.util.Date) PersistentDate(edu.cmu.cs.hcii.cogtool.util.PersistentDate) RcvrParsingException(edu.cmu.cs.hcii.cogtool.util.RcvrParsingException) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) DictValue(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictValue) DictEntry(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictEntry) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit) FileReader(java.io.FileReader) File(java.io.File)

Example 48 with AUndoableEdit

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

the class DictionaryEditorController method createAddNewTermAction.

protected IListenerAction createAddNewTermAction() {
    return new IListenerAction() {

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

        public boolean performAction(Object actionParms) {
            final DictionaryEditorUI.AddStringParms parms = (DictionaryEditorUI.AddStringParms) actionParms;
            final String oldString;
            if (parms.isGoal) {
                oldString = pendingEntry.getDictEntry().goalWord;
                if (parms.newString.equals(oldString)) {
                    interaction.setStatusMessage(STRING_UNCHANGED);
                    return true;
                }
                pendingEntry.setGoal(parms.newString);
            } else {
                oldString = pendingEntry.getDictEntry().searchWord;
                if (parms.newString.equals(oldString)) {
                    interaction.setStatusMessage(STRING_UNCHANGED);
                    return true;
                }
                pendingEntry.setSearch(parms.newString);
            }
            undoMgr.addEdit(new AUndoableEdit(DictionaryEditorLID.StartNewEntry) {

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

                @Override
                public void redo() {
                    super.redo();
                    if (parms.isGoal) {
                        pendingEntry.setGoal(parms.newString);
                    } else {
                        pendingEntry.setSearch(parms.newString);
                    }
                }

                @Override
                public void undo() {
                    super.undo();
                    if (parms.isGoal) {
                        pendingEntry.setGoal(oldString);
                    } else {
                        pendingEntry.setSearch(oldString);
                    }
                }
            });
            return true;
        }
    };
}
Also used : DictionaryEditorUI(edu.cmu.cs.hcii.cogtool.ui.DictionaryEditorUI) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)

Example 49 with AUndoableEdit

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

the class DictionaryEditorController method createAddNewEntryAction.

protected IListenerAction createAddNewEntryAction() {
    return new IListenerAction() {

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

        public boolean performAction(Object actionParms) {
            final DictionaryEditorUI.ComputeSimilarityParms parms = (DictionaryEditorUI.ComputeSimilarityParms) actionParms;
            final ITermSimilarity oldAlg = pendingEntry.getDictEntry().algorithm;
            final double oldSimilarity = pendingEntry.getSimilarity();
            final double similarity;
            if (checkNewEntry(parms.goalString, parms.searchString, parms.algorithm)) {
                interaction.reportProblem(ERROR_TITLE, ENTRY_EXISTS);
                return false;
            }
            if (parms.algorithm == ITermSimilarity.MANUAL) {
                similarity = oldSimilarity;
            } else {
                similarity = computeSimilarity(parms.goalString, parms.searchString, parms.algorithm);
            }
            final String oldGoal = pendingEntry.getDictEntry().goalWord;
            final String oldSearch = pendingEntry.getDictEntry().searchWord;
            pendingEntry.clear();
            final DictValue value = new DictValue(similarity);
            dictionary.setSimilarity(parms.goalString, parms.searchString, parms.algorithm, value, parms.rowIndex);
            undoMgr.addEdit(new AUndoableEdit(DictionaryEditorLID.CreateNewEntry) {

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

                @Override
                public void redo() {
                    super.redo();
                    pendingEntry.clear();
                    dictionary.setSimilarity(parms.goalString, parms.searchString, parms.algorithm, value, parms.rowIndex);
                }

                @Override
                public void undo() {
                    super.undo();
                    dictionary.removeEntry(parms.rowIndex);
                    pendingEntry.setGoal(oldGoal);
                    pendingEntry.setSearch(oldSearch);
                    pendingEntry.setAlgorithm(oldAlg);
                    pendingEntry.setSimilarity(oldSimilarity);
                }
            });
            return true;
        }
    };
}
Also used : DictionaryEditorUI(edu.cmu.cs.hcii.cogtool.ui.DictionaryEditorUI) IListenerAction(edu.cmu.cs.hcii.cogtool.util.IListenerAction) ITermSimilarity(edu.cmu.cs.hcii.cogtool.model.ITermSimilarity) DictValue(edu.cmu.cs.hcii.cogtool.model.ISimilarityDictionary.DictValue) AUndoableEdit(edu.cmu.cs.hcii.cogtool.util.AUndoableEdit)

Example 50 with AUndoableEdit

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

the class DictionaryEditorController method createSetSimilarityAction.

protected IListenerAction createSetSimilarityAction() {
    return new IListenerAction() {

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

        public boolean performAction(Object actionParms) {
            DictionaryEditorUI.SetSimilarityParms parms = (DictionaryEditorUI.SetSimilarityParms) actionParms;
            DictEntry entry = dictionary.getEntry(parms.rowIndex);
            if (entry == null) {
                final double oldSimil = pendingEntry.getSimilarity();
                final double newSimil = parms.similarity;
                if (PrecisionUtilities.withinEpsilon(oldSimil, newSimil, 0.001)) {
                    interaction.setStatusMessage(SIMIL_UNCHANGED);
                    return true;
                }
                final ITermSimilarity oldAlg = pendingEntry.getDictEntry().algorithm;
                pendingEntry.setSimilarity(newSimil);
                pendingEntry.setAlgorithm(ITermSimilarity.MANUAL);
                undoMgr.addEdit(new AUndoableEdit(DictionaryEditorLID.SetSimilarity) {

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

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

                    @Override
                    public void undo() {
                        super.undo();
                        pendingEntry.setSimilarity(oldSimil);
                        pendingEntry.setAlgorithm(oldAlg);
                    }
                });
                return true;
            }
            if (PrecisionUtilities.withinEpsilon(dictionary.getValue(entry).similarity, parms.similarity, 0.001)) {
                interaction.setStatusMessage(SIMIL_UNCHANGED);
                return true;
            }
            setSimilarity(parms.rowIndex, parms.similarity, ITermSimilarity.MANUAL, undoMgr);
            return true;
        }
    };
}
Also used : DictionaryEditorUI(edu.cmu.cs.hcii.cogtool.ui.DictionaryEditorUI) 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)

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