Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 34848 Details for
Bug 128228
make figures on FigureCanvas selectable
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
SelectableFigureCanvas.java
SelectableFigureCanvas.java (text/plain), 12.21 KB, created by
Jennifer Cormier
on 2006-02-16 11:06:48 EST
(
hide
)
Description:
SelectableFigureCanvas.java
Filename:
MIME Type:
Creator:
Jennifer Cormier
Created:
2006-02-16 11:06:48 EST
Size:
12.21 KB
patch
obsolete
>/******************************************************************************* > * Copyright (c) 2005 ATC-NY. > * All rights reserved. This program and the accompanying materials > * are made available under the terms of the Eclipse Public License v1.0 > * which accompanies this distribution, and is available at > * http://www.eclipse.org/legal/epl-v10.html > * > * Contributors: > * ATC-NY > *******************************************************************************/ > >package org.eclipse.draw2d; > >import java.util.EventObject; >import java.util.Iterator; >import java.util.LinkedList; >import java.util.List; > >import org.eclipse.core.runtime.Platform; >import org.eclipse.draw2d.FigureCanvas; >import org.eclipse.draw2d.IFigure; >import org.eclipse.draw2d.MouseEvent; >import org.eclipse.draw2d.MouseListener; >import org.eclipse.draw2d.geometry.Point; >import org.eclipse.jface.util.Assert; >import org.eclipse.jface.util.ListenerList; >import org.eclipse.jface.util.SafeRunnable; >import org.eclipse.jface.viewers.ISelection; >import org.eclipse.jface.viewers.ISelectionChangedListener; >import org.eclipse.jface.viewers.ISelectionProvider; >import org.eclipse.jface.viewers.SelectionChangedEvent; >import org.eclipse.jface.viewers.StructuredSelection; >import org.eclipse.swt.widgets.Composite; > >/** A FigureCanvas whose figures are selectable. > * Callers can listen for selection changes and for > * double-click events. <br> > * > * Subclasses may override method <code>isFigureSelectable<code> > * to specify which types of figures are selectable. <br> > * > * Implementation Note: There are two possible ways in which we > * could listen to mouse events. We could add a MouseListener to > * the canvas (this) or to the figure when the canvas' contents are set. <br> > * We are adding the listener to the figure because that makes it > * easier to find the figure corresponding to an event. > * > * @author Jennifer Cormier > */ >public class SelectableFigureCanvas extends FigureCanvas implements > ISelectionProvider { > > /**Figures on the canvas which are selected > * (element type: <code>IFigure</code>). > */ > protected List selectedFigures = new LinkedList(); > > /** Registered selection changed listeners > * (element type: <code>ISelectionChangedListener</code>). > */ > protected ListenerList selectionListenerList = new ListenerList(); > > /** List of double-click state listeners > * (element type: <code>IDoubleClickListener</code>). > * > * @see #fireDoubleClick > */ > private ListenerList doubleClickListeners = new ListenerList(); > > public SelectableFigureCanvas(Composite parent) { > super(parent); > } > > /* (non-Javadoc) > * @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) > */ > public void addSelectionChangedListener(ISelectionChangedListener listener) { > selectionListenerList.add(listener); > } > > /* (non-Javadoc) > * @see org.eclipse.jface.viewers.ISelectionProvider#getSelection() > */ > public ISelection getSelection() { > if ( isDisposed() || selectedFigures.isEmpty() ) { > return StructuredSelection.EMPTY; > } else { > return new StructuredSelection(selectedFigures); > } > } > > /* (non-Javadoc) > * @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) > */ > public void removeSelectionChangedListener(ISelectionChangedListener listener) { > selectionListenerList.remove(listener); > } > > /* (non-Javadoc) > * @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection) > */ > public void setSelection(ISelection selection) { > IFigure fig = null; > if ( selection == null || selection.isEmpty() || > ! ( selection instanceof StructuredSelection ) ) { > > selectedFigures.clear(); > notifySelectionListeners(); > return; > } > > // Only update the selection if all its elements are valid > > List figuresToSelect = new LinkedList(); > > // Loop through the items in the selection, > // add valid items to figuresToSelect, > // stop at first invalid item. > boolean invalidSelection = false; > StructuredSelection ss = (StructuredSelection) selection; > Iterator it = ss.iterator(); > while ( it.hasNext() ) { > fig = null; > Object obj = it.next(); > if ( ( ! ( obj instanceof IFigure ) ) || > ( ! isFigureSelectable((IFigure) obj) ) ) { > invalidSelection = true; > break; > } > fig = (IFigure) obj; > if ( fig != null ) { > figuresToSelect.add(fig); > } > } > > if ( !invalidSelection ) { > selectedFigures.addAll(figuresToSelect); > notifySelectionListeners(); > } > } > > /* (non-Javadoc) > * @see org.eclipse.draw2d.FigureCanvas#setContents(org.eclipse.draw2d.IFigure) > */ > public void setContents(IFigure figure) { > // Override in order to add a selection listener to the figure > // Note that the basic implementation of setContents does not > // remove all the previous contents, it just adds to them. > super.setContents(figure); > FigureMouseListener mouseListener = new FigureMouseListener(); > figure.addMouseListener(mouseListener); > } > > /** Adds a listener for double-clicks in this viewer. Has no effect if an > * identical listener is already registered. > * > * @param listener A double-click listener. > */ > public void addDoubleClickListener(IDoubleClickListener listener) { > doubleClickListeners.add(listener); > } > > /** Notifies any double-click listeners that a double-click has been > * received. Only listeners registered at the time this method is called are > * notified. > * > * @param event A double-click event. > * > * @see IDoubleClickListener#doubleClick > */ > protected void fireDoubleClick(final DoubleClickEvent event) { > Object[] listeners = doubleClickListeners.getListeners(); > for (int i = 0; i < listeners.length; ++i) { > final IDoubleClickListener listener = (IDoubleClickListener) listeners[i]; > Platform.run(new SafeRunnable() { > public void run() { > listener.doubleClick(event); > } > }); > } > } > > /** Removes the given double-click listener from this viewer. Has no affect > * if an identical listener is not registered. > * > * @param listener A double-click listener. > */ > public void removeDoubleClickListener(IDoubleClickListener listener) { > doubleClickListeners.remove(listener); > } > > /** Notify listeners that a selection change has occurred. */ > protected void notifySelectionListeners() { > SelectionChangedEvent event = new SelectionChangedEvent(this, getSelection()); > Object listeners [] = selectionListenerList.getListeners(); > for ( int i = 0; i < listeners.length; i++ ) { > ISelectionChangedListener listener = (ISelectionChangedListener) listeners[i]; > listener.selectionChanged(event); > } > } > > /** Find a figure which is a parent (grandparent, etc.) of > * the given figure and which is selectable. <br> > * > * @param figure The figure to examine. > * @return The closest ancestor which is selectable. > */ > protected IFigure findSelectableParentFigure(IFigure figure) { > // findFigureAt() returns the lowest level figure, > // which may be too low (e.g. a label). > // Need to loop considering parents until we find a figure we're > // interested in, or until we can be certain that there is none. > IFigure parentFigure = figure; > while ( parentFigure != null ) { > if ( isFigureSelectable(parentFigure) ) { > figure = parentFigure; > break; > } else { > parentFigure = parentFigure.getParent(); > figure = null; > } > } > return figure; > } > > /** Return whether the given figure can be selected. <br> > * > * Default implementation allows all figures to be selected. <br> > * > * Subclasses may override. > * > * @param figure The IFigure of interest. > * @return <code>true</code> if the figure can be selected. > */ > protected boolean isFigureSelectable(IFigure figure) { > return true; > } > > /** Listener for mouse events involving Draw2D Figures > * > * @author Jennifer Cormier > */ > public class FigureMouseListener implements MouseListener { > > /* (non-Javadoc) > * @see org.eclipse.draw2d.MouseListener#mousePressed(org.eclipse.draw2d.MouseEvent) > */ > public void mousePressed(MouseEvent me) { > handleMousePressed(me); > } > > /* (non-Javadoc) > * @see org.eclipse.draw2d.MouseListener#mouseReleased(org.eclipse.draw2d.MouseEvent) > */ > public void mouseReleased(MouseEvent me) { > // No need to listen for button release > } > > /* (non-Javadoc) > * @see org.eclipse.draw2d.MouseListener#mouseDoubleClicked(org.eclipse.draw2d.MouseEvent) > */ > public void mouseDoubleClicked(MouseEvent me) { > IFigure figure = handleMousePressed(me); > if ( figure != null ) { > StructuredSelection sel = new StructuredSelection(figure); > fireDoubleClick(new DoubleClickEvent(this, sel)); > } > } > > /** Handles the event resulting from a mouse button being pressed. > * > * @param me The mouse event which needs to be handled. > * @return the figure (if any) under the mouse when it was pressed. > */ > public IFigure handleMousePressed(MouseEvent me) { > Point point = me.getLocation(); > IFigure figure = getContents().findFigureAt(point); > figure = findSelectableParentFigure(figure); > > if ( me.button == 3 ) { // for some reason comparing to MouseEvent.BUTTON3 does not work as expected > // Right mouse button was pressed > if ( ! selectedFigures.isEmpty() ) { > // there is already a selection > // Don't change the selection unless there is a figure and it is not currently in the selection > // and the Control key is not pressed > if ( ( figure != null ) && ! selectedFigures.contains(figure) && > ( me.getState() & MouseEvent.CONTROL ) == 0 ) { > > selectedFigures.clear(); > selectedFigures.add(figure); > notifySelectionListeners(); > } > } else if ( figure != null ) { > // there is a figure to select > // Clear the list of selected figures before adding a figure > selectedFigures.clear(); > selectedFigures.add(figure); > notifySelectionListeners(); > } > } else { > // Left mouse button was pressed > if ( ( me.getState() & MouseEvent.CONTROL ) == 0 ) { > // Control key was NOT pressed at the time > // Clear the list of selected figures before adding a figure > selectedFigures.clear(); > if ( figure != null ) { > selectedFigures.add(figure); > } > } else { > // Control key WAS pressed at the time. > // If the figure was previously selected then > // it should be removed from the selection - special case. > if ( selectedFigures.contains(figure) ) { > selectedFigures.remove(figure); > } else if ( figure != null ) { > selectedFigures.add(figure); > } > } > notifySelectionListeners(); > } > > // Consume the event so other listeners are not called. > // Otherwise this is called multiple times on one mouse press. > me.consume(); > return figure; > } > } > > /** Event object describing a double-click. This is needed for > * our selectable Draw2D graph, which isn't a Viewer subclass. > * > * @author Jennifer Cormier > */ > public class DoubleClickEvent extends EventObject { > > private static final long serialVersionUID = 3396065792670912232L; > > /** The selection. */ > protected ISelection selection; > > /** Creates a new event for the given source and selection. > * > * @param source The source of the event (e.g. viewer, canvas) > * @param selection The selection; should not be <code>null</code>. > */ > public DoubleClickEvent(Object source, ISelection selection) { > super(source); > Assert.isNotNull(selection); > this.selection = selection; > } > > /** Returns the selection. > * > * @return the selection. > */ > public ISelection getSelection() { > return selection; > } > > } > > /** A listener which is notified of double-click events. > * This is needed for our selectable Draw2D graph, which > * isn't a Viewer subclass. > * > * @author Jennifer Cormier > */ > public interface IDoubleClickListener { > > /** Notifies of a double click. > * > * @param event An event object describing the double-click. > */ > public void doubleClick(DoubleClickEvent event); > > } >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 128228
: 34848