KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > web > ToolBar


1 /*
2   Copyright (C) 2001-2003 Laurent Martelli <laurent@aopsys.com>
3   
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.gui.web;
19
20 import org.objectweb.jac.aspects.gui.*;
21 import java.io.PrintWriter JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 public class ToolBar extends AbstractView implements MenuView, HTMLViewer, MenuListener {
27
28     // key -> [ callback | Menu | null ]
29
HashMap JavaDoc map = new HashMap JavaDoc();
30     // item order
31
Vector JavaDoc keys = new Vector JavaDoc();
32
33     public ToolBar(ViewFactory factory, DisplayContext context) {
34         super(factory,context);
35     }
36
37     // MenuView interface
38

39     public void addSubMenu(String JavaDoc label, String JavaDoc icon, MenuView submenu) {
40     }
41
42     public void addAction(String JavaDoc label, String JavaDoc icon, Callback callback) {
43         if (!map.containsKey(label)) {
44             keys.add(label);
45             map.put(label,new MenuItem(label,icon,callback));
46         }
47     }
48
49     public void addSeparator() {
50     }
51
52     String JavaDoc position;
53    
54     /**
55      * Get the value of position.
56      * @return value of position.
57      */

58     public String JavaDoc getPosition() {
59         return position;
60     }
61    
62     /**
63      * Set the value of position.
64      * @param v Value to assign to position.
65      */

66     public void setPosition(String JavaDoc v) {
67         this.position = v;
68     }
69    
70     // HTMLViewer interface
71

72     public void genHTML(PrintWriter JavaDoc out) {
73         out.println("<div class=\"toolBar\">");
74         Iterator JavaDoc i = keys.iterator();
75         while (i.hasNext()) {
76             String JavaDoc key = (String JavaDoc)i.next();
77             Object JavaDoc item = map.get(key);
78             if (item instanceof MenuItem) {
79                 out.print("<a HREF=\""+eventURL("onMenuClick")+
80                           "&amp;item="+key+"\">");
81                 if (((MenuItem)item).icon!=null)
82                     out.print(iconElement(((MenuItem)item).icon,""));
83                 else
84                     out.println(key);
85                 out.println("</a>");
86             }
87         }
88         out.println("</div>");
89     }
90
91     // MenuListener interface
92

93     public void onMenuClick(String JavaDoc key) {
94         try {
95             MenuItem item = (MenuItem)map.get(key);
96             if (item.callback!=null)
97                 EventHandler.get().onInvoke(
98                     context,
99                     new InvokeEvent(this,null,item.callback.getMethod()));
100             else
101                 context.getDisplay().refresh();
102         } catch (Exception JavaDoc e) {
103             context.getDisplay().showError("Menu error",e.toString());
104         }
105     }
106 }
107
Popular Tags