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 javax.swing.event.ListSelectionEvent;
19  import javax.swing.event.ListSelectionListener;
20  import javax.swing.event.TreeSelectionEvent;
21  import javax.swing.event.TreeSelectionListener;
22  
23  import org.xulux.api.core.PartRequest;
24  import org.xulux.api.gui.IWidget;
25  import org.xulux.rules.Rule;
26  import org.xulux.rules.impl.WidgetRequestImpl;
27  import org.xulux.utils.ClassLoaderUtils;
28  
29  /**
30   * The selection listener refreshes all widgets when a new entry has been
31   * selected. It only refreshes when a widget actually needs data from
32   * the calling widget.
33   *
34   * eg have a pointer in the field like ?Table:Person.name
35   *
36   * This works only within parts.
37   *
38   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
39   * @version $Id: NewSelectionListener.java,v 1.1 2005/12/18 12:58:21 mvdb Exp $
40   */
41  public class NewSelectionListener implements ListSelectionListener, TreeSelectionListener {
42  
43      /**
44       * the widget
45       */
46      protected IWidget widget;
47  
48      /**
49       * @param widget the widget
50       */
51      public NewSelectionListener(IWidget widget) {
52          this.widget = widget;
53      }
54  
55      /**
56       * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
57       */
58      public void valueChanged(ListSelectionEvent e) {
59          valueChanged();
60      }
61  
62      /**
63       * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent)
64       */
65      public void valueChanged(TreeSelectionEvent e) {
66          valueChanged();
67      }
68  
69      /**
70       * Refresh the value of all widgets that are pointing this widget.
71       */
72      public void valueChanged() {
73          // don't do anything when the widget is null
74          if (widget == null) {
75              return;
76          }
77          String singleClick = widget.getProperty("singleclick");
78          if (singleClick != null) {
79              try {
80                  Rule rule = (Rule) ClassLoaderUtils.getObjectFromClassString(singleClick);
81                  rule.setOwner(widget);
82                  WidgetRequestImpl impl = new WidgetRequestImpl(widget, PartRequest.NO_ACTION);
83                  rule.post(impl);
84              } catch (Throwable t) {
85                  System.err.println("Error occured during execution of post rule");
86                  t.printStackTrace();
87              }
88          }
89          if (widget != null) {
90              widget.getPart().refreshWidgets(widget);
91          }
92      }
93  
94  }