Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: iiuf/xmillum/handlers/PopupFlagger.java


1   /* (C) 2000-2002, DIUF, http://www.unifr.ch/diuf
2    *
3    * This program is free software; you can redistribute it and/or modify it
4    * under the terms of the GNU General Public License as published by the
5    * Free Software Foundation; either version 2 of the License, or (at your
6    * option) any later version.
7    *
8    * This program is distributed in the hope that it will be useful, but
9    * WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
11   * Public License for more details.
12   *
13   * You should have received a copy of the GNU General Public License along
14   * with this program; if not, write to the Free Software Foundation, Inc.,
15   * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16   */
17  
18  package iiuf.xmillum.handlers;
19  
20  import iiuf.dom.DOMUtils;
21  import iiuf.xmillum.ActionHandler;
22  import iiuf.xmillum.ActionHandlerParam;
23  import iiuf.xmillum.BrowserContext;
24  import iiuf.xmillum.Displayable;
25  import iiuf.xmillum.FlagAccess;
26  import iiuf.xmillum.FlagListener;
27  
28  import java.awt.event.ActionEvent;
29  import java.awt.event.ActionListener;
30  import java.util.Iterator;
31  import java.util.Set;
32  import javax.swing.JPopupMenu;
33  
34  import org.w3c.dom.Element;
35  import org.w3c.dom.NodeList;
36  
37  /**
38   * PopupFlagger
39   *
40   * Shows a popup menu that can be used to set a flag on an element.
41   *
42   * <p>Initialization parameters:
43   * <ul>
44   *   <li>flag: Name of the flag to set
45   *   <li>allow-clear: "yes", "true" or "1" to Allow clearing the flag
46   *       (i.e. setting the "null" flag value)
47   * </ul>
48   *
49   * @author $Author: ohitz $
50   * @version $Revision: 1.1 $
51   */
52  public class PopupFlagger extends ActionHandler {
53  
54    FlagAccess access;
55    JPopupMenu menu;
56  
57    public void init(BrowserContext context, Element param) {
58      String    flagName = null;
59      boolean   allowClear = false;
60  
61      NodeList prefs = DOMUtils.getChildsByTagName(param, "param");
62      for (int i = 0; i < prefs.getLength(); i++) {
63        Element p = (Element) prefs.item(i);
64        String key   = p.getAttribute("name");
65        String value = p.getAttribute("value");
66        if (key.equals("flag")) {
67    flagName = value;
68        } else if (key.equals("allow-clear") && ("1".equals(value) || "true".equals(value) || "yes".equals(value))) {
69    allowClear = true;
70        } else {
71    context.log("Unknown initialization parameter `"+key+"' in "+PopupFlagger.class.getName());
72        }
73      }
74  
75      if (flagName == null) {
76        context.log("No flag name set in "+PopupFlagger.class.getName());
77        return;
78      }
79      access = context.flagger.addFlagListener(flagName, new FlagListener() {
80    public void setFlag(Element e, String v) {
81    }
82        });
83  
84      menu = new JPopupMenu();
85      if (allowClear) {
86        menu.add("<clear>").addActionListener(listener);
87      }
88      Set values = context.flagger.getFlagValues(flagName);
89      Iterator i = values.iterator();
90      while (i.hasNext()) {
91        String v = (String) i.next();
92        menu.add(v).addActionListener(listener);
93      }
94    }
95  
96    Element element;
97  
98    ActionListener listener = new ActionListener() {
99        public void actionPerformed(ActionEvent e) {
100   if ("<clear>".equals(e.getActionCommand())) {
101     access.setFlag(element, null);
102   } else {
103     access.setFlag(element, e.getActionCommand());
104   }
105       }
106     };
107 
108   public void handle(ActionHandlerParam param) {
109     if (menu == null) {
110       param.getContext().log("Popup menu not initialized in "+PopupFlagger.class.getName());
111       return;
112     }
113     element = param.getElement();
114     param.getContext().browserPanel.showPopup(menu);
115   }
116 }