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.event.ActionEvent;
19  import java.awt.event.ActionListener;
20  import java.awt.event.FocusEvent;
21  import java.awt.event.FocusListener;
22  import java.awt.event.ItemEvent;
23  import java.awt.event.ItemListener;
24  
25  import javax.swing.JCheckBox;
26  
27  import org.xulux.api.gui.IWidget;
28  import org.xulux.gui.GuiUtils;
29  import org.xulux.gui.XuluxListener;
30  import org.xulux.gui.utils.XuluxEventQueue;
31  import org.xulux.guilayer.swing.widgets.Button;
32  import org.xulux.guilayer.swing.widgets.CheckBox;
33  import org.xulux.guilayer.swing.widgets.MenuItem;
34  import org.xulux.guilayer.swing.widgets.RadioButton;
35  import org.xulux.guilayer.swing.widgets.ToggleButton;
36  
37  /**
38   * Maybe use some kind of cache to see what next event comes through??
39   * Functionality like hasWaitingRequests() or something like that.
40   * Also if a user closes the window, widget.destroy should be called
41   * 
42   * @todo Find a better way to handle the cancel button.
43   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
44   * @version $Id: PrePostFieldListener.java,v 1.1 2005/12/18 12:58:21 mvdb Exp $
45   */
46  public class PrePostFieldListener extends XuluxListener implements FocusListener, ActionListener, ItemListener {
47  
48      /**
49       * The constructor
50       */
51      public PrePostFieldListener() {
52          super();
53      }
54      /**
55       * Constructor for PrePostFieldListener.
56       * @param widget the widget
57       */
58      public PrePostFieldListener(IWidget widget) {
59          super(widget);
60      }
61  
62      /**
63       * now call pre..
64       * @see java.awt.event.FocusListener#focusGained(FocusEvent)
65       */
66      public void focusGained(FocusEvent e) {
67          if (isProcessing()) {
68              return;
69          }
70          if (e.getID() != FocusEvent.FOCUS_GAINED || e.isTemporary()) {
71              return;
72          }
73          XuluxEventQueue q = XuluxEventQueue.getInstance();
74          q.holdEvents(false);
75          started();
76      }
77  
78      /**
79       * now call post..
80       * @see java.awt.event.FocusListener#focusLost(FocusEvent)
81       */
82      public void focusLost(FocusEvent e) {
83          if (isProcessing()) {
84              return;
85          }
86          if (e.getID() != FocusEvent.FOCUS_LOST || e.isTemporary()) {
87              return;
88          }
89          XuluxEventQueue q = XuluxEventQueue.getInstance();
90          // @todo make test..
91          // A checkbox would consume an event, so it wouldn't process 
92          // any further.. Need to make a test of this!
93          if (!(e.getComponent() instanceof JCheckBox)) { 
94              q.holdEvents(true);
95              q.holdAccepted(this);
96          }
97      }
98  
99      /**
100      * @todo Make required check for combo with the notselectedValue..
101      *
102      * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
103      */
104     public void actionPerformed(ActionEvent e) {
105         if (isProcessing()) {
106             return;
107         }
108         if (widget.isRefreshing()) {
109             return;
110         }
111         XuluxEventQueue q = XuluxEventQueue.getInstance();
112         if (widget instanceof Button || widget instanceof MenuItem) {
113             boolean isCancel = GuiUtils.processCancel(widget);
114             if (isCancel) {
115                 // drop all events and accepted in the event queue..
116                 q.clearAccepted();
117             }
118             // free event queue.
119             q.holdEvents(false);
120         }
121         if (accepted(widget)) {
122             completed();
123         }
124     }
125 
126     /**
127      * Sets the correct value when a checkbox is
128      * clicked. It will call the post after the
129      * value is adjusted.
130      *
131      * @todo optimize this using native boolean ??
132      * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
133      */
134     public void itemStateChanged(ItemEvent e) {
135         // make sure we don't end up in a loop by checking
136         // the fact if the widget is currently refreshing or not..
137         //        if (isProcessing()) {
138         //            return;
139         //        }
140         if (widget.isRefreshing()) {
141             return;
142         }
143         boolean refresh = false;
144         // reset the hold events to process previous events..
145         if (widget instanceof CheckBox || widget instanceof RadioButton || widget instanceof ToggleButton) {
146             if (e.getStateChange() == ItemEvent.SELECTED) {
147                 widget.setValue("true");
148                 refresh = true;
149 
150             } else if (e.getStateChange() == ItemEvent.DESELECTED) {
151                 widget.setValue("false");
152                 refresh = true;
153             }
154             System.out.println("Checkbox or RadioButton clicked on NyxWidget : " + widget.getName() + " value: " + widget.getValue());
155         }
156         if (refresh) {
157             XuluxEventQueue.getInstance().holdEvents(false);
158             widget.getPart().refreshFields(widget);
159             widget.getPart().updateDependandWidgets(widget);
160         }
161         completed();
162     }
163 
164 }