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: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.lib.uihandler;
029:
030: import java.text.MessageFormat;
031: import java.util.MissingResourceException;
032: import java.util.logging.Level;
033: import java.util.logging.LogRecord;
034: import java.util.logging.Logger;
035: import java.util.logging.SimpleFormatter;
036: import javax.swing.JButton;
037: import org.openide.util.NbBundle;
038:
039: /**
040: *
041: * @author Jaroslav Tulach
042: */
043: final class Decorations {
044:
045: private static final SimpleFormatter FORMATTER = new SimpleFormatter();
046:
047: public static void decorate(LogRecord r, Decorable d) {
048: if (r.getMessage() == null) {
049: d.setName("Seq: " + r.getSequenceNumber());
050: } else {
051: d.setName(r.getMessage());
052: }
053: if (r.getResourceBundle() != null) {
054: try {
055: String msg = r.getResourceBundle().getString(
056: r.getMessage());
057: if (r.getParameters() != null) {
058: msg = MessageFormat.format(msg, r.getParameters());
059: }
060: d.setDisplayName(msg);
061: } catch (MissingResourceException ex) {
062: Logger.getAnonymousLogger().log(Level.INFO, null, ex);
063: }
064:
065: try {
066: String iconBase = r.getResourceBundle().getString(
067: r.getMessage() + "_ICON_BASE"); // NOI18N
068: d.setIconBaseWithExtension(iconBase);
069: } catch (MissingResourceException ex) {
070: // ok, use default
071: d
072: .setIconBaseWithExtension("org/netbeans/lib/uihandler/def.png");
073: }
074: }
075:
076: String htmlKey;
077: if (r.getThrown() != null) {
078: d
079: .setIconBaseWithExtension("org/netbeans/lib/uihandler/exception.gif");
080: htmlKey = "HTML_exception";
081: }
082:
083: if ("UI_ACTION_BUTTON_PRESS".equals(r.getMessage())) { // NOI18N
084: d.setDisplayName(cutAmpersand(getParam(r, 4)));
085: String thru = getParam(r, 1, String.class);
086: if ((thru != null && thru.contains("Toolbar"))
087: || getParam(r, 0, Object.class) instanceof JButton) {
088: d
089: .setIconBaseWithExtension("org/netbeans/lib/uihandler/toolbars.gif");
090: htmlKey = "HTML_toolbar";
091: } else if (thru != null && thru.contains("MenuItem")) {
092: d
093: .setIconBaseWithExtension("org/netbeans/lib/uihandler/menus.gif");
094: htmlKey = "HTML_menu";
095: }
096: } else if ("UI_ACTION_KEY_PRESS".equals(r.getMessage())) { // NOI18N
097: d.setDisplayName(cutAmpersand(getParam(r, 4)));
098: d
099: .setIconBaseWithExtension("org/netbeans/lib/uihandler/key.png");
100: htmlKey = "HTML_key";
101: } else if ("UI_ACTION_EDITOR".equals(r.getMessage())) { // NOI18N
102: d.setDisplayName(cutAmpersand(getParam(r, 4)));
103: d
104: .setIconBaseWithExtension("org/netbeans/lib/uihandler/key.png");
105: htmlKey = "HTML_key";
106: } else if ("UI_ENABLED_MODULES".equals(r.getMessage())) { // NOI18N
107: d.setDisplayName(NbBundle.getMessage(Decorations.class,
108: "MSG_EnabledModules"));
109: d
110: .setIconBaseWithExtension("org/netbeans/lib/uihandler/module.gif");
111: htmlKey = null;
112: } else if ("UI_DISABLED_MODULES".equals(r.getMessage())) { // NOI18N
113: d.setDisplayName(NbBundle.getMessage(Decorations.class,
114: "MSG_DisabledModules"));
115: d
116: .setIconBaseWithExtension("org/netbeans/lib/uihandler/module.gif");
117: htmlKey = null;
118: } else if ("UI_USER_CONFIGURATION".equals(r.getMessage())) {// NOI18N
119: d.setDisplayName(NbBundle.getMessage(Decorations.class,
120: "MSG_USER_CONFIGURATION"));
121: htmlKey = null;
122: }
123:
124: d.setShortDescription(FORMATTER.format(r));
125:
126: }
127:
128: private static <T> T getParam(LogRecord r, int index, Class<T> type) {
129: if (r == null || r.getParameters() == null
130: || r.getParameters().length <= index) {
131: return null;
132: }
133: Object o = r.getParameters()[index];
134: return type.isInstance(o) ? type.cast(o) : null;
135: }
136:
137: private static String getParam(LogRecord r, int index) {
138: Object[] arr = r.getParameters();
139: if (arr == null || arr.length <= index
140: || !(arr[index] instanceof String)) {
141: return "";
142: }
143: return (String) arr[index];
144: }
145:
146: static String cutAmpersand(String text) {
147: // XXX should this also be deprecated by something in Mnemonics?
148: int i;
149: String result = text;
150:
151: /* First check of occurence of '(&'. If not found check
152: * for '&' itself.
153: * If '(&' is found then remove '(&??'.
154: */
155: i = text.indexOf("(&"); // NOI18N
156:
157: if ((i >= 0) && ((i + 3) < text.length()) && /* #31093 */
158: (text.charAt(i + 3) == ')')) { // NOI18N
159: result = text.substring(0, i) + text.substring(i + 4);
160: } else {
161: //Sequence '(&?)' not found look for '&' itself
162: i = text.indexOf('&');
163:
164: if (i < 0) {
165: //No ampersand
166: result = text;
167: } else if (i == (text.length() - 1)) {
168: //Ampersand is last character, wrong shortcut but we remove it anyway
169: result = text.substring(0, i);
170: } else {
171: //Remove ampersand from middle of string
172: //Is ampersand followed by space? If yes do not remove it.
173: if (" ".equals(text.substring(i + 1, i + 2))) {
174: result = text;
175: } else {
176: result = text.substring(0, i)
177: + text.substring(i + 1);
178: }
179: }
180: }
181:
182: return result;
183: }
184: }
|