use of java.awt.dnd.DragSourceAdapter in project airavata by apache.
the class ComponentSelector method initGUI.
private void initGUI() {
this.treeModel = new ComponentTreeModel(new ComponentTreeNode("Components"));
this.tree = new JTree(this.treeModel);
// Add a tool tip.
ToolTipManager.sharedInstance().registerComponent(this.tree);
DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer() {
@Override
public java.awt.Component getTreeCellRendererComponent(JTree t, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean focus) {
super.getTreeCellRendererComponent(t, value, sel, expanded, leaf, row, focus);
ComponentTreeNode node = (ComponentTreeNode) value;
if (node.getComponentReference() == null) {
setToolTipText(null);
} else {
setToolTipText("Drag a component to the composer to add");
}
return this;
}
};
// Change icons
try {
renderer.setOpenIcon(SwingUtil.createImageIcon("opened.gif"));
renderer.setClosedIcon(SwingUtil.createImageIcon("closed.gif"));
renderer.setLeafIcon(SwingUtil.createImageIcon("leaf.gif"));
} catch (RuntimeException e) {
logger.warn("Failed to load image icons. " + "It will use the default icons instead.", e);
}
this.tree.setCellRenderer(renderer);
this.tree.setShowsRootHandles(true);
this.tree.setEditable(false);
this.tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
this.tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent event) {
// update.
if (event.isAddedPath()) {
TreePath path = event.getPath();
select(path);
}
}
});
// Drag and dtop
DragGestureListener dragGestureListener = new DragGestureListener() {
public void dragGestureRecognized(DragGestureEvent event) {
ComponentSelector.this.dragGestureRecognized(event);
}
};
DragSource dragSource = DragSource.getDefaultDragSource();
dragSource.createDefaultDragGestureRecognizer(this.tree, DnDConstants.ACTION_COPY_OR_MOVE, dragGestureListener);
this.dragSourceListener = new DragSourceAdapter() {
};
// Popup
this.tree.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent event) {
if (event.isPopupTrigger()) {
showPopupIfNecessary(event);
}
}
});
createNodePopupMenu();
}
Aggregations