diff --git a/src/java/org/jivesoftware/spark/util/GraphicUtils.java b/src/java/org/jivesoftware/spark/util/GraphicUtils.java index 99c909a4..47715c7c 100644 --- a/src/java/org/jivesoftware/spark/util/GraphicUtils.java +++ b/src/java/org/jivesoftware/spark/util/GraphicUtils.java @@ -46,10 +46,13 @@ import java.io.File; import java.io.IOException; import java.net.URL; import java.util.HashMap; +import java.util.Iterator; import java.util.Map; import java.util.StringTokenizer; import javax.imageio.ImageIO; +import javax.imageio.ImageReader; +import javax.imageio.stream.ImageInputStream; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JComponent; @@ -61,20 +64,21 @@ import org.jivesoftware.resource.SparkRes; import org.jivesoftware.spark.util.log.Log; /** - * GraphicsUtils class defines common user-interface related utility - * functions. + * GraphicsUtils class defines common user-interface related + * utility functions. */ public final class GraphicUtils { private static final Insets HIGHLIGHT_INSETS = new Insets(1, 1, 1, 1); - public static final Color SELECTION_COLOR = new java.awt.Color(166, 202, 240); + public static final Color SELECTION_COLOR = new java.awt.Color(166, 202, + 240); public static final Color TOOLTIP_COLOR = new java.awt.Color(166, 202, 240); protected final static Component component = new Component() { - private static final long serialVersionUID = -7556405112141454291L; + private static final long serialVersionUID = -7556405112141454291L; }; protected final static MediaTracker tracker = new MediaTracker(component); - private static Map imageCache = new HashMap(); + private static Map imageCache = new HashMap(); /** * The default Hand cursor. @@ -84,595 +88,771 @@ public final class GraphicUtils { /** * The default Text Cursor. */ - public static final Cursor DEFAULT_CURSOR = new Cursor(Cursor.DEFAULT_CURSOR); + public static final Cursor DEFAULT_CURSOR = new Cursor( + Cursor.DEFAULT_CURSOR); private GraphicUtils() { } - /** - * Sets the location of the specified window so that it is centered on screen. - * - * @param window The window to be centered. + * Sets the location of the specified window so that it is centered on + * screen. + * + * @param window + * The window to be centered. */ public static void centerWindowOnScreen(Window window) { - final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); - final Dimension size = window.getSize(); + final Dimension screenSize = Toolkit.getDefaultToolkit() + .getScreenSize(); + final Dimension size = window.getSize(); - if (size.height > screenSize.height) { - size.height = screenSize.height; - } + if (size.height > screenSize.height) { + size.height = screenSize.height; + } - if (size.width > screenSize.width) { - size.width = screenSize.width; - } + if (size.width > screenSize.width) { + size.width = screenSize.width; + } - window.setLocation((screenSize.width - size.width) / 2, - (screenSize.height - size.height) / 2); + window.setLocation((screenSize.width - size.width) / 2, + (screenSize.height - size.height) / 2); } /** * Draws a single-line highlight border rectangle. - * - * @param g The graphics context to use for drawing. - * @param x The left edge of the border. - * @param y The top edge of the border. - * @param width The width of the border. - * @param height The height of the border. - * @param raised true if the border is to be drawn raised, - * false if lowered. - * @param shadow The shadow color for the border. - * @param highlight The highlight color for the border. + * + * @param g + * The graphics context to use for drawing. + * @param x + * The left edge of the border. + * @param y + * The top edge of the border. + * @param width + * The width of the border. + * @param height + * The height of the border. + * @param raised + * true if the border is to be drawn raised, + * false if lowered. + * @param shadow + * The shadow color for the border. + * @param highlight + * The highlight color for the border. * @see javax.swing.border.EtchedBorder */ - public static void drawHighlightBorder(Graphics g, int x, int y, - int width, int height, boolean raised, - Color shadow, Color highlight) { - final Color oldColor = g.getColor(); - g.translate(x, y); + public static void drawHighlightBorder(Graphics g, int x, int y, int width, + int height, boolean raised, Color shadow, Color highlight) { + final Color oldColor = g.getColor(); + g.translate(x, y); - g.setColor(raised ? highlight : shadow); - g.drawLine(0, 0, width - 2, 0); - g.drawLine(0, 1, 0, height - 2); + g.setColor(raised ? highlight : shadow); + g.drawLine(0, 0, width - 2, 0); + g.drawLine(0, 1, 0, height - 2); - g.setColor(raised ? shadow : highlight); - g.drawLine(width - 1, 0, width - 1, height - 1); - g.drawLine(0, height - 1, width - 2, height - 1); + g.setColor(raised ? shadow : highlight); + g.drawLine(width - 1, 0, width - 1, height - 1); + g.drawLine(0, height - 1, width - 2, height - 1); - g.translate(-x, -y); - g.setColor(oldColor); + g.translate(-x, -y); + g.setColor(oldColor); } /** * Return the amount of space taken up by a highlight border drawn by * drawHighlightBorder(). - * + * * @return The Insets needed for the highlight border. * @see #drawHighlightBorder */ public static Insets getHighlightBorderInsets() { - return HIGHLIGHT_INSETS; - } - - public static ImageIcon createImageIcon(Image image) { - if (image == null) { - return null; - } - - synchronized (tracker) { - tracker.addImage(image, 0); - try { - tracker.waitForID(0, 0); - } - catch (InterruptedException e) { - System.out.println("INTERRUPTED while loading Image"); - } - tracker.removeImage(image, 0); - } - - return new ImageIcon(image); + return HIGHLIGHT_INSETS; } /** - * Returns a point where the given popup menu should be shown. The - * point is calculated by adjusting the X and Y coordinates from the - * given mouse event so that the popup menu will not be clipped by - * the screen boundaries. - * - * @param popup the popup menu - * @param event the mouse event + * Creates an ImageIcon from an Image + * + * @param image + * {@link Image} + * @return {@link ImageIcon} + */ + public static ImageIcon createImageIcon(Image image) { + if (image == null) { + return null; + } + + synchronized (tracker) { + tracker.addImage(image, 0); + try { + tracker.waitForID(0, 0); + } catch (InterruptedException e) { + System.out.println("INTERRUPTED while loading Image"); + } + tracker.removeImage(image, 0); + } + + return new ImageIcon(image); + } + + /** + * Returns a point where the given popup menu should be shown. The point is + * calculated by adjusting the X and Y coordinates from the given mouse + * event so that the popup menu will not be clipped by the screen + * boundaries. + * + * @param popup + * the popup menu + * @param event + * the mouse event * @return the point where the popup menu should be shown */ public static Point getPopupMenuShowPoint(JPopupMenu popup, MouseEvent event) { - Component source = (Component)event.getSource(); - Point topLeftSource = source.getLocationOnScreen(); - Point ptRet = getPopupMenuShowPoint(popup, - topLeftSource.x + event.getX(), - topLeftSource.y + event.getY()); - ptRet.translate(-topLeftSource.x, -topLeftSource.y); - return ptRet; + Component source = (Component) event.getSource(); + Point topLeftSource = source.getLocationOnScreen(); + Point ptRet = getPopupMenuShowPoint(popup, + topLeftSource.x + event.getX(), topLeftSource.y + event.getY()); + ptRet.translate(-topLeftSource.x, -topLeftSource.y); + return ptRet; } /** - * Returns a point where the given popup menu should be shown. The - * point is calculated by adjusting the X and Y coordinates so that - * the popup menu will not be clipped by the screen boundaries. - * - * @param popup the popup menu - * @param x the x position in screen coordinate - * @param y the y position in screen coordinates + * Returns a point where the given popup menu should be shown. The point is + * calculated by adjusting the X and Y coordinates so that the popup menu + * will not be clipped by the screen boundaries. + * + * @param popup + * the popup menu + * @param x + * the x position in screen coordinate + * @param y + * the y position in screen coordinates * @return the point where the popup menu should be shown in screen * coordinates */ public static Point getPopupMenuShowPoint(JPopupMenu popup, int x, int y) { - Dimension sizeMenu = popup.getPreferredSize(); - Point bottomRightMenu = new Point(x + sizeMenu.width, y + sizeMenu.height); + Dimension sizeMenu = popup.getPreferredSize(); + Point bottomRightMenu = new Point(x + sizeMenu.width, y + + sizeMenu.height); - Rectangle[] screensBounds = getScreenBounds(); - int n = screensBounds.length; - for (int i = 0; i < n; i++) { - Rectangle screenBounds = screensBounds[i]; - if (screenBounds.x <= x && x <= (screenBounds.x + screenBounds.width)) { - Dimension sizeScreen = screenBounds.getSize(); - sizeScreen.height -= 32; // Hack to help prevent menu being clipped by Windows/Linux/Solaris Taskbar. + Rectangle[] screensBounds = getScreenBounds(); + int n = screensBounds.length; + for (int i = 0; i < n; i++) { + Rectangle screenBounds = screensBounds[i]; + if (screenBounds.x <= x + && x <= (screenBounds.x + screenBounds.width)) { + Dimension sizeScreen = screenBounds.getSize(); + sizeScreen.height -= 32; // Hack to help prevent menu being + // clipped by Windows/Linux/Solaris + // Taskbar. - int xOffset = 0; - if (bottomRightMenu.x > (screenBounds.x + sizeScreen.width)) - xOffset = -sizeMenu.width; + int xOffset = 0; + if (bottomRightMenu.x > (screenBounds.x + sizeScreen.width)) + xOffset = -sizeMenu.width; - int yOffset = 0; - if (bottomRightMenu.y > (screenBounds.y + sizeScreen.height)) - yOffset = sizeScreen.height - bottomRightMenu.y; + int yOffset = 0; + if (bottomRightMenu.y > (screenBounds.y + sizeScreen.height)) + yOffset = sizeScreen.height - bottomRightMenu.y; - return new Point(x + xOffset, y + yOffset); - } - } + return new Point(x + xOffset, y + yOffset); + } + } - return new Point(x, y); // ? that would mean that the top left point was not on any screen. + return new Point(x, y); // ? that would mean that the top left point was + // not on any screen. } /** - * Centers the window over a component (usually another window). - * The window must already have been sized. - * @param window Window to center. - * @param over Component to center over. + * Centers the window over a component (usually another window). The window + * must already have been sized. + * + * @param window + * Window to center. + * @param over + * Component to center over. */ public static void centerWindowOnComponent(Window window, Component over) { - if ((over == null) || !over.isShowing()) { - centerWindowOnScreen(window); - return; - } + if ((over == null) || !over.isShowing()) { + centerWindowOnScreen(window); + return; + } + Point parentLocation = over.getLocationOnScreen(); + Dimension parentSize = over.getSize(); + Dimension size = window.getSize(); - Point parentLocation = over.getLocationOnScreen(); - Dimension parentSize = over.getSize(); - Dimension size = window.getSize(); + // Center it. + int x = parentLocation.x + (parentSize.width - size.width) / 2; + int y = parentLocation.y + (parentSize.height - size.height) / 2; - // Center it. - int x = parentLocation.x + (parentSize.width - size.width) / 2; - int y = parentLocation.y + (parentSize.height - size.height) / 2; + // Now, make sure it's onscreen + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); - // Now, make sure it's onscreen - Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + // This doesn't actually work on the Mac, where the screen + // doesn't necessarily start at 0,0 + if (x + size.width > screenSize.width) + x = screenSize.width - size.width; - // This doesn't actually work on the Mac, where the screen - // doesn't necessarily start at 0,0 - if (x + size.width > screenSize.width) - x = screenSize.width - size.width; + if (x < 0) + x = 0; - if (x < 0) - x = 0; + if (y + size.height > screenSize.height) + y = screenSize.height - size.height; - if (y + size.height > screenSize.height) - y = screenSize.height - size.height; + if (y < 0) + y = 0; - if (y < 0) - y = 0; - - window.setLocation(x, y); + window.setLocation(x, y); } /** - * @param c Component to check on. + * @param c + * Component to check on. * @return returns true if the component of one of its child has the focus */ public static boolean isAncestorOfFocusedComponent(Component c) { - if (c.hasFocus()) { - return true; - } - else { - if (c instanceof Container) { - Container cont = (Container)c; - int n = cont.getComponentCount(); - for (int i = 0; i < n; i++) { - Component child = cont.getComponent(i); - if (isAncestorOfFocusedComponent(child)) - return true; - } - } - } - return false; + if (c.hasFocus()) { + return true; + } else { + if (c instanceof Container) { + Container cont = (Container) c; + int n = cont.getComponentCount(); + for (int i = 0; i < n; i++) { + Component child = cont.getComponent(i); + if (isAncestorOfFocusedComponent(child)) + return true; + } + } + } + return false; } /** * Returns the first component in the tree of c that can accept * the focus. - * - * @param c the root of the component hierarchy to search + * + * @param c + * the root of the component hierarchy to search * @see #focusComponentOrChild - * @deprecated replaced by {@link #getFocusableComponentOrChild(Component,boolean)} + * @deprecated replaced by + * {@link #getFocusableComponentOrChild(Component,boolean)} * @return Component that was focused on. */ public static Component getFocusableComponentOrChild(Component c) { - return getFocusableComponentOrChild(c, false); + return getFocusableComponentOrChild(c, false); } /** * Returns the first component in the tree of c that can accept * the focus. - * - * @param c the root of the component hierarchy to search - * @param deepest if deepest is true the method will return the first and deepest component that can accept the - * focus. For example, if both a child and its parent are focusable and deepest is true, the child is - * returned. + * + * @param c + * the root of the component hierarchy to search + * @param deepest + * if deepest is true the method will return the + * first and deepest component that can accept the focus. For + * example, if both a child and its parent are focusable and + * deepest is true, the child is returned. * @see #focusComponentOrChild * @return Component that was focused on. */ - public static Component getFocusableComponentOrChild(Component c, boolean deepest) { - if (c != null && c.isEnabled() && c.isVisible()) { - if (c instanceof Container) { - Container cont = (Container)c; + public static Component getFocusableComponentOrChild(Component c, + boolean deepest) { + if (c != null && c.isEnabled() && c.isVisible()) { + if (c instanceof Container) { + Container cont = (Container) c; - if (!deepest) { // first one is a good one - if (c instanceof JComponent) { - JComponent jc = (JComponent)c; - if (jc.isRequestFocusEnabled()) { - return jc; - } - } - } + if (!deepest) { // first one is a good one + if (c instanceof JComponent) { + JComponent jc = (JComponent) c; + if (jc.isRequestFocusEnabled()) { + return jc; + } + } + } - int n = cont.getComponentCount(); - for (int i = 0; i < n; i++) { - Component child = cont.getComponent(i); - Component focused = getFocusableComponentOrChild(child, deepest); - if (focused != null) { - return focused; - } - } + int n = cont.getComponentCount(); + for (int i = 0; i < n; i++) { + Component child = cont.getComponent(i); + Component focused = getFocusableComponentOrChild(child, + deepest); + if (focused != null) { + return focused; + } + } - if (c instanceof JComponent) { - if (deepest) { - JComponent jc = (JComponent)c; - if (jc.isRequestFocusEnabled()) { - return jc; - } - } - } - else { - return c; - } - } - } + if (c instanceof JComponent) { + if (deepest) { + JComponent jc = (JComponent) c; + if (jc.isRequestFocusEnabled()) { + return jc; + } + } + } else { + return c; + } + } + } - return null; + return null; } /** * Puts the focus on the first component in the tree of c that * can accept the focus. - * + * * @see #getFocusableComponentOrChild - * @param c Component to focus on. + * @param c + * Component to focus on. * @return Component that was focused on. */ public static Component focusComponentOrChild(Component c) { - return focusComponentOrChild(c, false); + return focusComponentOrChild(c, false); } /** * Puts the focus on the first component in the tree of c that * can accept the focus. - * - * @param c the root of the component hierarchy to search - * @param deepest if deepest is true the method will focus the first and deepest component that can - * accept the focus. - * For example, if both a child and its parent are focusable and deepest is true, the child is focused. + * + * @param c + * the root of the component hierarchy to search + * @param deepest + * if deepest is true the method will focus the + * first and deepest component that can accept the focus. For + * example, if both a child and its parent are focusable and + * deepest is true, the child is focused. * @see #getFocusableComponentOrChild * @return Component that was focused on. */ public static Component focusComponentOrChild(Component c, boolean deepest) { - final Component focusable = getFocusableComponentOrChild(c, deepest); - if (focusable != null) { - focusable.requestFocus(); - } - return focusable; + final Component focusable = getFocusableComponentOrChild(c, deepest); + if (focusable != null) { + focusable.requestFocus(); + } + return focusable; } /** * Loads an {@link Image} named imageName as a resource - * relative to the Class cls. If the Image can - * not be loaded, then null is returned. Images loaded here - * will be added to an internal cache based upon the full {@link URL} to - * their location. + * relative to the Class cls. If the Image can not + * be loaded, then null is returned. Images loaded here will be + * added to an internal cache based upon the full {@link URL} to their + * location. *

* This method replaces legacy code from JDeveloper 3.x and earlier. - * + * * @see Class#getResource(String) * @see Toolkit#createImage(URL) - * @param imageName Name of the resource to load. - * @param cls Class to pull resource from. + * @param imageName + * Name of the resource to load. + * @param cls + * Class to pull resource from. * @return Image loaded from resource. */ public static Image loadFromResource(String imageName, Class cls) { - try { - final URL url = cls.getResource(imageName); + try { + final URL url = cls.getResource(imageName); - if (url == null) { - return null; - } + if (url == null) { + return null; + } - Image image = imageCache.get(url.toString()); + Image image = imageCache.get(url.toString()); - if (image == null) { - image = Toolkit.getDefaultToolkit().createImage(url); - imageCache.put(url.toString(), image); - } + if (image == null) { + image = Toolkit.getDefaultToolkit().createImage(url); + imageCache.put(url.toString(), image); + } - return image; - } - catch (Exception e) { - Log.error(e); - } + return image; + } catch (Exception e) { + Log.error(e); + } - return null; + return null; } + /** + * Returns the ScreenBounds + * + * @return Array of {@link Rectangle}'s + */ public static Rectangle[] getScreenBounds() { - GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment(); - final GraphicsDevice[] screenDevices = graphicsEnvironment.getScreenDevices(); - Rectangle[] screenBounds = new Rectangle[screenDevices.length]; - for (int i = 0; i < screenDevices.length; i++) { - GraphicsDevice screenDevice = screenDevices[i]; - final GraphicsConfiguration defaultConfiguration = screenDevice.getDefaultConfiguration(); - screenBounds[i] = defaultConfiguration.getBounds(); - } + GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment + .getLocalGraphicsEnvironment(); + final GraphicsDevice[] screenDevices = graphicsEnvironment + .getScreenDevices(); + Rectangle[] screenBounds = new Rectangle[screenDevices.length]; + for (int i = 0; i < screenDevices.length; i++) { + GraphicsDevice screenDevice = screenDevices[i]; + final GraphicsConfiguration defaultConfiguration = screenDevice + .getDefaultConfiguration(); + screenBounds[i] = defaultConfiguration.getBounds(); + } - return screenBounds; + return screenBounds; } - + /** + * Makes all Compontens the same Size + * + * @param comps + */ public static void makeSameSize(JComponent... comps) { - if (comps.length == 0) { - return; - } + if (comps.length == 0) { + return; + } - int max = 0; - for (JComponent comp1 : comps) { - int w = comp1.getPreferredSize().width; - max = w > max ? w : max; - } + int max = 0; + for (JComponent comp1 : comps) { + int w = comp1.getPreferredSize().width; + max = w > max ? w : max; + } - Dimension dim = new Dimension(max, comps[0].getPreferredSize().height); - for (JComponent comp : comps) { - comp.setPreferredSize(dim); - } + Dimension dim = new Dimension(max, comps[0].getPreferredSize().height); + for (JComponent comp : comps) { + comp.setPreferredSize(dim); + } } /** * Return the hexidecimal color from a java.awt.Color - * - * @param c Color to convert. + * + * @param c + * Color to convert. * @return hexadecimal string */ public static String toHTMLColor(Color c) { - int color = c.getRGB(); - color |= 0xff000000; - String s = Integer.toHexString(color); - return s.substring(2); + int color = c.getRGB(); + color |= 0xff000000; + String s = Integer.toHexString(color); + return s.substring(2); } - public static String createToolTip(String text, int width) { - final String htmlColor = toHTMLColor(TOOLTIP_COLOR); - return "
" + text + "
"; - } - - public static String createToolTip(String text) { - final String htmlColor = toHTMLColor(TOOLTIP_COLOR); - return "
" + text + "
"; - } - - public static String getHighlightedWords(String text, String query) { - final StringTokenizer tkn = new StringTokenizer(query, " "); - int tokenCount = tkn.countTokens(); - String[] words = new String[tokenCount]; - for (int j = 0; j < tokenCount; j++) { - String queryWord = tkn.nextToken(); - words[j] = queryWord; - } - - String highlightedWords; - try { - highlightedWords = StringUtils.highlightWords(text, words, "", ""); - } - catch (Exception e) { - highlightedWords = text; - } - - - return highlightedWords; - } - - public static ImageIcon createShadowPicture(Image buf) { - buf = removeTransparency(buf); - - BufferedImage splash; - - JLabel label = new JLabel(); - int width = buf.getWidth(null); - int height = buf.getHeight(null); - int extra = 4; - - splash = new BufferedImage(width + extra, height + extra, BufferedImage.TYPE_INT_ARGB); - Graphics2D g2 = (Graphics2D)splash.getGraphics(); - - - BufferedImage shadow = new BufferedImage(width + extra, height + extra, BufferedImage.TYPE_INT_ARGB); - Graphics g = shadow.getGraphics(); - g.setColor(new Color(0.0f, 0.0f, 0.0f, 0.3f)); - g.fillRoundRect(0, 0, width, height, 12, 12); - - g2.drawImage(shadow, getBlurOp(7), 0, 0); - g2.drawImage(buf, 0, 0, label); - return new ImageIcon(splash); - } - - public static BufferedImage removeTransparency(Image image) { - int w = image.getWidth(null); - int h = image.getHeight(null); - BufferedImage bi2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); - Graphics2D g = bi2.createGraphics(); - g.setColor(Color.WHITE); - g.fillRect(0, 0, w, h); - g.drawImage(image, 0, 0, null); - g.dispose(); - - return bi2; - } - - public static Image toImage(BufferedImage bufferedImage) { - return Toolkit.getDefaultToolkit().createImage(bufferedImage.getSource()); - } - - private static ConvolveOp getBlurOp(int size) { - float[] data = new float[size * size]; - float value = 1 / (float)(size * size); - for (int i = 0; i < data.length; i++) { - data[i] = value; - } - return new ConvolveOp(new Kernel(size, size, data)); - } - - public static BufferedImage convert(Image im) throws InterruptedException, IOException { - load(im); - BufferedImage bi = new BufferedImage(im.getWidth(null), im.getHeight(null), BufferedImage.TYPE_INT_ARGB_PRE); - Graphics bg = bi.getGraphics(); - bg.drawImage(im, 0, 0, null); - bg.dispose(); - return bi; - } - - public static void load(Image image) throws InterruptedException, IOException { - MediaTracker tracker = new MediaTracker(new Label()); //any component will do - tracker.addImage(image, 0); - tracker.waitForID(0); - if (tracker.isErrorID(0)) - throw new IOException("error loading image"); - } - - public static byte[] getBytesFromImage(Image image) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try { - ImageIO.write(convert(image), "PNG", baos); - } - catch (IOException e) { - Log.error(e); - } - catch (InterruptedException e) { - Log.error(e); - } - - return baos.toByteArray(); - } - - /** - * Returns a scaled down image if the height or width is smaller than - * the image size. - * - * @param icon the image icon. - * @param newHeight the preferred height. - * @param newWidth the preferred width. + * Creates a Tooltip with specified width + * + * @param text + * the Text appearing in the Tooltip + * @param width + * the width of the Tooltip + * @return HTML-String displaying the Tooltip + */ + public static String createToolTip(String text, int width) { + final String htmlColor = toHTMLColor(TOOLTIP_COLOR); + return "
" + text + "
"; + } + + /** + * Creates a Tooltop + * + * @param text + * , the Text appearing in the Tooltip + * @return HTML-String displaying the Tooltip + */ + public static String createToolTip(String text) { + final String htmlColor = toHTMLColor(TOOLTIP_COLOR); + return "
" + text + + "
"; + } + + /** + * Highlights Words + * + * @param text + * , the Text containing Words that should be Highlighted + * @param query + * , the Words that should be Highlighted + * @return HTML-String, with Highlighted Words + */ + public static String getHighlightedWords(String text, String query) { + final StringTokenizer tkn = new StringTokenizer(query, " "); + int tokenCount = tkn.countTokens(); + String[] words = new String[tokenCount]; + for (int j = 0; j < tokenCount; j++) { + String queryWord = tkn.nextToken(); + words[j] = queryWord; + } + + String highlightedWords; + try { + highlightedWords = StringUtils.highlightWords(text, words, + "", + ""); + } catch (Exception e) { + highlightedWords = text; + } + + return highlightedWords; + } + + /** + * Creates an {@link ImageIcon} with a Shadow + * + * @param buf + * , the {@link Image} where a Shadow will be applied + * @return {@link ImageIcon} with shadow + */ + public static ImageIcon createShadowPicture(Image buf) { + buf = removeTransparency(buf); + + BufferedImage splash; + + JLabel label = new JLabel(); + int width = buf.getWidth(null); + int height = buf.getHeight(null); + int extra = 4; + + splash = new BufferedImage(width + extra, height + extra, + BufferedImage.TYPE_INT_ARGB); + Graphics2D g2 = (Graphics2D) splash.getGraphics(); + + BufferedImage shadow = new BufferedImage(width + extra, height + extra, + BufferedImage.TYPE_INT_ARGB); + Graphics g = shadow.getGraphics(); + g.setColor(new Color(0.0f, 0.0f, 0.0f, 0.3f)); + g.fillRoundRect(0, 0, width, height, 12, 12); + + g2.drawImage(shadow, getBlurOp(7), 0, 0); + g2.drawImage(buf, 0, 0, label); + return new ImageIcon(splash); + } + + /** + * removes the Transparency of an {@link Image} + * + * @param image + * @return {@link BufferedImage} without Transparency + */ + public static BufferedImage removeTransparency(Image image) { + int w = image.getWidth(null); + int h = image.getHeight(null); + BufferedImage bi2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); + Graphics2D g = bi2.createGraphics(); + g.setColor(Color.WHITE); + g.fillRect(0, 0, w, h); + g.drawImage(image, 0, 0, null); + g.dispose(); + + return bi2; + } + + /** + * Converts a {@link BufferedImage} to {@link Image} + * + * @param bufferedImage + * , the {@link BufferedImage} + * @return {@link Image} + */ + public static Image toImage(BufferedImage bufferedImage) { + return Toolkit.getDefaultToolkit().createImage( + bufferedImage.getSource()); + } + + /** + * + * @param size + * @return + */ + private static ConvolveOp getBlurOp(int size) { + float[] data = new float[size * size]; + float value = 1 / (float) (size * size); + for (int i = 0; i < data.length; i++) { + data[i] = value; + } + return new ConvolveOp(new Kernel(size, size, data)); + } + + /** + * Converting an {@link Image} into a {@link BufferedImage} Transparency + * some Images gets lost + * + * @param im + * @return {@link BufferedImage} + * @throws InterruptedException + * @throws {@link IOException} + */ + public static BufferedImage convert(Image im) throws InterruptedException, + IOException { + load(im); + BufferedImage bi = new BufferedImage(im.getWidth(null), + im.getHeight(null), BufferedImage.TYPE_INT_ARGB_PRE); + Graphics bg = bi.getGraphics(); + bg.drawImage(im, 0, 0, null); + bg.dispose(); + return bi; + } + + /** + * Loading an image via a MediaTracker + * + * @param image + * @throws InterruptedException + * @throws IOException + */ + public static void load(Image image) throws InterruptedException, + IOException { + MediaTracker tracker = new MediaTracker(new Label()); // any component + // will do + tracker.addImage(image, 0); + tracker.waitForID(0); + if (tracker.isErrorID(0)) + throw new IOException("error loading image"); + } + + /** + * Gets the Bytes from an Image using an ImageInputStream, this way + * transparency is kept + * + * @param file + * , input {@link File} + * @return byte[] + */ + public static byte[] getBytesFromImage(File file) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try { + Object input = file; + + ImageInputStream stream = ImageIO.createImageInputStream(input); + Iterator readers = ImageIO.getImageReaders(stream); + + if (!readers.hasNext()) + throw new RuntimeException("no image reader found"); + + ImageReader reader = readers.next(); + reader.setInput(stream); + int n = reader.getNumImages(true); + for (int i = 0; i < n; i++) { + BufferedImage image = reader.read(i); + ImageIO.write(image, "PNG", baos); + } + stream.close(); + + } catch (IOException e) { + Log.error(e); + } + return baos.toByteArray(); + + } + + /** + * Returns the ByteArray From an Image using ImageIO.write Transparency is + * not handled + * + * @param image + * , input {@link Image} + * @return byte[] + */ + public static byte[] getBytesFromImage(Image image) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try { + ImageIO.write(convert(image), "PNG", baos); + } catch (IOException e) { + Log.error(e); + } catch (InterruptedException e) { + Log.error(e); + } + + return baos.toByteArray(); + } + + /** + * Returns a scaled down image if the height or width is smaller than the + * image size. + * + * @param icon + * the image icon. + * @param newHeight + * the preferred height. + * @param newWidth + * the preferred width. * @return the icon. */ - public static ImageIcon scaleImageIcon(ImageIcon icon, int newHeight, int newWidth) { - Image img = icon.getImage(); - int height = icon.getIconHeight(); - int width = icon.getIconWidth(); + public static ImageIcon scaleImageIcon(ImageIcon icon, int newHeight, + int newWidth) { + Image img = icon.getImage(); + int height = icon.getIconHeight(); + int width = icon.getIconWidth(); - if (height > newHeight) { - height = newHeight; - } + if (height > newHeight) { + height = newHeight; + } - if (width > newWidth) { - width = newWidth; - } - img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH); - return new ImageIcon(img); + if (width > newWidth) { + width = newWidth; + } + img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH); + return new ImageIcon(img); } - /** - * Returns a scaled down image if the height or width is smaller than - * the image size. - * - * @param icon the image icon. - * @param newHeight the preferred height. - * @param newWidth the preferred width. + * Returns a scaled down image if the height or width is smaller than the + * image size. + * + * @param icon + * the image icon. + * @param newHeight + * the preferred height. + * @param newWidth + * the preferred width. * @return the icon. */ public static ImageIcon scale(ImageIcon icon, int newHeight, int newWidth) { - Image img = icon.getImage(); - img = img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH); - return new ImageIcon(img); + Image img = icon.getImage(); + img = img.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH); + return new ImageIcon(img); } /** * Returns the native icon, if one exists for the filetype, otherwise * returns a default document icon. - * - * @param file the file to check icon type. + * + * @param file + * the file to check icon type. * @return the native icon, otherwise default document icon. */ public static Icon getIcon(File file) { - try { - sun.awt.shell.ShellFolder sf = sun.awt.shell.ShellFolder.getShellFolder(file); + try { + sun.awt.shell.ShellFolder sf = sun.awt.shell.ShellFolder + .getShellFolder(file); - // Get large icon - return new ImageIcon(sf.getIcon(true), sf.getFolderType()); - } - catch (Exception e) { - try { - return new JFileChooser().getIcon(file); - } - catch (Exception e1) { - // Do nothing. - } - } + // Get large icon + return new ImageIcon(sf.getIcon(true), sf.getFolderType()); + } catch (Exception e) { + try { + return new JFileChooser().getIcon(file); + } catch (Exception e1) { + // Do nothing. + } + } - return SparkRes.getImageIcon(SparkRes.DOCUMENT_INFO_32x32); + return SparkRes.getImageIcon(SparkRes.DOCUMENT_INFO_32x32); } - + /** + * Converts a File holding an Image into a Buffered Image + * + * @param file + * {@link File} + * @return {@link BufferedImage} + */ public static BufferedImage getBufferedImage(File file) { - // Why wasn't this using it's code that pulled from the file? Hrm. - Icon icon = SparkRes.getImageIcon(SparkRes.DOCUMENT_INFO_32x32); + // Why wasn't this using it's code that pulled from the file? Hrm. + Icon icon = SparkRes.getImageIcon(SparkRes.DOCUMENT_INFO_32x32); - BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.OPAQUE); - Graphics bg = bi.getGraphics(); + BufferedImage bi = new BufferedImage(icon.getIconWidth(), + icon.getIconHeight(), BufferedImage.OPAQUE); + Graphics bg = bi.getGraphics(); - ImageIcon i = (ImageIcon)icon; + ImageIcon i = (ImageIcon) icon; - bg.drawImage(i.getImage(), 0, 0, null); - bg.dispose(); + bg.drawImage(i.getImage(), 0, 0, null); + bg.dispose(); - return bi; + return bi; } - - - public static void centerWindowOnScreen(Runnable runnable) - { - // TODO Auto-generated method stub - - } + + // public static void centerWindowOnScreen(Runnable runnable) { + // // This method is never used + // + // } } diff --git a/src/java/org/jivesoftware/spark/util/StringUtils.java b/src/java/org/jivesoftware/spark/util/StringUtils.java index 8f789ed1..781386c9 100644 --- a/src/java/org/jivesoftware/spark/util/StringUtils.java +++ b/src/java/org/jivesoftware/spark/util/StringUtils.java @@ -19,7 +19,6 @@ */ package org.jivesoftware.spark.util; - import javax.swing.KeyStroke; import java.awt.event.InputEvent; @@ -36,7 +35,6 @@ import java.util.Random; import java.util.regex.Matcher; import java.util.regex.Pattern; - /** * Utility class to peform common String manipulation algorithms. */ @@ -58,320 +56,329 @@ public class StringUtils { // prepare the patterns static { - // constants used in the parsing of email addresses - String basicAddress = "^([\\w\\.-]+)@([\\w\\.-]+)$"; - String specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]"; - String validChars = "[^ \f\n\r\t" + specialChars + "]"; - String atom = validChars + "+"; - String quotedUser = "(\"[^\"]+\")"; - String word = "(" + atom + "|" + quotedUser + ")"; - String validUser = "^" + word + "(\\." + word + ")*$"; - String domain = "^" + atom + "(\\." + atom + ")+$"; - String ipDomain = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$"; + // constants used in the parsing of email addresses + String basicAddress = "^([\\w\\.-]+)@([\\w\\.-]+)$"; + String specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]"; + String validChars = "[^ \f\n\r\t" + specialChars + "]"; + String atom = validChars + "+"; + String quotedUser = "(\"[^\"]+\")"; + String word = "(" + atom + "|" + quotedUser + ")"; + String validUser = "^" + word + "(\\." + word + ")*$"; + String domain = "^" + atom + "(\\." + atom + ")+$"; + String ipDomain = "^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$"; - // from http://www.icann.org/tlds/ - String knownTLDs = "^\\.(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$"; + // from http://www.icann.org/tlds/ + String knownTLDs = "^\\.(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$"; - basicAddressPattern = Pattern.compile(basicAddress, Pattern.CASE_INSENSITIVE); - validUserPattern = Pattern.compile(validUser, Pattern.CASE_INSENSITIVE); - domainPattern = Pattern.compile(domain, Pattern.CASE_INSENSITIVE); - ipDomainPattern = Pattern.compile(ipDomain, Pattern.CASE_INSENSITIVE); - tldPattern = Pattern.compile(knownTLDs, Pattern.CASE_INSENSITIVE); + basicAddressPattern = Pattern.compile(basicAddress, + Pattern.CASE_INSENSITIVE); + validUserPattern = Pattern.compile(validUser, Pattern.CASE_INSENSITIVE); + domainPattern = Pattern.compile(domain, Pattern.CASE_INSENSITIVE); + ipDomainPattern = Pattern.compile(ipDomain, Pattern.CASE_INSENSITIVE); + tldPattern = Pattern.compile(knownTLDs, Pattern.CASE_INSENSITIVE); } - /** * Replaces all instances of oldString with newString in string. - * - * @param string the String to search to perform replacements on - * @param oldString the String that should be replaced by newString - * @param newString the String that will replace all instances of oldString + * + * @param string + * the String to search to perform replacements on + * @param oldString + * the String that should be replaced by newString + * @param newString + * the String that will replace all instances of oldString * @return a String will all instances of oldString replaced by newString */ - public static String replace(String string, String oldString, String newString) { - if (string == null) { - return null; - } + public static String replace(String string, String oldString, + String newString) { + if (string == null) { + return null; + } - // If the newString is null or zero length, just return the string since there's nothing - // to replace. - if (newString == null) { - return string; - } + // If the newString is null or zero length, just return the string since + // there's nothing + // to replace. + if (newString == null) { + return string; + } - int i = 0; + int i = 0; - // Make sure that oldString appears at least once before doing any processing. - if ((i = string.indexOf(oldString, i)) >= 0) { - // Use char []'s, as they are more efficient to deal with. - char [] string2 = string.toCharArray(); - char [] newString2 = newString.toCharArray(); - int oLength = oldString.length(); - StringBuffer buf = new StringBuffer(string2.length); - buf.append(string2, 0, i).append(newString2); - i += oLength; - int j = i; + // Make sure that oldString appears at least once before doing any + // processing. + if ((i = string.indexOf(oldString, i)) >= 0) { + // Use char []'s, as they are more efficient to deal with. + char[] string2 = string.toCharArray(); + char[] newString2 = newString.toCharArray(); + int oLength = oldString.length(); + StringBuffer buf = new StringBuffer(string2.length); + buf.append(string2, 0, i).append(newString2); + i += oLength; + int j = i; - // Replace all remaining instances of oldString with newString. - while ((i = string.indexOf(oldString, i)) > 0) { - buf.append(string2, j, i - j).append(newString2); - i += oLength; - j = i; - } + // Replace all remaining instances of oldString with newString. + while ((i = string.indexOf(oldString, i)) > 0) { + buf.append(string2, j, i - j).append(newString2); + i += oLength; + j = i; + } - buf.append(string2, j, string2.length - j); - return buf.toString(); - } + buf.append(string2, j, string2.length - j); + return buf.toString(); + } - return string; - } - - - /** - * Replaces all instances of oldString with newString in line with the - *

- * added feature that matches of newString in oldString ignore case. - * - * @param line the String to search to perform replacements on - * @param oldString the String that should be replaced by newString - * @param newString the String that will replace all instances of oldString - * @return a String will all instances of oldString replaced by newString - */ - public static String replaceIgnoreCase(String line, String oldString, String newString) { - if (line == null) { - return null; - } - - String lcLine = line.toLowerCase(); - String lcOldString = oldString.toLowerCase(); - int i = 0; - if ((i = lcLine.indexOf(lcOldString, i)) >= 0) { - char [] line2 = line.toCharArray(); - char [] newString2 = newString.toCharArray(); - int oLength = oldString.length(); - StringBuffer buf = new StringBuffer(line2.length); - buf.append(line2, 0, i).append(newString2); - i += oLength; - int j = i; - while ((i = lcLine.indexOf(lcOldString, i)) > 0) { - buf.append(line2, j, i - j).append(newString2); - i += oLength; - j = i; - } - buf.append(line2, j, line2.length - j); - return buf.toString(); - } - return line; + return string; } /** - * Replaces all instances of oldString with newString in line with the - *

- * added feature that matches of newString in oldString ignore case. - *

- * The count paramater is set to the number of replaces performed. - * - * @param line the String to search to perform replacements on - * @param oldString the String that should be replaced by newString - * @param newString the String that will replace all instances of oldString - * @param count a value that will be updated with the number of replaces - *

- * performed. + * Replaces all instances of oldString with newString in line with the added + * feature that matches of newString in oldString ignore case. + * + * @param line + * the String to search to perform replacements on + * @param oldString + * the String that should be replaced by newString + * @param newString + * the String that will replace all instances of oldString + * @return a String will all instances of oldString replaced by newString + */ + public static String replaceIgnoreCase(String line, String oldString, + String newString) { + if (line == null) { + return null; + } + + String lcLine = line.toLowerCase(); + String lcOldString = oldString.toLowerCase(); + int i = 0; + if ((i = lcLine.indexOf(lcOldString, i)) >= 0) { + char[] line2 = line.toCharArray(); + char[] newString2 = newString.toCharArray(); + int oLength = oldString.length(); + StringBuffer buf = new StringBuffer(line2.length); + buf.append(line2, 0, i).append(newString2); + i += oLength; + int j = i; + while ((i = lcLine.indexOf(lcOldString, i)) > 0) { + buf.append(line2, j, i - j).append(newString2); + i += oLength; + j = i; + } + buf.append(line2, j, line2.length - j); + return buf.toString(); + } + return line; + } + + /** + * Replaces all instances of oldString with newString in line with the added + * feature that matches of newString in oldString ignore case.

The + * count paramater is set to the number of replaces performed. + * + * @param line + * the String to search to perform replacements on + * @param oldString + * the String that should be replaced by newString + * @param newString + * the String that will replace all instances of oldString + * @param count + * a value that will be updated with the number of replaces + *

+ * performed. * @return a String will all instances of oldString replaced by newString */ - public static String replaceIgnoreCase(String line, String oldString, String newString, int[] count) - { - if (line == null) { - return null; - } - String lcLine = line.toLowerCase(); - String lcOldString = oldString.toLowerCase(); - int i = 0; - if ((i = lcLine.indexOf(lcOldString, i)) >= 0) { - int counter = 1; - char [] line2 = line.toCharArray(); - char [] newString2 = newString.toCharArray(); - int oLength = oldString.length(); - StringBuffer buf = new StringBuffer(line2.length); - buf.append(line2, 0, i).append(newString2); - i += oLength; - int j = i; - while ((i = lcLine.indexOf(lcOldString, i)) > 0) { - counter++; - buf.append(line2, j, i - j).append(newString2); - i += oLength; - j = i; - } - buf.append(line2, j, line2.length - j); - count[0] = counter; - return buf.toString(); - } - return line; + public static String replaceIgnoreCase(String line, String oldString, + String newString, int[] count) { + if (line == null) { + return null; + } + String lcLine = line.toLowerCase(); + String lcOldString = oldString.toLowerCase(); + int i = 0; + if ((i = lcLine.indexOf(lcOldString, i)) >= 0) { + int counter = 1; + char[] line2 = line.toCharArray(); + char[] newString2 = newString.toCharArray(); + int oLength = oldString.length(); + StringBuffer buf = new StringBuffer(line2.length); + buf.append(line2, 0, i).append(newString2); + i += oLength; + int j = i; + while ((i = lcLine.indexOf(lcOldString, i)) > 0) { + counter++; + buf.append(line2, j, i - j).append(newString2); + i += oLength; + j = i; + } + buf.append(line2, j, line2.length - j); + count[0] = counter; + return buf.toString(); + } + return line; } /** * Replaces all instances of oldString with newString in line. *

* The count Integer is updated with number of replaces. - * - * @param line the String to search to perform replacements on - * @param oldString the String that should be replaced by newString - * @param newString the String that will replace all instances of oldString - * @param count Number of replaces. + * + * @param line + * the String to search to perform replacements on + * @param oldString + * the String that should be replaced by newString + * @param newString + * the String that will replace all instances of oldString + * @param count + * Number of replaces. * @return a String will all instances of oldString replaced by newString */ - public static String replace(String line, String oldString, String newString, int[] count) - { - if (line == null) { - return null; - } - int i = 0; - if ((i = line.indexOf(oldString, i)) >= 0) { - int counter = 1; - char [] line2 = line.toCharArray(); - char [] newString2 = newString.toCharArray(); - int oLength = oldString.length(); - StringBuffer buf = new StringBuffer(line2.length); - buf.append(line2, 0, i).append(newString2); - i += oLength; - int j = i; - while ((i = line.indexOf(oldString, i)) > 0) { - counter++; - buf.append(line2, j, i - j).append(newString2); - i += oLength; - j = i; - } - buf.append(line2, j, line2.length - j); - count[0] = counter; - return buf.toString(); - } - return line; + public static String replace(String line, String oldString, + String newString, int[] count) { + if (line == null) { + return null; + } + int i = 0; + if ((i = line.indexOf(oldString, i)) >= 0) { + int counter = 1; + char[] line2 = line.toCharArray(); + char[] newString2 = newString.toCharArray(); + int oLength = oldString.length(); + StringBuffer buf = new StringBuffer(line2.length); + buf.append(line2, 0, i).append(newString2); + i += oLength; + int j = i; + while ((i = line.indexOf(oldString, i)) > 0) { + counter++; + buf.append(line2, j, i - j).append(newString2); + i += oLength; + j = i; + } + buf.append(line2, j, line2.length - j); + count[0] = counter; + return buf.toString(); + } + return line; } /** - * This method takes a string and strips out all tags except
tags while still leaving - *

- * the tag body intact. - * - * @param in the text to be converted. + * This method takes a string and strips out all tags except
+ * tags while still leaving the tag body intact. + * + * @param in + * the text to be converted. * @return the input string with all tags removed. */ public static String stripTags(String in) { - if (in == null) { - return null; - } + if (in == null) { + return null; + } - return stripTags(in, false); + return stripTags(in, false); } /** * This method takes a string and strips out all tags while still leaving - *

* the tag body intact. - * - * @param in the text to be converted. - * @param stripBRTag Remove BR tags. + * + * @param in + * the text to be converted. + * @param stripBRTag + * Remove BR tags. * @return the input string with all tags removed. */ public static String stripTags(String in, boolean stripBRTag) { - if (in == null) { - return null; - } + if (in == null) { + return null; + } - char ch; - int i = 0; - int last = 0; - char[] input = in.toCharArray(); - int len = input.length; - StringBuffer out = new StringBuffer((int)(len * 1.3)); - for (; i < len; i++) { - ch = input[i]; - if (ch > '>') { - // Nothing to do - } - else if (ch == '<') { - if (!stripBRTag && i + 3 < len && input[i + 1] == 'b' && input[i + 2] == 'r' && input[i + 3] == '>') { - i += 3; - continue; - } - if (i > last) { - if (last > 0) { - out.append(" "); - } - out.append(input, last, i - last); - } - last = i + 1; - } - else if (ch == '>') { - last = i + 1; - } - } - if (last == 0) { - return in; - } - if (i > last) { - out.append(input, last, i - last); - } - return out.toString(); + char ch; + int i = 0; + int last = 0; + char[] input = in.toCharArray(); + int len = input.length; + StringBuffer out = new StringBuffer((int) (len * 1.3)); + for (; i < len; i++) { + ch = input[i]; + if (ch > '>') { + // Nothing to do + } else if (ch == '<') { + if (!stripBRTag && i + 3 < len && input[i + 1] == 'b' + && input[i + 2] == 'r' && input[i + 3] == '>') { + i += 3; + continue; + } + if (i > last) { + if (last > 0) { + out.append(" "); + } + out.append(input, last, i - last); + } + last = i + 1; + } else if (ch == '>') { + last = i + 1; + } + } + if (last == 0) { + return in; + } + if (i > last) { + out.append(input, last, i - last); + } + return out.toString(); } /** * This method takes a string which may contain HTML tags (ie, <b>, - *

- * <table>, etc) and converts the '<'' and '>' characters to - *

- * their HTML escape sequences. - * - * @param in the text to be converted. + * <table>, etc) and converts the '<' and '>' characters to their + * HTML escape sequences. + * + * @param in + * the text to be converted. * @return the input string with the characters '<' and '>' replaced - *

* with their HTML escape sequences. */ public static String escapeHTMLTags(String in) { - if (in == null) { - return null; - } - char ch; - int i = 0; - int last = 0; - char[] input = in.toCharArray(); - int len = input.length; - StringBuffer out = new StringBuffer((int)(len * 1.3)); - for (; i < len; i++) { - ch = input[i]; - if (ch > '>') { - // Nothing to do - } - else if (ch == '<') { - if (i > last) { - out.append(input, last, i - last); - } - last = i + 1; - out.append(LT_ENCODE); - } - else if (ch == '>') { - if (i > last) { - out.append(input, last, i - last); - } - last = i + 1; - out.append(GT_ENCODE); - } - else if (ch == '"') { - if (i > last) { - out.append(input, last, i - last); - } - last = i + 1; - out.append(QUOTE_ENCODE); - } - } - if (last == 0) { - return in; - } - if (i > last) { - out.append(input, last, i - last); - } - return out.toString(); + if (in == null) { + return null; + } + char ch; + int i = 0; + int last = 0; + char[] input = in.toCharArray(); + int len = input.length; + StringBuffer out = new StringBuffer((int) (len * 1.3)); + for (; i < len; i++) { + ch = input[i]; + if (ch > '>') { + // Nothing to do + } else if (ch == '<') { + if (i > last) { + out.append(input, last, i - last); + } + last = i + 1; + out.append(LT_ENCODE); + } else if (ch == '>') { + if (i > last) { + out.append(input, last, i - last); + } + last = i + 1; + out.append(GT_ENCODE); + } else if (ch == '"') { + if (i > last) { + out.append(input, last, i - last); + } + last = i + 1; + out.append(QUOTE_ENCODE); + } + } + if (last == 0) { + return in; + } + if (i > last) { + out.append(input, last, i - last); + } + return out.toString(); } /** @@ -380,292 +387,272 @@ public class StringUtils { private static MessageDigest digest = null; /** - * Hashes a String using the Md5 algorithm and returns the result as a + * Hashes a String using the Md5 algorithm and returns the result as + * a String of hexadecimal numbers. This method is synchronized to avoid + * excessive MessageDigest object creation. If calling this method becomes a + * bottleneck in your code, you may wish to maintain a pool of MessageDigest + * objects instead of using this method. *

- * String of hexadecimal numbers. This method is synchronized to avoid + * A hash is a one-way function -- that is, given an input, an output is + * easily computed. However, given the output, the input is almost + * impossible to compute. This is useful for passwords since we can store + * the hash and a hacker will then have a very hard time determining the + * original password. *

- * excessive MessageDigest object creation. If calling this method becomes - *

- * a bottleneck in your code, you may wish to maintain a pool of - *

- * MessageDigest objects instead of using this method. - *

- *

- *

- * A hash is a one-way function -- that is, given an - *

- * input, an output is easily computed. However, given the output, the - *

- * input is almost impossible to compute. This is useful for passwords - *

- * since we can store the hash and a hacker will then have a very hard time - *

- * determining the original password. - *

- *

- *

- * In Jive, every time a user logs in, we simply - *

- * take their plain text password, compute the hash, and compare the - *

- * generated hash to the stored hash. Since it is almost impossible that - *

- * two passwords will generate the same hash, we know if the user gave us - *

- * the correct password or not. The only negative to this system is that - *

- * password recovery is basically impossible. Therefore, a reset password - *

- * method is used instead. - * - * @param data the String to compute the hash of. + * In Jive, every time a user logs in, we simply take their plain text + * password, compute the hash, and compare the generated hash to the stored + * hash. Since it is almost impossible that two passwords will generate the + * same hash, we know if the user gave us the correct password or not. The + * only negative to this system is that password recovery is basically + * impossible. Therefore, a reset password method is used instead. + * + * @param data + * the String to compute the hash of. * @return a hashed version of the passed-in String */ public synchronized static String hash(String data) { - if (digest == null) { - try { - digest = MessageDigest.getInstance("MD5"); - } - catch (NoSuchAlgorithmException nsae) { - // Nothing to do - } - } - // Now, compute hash. - try { - digest.update(data.getBytes("utf-8")); - } - catch (UnsupportedEncodingException e) { - // Nothing to do - } - return encodeHex(digest.digest()); + if (digest == null) { + try { + digest = MessageDigest.getInstance("MD5"); + } catch (NoSuchAlgorithmException nsae) { + // Nothing to do + } + } + // Now, compute hash. + try { + digest.update(data.getBytes("utf-8")); + } catch (UnsupportedEncodingException e) { + // Nothing to do + } + return encodeHex(digest.digest()); } public synchronized static String hash(byte[] data) { - if (digest == null) { - try { - digest = MessageDigest.getInstance("MD5"); - } - catch (NoSuchAlgorithmException nsae) { - // Nothing to do - } - } - // Now, compute hash. - digest.update(data); - return encodeHex(digest.digest()); + if (digest == null) { + try { + digest = MessageDigest.getInstance("MD5"); + } catch (NoSuchAlgorithmException nsae) { + // Nothing to do + } + } + // Now, compute hash. + digest.update(data); + return encodeHex(digest.digest()); } /** * Turns an array of bytes into a String representing each byte as an * unsigned hex number. - * - * @param bytes an array of bytes to convert to a hex-string + * + * @param bytes + * an array of bytes to convert to a hex-string * @return generated hex string */ public static String encodeHex(byte[] bytes) { - StringBuffer buf = new StringBuffer(bytes.length * 2); - int i; + StringBuffer buf = new StringBuffer(bytes.length * 2); + int i; - for (i = 0; i < bytes.length; i++) { - if (((int)bytes[i] & 0xff) < 0x10) { - buf.append("0"); - } - buf.append(Long.toString((int)bytes[i] & 0xff, 16)); - } - return buf.toString(); + for (i = 0; i < bytes.length; i++) { + if (((int) bytes[i] & 0xff) < 0x10) { + buf.append("0"); + } + buf.append(Long.toString((int) bytes[i] & 0xff, 16)); + } + return buf.toString(); } /** - * Turns a hex encoded string into a byte array. It is specifically meant - *

- * to "reverse" the toHex(byte[]) method. - * - * @param hex a hex encoded String to transform into a byte array. + * Turns a hex encoded string into a byte array. It is specifically meant to + * "reverse" the toHex(byte[]) method. + * + * @param hex + * a hex encoded String to transform into a byte array. * @return a byte array representing the hex String[ */ public static byte[] decodeHex(String hex) { - char [] chars = hex.toCharArray(); - byte[] bytes = new byte[chars.length / 2]; - int byteCount = 0; - for (int i = 0; i < chars.length; i += 2) { - int newByte = 0x00; - newByte |= hexCharToByte(chars[i]); - newByte <<= 4; - newByte |= hexCharToByte(chars[i + 1]); - bytes[byteCount] = (byte)newByte; - byteCount++; - } - return bytes; + char[] chars = hex.toCharArray(); + byte[] bytes = new byte[chars.length / 2]; + int byteCount = 0; + for (int i = 0; i < chars.length; i += 2) { + int newByte = 0x00; + newByte |= hexCharToByte(chars[i]); + newByte <<= 4; + newByte |= hexCharToByte(chars[i + 1]); + bytes[byteCount] = (byte) newByte; + byteCount++; + } + return bytes; } /** * Returns the the byte value of a hexadecmical char (0-f). It's assumed - *

* that the hexidecimal chars are lower case as appropriate. - * - * @param ch a hexedicmal character (0-f) + * + * @param ch + * a hexedicmal character (0-f) * @return the byte value of the character (0x00-0x0F) */ private static byte hexCharToByte(char ch) { - switch (ch) { - case '0': - return 0x00; - case '1': - return 0x01; - case '2': - return 0x02; - case '3': - return 0x03; - case '4': - return 0x04; - case '5': - return 0x05; - case '6': - return 0x06; - case '7': - return 0x07; - case '8': - return 0x08; - case '9': - return 0x09; - case 'a': - return 0x0A; - case 'b': - return 0x0B; - case 'c': - return 0x0C; - case 'd': - return 0x0D; - case 'e': - return 0x0E; - case 'f': - return 0x0F; - } - return 0x00; + switch (ch) { + case '0': + return 0x00; + case '1': + return 0x01; + case '2': + return 0x02; + case '3': + return 0x03; + case '4': + return 0x04; + case '5': + return 0x05; + case '6': + return 0x06; + case '7': + return 0x07; + case '8': + return 0x08; + case '9': + return 0x09; + case 'a': + return 0x0A; + case 'b': + return 0x0B; + case 'c': + return 0x0C; + case 'd': + return 0x0D; + case 'e': + return 0x0E; + case 'f': + return 0x0F; + } + return 0x00; } - //********************************************************************* - //* Base64 - a simple base64 encoder and decoder. - //* - //* Copyright (c) 1999, Bob Withers - bwit@pobox.com - //* - //* This code may be freely used for any purpose, either personal - //* or commercial, provided the authors copyright notice remains - //* intact. - //********************************************************************* + // ********************************************************************* + // * Base64 - a simple base64 encoder and decoder. + // * + // * Copyright (c) 1999, Bob Withers - bwit@pobox.com + // * + // * This code may be freely used for any purpose, either personal + // * or commercial, provided the authors copyright notice remains + // * intact. + // ********************************************************************* /** * Encodes a String as a base64 String. - * - * @param data a String to encode. + * + * @param data + * a String to encode. * @return a base64 encoded String. */ public static String encodeBase64(String data) { - byte [] bytes = null; - try { - bytes = data.getBytes("ISO-8859-1"); - } - catch (UnsupportedEncodingException uee) { - // Nothing to do - } - return encodeBase64(bytes); + byte[] bytes = null; + try { + bytes = data.getBytes("ISO-8859-1"); + } catch (UnsupportedEncodingException uee) { + // Nothing to do + } + return encodeBase64(bytes); } /** * Encodes a byte array into a base64 String. - * - * @param data a byte array to encode. + * + * @param data + * a byte array to encode. * @return a base64 encode String. */ public static String encodeBase64(byte[] data) { - int c; - int len = data.length; - StringBuffer ret = new StringBuffer(((len / 3) + 1) * 4); - for (int i = 0; i < len; ++i) { - c = (data[i] >> 2) & 0x3f; - ret.append(cvt.charAt(c)); - c = (data[i] << 4) & 0x3f; - if (++i < len) - c |= (data[i] >> 4) & 0x0f; + int c; + int len = data.length; + StringBuffer ret = new StringBuffer(((len / 3) + 1) * 4); + for (int i = 0; i < len; ++i) { + c = (data[i] >> 2) & 0x3f; + ret.append(cvt.charAt(c)); + c = (data[i] << 4) & 0x3f; + if (++i < len) + c |= (data[i] >> 4) & 0x0f; - ret.append(cvt.charAt(c)); - if (i < len) { - c = (data[i] << 2) & 0x3f; - if (++i < len) - c |= (data[i] >> 6) & 0x03; + ret.append(cvt.charAt(c)); + if (i < len) { + c = (data[i] << 2) & 0x3f; + if (++i < len) + c |= (data[i] >> 6) & 0x03; - ret.append(cvt.charAt(c)); - } - else { - ++i; - ret.append((char)fillchar); - } + ret.append(cvt.charAt(c)); + } else { + ++i; + ret.append((char) fillchar); + } - if (i < len) { - c = data[i] & 0x3f; - ret.append(cvt.charAt(c)); - } - else { - ret.append((char)fillchar); - } - } - return ret.toString(); + if (i < len) { + c = data[i] & 0x3f; + ret.append(cvt.charAt(c)); + } else { + ret.append((char) fillchar); + } + } + return ret.toString(); } /** * Decodes a base64 String. - * - * @param data a base64 encoded String to decode. + * + * @param data + * a base64 encoded String to decode. * @return the decoded String. */ public static String decodeBase64(String data) { - byte [] bytes = null; - try { - bytes = data.getBytes("ISO-8859-1"); - } - catch (UnsupportedEncodingException uee) { - // Nothing to do - } - return decodeBase64(bytes); + byte[] bytes = null; + try { + bytes = data.getBytes("ISO-8859-1"); + } catch (UnsupportedEncodingException uee) { + // Nothing to do + } + return decodeBase64(bytes); } /** * Decodes a base64 aray of bytes. - * - * @param data a base64 encode byte array to decode. + * + * @param data + * a base64 encode byte array to decode. * @return the decoded String. */ public static String decodeBase64(byte[] data) { - int c, c1; - int len = data.length; - StringBuffer ret = new StringBuffer((len * 3) / 4); - for (int i = 0; i < len; ++i) { - c = cvt.indexOf(data[i]); - ++i; - c1 = cvt.indexOf(data[i]); - c = ((c << 2) | ((c1 >> 4) & 0x3)); - ret.append((char)c); - if (++i < len) { - c = data[i]; - if (fillchar == c) - break; + int c, c1; + int len = data.length; + StringBuffer ret = new StringBuffer((len * 3) / 4); + for (int i = 0; i < len; ++i) { + c = cvt.indexOf(data[i]); + ++i; + c1 = cvt.indexOf(data[i]); + c = ((c << 2) | ((c1 >> 4) & 0x3)); + ret.append((char) c); + if (++i < len) { + c = data[i]; + if (fillchar == c) + break; - c = cvt.indexOf(c); - c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf); - ret.append((char)c1); - } + c = cvt.indexOf(c); + c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf); + ret.append((char) c1); + } - if (++i < len) { - c1 = data[i]; - if (fillchar == c1) - break; + if (++i < len) { + c1 = data[i]; + if (fillchar == c1) + break; - c1 = cvt.indexOf(c1); - c = ((c << 6) & 0xc0) | c1; - ret.append((char)c); - } - } - return ret.toString(); + c1 = cvt.indexOf(c1); + c = ((c << 6) & 0xc0) | c1; + ret.append((char) c); + } + } + return ret.toString(); } /** @@ -681,7 +668,7 @@ public class StringUtils { *

*

*

- * Copyright (c) 2002-2003 The Apache Software Foundation. All rights + * Copyright (c) 2002-2003 The Apache Software Foundation. All rights *

* reserved. *

@@ -749,7 +736,7 @@ public class StringUtils { *

* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE *

- * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR *

* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *

@@ -773,7 +760,7 @@ public class StringUtils { *

* This software consists of voluntary contributions made by many *

- * individuals on behalf of the Apache Software Foundation. For more + * individuals on behalf of the Apache Software Foundation. For more *

* information on the Apache Software Foundation, please see *

@@ -782,276 +769,269 @@ public class StringUtils { private static final BitSet allowed_query = new BitSet(256); static { - for (int i = '0'; i <= '9'; i++) { - allowed_query.set(i); - } + for (int i = '0'; i <= '9'; i++) { + allowed_query.set(i); + } - for (int i = 'a'; i <= 'z'; i++) { - allowed_query.set(i); - } + for (int i = 'a'; i <= 'z'; i++) { + allowed_query.set(i); + } - for (int i = 'A'; i <= 'Z'; i++) { - allowed_query.set(i); - } + for (int i = 'A'; i <= 'Z'; i++) { + allowed_query.set(i); + } - allowed_query.set('-'); - allowed_query.set('_'); - allowed_query.set('.'); - allowed_query.set('!'); - allowed_query.set('~'); - allowed_query.set('*'); - allowed_query.set('\''); - allowed_query.set('('); - allowed_query.set(')'); + allowed_query.set('-'); + allowed_query.set('_'); + allowed_query.set('.'); + allowed_query.set('!'); + allowed_query.set('~'); + allowed_query.set('*'); + allowed_query.set('\''); + allowed_query.set('('); + allowed_query.set(')'); } /** - * Encodes URI string. This is a replacement for the java.net.URLEncode#encode(String, String) - *

- * class which is broken under JDK 1.3. - *

- *

- * - * @param original the original character sequence - * @param charset the protocol charset + * Encodes URI string. This is a replacement for the + * java.net.URLEncode#encode(String, String) class which is broken under JDK + * 1.3. + * + * @param original + * the original character sequence + * @param charset + * the protocol charset * @return URI character sequence - * @throws UnsupportedEncodingException unsupported character encoding + * @throws UnsupportedEncodingException + * unsupported character encoding */ public static String URLEncode(String original, String charset) - throws UnsupportedEncodingException - { - // encode original to uri characters. - if (original == null) { - return null; - } - // escape octet to uri characters. - byte[] octets; + throws UnsupportedEncodingException { + // encode original to uri characters. + if (original == null) { + return null; + } + // escape octet to uri characters. + byte[] octets; - try { - octets = original.getBytes(charset); - } - catch (UnsupportedEncodingException error) { - throw new UnsupportedEncodingException(); - } + try { + octets = original.getBytes(charset); + } catch (UnsupportedEncodingException error) { + throw new UnsupportedEncodingException(); + } - StringBuffer buf = new StringBuffer(octets.length); - for (byte octet : octets) { - char c = (char) octet; - if (allowed_query.get(c)) { - buf.append(c); - } else { - buf.append('%'); - char hexadecimal = Character.forDigit((octet >> 4) & 0xF, 16); - buf.append(Character.toUpperCase(hexadecimal)); // high - hexadecimal = Character.forDigit(octet & 0xF, 16); - buf.append(Character.toUpperCase(hexadecimal)); // low - } - } + StringBuffer buf = new StringBuffer(octets.length); + for (byte octet : octets) { + char c = (char) octet; + if (allowed_query.get(c)) { + buf.append(c); + } else { + buf.append('%'); + char hexadecimal = Character.forDigit((octet >> 4) & 0xF, 16); + buf.append(Character.toUpperCase(hexadecimal)); // high + hexadecimal = Character.forDigit(octet & 0xF, 16); + buf.append(Character.toUpperCase(hexadecimal)); // low + } + } - return buf.toString(); + return buf.toString(); } private static final int fillchar = '='; private static final String cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - + "abcdefghijklmnopqrstuvwxyz" - + "0123456789+/"; + + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; /** * Converts a line of text into an array of lower case words using a + * {@link BreakIterator}.wordInstance(). *

- * BreakIterator.wordInstance().

- *

- *

- *

- * This method is under the Jive Open Source Software License and was - *

- * written by Mark Imbriaco. - * - * @param text a String of text to convert into an array of words + * This method is under the Jive Open Source Software License + * + * @author Mark Imbriaco. + * + * @param text + * a String of text to convert into an array of words * @return text broken up into an array of words. */ public static String[] toLowerCaseWordArray(String text) { - if (text == null || text.length() == 0) { - return new String[0]; - } + if (text == null || text.length() == 0) { + return new String[0]; + } - ArrayList wordList = new ArrayList(); - BreakIterator boundary = BreakIterator.getWordInstance(); - boundary.setText(text); - int start = 0; + ArrayList wordList = new ArrayList(); + BreakIterator boundary = BreakIterator.getWordInstance(); + boundary.setText(text); + int start = 0; - for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) { - String tmp = text.substring(start, end).trim(); - // Remove characters that are not needed. - tmp = replace(tmp, "+", ""); - tmp = replace(tmp, "/", ""); - tmp = replace(tmp, "\\", ""); - tmp = replace(tmp, "#", ""); - tmp = replace(tmp, "*", ""); - tmp = replace(tmp, ")", ""); - tmp = replace(tmp, "(", ""); - tmp = replace(tmp, "&", ""); - if (tmp.length() > 0) { - wordList.add(tmp); - } - } - return wordList.toArray(new String[wordList.size()]); + for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary + .next()) { + String tmp = text.substring(start, end).trim(); + // Remove characters that are not needed. + tmp = replace(tmp, "+", ""); + tmp = replace(tmp, "/", ""); + tmp = replace(tmp, "\\", ""); + tmp = replace(tmp, "#", ""); + tmp = replace(tmp, "*", ""); + tmp = replace(tmp, ")", ""); + tmp = replace(tmp, "(", ""); + tmp = replace(tmp, "&", ""); + if (tmp.length() > 0) { + wordList.add(tmp); + } + } + return wordList.toArray(new String[wordList.size()]); } /** - * Pseudo-random number generator object for use with randomString(). - *

- * The Random class is not considered to be cryptographically secure, so - *

- * only use these random Strings for low to medium security applications. + * Pseudo-random number generator object for use with randomString(). The + * Random class is not considered to be cryptographically secure, so only + * use these random Strings for low to medium security applications. */ private static Random randGen = new Random(); /** * Array of numbers and letters of mixed case. Numbers appear in the list - *

* twice so that there is a more equal chance that a number will be picked. - *

* We can use the array to get a random number or letter by picking a random - *

* array index. */ - private static char[] numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" + - "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray(); + private static char[] numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" + + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray(); /** - * Returns a random String of numbers and letters (lower and upper case) - *

- * of the specified length. The method uses the Random class that is - *

- * built-in to Java which is suitable for low to medium grade security uses. - *

- * This means that the output is only pseudo random, i.e., each number is - *

- * mathematically generated so is not truly random.

- *

+ * Returns a random String of numbers and letters (lower and upper case) of + * the specified length. The method uses the Random class that is built-in + * to Java which is suitable for low to medium grade security uses. This + * means that the output is only pseudo random, i.e., each number is + * mathematically generated so is not truly random. + *

*

*

* The specified length must be at least one. If not, the method will return - *

- * null. - * - * @param length the desired length of the random String to return. + * null. + * + * @param length + * the desired length of the random String to return. * @return a random String of numbers and letters of the specified length. */ public static String randomString(int length) { - if (length < 1) { - return null; - } - // Create a char buffer to put random letters and numbers in. - char [] randBuffer = new char[length]; - for (int i = 0; i < randBuffer.length; i++) { - randBuffer[i] = numbersAndLetters[randGen.nextInt(71)]; - } - return new String(randBuffer); + if (length < 1) { + return null; + } + // Create a char buffer to put random letters and numbers in. + char[] randBuffer = new char[length]; + for (int i = 0; i < randBuffer.length; i++) { + randBuffer[i] = numbersAndLetters[randGen.nextInt(71)]; + } + return new String(randBuffer); } /** * Intelligently chops a String at a word boundary (whitespace) that occurs - *

* at the specified index in the argument or before. However, if there is a - *

* newline character before length, the String will be chopped - *

* there. If no newline or whitespace is found in string up to - *

- * the index length, the String will chopped at length. - *

- *

+ * the index length, the String will chopped at + * length. *

* For example, chopAtWord("This is a nice String", 10, -1) will return *

* "This is a" which is the first word boundary less than or equal to 10 - *

* characters into the original String. - * - * @param string the String to chop. - * @param length the index in string to start looking for a - *

- * whitespace boundary at. - * @param minLength the minimum length the word should be chopped at. This is helpful - *

- * for words with no natural boundaries, ie: "thisisareallylonglonglongword". - *

- * This must be smaller than length and can be -1 if no minLength is wanted + * + * @param string + * the String to chop. + * @param length + * the index in string to start looking for a + *

+ * whitespace boundary at. + * @param minLength + * the minimum length the word should be chopped at. This is + * helpful + *

+ * for words with no natural boundaries, ie: + * "thisisareallylonglonglongword". + *

+ * This must be smaller than length and can be -1 if no minLength + * is wanted * @return a substring of string whose length is less than or *

* equal to length, and that is chopped at whitespace. */ public static String chopAtWord(String string, int length, int minLength) { - // guard clauses - if (length < 2) { - throw new IllegalArgumentException("Length specified (" + length + ") must be > 2"); - } - else if (minLength >= length) { - throw new IllegalArgumentException("minLength must be smaller than length"); - } + // guard clauses + if (length < 2) { + throw new IllegalArgumentException("Length specified (" + length + + ") must be > 2"); + } else if (minLength >= length) { + throw new IllegalArgumentException( + "minLength must be smaller than length"); + } - int sLength = (string == null) ? -1 : string.length(); - // shortcircuit clauses - if (sLength < 1) { - return string; - } - // minLength specified, string is smaller than the minLength, return the string - else if (minLength != -1 && sLength < minLength) { - return string; - } - // no minLength specified, string is smaller than length - else if (minLength == -1 && sLength < length) { - return string; - } + int sLength = (string == null) ? -1 : string.length(); + // shortcircuit clauses + if (sLength < 1) { + return string; + } + // minLength specified, string is smaller than the minLength, return the + // string + else if (minLength != -1 && sLength < minLength) { + return string; + } + // no minLength specified, string is smaller than length + else if (minLength == -1 && sLength < length) { + return string; + } - if (string == null) return null; - char [] charArray = string.toCharArray(); - // String is longer than the length specified, attempt to find a newline - // or a space - if (sLength > length) { - sLength = length; - // First check if there is a newline character before length; if so, - // chop word there. - for (int i = 0; i < sLength - 1; i++) { - // Windows - if (charArray[i] == '\r' && charArray[i + 1] == '\n') { - return string.substring(0, i + 1); - } - // Unix - else if (charArray[i] == '\n') { - return string.substring(0, i); - } - } - // Also check boundary case of Unix newline - if (charArray[sLength - 1] == '\n') { - return string.substring(0, sLength - 1); - } - // No newline, so chop at the first whitespace. - for (int i = sLength - 1; i > 0; i--) { - if (charArray[i] == ' ') { - return string.substring(0, i).trim(); - } - } - } - // String is shorter than length but longer than minLength, - // make sure there is a space in the string before minLength - else if (minLength != -1 && sLength > minLength) { - for (int i = 0; i < minLength; i++) { - if (charArray[i] == ' ') { - return string; - } - } - } - // Did not find a word boundary, so return a string at the min length, if a min - // length was specified: - if (minLength > -1 && minLength <= string.length()) { - return string.substring(0, minLength); - } - // Did not find word boundary or min length so return original String chopped at - // specified length. - return string.substring(0, length); + if (string == null) + return null; + char[] charArray = string.toCharArray(); + // String is longer than the length specified, attempt to find a newline + // or a space + if (sLength > length) { + sLength = length; + // First check if there is a newline character before length; if so, + // chop word there. + for (int i = 0; i < sLength - 1; i++) { + // Windows + if (charArray[i] == '\r' && charArray[i + 1] == '\n') { + return string.substring(0, i + 1); + } + // Unix + else if (charArray[i] == '\n') { + return string.substring(0, i); + } + } + // Also check boundary case of Unix newline + if (charArray[sLength - 1] == '\n') { + return string.substring(0, sLength - 1); + } + // No newline, so chop at the first whitespace. + for (int i = sLength - 1; i > 0; i--) { + if (charArray[i] == ' ') { + return string.substring(0, i).trim(); + } + } + } + // String is shorter than length but longer than minLength, + // make sure there is a space in the string before minLength + else if (minLength != -1 && sLength > minLength) { + for (int i = 0; i < minLength; i++) { + if (charArray[i] == ' ') { + return string; + } + } + } + // Did not find a word boundary, so return a string at the min length, + // if a min + // length was specified: + if (minLength > -1 && minLength <= string.length()) { + return string.substring(0, minLength); + } + // Did not find word boundary or min length so return original String + // chopped at + // specified length. + return string.substring(0, length); } /** @@ -1063,7 +1043,8 @@ public class StringUtils { *

* there. If no newline or whitespace is found in string up to *

- * the index length, the String will chopped at length. + * the index length, the String will chopped at + * length. *

*

*

@@ -1072,76 +1053,91 @@ public class StringUtils { * "This is a" which is the first word boundary less than or equal to 10 *

* characters into the original String. - * - * @param string the String to chop. - * @param length the index in string to start looking for a - *

- * whitespace boundary at. + * + * @param string + * the String to chop. + * @param length + * the index in string to start looking for a + *

+ * whitespace boundary at. * @return a substring of string whose length is less than or *

* equal to length, and that is chopped at whitespace. */ public static String chopAtWord(String string, int length) { - return chopAtWord(string, length, -1); + return chopAtWord(string, length, -1); } /** - * Returns a substring of the given string which represents the words around the given word. + * Returns a substring of the given string which represents the words around + * the given word. *

- * For example, passing in "This is a quick test a test", "{a,test}" and 5 would return a string + * For example, passing in "This is a quick test a test", "{a,test}" and 5 + * would return a string *

- * of "This is a quick" - that's 5 characters (or to the end of the word, whichever + * of "This is a quick" - that's 5 characters (or to the end of the word, + * whichever *

- * is greater) on either side of "a". Also, since {a,test} is passed in a "a" is found + * is greater) on either side of "a". Also, since {a,test} is passed in a + * "a" is found *

- * first in the string, we base the substring off of the position of "a". The wordList is + * first in the string, we base the substring off of the position of "a". + * The wordList is *

- * really just a list of strings to try - the first one found is used.

+ * really just a list of strings to try - the first one found is used. + *

*

*

*

* Note: The wordList passed in should be lowercase. - * - * @param input The string to parse. - * @param wordList The words to look for - the first one found in the string is used. - * @param numChars The number of characters on either side to include in the chop. - * @return a substring of the given string matching the criteria, otherwise "". + * + * @param input + * The string to parse. + * @param wordList + * The words to look for - the first one found in the string is + * used. + * @param numChars + * The number of characters on either side to include in the + * chop. + * @return a substring of the given string matching the criteria, otherwise + * "". */ - public static String chopAtWordsAround(String input, String[] wordList, int numChars) { - if (input == null || "".equals(input.trim()) || wordList == null - || wordList.length == 0 || numChars == 0) - { - return ""; - } - String lc = input.toLowerCase(); - for (String aWordList : wordList) { - int pos = lc.indexOf(aWordList); - if (pos > -1) { - int beginIdx = pos - numChars; - if (beginIdx < 0) { - beginIdx = 0; - } + public static String chopAtWordsAround(String input, String[] wordList, + int numChars) { + if (input == null || "".equals(input.trim()) || wordList == null + || wordList.length == 0 || numChars == 0) { + return ""; + } + String lc = input.toLowerCase(); + for (String aWordList : wordList) { + int pos = lc.indexOf(aWordList); + if (pos > -1) { + int beginIdx = pos - numChars; + if (beginIdx < 0) { + beginIdx = 0; + } - int endIdx = pos + numChars; + int endIdx = pos + numChars; - if (endIdx > input.length() - 1) { - endIdx = input.length() - 1; - } + if (endIdx > input.length() - 1) { + endIdx = input.length() - 1; + } - char[] chars = input.toCharArray(); + char[] chars = input.toCharArray(); - while (beginIdx > 0 && chars[beginIdx] != ' ' && chars[beginIdx] != '\n' && chars[beginIdx] != '\r') { - beginIdx--; - } - while (endIdx < input.length() && chars[endIdx] != ' ' - && chars[endIdx] != '\n' && chars[endIdx] != '\r') - { - endIdx++; - } - return input.substring(beginIdx, endIdx); - } - } - return input.substring(0, (input.length() >= 200) ? 200 : input.length()); + while (beginIdx > 0 && chars[beginIdx] != ' ' + && chars[beginIdx] != '\n' && chars[beginIdx] != '\r') { + beginIdx--; + } + while (endIdx < input.length() && chars[endIdx] != ' ' + && chars[endIdx] != '\n' && chars[endIdx] != '\r') { + endIdx++; + } + return input.substring(beginIdx, endIdx); + } + } + return input.substring(0, + (input.length() >= 200) ? 200 : input.length()); } /** @@ -1158,199 +1154,198 @@ public class StringUtils { * Please note that this method can be lossy - trailing spaces on wrapped *

* lines may be trimmed. - * - * @param input the String to reformat. - * @param width the maximum length of any one line. - * @param locale of the string to be wrapped. + * + * @param input + * the String to reformat. + * @param width + * the maximum length of any one line. + * @param locale + * of the string to be wrapped. * @return a new String with reformatted as needed. */ public static String wordWrap(String input, int width, Locale locale) { - // protect ourselves - if (input == null) { - return ""; - } - else if (width < 5) { - return input; - } - else if (width >= input.length()) { - return input; - } + // protect ourselves + if (input == null) { + return ""; + } else if (width < 5) { + return input; + } else if (width >= input.length()) { + return input; + } - StringBuffer buf = new StringBuffer(input); - boolean endOfLine = false; - int lineStart = 0; + StringBuffer buf = new StringBuffer(input); + boolean endOfLine = false; + int lineStart = 0; - for (int i = 0; i < buf.length(); i++) { - if (buf.charAt(i) == '\n') { - lineStart = i + 1; - endOfLine = true; - } - // handle splitting at width character - if (i > lineStart + width - 1) { - if (!endOfLine) { - int limit = i - lineStart - 1; - BreakIterator breaks = BreakIterator.getLineInstance(locale); - breaks.setText(buf.substring(lineStart, i)); - int end = breaks.last(); - // if the last character in the search string isn't a space, - // we can't split on it (looks bad). Search for a previous - // break character - if (end == limit + 1) { - if (!Character.isWhitespace(buf.charAt(lineStart + end))) { - end = breaks.preceding(end - 1); - } - } - // if the last character is a space, replace it with a \n - if (end != BreakIterator.DONE && end == limit + 1) { - buf.replace(lineStart + end, lineStart + end + 1, "\n"); - lineStart = lineStart + end; - } - // otherwise, just insert a \n - else if (end != BreakIterator.DONE && end != 0) { - buf.insert(lineStart + end, '\n'); - lineStart = lineStart + end + 1; - } - else { - buf.insert(i, '\n'); - lineStart = i + 1; - } - } - else { - buf.insert(i, '\n'); - lineStart = i + 1; - endOfLine = false; - } - } - } + for (int i = 0; i < buf.length(); i++) { + if (buf.charAt(i) == '\n') { + lineStart = i + 1; + endOfLine = true; + } + // handle splitting at width character + if (i > lineStart + width - 1) { + if (!endOfLine) { + int limit = i - lineStart - 1; + BreakIterator breaks = BreakIterator + .getLineInstance(locale); + breaks.setText(buf.substring(lineStart, i)); + int end = breaks.last(); + // if the last character in the search string isn't a space, + // we can't split on it (looks bad). Search for a previous + // break character + if (end == limit + 1) { + if (!Character + .isWhitespace(buf.charAt(lineStart + end))) { + end = breaks.preceding(end - 1); + } + } + // if the last character is a space, replace it with a \n + if (end != BreakIterator.DONE && end == limit + 1) { + buf.replace(lineStart + end, lineStart + end + 1, "\n"); + lineStart = lineStart + end; + } + // otherwise, just insert a \n + else if (end != BreakIterator.DONE && end != 0) { + buf.insert(lineStart + end, '\n'); + lineStart = lineStart + end + 1; + } else { + buf.insert(i, '\n'); + lineStart = i + 1; + } + } else { + buf.insert(i, '\n'); + lineStart = i + 1; + endOfLine = false; + } + } + } - return buf.toString(); + return buf.toString(); } /** * Highlights words in a string. Words matching ignores case. The actual - *

* higlighting method is specified with the start and end higlight tags. - *

- * Those might be beginning and ending HTML bold tags, or anything else.

- *

- *

- *

+ * Those might be beginning and ending HTML bold tags, or anything else. * This method is under the Jive Open Source Software License and was - *

* written by Mark Imbriaco. - * - * @param string the String to highlight words in. - * @param words an array of words that should be highlighted in the string. - * @param startHighlight the tag that should be inserted to start highlighting. - * @param endHighlight the tag that should be inserted to end highlighting. + * + * @param string + * the String to highlight words in. + * @param words + * an array of words that should be highlighted in the string. + * @param startHighlight + * the tag that should be inserted to start highlighting. + * @param endHighlight + * the tag that should be inserted to end highlighting. * @return a new String with the specified words highlighted. */ - public static String highlightWords(String string, String[] words, String startHighlight, String endHighlight) - { - if (string == null || words == null || startHighlight == null || endHighlight == null) { - return null; - } + public static String highlightWords(String string, String[] words, + String startHighlight, String endHighlight) { + if (string == null || words == null || startHighlight == null + || endHighlight == null) { + return null; + } - StringBuffer regexp = new StringBuffer(); - regexp.append("(?i)\\b("); - // Iterate through each word and generate a word list for the regexp. - for (int x = 0; x < words.length; x++) { - // Escape "$", "|", ".", "/", and "?" to keep us out of trouble in our regexp. - words[x] = words[x].replaceAll("([\\$\\?\\|\\/\\.])", "\\\\$1"); - regexp.append(words[x]); + StringBuffer regexp = new StringBuffer(); + regexp.append("(?i)\\b("); + // Iterate through each word and generate a word list for the regexp. + for (int x = 0; x < words.length; x++) { + // Escape "$", "|", ".", "/", and "?" to keep us out of trouble in + // our regexp. + words[x] = words[x].replaceAll("([\\$\\?\\|\\/\\.])", "\\\\$1"); + regexp.append(words[x]); - if (x != words.length - 1) { - regexp.append("|"); - } - } - regexp.append(")"); + if (x != words.length - 1) { + regexp.append("|"); + } + } + regexp.append(")"); - return string.replaceAll(regexp.toString(), startHighlight + "$1" + endHighlight); + return string.replaceAll(regexp.toString(), startHighlight + "$1" + + endHighlight); } /** * Escapes all necessary characters in the String so that it can be used *

* in an XML doc. - * - * @param string the string to escape. + * + * @param string + * the string to escape. * @return the string with appropriate characters escaped. */ public static String escapeForXML(String string) { - if (string == null) { - return null; - } - char ch; - int i = 0; - int last = 0; - char[] input = string.toCharArray(); - int len = input.length; - StringBuffer out = new StringBuffer((int)(len * 1.3)); - for (; i < len; i++) { - ch = input[i]; + if (string == null) { + return null; + } + char ch; + int i = 0; + int last = 0; + char[] input = string.toCharArray(); + int len = input.length; + StringBuffer out = new StringBuffer((int) (len * 1.3)); + for (; i < len; i++) { + ch = input[i]; - if (ch > '>') { - // Nothing to do - } - else if (ch == '<') { - if (i > last) { - out.append(input, last, i - last); - } - last = i + 1; - out.append(LT_ENCODE); - } - else if (ch == '&') { - if (i > last) { - out.append(input, last, i - last); - } - last = i + 1; - out.append(AMP_ENCODE); - } - else if (ch == '"') { - if (i > last) { - out.append(input, last, i - last); - } - last = i + 1; - out.append(QUOTE_ENCODE); - } - else if (ch == 10 || ch == 13 || ch == 9) { - // Nothing to do - } - else if (ch < 32) { - // Disallow all ASCII control characters, except space, - // enter characters and tabs: - if (i > last) { - out.append(input, last, i - last); - } - last = i + 1; - } - } - if (last == 0) { - return string; - } - if (i > last) { - out.append(input, last, i - last); - } - return out.toString(); + if (ch > '>') { + // Nothing to do + } else if (ch == '<') { + if (i > last) { + out.append(input, last, i - last); + } + last = i + 1; + out.append(LT_ENCODE); + } else if (ch == '&') { + if (i > last) { + out.append(input, last, i - last); + } + last = i + 1; + out.append(AMP_ENCODE); + } else if (ch == '"') { + if (i > last) { + out.append(input, last, i - last); + } + last = i + 1; + out.append(QUOTE_ENCODE); + } else if (ch == 10 || ch == 13 || ch == 9) { + // Nothing to do + } else if (ch < 32) { + // Disallow all ASCII control characters, except space, + // enter characters and tabs: + if (i > last) { + out.append(input, last, i - last); + } + last = i + 1; + } + } + if (last == 0) { + return string; + } + if (i > last) { + out.append(input, last, i - last); + } + return out.toString(); } /** * Unescapes the String by converting XML escape sequences back into normal *

* characters. - * - * @param string the string to unescape. + * + * @param string + * the string to unescape. * @return the string with appropriate characters unescaped. */ public static String unescapeFromXML(String string) { - string = replace(string, "<", "<"); - string = replace(string, ">", ">"); - string = replace(string, """, "\""); - return replace(string, "&", "&"); + string = replace(string, "<", "<"); + string = replace(string, ">", ">"); + string = replace(string, """, "\""); + return replace(string, "&", "&"); } - private static final char[] zeroArray = - "0000000000000000000000000000000000000000000000000000000000000000".toCharArray(); + private static final char[] zeroArray = "0000000000000000000000000000000000000000000000000000000000000000" + .toCharArray(); /** * Pads the supplied String with 0's to the specified length and returns @@ -1366,567 +1361,566 @@ public class StringUtils { * implementation of this method allows for a maximum length of *

* 64. - * - * @param string the original String to pad. - * @param length the desired length of the new padded String. + * + * @param string + * the original String to pad. + * @param length + * the desired length of the new padded String. * @return a new String padded with the required number of 0's. */ public static String zeroPadString(String string, int length) { - if (string == null || string.length() > length) { - return string; - } - StringBuffer buf = new StringBuffer(length); - buf.append(zeroArray, 0, length - string.length()).append(string); - return buf.toString(); + if (string == null || string.length() > length) { + return string; + } + StringBuffer buf = new StringBuffer(length); + buf.append(zeroArray, 0, length - string.length()).append(string); + return buf.toString(); } /** - * Formats a Date as a String. Depending on how Dates are defined in the database + * Formats a Date as a String. Depending on how Dates are defined in the + * database *

- * (character data or numberic), the format return will either be a fifteen character long String + * (character data or numberic), the format return will either be a fifteen + * character long String *

- * made up of the Date's padded millisecond value, or will simply be the Date's millesecond value. - * - * @param date Date to convert to milliseconds. + * made up of the Date's padded millisecond value, or will simply be the + * Date's millesecond value. + * + * @param date + * Date to convert to milliseconds. * @return a Date encoded as a String. */ public static String dateToMillis(Date date) { - return Long.toString(date.getTime()); + return Long.toString(date.getTime()); } /** - * Validate an email address. This isn't 100% perfect but should handle just about everything + * Validate an email address. This isn't 100% perfect but should handle just + * about everything *

* that is in common use. - * - * @param addr the email address to validate + * + * @param addr + * the email address to validate * @return true if the address is valid, false otherwise */ public static boolean isValidEmailAddress(String addr) { - if (addr == null) { - return false; - } + if (addr == null) { + return false; + } - addr = addr.trim(); + addr = addr.trim(); - if (addr.length() == 0) { - return false; - } - // basic address check - Matcher matcher = basicAddressPattern.matcher(addr); - if (!matcher.matches()) { - return false; - } - String userPart = matcher.group(1); - String domainPart = matcher.group(2); - // user address check - matcher = validUserPattern.matcher(userPart); - if (!matcher.matches()) { - return false; - } - // ip domain check - matcher = ipDomainPattern.matcher(domainPart); - if (matcher.matches()) { - // if the pattern matched, check to make sure that the ip range is valid - for (int i = 1; i < 5; i++) { - String num = matcher.group(i); + if (addr.length() == 0) { + return false; + } + // basic address check + Matcher matcher = basicAddressPattern.matcher(addr); + if (!matcher.matches()) { + return false; + } + String userPart = matcher.group(1); + String domainPart = matcher.group(2); + // user address check + matcher = validUserPattern.matcher(userPart); + if (!matcher.matches()) { + return false; + } + // ip domain check + matcher = ipDomainPattern.matcher(domainPart); + if (matcher.matches()) { + // if the pattern matched, check to make sure that the ip range is + // valid + for (int i = 1; i < 5; i++) { + String num = matcher.group(i); - if (num == null) { - return false; - } + if (num == null) { + return false; + } - if (Integer.parseInt(num) > 254) { - return false; - } - } - return true; - } - // symbolic domain check - matcher = domainPattern.matcher(domainPart); - if (matcher.matches()) { - String tld = matcher.group(matcher.groupCount()); - // Permit top-level-domains of 3 (includes dot separator) because these could be - // country codes which we are not going to check for. - matcher = tldPattern.matcher(tld); - if (tld.length() != 3 && !matcher.matches()) { - return false; - } - } - else { - return false; - } - // all tests passed - return true; + if (Integer.parseInt(num) > 254) { + return false; + } + } + return true; + } + // symbolic domain check + matcher = domainPattern.matcher(domainPart); + if (matcher.matches()) { + String tld = matcher.group(matcher.groupCount()); + // Permit top-level-domains of 3 (includes dot separator) because + // these could be + // country codes which we are not going to check for. + matcher = tldPattern.matcher(tld); + if (tld.length() != 3 && !matcher.matches()) { + return false; + } + } else { + return false; + } + // all tests passed + return true; } // Testing method /* - public static void main(String[] args) { - { - String test1 = "The quick brown fox jumped"; - int chop11 = test1.length(); - int chop12 = test1.length() - 1; - int chop13 = test1.length() - 3; - int chop14 = test1.length() - "jumped".length(); - int chop15 = test1.length() - "ox jumped".length(); - // run test 1 - String result11 = chopAtWord(test1, chop11); - String result12 = chopAtWord(test1, chop12); - String result13 = chopAtWord(test1, chop13); - String result14 = chopAtWord(test1, chop14); - String result15 = chopAtWord(test1, chop15); - // print results - if (test1.equals(result11)) { System.err.println("Test 1.1 passed, result: " + result11); } - else { System.err.println("Test 1.1 failed, result: " + result11); } - - if ("The quick brown fox".equals(result12)) { System.err.println("Test 1.2 passed, result: " + result12); } - else { System.err.println("Test 1.2 failed, result: " + result12); } - - if ("The quick brown fox".equals(result13)) { System.err.println("Test 1.3 passed, result: " + result13); } - else { System.err.println("Test 1.3 failed, result: " + result13); } - - if ("The quick brown fox".equals(result14)) { System.err.println("Test 1.4 passed, result: " + result14); } - else { System.err.println("Test 1.4 failed, result: " + result14); } - - if ("The quick brown".equals(result15)) { System.err.println("Test 1.5 passed, result: " + result15); } - else { System.err.println("Test 1.5 failed, result: " + result15); } - } - - System.err.println(""); - - { - String test2 = "The quick brown fox jumped"; - int chop21 = test2.length(); - int chop22 = test2.length() - 1; - int chop23 = test2.length() - 3; - int chop24 = test2.length() - "jumped".length(); - int chop25 = test2.length() - "ox jumped".length(); - // run test 1 - String result21 = chopAtWord(test2, chop21, 0); - String result22 = chopAtWord(test2, chop22, 0); - String result23 = chopAtWord(test2, chop23, 0); - String result24 = chopAtWord(test2, chop24, 0); - String result25 = chopAtWord(test2, chop25, 0); - // print results - if (test2.equals(result21)) { System.err.println("Test 2.1 passed, result: " + result21); } - else { System.err.println("Test 2.1 failed, result: " + result21); } - - if ("The quick brown fox".equals(result22)) { System.err.println("Test 2.2 passed, result: " + result22); } - else { System.err.println("Test 2.2 failed, result: " + result22); } - - if ("The quick brown fox".equals(result23)) { System.err.println("Test 2.3 passed, result: " + result23); } - else { System.err.println("Test 2.3 failed, result: " + result23); } - - if ("The quick brown fox".equals(result24)) { System.err.println("Test 2.4 passed, result: " + result24); } - else { System.err.println("Test 2.4 failed, result: " + result24); } - - if ("The quick brown".equals(result25)) { System.err.println("Test 2.5 passed, result: " + result25); } - else { System.err.println("Test 2.5 failed, result: " + result25); } - } - - System.err.println(""); - - { - String test3 = "Thequickbrownfoxjumped"; - int chop31 = test3.length(); - int chop32 = test3.length() - 1; - int chop33 = test3.length() - 3; - int chop34 = test3.length() - "jumped".length(); - int chop35 = test3.length() - "ox jumped".length(); - // run test 1 - String result31 = chopAtWord(test3, chop31, "Thequickbrownfoxjumped".length()); - String result32 = chopAtWord(test3, chop32, "Thequick".length()); - String result33 = chopAtWord(test3, chop33, "Thequick".length()); - String result34 = chopAtWord(test3, chop34, "Thequick".length()); - String result35 = chopAtWord(test3, chop35, "Thequick".length()); - // print results - if ("Thequick".equals(result31)) { System.err.println("Test 3.1 passed, result: " + result31); } - else { System.err.println("Test 3.1 failed, result: " + result31); } - - if ("Thequick".equals(result32)) { System.err.println("Test 3.2 passed, result: " + result32); } - else { System.err.println("Test 3.2 failed, result: " + result32); } - - if ("Thequick".equals(result33)) { System.err.println("Test 3.3 passed, result: " + result33); } - else { System.err.println("Test 3.3 failed, result: " + result33); } - - if ("Thequick".equals(result34)) { System.err.println("Test 3.4 passed, result: " + result34); } - else { System.err.println("Test 3.4 failed, result: " + result34); } - - if ("Thequick".equals(result35)) { System.err.println("Test 3.5 passed, result: " + result35); } - else { System.err.println("Test 3.5 failed, result: " + result35); } - } - - System.err.println(""); - - { - String test4 = "Java.Lang.ClassNotFoundException:com.Citrix"; - int length = test4.length()-3; - int min = 20; - String result = chopAtWord(test4, length, min); - System.err.println("result: " + result); - } - } - */ + * public static void main(String[] args) { { String test1 = + * "The quick brown fox jumped"; int chop11 = test1.length(); int chop12 = + * test1.length() - 1; int chop13 = test1.length() - 3; int chop14 = + * test1.length() - "jumped".length(); int chop15 = test1.length() - + * "ox jumped".length(); // run test 1 String result11 = chopAtWord(test1, + * chop11); String result12 = chopAtWord(test1, chop12); String result13 = + * chopAtWord(test1, chop13); String result14 = chopAtWord(test1, chop14); + * String result15 = chopAtWord(test1, chop15); // print results if + * (test1.equals(result11)) { System.err.println("Test 1.1 passed, result: " + * + result11); } else { System.err.println("Test 1.1 failed, result: " + + * result11); } + * + * if ("The quick brown fox".equals(result12)) { + * System.err.println("Test 1.2 passed, result: " + result12); } else { + * System.err.println("Test 1.2 failed, result: " + result12); } + * + * if ("The quick brown fox".equals(result13)) { + * System.err.println("Test 1.3 passed, result: " + result13); } else { + * System.err.println("Test 1.3 failed, result: " + result13); } + * + * if ("The quick brown fox".equals(result14)) { + * System.err.println("Test 1.4 passed, result: " + result14); } else { + * System.err.println("Test 1.4 failed, result: " + result14); } + * + * if ("The quick brown".equals(result15)) { + * System.err.println("Test 1.5 passed, result: " + result15); } else { + * System.err.println("Test 1.5 failed, result: " + result15); } } + * + * System.err.println(""); + * + * { String test2 = "The quick brown fox jumped"; int chop21 = + * test2.length(); int chop22 = test2.length() - 1; int chop23 = + * test2.length() - 3; int chop24 = test2.length() - "jumped".length(); int + * chop25 = test2.length() - "ox jumped".length(); // run test 1 String + * result21 = chopAtWord(test2, chop21, 0); String result22 = + * chopAtWord(test2, chop22, 0); String result23 = chopAtWord(test2, chop23, + * 0); String result24 = chopAtWord(test2, chop24, 0); String result25 = + * chopAtWord(test2, chop25, 0); // print results if + * (test2.equals(result21)) { System.err.println("Test 2.1 passed, result: " + * + result21); } else { System.err.println("Test 2.1 failed, result: " + + * result21); } + * + * if ("The quick brown fox".equals(result22)) { + * System.err.println("Test 2.2 passed, result: " + result22); } else { + * System.err.println("Test 2.2 failed, result: " + result22); } + * + * if ("The quick brown fox".equals(result23)) { + * System.err.println("Test 2.3 passed, result: " + result23); } else { + * System.err.println("Test 2.3 failed, result: " + result23); } + * + * if ("The quick brown fox".equals(result24)) { + * System.err.println("Test 2.4 passed, result: " + result24); } else { + * System.err.println("Test 2.4 failed, result: " + result24); } + * + * if ("The quick brown".equals(result25)) { + * System.err.println("Test 2.5 passed, result: " + result25); } else { + * System.err.println("Test 2.5 failed, result: " + result25); } } + * + * System.err.println(""); + * + * { String test3 = "Thequickbrownfoxjumped"; int chop31 = test3.length(); + * int chop32 = test3.length() - 1; int chop33 = test3.length() - 3; int + * chop34 = test3.length() - "jumped".length(); int chop35 = test3.length() + * - "ox jumped".length(); // run test 1 String result31 = chopAtWord(test3, + * chop31, "Thequickbrownfoxjumped".length()); String result32 = + * chopAtWord(test3, chop32, "Thequick".length()); String result33 = + * chopAtWord(test3, chop33, "Thequick".length()); String result34 = + * chopAtWord(test3, chop34, "Thequick".length()); String result35 = + * chopAtWord(test3, chop35, "Thequick".length()); // print results if + * ("Thequick".equals(result31)) { + * System.err.println("Test 3.1 passed, result: " + result31); } else { + * System.err.println("Test 3.1 failed, result: " + result31); } + * + * if ("Thequick".equals(result32)) { + * System.err.println("Test 3.2 passed, result: " + result32); } else { + * System.err.println("Test 3.2 failed, result: " + result32); } + * + * if ("Thequick".equals(result33)) { + * System.err.println("Test 3.3 passed, result: " + result33); } else { + * System.err.println("Test 3.3 failed, result: " + result33); } + * + * if ("Thequick".equals(result34)) { + * System.err.println("Test 3.4 passed, result: " + result34); } else { + * System.err.println("Test 3.4 failed, result: " + result34); } + * + * if ("Thequick".equals(result35)) { + * System.err.println("Test 3.5 passed, result: " + result35); } else { + * System.err.println("Test 3.5 failed, result: " + result35); } } + * + * System.err.println(""); + * + * { String test4 = "Java.Lang.ClassNotFoundException:com.Citrix"; int + * length = test4.length()-3; int min = 20; String result = + * chopAtWord(test4, length, min); System.err.println("result: " + result); + * } } + */ public static String keyStroke2String(KeyStroke key) { - StringBuffer s = new StringBuffer(50); - int m = key.getModifiers(); + StringBuffer s = new StringBuffer(50); + int m = key.getModifiers(); - if ((m & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) { - s.append("shift "); - } - if ((m & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) { - s.append("ctrl "); - } - if ((m & (InputEvent.META_DOWN_MASK | InputEvent.META_MASK)) != 0) { - s.append("meta "); - } - if ((m & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) { - s.append("alt "); - } - if ((m & (InputEvent.BUTTON1_DOWN_MASK | InputEvent.BUTTON1_MASK)) != 0) { - s.append("button1 "); - } - if ((m & (InputEvent.BUTTON2_DOWN_MASK | InputEvent.BUTTON2_MASK)) != 0) { - s.append("button2 "); - } - if ((m & (InputEvent.BUTTON3_DOWN_MASK | InputEvent.BUTTON3_MASK)) != 0) { - s.append("button3 "); - } + if ((m & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) { + s.append("shift "); + } + if ((m & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) { + s.append("ctrl "); + } + if ((m & (InputEvent.META_DOWN_MASK | InputEvent.META_MASK)) != 0) { + s.append("meta "); + } + if ((m & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) { + s.append("alt "); + } + if ((m & (InputEvent.BUTTON1_DOWN_MASK | InputEvent.BUTTON1_MASK)) != 0) { + s.append("button1 "); + } + if ((m & (InputEvent.BUTTON2_DOWN_MASK | InputEvent.BUTTON2_MASK)) != 0) { + s.append("button2 "); + } + if ((m & (InputEvent.BUTTON3_DOWN_MASK | InputEvent.BUTTON3_MASK)) != 0) { + s.append("button3 "); + } - switch (key.getKeyEventType()) { - case KeyEvent.KEY_TYPED: - s.append("typed "); - s.append(key.getKeyChar()).append(" "); - break; - case KeyEvent.KEY_PRESSED: - s.append("pressed "); - s.append(getKeyText(key.getKeyCode())).append(" "); - break; - case KeyEvent.KEY_RELEASED: - s.append("released "); - s.append(getKeyText(key.getKeyCode())).append(" "); - break; - default: - s.append("unknown-event-type "); - break; - } + switch (key.getKeyEventType()) { + case KeyEvent.KEY_TYPED: + s.append("typed "); + s.append(key.getKeyChar()).append(" "); + break; + case KeyEvent.KEY_PRESSED: + s.append("pressed "); + s.append(getKeyText(key.getKeyCode())).append(" "); + break; + case KeyEvent.KEY_RELEASED: + s.append("released "); + s.append(getKeyText(key.getKeyCode())).append(" "); + break; + default: + s.append("unknown-event-type "); + break; + } - return s.toString(); + return s.toString(); } public static String getKeyText(int keyCode) { - if (keyCode >= KeyEvent.VK_0 && keyCode <= KeyEvent.VK_9 || - keyCode >= KeyEvent.VK_A && keyCode <= KeyEvent.VK_Z) { - return String.valueOf((char)keyCode); - } + if (keyCode >= KeyEvent.VK_0 && keyCode <= KeyEvent.VK_9 + || keyCode >= KeyEvent.VK_A && keyCode <= KeyEvent.VK_Z) { + return String.valueOf((char) keyCode); + } - switch (keyCode) { - case KeyEvent.VK_COMMA: - return "COMMA"; - case KeyEvent.VK_PERIOD: - return "PERIOD"; - case KeyEvent.VK_SLASH: - return "SLASH"; - case KeyEvent.VK_SEMICOLON: - return "SEMICOLON"; - case KeyEvent.VK_EQUALS: - return "EQUALS"; - case KeyEvent.VK_OPEN_BRACKET: - return "OPEN_BRACKET"; - case KeyEvent.VK_BACK_SLASH: - return "BACK_SLASH"; - case KeyEvent.VK_CLOSE_BRACKET: - return "CLOSE_BRACKET"; + switch (keyCode) { + case KeyEvent.VK_COMMA: + return "COMMA"; + case KeyEvent.VK_PERIOD: + return "PERIOD"; + case KeyEvent.VK_SLASH: + return "SLASH"; + case KeyEvent.VK_SEMICOLON: + return "SEMICOLON"; + case KeyEvent.VK_EQUALS: + return "EQUALS"; + case KeyEvent.VK_OPEN_BRACKET: + return "OPEN_BRACKET"; + case KeyEvent.VK_BACK_SLASH: + return "BACK_SLASH"; + case KeyEvent.VK_CLOSE_BRACKET: + return "CLOSE_BRACKET"; - case KeyEvent.VK_ENTER: - return "ENTER"; - case KeyEvent.VK_BACK_SPACE: - return "BACK_SPACE"; - case KeyEvent.VK_TAB: - return "TAB"; - case KeyEvent.VK_CANCEL: - return "CANCEL"; - case KeyEvent.VK_CLEAR: - return "CLEAR"; - case KeyEvent.VK_SHIFT: - return "SHIFT"; - case KeyEvent.VK_CONTROL: - return "CONTROL"; - case KeyEvent.VK_ALT: - return "ALT"; - case KeyEvent.VK_PAUSE: - return "PAUSE"; - case KeyEvent.VK_CAPS_LOCK: - return "CAPS_LOCK"; - case KeyEvent.VK_ESCAPE: - return "ESCAPE"; - case KeyEvent.VK_SPACE: - return "SPACE"; - case KeyEvent.VK_PAGE_UP: - return "PAGE_UP"; - case KeyEvent.VK_PAGE_DOWN: - return "PAGE_DOWN"; - case KeyEvent.VK_END: - return "END"; - case KeyEvent.VK_HOME: - return "HOME"; - case KeyEvent.VK_LEFT: - return "LEFT"; - case KeyEvent.VK_UP: - return "UP"; - case KeyEvent.VK_RIGHT: - return "RIGHT"; - case KeyEvent.VK_DOWN: - return "DOWN"; + case KeyEvent.VK_ENTER: + return "ENTER"; + case KeyEvent.VK_BACK_SPACE: + return "BACK_SPACE"; + case KeyEvent.VK_TAB: + return "TAB"; + case KeyEvent.VK_CANCEL: + return "CANCEL"; + case KeyEvent.VK_CLEAR: + return "CLEAR"; + case KeyEvent.VK_SHIFT: + return "SHIFT"; + case KeyEvent.VK_CONTROL: + return "CONTROL"; + case KeyEvent.VK_ALT: + return "ALT"; + case KeyEvent.VK_PAUSE: + return "PAUSE"; + case KeyEvent.VK_CAPS_LOCK: + return "CAPS_LOCK"; + case KeyEvent.VK_ESCAPE: + return "ESCAPE"; + case KeyEvent.VK_SPACE: + return "SPACE"; + case KeyEvent.VK_PAGE_UP: + return "PAGE_UP"; + case KeyEvent.VK_PAGE_DOWN: + return "PAGE_DOWN"; + case KeyEvent.VK_END: + return "END"; + case KeyEvent.VK_HOME: + return "HOME"; + case KeyEvent.VK_LEFT: + return "LEFT"; + case KeyEvent.VK_UP: + return "UP"; + case KeyEvent.VK_RIGHT: + return "RIGHT"; + case KeyEvent.VK_DOWN: + return "DOWN"; - // numpad numeric keys handled below - case KeyEvent.VK_MULTIPLY: - return "MULTIPLY"; - case KeyEvent.VK_ADD: - return "ADD"; - case KeyEvent.VK_SEPARATOR: - return "SEPARATOR"; - case KeyEvent.VK_SUBTRACT: - return "SUBTRACT"; - case KeyEvent.VK_DECIMAL: - return "DECIMAL"; - case KeyEvent.VK_DIVIDE: - return "DIVIDE"; - case KeyEvent.VK_DELETE: - return "DELETE"; - case KeyEvent.VK_NUM_LOCK: - return "NUM_LOCK"; - case KeyEvent.VK_SCROLL_LOCK: - return "SCROLL_LOCK"; + // numpad numeric keys handled below + case KeyEvent.VK_MULTIPLY: + return "MULTIPLY"; + case KeyEvent.VK_ADD: + return "ADD"; + case KeyEvent.VK_SEPARATOR: + return "SEPARATOR"; + case KeyEvent.VK_SUBTRACT: + return "SUBTRACT"; + case KeyEvent.VK_DECIMAL: + return "DECIMAL"; + case KeyEvent.VK_DIVIDE: + return "DIVIDE"; + case KeyEvent.VK_DELETE: + return "DELETE"; + case KeyEvent.VK_NUM_LOCK: + return "NUM_LOCK"; + case KeyEvent.VK_SCROLL_LOCK: + return "SCROLL_LOCK"; - case KeyEvent.VK_F1: - return "F1"; - case KeyEvent.VK_F2: - return "F2"; - case KeyEvent.VK_F3: - return "F3"; - case KeyEvent.VK_F4: - return "F4"; - case KeyEvent.VK_F5: - return "F5"; - case KeyEvent.VK_F6: - return "F6"; - case KeyEvent.VK_F7: - return "F7"; - case KeyEvent.VK_F8: - return "F8"; - case KeyEvent.VK_F9: - return "F9"; - case KeyEvent.VK_F10: - return "F10"; - case KeyEvent.VK_F11: - return "F11"; - case KeyEvent.VK_F12: - return "F12"; - case KeyEvent.VK_F13: - return "F13"; - case KeyEvent.VK_F14: - return "F14"; - case KeyEvent.VK_F15: - return "F15"; - case KeyEvent.VK_F16: - return "F16"; - case KeyEvent.VK_F17: - return "F17"; - case KeyEvent.VK_F18: - return "F18"; - case KeyEvent.VK_F19: - return "F19"; - case KeyEvent.VK_F20: - return "F20"; - case KeyEvent.VK_F21: - return "F21"; - case KeyEvent.VK_F22: - return "F22"; - case KeyEvent.VK_F23: - return "F23"; - case KeyEvent.VK_F24: - return "F24"; + case KeyEvent.VK_F1: + return "F1"; + case KeyEvent.VK_F2: + return "F2"; + case KeyEvent.VK_F3: + return "F3"; + case KeyEvent.VK_F4: + return "F4"; + case KeyEvent.VK_F5: + return "F5"; + case KeyEvent.VK_F6: + return "F6"; + case KeyEvent.VK_F7: + return "F7"; + case KeyEvent.VK_F8: + return "F8"; + case KeyEvent.VK_F9: + return "F9"; + case KeyEvent.VK_F10: + return "F10"; + case KeyEvent.VK_F11: + return "F11"; + case KeyEvent.VK_F12: + return "F12"; + case KeyEvent.VK_F13: + return "F13"; + case KeyEvent.VK_F14: + return "F14"; + case KeyEvent.VK_F15: + return "F15"; + case KeyEvent.VK_F16: + return "F16"; + case KeyEvent.VK_F17: + return "F17"; + case KeyEvent.VK_F18: + return "F18"; + case KeyEvent.VK_F19: + return "F19"; + case KeyEvent.VK_F20: + return "F20"; + case KeyEvent.VK_F21: + return "F21"; + case KeyEvent.VK_F22: + return "F22"; + case KeyEvent.VK_F23: + return "F23"; + case KeyEvent.VK_F24: + return "F24"; - case KeyEvent.VK_PRINTSCREEN: - return "PRINTSCREEN"; - case KeyEvent.VK_INSERT: - return "INSERT"; - case KeyEvent.VK_HELP: - return "HELP"; - case KeyEvent.VK_META: - return "META"; - case KeyEvent.VK_BACK_QUOTE: - return "BACK_QUOTE"; - case KeyEvent.VK_QUOTE: - return "QUOTE"; + case KeyEvent.VK_PRINTSCREEN: + return "PRINTSCREEN"; + case KeyEvent.VK_INSERT: + return "INSERT"; + case KeyEvent.VK_HELP: + return "HELP"; + case KeyEvent.VK_META: + return "META"; + case KeyEvent.VK_BACK_QUOTE: + return "BACK_QUOTE"; + case KeyEvent.VK_QUOTE: + return "QUOTE"; - case KeyEvent.VK_KP_UP: - return "KP_UP"; - case KeyEvent.VK_KP_DOWN: - return "KP_DOWN"; - case KeyEvent.VK_KP_LEFT: - return "KP_LEFT"; - case KeyEvent.VK_KP_RIGHT: - return "KP_RIGHT"; + case KeyEvent.VK_KP_UP: + return "KP_UP"; + case KeyEvent.VK_KP_DOWN: + return "KP_DOWN"; + case KeyEvent.VK_KP_LEFT: + return "KP_LEFT"; + case KeyEvent.VK_KP_RIGHT: + return "KP_RIGHT"; - case KeyEvent.VK_DEAD_GRAVE: - return "DEAD_GRAVE"; - case KeyEvent.VK_DEAD_ACUTE: - return "DEAD_ACUTE"; - case KeyEvent.VK_DEAD_CIRCUMFLEX: - return "DEAD_CIRCUMFLEX"; - case KeyEvent.VK_DEAD_TILDE: - return "DEAD_TILDE"; - case KeyEvent.VK_DEAD_MACRON: - return "DEAD_MACRON"; - case KeyEvent.VK_DEAD_BREVE: - return "DEAD_BREVE"; - case KeyEvent.VK_DEAD_ABOVEDOT: - return "DEAD_ABOVEDOT"; - case KeyEvent.VK_DEAD_DIAERESIS: - return "DEAD_DIAERESIS"; - case KeyEvent.VK_DEAD_ABOVERING: - return "DEAD_ABOVERING"; - case KeyEvent.VK_DEAD_DOUBLEACUTE: - return "DEAD_DOUBLEACUTE"; - case KeyEvent.VK_DEAD_CARON: - return "DEAD_CARON"; - case KeyEvent.VK_DEAD_CEDILLA: - return "DEAD_CEDILLA"; - case KeyEvent.VK_DEAD_OGONEK: - return "DEAD_OGONEK"; - case KeyEvent.VK_DEAD_IOTA: - return "DEAD_IOTA"; - case KeyEvent.VK_DEAD_VOICED_SOUND: - return "DEAD_VOICED_SOUND"; - case KeyEvent.VK_DEAD_SEMIVOICED_SOUND: - return "DEAD_SEMIVOICED_SOUND"; + case KeyEvent.VK_DEAD_GRAVE: + return "DEAD_GRAVE"; + case KeyEvent.VK_DEAD_ACUTE: + return "DEAD_ACUTE"; + case KeyEvent.VK_DEAD_CIRCUMFLEX: + return "DEAD_CIRCUMFLEX"; + case KeyEvent.VK_DEAD_TILDE: + return "DEAD_TILDE"; + case KeyEvent.VK_DEAD_MACRON: + return "DEAD_MACRON"; + case KeyEvent.VK_DEAD_BREVE: + return "DEAD_BREVE"; + case KeyEvent.VK_DEAD_ABOVEDOT: + return "DEAD_ABOVEDOT"; + case KeyEvent.VK_DEAD_DIAERESIS: + return "DEAD_DIAERESIS"; + case KeyEvent.VK_DEAD_ABOVERING: + return "DEAD_ABOVERING"; + case KeyEvent.VK_DEAD_DOUBLEACUTE: + return "DEAD_DOUBLEACUTE"; + case KeyEvent.VK_DEAD_CARON: + return "DEAD_CARON"; + case KeyEvent.VK_DEAD_CEDILLA: + return "DEAD_CEDILLA"; + case KeyEvent.VK_DEAD_OGONEK: + return "DEAD_OGONEK"; + case KeyEvent.VK_DEAD_IOTA: + return "DEAD_IOTA"; + case KeyEvent.VK_DEAD_VOICED_SOUND: + return "DEAD_VOICED_SOUND"; + case KeyEvent.VK_DEAD_SEMIVOICED_SOUND: + return "DEAD_SEMIVOICED_SOUND"; - case KeyEvent.VK_AMPERSAND: - return "AMPERSAND"; - case KeyEvent.VK_ASTERISK: - return "ASTERISK"; - case KeyEvent.VK_QUOTEDBL: - return "QUOTEDBL"; - case KeyEvent.VK_LESS: - return "LESS"; - case KeyEvent.VK_GREATER: - return "GREATER"; - case KeyEvent.VK_BRACELEFT: - return "BRACELEFT"; - case KeyEvent.VK_BRACERIGHT: - return "BRACERIGHT"; - case KeyEvent.VK_AT: - return "AT"; - case KeyEvent.VK_COLON: - return "COLON"; - case KeyEvent.VK_CIRCUMFLEX: - return "CIRCUMFLEX"; - case KeyEvent.VK_DOLLAR: - return "DOLLAR"; - case KeyEvent.VK_EURO_SIGN: - return "EURO_SIGN"; - case KeyEvent.VK_EXCLAMATION_MARK: - return "EXCLAMATION_MARK"; - case KeyEvent.VK_INVERTED_EXCLAMATION_MARK: - return "INVERTED_EXCLAMATION_MARK"; - case KeyEvent.VK_LEFT_PARENTHESIS: - return "LEFT_PARENTHESIS"; - case KeyEvent.VK_NUMBER_SIGN: - return "NUMBER_SIGN"; - case KeyEvent.VK_MINUS: - return "MINUS"; - case KeyEvent.VK_PLUS: - return "PLUS"; - case KeyEvent.VK_RIGHT_PARENTHESIS: - return "RIGHT_PARENTHESIS"; - case KeyEvent.VK_UNDERSCORE: - return "UNDERSCORE"; + case KeyEvent.VK_AMPERSAND: + return "AMPERSAND"; + case KeyEvent.VK_ASTERISK: + return "ASTERISK"; + case KeyEvent.VK_QUOTEDBL: + return "QUOTEDBL"; + case KeyEvent.VK_LESS: + return "LESS"; + case KeyEvent.VK_GREATER: + return "GREATER"; + case KeyEvent.VK_BRACELEFT: + return "BRACELEFT"; + case KeyEvent.VK_BRACERIGHT: + return "BRACERIGHT"; + case KeyEvent.VK_AT: + return "AT"; + case KeyEvent.VK_COLON: + return "COLON"; + case KeyEvent.VK_CIRCUMFLEX: + return "CIRCUMFLEX"; + case KeyEvent.VK_DOLLAR: + return "DOLLAR"; + case KeyEvent.VK_EURO_SIGN: + return "EURO_SIGN"; + case KeyEvent.VK_EXCLAMATION_MARK: + return "EXCLAMATION_MARK"; + case KeyEvent.VK_INVERTED_EXCLAMATION_MARK: + return "INVERTED_EXCLAMATION_MARK"; + case KeyEvent.VK_LEFT_PARENTHESIS: + return "LEFT_PARENTHESIS"; + case KeyEvent.VK_NUMBER_SIGN: + return "NUMBER_SIGN"; + case KeyEvent.VK_MINUS: + return "MINUS"; + case KeyEvent.VK_PLUS: + return "PLUS"; + case KeyEvent.VK_RIGHT_PARENTHESIS: + return "RIGHT_PARENTHESIS"; + case KeyEvent.VK_UNDERSCORE: + return "UNDERSCORE"; - case KeyEvent.VK_FINAL: - return "FINAL"; - case KeyEvent.VK_CONVERT: - return "CONVERT"; - case KeyEvent.VK_NONCONVERT: - return "NONCONVERT"; - case KeyEvent.VK_ACCEPT: - return "ACCEPT"; - case KeyEvent.VK_MODECHANGE: - return "MODECHANGE"; - case KeyEvent.VK_KANA: - return "KANA"; - case KeyEvent.VK_KANJI: - return "KANJI"; - case KeyEvent.VK_ALPHANUMERIC: - return "ALPHANUMERIC"; - case KeyEvent.VK_KATAKANA: - return "KATAKANA"; - case KeyEvent.VK_HIRAGANA: - return "HIRAGANA"; - case KeyEvent.VK_FULL_WIDTH: - return "FULL_WIDTH"; - case KeyEvent.VK_HALF_WIDTH: - return "HALF_WIDTH"; - case KeyEvent.VK_ROMAN_CHARACTERS: - return "ROMAN_CHARACTERS"; - case KeyEvent.VK_ALL_CANDIDATES: - return "ALL_CANDIDATES"; - case KeyEvent.VK_PREVIOUS_CANDIDATE: - return "PREVIOUS_CANDIDATE"; - case KeyEvent.VK_CODE_INPUT: - return "CODE_INPUT"; - case KeyEvent.VK_JAPANESE_KATAKANA: - return "JAPANESE_KATAKANA"; - case KeyEvent.VK_JAPANESE_HIRAGANA: - return "JAPANESE_HIRAGANA"; - case KeyEvent.VK_JAPANESE_ROMAN: - return "JAPANESE_ROMAN"; - case KeyEvent.VK_KANA_LOCK: - return "KANA_LOCK"; - case KeyEvent.VK_INPUT_METHOD_ON_OFF: - return "INPUT_METHOD_ON_OFF"; + case KeyEvent.VK_FINAL: + return "FINAL"; + case KeyEvent.VK_CONVERT: + return "CONVERT"; + case KeyEvent.VK_NONCONVERT: + return "NONCONVERT"; + case KeyEvent.VK_ACCEPT: + return "ACCEPT"; + case KeyEvent.VK_MODECHANGE: + return "MODECHANGE"; + case KeyEvent.VK_KANA: + return "KANA"; + case KeyEvent.VK_KANJI: + return "KANJI"; + case KeyEvent.VK_ALPHANUMERIC: + return "ALPHANUMERIC"; + case KeyEvent.VK_KATAKANA: + return "KATAKANA"; + case KeyEvent.VK_HIRAGANA: + return "HIRAGANA"; + case KeyEvent.VK_FULL_WIDTH: + return "FULL_WIDTH"; + case KeyEvent.VK_HALF_WIDTH: + return "HALF_WIDTH"; + case KeyEvent.VK_ROMAN_CHARACTERS: + return "ROMAN_CHARACTERS"; + case KeyEvent.VK_ALL_CANDIDATES: + return "ALL_CANDIDATES"; + case KeyEvent.VK_PREVIOUS_CANDIDATE: + return "PREVIOUS_CANDIDATE"; + case KeyEvent.VK_CODE_INPUT: + return "CODE_INPUT"; + case KeyEvent.VK_JAPANESE_KATAKANA: + return "JAPANESE_KATAKANA"; + case KeyEvent.VK_JAPANESE_HIRAGANA: + return "JAPANESE_HIRAGANA"; + case KeyEvent.VK_JAPANESE_ROMAN: + return "JAPANESE_ROMAN"; + case KeyEvent.VK_KANA_LOCK: + return "KANA_LOCK"; + case KeyEvent.VK_INPUT_METHOD_ON_OFF: + return "INPUT_METHOD_ON_OFF"; - case KeyEvent.VK_AGAIN: - return "AGAIN"; - case KeyEvent.VK_UNDO: - return "UNDO"; - case KeyEvent.VK_COPY: - return "COPY"; - case KeyEvent.VK_PASTE: - return "PASTE"; - case KeyEvent.VK_CUT: - return "CUT"; - case KeyEvent.VK_FIND: - return "FIND"; - case KeyEvent.VK_PROPS: - return "PROPS"; - case KeyEvent.VK_STOP: - return "STOP"; + case KeyEvent.VK_AGAIN: + return "AGAIN"; + case KeyEvent.VK_UNDO: + return "UNDO"; + case KeyEvent.VK_COPY: + return "COPY"; + case KeyEvent.VK_PASTE: + return "PASTE"; + case KeyEvent.VK_CUT: + return "CUT"; + case KeyEvent.VK_FIND: + return "FIND"; + case KeyEvent.VK_PROPS: + return "PROPS"; + case KeyEvent.VK_STOP: + return "STOP"; - case KeyEvent.VK_COMPOSE: - return "COMPOSE"; - case KeyEvent.VK_ALT_GRAPH: - return "ALT_GRAPH"; - } + case KeyEvent.VK_COMPOSE: + return "COMPOSE"; + case KeyEvent.VK_ALT_GRAPH: + return "ALT_GRAPH"; + } - if (keyCode >= KeyEvent.VK_NUMPAD0 && keyCode <= KeyEvent.VK_NUMPAD9) { - char c = (char)(keyCode - KeyEvent.VK_NUMPAD0 + '0'); - return "NUMPAD" + c; - } + if (keyCode >= KeyEvent.VK_NUMPAD0 && keyCode <= KeyEvent.VK_NUMPAD9) { + char c = (char) (keyCode - KeyEvent.VK_NUMPAD0 + '0'); + return "NUMPAD" + c; + } - return "unknown(0x" + Integer.toString(keyCode, 16) + ")"; + return "unknown(0x" + Integer.toString(keyCode, 16) + ")"; } public static String makeFirstWordCaptial(String word) { - if (word.length() < 2) { - return word; - } + if (word.length() < 2) { + return word; + } - String firstWord = word.substring(0, 1); - String restOfWord = word.substring(1); + String firstWord = word.substring(0, 1); + String restOfWord = word.substring(1); - return firstWord.toUpperCase() + restOfWord; + return firstWord.toUpperCase() + restOfWord; } } \ No newline at end of file diff --git a/src/java/org/jivesoftware/sparkimpl/profile/VCardEditor.java b/src/java/org/jivesoftware/sparkimpl/profile/VCardEditor.java index 6f6a4ce9..7b9091d2 100644 --- a/src/java/org/jivesoftware/sparkimpl/profile/VCardEditor.java +++ b/src/java/org/jivesoftware/sparkimpl/profile/VCardEditor.java @@ -67,403 +67,415 @@ public class VCardEditor { /** * Displays the VCard for an individual. - * - * @param vCard the users vcard. - * @param parent the parent component, used for location. + * + * @param vCard + * the users vcard. + * @param parent + * the parent component, used for location. */ public void editProfile(final VCard vCard, JComponent parent) { - final JTabbedPane tabbedPane = new JTabbedPane(); + final JTabbedPane tabbedPane = new JTabbedPane(); - // Initialize Panels - personalPanel = new PersonalPanel(); - personalPanel.showJID(false); + // Initialize Panels + personalPanel = new PersonalPanel(); + personalPanel.showJID(false); - tabbedPane.addTab(Res.getString("tab.personal"), personalPanel); + tabbedPane.addTab(Res.getString("tab.personal"), personalPanel); - businessPanel = new BusinessPanel(); - tabbedPane.addTab(Res.getString("tab.business"), businessPanel); + businessPanel = new BusinessPanel(); + tabbedPane.addTab(Res.getString("tab.business"), businessPanel); - homePanel = new HomePanel(); - tabbedPane.addTab(Res.getString("tab.home"), homePanel); + homePanel = new HomePanel(); + tabbedPane.addTab(Res.getString("tab.home"), homePanel); - avatarPanel = new AvatarPanel(); - tabbedPane.addTab(Res.getString("tab.avatar"), avatarPanel); + avatarPanel = new AvatarPanel(); + tabbedPane.addTab(Res.getString("tab.avatar"), avatarPanel); - // Build the UI - buildUI(vCard); + // Build the UI + buildUI(vCard); - final JOptionPane pane; - final JDialog dlg; + final JOptionPane pane; + final JDialog dlg; - TitlePanel titlePanel; + TitlePanel titlePanel; - ImageIcon icon = VCardManager.getAvatarIcon(vCard); - if (icon == null) { - icon = SparkRes.getImageIcon(SparkRes.BLANK_24x24); - } + ImageIcon icon = VCardManager.getAvatarIcon(vCard); + if (icon == null) { + icon = SparkRes.getImageIcon(SparkRes.BLANK_24x24); + } - // Create the title panel for this dialog - titlePanel = new TitlePanel(Res.getString("title.edit.profile"), Res.getString("message.save.profile"), icon, true); + // Create the title panel for this dialog + titlePanel = new TitlePanel(Res.getString("title.edit.profile"), + Res.getString("message.save.profile"), icon, true); - // Construct main panel w/ layout. - final JPanel mainPanel = new JPanel(); - mainPanel.setLayout(new BorderLayout()); - mainPanel.add(titlePanel, BorderLayout.NORTH); + // Construct main panel w/ layout. + final JPanel mainPanel = new JPanel(); + mainPanel.setLayout(new BorderLayout()); + mainPanel.add(titlePanel, BorderLayout.NORTH); - // The user should only be able to close this dialog. - Object[] options = {Res.getString("save"), Res.getString("cancel")}; - pane = new JOptionPane(tabbedPane, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[0]); + // The user should only be able to close this dialog. + Object[] options = { Res.getString("save"), Res.getString("cancel") }; + pane = new JOptionPane(tabbedPane, JOptionPane.PLAIN_MESSAGE, + JOptionPane.OK_CANCEL_OPTION, null, options, options[0]); - mainPanel.add(pane, BorderLayout.CENTER); + mainPanel.add(pane, BorderLayout.CENTER); - JOptionPane p = new JOptionPane(); - dlg = p.createDialog(parent, Res.getString("title.profile.information")); - dlg.setModal(false); + JOptionPane p = new JOptionPane(); + dlg = p.createDialog(parent, Res.getString("title.profile.information")); + dlg.setModal(false); - dlg.pack(); - dlg.setSize(600, 400); - dlg.setResizable(true); - dlg.setContentPane(mainPanel); - dlg.setLocationRelativeTo(parent); + dlg.pack(); + dlg.setSize(600, 400); + dlg.setResizable(true); + dlg.setContentPane(mainPanel); + dlg.setLocationRelativeTo(parent); - PropertyChangeListener changeListener = new PropertyChangeListener() { - public void propertyChange(PropertyChangeEvent e) { - String value = (String)pane.getValue(); - if (Res.getString("cancel").equals(value)) { - pane.removePropertyChangeListener(this); - dlg.dispose(); - } - else if (Res.getString("save").equals(value)) { - pane.removePropertyChangeListener(this); - dlg.dispose(); - saveVCard(); - } - } - }; + PropertyChangeListener changeListener = new PropertyChangeListener() { + public void propertyChange(PropertyChangeEvent e) { + String value = (String) pane.getValue(); + if (Res.getString("cancel").equals(value)) { + pane.removePropertyChangeListener(this); + dlg.dispose(); + } else if (Res.getString("save").equals(value)) { + pane.removePropertyChangeListener(this); + dlg.dispose(); + saveVCard(); + } + } + }; - pane.addPropertyChangeListener(changeListener); - avatarPanel.setParentDialog(dlg); - dlg.setVisible(true); - dlg.toFront(); - dlg.requestFocus(); + pane.addPropertyChangeListener(changeListener); + avatarPanel.setParentDialog(dlg); + dlg.setVisible(true); + dlg.toFront(); + dlg.requestFocus(); - personalPanel.focus(); + personalPanel.focus(); } /** * Displays the VCard for an individual. - * - * @param vCard the users vcard. - * @param parent the parent component, used for location. + * + * @param vCard + * the users vcard. + * @param parent + * the parent component, used for location. */ public void viewFullProfile(final VCard vCard, JComponent parent) { - final JTabbedPane tabbedPane = new JTabbedPane(); + final JTabbedPane tabbedPane = new JTabbedPane(); - // Initialize Panels - personalPanel = new PersonalPanel(); - personalPanel.allowEditing(false); - personalPanel.showJID(false); + // Initialize Panels + personalPanel = new PersonalPanel(); + personalPanel.allowEditing(false); + personalPanel.showJID(false); - tabbedPane.addTab(Res.getString("tab.personal"), personalPanel); + tabbedPane.addTab(Res.getString("tab.personal"), personalPanel); - businessPanel = new BusinessPanel(); - businessPanel.allowEditing(false); - tabbedPane.addTab(Res.getString("tab.business"), businessPanel); + businessPanel = new BusinessPanel(); + businessPanel.allowEditing(false); + tabbedPane.addTab(Res.getString("tab.business"), businessPanel); - homePanel = new HomePanel(); - homePanel.allowEditing(false); - tabbedPane.addTab(Res.getString("tab.home"), homePanel); + homePanel = new HomePanel(); + homePanel.allowEditing(false); + tabbedPane.addTab(Res.getString("tab.home"), homePanel); - avatarPanel = new AvatarPanel(); - avatarPanel.allowEditing(false); - tabbedPane.addTab(Res.getString("tab.avatar"), avatarPanel); + avatarPanel = new AvatarPanel(); + avatarPanel.allowEditing(false); + tabbedPane.addTab(Res.getString("tab.avatar"), avatarPanel); - // Build the UI - buildUI(vCard); + // Build the UI + buildUI(vCard); - final JOptionPane pane; - final JDialog dlg; + final JOptionPane pane; + final JDialog dlg; - TitlePanel titlePanel; + TitlePanel titlePanel; - ImageIcon icon = VCardManager.getAvatarIcon(vCard); - if (icon == null) { - icon = SparkRes.getImageIcon(SparkRes.BLANK_24x24); - } + ImageIcon icon = VCardManager.getAvatarIcon(vCard); + if (icon == null) { + icon = SparkRes.getImageIcon(SparkRes.BLANK_24x24); + } - // Create the title panel for this dialog - titlePanel = new TitlePanel(Res.getString("title.profile.information"), "", icon, true); + // Create the title panel for this dialog + titlePanel = new TitlePanel(Res.getString("title.profile.information"), + "", icon, true); - // Construct main panel w/ layout. - final JPanel mainPanel = new JPanel(); - mainPanel.setLayout(new BorderLayout()); - mainPanel.add(titlePanel, BorderLayout.NORTH); + // Construct main panel w/ layout. + final JPanel mainPanel = new JPanel(); + mainPanel.setLayout(new BorderLayout()); + mainPanel.add(titlePanel, BorderLayout.NORTH); - // The user should only be able to close this dialog. - Object[] options = {Res.getString("close")}; - pane = new JOptionPane(tabbedPane, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[0]); + // The user should only be able to close this dialog. + Object[] options = { Res.getString("close") }; + pane = new JOptionPane(tabbedPane, JOptionPane.PLAIN_MESSAGE, + JOptionPane.OK_CANCEL_OPTION, null, options, options[0]); - mainPanel.add(pane, BorderLayout.CENTER); + mainPanel.add(pane, BorderLayout.CENTER); - JOptionPane p = new JOptionPane(); - dlg = p.createDialog(parent, Res.getString("title.profile.information")); - dlg.setModal(false); + JOptionPane p = new JOptionPane(); + dlg = p.createDialog(parent, Res.getString("title.profile.information")); + dlg.setModal(false); - dlg.pack(); - dlg.setSize(600, 400); - dlg.setResizable(true); - dlg.setContentPane(mainPanel); - dlg.setLocationRelativeTo(parent); + dlg.pack(); + dlg.setSize(600, 400); + dlg.setResizable(true); + dlg.setContentPane(mainPanel); + dlg.setLocationRelativeTo(parent); + PropertyChangeListener changeListener = new PropertyChangeListener() { + public void propertyChange(PropertyChangeEvent e) { + Object o = pane.getValue(); + if (o instanceof Integer) { + pane.removePropertyChangeListener(this); + dlg.dispose(); + return; + } - PropertyChangeListener changeListener = new PropertyChangeListener() { - public void propertyChange(PropertyChangeEvent e) { - Object o = pane.getValue(); - if (o instanceof Integer) { - pane.removePropertyChangeListener(this); - dlg.dispose(); - return; - } + String value = (String) pane.getValue(); + if (Res.getString("close").equals(value)) { + pane.removePropertyChangeListener(this); + dlg.dispose(); + } + } + }; - String value = (String)pane.getValue(); - if (Res.getString("close").equals(value)) { - pane.removePropertyChangeListener(this); - dlg.dispose(); - } - } - }; + pane.addPropertyChangeListener(changeListener); - pane.addPropertyChangeListener(changeListener); + dlg.setVisible(true); + dlg.toFront(); + dlg.requestFocus(); - dlg.setVisible(true); - dlg.toFront(); - dlg.requestFocus(); - - personalPanel.focus(); + personalPanel.focus(); } /** * Displays a users profile. - * - * @param jid the jid of the user. - * @param vcard the users vcard. - * @param parent the parent component, used for location handling. + * + * @param jid + * the jid of the user. + * @param vcard + * the users vcard. + * @param parent + * the parent component, used for location handling. */ public void displayProfile(final String jid, VCard vcard, JComponent parent) { - VCardViewer viewer = new VCardViewer(jid); + VCardViewer viewer = new VCardViewer(jid); - final JFrame dlg = new JFrame(Res.getString("title.view.profile.for", jid)); + final JFrame dlg = new JFrame(Res.getString("title.view.profile.for", + jid)); + avatarLabel = new JLabel(); + avatarLabel.setHorizontalAlignment(JButton.RIGHT); + avatarLabel.setBorder(BorderFactory.createBevelBorder(0, Color.white, + Color.lightGray)); - avatarLabel = new JLabel(); - avatarLabel.setHorizontalAlignment(JButton.RIGHT); - avatarLabel.setBorder(BorderFactory.createBevelBorder(0, Color.white, Color.lightGray)); - - // The user should only be able to close this dialog. - Object[] options = { Res.getString("button.view.profile"), Res.getString("close")}; - final JOptionPane pane = new JOptionPane(viewer, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, options, options[0]); + // The user should only be able to close this dialog. + Object[] options = { Res.getString("button.view.profile"), + Res.getString("close") }; + final JOptionPane pane = new JOptionPane(viewer, + JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, + options, options[0]); - // mainPanel.add(pane, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 5, 5, 5), 0, 0)); + // mainPanel.add(pane, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0, + // GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 5, + // 5, 5), 0, 0)); + dlg.setIconImage(SparkRes.getImageIcon(SparkRes.PROFILE_IMAGE_16x16) + .getImage()); - dlg.setIconImage(SparkRes.getImageIcon(SparkRes.PROFILE_IMAGE_16x16).getImage()); + dlg.pack(); + dlg.setSize(350, 250); + dlg.setResizable(true); + dlg.setContentPane(pane); + dlg.setLocationRelativeTo(parent); - dlg.pack(); - dlg.setSize(350, 250); - dlg.setResizable(true); - dlg.setContentPane(pane); - dlg.setLocationRelativeTo(parent); + PropertyChangeListener changeListener = new PropertyChangeListener() { + public void propertyChange(PropertyChangeEvent e) { + if (pane.getValue() instanceof Integer) { + pane.removePropertyChangeListener(this); + dlg.dispose(); + return; + } + String value = (String) pane.getValue(); + if (Res.getString("close").equals(value)) { + pane.removePropertyChangeListener(this); + dlg.dispose(); + } else if (Res.getString("button.view.profile").equals(value)) { + pane.setValue(JOptionPane.UNINITIALIZED_VALUE); + SparkManager.getVCardManager().viewFullProfile(jid, pane); + } + } + }; - PropertyChangeListener changeListener = new PropertyChangeListener() { - public void propertyChange(PropertyChangeEvent e) { - if (pane.getValue() instanceof Integer) { - pane.removePropertyChangeListener(this); - dlg.dispose(); - return; - } - String value = (String)pane.getValue(); - if (Res.getString("close").equals(value)) { - pane.removePropertyChangeListener(this); - dlg.dispose(); - } - else if (Res.getString("button.view.profile").equals(value)) { - pane.setValue(JOptionPane.UNINITIALIZED_VALUE); - SparkManager.getVCardManager().viewFullProfile(jid, pane); - } - } - }; + pane.addPropertyChangeListener(changeListener); - pane.addPropertyChangeListener(changeListener); + dlg.addKeyListener(new KeyAdapter() { + public void keyPressed(KeyEvent keyEvent) { + if (keyEvent.getKeyChar() == KeyEvent.VK_ESCAPE) { + dlg.dispose(); + } + } + }); - dlg.addKeyListener(new KeyAdapter() { - public void keyPressed(KeyEvent keyEvent) { - if (keyEvent.getKeyChar() == KeyEvent.VK_ESCAPE) { - dlg.dispose(); - } - } - }); - - dlg.setVisible(true); - dlg.toFront(); - dlg.requestFocus(); + dlg.setVisible(true); + dlg.toFront(); + dlg.requestFocus(); } - /** * Builds the UI based on a VCard. - * - * @param vcard the vcard used to build the UI. + * + * @param vcard + * the vcard used to build the UI. */ private void buildUI(VCard vcard) { - personalPanel.setFirstName(vcard.getFirstName()); - personalPanel.setMiddleName(vcard.getMiddleName()); - personalPanel.setLastName(vcard.getLastName()); - personalPanel.setEmailAddress(vcard.getEmailHome()); - personalPanel.setNickname(vcard.getNickName()); - personalPanel.setJID(vcard.getJabberId()); + personalPanel.setFirstName(vcard.getFirstName()); + personalPanel.setMiddleName(vcard.getMiddleName()); + personalPanel.setLastName(vcard.getLastName()); + personalPanel.setEmailAddress(vcard.getEmailHome()); + personalPanel.setNickname(vcard.getNickName()); + personalPanel.setJID(vcard.getJabberId()); - businessPanel.setCompany(vcard.getOrganization()); - businessPanel.setDepartment(vcard.getOrganizationUnit()); - businessPanel.setStreetAddress(vcard.getAddressFieldWork("STREET")); - businessPanel.setCity(vcard.getAddressFieldWork("LOCALITY")); - businessPanel.setState(vcard.getAddressFieldWork("REGION")); - businessPanel.setZipCode(vcard.getAddressFieldWork("PCODE")); - businessPanel.setCountry(vcard.getAddressFieldWork("CTRY")); - businessPanel.setJobTitle(vcard.getField("TITLE")); - businessPanel.setPhone(vcard.getPhoneWork("VOICE")); - businessPanel.setFax(vcard.getPhoneWork("FAX")); - businessPanel.setPager(vcard.getPhoneWork("PAGER")); - businessPanel.setMobile(vcard.getPhoneWork("CELL")); - businessPanel.setWebPage(vcard.getField("URL")); + businessPanel.setCompany(vcard.getOrganization()); + businessPanel.setDepartment(vcard.getOrganizationUnit()); + businessPanel.setStreetAddress(vcard.getAddressFieldWork("STREET")); + businessPanel.setCity(vcard.getAddressFieldWork("LOCALITY")); + businessPanel.setState(vcard.getAddressFieldWork("REGION")); + businessPanel.setZipCode(vcard.getAddressFieldWork("PCODE")); + businessPanel.setCountry(vcard.getAddressFieldWork("CTRY")); + businessPanel.setJobTitle(vcard.getField("TITLE")); + businessPanel.setPhone(vcard.getPhoneWork("VOICE")); + businessPanel.setFax(vcard.getPhoneWork("FAX")); + businessPanel.setPager(vcard.getPhoneWork("PAGER")); + businessPanel.setMobile(vcard.getPhoneWork("CELL")); + businessPanel.setWebPage(vcard.getField("URL")); - // Load Home Info - homePanel.setStreetAddress(vcard.getAddressFieldHome("STREET")); - homePanel.setCity(vcard.getAddressFieldHome("LOCALITY")); - homePanel.setState(vcard.getAddressFieldHome("REGION")); - homePanel.setZipCode(vcard.getAddressFieldHome("PCODE")); - homePanel.setCountry(vcard.getAddressFieldHome("CTRY")); - homePanel.setPhone(vcard.getPhoneHome("VOICE")); - homePanel.setFax(vcard.getPhoneHome("FAX")); - homePanel.setPager(vcard.getPhoneHome("PAGER")); - homePanel.setMobile(vcard.getPhoneHome("CELL")); + // Load Home Info + homePanel.setStreetAddress(vcard.getAddressFieldHome("STREET")); + homePanel.setCity(vcard.getAddressFieldHome("LOCALITY")); + homePanel.setState(vcard.getAddressFieldHome("REGION")); + homePanel.setZipCode(vcard.getAddressFieldHome("PCODE")); + homePanel.setCountry(vcard.getAddressFieldHome("CTRY")); + homePanel.setPhone(vcard.getPhoneHome("VOICE")); + homePanel.setFax(vcard.getPhoneHome("FAX")); + homePanel.setPager(vcard.getPhoneHome("PAGER")); + homePanel.setMobile(vcard.getPhoneHome("CELL")); - // Set avatar - byte[] bytes = vcard.getAvatar(); - if (bytes != null && bytes.length > 0) { - ImageIcon icon = new ImageIcon(bytes); - avatarPanel.setAvatar(icon); - avatarPanel.setAvatarBytes(bytes); - if (avatarLabel != null) { - icon = GraphicUtils.scaleImageIcon(icon, 48, 48); + // Set avatar + byte[] bytes = vcard.getAvatar(); + if (bytes != null && bytes.length > 0) { + ImageIcon icon = new ImageIcon(bytes); + avatarPanel.setAvatar(icon); + avatarPanel.setAvatarBytes(bytes); + if (avatarLabel != null) { + icon = GraphicUtils.scaleImageIcon(icon, 48, 48); - avatarLabel.setIcon(icon); - } - } + avatarLabel.setIcon(icon); + } + } } /** * Saves the VCard. */ private void saveVCard() { - final VCard vcard = new VCard(); + final VCard vcard = new VCard(); - // Save personal info - vcard.setFirstName(personalPanel.getFirstName()); - vcard.setLastName(personalPanel.getLastName()); - vcard.setMiddleName(personalPanel.getMiddleName()); - vcard.setEmailHome(personalPanel.getEmailAddress()); - vcard.setNickName(personalPanel.getNickname()); + // Save personal info + vcard.setFirstName(personalPanel.getFirstName()); + vcard.setLastName(personalPanel.getLastName()); + vcard.setMiddleName(personalPanel.getMiddleName()); + vcard.setEmailHome(personalPanel.getEmailAddress()); + vcard.setNickName(personalPanel.getNickname()); - // Save business info - vcard.setOrganization(businessPanel.getCompany()); - vcard.setAddressFieldWork("STREET", businessPanel.getStreetAddress()); - vcard.setAddressFieldWork("LOCALITY", businessPanel.getCity()); - vcard.setAddressFieldWork("REGION", businessPanel.getState()); - vcard.setAddressFieldWork("PCODE", businessPanel.getZipCode()); - vcard.setAddressFieldWork("CTRY", businessPanel.getCountry()); - vcard.setField("TITLE", businessPanel.getJobTitle()); - vcard.setOrganizationUnit(businessPanel.getDepartment()); - vcard.setPhoneWork("VOICE", businessPanel.getPhone()); - vcard.setPhoneWork("FAX", businessPanel.getFax()); - vcard.setPhoneWork("PAGER", businessPanel.getPager()); - vcard.setPhoneWork("CELL", businessPanel.getMobile()); - vcard.setField("URL", businessPanel.getWebPage()); + // Save business info + vcard.setOrganization(businessPanel.getCompany()); + vcard.setAddressFieldWork("STREET", businessPanel.getStreetAddress()); + vcard.setAddressFieldWork("LOCALITY", businessPanel.getCity()); + vcard.setAddressFieldWork("REGION", businessPanel.getState()); + vcard.setAddressFieldWork("PCODE", businessPanel.getZipCode()); + vcard.setAddressFieldWork("CTRY", businessPanel.getCountry()); + vcard.setField("TITLE", businessPanel.getJobTitle()); + vcard.setOrganizationUnit(businessPanel.getDepartment()); + vcard.setPhoneWork("VOICE", businessPanel.getPhone()); + vcard.setPhoneWork("FAX", businessPanel.getFax()); + vcard.setPhoneWork("PAGER", businessPanel.getPager()); + vcard.setPhoneWork("CELL", businessPanel.getMobile()); + vcard.setField("URL", businessPanel.getWebPage()); - // Save Home Info - vcard.setAddressFieldHome("STREET", homePanel.getStreetAddress()); - vcard.setAddressFieldHome("LOCALITY", homePanel.getCity()); - vcard.setAddressFieldHome("REGION", homePanel.getState()); - vcard.setAddressFieldHome("PCODE", homePanel.getZipCode()); - vcard.setAddressFieldHome("CTRY", homePanel.getCountry()); - vcard.setPhoneHome("VOICE", homePanel.getPhone()); - vcard.setPhoneHome("FAX", homePanel.getFax()); - vcard.setPhoneHome("PAGER", homePanel.getPager()); - vcard.setPhoneHome("CELL", homePanel.getMobile()); + // Save Home Info + vcard.setAddressFieldHome("STREET", homePanel.getStreetAddress()); + vcard.setAddressFieldHome("LOCALITY", homePanel.getCity()); + vcard.setAddressFieldHome("REGION", homePanel.getState()); + vcard.setAddressFieldHome("PCODE", homePanel.getZipCode()); + vcard.setAddressFieldHome("CTRY", homePanel.getCountry()); + vcard.setPhoneHome("VOICE", homePanel.getPhone()); + vcard.setPhoneHome("FAX", homePanel.getFax()); + vcard.setPhoneHome("PAGER", homePanel.getPager()); + vcard.setPhoneHome("CELL", homePanel.getMobile()); - // Save Avatar - final File avatarFile = avatarPanel.getAvatarFile(); - byte[] avatarBytes = avatarPanel.getAvatarBytes(); + // Save Avatar + final File avatarFile = avatarPanel.getAvatarFile(); + byte[] avatarBytes = avatarPanel.getAvatarBytes(); - if (avatarFile != null) { - try { - // Make it 48x48 - ImageIcon icon = new ImageIcon(avatarFile.toURI().toURL()); - Image image = icon.getImage(); - image = image.getScaledInstance(-1, 48, Image.SCALE_SMOOTH); - avatarBytes = GraphicUtils.getBytesFromImage(image); - } - catch (MalformedURLException e) { - Log.error("Unable to set avatar.", e); - } - } + if (avatarFile != null) { + avatarBytes = GraphicUtils.getBytesFromImage(avatarFile); + ImageIcon icon = new ImageIcon(avatarBytes); + Image image = icon.getImage(); + image = image.getScaledInstance(-1, 48, Image.SCALE_SMOOTH); + avatarBytes = GraphicUtils.getBytesFromImage(image); - // If avatar bytes, persist as vcard. - if (avatarBytes != null) { - vcard.setAvatar(avatarBytes); - } + } - try { - final VCardManager vcardManager = SparkManager.getVCardManager(); - vcardManager.setPersonalVCard(vcard); + // If avatar bytes, persist as vcard. + if (avatarBytes != null) { + vcard.setAvatar(avatarBytes); + } - vcard.save(SparkManager.getConnection()); + try { + final VCardManager vcardManager = SparkManager.getVCardManager(); + vcardManager.setPersonalVCard(vcard); - // Notify users. - if (avatarFile != null || avatarBytes != null) { - Presence presence = SparkManager.getWorkspace().getStatusBar().getPresence(); - Presence newPresence = new Presence(presence.getType(), presence.getStatus(), presence.getPriority(), presence.getMode()); + vcard.save(SparkManager.getConnection()); - // Change my own presence - SparkManager.getSessionManager().changePresence(newPresence); + // Notify users. + if (avatarFile != null || avatarBytes != null) { + Presence presence = SparkManager.getWorkspace().getStatusBar() + .getPresence(); + Presence newPresence = new Presence(presence.getType(), + presence.getStatus(), presence.getPriority(), + presence.getMode()); - // Chnage avatar in status bar. - StatusBar statusBar = SparkManager.getWorkspace().getStatusBar(); - statusBar.setAvatar(new ImageIcon(vcard.getAvatar())); - } - else { - String firstName = vcard.getFirstName(); - String lastName = vcard.getLastName(); - StatusBar statusBar = SparkManager.getWorkspace().getStatusBar(); - if (ModelUtil.hasLength(firstName) && ModelUtil.hasLength(lastName)) { - statusBar.setNickname(firstName + " " + lastName); - } - else if (ModelUtil.hasLength(firstName)) { - statusBar.setNickname(firstName); - } + // Change my own presence + SparkManager.getSessionManager().changePresence(newPresence); - statusBar.setAvatar(null); - } + // Chnage avatar in status bar. + StatusBar statusBar = SparkManager.getWorkspace() + .getStatusBar(); + statusBar.setAvatar(new ImageIcon(vcard.getAvatar())); + } else { + String firstName = vcard.getFirstName(); + String lastName = vcard.getLastName(); + StatusBar statusBar = SparkManager.getWorkspace() + .getStatusBar(); + if (ModelUtil.hasLength(firstName) + && ModelUtil.hasLength(lastName)) { + statusBar.setNickname(firstName + " " + lastName); + } else if (ModelUtil.hasLength(firstName)) { + statusBar.setNickname(firstName); + } - // Notify listenres - SparkManager.getVCardManager().notifyVCardListeners(); - } - catch (XMPPException e) { - Log.error(e); - JOptionPane.showMessageDialog(SparkManager.getMainWindow(), Res.getString("message.vcard.not.supported"), Res.getString("title.error"), JOptionPane.ERROR_MESSAGE); - } + statusBar.setAvatar(null); + } + + // Notify listenres + SparkManager.getVCardManager().notifyVCardListeners(); + } catch (XMPPException e) { + Log.error(e); + JOptionPane.showMessageDialog(SparkManager.getMainWindow(), + Res.getString("message.vcard.not.supported"), + Res.getString("title.error"), JOptionPane.ERROR_MESSAGE); + } } - } - -