home *** CD-ROM | disk | FTP | other *** search
- package sub_arctic.input;
-
- import sub_arctic.lib.interactor;
- import sub_arctic.lib.manager;
- import sub_arctic.lib.sub_arctic_error;
-
- import java.util.Vector;
-
- /**
- * Positional dispatch agent for maintaining a currently selected set. This
- * agent dispatches inputs under the selectable input protocol and maintains
- * a currently selected object set. Objects can be placed in the set by
- * clicking on them using a Macintosh style interaction where simple clicks
- * make objects the single selection, and clicks with the shift key held down
- * add to the set.
- *
- * @see sub_arctic.input.selectable
- * @author Scott Hudson
- */
- public class selection_agent_class extends dispatch_agent {
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Simple constructor */
- public selection_agent_class()
- {
- _selection_set = new Vector();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** The currently selected set. This vector contains selectable objects. */
- protected Vector _selection_set;
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /** Number of objects currently selected */
- public int num_selected() { return _selection_set.size(); }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Retrieve the selected object at the given index in the currently selected
- * set.
- * @param int index the index of the desired element.
- * @return selectable the desired element
- */
- public selectable selected_set_item(int index)
- {
- if (index < 0 || index >= num_selected())
- throw new sub_arctic_error("Index into selection set out of range (" +
- index + ")");
-
- return (selectable)_selection_set.elementAt(index);
- }
-
- //had:
- //* @exception index_bounds if the given index is out of range.
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Clear the selection set to empty.
- *
- * @param event evt the event which "caused" the clear.
- * @param Object user_info the user information that should be passed to
- * objects dropping out of the selection set.
- */
- public void clear_selection_set(event evt, Object user_info)
- {
- /* walk down the set and tell everyone */
- for (int i = 0; i < _selection_set.size(); i++)
- {
- inform_removal((selectable)_selection_set.elementAt(i),evt,user_info);
- }
-
- /* clear the list */
- _selection_set.removeAllElements();
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Set the selection set to the given single element. Objects have the
- * option of rejecting selection (by returning false from select(), in
- * which case this routine returns false and the selection set is left
- * as is.
- *
- * @param selectable obj the object being selected
- * @param event evt the event which "caused" the selection.
- * @param Object user_info the user information that should be passed to
- * objects added and dropped out of the selection
- * set.
- * @return boolean indicating if the object accepted the selection.
- */
- public boolean set_selection(selectable obj, event evt, Object user_info)
- {
- int indx;
-
- /* see if our addition is already in the set, if not tell it about add */
- indx = find_selection(obj);
- if (indx == -1)
- {
- /* if it rejects the add we leave things alone and bail out */
- if (!inform_addition(obj,evt,user_info))
- return false;
- }
-
- /* walk down the set and tell everyone accept the object */
- for (int i = 0; i < _selection_set.size(); i++)
- {
- /* tell it its gone unless we are going to put it right back in */
- if (i != indx)
- inform_removal((selectable)_selection_set.elementAt(i), evt,
- user_info);
- }
-
- /* clear the list and put in our one element */
- _selection_set.removeAllElements();
- _selection_set.addElement(obj);
-
- return true;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Return the index of the given object in the currently selected object
- * set or -1 if the object is not selected.
- *
- * @param selectable obj the object we are inquiring about.
- * @return int the index of the object in the currently selected object set
- * or -1 if it is not currently selected.
- */
- public int find_selection(selectable obj)
- {
- return _selection_set.indexOf(obj);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Add the given object to the selection set. Objects have the
- * option of rejecting selection (by returning false from select(), in
- * which case this routine returns false and the selection set is left
- * as is.
- *
- * @param selectable obj the object being selected
- * @param event evt the event which "caused" the selection.
- * @param Object user_info the user information that should be passed to
- * object added to the selection set.
- * @return boolean indicating if the object accepted the selection.
- */
- public boolean add_to_selection_set(
- selectable obj,
- event evt,
- Object user_info)
- {
- /* only do something if the object in question is not already there */
- if (find_selection(obj) == -1)
- {
- /* put it in and let the object know */
- if (inform_addition(obj, evt, user_info))
- {
- _selection_set.addElement(obj);
- return true;
- }
- else
- return false;
- }
-
- /* already there so we accept the dispatch */
- return true;;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Remove the given object to the selection set. False is returned if
- * the object is not in the selection set, or if the object's unselect()
- * method returns false (however, the object is always removed from the
- * selection set).
- *
- * @param selectable obj the object being deselected
- * @param event evt the event which "caused" the deselection.
- * @param Object user_info the user information that should be passed to
- * object dropped out of the selection set.
- * @return boolean indicating if the object accepted the selection.
- */
- public boolean remove_from_selection_set(
- selectable obj,
- event evt,
- Object user_info)
- {
- int indx;
-
- /* only do something if the object in question is there */
- indx = find_selection(obj);
- if (indx != -1)
- {
- /* take it out and let the object know */
- _selection_set.removeElementAt(indx);
- return inform_removal(obj, evt, user_info);
- }
-
- return false;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Indicate which events we are interested in seeing. We are only
- * interested in presses of the mouse button.
- *
- * @param event evt the event we are testing.
- * @return boolean indicating whether we want to see the event.
- */
- public boolean event_is_useful(event evt)
- {
- return evt.id() == event.MOUSE_DOWN;
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Do the work of informing an object that it has been added to the currently
- * selected object set. Returns the result of the select() call on the
- * object. Note: this routine does not put the object in the set.
- *
- * @param selectable obj the object we are informing.
- * @param event evt the event "causing" the selection.
- * @param Object user_info the user info that should be passed to the
- * object.
- * @return boolean which was the result of the object's select() method.
- */
- protected boolean inform_addition(
- selectable obj,
- event evt,
- Object user_info)
- {
- /* put the event into the proper coords */
- if (obj instanceof interactor)
- evt.global_to_local((interactor)obj);
- else
- evt.reset_to_global();
-
- /* tell the object */
- return obj.select(evt, user_info);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Do the work of informing an object that it has been removed from the
- * currently selected object set. Returns the result of the unselect() call
- * on the object. Note: this routine does not remove the object from the set.
- *
- * @param selectable obj the object we are informing.
- * @param event evt the event "causing" the deselection.
- * @param Object user_info the user info that should be passed to the
- * object.
- * @return boolean which was the result of the object's unselect() method.
- */
- protected boolean inform_removal(
- selectable obj,
- event evt,
- Object user_info)
- {
- /* put the event into the proper coords */
- if (obj instanceof interactor)
- evt.global_to_local((interactor)obj);
- else
- evt.reset_to_global();
-
- /* tell the object */
- return obj.unselect(evt, user_info);
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
-
- /**
- * Attempt to dispatch an event with this agent. Mouse presses with the
- * shift key held down add to the currently selected object set. Plain
- * presses replace the currently selected object set with a single object.
- *
- * @param event evt the event we are trying to dispatch
- * @param Object user_info the user info value to pass with the dispatch
- * @param interactor to_obj the object to dispatch to.
- * @param int seq_num the sequence number of this event.
- * @return boolean indicating whether the event was consumed.
- */
- public boolean dispatch_event(
- event evt,
- Object user_info,
- interactor to_obj,
- int seq_num)
- {
- selectable sel_obj;
-
- /* if the object doesn't want selection bail out now */
- if (!(to_obj instanceof selectable)) return false;
-
- sel_obj = (selectable)to_obj;
-
- /* is the shift key is down? */
- if ((evt.modifiers() & event.SHIFT_MASK) != 0)
- {
- /* if object is already in the selection set remove it */
- if (find_selection(sel_obj) != -1)
- return remove_from_selection_set(sel_obj, evt, user_info);
- else
- /* otherwise add object to selection */
- return add_to_selection_set(sel_obj, evt, user_info);
- }
- else
- {
- /* set object as sole selection */
- return set_selection(sel_obj, evt, user_info);
- }
- }
-
- /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
- }
- /*=========================== COPYRIGHT NOTICE ===========================
-
- This file is part of the subArctic user interface toolkit.
-
- Copyright (c) 1996 Scott Hudson and Ian Smith
- All rights reserved.
-
- The subArctic system is freely available for most uses under the terms
- and conditions described in
- http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html
- and appearing in full in the lib/interactor.java source file.
-
- The current release and additional information about this software can be
- found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
-
- ========================================================================*/
-