Articles/Window/OpenWindowNearButton: Button.diff

File Button.diff, 3.6 kB (added by Mauno Haukila, 4 months ago)
  • Button.java

     
    131131    public void paintContent(PaintTarget target) throws PaintException { 
    132132        super.paintContent(target); 
    133133 
    134         if (isSwitchMode()) { 
    135             target.addAttribute("type", "switch"); 
    136         } 
    137134        boolean state; 
    138135        try { 
    139136            state = ((Boolean) getValue()).booleanValue(); 
    140137        } catch (final NullPointerException e) { 
    141138            state = false; 
    142139        } 
    143         target.addVariable(this, "state", state); 
     140        if (isSwitchMode()) { 
     141            target.addAttribute("type", "switch"); 
     142            target.addVariable(this, "state", state); 
     143        } else { 
     144            target.addVariable(this, "clientX",-1); 
     145            target.addVariable(this, "clientY",-1); 
     146        } 
    144147 
    145148    } 
    146149 
     
    159162            final Boolean newValue = (Boolean) variables.get("state"); 
    160163            final Boolean oldValue = (Boolean) getValue(); 
    161164 
    162             if (isSwitchMode()) { 
     165            // For switch button, the event is only sent if the 
     166            // switch state is changed 
     167            if (newValue != null && !newValue.equals(oldValue) && !isReadOnly()) { 
     168                setValue(newValue); 
     169                fireClick(); 
     170            } 
    163171 
    164                 // For switch button, the event is only sent if the 
    165                 // switch state is changed 
    166                 if (newValue != null && !newValue.equals(oldValue) 
    167                         && !isReadOnly()) { 
    168                     setValue(newValue); 
    169                     fireClick(); 
    170                 } 
    171             } else { 
     172        } else if (!isReadOnly() && variables.containsKey("clientX")) { 
     173            // Only send click event if the button is pushed 
     174            final Integer clientX = (Integer) variables.get("clientX"); 
     175            final Integer clientY = (Integer) variables.get("clientY"); 
     176            final Boolean oldValue = (Boolean) getValue(); 
     177            fireClick(clientX, clientY); 
    172178 
    173                 // Only send click event if the button is pushed 
    174                 if (newValue.booleanValue()) { 
    175                     fireClick(); 
    176                 } 
    177  
    178                 // If the button is true for some reason, release it 
    179                 if (oldValue.booleanValue()) { 
    180                     setValue(new Boolean(false)); 
    181                 } 
     179            // If the button is true for some reason, release it 
     180            if (oldValue.booleanValue()) { 
     181                setValue(new Boolean(false)); 
    182182            } 
    183183        } 
    184184    } 
     
    262262     * @since 3.0 
    263263     */ 
    264264    public class ClickEvent extends Component.Event { 
    265  
     265        private int clientX = -1; 
     266        private int clientY = -1; 
    266267        /** 
    267268         * Serial generated by eclipse. 
    268269         */ 
     
    278279            super(source); 
    279280        } 
    280281 
     282        public ClickEvent(Component source, int positionX, int positionY) { 
     283            this(source); 
     284            clientX = positionX; 
     285            clientY = positionY; 
     286        } 
     287 
    281288        /** 
    282289         * Gets the Button where the event occurred. 
    283290         *  
     
    286293        public Button getButton() { 
    287294            return (Button) getSource(); 
    288295        } 
     296 
     297        public int getClientX() { 
     298            return clientX; 
     299        } 
     300 
     301        public int getClientY() { 
     302            return clientY; 
     303        } 
    289304    } 
    290305 
    291306    /** 
     
    333348    protected void fireClick() { 
    334349        fireEvent(new Button.ClickEvent(this)); 
    335350    } 
    336  
     351    protected void fireClick(int clientX, int clientY) { 
     352        fireEvent(new Button.ClickEvent(this,clientX,clientY)); 
     353    } 
    337354}