use of org.gephi.tools.spi.UnselectToolException in project gephi by gephi.
the class DesktopToolController method getToolbar.
@Override
public JComponent getToolbar() {
// Get tools ui
HashMap<ToolUI, Tool> toolMap = new HashMap<>();
List<ToolUI> toolsUI = new ArrayList<>();
for (Tool tool : tools) {
ToolUI ui = tool.getUI();
if (ui != null) {
toolsUI.add(ui);
toolMap.put(ui, tool);
}
}
// Sort by priority
Collections.sort(toolsUI, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
Integer p1 = ((ToolUI) o1).getPosition();
Integer p2 = ((ToolUI) o2).getPosition();
return p1.compareTo(p2);
}
});
// Create toolbar
final Toolbar toolbar = new Toolbar();
for (final ToolUI toolUI : toolsUI) {
final Tool tool = toolMap.get(toolUI);
JToggleButton btn;
if (toolUI.getIcon() != null) {
btn = new JToggleButton(toolUI.getIcon());
} else {
btn = new JToggleButton(new ImageIcon(getClass().getResource("/org/gephi/desktop/tools/tool.png")));
}
btn.setToolTipText(toolUI.getName() + " - " + toolUI.getDescription());
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Let the user unselect a tool (by clicking on it again) without having to select other tool:
if (tool == currentTool) {
toolbar.clearSelection();
unselect();
} else {
try {
select(tool);
propertiesBar.select(toolUI.getPropertiesBar(tool));
} catch (UnselectToolException unselectToolException) {
toolbar.clearSelection();
unselect();
}
}
}
});
toolbar.add(btn);
}
// SelectionManager events
VizController.getInstance().getSelectionManager().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
SelectionManager selectionManager = VizController.getInstance().getSelectionManager();
if (selectionManager.isRectangleSelection() && currentTool != null) {
toolbar.clearSelection();
unselect();
} else if (selectionManager.isSelectionEnabled() && currentTool != null && currentTool.getSelectionType() == ToolSelectionType.NONE) {
toolbar.clearSelection();
unselect();
} else if (selectionManager.isDraggingEnabled() && currentTool != null) {
toolbar.clearSelection();
unselect();
}
}
});
return toolbar;
}
Aggregations