alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Nokia. Portions Copyright 2004 Nokia. All Rights Reserved.
 */

package org.openide.awt;

import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import java.util.Observable;
import javax.swing.AbstractButton;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.text.Keymap;

import org.openide.util.actions.SystemAction;

import org.netbeans.junit.*;
import junit.textui.TestRunner;
import org.openide.util.Lookup;
import org.openide.util.lookup.AbstractLookup;

/**
 * Tests for the Actions class.
 * @author David Strupl
 */
public class ActionsTest extends NbTestCase {

    // colors of the testing images in this order:
    // (test recognizes the icon by the white/black colors in specified positions :-)))
    // testIcon.gif
    // testIcon_rollover.gif
    // testIcon_pressed.gif
    // testIcon_disabled.gif
    private static int[][] RESULT_COLORS_00 = {
        {255, 255, 255},
        {0, 0, 0},
        {255, 255, 255},
        {0, 0, 0},
        {255, 255, 255},
        {0, 0, 0},
        {255, 255, 255},
        {0, 0, 0},
    };
    private static int[][] RESULT_COLORS_01 = {
        {255, 255, 255},
        {255, 255, 255},
        {0, 0, 0},
        {0, 0, 0},
        {255, 255, 255},
        {255, 255, 255},
        {0, 0, 0},
        {0, 0, 0},
    };    
    private static int[][] RESULT_COLORS_11 = {
        {255, 255, 255},
        {255, 255, 255},
        {255, 255, 255},
        {255, 255, 255},
        {0, 0, 0},
        {0, 0, 0},
        {0, 0, 0},
        {0, 0, 0},
    };
    
    
    public ActionsTest(String name) {
        super(name);
    }
    
    public static void main(String[] args) {
        TestRunner.run(new NbTestSuite(ActionsTest.class));
    }
    
     protected void setUp () {
        System.setProperty("org.openide.util.Lookup", "org.openide.awt.ActionsTest$Lkp");
        assertNotNull ("Keymap has to be in lookup", org.openide.util.Lookup.getDefault ().lookup (Keymap.class));
    }
    
    /**
     * Test whether pressed, rollover and disabled icons
     * work for javax.swing.Action.
     */
    public void testIconsAction() throws Exception {
        JButton jb = new JButton();
        Actions.connect(jb, new TestAction());
        
        Icon icon = jb.getIcon();
        assertNotNull(icon);
        checkIfLoadedCorrectIcon (icon, jb, 0, "Enabled icon");
        
        Icon rolloverIcon = jb.getRolloverIcon();
        assertNotNull(rolloverIcon);
        checkIfLoadedCorrectIcon(rolloverIcon, jb, 1, "Rollover icon");
        
        Icon pressedIcon = jb.getPressedIcon();
        assertNotNull(pressedIcon);
        checkIfLoadedCorrectIcon(pressedIcon, jb, 2, "Pressed icon");
        
        Icon disabledIcon = jb.getDisabledIcon();
        assertNotNull(disabledIcon);
        checkIfLoadedCorrectIcon(disabledIcon, jb, 3, "Disabled icon");
    }
    
    /**
     * Test whether pressed, rollover and disabled icons
     * work for SystemAction.
     */
    public void testIconsSystemAction() throws Exception {
        SystemAction saInstance = SystemAction.get(TestSystemAction.class);
        
        JButton jb = new JButton();
        Actions.connect(jb, saInstance);
        
        Icon icon = jb.getIcon();
        assertNotNull(icon);
        checkIfLoadedCorrectIcon(icon, jb, 0, "Enabled icon");
        
        Icon rolloverIcon = jb.getRolloverIcon();
        assertNotNull(rolloverIcon);
        checkIfLoadedCorrectIcon(rolloverIcon, jb, 1, "Rollover icon");
        
        Icon pressedIcon = jb.getPressedIcon();
        assertNotNull(pressedIcon);
        checkIfLoadedCorrectIcon(pressedIcon, jb, 2, "Pressed icon");
        
        Icon disabledIcon = jb.getDisabledIcon();
        assertNotNull(disabledIcon);
        checkIfLoadedCorrectIcon(disabledIcon, jb, 3, "Disabled icon");
    }
    
    /**
     * Test whether pressed, rollover and disabled 24x24 icons
     * work for javax.swing.Action.
     */
    public void testIconsAction24() throws Exception {
        JButton jb = new JButton();
        jb.putClientProperty("PreferredIconSize",new Integer(24));
        Actions.connect(jb, new TestAction());
        
        Icon icon = jb.getIcon();
        assertNotNull(icon);
        checkIfLoadedCorrectIcon(icon, jb, 4, "Enabled icon");
        
        Icon rolloverIcon = jb.getRolloverIcon();
        assertNotNull(rolloverIcon);
        checkIfLoadedCorrectIcon(rolloverIcon, jb, 5, "Rollover icon");
        
        Icon pressedIcon = jb.getPressedIcon();
        assertNotNull(pressedIcon);
        checkIfLoadedCorrectIcon(pressedIcon, jb, 6, "Pressed icon");
        
        Icon disabledIcon = jb.getDisabledIcon();
        assertNotNull(disabledIcon);
        checkIfLoadedCorrectIcon(disabledIcon, jb, 7, "Disabled icon");
    }
    
    /**
     * Test whether pressed, rollover and disabled 24x24 icons
     * work for SystemAction.
     */
    public void testIconsSystemAction24() throws Exception {
        SystemAction saInstance = SystemAction.get(TestSystemAction.class);
        
        JButton jb = new JButton();
        jb.putClientProperty("PreferredIconSize",new Integer(24));
        Actions.connect(jb, saInstance);
        
        Icon icon = jb.getIcon();
        assertNotNull(icon);
        checkIfLoadedCorrectIcon(icon, jb, 4, "Enabled icon");
        
        Icon rolloverIcon = jb.getRolloverIcon();
        assertNotNull(rolloverIcon);
        checkIfLoadedCorrectIcon(rolloverIcon, jb, 5, "Rollover icon");
        
        Icon pressedIcon = jb.getPressedIcon();
        assertNotNull(pressedIcon);
        checkIfLoadedCorrectIcon(pressedIcon, jb, 6, "Pressed icon");
        
        Icon disabledIcon = jb.getDisabledIcon();
        assertNotNull(disabledIcon);
        checkIfLoadedCorrectIcon(disabledIcon, jb, 7, "Disabled icon");
    }
    
    /**
     * tests if the accelerator for JMenuItem is reset when the global KeyMap changes.
     * Has to work even when the menu is not visible (when visible is handled by Actions.Bridge listeners)
     * when not visible handled by the tested Actions.setMenuActionConnection() - only for menu items.
     * #39508
     */
    public void testActionRemoval_Issue39508() throws Exception {
        // prepare
        Keymap map = (Keymap)Lookup.getDefault().lookup(Keymap.class);
        map.removeBindings();
        Action action = new ActionsTest.TestAction();
        KeyStroke stroke = KeyStroke.getKeyStroke("ctrl alt 7");
        assertNotNull(stroke);
        //test start
        JMenuItem menu = new JMenuItem();
        assertNull(menu.getAccelerator());
        Actions.connect(menu, action, false);
        assertEquals(1, ((Observable)map).countObservers());
        assertNull(menu.getAccelerator());
        map.addActionForKeyStroke(stroke, action);
        assertNotNull(action.getValue(Action.ACCELERATOR_KEY));
        assertNotNull(menu.getAccelerator());
        map.removeKeyStrokeBinding(stroke);
        assertNull(action.getValue(Action.ACCELERATOR_KEY));
        assertNull(menu.getAccelerator());
        java.lang.ref.Reference ref = new java.lang.ref.WeakReference (action);
        menu = null;
        action = null;
        assertGC ("action can dissappear", ref);        
    }
    
    private void checkIfLoadedCorrectIcon (Icon icon, java.awt.Component c, int rowToCheck, String nameOfIcon) {
        checkIfIconOk (icon, c, 0, 0, RESULT_COLORS_00[rowToCheck], nameOfIcon);
        checkIfIconOk (icon, c, 0, 1, RESULT_COLORS_01[rowToCheck], nameOfIcon);
        checkIfIconOk (icon, c, 1, 1, RESULT_COLORS_11[rowToCheck], nameOfIcon);
    }
    
    /**
     * Checks colors on coordinates X,Y of the icon and compares them
     * to expectedResult.
     */
    private void checkIfIconOk(Icon icon, java.awt.Component c, int pixelX, int pixelY, int[] expectedResult, String nameOfIcon) {
        BufferedImage bufImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
        icon.paintIcon(c, bufImg.getGraphics(), 0, 0);
        int[] res = bufImg.getData().getPixel(pixelX, pixelY, (int[])null);
        log ("Icon height is " + icon.getIconHeight ());
        log ("Icon width is " + icon.getIconWidth ());
        for (int i = 0; i < res.length; i++) {
            assertEquals (nameOfIcon + ": Color of the ["+pixelX+","+pixelY+"] pixel is ", expectedResult[i], res[i]);
        }
    }
    
    private static final class TestSystemAction extends SystemAction {
        
        public void actionPerformed(java.awt.event.ActionEvent e) {
        }
        
        public org.openide.util.HelpCtx getHelpCtx() {
            return null;
        }
        
        public String getName() {
            return "TestSystemAction";
        }
        
        protected String iconResource() {
            return "org/openide/awt/data/testIcon.gif";
        }
        
    }
    
    private static final class TestAction extends AbstractAction {
        
        public TestAction() {
            putValue("iconBase", "org/openide/awt/data/testIcon.gif");
            putValue(NAME, "test");
        }
        
        public void actionPerformed(java.awt.event.ActionEvent e) {
        }
        
    }
    
    public static final class Lkp extends AbstractLookup {
        public Lkp () {
            this (new org.openide.util.lookup.InstanceContent ());
        }
        
        private Lkp (org.openide.util.lookup.InstanceContent ic) {
            super (ic);
            ic.add (new TestKeymap());
        }
    }    
    
     private static final class TestKeymap extends Observable implements Keymap {
         
         private Map map = new HashMap();
         private Action defAct;
         
         public void addActionForKeyStroke(javax.swing.KeyStroke key, Action act) {
             map.put(key, act);
             act.putValue(Action.ACCELERATOR_KEY, key);
             setChanged();
             notifyObservers();
         }
         
         public javax.swing.Action getAction(javax.swing.KeyStroke key) {
             return (Action)map.get(key);
         }
         
         public javax.swing.Action[] getBoundActions() {
             return new Action[0];
         }
         
         public javax.swing.KeyStroke[] getBoundKeyStrokes() {
             return new KeyStroke[0];
         }
         
         public javax.swing.Action getDefaultAction() {
             return defAct;
         }
         
         public javax.swing.KeyStroke[] getKeyStrokesForAction(javax.swing.Action a) {
             return new KeyStroke[0];
         }
         
         public String getName() {
             return "testKeymap";
         }
         
         public Keymap getResolveParent() {
             return null;
         }
         
         public boolean isLocallyDefined(javax.swing.KeyStroke key) {
             return true;
         }
         
         public void removeBindings() {
             map.clear();
         }
         
         public void removeKeyStrokeBinding(javax.swing.KeyStroke keys) {
             Action act = (Action)map.remove(keys);
             if (act != null) {
                 act.putValue(Action.ACCELERATOR_KEY, null);
             }
             setChanged();
             notifyObservers();
         }
         
         public void setDefaultAction(javax.swing.Action a) {
             defAct = a;
         }
         
         public void setResolveParent(Keymap parent) {
             // ignore
         }
         
     }
    
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.