📄 annotatedstring.java
字号:
/* * FindBugs - Find bugs in Java programs * Copyright (C) 2004 Rohan Lloyd <findbugs@rohanl.com> * Copyright (C) 2003,2004 University of Maryland * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//* * AnnotatedString.java * * Created on September 14, 2004 */package edu.umd.cs.findbugs.gui;import java.util.regex.Pattern;import java.awt.event.KeyEvent;import javax.swing.*;import java.awt.FlowLayout;/** * Class to handle Strings annotated with embedded mnemonics * * Note: Since the human interface guidelines for Mac OS X say never * to use mnemonics, this class behaves as if no mnemonics are set * when run on Mac OS X. */public class AnnotatedString { private static final boolean MAC_OS_X = System.getProperty("os.name").toLowerCase().startsWith("mac os x"); private final String myAnnotatedString; public AnnotatedString(String s) { myAnnotatedString = s; } /* * Return the string minus any annotation */ public String toString() { return myAnnotatedString.replaceFirst("&", ""); } /** * Return the appropriate mnemonic character for this string. If no mnemonic * should be displayed, KeyEvent.VK_UNDEFINED is returned. * * @return the Mnemonic character, or VK_UNDEFINED if no mnemonic should * be set */ public int getMnemonic() { int mnemonic = KeyEvent.VK_UNDEFINED; if (!MAC_OS_X) { int index = getMnemonicIndex(); if ((index >= 0) && ((index + 1) < myAnnotatedString.length())) { mnemonic = Character.toUpperCase(myAnnotatedString.charAt(index + 1)); } } return mnemonic; } /** * @return the index in the plain string at which the mnemonic should be * displayed, or -1 if no mnemonic should be set */ public int getMnemonicIndex() { int index = -1; if (!MAC_OS_X) { index = myAnnotatedString.indexOf('&'); if (index + 1 >= myAnnotatedString.length()) { index = -1; } } return index; } /* * Some test code */ private static void addButton(JFrame frame, String s) { AnnotatedString as = new AnnotatedString(s); JButton button = new JButton(as.toString()); button.setMnemonic(as.getMnemonic()); button.setDisplayedMnemonicIndex(as.getMnemonicIndex()); frame.getContentPane().add(button); System.out.println("\"" + s + "\" \"" + as + "\" '" + as.getMnemonic() + "' " + as.getMnemonicIndex()); } public static void main(String[] args) { // Some basic tests JFrame frame = new JFrame(); frame.getContentPane().setLayout(new FlowLayout()); JButton button; addButton(frame, "&File"); addButton(frame, "S&ave As..."); addButton(frame, "Save &As..."); addButton(frame, "Fo&o"); addButton(frame, "Foo"); // Error cases - make sure we handle them sensibly addButton(frame, ""); addButton(frame, "&"); addButton(frame, "Foo&"); addButton(frame, "Cat & Dog"); addButton(frame, "Cat && Dog"); frame.pack(); frame.show(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -