• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2007 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); You may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
6 // applicable law or agreed to in writing, software distributed under the
7 // License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
8 // OF ANY KIND, either express or implied. See the License for the specific
9 // language governing permissions and limitations under the License.
10 
11 package com.google.scrollview.ui;
12 
13 import com.google.scrollview.events.SVEventType;
14 import com.google.scrollview.ui.SVMenuItem;
15 import com.google.scrollview.ui.SVWindow;
16 
17 import java.awt.Component;
18 import java.awt.event.ActionEvent;
19 import java.awt.event.ActionListener;
20 import java.util.HashMap;
21 
22 import javax.swing.JMenu;
23 import javax.swing.JPopupMenu;
24 
25 /**
26  * The SVPopupMenu class provides the functionality to add a popup menu to
27  * ScrollView. Each popup menu item gets associated with a (client-defined)
28  * command-id, which SVPopupMenu will return upon clicking it.
29  *
30  * @author wanke@google.com
31  *
32  */
33 
34 public class SVPopupMenu implements ActionListener {
35   /** The root entry to add items to. */
36   private JPopupMenu root;
37   /** Contains a map of item name to its actual entry. */
38   private HashMap<String, SVAbstractMenuItem> items;
39   /** The window the menubar belongs to. */
40   private SVWindow svWindow;
41 
42   /**
43    * Create a new SVPopupMenu and associate it with a ScrollView window.
44    *
45    * @param sv The window our popup menu belongs to.
46    */
SVPopupMenu(SVWindow sv)47   SVPopupMenu(SVWindow sv) {
48     root = new JPopupMenu();
49     svWindow = sv;
50     items = new HashMap<String, SVAbstractMenuItem>();
51   }
52 
53   /**
54    * Add a new entry to the menubar. For these items, the server will poll the
55    * client to ask what to do.
56    *
57    * @param parent The menu we add our new entry to (should have been defined
58    *        before). If the parent is "", we will add the entry to the root
59    *        (top-level)
60    * @param name The caption of the new entry.
61    * @param id The Id of the new entry. If it is -1, the entry will be treated
62    *        as a menu.
63    */
add(String parent, String name, int id)64   public void add(String parent, String name, int id) {
65     // A duplicate entry - we just throw it away, since its already in.
66     if (items.get(name) != null) { return; }
67     // A new submenu at the top-level
68     if (parent.equals("")) {
69       JMenu jli = new JMenu(name);
70       SVAbstractMenuItem mli = new SVSubMenuItem(name, jli);
71       items.put(name, mli);
72       root.add(jli);
73     }
74     // A new sub-submenu
75     else if (id == -1) {
76       SVAbstractMenuItem jmi = items.get(parent);
77       JMenu jli = new JMenu(name);
78       SVAbstractMenuItem mli = new SVSubMenuItem(name, jli);
79       items.put(name, mli);
80       jmi.add(jli);
81     }
82     // A new child entry. Add to appropriate parent.
83     else {
84       SVAbstractMenuItem jmi = items.get(parent);
85       if (jmi == null) {
86         System.out.println("ERROR: Unknown parent " + parent);
87         System.exit(1);
88       }
89       SVAbstractMenuItem mli = new SVEmptyMenuItem(id, name);
90       mli.mi.addActionListener(this);
91       items.put(name, mli);
92       jmi.add(mli);
93     }
94   }
95 
96   /**
97    * Add a new entry to the menubar. In this case, we also know its value and
98    * possibly even have a description. For these items, the server will not poll
99    * the client to ask what to do, but just show an input dialog and send a
100    * message with the new value.
101    *
102    * @param parent The menu we add our new entry to (should have been defined
103    *        before). If the parent is "", we will add the entry to the root
104    *        (top-level)
105    * @param name The caption of the new entry.
106    * @param id The Id of the new entry. If it is -1, the entry will be treated
107    *        as a menu.
108    */
add(String parent, String name, int id, String value, String desc)109   public void add(String parent, String name, int id, String value, String desc) {
110     SVAbstractMenuItem jmi = items.get(parent);
111     SVMenuItem mli = new SVMenuItem(id, name, value, desc);
112     mli.mi.addActionListener(this);
113     items.put(name, mli);
114     if (jmi == null) { // add to root
115       root.add(mli.mi);
116     } else { // add to parent
117       jmi.add(mli);
118     }
119   }
120 
121 
122 
123   /**
124    * A click on one of the items in our menubar has occured. Forward it
125    * to the item itself to let it decide what happens.
126    */
actionPerformed(ActionEvent e)127   public void actionPerformed(ActionEvent e) {
128 
129     // Get the corresponding menuitem
130     SVAbstractMenuItem svm = items.get(e.getActionCommand());
131 
132    svm.performAction(svWindow, SVEventType.SVET_POPUP);
133   }
134 
135   /**
136    * Gets called by the SVEventHandler of the window to actually show the
137    * content of the popup menu.
138    */
show(Component Invoker, int x, int y)139   public void show(Component Invoker, int x, int y) {
140     root.show(Invoker, x, y);
141   }
142 }
143