001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library 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 GNU
012: * 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 library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.handler;
020:
021: import java.awt.Component;
022: import java.awt.Point;
023: import java.awt.Rectangle;
024: import java.awt.event.MouseEvent;
025:
026: import javax.swing.JComponent;
027: import javax.swing.SwingUtilities;
028:
029: import com.jeta.forms.gui.components.ComponentSource;
030: import com.jeta.forms.gui.form.FormComponent;
031: import com.jeta.forms.gui.form.GridCellEvent;
032: import com.jeta.forms.gui.form.GridComponent;
033: import com.jeta.forms.gui.form.GridOverlay;
034: import com.jeta.forms.gui.form.GridView;
035: import com.jeta.forms.gui.formmgr.FormManager;
036: import com.jeta.forms.gui.handler.CellMouseHandler;
037: import com.jeta.open.registry.JETARegistry;
038: import com.jeta.swingbuilder.gui.editor.DesignFormComponent;
039: import com.jeta.swingbuilder.gui.editor.DesignGridOverlay;
040: import com.jeta.swingbuilder.gui.editor.FormEditor;
041:
042: /**
043: * MouseHandler for a Form in design mode.
044: *
045: * @author Jeff Tassin
046: */
047: public class FormCellHandler extends AbstractMouseHandler {
048: /** @directed */
049: private DesignFormComponent m_comp;
050:
051: /**
052: * The component source
053: */
054: private ComponentSource m_compsrc;
055:
056: public FormCellHandler(FormComponent comp, ComponentSource compsrc) {
057: super (comp);
058: assert (comp != null);
059: assert (compsrc != null);
060: m_comp = (DesignFormComponent) comp;
061: m_compsrc = compsrc;
062: }
063:
064: /**
065: * @return the grid component that contains the given mouse ponit.
066: */
067: private GridComponent getComponent(MouseEvent e) {
068: DesignGridOverlay overlay = (DesignGridOverlay) m_comp
069: .getChildOverlay();
070: Point local_pt = SwingUtilities.convertPoint((Component) e
071: .getSource(), e.getPoint(), (Component) overlay);
072: GridComponent cell = overlay.getCell(local_pt);
073: if (cell == null) {
074: DesignFormComponent parent = (DesignFormComponent) getParentForm(m_comp);
075: if (parent != null) {
076: DesignGridOverlay parentoverlay = (DesignGridOverlay) parent
077: .getChildOverlay();
078: local_pt = SwingUtilities.convertPoint((Component) e
079: .getSource(), e.getPoint(),
080: (Component) parentoverlay);
081:
082: int y = local_pt.y;
083: int x = local_pt.x;
084:
085: if (x >= m_comp.getCellX()
086: && x <= m_comp.getCellX()
087: + m_comp.getCellWidth()
088: && y >= m_comp.getCellY()
089: && y <= m_comp.getCellY()
090: + m_comp.getCellHeight()) {
091: cell = m_comp;
092: }
093: }
094: }
095: return cell;
096: }
097:
098: /**
099: * @return the parent form of the given child form.
100: */
101: public FormComponent getParentForm(FormComponent comp) {
102: Component parent = comp.getParent();
103: while (parent != null) {
104: if (parent instanceof FormComponent)
105: return (FormComponent) parent;
106: else if (parent instanceof FormEditor)
107: return null;
108: else if (parent instanceof javax.swing.JFrame)
109: return null;
110:
111: parent = parent.getParent();
112: }
113: return null;
114: }
115:
116: /**
117: * @return the view that is contained within this component.
118: */
119: private GridView getView() {
120: return (GridView) m_comp.getBean().getDelegate();
121: }
122:
123: /**
124: * Determines if the mouse is pressed over a control button. If so, handles
125: * the event accordingly.
126: */
127: private boolean handleControlButton(MouseEvent e) {
128: if (e.getID() == MouseEvent.MOUSE_PRESSED) {
129: java.awt.Container btnpanel = m_comp.getButtonPanel();
130: JComponent btn = m_comp.getExpandButton();
131: Point local_pt = SwingUtilities.convertPoint((Component) e
132: .getSource(), e.getPoint(), btnpanel);
133:
134: Rectangle rect = btn.getBounds();
135: if (rect.contains(local_pt)) {
136: m_comp.setGridViewVisible(!m_comp.isGridViewVisible());
137: return true;
138: }
139:
140: btn = m_comp.getEditButton();
141: rect = btn.getBounds();
142: if (rect.contains(local_pt)) {
143: FormManager fmgr = (FormManager) JETARegistry
144: .lookup(FormManager.COMPONENT_ID);
145: fmgr.showForm(m_comp.getId());
146: return true;
147: }
148:
149: btn = m_comp.getGridButton();
150: rect = btn.getBounds();
151: if (rect.contains(local_pt)) {
152: GridView view = m_comp.getChildView();
153: boolean bvis = !view.isGridVisible();
154: if (bvis) {
155: m_comp.setGridViewVisible(bvis);
156: } else {
157: view.setGridVisible(bvis);
158: }
159: return true;
160: }
161: }
162:
163: return false;
164: }
165:
166: public void mousePressed(MouseEvent e) {
167: GridView view = getView();
168: if (view != null)
169: view.deselectAll();
170:
171: GridComponent cell = getComponent(e);
172: if (cell == m_comp) {
173: if (!handleControlButton(e)) {
174: GridView childview = m_comp.getChildView();
175: childview.deselectAll();
176:
177: if (m_compsrc.isSelectionTool()) {
178: m_comp.setSelected(true);
179: m_comp.repaint();
180: if (e.getClickCount() > 1) {
181: m_comp.fireGridCellEvent(new GridCellEvent(
182: GridCellEvent.EDIT_COMPONENT, m_comp));
183: } else if (e.getClickCount() == 1) {
184: setDragSource(this );
185: }
186: }
187: }
188: } else if (cell != null) {
189: CellMouseHandler handler = cell.getMouseHandler();
190: assert (handler != null);
191: if (handler != null) {
192: handler.mousePressed(e);
193: }
194: m_comp.repaint();
195: }
196: }
197:
198: public void mouseReleased(MouseEvent e) {
199: if (isDragging()) {
200: GridComponent cell = getComponent(e);
201: if (cell == m_comp) {
202: if (getDragSource() != this ) {
203: // mouse released on this entire form, so we replace the
204: // form.
205:
206: }
207: } else if (cell != null) {
208: if (getDragSource() != this ) {
209: // don't allow drops on children of this form if we are the
210: // drag source
211: CellMouseHandler handler = cell.getMouseHandler();
212: if (handler != null) {
213: handler.mouseReleased(e);
214: }
215: m_comp.repaint();
216: }
217: }
218: }
219: }
220:
221: /** MouseMotionListener implemenation */
222: public void mouseDragged(MouseEvent e) {
223: mouseMoved(e);
224: }
225:
226: /**
227: * MouseMotionListener implementation. Note that the source will always be
228: * the top level window. So, we need to convert the mouse point to the
229: * coordinates of our associated overlay window before any other operation.
230: */
231: public void mouseMoved(MouseEvent e) {
232: if (!m_compsrc.isSelectionTool() || isDragging()) {
233: GridView view = getView();
234: if (view != null)
235: view.deselectAll();
236: }
237:
238: GridOverlay overlay = m_comp.getChildOverlay();
239: GridComponent cell = getComponent(e);
240: if (cell != null) {
241: CellMouseHandler handler = cell.getMouseHandler();
242: if (handler == null) {
243: // System.out.println( "found null handler at row: " +
244: // cell.getRow() + " col: " + cell.getColumn() + " for: " +
245: // cell.getComponent() );
246: assert (false);
247: } else {
248: if (getDragSource() != this) {
249: if (handler != this) {
250: handler.mouseMoved(e);
251: }
252: }
253: }
254: }
255: }
256:
257: }
|