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.event.MouseEvent;
024:
025: import javax.swing.JTabbedPane;
026: import javax.swing.SwingUtilities;
027:
028: import com.jeta.forms.gui.components.ComponentSource;
029: import com.jeta.forms.gui.form.FormComponent;
030: import com.jeta.forms.gui.form.GridComponent;
031: import com.jeta.forms.gui.handler.CellMouseHandler;
032:
033: /**
034: * Mouse event handler for a standard grid cell that contains a JTabbedPane.
035: *
036: * @author Jeff Tassin
037: */
038: public class TabPaneCellHandler extends StandardCellHandler {
039: /** @directed */
040: private JTabbedPane m_tabpane;
041:
042: /** ctor */
043: public TabPaneCellHandler(GridComponent comp,
044: ComponentSource compsrc) {
045: super (comp, compsrc);
046: m_tabpane = (JTabbedPane) comp.getBeanDelegate();
047: }
048:
049: /**
050: * If the mouse event is over a form contained by the JTabPane
051: */
052: private FormComponent getForm(MouseEvent e) {
053: Component comp = m_tabpane.getSelectedComponent();
054: if (comp instanceof FormComponent) {
055: FormComponent fc = (FormComponent) comp;
056: Point local_pt = SwingUtilities.convertPoint((Component) e
057: .getSource(), e.getPoint(), fc.getParent());
058: if (local_pt.x >= fc.getX()
059: && local_pt.x < (fc.getX() + fc.getWidth())
060: && local_pt.y >= fc.getY()
061: && local_pt.y < (fc.getY() + fc.getHeight())) {
062: return fc;
063: }
064: }
065: return null;
066: }
067:
068: /**
069: * MouseMotionListener implementation. Note that the source will always be
070: * the top level window. So, we need to convert the mouse point to the
071: * coordinates of our associated overlay window before any other operation.
072: */
073: public void mouseMoved(MouseEvent e) {
074: FormComponent form = getForm(e);
075: if (form != null) {
076: CellMouseHandler handler = form.getMouseHandler();
077: if (handler != null)
078: handler.mouseMoved(e);
079: } else {
080: super .mouseMoved(e);
081: }
082: }
083:
084: public void mousePressed(MouseEvent e) {
085: FormComponent form = getForm(e);
086: if (form != null) {
087: CellMouseHandler handler = form.getMouseHandler();
088: if (handler != null)
089: handler.mousePressed(e);
090: } else {
091: isTabClicked(e);
092: super .mousePressed(e);
093: }
094: }
095:
096: private boolean isTabClicked(MouseEvent e) {
097: Point local_pt = SwingUtilities.convertPoint((Component) e
098: .getSource(), e.getPoint(), m_tabpane);
099: for (int index = 0; index < m_tabpane.getTabCount(); index++) {
100: java.awt.Rectangle rect = m_tabpane.getBoundsAt(index);
101: if (rect.contains(local_pt)) {
102: m_tabpane.setSelectedIndex(index);
103: return true;
104: }
105: }
106: return false;
107: }
108:
109: public void mouseReleased(MouseEvent e) {
110: FormComponent form = getForm(e);
111: if (form != null) {
112: CellMouseHandler handler = form.getMouseHandler();
113: if (handler != null)
114: handler.mouseReleased(e);
115: } else {
116: super .mouseReleased(e);
117: }
118: }
119:
120: /** MouseMotionListener implemenation */
121: public void mouseDragged(MouseEvent e) {
122: mouseMoved(e);
123: }
124:
125: }
|