private void setupAttributes() { Color frameColor = (Color) AttributeFigure.getDefaultAttribute("FrameColor"); Color fillColor = (Color) AttributeFigure.getDefaultAttribute("FillColor"); Color textColor = (Color) AttributeFigure.getDefaultAttribute("TextColor"); Integer arrowMode = (Integer) AttributeFigure.getDefaultAttribute("ArrowMode"); String fontName = (String) AttributeFigure.getDefaultAttribute("FontName"); FigureEnumeration k = view().selectionElements(); while (k.hasMoreElements()) { Figure f = k.nextFigure(); frameColor = (Color) f.getAttribute("FrameColor"); fillColor = (Color) f.getAttribute("FillColor"); textColor = (Color) f.getAttribute("TextColor"); arrowMode = (Integer) f.getAttribute("ArrowMode"); fontName = (String) f.getAttribute("FontName"); } fFrameColor.setSelectedIndex(ColorMap.colorIndex(frameColor)); fFillColor.setSelectedIndex(ColorMap.colorIndex(fillColor)); // fTextColor.select(ColorMap.colorIndex(textColor)); if (arrowMode != null) { fArrowChoice.setSelectedIndex(arrowMode.intValue()); } if (fontName != null) { fFontChoice.setSelectedItem(fontName); } }
/** Replaces a figure in the drawing without removing it from the drawing. */ public synchronized void replace(Figure figure, Figure replacement) { int index = fFigures.indexOf(figure); if (index != -1) { replacement.addToContainer(this); // will invalidate figure figure.changed(); fFigures.setElementAt(replacement, index); } }
/** * Finds a top level Figure. Use this call for hit detection that should not descend into the * figure's children. */ public Figure findFigure(int x, int y) { FigureEnumeration k = figuresReverse(); while (k.hasMoreElements()) { Figure figure = k.nextFigure(); if (figure.containsPoint(x, y)) return figure; } return null; }
/** * Removes all children. * * @see #remove */ public void removeAll() { FigureEnumeration k = figures(); while (k.hasMoreElements()) { Figure figure = k.nextFigure(); figure.removeFromContainer(this); } fFigures.removeAllElements(); }
/** Releases the figure and all its children. */ public void release() { super.release(); FigureEnumeration k = figures(); while (k.hasMoreElements()) { Figure figure = k.nextFigure(); figure.release(); } }
/** * Finds a top level Figure, but supresses the passed in figure. Use this method to ignore a * figure that is temporarily inserted into the drawing. * * @param x the x coordinate * @param y the y coordinate * @param without the figure to be ignored during the find. */ public Figure findFigureWithout(int x, int y, Figure without) { if (without == null) return findFigure(x, y); FigureEnumeration k = figuresReverse(); while (k.hasMoreElements()) { Figure figure = k.nextFigure(); if (figure.containsPoint(x, y) && !figure.includes(without)) return figure; } return null; }
/** Finds a top level Figure that intersects the given rectangle. */ public Figure findFigure(Rectangle r) { FigureEnumeration k = figuresReverse(); while (k.hasMoreElements()) { Figure figure = k.nextFigure(); Rectangle fr = figure.displayBox(); if (r.intersects(fr)) return figure; } return null; }
/** Inserts a vector of figures and translates them by the given offset. */ protected void insertFigures(Vector figures, int dx, int dy) { FigureEnumeration e = new FigureEnumerator(figures); while (e.hasMoreElements()) { Figure figure = e.nextFigure(); figure.moveBy(dx, dy); figure = fView.add(figure); fView.addToSelection(figure); } }
/** * Moves all the given figures by x and y. Doesn't announce any changes. Subclassers override * basicMoveBy. Clients usually call moveBy. * * @see moveBy */ protected void basicMoveBy(int x, int y) { FigureEnumeration k = figures(); while (k.hasMoreElements()) { Figure fig = k.nextFigure(); Figure obsrvd = (Figure) fig.getAttribute("observed.figure"); if (obsrvd == null || !includes(obsrvd)) { fig.moveBy(x, y); } } }
/** * Finds a top level Figure that intersects the given rectangle. It supresses the passed in * figure. Use this method to ignore a figure that is temporarily inserted into the drawing. */ public Figure findFigure(Rectangle r, Figure without) { if (without == null) return findFigure(r); FigureEnumeration k = figuresReverse(); while (k.hasMoreElements()) { Figure figure = k.nextFigure(); Rectangle fr = figure.displayBox(); if (r.intersects(fr) && !figure.includes(without)) return figure; } return null; }
private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException { s.defaultReadObject(); FigureEnumeration k = figures(); while (k.hasMoreElements()) { Figure figure = k.nextFigure(); figure.addToContainer(this); } }
/** Checks if the composite figure has the argument as one of its children. */ public boolean includes(Figure figure) { if (super.includes(figure)) return true; FigureEnumeration k = figures(); while (k.hasMoreElements()) { Figure f = k.nextFigure(); if (f.includes(figure)) return true; } return false; }
/** * Finds a figure but descends into a figure's children. It supresses the passed in figure. Use * this method to ignore a figure that is temporarily inserted into the drawing. */ public Figure findFigureInsideWithout(int x, int y, Figure without) { FigureEnumeration k = figuresReverse(); while (k.hasMoreElements()) { Figure figure = k.nextFigure(); if (figure != without) { Figure found = figure.findFigureInside(x, y); if (found != null) return found; } } return null; }
/** Check existance of figure in the drawing */ public boolean figureExists(Figure inf, FigureEnumeration fe) { while (fe.hasNextFigure()) { Figure figure = fe.nextFigure(); if (figure.includes(inf)) { return true; } } return false; }
/** * Hook method which can be overriden by subclasses to provide specialised behaviour in the event * of a popup trigger. */ protected void handlePopupMenu(MouseEvent e, int x, int y) { Figure figure = drawing().findFigure(e.getX(), e.getY()); if (figure != null) { Object attribute = figure.getAttribute(Figure.POPUP_MENU); if (attribute == null) { figure = drawing().findFigureInside(e.getX(), e.getY()); } if (figure != null) { showPopupMenu(figure, e.getX(), e.getY(), e.getComponent()); } } }
/** Gets an enumeration of the currently active handles. */ protected HandleEnumeration selectionHandles() { if (fSelectionHandles == null) { fSelectionHandles = CollectionsFactory.current().createList(); FigureEnumeration fe = selection(); while (fe.hasNextFigure()) { Figure figure = fe.nextFigure(); HandleEnumeration kk = figure.handles(); while (kk.hasNextHandle()) { fSelectionHandles.add(kk.nextHandle()); } } } return new HandleEnumerator(fSelectionHandles); }
/** Sends a figure to the back of the drawing. */ public synchronized void sendToBack(Figure figure) { if (fFigures.contains(figure)) { fFigures.removeElement(figure); fFigures.insertElementAt(figure, 0); figure.changed(); } }
/** Returns a FigureEnumeration of connectionfigures attached to this figure */ public FigureEnumeration getConnectionFigures(Figure inFigure) { // If no figure or figure is non connectable, just return null if (inFigure == null || !inFigure.canConnect()) { return null; } // if (inFigure instanceof ConnectionFigure) // return null; List result = CollectionsFactory.current().createList(5); FigureEnumeration figures = drawing().figures(); // Find all connection figures while (figures.hasNextFigure()) { Figure f = figures.nextFigure(); if ((f instanceof ConnectionFigure) && !(isFigureSelected(f))) { ConnectionFigure cf = (ConnectionFigure) f; if (cf.startFigure().includes(inFigure) || cf.endFigure().includes(inFigure)) { result.add(f); } } } return new FigureEnumerator(result); }
/** * Removes a figure from the composite. * * @see #removeAll */ public Figure remove(Figure figure) { if (fFigures.contains(figure)) { figure.removeFromContainer(this); fFigures.removeElement(figure); } return figure; }
/** Adds a figure to the list of figures. Initializes the the figure's container. */ public Figure add(Figure figure) { if (!fFigures.contains(figure)) { fFigures.addElement(figure); figure.addToContainer(this); } return figure; }
/** Brings a figure to the front. */ public synchronized void bringToFront(Figure figure) { if (fFigures.contains(figure)) { fFigures.removeElement(figure); fFigures.addElement(figure); figure.changed(); } }
/** Removes a figure from the selection. */ public void removeFromSelection(Figure figure) { if (isFigureSelected(figure)) { fSelection.remove(figure); fSelectionHandles = null; figure.invalidate(); fireSelectionChanged(); } }
/** * Adds a figure to the current selection. The figure is only selected if it is also contained in * the Drawing associated with this DrawingView. */ public void addToSelection(Figure figure) { if (!isFigureSelected(figure) && drawing().includes(figure)) { fSelection.add(figure); fSelectionHandles = null; figure.invalidate(); fireSelectionChanged(); } }
/** * This method displays a popup menu, if there is one registered with the Figure (the Figure's * attributes are queried for Figure.POPUP_MENU which is used to indicate an association of a * popup menu with the Figure). * * @param figure Figure for which a popup menu should be displayed * @param x x coordinate where the popup menu should be displayed * @param y y coordinate where the popup menu should be displayed * @param component Component which invoked the popup menu */ protected void showPopupMenu(Figure figure, int x, int y, Component comp) { final Object attribute = figure.getAttribute(Figure.POPUP_MENU); if ((attribute != null) && (attribute instanceof JPopupMenu)) { JPopupMenu popup = (JPopupMenu) attribute; if (popup instanceof PopupMenuFigureSelection) { ((PopupMenuFigureSelection) popup).setSelectedFigure(figure); } // calculate offsets for internal MDI frames Point newLocation = new Point(x, y); adjustOffsets(comp.getParent(), newLocation); popup.setLocation(newLocation); popup.setInvoker(comp); popup.setVisible(true); } }
/** * Inserts a FigureEnumeration of figures and translates them by the given offset. This function * is used to insert figures from clipboards (cut/copy) * * @return enumeration which has been added to the drawing. The figures in the enumeration can * have changed during adding them (e.g. they could have been decorated). */ public FigureEnumeration insertFigures(FigureEnumeration fe, int dx, int dy, boolean bCheck) { if (fe == null) { return FigureEnumerator.getEmptyEnumeration(); } List vCF = CollectionsFactory.current().createList(10); InsertIntoDrawingVisitor visitor = new InsertIntoDrawingVisitor(drawing()); while (fe.hasNextFigure()) { Figure figure = fe.nextFigure(); if (figure instanceof ConnectionFigure) { vCF.add(figure); } else if (figure != null) { figure.moveBy(dx, dy); figure.visit(visitor); } } FigureEnumeration ecf = new FigureEnumerator(vCF); while (ecf.hasNextFigure()) { ConnectionFigure cf = (ConnectionFigure) ecf.nextFigure(); Figure sf = cf.startFigure(); Figure ef = cf.endFigure(); if (figureExists(sf, drawing().figures()) && figureExists(ef, drawing().figures()) && (!bCheck || cf.canConnect(sf, ef))) { if (bCheck) { Point sp = sf.center(); Point ep = ef.center(); Connector fStartConnector = cf.startFigure().connectorAt(ep.x, ep.y); Connector fEndConnector = cf.endFigure().connectorAt(sp.x, sp.y); if (fEndConnector != null && fStartConnector != null) { cf.connectStart(fStartConnector); cf.connectEnd(fEndConnector); cf.updateConnection(); } } cf.visit(visitor); } } addToSelectionAll(visitor.getInsertedFigures()); return visitor.getInsertedFigures(); }
public Point locate(Figure owner) { Rectangle r = owner.displayBox(); return new Point(r.x + (int) (r.width * fRelativeX), r.y + (int) (r.height * fRelativeY)); }