[Bprocessor-commit] /gl/src/net/sourceforge/bprocessor/gl/view PopupMenu.java, NONE, 1.1
Status: Pre-Alpha
Brought to you by:
henryml
From: rimestad <rim...@us...> - 2007-12-18 23:16:05
|
Update of /cvsroot/bprocessor//gl/src/net/sourceforge/bprocessor/gl/view In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv6128/src/net/sourceforge/bprocessor/gl/view Added Files: PopupMenu.java Log Message: more popupmenu refactoring --- NEW FILE: PopupMenu.java --- //--------------------------------------------------------------------------------- // $Id: PopupMenu.java,v 1.1 2007/12/18 23:15:01 rimestad Exp $ // // Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net) // Released under the Lesser GNU Public License v2.1 //--------------------------------------------------------------------------------- package net.sourceforge.bprocessor.gl.view; import java.awt.event.ActionEvent; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Set; import javax.swing.AbstractAction; import javax.swing.JMenu; import javax.swing.JPopupMenu; import net.sourceforge.bprocessor.gui.GUI; import net.sourceforge.bprocessor.gui.MenuFactory; import net.sourceforge.bprocessor.gui.actions.AttributeMenuAction; import net.sourceforge.bprocessor.gui.actions.CollectionMenuAction; import net.sourceforge.bprocessor.gui.actions.GeometricMenuAction; import net.sourceforge.bprocessor.gui.actions.EntityMenuAction; import net.sourceforge.bprocessor.gui.actions.ModellorMenuAction; import net.sourceforge.bprocessor.gui.actions.SpaceMenuAction; import net.sourceforge.bprocessor.gui.attrview.AttributeView; import net.sourceforge.bprocessor.model.Attribute; import net.sourceforge.bprocessor.model.Camera; import net.sourceforge.bprocessor.model.Command; import net.sourceforge.bprocessor.model.Component; import net.sourceforge.bprocessor.model.Constructor; import net.sourceforge.bprocessor.model.Container; import net.sourceforge.bprocessor.model.CoordinateSystem; import net.sourceforge.bprocessor.model.Edge; import net.sourceforge.bprocessor.model.Entity; import net.sourceforge.bprocessor.model.Geometric; import net.sourceforge.bprocessor.model.Geometry; import net.sourceforge.bprocessor.model.Mesh; import net.sourceforge.bprocessor.model.Project; import net.sourceforge.bprocessor.model.Selection; import net.sourceforge.bprocessor.model.Surface; import net.sourceforge.bprocessor.model.SurfaceAnalysis; import net.sourceforge.bprocessor.model.Vertex; import net.sourceforge.bprocessor.model.modellor.Modellor; import org.apache.log4j.Logger; /** * The static popup menu generator */ public class PopupMenu implements MenuFactory { /** The logger */ private static Logger log = Logger.getLogger(PopupMenu.class); private static final boolean CHECKS = false; /** * * */ public PopupMenu() { } /** * Make a popupmenu for a list of surfaces * @param surfaces the list of surfaces * @return the popupmenu */ private static JPopupMenu getSurfaceMenu(Collection surfaces) { JPopupMenu menu = getGeometricMenu(surfaces); if (surfaces.size() == 1) { Surface s = (Surface)surfaces.iterator().next(); AbstractAction focus = new EntityMenuAction(s, "Focus on") { public void actionPerformed(ActionEvent arg0) { Surface s = (Surface)entity; Camera.focusOn(Project.getInstance().getCurrentCamera(), s); } }; menu.add(focus); AbstractAction holeanalysis = new EntityMenuAction(s, "Hole analysis") { public void actionPerformed(ActionEvent arg0) { Surface surface = (Surface)entity; Geometry.holeAnalysis(surface); } }; menu.add(holeanalysis); AbstractAction flip = new EntityMenuAction(s, "Flip") { public void actionPerformed(ActionEvent arg0) { Surface surface = (Surface) entity; surface.flip(); } }; menu.add(flip); { AbstractAction action = new GeometricMenuAction(surfaces, "Smooth...") { public void actionPerformed(ActionEvent event) { Command command = new Command.Smooth((Surface) entities.iterator().next()); AttributeView.instance().display(command); } }; menu.add(action); } { AbstractAction action = new GeometricMenuAction(surfaces, "Offset Planar...") { public void actionPerformed(ActionEvent event) { Surface surface = (Surface) entities.iterator().next(); Command command = new Command.Offset(surface.getEdges()); AttributeView.instance().display(command); } }; menu.add(action); } { AbstractAction action = new GeometricMenuAction(surfaces, "Offset Normal...") { public void actionPerformed(ActionEvent event) { Surface surface = (Surface) entities.iterator().next(); Command command = new Command.OffsetNormal(surface); AttributeView.instance().display(command); } }; menu.add(action); } { AbstractAction action = new GeometricMenuAction(surfaces, "Extrude...") { public void actionPerformed(ActionEvent event) { Surface surface = (Surface) entities.iterator().next(); Command command = new Command.Extrude(surface); AttributeView.instance().display(command); } }; menu.add(action); } { AbstractAction action = new GeometricMenuAction(surfaces, "Insert Net...") { public void actionPerformed(ActionEvent event) { Surface surface = (Surface) entities.iterator().next(); Command command = new Command.InsertNet(surface); AttributeView.instance().display(command); } }; menu.add(action); } } return menu; } /** * * @param edges Collection of edges * @return Menu for edges */ private static JPopupMenu getEdgeMenu(Collection edges) { JPopupMenu menu = getGeometricMenu(edges); { AbstractAction action = new GeometricMenuAction(edges, "Split...") { public void actionPerformed(ActionEvent event) { Command command = new Command.Split((Edge) entities.iterator().next()); AttributeView.instance().display(command); } }; menu.add(action); } { AbstractAction action = new GeometricMenuAction(edges, "Offset...") { public void actionPerformed(ActionEvent event) { LinkedList<Edge> edges = new LinkedList(entities); if (edges.size() > 1) { List<Edge> ordered = Command.Offset.order(edges); if (ordered != null) { Command command = new Command.Offset(ordered); AttributeView.instance().display(command); } else { GUI.getInstance().alert("Select a connected set of edges"); } } else { GUI.getInstance().alert("Select more than one edge"); } } }; menu.add(action); } return menu; } /** * * @param systems Collection of coordinate systems * @return Menu for coordinate systems */ private static JPopupMenu getCoordinateSystemMenu(Collection systems) { JPopupMenu menu = getGeometricMenu(systems); return menu; } private abstract static class ModellorAction extends AbstractAction { protected Modellor modellor; protected Container space; public ModellorAction(Modellor modellor, Container space) { super(modellor.title()); this.modellor = modellor; this.space = space; } } /** * A popup generator for a collection of space * @param s the collection of spaces * @return the popup menu */ private static JPopupMenu getSpaceMenu(Collection s) { JPopupMenu menu = PopupMenu.getGeometricMenu(s); menu.addSeparator(); { AbstractAction action = new CollectionMenuAction(s, "Dissolve") { public void actionPerformed(ActionEvent event) { List<Container> spaces = new LinkedList(col); for (Container space : spaces) { Collection<Vertex> vertices = new LinkedList(space.getVertices()); Collection<Edge> edges = new LinkedList(space.getEdges()); Collection<Surface> surfaces = new LinkedList(space.getSurfaces()); for (Vertex current : vertices) { space.remove(current); } for (Vertex current : vertices) { space.getOwner().insert(current); } for (Edge current : edges) { space.remove(current); } for (Edge current : edges) { space.getOwner().insert(current); } for (Surface current : surfaces) { space.remove(current); current.setBackDomain(null); current.setFrontDomain(null); } for (Surface current : surfaces) { space.getOwner().insert(current); } } } }; menu.add(action); } if (s.size() == 1) { // Some options do only apply to one space Container sp = (Container)s.iterator().next(); AbstractAction edit = new SpaceMenuAction(sp, "Edit") { public void actionPerformed(ActionEvent arg0) { Selection.primary().clear(); Project p = Project.getInstance(); space.edit(); //FIXME: changed is also called in the edit-method on space //It is properly only needed once. p.changed(p); } }; menu.add(edit); { AbstractAction action = new SpaceMenuAction(sp, "Subdivide...") { public void actionPerformed(ActionEvent event) { Command command = new Command.Subdivide(space); AttributeView.instance().display(command); } }; menu.add(action); } { AbstractAction action = new SpaceMenuAction(sp, "Frame..") { public void actionPerformed(ActionEvent event) { Command command = new Command.Frame(space); AttributeView.instance().display(command); } }; menu.add(action); } { AbstractAction action = new SpaceMenuAction(sp, "Offset..") { public void actionPerformed(ActionEvent event) { Command command = new Command.Layer(space); AttributeView.instance().display(command); } }; menu.add(action); } { AbstractAction action = new SpaceMenuAction(sp, "Normals") { public void actionPerformed(ActionEvent event) { Container union = new Container("Normals", Container.CONSTRUCTION, true); for (Surface surface : space.getSurfaces()) { Vertex normal = surface.normal(); Vertex origin = surface.center(); CoordinateSystem system = CoordinateSystem.systemFor(origin, normal); Container pyramid = Command.Pyramid.pyramid(0.05, 0.05, 0.2); for (Vertex current : pyramid.getVertices()) { current.set(system.unTranslate(current)); } union.add(pyramid); } Project.getInstance().world().add(union); Project.getInstance().changed(Project.getInstance()); Project.getInstance().checkpoint(); } }; menu.add(action); } { AbstractAction action = new SpaceMenuAction(sp, "Make Component") { public void actionPerformed(ActionEvent event) { Component component = new Component(space); space.getOwner().remove(space); Project.getInstance().addCalalogObject(component); Selection.primary().clear(); Project.getInstance().changed(Project.getInstance()); Project.getInstance().checkpoint(); } }; menu.add(action); } if (CHECKS) { AbstractAction check = new SpaceMenuAction(sp, "Consistency Check") { public void actionPerformed(ActionEvent arg0) { space.check(); } }; menu.add(check); AbstractAction modCheck = new SpaceMenuAction(sp, "Modellor Check") { public void actionPerformed(ActionEvent arg0) { log.info(space.getModellor()); } }; menu.add(modCheck); } for (Modellor current : Modellor.getRegisteredModellors()) { if (current.application() == Modellor.DETAIL) { AbstractAction action = new ModellorAction(current, sp) { public void actionPerformed(ActionEvent e) { System.out.println("detail " + modellor); Modellor detailer = modellor.newInstance(space); Project.getInstance().changed(Project.getInstance()); Project.getInstance().addObserver(detailer); } }; menu.add(action); } } JMenu mm = new JMenu("Modellors"); Collection c = Modellor.getRegisteredModellors(); Iterator iter = c.iterator(); while (iter.hasNext()) { Modellor m = (Modellor)iter.next(); AbstractAction modellor = new ModellorMenuAction(sp, m); mm.add(modellor); } menu.add(mm); } return menu; } /** * a popup generator for a single entity * @param e The entity * @return the popup menu */ private static JPopupMenu getEntityMenu(Entity e) { Collection c = new LinkedList(); c.add(e); return getGeometricMenu(c); } /** * A generator of the attribute popup menu * @param attribute the attribute to make popup for * @return The popupmenu */ private static JPopupMenu getAttributeMenu(Attribute attribute) { JPopupMenu pm = new JPopupMenu(); AbstractAction delete = new AttributeMenuAction(attribute, "Delete") { public void actionPerformed(ActionEvent arg0) { Project p = Project.getInstance(); p.getGlobals().remove(attribute); p.changed(p); p.checkpoint(); } }; pm.add(delete); return pm; } private static CoordinateSystem systemFor(Geometric geometric) { if (geometric instanceof Vertex) { return systemFor((Vertex) geometric); } else if (geometric instanceof Surface) { return systemFor((Surface) geometric); } else if (geometric instanceof CoordinateSystem) { return (CoordinateSystem) geometric; } else { CoordinateSystem active = Project.getInstance().getActiveCoordinateSystem(); return active.copy(geometric.center()); } } private static CoordinateSystem systemFor(Vertex vertex) { Collection<Edge> edges = vertex.getEdges(); if (edges.size() == 0) { CoordinateSystem active = Project.getInstance().getActiveCoordinateSystem(); return active.copy(vertex.copy()); } else { double x = 0; double y = 0; double z = 0; for (Edge current : edges) { Vertex other = current.otherVertex(vertex); Vertex v = other.minus(vertex); x += v.getX(); y += v.getY(); z += v.getZ(); } x /= edges.size(); y /= edges.size(); z /= edges.size(); Vertex n = new Vertex(-x, -y, -z); Edge edge = edges.iterator().next(); Vertex other = edge.otherVertex(vertex); Vertex v = other.minus(vertex); Vertex i = n.cross(v); Vertex j = n.cross(i); Vertex o = vertex.copy(); return new CoordinateSystem(i, j, n, o); } } private static CoordinateSystem systemFor(Surface surface) { Vertex normal = surface.normal(); Vertex origin = surface.center(); return CoordinateSystem.systemFor(origin, normal); } /** * A popupmenu generator for entities * @param arguments the entity list to make popup for * @return the popupmenu */ private static JPopupMenu getGeometricMenu(Collection<Geometric> arguments) { JPopupMenu menu = new JPopupMenu(); AbstractAction delete = new GeometricMenuAction(arguments, "Delete") { public void actionPerformed(ActionEvent arg0) { Selection.primary().clear(); for (Geometric current : entities) { current.erase(); Project.getInstance().changed(current); } Project.getInstance().changed(Geometry.collect(entities)); Project.getInstance().checkpoint(); } }; menu.add(delete); AbstractAction allConnected = new GeometricMenuAction(arguments, "All Connected") { public void actionPerformed(ActionEvent arg0) { Collection<Geometric> geometrics = Geometric.connected(new LinkedList(entities)); Selection.primary().set(geometrics); } }; menu.add(allConnected); AbstractAction simplify = new GeometricMenuAction(arguments, "Simplify Geometry") { public void actionPerformed(ActionEvent arg0) { Geometry.simplifyGeometry(entities); } }; menu.add(simplify); { AbstractAction action = new GeometricMenuAction(arguments, "Make Union") { public void actionPerformed(ActionEvent event) { Mesh mesh = new Mesh(entities); HashMap map = new HashMap(); Mesh copy = mesh.copy(map); Container union = new Container("Union", Container.CONSTRUCTION, true); union.setUnion(true); for (Vertex current : copy.vertices()) { union.add(current); } for (Edge current : copy.edges()) { union.add(current); } for (Surface current : copy.surfaces()) { union.add(current); } for (Container current : copy.elements()) { union.add(current); } for (Geometric current : entities) { current.delete(); } Selection.primary().clear(); Project.getInstance().getActiveSpace().add(union); Project.getInstance().changed(Project.getInstance()); Project.getInstance().checkpoint(); } }; menu.add(action); } { AbstractAction action = new GeometricMenuAction(arguments, "Align Axes") { public void actionPerformed(ActionEvent event) { if (entities.size() == 1) { Geometric geometric = entities.iterator().next(); CoordinateSystem system = systemFor(geometric); Vertex i = system.getI(); Vertex j = system.getJ(); Vertex n = system.getN(); Vertex o = system.getOrigin(); CoordinateSystem active = Project.getInstance().getActiveCoordinateSystem(); active.setIJN(i, j, n); active.setOrigin(o); Project.getInstance().changed(Project.getInstance()); Project.getInstance().checkpoint(); } } }; menu.add(action); } { AbstractAction action = new GeometricMenuAction(arguments, "Scale...") { public void actionPerformed(ActionEvent event) { Set<Vertex> vertices = Geometry.collect(entities); Vertex center = null; if (entities.size() == 1) { Geometric geometric = entities.iterator().next(); if (geometric instanceof Constructor) { Constructor constructor = (Constructor) geometric; center = constructor.getOrigin(); } } if (center == null) { center = Vertex.center(vertices); } CoordinateSystem system = Project.getInstance().getActiveCoordinateSystem(); system = system.copy(); system.setOrigin(center); Command command = new Command.Scale(vertices, system); AttributeView.instance().display(command); } }; menu.add(action); } { AbstractAction action = new GeometricMenuAction(arguments, "Translate...") { public void actionPerformed(ActionEvent event) { Set<Vertex> vertices = Geometry.collect(entities); Vertex center = null; if (entities.size() == 1) { Geometric geometric = entities.iterator().next(); if (geometric instanceof Constructor) { Constructor constructor = (Constructor) geometric; center = constructor.getOrigin(); } } if (center == null) { center = Vertex.center(vertices); } CoordinateSystem system = Project.getInstance().getActiveCoordinateSystem(); system = system.copy(); system.setOrigin(center); Command command = new Command.Translate(vertices, system); AttributeView.instance().display(command); } }; menu.add(action); } { AbstractAction action = new GeometricMenuAction(arguments, "Rotate...") { public void actionPerformed(ActionEvent event) { Set<Vertex> vertices = Geometry.collect(entities); Vertex center = null; if (entities.size() == 1) { Geometric geometric = entities.iterator().next(); if (geometric instanceof Constructor) { Constructor constructor = (Constructor) geometric; center = constructor.getOrigin(); } } if (center == null) { center = Vertex.center(vertices); } CoordinateSystem system = Project.getInstance().getActiveCoordinateSystem(); system = system.copy(); system.setOrigin(center); Command command = new Command.Rotate(vertices, system); AttributeView.instance().display(command); } }; menu.add(action); } { AbstractAction action = new GeometricMenuAction(arguments, "Transform In") { public void actionPerformed(ActionEvent event) { List<Vertex> vertices = new ArrayList(Geometry.collect(entities)); CoordinateSystem system = Project.getInstance().getActiveCoordinateSystem(); system.unTranslateIt(vertices); for (Vertex current : vertices) { current.update(); } Project.getInstance().changed(Project.getInstance()); Project.getInstance().checkpoint(); } }; menu.add(action); } { AbstractAction action = new GeometricMenuAction(arguments, "Transform Out") { public void actionPerformed(ActionEvent event) { List<Vertex> vertices = new ArrayList(Geometry.collect(entities)); CoordinateSystem system = Project.getInstance().getActiveCoordinateSystem(); system.translateIt(vertices); for (Vertex current : vertices) { current.update(); } Project.getInstance().changed(Project.getInstance()); Project.getInstance().checkpoint(); } }; menu.add(action); } return menu; } /** * Get background menu * @return Background menu */ private static JPopupMenu getBackgroundMenu() { JPopupMenu menu = new JPopupMenu(); { AbstractAction action = new AbstractAction("Reset Axes") { public void actionPerformed(ActionEvent event) { CoordinateSystem system = Project.getInstance().getActiveCoordinateSystem(); Vertex i = new Vertex(1, 0, 0); Vertex j = new Vertex(0, 1, 0); Vertex n = new Vertex(0, 0, 1); Vertex o = new Vertex(0, 0, 0); system.setIJN(i, j, n); system.setOrigin(o); Project.getInstance().changed(Project.getInstance()); Project.getInstance().checkpoint(); } }; menu.add(action); } { AbstractAction action = new AbstractAction("Cube...") { public void actionPerformed(ActionEvent event) { Command command = new Command.Cube(); AttributeView.instance().display(command); } }; menu.add(action); } { AbstractAction action = new AbstractAction("Cylinder...") { public void actionPerformed(ActionEvent event) { Command command = new Command.Cylinder(); AttributeView.instance().display(command); } }; menu.add(action); } { AbstractAction action = new AbstractAction("Pyramid...") { public void actionPerformed(ActionEvent event) { Command command = new Command.Pyramid(); AttributeView.instance().display(command); } }; menu.add(action); } return menu; } /** * Create a popupMenu for the selection * @return The popupmenu or null if the selection is empty */ private static JPopupMenu makeSelectionMenu() { if (Selection.primary().isEmpty()) { return null; } else { Iterator sel = Selection.primary().iterator(); Object type = null; while (sel.hasNext()) { Object next = sel.next(); if (type == null) { type = next; } else if (!next.getClass().isInstance(type)) { return PopupMenu.getGeometricMenu(new LinkedList<Geometric>(Selection.primary())); } } if (type instanceof Container) { return PopupMenu.getSpaceMenu(new LinkedList<Geometric>(Selection.primary())); } else if (type instanceof Surface) { return PopupMenu.getSurfaceMenu(new LinkedList<Geometric>(Selection.primary())); } else if (type instanceof Edge) { return getEdgeMenu(new LinkedList<Geometric>(Selection.primary())); } else if (type instanceof CoordinateSystem) { return getCoordinateSystemMenu(new LinkedList<Geometric>(Selection.primary())); } else { return PopupMenu.getGeometricMenu(new LinkedList<Geometric>(Selection.primary())); } } } /** * * @return JPopupMenu */ private JPopupMenu getAnalysisMenu() { JPopupMenu menu = new JPopupMenu(); AbstractAction planeanalysis = new AbstractAction("Plane Analysis") { public void actionPerformed(ActionEvent arg0) { Container space = Project.getInstance().getActiveSpace(); SurfaceAnalysis analysis = new SurfaceAnalysis(); analysis.clearPlanes(space); Collection planes = analysis.planeAnalysis(space); Iterator iter = planes.iterator(); while (iter.hasNext()) { CoordinateSystem current = (CoordinateSystem) iter.next(); space.insert(current); } Project.getInstance().changed(space); Project.getInstance().checkpoint(); } }; menu.add(planeanalysis); AbstractAction clearplanes = new AbstractAction("Delete Planes") { public void actionPerformed(ActionEvent arg0) { Container space = Project.getInstance().getActiveSpace(); SurfaceAnalysis analysis = new SurfaceAnalysis(); analysis.clearPlanes(space); Project.getInstance().changed(space); Project.getInstance().checkpoint(); } }; menu.add(clearplanes); AbstractAction surfaceanalysis = new AbstractAction("Surface Analysis") { public void actionPerformed(ActionEvent arg0) { Container space = Project.getInstance().getActiveSpace(); SurfaceAnalysis analysis = new SurfaceAnalysis(); analysis.surfaceAnalysis(space); Project.getInstance().changed(space); Project.getInstance().checkpoint(); } }; menu.add(surfaceanalysis); AbstractAction clearsurfaces = new AbstractAction("Delete Surfaces") { public void actionPerformed(ActionEvent arg0) { Container space = Project.getInstance().getActiveSpace(); SurfaceAnalysis analysis = new SurfaceAnalysis(); analysis.clearSurfaces(space); Project.getInstance().changed(space); Project.getInstance().checkpoint(); } }; menu.add(clearsurfaces); return menu; } /** {@inheritDoc} */ public JPopupMenu menuFor(Geometric geometric) { return null; } /** {@inheritDoc} */ public JPopupMenu menuFor(Collection<Geometric> geometrics) { if (geometrics == Selection.primary()) { return makeSelectionMenu(); } else if (geometrics.isEmpty()) { return PopupMenu.getBackgroundMenu(); } else { return PopupMenu.getGeometricMenu(geometrics); } } /** {@inheritDoc} */ public JPopupMenu menuFor(Entity entity) { return getEntityMenu(entity); } /** {@inheritDoc} */ public JPopupMenu menuFor(Attribute attribute) { return getAttributeMenu(attribute); } } |