use of edu.cmu.cs.hcii.cogtool.CogToolLID in project cogtool by cogtool.
the class FrameEditorUI method setViewEnabledState.
/**
* Enables or disables LIDs as appropriate
* @param sel the selection state on which to base enabling/disabling
* @param availability NORMAL or CONTEXT
* @see ListenerIdentifierMap
*/
protected void setViewEnabledState(FrameEditorSelectionState sel, Boolean availability) {
String label = "";
int widgetCount = sel.getWidgetSelectionCount();
int elementCount = sel.getElementSelectionCount();
IWidget selectedWidget = null;
FrameElement selectedElement = null;
if ((widgetCount > 0) || (elementCount > 0)) {
if (widgetCount == 1) {
label = " " + WIDGET_LABEL;
selectedWidget = sel.getSelectedIWidgets()[0];
selectedElement = selectedWidget;
} else {
label = " " + WIDGETS_LABEL;
if (elementCount == 1) {
selectedElement = sel.getSelectedIFrameElements()[0];
}
}
}
Text t = WindowUtil.getFocusedText();
boolean editing = ((editor != null) && editor.getVisible());
String cutCopyLabel = (editing || (t != null)) ? "" : label;
// Turn on rename support if selection is exactly 1
boolean enabled = (widgetCount == 1);
String renameString = MenuFactory.RENAME_STRING;
String relabelString = MenuFactory.RELABEL_STRING;
if (enabled) {
renameString += label;
relabelString += label;
}
setEnabled(CogToolLID.Rename, availability, enabled, renameString);
setEnabled(FrameEditorLID.Relabel, availability, enabled, relabelString);
setEnabled(CogToolLID.CopyPath, availability, enabled);
setEnabled(FrameEditorLID.RemoveImageProperty, availability, (selectedWidget != null) && (selectedWidget.getImage() != null));
setEnabled(FrameEditorLID.ToggleRenderSkin, availability, enabled, MenuFactory.RENDER_WIDGET_SKIN_LABEL, (selectedWidget != null) && selectedWidget.isRendered());
if (enabled && (selectedWidget != null)) {
enabled = selectedWidget.getAttribute(WidgetAttributes.REMOTE_LABEL_OWNER_ATTR) != null;
}
setEnabled(FrameEditorLID.SetRemoteLabelType, availability, enabled);
if (elementCount == 1) {
enabled = selectedElement.getRemoteLabelOwner() != null;
}
setEnabled(FrameEditorLID.SetRemoteLabelText, availability, enabled);
// Turn on these LIDs if the selection is > 0
enabled = (widgetCount > 0);
setEnabled(FrameEditorLID.SetImageProperty, availability, enabled);
setEnabled(FrameEditorLID.CaptureImageProperty, availability, enabled && view.isBackgroundAvailable());
enabled = (elementCount > 0);
String deleteString = MenuFactory.DELETE_STRING + label;
setEnabled(CogToolLID.Delete, availability, enabled, deleteString);
String dupString = MenuFactory.DUPLICATE_STRING + label;
setEnabled(CogToolLID.Duplicate, availability, enabled, dupString);
// For these, there must exist a non-IChildWidget selected widget
int nonchildWidgetCount = sel.getNonchildSelectionCount();
enabled = (nonchildWidgetCount > 0);
String cutString = MenuFactory.CUT_STRING + cutCopyLabel;
setEnabled(CogToolLID.Cut, availability, enabled, cutString);
String copyString = MenuFactory.COPY_STRING + cutCopyLabel;
setEnabled(CogToolLID.Copy, availability, enabled, copyString);
setEnabled(CogToolLID.SetFrameTemplate, availability, enabled);
setEnabled(CogToolLID.NudgeLeft, availability, enabled);
setEnabled(CogToolLID.NudgeRight, availability, enabled);
setEnabled(CogToolLID.NudgeDown, availability, enabled);
setEnabled(CogToolLID.NudgeUp, availability, enabled);
setEnabled(CogToolLID.BringToFront, availability, enabled);
setEnabled(CogToolLID.BringForward, availability, enabled);
setEnabled(CogToolLID.SendBackward, availability, enabled);
setEnabled(CogToolLID.SendToBack, availability, enabled);
// Grouping enabled only if multiple unrelated IFrameElements are selected
enabled = false;
FrameElement firstSelectedElt = null;
Iterator<FrameElement> selectedElts = sel.getSelectedElementsIterator();
while (selectedElts.hasNext()) {
FrameElement elt = selectedElts.next();
if (firstSelectedElt == null) {
firstSelectedElt = elt.getRootElement();
} else if (firstSelectedElt != elt.getRootElement()) {
enabled = true;
break;
}
}
setEnabled(CogToolLID.Group, availability, enabled);
enabled = false;
selectedElts = sel.getSelectedElementsIterator();
while (selectedElts.hasNext()) {
FrameElement elt = selectedElts.next();
enabled = enabled || (elt instanceof FrameElementGroup) || (elt.getRootElement().getEltGroups().size() > 0);
}
setEnabled(CogToolLID.Ungroup, availability, enabled);
// don't allow alignment if any of the selected widgets are part of a
// widget group (e.g. menu headers, list box items, grid buttons)
// TODO might want to allow it for grid buttons, with extra work in the
// controller to calculate horizontal and vertical space
boolean groupWidgetSelected = false;
IWidget[] widgets = sel.getSelectedIWidgets();
for (IWidget widget : widgets) {
if (widget.getParentGroup() != null) {
groupWidgetSelected = true;
}
}
// Enable alignment items if multiple non-child widgets are selected
enabled = (nonchildWidgetCount > 1);
setEnabled(CogToolLID.AlignTop, availability, enabled);
setEnabled(CogToolLID.AlignBottom, availability, enabled);
setEnabled(CogToolLID.AlignLeft, availability, enabled);
setEnabled(CogToolLID.AlignRight, availability, enabled);
setEnabled(CogToolLID.AlignCenter, availability, enabled);
setEnabled(CogToolLID.AlignHorizCenter, availability, enabled);
setEnabled(CogToolLID.AlignVertCenter, availability, enabled);
// Enable spacing items if at least 3 non-child widgets are selected
enabled = (nonchildWidgetCount >= 3) && !groupWidgetSelected;
setEnabled(CogToolLID.SpaceVertically, availability, enabled);
setEnabled(CogToolLID.SpaceHorizontally, availability, enabled);
// If there is at least one widget in the model, enable these.
enabled = (frame.getWidgets().size() > 0);
setEnabled(CogToolLID.SelectAll, availability, enabled, SELECT_ALL_WIDGETS);
// Draws the dot to indicate that the correct skin type is selected
setSelected(CogToolLID.SkinNone, availability, false);
setSelected(CogToolLID.SkinWireFrame, availability, false);
setSelected(CogToolLID.SkinMacOSX, availability, false);
setSelected(CogToolLID.SkinWinXP, availability, false);
setSelected(CogToolLID.SkinPalm, availability, false);
SkinType skin = design.getSkin();
CogToolLID id = null;
if (skin == SkinType.None) {
id = CogToolLID.SkinNone;
} else if (skin == SkinType.WireFrame) {
id = CogToolLID.SkinWireFrame;
} else if (skin == SkinType.MacOSX) {
id = CogToolLID.SkinMacOSX;
} else if (skin == SkinType.WinXP) {
id = CogToolLID.SkinWinXP;
} else if (skin == SkinType.Palm) {
id = CogToolLID.SkinPalm;
}
if (id != null) {
setSelected(id, availability, true);
}
setEnabled(CogToolLID.ClearFrameTemplate, ListenerIdentifierMap.ALL, FrameTemplateSupport.hasFrameTemplate(design));
}
use of edu.cmu.cs.hcii.cogtool.CogToolLID 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;
}
};
}
use of edu.cmu.cs.hcii.cogtool.CogToolLID in project cogtool by cogtool.
the class DesignEditorController method spaceFramesEqually.
/**
* Spaces the selected frames equally along a certain axis
* @param vertical true for vertical axis; false for horizontal
*/
protected boolean spaceFramesEqually(Map<Frame, DoubleRectangle> frameMap, final boolean vertical) {
final String undoRedoLabel = vertical ? SPACE_VERTICALLY : SPACE_HORIZONTALLY;
CogToolLID lid = vertical ? DesignEditorLID.SpaceVertically : DesignEditorLID.SpaceHorizontally;
CompoundUndoableEdit editSequence = new CompoundUndoableEdit(undoRedoLabel, lid);
// Order the widgets according to location
// (either from the left or from the top)
Comparator<Map.Entry<Frame, DoubleRectangle>> c = vertical ? frameVerticalComparator : frameHorizontalComparator;
@SuppressWarnings("unchecked") Map.Entry<Frame, DoubleRectangle>[] entries = new Map.Entry[frameMap.size()];
frameMap.entrySet().toArray(entries);
Arrays.sort(entries, c);
// Calculate the spacing between widgets
double sum = 0;
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
// this can then be used to do spacing.
for (Entry<Frame, DoubleRectangle> entrie : entries) {
DoubleRectangle bounds = entrie.getValue();
double size = vertical ? bounds.height : bounds.width;
double position = vertical ? bounds.y : bounds.x;
sum += size;
min = Math.min(min, position);
max = Math.max(max, size + position);
}
// Get the spacing to use between each item.
double spacing = ((max - min) - sum) / (entries.length - 1);
// Adjust the spacings to the correct values
for (Entry<Frame, DoubleRectangle> entrie : entries) {
// go through each frame and set the frame's origin
final Frame f = entrie.getKey();
final DoubleRectangle bounds = entrie.getValue();
// TODO (dfm) this is probably the place to walk over the design pre-flighting
// problems we want to warn the user about, and give the opportunity
// to skip the export if desired
// Determine the old point...
final double p_old = vertical ? bounds.y : bounds.x;
final double p_new = min;
// Set the new location
f.setFrameOrigin(vertical ? bounds.x : p_new, vertical ? p_new : bounds.y);
// Advance the pointer to the next location
min += spacing + (vertical ? bounds.height : bounds.width);
IUndoableEdit edit = new AUndoableEdit(lid) {
@Override
public String getPresentationName() {
return undoRedoLabel;
}
@Override
public void redo() {
super.redo();
f.setFrameOrigin(vertical ? bounds.x : p_new, vertical ? p_new : bounds.y);
}
@Override
public void undo() {
super.undo();
f.setFrameOrigin(vertical ? bounds.x : p_old, vertical ? p_old : bounds.y);
}
};
editSequence.addEdit(edit);
}
editSequence.end();
// Only add this edit if it is significant
if (editSequence.isSignificant()) {
undoMgr.addEdit(editSequence);
}
return true;
}
use of edu.cmu.cs.hcii.cogtool.CogToolLID in project cogtool by cogtool.
the class FrameEditorController method setBackgroundImage.
/**
* Sets the background image for the frame
*
* @param imageData the new image, or null if none
*/
private void setBackgroundImage(final byte[] imageData, final String imagePath) {
try {
// compute the size of the new image.
final DoubleRectangle imageSize = GraphicsUtil.getImageBounds(imageData);
// Get existing image
final byte[] previousImageData = model.getBackgroundImage();
final DoubleRectangle previmageSize = model.getBackgroundBounds();
final String oldPath = (String) model.getAttribute(WidgetAttributes.IMAGE_PATH_ATTR);
// Perform operation
model.setBackgroundImage(imageData, imageSize);
model.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, imagePath);
CogToolLID lid = (imageData == null) ? FrameEditorLID.RemoveBackgroundImage : FrameEditorLID.SetBackgroundImage;
// Add the undo edit
IUndoableEdit edit = new AUndoableEdit(lid) {
@Override
public String getPresentationName() {
return (imageData == null) ? REMOVE_BKG_IMG : SET_BKG_IMG;
}
@Override
public void redo() {
super.redo();
try {
model.setBackgroundImage(imageData, imageSize);
model.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, imagePath);
} catch (GraphicsUtil.ImageException ex) {
throw new RcvrImageException("Redo set background image failed", ex);
}
}
@Override
public void undo() {
super.undo();
try {
model.setBackgroundImage(previousImageData, previmageSize);
model.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, oldPath);
} catch (GraphicsUtil.ImageException ex) {
throw new RcvrImageException("Undo set background image failed", ex);
}
}
};
undoMgr.addEdit(edit);
} catch (GraphicsUtil.ImageException e) {
interaction.protestInvalidImageFile();
}
}
use of edu.cmu.cs.hcii.cogtool.CogToolLID in project cogtool by cogtool.
the class FrameEditorController method spaceElementsEqually.
/**
* Spaces the selected frame elements equally along a certain axis
* @param vertical true for vertical axis; false for horizontal
*/
private boolean spaceElementsEqually(FrameEditorSelectionState selection, boolean vertical) {
// Order the widgets according to location
// (either from the left or from the top)
Comparator<FrameElement> c = vertical ? elementVerticalComparator : elementHorizontalComparator;
Set<FrameElement> elements = getSelectedElements(selection, c);
if (elements.size() <= 2) {
interaction.protestTooFewElements();
return false;
}
// Calculate the spacing between widgets
double sum = 0;
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
// Go through each element that is selected
// Determine the size, min & max of the region.
// this can then be used to do spacing.
Iterator<FrameElement> eltIter = elements.iterator();
while (eltIter.hasNext()) {
DoubleRectangle bounds = eltIter.next().getEltBounds();
double size = vertical ? bounds.height : bounds.width;
double position = vertical ? bounds.y : bounds.x;
sum += size;
min = Math.min(min, position);
max = Math.max(max, size + position);
}
// Get the spacing to use between each item.
double spacing = ((max - min) - sum) / (elements.size() - 1);
String undoRedoLabel = vertical ? SPACE_VERTICALLY : SPACE_HORIZONTALLY;
CogToolLID lid = vertical ? FrameEditorLID.SpaceVertically : FrameEditorLID.SpaceHorizontally;
CompoundUndoableEdit editSequence = new CompoundUndoableEdit(undoRedoLabel, lid);
// Avoid moving a group more than once
Set<SimpleWidgetGroup> movedGroups = new HashSet<SimpleWidgetGroup>();
Set<IWidget> movedWidgets = new HashSet<IWidget>();
// Adjust the spacings to the correct values and go through
// each element performing the appropriate move.
eltIter = elements.iterator();
while (eltIter.hasNext()) {
FrameElement elt = eltIter.next();
DoubleRectangle bounds = elt.getEltBounds();
// Determine the amount to move each element
double deltaX = vertical ? 0.0 : (min - bounds.x);
double deltaY = vertical ? (min - bounds.y) : 0.0;
// Set the new location, adding the undoable edit
moveElement(lid, undoRedoLabel, elt, deltaX, deltaY, true, movedGroups, movedWidgets, editSequence);
// Advance the pointer to the next location
min += spacing + (vertical ? bounds.height : bounds.width);
}
editSequence.end();
// Only add this edit if it is significant
if (editSequence.isSignificant()) {
undoMgr.addEdit(editSequence);
}
return true;
}
Aggregations