📄 contextmenuandtooltipmanipulator.java
字号:
package net.sourceforge.jpowergraph.manipulator.contextandtooltip;
import net.sourceforge.jpowergraph.Edge;
import net.sourceforge.jpowergraph.Legend;
import net.sourceforge.jpowergraph.Node;
import net.sourceforge.jpowergraph.lens.TooltipLens;
import net.sourceforge.jpowergraph.manipulator.AbstractManipulator;
import net.sourceforge.jpowergraph.pane.JGraphPane;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.MenuListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
/**
* A manipulator providing the contextmenu & tooltip for nodes.
*/
public class ContextMenuAndToolTipManipulator extends AbstractManipulator {
protected static final String CLASS_NAME = ContextMenuAndToolTipManipulator.class.toString();
public static final String NAME = "ContextMenuAndToolTipManipulator";
private Menu rightClick;
private Shell tipShell;
private Node lastNode = null;
private Color backgroundColor;
private ContextMenuListener contextMenuListener;
private ToolTipListener toolTipListener;
private JGraphPane wsmlGraphPane;
private TooltipLens tooltipLens;
private boolean disposed = false;
public ContextMenuAndToolTipManipulator(JGraphPane theWSMLGraphPane, ContextMenuListener theContextMenuListener, ToolTipListener theToolTipListener, TooltipLens theTooltipLens) {
this.wsmlGraphPane = theWSMLGraphPane;
setGraphPane(wsmlGraphPane);
this.contextMenuListener = theContextMenuListener;
this.toolTipListener = theToolTipListener;
this.tooltipLens = theTooltipLens;
this.backgroundColor = wsmlGraphPane.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND);
}
public String getName() {
return NAME;
}
public void mouseDown(MouseEvent e) {
closeToolTipIfNeeded();
doRightClickPopup(e);
}
public void mouseUp(MouseEvent e) {
doRightClickPopup(e);
}
public void mouseExit(MouseEvent e) {
closeToolTipIfNeeded();
}
public void mouseHover(MouseEvent e) {
if (showToolTips() && !isRightClickShowing()){
doToolTipPopup(e);
}
}
private boolean showToolTips() {
return tooltipLens == null || tooltipLens.isShowToolTips();
}
public void mouseMove(MouseEvent e){
if (isToolTipShowing()){
doToolTipPopup(e);
}
}
protected void doRightClickPopup(MouseEvent e) {
if (m_graphPane.isEnabled() && e.button == 3) {
Point point = new Point(e.x, e.y);
Legend legend = m_graphPane.getLegendAtPoint(point);
Node node = m_graphPane.getNodeAtPoint(point);
Edge edge = m_graphPane.getNearestEdge(point);
closeRightClickIfNeeded();
rightClick = new Menu(wsmlGraphPane);
wsmlGraphPane.setMenu(rightClick);
rightClick.addMenuListener(new MenuListener() {
public void menuHidden(MenuEvent arg0) {
wsmlGraphPane.redraw();
}
public void menuShown(MenuEvent arg0) {}
});
if (legend != null){
if (contextMenuListener != null){
contextMenuListener.fillLegendContextMenu(legend, rightClick);
}
}
else if (node != null){
if (contextMenuListener != null){
contextMenuListener.fillNodeContextMenu(node, rightClick);
}
}
else if (edge != null){
if (contextMenuListener != null){
contextMenuListener.fillEdgeContextMenu(edge, rightClick);
}
}
else{
if (contextMenuListener != null){
contextMenuListener.fillBackgroundContextMenu(rightClick);
}
}
if (rightClick.getItemCount() > 0){
closeToolTipIfNeeded();
rightClick.setVisible(true);
}
}
else{
closeRightClickIfNeeded();
}
}
protected void doToolTipPopup(MouseEvent e) {
if (m_graphPane.isEnabled()) {
Point point = new Point(e.x, e.y);
Node node = m_graphPane.getNodeAtPoint(point);
if (node != null && node != lastNode) {
closeToolTipIfNeeded();
if (toolTipListener != null){
tipShell = new Shell(wsmlGraphPane.getShell(), SWT.ON_TOP | SWT.TOOL);
tipShell.setBackground(backgroundColor);
boolean okay = toolTipListener.addNodeToolTipItems(node, tipShell, backgroundColor);
if (okay){
tipShell.pack();
setTooltipLocation(tipShell, wsmlGraphPane.toDisplay(point));
tipShell.setVisible(true);
}
}
}
else if (node == null){
closeToolTipIfNeeded();
}
lastNode = node;
}
}
private void setTooltipLocation(Shell shell, Point position) {
Rectangle displayBounds = shell.getDisplay().getBounds();
Rectangle shellBounds = shell.getBounds();
shellBounds.x = Math.max(Math.min(position.x, displayBounds.width - shellBounds.width), 0);
shellBounds.y = Math.max(Math.min(position.y + 16, displayBounds.height - shellBounds.height), 0);
shell.setBounds(shellBounds);
}
private boolean isRightClickShowing(){
return rightClick != null && !rightClick.isDisposed() && rightClick.isVisible();
}
private boolean isToolTipShowing(){
return tipShell != null && !tipShell.isDisposed() && tipShell.isVisible();
}
private void closeRightClickIfNeeded(){
if (isRightClickShowing()) {
rightClick.setVisible(false);
rightClick.dispose();
rightClick = null;
wsmlGraphPane.redraw();
}
}
private void closeToolTipIfNeeded(){
if (isToolTipShowing()) {
tipShell.setVisible(false);
toolTipListener.removeNodeToolTipItems(lastNode, tipShell);
tipShell.dispose();
tipShell = null;
wsmlGraphPane.redraw();
lastNode = null;
}
}
public void dispose(){
if (rightClick != null && !rightClick.isDisposed()){
rightClick.dispose();
}
if (tipShell != null && !tipShell.isDisposed()){
tipShell.dispose();
}
if (backgroundColor != null && !backgroundColor.isDisposed()){
backgroundColor.dispose();
}
disposed = true;
}
public boolean isDisposed(){
return disposed;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -