001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.swing;
019:
020: import java.awt.Component;
021: import java.awt.Graphics;
022: import java.awt.Insets;
023: import java.awt.event.KeyEvent;
024: import java.awt.event.MouseEvent;
025: import javax.accessibility.Accessible;
026: import javax.accessibility.AccessibleContext;
027: import javax.accessibility.AccessibleRole;
028: import javax.accessibility.AccessibleSelection;
029: import javax.accessibility.AccessibleStateSet;
030: import javax.swing.plaf.MenuBarUI;
031: import org.apache.harmony.luni.util.NotImplementedException;
032: import org.apache.harmony.x.swing.StringConstants;
033: import org.apache.harmony.x.swing.Utilities;
034:
035: import org.apache.harmony.x.swing.internal.nls.Messages;
036:
037: /**
038: * <p>
039: * <i>JMenuBar</i>
040: * </p>
041: * <h3>Implementation Notes:</h3>
042: * <ul>
043: * <li>The <code>serialVersionUID</code> fields are explicitly declared as a performance
044: * optimization, not as a guarantee of serialization compatibility.</li>
045: * </ul>
046: */
047: public class JMenuBar extends JComponent implements Accessible,
048: MenuElement {
049: private static final long serialVersionUID = 6620404810314292434L;
050:
051: // TODO: implement
052: protected class AccessibleJMenuBar extends AccessibleJComponent
053: implements AccessibleSelection {
054: private static final long serialVersionUID = 1L;
055:
056: public void addAccessibleSelection(int i)
057: throws NotImplementedException {
058: throw new NotImplementedException();
059: }
060:
061: public void clearAccessibleSelection()
062: throws NotImplementedException {
063: throw new NotImplementedException();
064: }
065:
066: public Accessible getAccessibleSelection(int i)
067: throws NotImplementedException {
068: throw new NotImplementedException();
069: }
070:
071: public int getAccessibleSelectionCount()
072: throws NotImplementedException {
073: throw new NotImplementedException();
074: }
075:
076: @Override
077: public AccessibleRole getAccessibleRole() {
078: return AccessibleRole.MENU_BAR;
079: }
080:
081: @Override
082: public AccessibleSelection getAccessibleSelection()
083: throws NotImplementedException {
084: throw new NotImplementedException();
085: }
086:
087: @Override
088: public AccessibleStateSet getAccessibleStateSet()
089: throws NotImplementedException {
090: throw new NotImplementedException();
091: }
092:
093: public boolean isAccessibleChildSelected(int i)
094: throws NotImplementedException {
095: throw new NotImplementedException();
096: }
097:
098: public void removeAccessibleSelection(int i)
099: throws NotImplementedException {
100: throw new NotImplementedException();
101: }
102:
103: public void selectAllAccessibleSelection()
104: throws NotImplementedException {
105: throw new NotImplementedException();
106: }
107: }
108:
109: private final static String UI_CLASS_ID = "MenuBarUI";
110:
111: private SingleSelectionModel selectionModel = new DefaultSingleSelectionModel();
112:
113: private boolean borderPainted = true;
114:
115: private Insets margin;
116:
117: public JMenuBar() {
118: updateUI();
119: }
120:
121: public JMenu add(JMenu menu) {
122: super .add(menu);
123: return menu;
124: }
125:
126: @Override
127: public AccessibleContext getAccessibleContext() {
128: return (accessibleContext == null) ? (accessibleContext = new AccessibleJMenuBar())
129: : accessibleContext;
130: }
131:
132: public JMenu getMenu(int index) {
133: Component c = getComponent(index);
134: return (c instanceof JMenu) ? (JMenu) c : null;
135: }
136:
137: public int getMenuCount() {
138: return getComponentCount();
139: }
140:
141: @Deprecated
142: public Component getComponentAtIndex(int index) {
143: return getComponent(index);
144: }
145:
146: public int getComponentIndex(Component c) {
147: for (int i = 0; i < getComponentCount(); i++) {
148: if (getComponent(i) == c) {
149: return i;
150: }
151: }
152: return -1;
153: }
154:
155: public MenuElement[] getSubElements() {
156: return Utilities.getSubElements(this );
157: }
158:
159: public void setSelectionModel(SingleSelectionModel model) {
160: SingleSelectionModel oldValue = selectionModel;
161: selectionModel = model;
162: firePropertyChange(StringConstants.SELECTION_MODEL_PROPERTY,
163: oldValue, selectionModel);
164: }
165:
166: public SingleSelectionModel getSelectionModel() {
167: return selectionModel;
168: }
169:
170: public void setSelected(Component selection) {
171: if (selectionModel != null) {
172: selectionModel
173: .setSelectedIndex(getComponentIndex(selection));
174: }
175: }
176:
177: public boolean isSelected() {
178: return (selectionModel != null) ? selectionModel.isSelected()
179: : false;
180: }
181:
182: public void setBorderPainted(boolean painted) {
183: boolean oldValue = borderPainted;
184: borderPainted = painted;
185: firePropertyChange(
186: AbstractButton.BORDER_PAINTED_CHANGED_PROPERTY,
187: oldValue, borderPainted);
188: }
189:
190: public boolean isBorderPainted() {
191: return borderPainted;
192: }
193:
194: @Override
195: protected void paintBorder(Graphics g) {
196: if (isBorderPainted()) {
197: super .paintBorder(g);
198: }
199: }
200:
201: @Override
202: protected boolean processKeyBinding(KeyStroke ks, KeyEvent event,
203: int condition, boolean pressed) {
204: MenuSelectionManager.defaultManager().processKeyEvent(event);
205: if ((event != null) && event.isConsumed()) {
206: return true;
207: }
208: if (super .processKeyBinding(ks, event, condition, pressed)) {
209: return true;
210: }
211: return SwingUtilities.processKeyEventOnChildren(this , event);
212: }
213:
214: public void processKeyEvent(KeyEvent e, MenuElement[] path,
215: MenuSelectionManager manager) {
216: }
217:
218: public void processMouseEvent(MouseEvent event, MenuElement[] path,
219: MenuSelectionManager manager) {
220: }
221:
222: public void menuSelectionChanged(boolean isIncluded) {
223: }
224:
225: public Component getComponent() {
226: return this ;
227: }
228:
229: public void setHelpMenu(JMenu menu) {
230: throw new Error(Messages.getString(
231: "swing.err.0C", "setHelpMenu()")); //$NON-NLS-1$ //$NON-NLS-2$
232: }
233:
234: public JMenu getHelpMenu() {
235: throw new Error(Messages.getString(
236: "swing.err.0C", "getHelpMenu()")); //$NON-NLS-1$ //$NON-NLS-2$
237: }
238:
239: public void setMargin(Insets margin) {
240: Insets oldValue = this .margin;
241: this .margin = margin;
242: firePropertyChange(AbstractButton.MARGIN_CHANGED_PROPERTY,
243: oldValue, margin);
244: }
245:
246: public Insets getMargin() {
247: return margin;
248: }
249:
250: @Override
251: public String getUIClassID() {
252: return UI_CLASS_ID;
253: }
254:
255: public MenuBarUI getUI() {
256: return (MenuBarUI) ui;
257: }
258:
259: public void setUI(MenuBarUI ui) {
260: super .setUI(ui);
261: }
262:
263: @Override
264: public void updateUI() {
265: setUI((MenuBarUI) UIManager.getUI(this));
266: }
267: }
|