View Javadoc

1   /*
2      Copyright 2002-2006 Martin van den Bemt
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15  */
16  package org.xulux.guilayer.swing.listeners;
17  
18  import java.awt.Component;
19  import java.awt.event.MouseEvent;
20  import java.awt.event.MouseListener;
21  import java.util.List;
22  
23  import javax.swing.JComponent;
24  import javax.swing.JPopupMenu;
25  import javax.swing.JTable;
26  import javax.swing.SwingUtilities;
27  
28  import org.xulux.api.core.PartRequest;
29  import org.xulux.api.gui.IWidget;
30  import org.xulux.api.rules.IRuleEngine;
31  import org.xulux.core.XuluxContext;
32  import org.xulux.gui.GuiUtils;
33  import org.xulux.gui.XuluxListener;
34  import org.xulux.rules.impl.WidgetRequestImpl;
35  
36  /**
37   * A popuplistener. Shows the popup when the right mousebutton is clicked
38   *
39   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
40   * @version $Id: PopupListener.java,v 1.1 2005/12/18 12:58:21 mvdb Exp $
41   */
42  public class PopupListener extends XuluxListener implements MouseListener {
43  
44      /**
45       * The widget that instantiated the popuplistener
46       */
47      private IWidget parent;
48      /**
49       *
50       */
51      public PopupListener() {
52          super();
53      }
54  
55      /**
56       * @param widget the widget
57       * @param parent the widget that instantiated the popuplistener.
58       */
59      public PopupListener(IWidget widget, IWidget parent) {
60          super(widget);
61          this.parent = parent;
62      }
63  
64      /**
65       * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
66       */
67      public void mousePressed(MouseEvent e) {
68        showPopup(e);
69      }
70  
71      /**
72       * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
73       */
74      public void mouseReleased(MouseEvent e) {
75          showPopup(e);
76      }
77      
78      public void showPopup(MouseEvent e) {
79        if (e.isPopupTrigger()) {
80          Object value = null;
81          Component component = e.getComponent();
82          if (component instanceof JTable) {
83            JTable table = (JTable) component;
84            int row = table.rowAtPoint(e.getPoint());
85            table.setRowSelectionInterval(row, row);
86            value = table.getValueAt(row, -1);
87          }
88          parent.setLazyProperty("rowValue", value);
89          System.out.println("Comp : " + e.getComponent().getComponentAt(e.getPoint()).getComponentAt(e.getPoint()));
90          WidgetRequestImpl impl = new WidgetRequestImpl(getWidget().getParent(), PartRequest.ACTION_VALUE_CHANGED);
91          XuluxContext.getRuleEngine().fireFieldRequest(getWidget(), impl, IRuleEngine.PRE_REQUEST);
92          List list = getWidget().getChildWidgets();
93          if (list != null) {
94              for (int i = 0; i < list.size(); i++) {
95                  IWidget widget = (IWidget) list.get(i);
96                  XuluxContext.getRuleEngine().fireFieldRequest(widget, impl, IRuleEngine.PRE_REQUEST);
97              }
98          }
99  //        parent.setLazyProperty("rowValue", null);
100         JComponent comp = (JComponent) getWidget().getNativeWidget();
101         if (comp instanceof JPopupMenu) {
102           ((JPopupMenu) comp).show((Component) e.getSource(), e.getX(), e.getY());
103         }
104         
105       }
106     }
107     /**
108      * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
109      */
110     public void mouseClicked(MouseEvent e) {
111       if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e)) {
112         // if detected a doubleclick and item is doubleclick item.
113         String dblName = widget.getProperty("doubleclick");
114         if (dblName == null) {
115           // try to get it at the parent
116           dblName = widget.getParent().getProperty("doubleclick");
117         }
118         if (dblName != null) {
119           Component component = e.getComponent();
120           if (component instanceof JTable) {
121             JTable jt = (JTable) component;
122             widget.getParent().setLazyProperty("rowValue", jt.getValueAt(jt.getSelectedRow(), -1));
123           }
124           IWidget widget = (IWidget) getWidget().getPart().getWidget(dblName);
125           GuiUtils.fireFieldPostRule(widget, widget, PartRequest.NO_ACTION);
126           // clear the rowValue..
127           widget.setLazyProperty("rowValue", null);
128           return;
129         }
130       }
131       showPopup(e);
132     }
133 
134     /**
135      * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
136      */
137     public void mouseEntered(MouseEvent e) {
138 
139     }
140 
141     /**
142      * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
143      */
144     public void mouseExited(MouseEvent e) {
145 
146     }
147 
148 }