use of org.eclipse.swt.widgets.TreeItem in project cogtool by cogtool.
the class ProjectUI method setUpDragAndDrop.
// See http://www.eclipse.org/articles/Article-SWT-DND/DND-in-SWT.html
// for more documentation of SWT drag-and-drop support.
protected void setUpDragAndDrop() {
DragSource treeAsSource = new DragSource(tree, DND.DROP_MOVE | DND.DROP_COPY);
TaskDnDTransfer taskTransfer = TaskDnDTransfer.getInstance();
TaskAppDnDTransfer taskAppTransfer = TaskAppDnDTransfer.getInstance();
Transfer[] types = new Transfer[] { taskTransfer, taskAppTransfer };
treeAsSource.setTransfer(types);
// DropSourceEvent fields:
// dataType:
// the Transfer type of the data the target prefers to receive;
// useful in dragSetData
// detail:
// the operation the target performed; one of:
// DROP_MOVE - move from source to target; remove from source
// DROP_COPY - copy the source to target; leave the source
// DROP_LINK - create a link of the source at the target
// useful in dragFinished in case the source needs to be removed
// doit:
// in dragStart, determines if the operation should proceed
// in dragFinished, may be set to indicate if the operation succeeded
// image:
// may be set to the Image displayed during drag
// x, y: position within the Tree
DragSourceListener srcListener = new TreeDragSourceEffect(tree) {
@Override
public void dragStart(DragSourceEvent evt) {
// If the Transfer type cannot be determined until the drag
// starts, the setTransfer() call can be invoked here.
// Set evt.doit to false here if action is inappropriate.
// Reset, just in case no drag-and-drop should happen
currentDnDSource = null;
// Must be in first column!
TreeColumn column = findColumn(evt.x);
TreeItem row = tree.getItem(new Point(evt.x, evt.y));
if ((column != null) && (column.getData() == null)) {
if ((row != null) && (row.getData() != null)) {
if (((AUndertaking) row.getData()).isSpawned()) {
evt.doit = false;
return;
}
}
if (selection.getSelectedTaskCount() == 0) {
if (row != null) {
selection.setSelectedItem(row);
currentDnDSource = tree;
currentDnDColumn = 0;
}
} else {
currentDnDSource = tree;
currentDnDColumn = 0;
}
} else {
// Must be in cell with a valid TaskApplication!
if ((column != null) && (column.getData() != null)) {
if ((row != null) && (row.getData() != null)) {
Design design = (Design) column.getData();
AUndertaking task = (AUndertaking) row.getData();
TaskApplication taskApp = project.getTaskApplication(task, design);
if (taskApp != null) {
if (!taskApp.getDemonstration().isEditable()) {
evt.doit = false;
return;
}
// set some highlighting of the source cell
selection.setSelectedCell(row, column);
contextSelection.setSelectedDesign(design);
contextSelection.addSelectedTask(task);
currentDnDRow = row;
currentDnDSource = tree;
currentDnDColumn = tree.indexOf(column);
// do not do superclass work!
return;
}
}
}
evt.doit = false;
}
super.dragStart(evt);
}
@Override
public void dragSetData(DragSourceEvent evt) {
// Based on the requested Transfer data type, set evt.data
// if (taskTransfer.isSupportedType(evt.dataType)) {
// evt.data = "This is the requested data";
// }
super.dragSetData(evt);
}
@Override
public void dragFinished(DragSourceEvent evt) {
// Operation was performed by the drop target; clean up
// If needed, evt.detail should be the operation performed.
super.dragFinished(evt);
currentDnDSource = null;
currentDnDColumn = -1;
currentDnDRow = null;
currentDndTaskAppDropRow = null;
}
};
treeAsSource.addDragListener(srcListener);
DropTarget treeAsTarget = new DropTarget(tree, DND.DROP_MOVE | DND.DROP_COPY);
treeAsTarget.setTransfer(types);
// DropTargetEvent fields:
// currentDataType:
// the Transfer type of the data the target prefers to receive;
// can be set -- see the method comments below
// dataTypes:
// the array of Transfer types the source can "send"
// detail:
// the operation the user is trying to perform; one of:
// DROP_MOVE - move from source to target; remove from source
// DROP_COPY - copy the source to target; leave the source
// DROP_LINK - create a link of the source at the target
// DROP_DEFAULT - indicator that target must choose operation
// DROP_NONE - indicator that user is trying an unsupported op
// may be set to the operation the target feels is correct
// (thus, if initially DEFAULT, then the operation that would be
// performed; if initially DEFAULT and not changed, it will appear
// to the user as a MOVE -- also, set to NONE if target determines
// operation is not permitted)
// feedback:
// bitwise OR'ing of feedback effects displayed to the user;
// can be set using the following constants:
// FEEDBACK_SELECT - item under cursor is selected
// FEEDBACK_SCROLL - allows scrolling to make items visible
// FEEDBACK_EXPAND - allows tree items to be expanded
// FEEDBACK_INSERT_BEFORE - insertion mark before item under cursor
// FEEDBACK_INSERT_AFTER - insertion mark after item under cursor
// FEEDBACK_NONE - no feedback
// item:
// TreeItem or TableItem under the cursor, if applicable
// operations:
// bitwise OR'ing of the operations that the DragSource can support
treeAsTarget.addDropListener(new TreeDropTargetEffect(tree) {
protected static final int DRAG_FEEDBACK = DND.FEEDBACK_EXPAND | DND.FEEDBACK_INSERT_BEFORE | DND.FEEDBACK_SCROLL;
protected static final int DRAG_APP_FEEDBACK = DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL;
protected int requestedOp = DND.DROP_MOVE;
@Override
public void dragEnter(DropTargetEvent evt) {
// Set evt.detail to DND.DROP_NONE when the operation is a no-op
// or if the presented type is unacceptable. Other choices
// that make sense: DND.DROP_MOVE, DND.DROP_COPY
// evt.currentDataType is the type preferred by the target.
// evt.dataTypes contains types provided by the source.
super.dragEnter(evt);
if (currentDnDSource != getControl()) {
evt.detail = DND.DROP_NONE;
} else {
requestedOp = evt.detail;
}
}
@Override
public void dragLeave(DropTargetEvent evt) {
if (currentDndTaskAppDropRow != null) {
currentDndTaskAppDropRow.setBackground(currentDnDColumn, ProjectUIModel.unselectedTaskBackgroundColor);
}
super.dragLeave(evt);
}
@Override
public void dragOperationChanged(DropTargetEvent evt) {
// change evt.currentDataType if desired here.
if ((evt.detail != DND.DROP_MOVE) && (evt.detail != DND.DROP_COPY)) {
evt.detail = DND.DROP_NONE;
}
requestedOp = evt.detail;
super.dragOperationChanged(evt);
}
@Override
public void dragOver(DropTargetEvent evt) {
if (currentDndTaskAppDropRow != null) {
currentDndTaskAppDropRow.setBackground(currentDnDColumn, ProjectUIModel.unselectedTaskBackgroundColor);
}
Point toTreeEvtLoc = tree.toControl(evt.x, evt.y);
//System.out.println("dragOver; set feedback here?");
if (currentDnDSource != getControl()) {
evt.detail = DND.DROP_NONE;
evt.feedback = DND.FEEDBACK_NONE;
} else if (currentDnDColumn == 0) {
// Moving tasks
evt.feedback = DRAG_FEEDBACK;
evt.detail = requestedOp;
TreeItem row = tree.getItem(toTreeEvtLoc);
if ((row != null) && (row.getData() != null)) {
if (((AUndertaking) row.getData()).isSpawned()) {
evt.detail = DND.DROP_NONE;
evt.feedback = DND.FEEDBACK_NONE;
}
}
} else {
// Moving task applications
evt.feedback = DRAG_APP_FEEDBACK;
TreeColumn column = findColumn(toTreeEvtLoc.x);
if (column == null) {
evt.detail = DND.DROP_NONE;
} else {
Design design = (Design) column.getData();
if (design != contextSelection.getSelectedDesign()) {
evt.detail = DND.DROP_NONE;
} else {
TreeItem row = tree.getItem(toTreeEvtLoc);
if ((row == null) || (row.getData() == null)) {
evt.detail = DND.DROP_NONE;
} else {
AUndertaking task = (AUndertaking) row.getData();
if (task.isTaskGroup() || task.isSpawned() || contextSelection.isTaskSelected(task)) {
evt.detail = DND.DROP_NONE;
} else {
evt.detail = requestedOp;
currentDndTaskAppDropRow = row;
currentDndTaskAppDropRow.setBackground(currentDnDColumn, CONTEXT_COLOR);
}
}
}
}
}
super.dragOver(evt);
}
@Override
public void dropAccept(DropTargetEvent evt) {
// Can change evt.detail if desired here.
// Provide one last chance to define the type of data that
// will be returned in the drop event; thus, change
// evt.currentDataType if desired here
super.dropAccept(evt);
}
@Override
public void drop(DropTargetEvent evt) {
// When the drop operation is completed, update the
// evt.detail field with the operation performed.
// Do the operation!
AUndertaking beforeTask = null;
if (evt.item != null) {
beforeTask = (AUndertaking) evt.item.getData();
}
if (requestedOp == DND.DROP_COPY) {
if (currentDnDColumn == 0) {
ProjectUI.ChangeTaskPositionParms parms = new ProjectUI.ChangeTaskPositionParms(selection, beforeTask, true);
if (performAction(ProjectLID.DuplicateTaskFull, parms, true)) {
evt.detail = DND.DROP_COPY;
}
} else {
AUndertaking fromTask = (AUndertaking) currentDnDRow.getData();
AUndertaking toTask = (AUndertaking) currentDndTaskAppDropRow.getData();
TreeColumn column = tree.getColumn(currentDnDColumn);
Design design = (Design) column.getData();
ProjectUI.MoveCopyTaskApplicationParms parms = new ProjectUI.MoveCopyTaskApplicationParms(fromTask, toTask, design);
selection.setSelectedCell(currentDndTaskAppDropRow, column);
if (performAction(ProjectLID.DuplicateTaskApplication, parms, true)) {
uiModel.redisplayAllResults();
evt.detail = DND.DROP_COPY;
}
}
} else if (requestedOp == DND.DROP_MOVE) {
if (currentDnDColumn == 0) {
ProjectUI.ChangeTaskPositionParms parms = new ProjectUI.ChangeTaskPositionParms(selection, beforeTask, false);
if (performAction(ProjectLID.ChangeTaskPosition, parms, true)) {
evt.detail = DND.DROP_MOVE;
}
} else {
AUndertaking fromTask = (AUndertaking) currentDnDRow.getData();
AUndertaking toTask = (AUndertaking) currentDndTaskAppDropRow.getData();
TreeColumn column = tree.getColumn(currentDnDColumn);
Design design = (Design) column.getData();
ProjectUI.MoveCopyTaskApplicationParms parms = new ProjectUI.MoveCopyTaskApplicationParms(fromTask, toTask, design);
selection.setSelectedCell(currentDndTaskAppDropRow, column);
if (performAction(ProjectLID.MoveTaskApplication, parms, true)) {
uiModel.redisplayAllResults();
evt.detail = DND.DROP_MOVE;
}
}
}
super.drop(evt);
}
});
}
use of org.eclipse.swt.widgets.TreeItem in project cogtool by cogtool.
the class ProjectUI method findAncestorSibling.
protected TreeItem findAncestorSibling(TreeItem item) {
TreeItem itemParent = item.getParentItem();
if (itemParent == null) {
int index = tree.indexOf(item);
if (index < tree.getItemCount() - 1) {
return tree.getItem(index + 1);
}
return null;
}
int index = itemParent.indexOf(item);
if (index < itemParent.getItemCount() - 1) {
return itemParent.getItem(index + 1);
}
return findAncestorSibling(itemParent);
}
use of org.eclipse.swt.widgets.TreeItem in project cogtool by cogtool.
the class ProjectUI method findPrevItem.
protected TreeItem findPrevItem(TreeItem item) {
TreeItem prevItem = null;
TreeItem itemParent = item.getParentItem();
// If a root item, find previous root sibling, if one
if (itemParent == null) {
int index = tree.indexOf(item);
// There must be a previous sibling; if so, find last descendant
if (index > 0) {
prevItem = findLastDescendant(tree.getItem(index - 1));
}
// else nowhere to go!
} else {
// Find location within TaskGroup
int index = itemParent.indexOf(item);
// Check for previous sibling; if so, find last descendant
if (index > 0) {
prevItem = findLastDescendant(itemParent.getItem(index - 1));
} else {
// No previous sibling, so up to the TaskGroup itself
prevItem = itemParent;
}
}
return prevItem;
}
use of org.eclipse.swt.widgets.TreeItem in project cogtool by cogtool.
the class ProjectUI method createSetToolTipListener.
protected Listener createSetToolTipListener() {
return new Listener() {
public void handleEvent(Event evt) {
String toolTipText = null;
TreeColumn col = findColumn(evt.x);
if (col != null) {
Design colData = (Design) col.getData();
if (colData != null) {
toolTipText = colData.getName();
}
}
TreeItem row = tree.getItem(new Point(evt.x, evt.y));
if (row != null) {
AUndertaking rowData = (AUndertaking) row.getData();
if (rowData != null) {
if (toolTipText != null) {
toolTipText += '@' + rowData.getName();
} else {
toolTipText = rowData.getName();
}
}
}
tree.setToolTipText(toolTipText);
}
};
}
use of org.eclipse.swt.widgets.TreeItem in project cogtool by cogtool.
the class ProjectMouseState method dealWithMouseUp.
@Override
protected void dealWithMouseUp(MouseEvent me) {
super.dealWithMouseUp(me);
if (me.button == 1) {
TreeItem item = ui.tree.getItem(new Point(me.x, me.y));
TreeColumn column = ui.findColumn(me.x);
if ((item == null) && (column == null)) {
ui.selection.deselectAll();
} else {
ui.selection.setSelectedCell(item, column);
}
}
}
Aggregations