View Javadoc
1   /*
2    * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package com.apple.laf;
27  
28  import java.awt.*;
29  
30  import javax.swing.*;
31  import javax.swing.border.Border;
32  import javax.swing.plaf.UIResource;
33  import javax.swing.text.JTextComponent;
34  
35  import apple.laf.*;
36  import apple.laf.JRSUIConstants.*;
37  
38  import com.apple.laf.AquaUtilControlSize.*;
39  
40  public abstract class AquaBorder implements Border, UIResource {
41      protected final AquaPainter<? extends JRSUIState> painter;
42      protected final SizeDescriptor sizeDescriptor;
43      protected SizeVariant sizeVariant;
44  
45      protected AquaBorder(final SizeDescriptor sizeDescriptor) {
46          this.sizeDescriptor = sizeDescriptor;
47          this.sizeVariant = sizeDescriptor.get(Size.REGULAR);
48          this.painter = createPainter();
49      }
50  
51      protected AquaPainter<? extends JRSUIState> createPainter() {
52          final AquaPainter<JRSUIState> painter = AquaPainter.create(JRSUIState.getInstance());
53          painter.state.set(AlignmentVertical.CENTER);
54          painter.state.set(AlignmentHorizontal.CENTER);
55          return painter;
56      }
57  
58      protected AquaBorder(final AquaBorder other) {
59          this.sizeDescriptor = other.sizeDescriptor;
60          this.sizeVariant = other.sizeVariant;
61          this.painter = AquaPainter.create(other.painter.state.derive());
62          painter.state.set(AlignmentVertical.CENTER);
63          painter.state.set(AlignmentHorizontal.CENTER);
64      }
65  
66      protected void setSize(final Size size) {
67          sizeVariant = sizeDescriptor.get(size);
68          painter.state.set(size);
69      }
70  
71      public Insets getBorderInsets(final Component c) {
72          return sizeVariant.margins;
73      }
74  
75      protected AquaBorder deriveBorderForSize(final Size size) {
76          try {
77              final Class<? extends AquaBorder> clazz = getClass();
78              final AquaBorder border = clazz.getConstructor(new Class[] { clazz }).newInstance(new Object[] { this });
79              border.setSize(size);
80              return border;
81          } catch (final Throwable e) {
82              return null;
83          }
84      }
85  
86      public static void repaintBorder(final JComponent c) {
87          JComponent borderedComponent = c;
88          Border border = c.getBorder();
89          if (border == null) {
90              // See if it's inside a JScrollpane or something
91              final Container p = c.getParent();
92              if (p instanceof JViewport) {
93                  borderedComponent = (JComponent)p.getParent();
94                  if (borderedComponent != null) border = borderedComponent.getBorder();
95              }
96          }
97  
98          // If we really don't have a border, then bail
99          // It might be a compound border with a ThemeBorder inside
100         // The check for that case is tricky, so we just go ahead and repaint any border
101         if (border == null || borderedComponent == null) return;
102 
103         final int width = borderedComponent.getWidth();
104         final int height = borderedComponent.getHeight();
105         final Insets i = borderedComponent.getInsets();
106 
107         borderedComponent.repaint(0, 0, width, i.top); // Top edge
108         borderedComponent.repaint(0, 0, i.left, height); // Left edge
109         borderedComponent.repaint(0, height - i.bottom, width, i.bottom); // Bottom edge
110         borderedComponent.repaint(width - i.right, 0, i.right, height); // Right edge
111     }
112 
113     // The JScrollPane doesn't let us know if its viewport view has focus
114     protected boolean isFocused(final Component c) {
115         // Being really paranoid in case this Component isn't a Swing component
116         Component focusable = c;
117 
118         if (c instanceof JScrollPane) {
119             final JViewport vp = ((JScrollPane)c).getViewport();
120             if (vp != null) {
121                 focusable = vp.getView();
122                 // Lists, Tables & Trees get focus rings, TextAreas don't (JBuilder puts TextField border on TextAreas)
123                 if (focusable instanceof JTextComponent) return false;
124             }
125         } else if (focusable instanceof JTextComponent) {
126             // non-editable text areas don't draw the focus ring
127             if (!((javax.swing.text.JTextComponent)focusable).isEditable()) return false;
128         }
129 
130         return (focusable != null && focusable instanceof JComponent && ((JComponent)focusable).hasFocus());
131     }
132 
133     public boolean isBorderOpaque() { return false; }
134 
135     public void paintBorder(final Component c, final Graphics g, final int x, final int y, final int w, final int h) {
136         painter.paint(g, c, x, y, w, h);
137     }
138 
139     static class Default extends AquaBorder {
140         Default() { super(new SizeDescriptor(new SizeVariant())); }
141     }
142 }