All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SVMenuBar.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 
15 
16 import java.awt.event.ActionEvent;
17 import java.awt.event.ActionListener;
18 import java.util.HashMap;
19 
20 import javax.swing.JMenu;
21 import javax.swing.JMenuBar;
22 
31 public class SVMenuBar implements ActionListener {
33  private JMenuBar root;
35  private HashMap<String, SVAbstractMenuItem> items;
37  private SVWindow svWindow;
38 
44  public SVMenuBar(SVWindow scrollView) {
45  root = new JMenuBar();
46  svWindow = scrollView;
47  items = new HashMap<String, SVAbstractMenuItem>();
48  svWindow.setJMenuBar(root);
49  }
50 
51 
56  public void actionPerformed(ActionEvent e) {
57  // Get the corresponding menuitem.
58  SVAbstractMenuItem svm = items.get(e.getActionCommand());
59 
60  svm.performAction(svWindow, SVEventType.SVET_MENU);
61  }
62 
73  public void add(String parent, String name, int id) {
74  // A duplicate entry - we just throw it away, since its already in.
75  if (items.get(name) != null) { return; }
76  // A new submenu at the top-level
77  if (parent.equals("")) {
78  JMenu jli = new JMenu(name);
79  SVAbstractMenuItem mli = new SVSubMenuItem(name, jli);
80  items.put(name, mli);
81  root.add(jli);
82  }
83  // A new sub-submenu
84  else if (id == -1) {
85  SVAbstractMenuItem jmi = items.get(parent);
86  JMenu jli = new JMenu(name);
87  SVAbstractMenuItem mli = new SVSubMenuItem(name, jli);
88  items.put(name, mli);
89  jmi.add(jli);
90  }
91  // A new child entry. Add to appropriate parent.
92  else {
93  SVAbstractMenuItem jmi = items.get(parent);
94  if (jmi == null) {
95  System.out.println("ERROR: Unknown parent " + parent);
96  System.exit(1);
97  }
98  SVAbstractMenuItem mli = new SVEmptyMenuItem(id, name);
99  mli.mi.addActionListener(this);
100  items.put(name, mli);
101  jmi.add(mli);
102  }
103  }
104 
118  public void add(String parent, String name, int id, boolean b) {
119  SVAbstractMenuItem jmi = items.get(parent);
120  if (jmi == null) {
121  System.out.println("ERROR: Unknown parent " + parent);
122  System.exit(1);
123  }
124  SVAbstractMenuItem mli = new SVCheckboxMenuItem(id, name, b);
125  mli.mi.addActionListener(this);
126  items.put(name, mli);
127  jmi.add(mli);
128  }
129 
130 }
name_table name
SVMenuBar(SVWindow scrollView)
Definition: SVMenuBar.java:44
void add(String parent, String name, int id, boolean b)
Definition: SVMenuBar.java:118
void actionPerformed(ActionEvent e)
Definition: SVMenuBar.java:56
void add(String parent, String name, int id)
Definition: SVMenuBar.java:73