📄 display.java
字号:
} // end of inner class TransformActivity
// ------------------------------------------------------------------------
// Paint Listeners
/**
* Add a PaintListener to this Display to receive notifications
* about paint events.
* @param pl the {@link prefuse.util.display.PaintListener} to add
*/
public void addPaintListener(PaintListener pl) {
if ( m_painters == null )
m_painters = new CopyOnWriteArrayList();
m_painters.add(pl);
}
/**
* Remove a PaintListener from this Display.
* @param pl the {@link prefuse.util.display.PaintListener} to remove
*/
public void removePaintListener(PaintListener pl) {
m_painters.remove(pl);
}
/**
* Fires a pre-paint notification to PaintListeners.
* @param g the current graphics context
*/
protected void firePrePaint(Graphics2D g) {
if ( m_painters != null && m_painters.size() > 0 ) {
Object[] lstnrs = m_painters.getArray();
for ( int i=0; i<lstnrs.length; ++i ) {
try {
((PaintListener)lstnrs[i]).prePaint(this, g);
} catch ( Exception e ) {
s_logger.warning(
"Exception thrown by PaintListener: " + e + "\n" +
StringLib.getStackTrace(e));
}
}
}
}
/**
* Fires a post-paint notification to PaintListeners.
* @param g the current graphics context
*/
protected void firePostPaint(Graphics2D g) {
if ( m_painters != null && m_painters.size() > 0 ) {
Object[] lstnrs = m_painters.getArray();
for ( int i=0; i<lstnrs.length; ++i ) {
try {
((PaintListener)lstnrs[i]).postPaint(this, g);
} catch ( Exception e ) {
s_logger.warning(
"Exception thrown by PaintListener: " + e + "\n" +
StringLib.getStackTrace(e));
}
}
}
}
// ------------------------------------------------------------------------
// Item Bounds Listeners
/**
* Add an ItemBoundsListener to receive notifications when the bounds
* occupied by the VisualItems in this Display change.
* @param ibl the {@link prefuse.util.display.ItemBoundsListener} to add
*/
public void addItemBoundsListener(ItemBoundsListener ibl) {
if ( m_bounders == null )
m_bounders = new CopyOnWriteArrayList();
m_bounders.add(ibl);
}
/**
* Remove an ItemBoundsListener to receive notifications when the bounds
* occupied by the VisualItems in this Display change.
* @param ibl the {@link prefuse.util.display.ItemBoundsListener} to remove
*/
public void removeItemBoundsListener(ItemBoundsListener ibl) {
m_bounders.remove(ibl);
}
/**
* Check if the item bounds has changed, and if so, fire a notification.
* @param prev the previous item bounds of the Display
*/
protected void checkItemBoundsChanged(Rectangle2D prev) {
if ( m_bounds.equals(prev) )
return; // nothing to do
if ( m_bounders != null && m_bounders.size() > 0 ) {
Object[] lstnrs = m_bounders.getArray();
for ( int i=0; i<lstnrs.length; ++i ) {
try {
((ItemBoundsListener)lstnrs[i]).itemBoundsChanged(this);
} catch ( Exception e ) {
s_logger.warning(
"Exception thrown by ItemBoundsListener: " + e + "\n" +
StringLib.getStackTrace(e));
}
}
}
}
// ------------------------------------------------------------------------
// Control Listeners
/**
* Adds a ControlListener to receive all input events on VisualItems.
* @param cl the listener to add.
*/
public void addControlListener(Control cl) {
m_controls.add(cl);
}
/**
* Removes a registered ControlListener.
* @param cl the listener to remove.
*/
public void removeControlListener(Control cl) {
m_controls.remove(cl);
}
/**
* Returns the VisualItem located at the given point.
* @param p the Point at which to look
* @return the VisualItem located at the given point, if any
*/
public synchronized VisualItem findItem(Point p) {
// transform mouse point from screen space to item space
Point2D p2 = (m_itransform==null ? p :
m_itransform.transform(p, m_tmpPoint));
// ensure that the picking queue has been z-sorted
if ( !m_queue.psorted )
m_queue.sortPickingQueue();
// walk queue from front to back looking for hits
for ( int i = m_queue.psize; --i >= 0; ) {
VisualItem vi = m_queue.pitems[i];
if ( !vi.isValid() ) continue; // in case tuple went invalid
Renderer r = vi.getRenderer();
if (r!=null && vi.isInteractive() && r.locatePoint(p2, vi)) {
return vi;
}
}
return null;
}
/**
* Captures all mouse and key events on the display, detects relevant
* VisualItems, and informs ControlListeners.
*/
public class InputEventCapturer implements MouseMotionListener,
MouseWheelListener, MouseListener, KeyListener
{
private VisualItem activeItem = null;
private boolean mouseDown = false;
private boolean validityCheck() {
if ( activeItem.isValid() )
return true;
activeItem = null;
return false;
}
public void mouseDragged(MouseEvent e) {
synchronized ( m_vis ) {
if ( activeItem != null ) {
if ( validityCheck() )
fireItemDragged(activeItem, e);
} else {
fireMouseDragged(e);
}
}
}
public void mouseMoved(MouseEvent e) {
synchronized ( m_vis ) {
boolean earlyReturn = false;
//check if we've gone over any item
VisualItem vi = findItem(e.getPoint());
if ( activeItem != null && activeItem != vi ) {
if ( validityCheck() )
fireItemExited(activeItem, e);
earlyReturn = true;
}
if ( vi != null && vi != activeItem ) {
fireItemEntered(vi, e);
earlyReturn = true;
}
activeItem = vi;
if ( earlyReturn ) return;
if ( vi != null && vi == activeItem ) {
fireItemMoved(vi, e);
}
if ( vi == null ) {
fireMouseMoved(e);
}
}
}
public void mouseWheelMoved(MouseWheelEvent e) {
synchronized ( m_vis ) {
if ( activeItem != null ) {
if ( validityCheck() )
fireItemWheelMoved(activeItem, e);
} else {
fireMouseWheelMoved(e);
}
}
}
public void mouseClicked(MouseEvent e) {
synchronized ( m_vis ) {
if ( activeItem != null ) {
if ( validityCheck() )
fireItemClicked(activeItem, e);
} else {
fireMouseClicked(e);
}
}
}
public void mousePressed(MouseEvent e) {
synchronized ( m_vis ) {
mouseDown = true;
if ( activeItem != null ) {
if ( validityCheck() )
fireItemPressed(activeItem, e);
} else {
fireMousePressed(e);
}
}
}
public void mouseReleased(MouseEvent e) {
synchronized ( m_vis ) {
if ( activeItem != null ) {
if ( validityCheck() )
fireItemReleased(activeItem, e);
} else {
fireMouseReleased(e);
}
if ( activeItem != null && mouseDown && isOffComponent(e) ) {
// mouse was dragged off of the component,
// then released, so register an exit
fireItemExited(activeItem, e);
activeItem = null;
}
mouseDown = false;
}
}
public void mouseEntered(MouseEvent e) {
synchronized ( m_vis ) {
fireMouseEntered(e);
}
}
public void mouseExited(MouseEvent e) {
synchronized ( m_vis ) {
if ( !mouseDown && activeItem != null ) {
// we've left the component and an item
// is active but not being dragged, deactivate it
fireItemExited(activeItem, e);
activeItem = null;
}
fireMouseExited(e);
}
}
public void keyPressed(KeyEvent e) {
synchronized ( m_vis ) {
if ( activeItem != null ) {
if ( validityCheck() )
fireItemKeyPressed(activeItem, e);
} else {
fireKeyPressed(e);
}
}
}
public void keyReleased(KeyEvent e) {
synchronized ( m_vis ) {
if ( activeItem != null ) {
if ( validityCheck() )
fireItemKeyReleased(activeItem, e);
} else {
fireKeyReleased(e);
}
}
}
public void keyTyped(KeyEvent e) {
synchronized ( m_vis ) {
if ( activeItem != null ) {
if ( validityCheck() )
fireItemKeyTyped(activeItem, e);
} else {
fireKeyTyped(e);
}
}
}
private boolean isOffComponent(MouseEvent e) {
int x = e.getX(), y = e.getY();
return ( x<0 || x>getWidth() || y<0 || y>getHeight() );
}
// --------------------------------------------------------------------
// Fire Event Notifications
private void fireItemDragged(VisualItem item, MouseEvent e) {
Object[] lstnrs = m_controls.getArray();
for (int i = 0; i < lstnrs.length; ++i) {
Control ctrl = (Control) lstnrs[i];
if (ctrl.isEnabled())
try {
ctrl.itemDragged(item, e);
} catch ( Exception ex ) {
s_logger.warning(
"Exception thrown by Control: " + ex + "\n" +
StringLib.getStackTrace(ex));
}
}
}
private void fireItemMoved(VisualItem item, MouseEvent e) {
Object[] lstnrs = m_controls.getArray();
for (int i = 0; i < lstnrs.length; ++i) {
Control ctrl = (Control) lstnrs[i];
if (ctrl.isEnabled())
try {
ctrl.itemMoved(item, e);
} catch ( Exception ex ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -