All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SVPopupMenu.java
Go to the documentation of this file.
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 
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 
34 public class SVPopupMenu implements ActionListener {
36  private JPopupMenu root;
38  private HashMap<String, SVAbstractMenuItem> items;
40  private SVWindow svWindow;
41 
47  SVPopupMenu(SVWindow sv) {
48  root = new JPopupMenu();
49  svWindow = sv;
50  items = new HashMap<String, SVAbstractMenuItem>();
51  }
52 
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 
111  public void add(String parent, String name, int id, String value, String desc) {
112  SVAbstractMenuItem jmi = items.get(parent);
113  SVMenuItem mli = new SVMenuItem(id, name, value, desc);
114  mli.mi.addActionListener(this);
115  items.put(name, mli);
116  if (jmi == null) { // add to root
117  root.add(mli.mi);
118  } else { // add to parent
119  jmi.add(mli);
120  }
121  }
122 
123 
124 
129  public void actionPerformed(ActionEvent e) {
130 
131  // Get the corresponding menuitem
132  SVAbstractMenuItem svm = items.get(e.getActionCommand());
133 
134  svm.performAction(svWindow, SVEventType.SVET_POPUP);
135  }
136 
141  public void show(Component Invoker, int x, int y) {
142  root.show(Invoker, x, y);
143  }
144 }
void add(String parent, String name, int id)
name_table name
void show(Component Invoker, int x, int y)
void add(String parent, String name, int id, String value, String desc)