001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.options.keymap;
043:
044: import java.util.Map;
045: import org.netbeans.core.options.keymap.api.ShortcutAction;
046:
047: /**
048: *
049: * @author Jan Jancura, David Strupl
050: */
051: public class CompoundAction implements ShortcutAction {
052: private static final String DEFAULT_PROVIDER = "EditorBridge";
053: private Map<String, ShortcutAction> actions;
054:
055: public CompoundAction(Map<String, ShortcutAction> actions) {
056: this .actions = actions;
057: }
058:
059: public String getDisplayName() {
060: ShortcutAction s = actions.get(DEFAULT_PROVIDER);
061: if (s != null) {
062: return s.getDisplayName();
063: }
064: for (ShortcutAction sa : actions.values()) {
065: String dn = sa.getDisplayName();
066: if (dn != null) {
067: return dn;
068: }
069: }
070: return "<error>"; // TODO:
071: }
072:
073: public String getId() {
074: ShortcutAction s = actions.get(DEFAULT_PROVIDER);
075: if (s != null) {
076: return s.getId();
077: }
078: for (ShortcutAction sa : actions.values()) {
079: String id = sa.getId();
080: if (id != null) {
081: return id;
082: }
083: }
084: return "<error>"; // TODO:
085: }
086:
087: public String getDelegatingActionId() {
088: ShortcutAction s = actions.get(DEFAULT_PROVIDER);
089: if (s != null) {
090: return s.getDelegatingActionId();
091: }
092: for (ShortcutAction sa : actions.values()) {
093: String id = sa.getDelegatingActionId();
094: if (id != null) {
095: return id;
096: }
097: }
098: return null; // TODO:
099: }
100:
101: public boolean equals(Object o) {
102: if (!(o instanceof CompoundAction)) {
103: return false;
104: }
105: if (actions.get(DEFAULT_PROVIDER) != null) {
106: return (getKeymapManagerInstance(DEFAULT_PROVIDER)
107: .equals(((CompoundAction) o)
108: .getKeymapManagerInstance(DEFAULT_PROVIDER)));
109: }
110: if (actions.keySet().isEmpty()) {
111: return false;
112: }
113: String k = actions.keySet().iterator().next();
114: return (getKeymapManagerInstance(k).equals(((CompoundAction) o)
115: .getKeymapManagerInstance(k)));
116: }
117:
118: public int hashCode() {
119: if (actions.get(DEFAULT_PROVIDER) != null) {
120: return getKeymapManagerInstance(DEFAULT_PROVIDER)
121: .hashCode() * 2;
122: }
123: if (actions.keySet().isEmpty()) {
124: return 0;
125: }
126: String k = actions.keySet().iterator().next();
127: return actions.get(k).hashCode() * 2;
128: }
129:
130: public ShortcutAction getKeymapManagerInstance(
131: String keymapManagerName) {
132: return actions.get(keymapManagerName);
133: }
134:
135: public String toString() {
136: return "CompoundAction[" + actions + "]";
137: }
138: }
|