Selection in a manager
Describes how to select objects through the manager and display them as selected.
Explains how selection objects are used for displaying selected objects in a manager.
Describes how to select and deselect objects in a manager and perform related operations.
Explains how a selection object is assigned and how to override the default behavior.
Describes how to be notified of changes to selections in a manager.
Selection objects
The manager allows you to select objects. To display selected objects within a manager, Rogue Wave JViews creates selection objects which are drawn on top of the selected objects. An example of a selection object is a set of handles drawn around the selected object.
Selection objects are stored in the manager. Unlike regular graphic objects, they are internally managed and cannot be manipulated.
When a graphic object is selected, a selection object is created and is drawn on top of the graphic object. Selection objects are subclass instances of the class
IlvSelection. As such, they are also graphic objects. The class
IlvSelection is an abstract class that has been subclassed to create several classes of selection objects specialized in the selection of specific graphic objects. For example, the class
IlvSplineSelection is a selection object for the selection of an
IlvSpline object.
The default selection object for graphic objects is an instance of the class
IlvDrawSelection. This class draws eight handles around the object, one on each of the four sides and one on each corner.
Managing selected objects
To select or deselect a graphic object in a manager, use the setSelected method:
void setSelected(IlvGraphic obj, boolean select, boolean redraw)
Once an object has been selected, you can retrieve its selection object using:
IlvSelection getSelection(IlvGraphic obj)
This method returns null if the object does not have an associated selection object; in other words, if the graphic object is not selected. You can also use the following method to determine whether the object is selected or not:
boolean isSelected(IlvGraphic obj)
To obtain the selected object from the selection object, use the
getObject method of
IlvSelection.
You can obtain an enumeration of all the selected objects in the manager with:
IlvGraphicEnumeration getSelectedObjects()
You can use this method as follows.
IlvGraphicEnumeration selectedobjs = manager.getSelectedObjects();
IlvGraphic obj;
while(selectedobjs.hasMoreElements()) {
obj = selectedobjs.nextElement();
//perform some action
}
NOTE To avoid unpredictable results, you must not select or deselect graphic objects when stepping through the enumeration as in the example above.
Other methods of IlvManager allow you to select and deselect all objects in the manager or in a particular layer:
void selectAll(IlvManagerView view, boolean redraw)
void selectAll(boolean redraw)
void deSelectAll(boolean redraw)
void deSelectAll(int layer, boolean redraw)
Selection interactor
The library provides the
IlvSelectInteractor class which allows you to select and deselect objects in an interactive way (using the mouse). It also allows you to edit graphic objects. For more information, see
The selection interactor.
Creating your own selection object
The selection object depends on the graphic object. In fact, the manager creates the selection object using the following method of the graphic object:
IlvSelection makeSelection()
You can override this method to return your own instance of the selection object. Another possibility is to set an
IlvSelectionFactory on the manager and let this factory decide which subclass of
IlvSelection should be instantiated depending on the graphic object. The following is an example which creates a new selection object (a white border) around the selected object.
class mySelection extends IlvSelection
{
static final int thickness = 3;
mySelection(IlvGraphic obj)
{
super(obj);
}
public void draw(Graphics g, IlvTransformer t)
{
g.setColor(Color.white);
IlvRect rect = boundingBox(t);
for (int i = 0; i < thickness; i++) {
if ((int)Math.floor(rect.width) >
2*i && (int)Math.floor(rect.height) > 2*i)
g.drawRect((int)Math.floor(rect.x)+i,
(int)Math.floor(rect.y)+i,
(int)Math.floor(rect.width)-2*i-1,
(int)Math.floor(rect.height)-2*i-1);
}
}
public IlvRect boundingBox(IlvTransformer t)
{
// get the bounding rectangle of the selected object
IlvRect bbox = getObject().boundingBox(t);
bbox.x-= thickness;
bbox.y-= thickness;
bbox.width+= 2*thickness;
bbox.height+= 2*thickness;
return bbox;
}
public boolean contains(IlvPoint p, IlvPoint tp, IlvTransformer t)
{
return false;
}
}
You can see that the selection object is defined in the same way as a graphic object. The constructor of a selection object always takes the selected object as a parameter. Note that the boundingBox method of the selection object uses the boundingBox method of the selected object so that the selection object (in this case, the white border) is always around the selected object, whatever the transformer is.
Listener for the selections in a manager
A class must implement the
ManagerSelectionListener interface to be notified that selections in a manager have been modified. This interface contains only the
selectionChanged method, which is called each time an object is selected or deselected.
void selectionChanged(ManagerSelectionChangedEvent event)
To be notified of selections and deselections, a class must register itself using the following method of the class IlvManager:
void addManagerSelectionListener(ManagerSelectionListener l)
Note that the selectionChanged method is called just after the object is selected or deselected, so you can easily determine whether it is a selection or a deselection. You do this in the following way:
class MyListener implements ManagerSelectionListener
{
public void selectionChanged(ManagerSelectionChangedEvent event)
{
// retrieve the graphic object
IlvGraphic obj = event.getGraphic();
IlvManager manager = event.getManager();
if (manager.isSelected(obj)) {
// object was selected
} else {
// object was deselected
}
}
}
When numerous objects are being selected, for example, as a result of a call to the
selectAll method of the manager, many selection events will be sent to the selection listeners. This can be inefficient for some listeners that need to perform an action when the selection is stable. For example, a property inspector showing the properties of selected objects does not need to be updated for each individual selection when a number of objects are selected at the same time. To solve this kind of problem, the
ManagerSelectionChangedEvent class has the following methods:
isAdjusting to tell you if the event is part of a series of selection events.
isAdjustmentEnd to indicate that the event is the last one of a series.
In the case of a “property inspector,” the listener would be as follows:
class MyListener implements ManagerSelectionListener
{
public void selectionChanged(ManagerSelectionChangedEvent event)
{
if (!event.isAdjusting() || event.isAdjustmentEnd())
{
// update the properties only if this is a single
// selection or the end of a series.
}
}
}
You may want to use the same “adjustment” notification when selecting numerous objects in a manager. The IlvManager class allows you to do this using the setSelectionAdjusting method:
boolean isAdjusting = manager.isSelectionAdjusting();
manager.setSelectionAdjusting(true);
try {
// select or deselect a lot of objects.
} finally {
manager.setSelectionAdjusting(isAdjusting);
}
Copyright © 2018, Rogue Wave Software, Inc. All Rights Reserved.