001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.applications.designstudio.components;
016:
017: import java.awt.Color;
018: import java.awt.Cursor;
019: import java.awt.Graphics;
020: import java.awt.Point;
021: import java.awt.event.MouseEvent;
022: import java.awt.geom.Point2D;
023: import java.awt.geom.Rectangle2D;
024:
025: import javax.swing.SwingUtilities;
026:
027: import org.jgraph.graph.BasicMarqueeHandler;
028: import org.jgraph.graph.DefaultGraphCell;
029: import org.jgraph.graph.GraphConstants;
030: import org.jgraph.graph.Port;
031: import org.jgraph.graph.PortView;
032:
033: import com.metaboss.applications.designstudio.BaseGraphModel;
034: import com.metaboss.applications.designstudio.BaseGraphPanel;
035:
036: /* Graph Marquee Handler Class */
037:
038: public class MarqueeHandler extends BasicMarqueeHandler {
039: protected BaseGraphPanel mGraphPanel = null;
040: protected Point2D start, current; // Holds the Start and the Current Point
041: protected PortView port, firstPort; // Holds the First and the Current Port
042: protected MouseEvent mOldEvent = null;
043:
044: public MarqueeHandler(BaseGraphPanel pGraphPanel) {
045: super ();
046: mGraphPanel = pGraphPanel;
047: }
048:
049: // Override to Gain Control (for PopupMenu and ConnectMode)
050: public boolean isForceMarqueeEvent(MouseEvent e) {
051: if (SwingUtilities.isRightMouseButton(e)) {
052: Point2D lPoint = mGraphPanel.getGraph().fromScreen(
053: e.getPoint());
054: DefaultGraphCell lCell = (DefaultGraphCell) mGraphPanel
055: .getGraph().getFirstCellForLocation(lPoint.getX(),
056: lPoint.getY());
057: if (lCell != null /*&& !(lCell instanceof DefaultEdge)*/) {
058: mOldEvent = e;
059: return true;
060: }
061: }
062: port = getSourcePortAt(e.getPoint());
063: if (port != null && mGraphPanel.edgePending())
064: return true;
065: return super .isForceMarqueeEvent(e);
066: }
067:
068: // Display PopupMenu or Remember Start Location and First Port
069: public void mousePressed(final MouseEvent e) {
070: if (SwingUtilities.isRightMouseButton(e)) {
071: Point2D loc = mGraphPanel.getGraph().fromScreen(
072: e.getPoint());
073: Object cell = mGraphPanel.getGraph()
074: .getFirstCellForLocation(loc.getX(), loc.getY());
075: //mGraphPanel.showPopUpMenuForCell((DefaultGraphCell)cell, loc.x, loc.y);
076: mGraphPanel.showPopUpMenuForCell((DefaultGraphCell) cell, e
077: .getPoint().x, e.getPoint().y);
078: } else if (port != null && !e.isConsumed()
079: && mGraphPanel.edgePending()) {
080: start = mGraphPanel.getGraph().toScreen(
081: port.getLocation(null));
082: firstPort = port;
083: e.consume();
084: } else
085: super .mousePressed(e);
086: }
087:
088: // Find Port under Mouse and Repaint Connector
089: public void mouseDragged(MouseEvent e) {
090: // If remembered Start Point is Valid
091: if (start != null && !e.isConsumed()) {
092: // Fetch Graphics from Graph
093: Graphics g = mGraphPanel.getGraph().getGraphics();
094: // Xor-Paint the old Connector (Hide old Connector)
095: paintConnector(Color.black, mGraphPanel.getGraph()
096: .getBackground(), g);
097: // Reset Remembered Port
098: port = getTargetPortAt(e.getPoint());
099: // If Port was found then Point to Port Location
100: if (port != null)
101: current = mGraphPanel.getGraph().toScreen(
102: port.getLocation(null));
103: // Else If no Port was found then Point to Mouse Location
104: else
105: current = mGraphPanel.getGraph().snap(e.getPoint());
106: // Xor-Paint the new Connector
107: paintConnector(mGraphPanel.getGraph().getBackground(),
108: Color.black, g);
109: // Consume Event
110: e.consume();
111: }
112: super .mouseDragged(e);
113: }
114:
115: public PortView getSourcePortAt(Point point) {
116: // Scale from Screen to Model
117: Point2D tmp = mGraphPanel.getGraph().fromScreen(
118: GraphConstants.createPoint(point));
119: // Find a Port View in Model Coordinates and Remember
120: return mGraphPanel.getGraph().getPortViewAt(tmp.getX(),
121: tmp.getY());
122: }
123:
124: // Find a Cell at point and Return its first Port as a PortView
125: protected PortView getTargetPortAt(Point point) {
126: // Find Cell at point (No scaling needed here)
127: Object cell = mGraphPanel.getGraph().getFirstCellForLocation(
128: point.x, point.y);
129: // Loop Children to find PortView
130: for (int i = 0; i < mGraphPanel.getGraph().getModel()
131: .getChildCount(cell); i++) {
132: // Get Child from Model
133: Object tmp = mGraphPanel.getGraph().getModel().getChild(
134: cell, i);
135: // Get View for Child using the Graph's View as a Cell Mapper
136: //???tmp = graph.getView().getMapping(tmp, false);
137: // If Child View is a Port View and not equal to First Port
138: if (tmp instanceof PortView && tmp != firstPort)
139: return (PortView) tmp;
140: }
141: return getSourcePortAt(point);
142: }
143:
144: // Connect the First Port and the Current Port in the Graph or Repaint
145: public void mouseReleased(MouseEvent e) {
146: /*if (e!=null && !e.isConsumed() &&
147: port!=null && firstPort != null && firstPort != port)
148: {
149: mGraphPanel.connectEdges((Port) firstPort.getCell(), (Port) port.getCell());
150: e.consume();
151: }*/
152: ((BaseGraphModel) mGraphPanel.getGraph().getModel()).mOldEdgePoint = null;
153: if (e != null && !e.isConsumed() && firstPort != null
154: && firstPort != port) {
155: Port lPort = (port != null) ? (Port) port.getCell() : null;
156: if (lPort == null)
157: ((BaseGraphModel) mGraphPanel.getGraph().getModel()).mOldEdgePoint = GraphConstants
158: .createPoint(e.getX(), e.getY());
159: mGraphPanel.connectEdges((Port) firstPort.getCell(), lPort);
160: e.consume();
161: } else
162: mGraphPanel.getGraph().repaint();
163:
164: firstPort = port = null;
165: start = current = null;
166: super .mouseReleased(e);
167: }
168:
169: // Show Special Cursor if Over Port
170: public void mouseMoved(MouseEvent e) {
171: // Check Mode and Find Port
172: if (e != null && getSourcePortAt(e.getPoint()) != null
173: && !e.isConsumed() && mGraphPanel.edgePending()) {
174: // Set Cusor on Graph (Automatically Reset)
175: mGraphPanel.getGraph().setCursor(
176: new Cursor(Cursor.HAND_CURSOR));
177: // Consume Event
178: e.consume();
179: }
180: // Call Superclass
181: super .mouseReleased(e);
182: }
183:
184: public MouseEvent getOldMouseEvent() {
185: return mOldEvent;
186: }
187:
188: // Use Xor-Mode on Graphics to Paint Connector
189: protected void paintConnector(Color fg, Color bg, Graphics g) {
190: g.setColor(fg);
191: g.setXORMode(bg);
192: paintPort(mGraphPanel.getGraph().getGraphics());
193: if (firstPort != null && start != null && current != null)
194: g.drawLine((int) start.getX(), (int) start.getY(),
195: (int) current.getX(), (int) current.getY());
196: }
197:
198: // Use the Preview Flag to Draw a Highlighted Port
199: protected void paintPort(Graphics g) {
200: // If Current Port is Valid
201: if (port != null) {
202: // If Not Floating Port...
203: boolean o = (GraphConstants.getOffset(port.getAttributes()) != null);
204: // ...Then use Parent's Bounds
205: Rectangle2D r = (o) ? port.getBounds() : port
206: .getParentView().getBounds();
207: // Scale from Model to Screen
208: r = mGraphPanel.getGraph().toScreen(
209: GraphConstants.createRect(r));
210: // Add Space For the Highlight Border
211: r.setFrame(r.getX() - 3, r.getY() - 3, r.getWidth() + 6, r
212: .getHeight() + 6);
213: // Paint Port in Preview (=Highlight) Mode
214: mGraphPanel.getGraph().getUI().paintCell(g, port, r, true);
215: }
216: }
217: }
|