/[classpath]/classpath/javax/swing/JComponent.java
ViewVC logotype

Diff of /classpath/javax/swing/JComponent.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.20.2.17 by gnu_andrew, Wed Nov 2 21:44:48 2005 UTC revision 1.20.2.18 by gnu_andrew, Sun Nov 27 21:00:37 2005 UTC
# Line 390  public abstract class JComponent extends Line 390  public abstract class JComponent extends
390     * @see javax.swing.OverlayLayout     * @see javax.swing.OverlayLayout
391     * @see javax.swing.BoxLayout     * @see javax.swing.BoxLayout
392     */     */
393    float alignmentX = 0.5f;    float alignmentX = -1.0F;
394    
395    /**    /**
396     * A value between 0.0 and 1.0 indicating the preferred vertical     * A value between 0.0 and 1.0 indicating the preferred vertical
# Line 405  public abstract class JComponent extends Line 405  public abstract class JComponent extends
405     * @see javax.swing.OverlayLayout     * @see javax.swing.OverlayLayout
406     * @see javax.swing.BoxLayout     * @see javax.swing.BoxLayout
407     */     */
408    float alignmentY = 0.5f;    float alignmentY = -1.0F;
409    
410    /**    /**
411     * The border painted around this component.     * The border painted around this component.
# Line 546  public abstract class JComponent extends Line 546  public abstract class JComponent extends
546        
547    private InputMap inputMap_whenFocused;    private InputMap inputMap_whenFocused;
548    private InputMap inputMap_whenAncestorOfFocused;    private InputMap inputMap_whenAncestorOfFocused;
549    private InputMap inputMap_whenInFocusedWindow;    private ComponentInputMap inputMap_whenInFocusedWindow;
550    private ActionMap actionMap;    private ActionMap actionMap;
551    /** @since 1.3 */    /** @since 1.3 */
552    private boolean verifyInputWhenFocusTarget;    private boolean verifyInputWhenFocusTarget;
# Line 555  public abstract class JComponent extends Line 555  public abstract class JComponent extends
555    private TransferHandler transferHandler;    private TransferHandler transferHandler;
556    
557    /**    /**
558       * Indicates if this component is currently painting a tile or not.
559       */
560      private boolean paintingTile;
561    
562      /**
563     * A cached Rectangle object to be reused. Be careful when you use that,     * A cached Rectangle object to be reused. Be careful when you use that,
564     * so that it doesn't get modified in another context within the same     * so that it doesn't get modified in another context within the same
565     * method call chain.     * method call chain.
# Line 796  public abstract class JComponent extends Line 801  public abstract class JComponent extends
801    }    }
802    
803    /**    /**
804     * Return all registered listeners of a particular type.     * Returns all registered {@link EventListener}s of the given
805     *     * <code>listenerType</code>.
    * @param listenerType The type of listener to return  
    *  
    * @return All listeners in the {@link #listenerList} which  
    * are of the specified type  
806     *     *
807       * @param listenerType the class of listeners to filter (<code>null</code>
808       *                     not permitted).
809       *                    
810       * @return An array of registered listeners.
811       *
812       * @throws ClassCastException if <code>listenerType</code> does not implement
813       *                            the {@link EventListener} interface.
814       * @throws NullPointerException if <code>listenerType</code> is
815       *                              <code>null</code>.
816       *                            
817       * @see #getAncestorListeners()
818     * @see #listenerList     * @see #listenerList
819       *
820       * @since 1.3
821     */     */
822    public <T extends EventListener> T[] getListeners(Class<T> listenerType)    public <T extends EventListener> T[] getListeners(Class<T> listenerType)
823    {    {
# Line 983  public abstract class JComponent extends Line 997  public abstract class JComponent extends
997    {    {
998      VetoableChangeListener[] listeners = getVetoableChangeListeners();      VetoableChangeListener[] listeners = getVetoableChangeListeners();
999    
1000      PropertyChangeEvent evt = new PropertyChangeEvent(this, propertyName, oldValue, newValue);      PropertyChangeEvent evt =
1001          new PropertyChangeEvent(this, propertyName, oldValue, newValue);
1002    
1003      for (int i = 0; i < listeners.length; i++)      for (int i = 0; i < listeners.length; i++)
1004        listeners[i].vetoableChange(evt);        listeners[i].vetoableChange(evt);
# Line 1009  public abstract class JComponent extends Line 1024  public abstract class JComponent extends
1024     */     */
1025    public float getAlignmentX()    public float getAlignmentX()
1026    {    {
1027      return alignmentX;      float ret = alignmentX;
1028        if (alignmentX < 0)
1029          // alignment has not been set explicitly.
1030          ret = super.getAlignmentX();
1031    
1032        return ret;
1033    }    }
1034    
1035    /**    /**
# Line 1022  public abstract class JComponent extends Line 1042  public abstract class JComponent extends
1042     */     */
1043    public float getAlignmentY()    public float getAlignmentY()
1044    {    {
1045      return alignmentY;      float ret = alignmentY;
1046        if (alignmentY < 0)
1047          // alignment has not been set explicitly.
1048          ret = super.getAlignmentY();
1049    
1050        return ret;
1051    }    }
1052    
1053    /**    /**
# Line 1604  public abstract class JComponent extends Line 1629  public abstract class JComponent extends
1629    }    }
1630    
1631    /**    /**
1632     * Return <code>true</code> if this component is currently painting a tile.     * Return <code>true</code> if this component is currently painting a tile,
1633       * this means that paint() is called again on another child component. This
1634       * method returns <code>false</code> if this component does not paint a tile
1635       * or if the last tile is currently painted.
1636     *     *
1637     * @return Whether the component is painting a tile     * @return whether the component is painting a tile
1638     */     */
1639    public boolean isPaintingTile()    public boolean isPaintingTile()
1640    {    {
1641      return false;      return paintingTile;
1642    }    }
1643    
1644    /**    /**
# Line 1675  public abstract class JComponent extends Line 1703  public abstract class JComponent extends
1703        {        {
1704          if (g.getClip() == null)          if (g.getClip() == null)
1705            g.setClip(0, 0, getWidth(), getHeight());            g.setClip(0, 0, getWidth(), getHeight());
1706          paintComponent(g);          Graphics g2 = getComponentGraphics(g);
1707          paintBorder(g);          paintComponent(g2);
1708          paintChildren(g);          paintBorder(g2);
1709          Rectangle clip = g.getClipBounds();          paintChildren(g2);
1710            Rectangle clip = g2.getClipBounds();
1711          if (clip.x == 0 && clip.y == 0 && clip.width == getWidth()          if (clip.x == 0 && clip.y == 0 && clip.width == getWidth()
1712              && clip.height == getHeight())              && clip.height == getHeight())
1713            RepaintManager.currentManager(this).markCompletelyClean(this);            RepaintManager.currentManager(this).markCompletelyClean(this);
# Line 1724  public abstract class JComponent extends Line 1753  public abstract class JComponent extends
1753      Rectangle inner = SwingUtilities.calculateInnerArea(this, rectCache);      Rectangle inner = SwingUtilities.calculateInnerArea(this, rectCache);
1754      g.clipRect(inner.x, inner.y, inner.width, inner.height);      g.clipRect(inner.x, inner.y, inner.width, inner.height);
1755      Component[] children = getComponents();      Component[] children = getComponents();
1756      for (int i = children.length - 1; i >= 0; --i)  
1757        // Find the bottommost component that needs to be painted. This is a
1758        // component that completely covers the current clip and is opaque. In
1759        // this case we don't need to paint the components below it.
1760        int startIndex = children.length - 1;
1761        // No need to check for overlapping components when this component is
1762        // optimizedDrawingEnabled (== it tiles its children).
1763        if (! isOptimizedDrawingEnabled())
1764        {        {
1765            Rectangle clip = g.getClipBounds();
1766            for (int i = 0; i < children.length; i++)
1767              {
1768                Rectangle childBounds = children[i].getBounds();
1769                if (children[i].isOpaque()
1770                    && SwingUtilities.isRectangleContainingRectangle(childBounds,
1771                                                                g.getClipBounds()))
1772                  {
1773                    startIndex = i;
1774                    break;
1775                  }
1776              }
1777          }
1778        // paintingTile becomes true just before we start painting the component's
1779        // children.
1780        paintingTile = true;
1781        for (int i = startIndex; i >= 0; --i)
1782          {
1783            // paintingTile must be set to false before we begin to start painting
1784            // the last tile.
1785            if (i == 0)
1786              paintingTile = false;
1787    
1788          if (!children[i].isVisible())          if (!children[i].isVisible())
1789            continue;            continue;
1790    
# Line 1775  public abstract class JComponent extends Line 1834  public abstract class JComponent extends
1834          Graphics g2 = g;          Graphics g2 = g;
1835          if (!(g instanceof Graphics2D))          if (!(g instanceof Graphics2D))
1836            g2 = g.create();            g2 = g.create();
1837          ui.update(getComponentGraphics(g2), this);          ui.update(g2, this);
1838          if (!(g instanceof Graphics2D))          if (!(g instanceof Graphics2D))
1839            g2.dispose();            g2.dispose();
1840        }        }
# Line 1868  public abstract class JComponent extends Line 1927  public abstract class JComponent extends
1927      g2 = getComponentGraphics(g2);      g2 = getComponentGraphics(g2);
1928      g2.setClip(r.x, r.y, r.width, r.height);      g2.setClip(r.x, r.y, r.width, r.height);
1929      isPaintingDoubleBuffered = true;      isPaintingDoubleBuffered = true;
1930      paint(g2);      try
1931      isPaintingDoubleBuffered = false;        {
1932      g2.dispose();          paint(g2);
1933          }
1934        finally
1935          {
1936            isPaintingDoubleBuffered = false;
1937            g2.dispose();
1938          }
1939            
1940      // Paint the buffer contents on screen.      // Paint the buffer contents on screen.
1941      g.drawImage(buffer, 0, 0, this);      g.drawImage(buffer, 0, 0, this);
# Line 2011  public abstract class JComponent extends Line 2076  public abstract class JComponent extends
2076          break;          break;
2077    
2078        case WHEN_IN_FOCUSED_WINDOW:        case WHEN_IN_FOCUSED_WINDOW:
2079          inputMap_whenInFocusedWindow = map;          if (map != null && !(map instanceof ComponentInputMap))
2080                throw new
2081                  IllegalArgumentException("WHEN_IN_FOCUSED_WINDOW " +
2082                                           "InputMap must be a ComponentInputMap");
2083            inputMap_whenInFocusedWindow = (ComponentInputMap)map;
2084          break;          break;
2085                    
2086        case UNDEFINED_CONDITION:        case UNDEFINED_CONDITION:
# Line 2037  public abstract class JComponent extends Line 2106  public abstract class JComponent extends
2106    
2107        case WHEN_IN_FOCUSED_WINDOW:        case WHEN_IN_FOCUSED_WINDOW:
2108          if (inputMap_whenInFocusedWindow == null)          if (inputMap_whenInFocusedWindow == null)
2109            inputMap_whenInFocusedWindow = new InputMap();            inputMap_whenInFocusedWindow = new ComponentInputMap(this);
2110          return inputMap_whenInFocusedWindow;          return inputMap_whenInFocusedWindow;
2111    
2112        case UNDEFINED_CONDITION:        case UNDEFINED_CONDITION:
# Line 2152  public abstract class JComponent extends Line 2221  public abstract class JComponent extends
2221      // 4. The WHEN_IN_FOCUSED_WINDOW maps of all the enabled components in      // 4. The WHEN_IN_FOCUSED_WINDOW maps of all the enabled components in
2222      //    the focused window are searched.      //    the focused window are searched.
2223            
2224      if (processKeyBinding(KeyStroke.getKeyStrokeForEvent(e),      KeyStroke keyStroke = KeyStroke.getKeyStrokeForEvent(e);
2225                            e, WHEN_FOCUSED, e.getID() == KeyEvent.KEY_PRESSED))      boolean pressed = e.getID() == KeyEvent.KEY_PRESSED;
2226        // This is step 1 from above comment.      
2227        e.consume();      if (processKeyBinding(keyStroke, e, WHEN_FOCUSED, pressed))
     else if (processKeyBinding(KeyStroke.getKeyStrokeForEvent(e),  
                                e, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,  
                                e.getID() == KeyEvent.KEY_PRESSED))  
       // This is step 2 from above comment.  
       e.consume();  
     else  
2228        {        {
2229          // This is step 3 from above comment.          // This is step 1 from above comment.
2230          Container current = this;          e.consume();
2231          while ((current = current.getParent()) instanceof JComponent)          return;
2232          }
2233        else if (processKeyBinding
2234                 (keyStroke, e, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, pressed))
2235          {
2236            // This is step 2 from above comment.
2237            e.consume();
2238            return;
2239          }
2240        
2241        // This is step 3 from above comment.
2242        Container current = getParent();    
2243        while (current != null)
2244          {
2245            // If current is a JComponent, see if it handles the event in its
2246            // WHEN_ANCESTOR_OF_FOCUSED_COMPONENT maps.
2247            if ((current instanceof JComponent) &&
2248                ((JComponent)current).processKeyBinding
2249                (keyStroke, e,WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, pressed))
2250            {            {
2251              if (((JComponent)current).processKeyBinding              e.consume();
2252                  (KeyStroke.getKeyStrokeForEvent(e), e,              return;
2253                   WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,            }    
                  e.getID() == KeyEvent.KEY_PRESSED))  
               {  
                 e.consume();  
                 break;  
               }  
             if (current instanceof Window || current instanceof Applet  
                 || current instanceof JInternalFrame)  
               break;  
           }  
         if (e.isConsumed())  
           return;  
2254                    
2255          // This is step 4 from above comment.          // Stop when we've tried a top-level container and it didn't handle it
2256          // FIXME: Implement.  Note, should use ComponentInputMaps rather          if (current instanceof Window || current instanceof Applet)
2257          // than walking the entire containment hierarchy.            break;        
2258            
2259            // Move up the hierarchy
2260            current = current.getParent();
2261        }        }
2262        
2263        // Current being null means the JComponent does not currently have a
2264        // top-level ancestor, in which case we don't need to check
2265        // WHEN_IN_FOCUSED_WINDOW bindings.
2266        if (current == null || e.isConsumed())
2267          return;
2268        
2269        // This is step 4 from above comment.  KeyboardManager maintains mappings
2270        // related to WHEN_IN_FOCUSED_WINDOW bindings so that we don't have to
2271        // traverse the containment hierarchy each time.
2272        if (KeyboardManager.getManager().processKeyStroke(current, keyStroke, e))
2273          e.consume();
2274    }    }
2275    
2276    protected boolean processKeyBinding(KeyStroke ks,    protected boolean processKeyBinding(KeyStroke ks,
# Line 2337  public abstract class JComponent extends Line 2422  public abstract class JComponent extends
2422     */     */
2423    public void setAlignmentX(float a)    public void setAlignmentX(float a)
2424    {    {
2425      alignmentX = a;      if (a < 0.0F)
2426          alignmentX = 0.0F;
2427        else if (a > 1.0)
2428          alignmentX = 1.0F;
2429        else
2430          alignmentX = a;
2431    }    }
2432    
2433    /**    /**
# Line 2347  public abstract class JComponent extends Line 2437  public abstract class JComponent extends
2437     */     */
2438    public void setAlignmentY(float a)    public void setAlignmentY(float a)
2439    {    {
2440      alignmentY = a;      if (a < 0.0F)
2441          alignmentY = 0.0F;
2442        else if (a > 1.0)
2443          alignmentY = 1.0F;
2444        else
2445          alignmentY = a;
2446    }    }
2447    
2448    /**    /**
# Line 2435  public abstract class JComponent extends Line 2530  public abstract class JComponent extends
2530    }    }
2531    
2532    /**    /**
2533     * Set the value of the {@link #maximumSize} property.     * Set the value of the {@link #maximumSize} property. The passed value is
2534       * copied, the later direct changes on the argument have no effect on the
2535       * property value.
2536     *     *
2537     * @param max The new value of the property     * @param max The new value of the property
2538     */     */
2539    public void setMaximumSize(Dimension max)    public void setMaximumSize(Dimension max)
2540    {    {
2541      Dimension oldMaximumSize = maximumSize;      Dimension oldMaximumSize = maximumSize;
2542      maximumSize = max;      if (max != null)
2543          maximumSize = new Dimension(max);
2544        else
2545          maximumSize = null;
2546      firePropertyChange("maximumSize", oldMaximumSize, maximumSize);      firePropertyChange("maximumSize", oldMaximumSize, maximumSize);
2547    }    }
2548    
2549    /**    /**
2550     * Set the value of the {@link #minimumSize} property.     * Set the value of the {@link #minimumSize} property. The passed value is
2551       * copied, the later direct changes on the argument have no effect on the
2552       * property value.
2553     *     *
2554     * @param min The new value of the property     * @param min The new value of the property
2555     */     */
2556    public void setMinimumSize(Dimension min)    public void setMinimumSize(Dimension min)
2557    {    {
2558      Dimension oldMinimumSize = minimumSize;      Dimension oldMinimumSize = minimumSize;
2559      minimumSize = min;      if (min != null)
2560          minimumSize = new Dimension(min);
2561        else
2562          minimumSize = null;
2563      firePropertyChange("minimumSize", oldMinimumSize, minimumSize);      firePropertyChange("minimumSize", oldMinimumSize, minimumSize);
2564    }    }
2565    
2566    /**    /**
2567     * Set the value of the {@link #preferredSize} property.     * Set the value of the {@link #preferredSize} property. The passed value is
2568       * copied, the later direct changes on the argument have no effect on the
2569       * property value.
2570     *     *
2571     * @param pref The new value of the property     * @param pref The new value of the property
2572     */     */
2573    public void setPreferredSize(Dimension pref)    public void setPreferredSize(Dimension pref)
2574    {    {
2575      Dimension oldPreferredSize = preferredSize;      Dimension oldPreferredSize = preferredSize;
2576      preferredSize = pref;      if (pref != null)
2577          preferredSize = new Dimension(pref);
2578        else
2579          preferredSize = null;
2580      firePropertyChange("preferredSize", oldPreferredSize, preferredSize);      firePropertyChange("preferredSize", oldPreferredSize, preferredSize);
2581    }    }
2582    
# Line 2797  public abstract class JComponent extends Line 2907  public abstract class JComponent extends
2907     */     */
2908    public void addNotify()    public void addNotify()
2909    {    {
2910        // Register the WHEN_IN_FOCUSED_WINDOW keyboard bindings
2911        // Note that here we unregister all bindings associated with
2912        // this component and then re-register them.  This may be more than
2913        // necessary if the top-level ancestor hasn't changed.  Should
2914        // maybe improve this.
2915        KeyboardManager km = KeyboardManager.getManager();
2916        km.clearBindingsForComp(this);
2917        km.registerEntireMap((ComponentInputMap)
2918                             this.getInputMap(WHEN_IN_FOCUSED_WINDOW));
2919      super.addNotify();      super.addNotify();
2920    
     // let parents inherit the keybord mapping  
     InputMap input = getInputMap();  
     ActionMap actions = getActionMap();  
   
     Container parent = getParent();  
     while ((parent != null) && (parent instanceof JComponent))  
       {  
         JComponent jParent = (JComponent) parent;  
         InputMap parentInput = jParent.getInputMap();  
         ActionMap parentAction = jParent.getActionMap();  
   
         KeyStroke[] ikeys = input.keys();  
         for (int i = 0; i < ikeys.length; i++)  
           {  
             Object o = input.get(ikeys[i]);  
             parentInput.put(ikeys[i], o);  
           }  
   
         Object[] akeys = actions.keys();  
         for (int i = 0; i < akeys.length; i++)  
           {  
             Action a = actions.get(akeys[i]);  
             parentAction.put(akeys[i], a);  
           }  
   
         parent = jParent.getParent();  
       }  
       
2921      // Notify AncestorListeners.      // Notify AncestorListeners.
2922      fireAncestorEvent(this, AncestorEvent.ANCESTOR_ADDED);      fireAncestorEvent(this, AncestorEvent.ANCESTOR_ADDED);
2923    
2924      // fire property change event for 'ancestor'      // fire property change event for 'ancestor'
2925      firePropertyChange("ancestor", null, parent);      firePropertyChange("ancestor", null, getParent());
2926    }    }
2927    
2928    /**    /**
# Line 2854  public abstract class JComponent extends Line 2945  public abstract class JComponent extends
2945    {    {
2946      super.removeNotify();      super.removeNotify();
2947    
2948      // let parents inherit the keybord mapping      KeyboardManager.getManager().clearBindingsForComp(this);
2949      InputMap input = getInputMap();      
     ActionMap actions = getActionMap();  
   
     Container parent = getParent();  
     while ((parent != null) && (parent instanceof JComponent))  
       {  
         JComponent jParent = (JComponent) parent;  
         InputMap parentInput = jParent.getInputMap();  
         ActionMap parentAction = jParent.getActionMap();  
   
         KeyStroke[] ikeys = input.allKeys();  
         for (int i = 0; i < ikeys.length; i++)  
           {  
             parentInput.remove(ikeys[i]);  
           }  
   
         Object[] akeys = actions.allKeys();  
         for (int i = 0; i < akeys.length; i++)  
           {  
             parentAction.remove(akeys[i]);  
           }  
   
         parent = jParent.getParent();  
       }  
   
2950      // Notify ancestor listeners.      // Notify ancestor listeners.
2951      fireAncestorEvent(this, AncestorEvent.ANCESTOR_REMOVED);      fireAncestorEvent(this, AncestorEvent.ANCESTOR_REMOVED);
2952    
2953      // fire property change event for 'ancestor'      // fire property change event for 'ancestor'
2954      firePropertyChange("ancestor", parent, null);      firePropertyChange("ancestor", getParent(), null);
2955    }    }
2956    
2957    /**    /**
# Line 3271  public abstract class JComponent extends Line 3338  public abstract class JComponent extends
3338        }        }
3339      return found;      return found;
3340    }    }
3341      
3342      /**
3343       * This is the method that gets called when the WHEN_IN_FOCUSED_WINDOW map
3344       * is changed.
3345       *
3346       * @param changed the JComponent associated with the WHEN_IN_FOCUSED_WINDOW
3347       *        map
3348       */
3349      void updateComponentInputMap(ComponentInputMap changed)
3350      {
3351        // Since you can change a component's input map via
3352        // setInputMap, we have to check if <code>changed</code>
3353        // is still in our WHEN_IN_FOCUSED_WINDOW map hierarchy
3354        InputMap curr = getInputMap(WHEN_IN_FOCUSED_WINDOW);
3355        while (curr != null && curr != changed)
3356          curr = curr.getParent();
3357        
3358        // If curr is null then changed is not in the hierarchy
3359        if (curr == null)
3360          return;
3361        
3362        // Now we have to update the keyboard manager's hashtable
3363        KeyboardManager km = KeyboardManager.getManager();
3364        
3365        // This is a poor strategy, should be improved.  We currently
3366        // delete all the old bindings for the component and then register
3367        // the current bindings.
3368        km.clearBindingsForComp(changed.getComponent());
3369        km.registerEntireMap((ComponentInputMap)
3370                             getInputMap(WHEN_IN_FOCUSED_WINDOW));
3371      }
3372  }  }

Legend:
Removed from v.1.20.2.17  
changed lines
  Added in v.1.20.2.18

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26