001    /*
002      Copyright (C) 2002 Renaud Pawlak <renaud@aopsys.com>
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017    */
018    
019    package org.objectweb.jac.ide.diagrams;
020    
021    import CH.ifa.draw.framework.DrawingEditor;
022    import java.awt.Point;
023    import java.awt.event.MouseEvent;
024    import java.util.Collection;
025    import java.util.Iterator;
026    import java.util.List;
027    import java.util.Vector;
028    import org.objectweb.jac.aspects.gui.DisplayContext;
029    import org.objectweb.jac.aspects.gui.EventHandler;
030    import org.objectweb.jac.aspects.gui.InvokeEvent;
031    import org.objectweb.jac.core.rtti.ClassRepository;
032    import org.objectweb.jac.ide.Class;
033    import org.objectweb.jac.ide.Diagram;
034    import org.objectweb.jac.ide.Project;
035    import org.objectweb.jac.util.Log;
036    
037    public  class ClassFigureCreationTool extends CreationTool {
038    
039        DisplayContext context;
040    
041        public ClassFigureCreationTool(DrawingEditor newDrawingEditor, 
042                                       DisplayContext context) {
043            super(newDrawingEditor);
044            this.context = context;
045        }
046    
047        Point anchorPoint;
048    
049        /**
050         * Creates a new figure by cloning the prototype.
051         */
052        public void mouseDown(MouseEvent e, int x, int y) {
053            anchorPoint = new Point(x,y);
054            EventHandler.get().onInvoke(
055                context, 
056                new InvokeEvent(
057                    null,
058                    this, 
059                    ClassRepository.get().getClass(getClass())
060                    .getMethod("importClass(org.objectweb.jac.ide.Class,boolean)")));
061            // (view().add(getCreatedFigure())).displayBox(anchorPoint, anchorPoint);
062        }
063    
064        public void mouseUp(MouseEvent e, int x, int y) {
065        }
066    
067        /**
068         * Import class
069         * @param cl the class to import
070         * @param importRelations wether to also import relations with
071         * other classes on the diagram
072         */
073        public void importClass(Class cl, boolean importRelations) {
074            Log.trace("figures","createFigure for "+cl);
075            if (cl!=null) {
076                DiagramView diagram = ((DiagramView)editor());
077                diagram.addClass(cl,anchorPoint);
078    
079                if (importRelations) {
080                    diagram.importRelations(cl);            
081                }
082            }
083            editor().toolDone();
084        }
085    
086        public Collection importClassChoice() {
087            List result = new Vector();
088            Diagram diagram = (Diagram)((DiagramView)editor()).getSubstance();
089            Project project = diagram.getContainer().getProject();
090            Iterator it = project.getClasses().iterator();
091            while (it.hasNext()) {
092                Class cl = (Class)it.next();
093                if (!diagram.contains(cl))
094                    result.add(cl);
095            }
096            return result;
097        }
098    }