// File: SelectionMultiple.java // Classes: SelectionMultiple // Original Author: Jason Robbins // $Id: SelectionMultiple.java,v 1.1.1.1 1997/02/27 20:52:48 chandra Exp $ package uci.graphedit; import java.util.Vector; import java.util.Enumeration; import java.awt.Graphics; import java.awt.Rectangle; import java.awt.Event; /** This class handles multiple selections. It is basically a * collection of SelectionSingle instances. Most of its operations * just dispatch the same operation to each of the SelectionSingle * instances in turn. * @see SelectionSingle */ public class SelectionMultiple extends Selection { /** The collection of SelectionSingle instances */ protected Vector _contents; public SelectionMultiple() { _contents = new Vector(); } /** Add a new selection to the collection of selections */ public void addElement(Selection s) { _contents.addElement(s); } public void addElement(DiagramElement de) { _contents.addElement(de.selectionObject()); } public void addAllElements(Vector v) { Enumeration eles = v.elements(); while(eles.hasMoreElements()) { DiagramElement de = (DiagramElement) eles.nextElement(); addElement(de); } } /** Select some objects if they were not selected already, and * deslect others that were already selected. This is normally * called from ModeSelect when the shift key is held down. * @see ModeSelect * @see Editor */ public void toggleAllElements(Vector v) { Enumeration eles = ((Vector)v.clone()).elements(); while(eles.hasMoreElements()) { DiagramElement de = (DiagramElement) eles.nextElement(); if (contains(de)) removeElement(de); else addElement(de); } } /** Deselect the given DiagramElement */ public void removeElement(Selection s) { _contents.removeElement(s); } public void removeElement(DiagramElement de) { Enumeration eles = ((Vector)_contents.clone()).elements(); while(eles.hasMoreElements()) { Selection sel = (Selection) eles.nextElement(); if (sel.contains(de)) removeElement(sel); } } /** Deselect everything */ public void removeAllElements() { _contents.removeAllElements(); } /** Reply true if the given selection instance is part of my * collection */ public boolean contains(Selection s) { return _contents.contains(s); } /** Reply true if the given DiagramElement is selected by any of my * selection objects */ public boolean contains(DiagramElement de) { Enumeration eles = _contents.elements(); while(eles.hasMoreElements()) { Selection sel = (Selection) eles.nextElement(); if (sel.contains(de)) return true; } return false; } /** Reply the number of selected DiagramElement's. This assumes that * this collection holds only SelectionSingle instances and each of * those holds one DiagramElement */ public int size() { return _contents.size(); } /** Reply the collection of all selected DiagramElement's */ public DiagramElement firstelement(){ return ((SelectionSingle)_contents.firstElement()).content(); } // public DiagramElement content() { // if (size() == 1) { // Selection sel = (Selection) _contents.firstElement(); // return (DiagramElement) sel.content(); // } // else throw new java.util.NoSuchElementException(); // } /** Start a transaction that damages all selected DiagramElement's */ public void startTrans() { Enumeration eles = _contents.elements(); while(eles.hasMoreElements()) { Selection sel = (Selection) eles.nextElement(); sel.startTrans(); } } /** End a transaction that damages all selected DiagramElement's */ public void endTrans() { Enumeration eles = _contents.elements(); while(eles.hasMoreElements()) { Selection sel = (Selection) eles.nextElement(); sel.endTrans(); } } /** Draw all selection objects */ public void draw(Graphics g) { Enumeration eles = _contents.elements(); while(eles.hasMoreElements()) { Selection sel = (Selection) eles.nextElement(); sel.draw(g); } } /** Reply the first non-null value of the given graphical attribute */ public Object getGraphicAttribute(String k) { Object obj; Enumeration eles = _contents.elements(); while(eles.hasMoreElements()) { Selection sel = (Selection) eles.nextElement(); if (null != (obj = sel.getGraphicAttribute(k))) return obj; } return null; } /** Set the given graphical attribute for all selected objects */ public void setGraphicAttribute(String k, Object v) { Enumeration eles = _contents.elements(); while(eles.hasMoreElements()) { Selection sel = (Selection) eles.nextElement(); sel.setGraphicAttribute(k, v); } } /** When the SelectionMultiple is damaged, that implies that each * SelectionSingle should be damaged. */ public void damagedIn(Document ed) { Enumeration eles = _contents.elements(); while(eles.hasMoreElements()) { Selection sel = (Selection) eles.nextElement(); sel.damagedIn(ed); } } /** Reply true iff the given point is inside one of the selected * DiagramElement's */ boolean inside(int x, int y) { Enumeration eles = _contents.elements(); while(eles.hasMoreElements()) { Selection sel = (Selection) eles.nextElement(); if (sel.inside(x, y)) return true; } return false; } public Rectangle getBoundingBox() { Rectangle r = null; Enumeration eles = _contents.elements(); while(eles.hasMoreElements()) { Selection sel = (Selection) eles.nextElement(); if (null == r) r = sel.getBoundingBox(); else r.add(sel.getBoundingBox()); } return r; } /** Align the selected DiagramElement's relative to each other */ /* needs-more-work: more of this logic should be in ActionAlign */ void align(int direction) { Rectangle cur_rect; Selection sel; int dx, dy; int size = _contents.size(); Rectangle bbox = getBoundingBox(); for (int i=0; i