View Javadoc

1   /*
2    * Copyright (c) 2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.aequo.gui;
14  
15  import java.awt.BasicStroke;
16  import java.awt.Component;
17  import java.awt.Graphics;
18  import java.awt.Graphics2D;
19  import java.awt.Insets;
20  import java.awt.Stroke;
21  
22  import javax.swing.border.AbstractBorder;
23  
24  /**
25   * Border used for focus that can be easily manipulated.
26   *
27   * @author Daniel Sendula
28   */
29  public class RowFocusBorder extends AbstractBorder {
30  
31      /** Dashed focused cell stroke */
32      protected static BasicStroke stroke = new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, new float[]{2.0f}, 1.0f);
33      
34      /** Left margin */
35      protected int leftMargin;
36      
37      /** Right margin */
38      protected int rightMargin;
39      
40      /** Top margin */
41      protected int topMargin;
42      
43      /** Bottom margin */
44      protected int bottomMargin;
45  
46      /** Is cell focused or not */
47      protected boolean focused = false;
48      
49      /** Is this first cell in the row? Paints top, left and bottom lines */
50      public static int FIRST_CELL_IN_ROW = 1;
51      
52      /** Is this only cell in the row? Paints all 4 lines */
53      public static int ONLY_CELL_IN_ROW = 2;
54      
55      /** Is this last cell in the row? Paints top, right and bottom lines */
56      public static int LAST_CELL_IN_ROW = 3;
57      
58      /** Is this middle cell in the row? Paints top and bottom lines */
59      public static int MIDDLE_CELL_IN_ROW = 4;
60      
61      /** 
62       * Cell type
63       * @see #FIRST_CELL_IN_ROW
64       * @see #ONLY_CELL_IN_ROW
65       * @see #LAST_CELL_IN_ROW
66       * @see #MIDDLE_CELL_IN_ROW
67       */ 
68      protected int cellType = ONLY_CELL_IN_ROW;
69      
70      /**
71       * Constructor
72       */
73      public RowFocusBorder() {
74      }
75  
76      /**
77       * Sets if border is focused
78       * @param focused is focused
79       */
80      public void setFocused(boolean focused) {
81          this.focused = focused;
82      }
83      
84      /**
85       * Sets left margin
86       * @param pixels left margin
87       */
88      public void setLeftMargin(int pixels) {
89          leftMargin = pixels;
90      }
91  
92      /**
93       * Sets right margin
94       * @param pixels right margin
95       */
96      public void setRightMargin(int pixels) {
97          rightMargin = pixels;
98      }
99      
100     /**
101      * Sets top margin
102      * @param pixels top margin
103      */
104     public void setTopMargin(int pixels) {
105         topMargin = pixels;
106     }
107 
108     /**
109      * Sets bottom margin
110      * @param pixels bottom margin
111      */
112     public void setBottomMargin(int pixels) {
113         bottomMargin = pixels;
114     }
115 
116     /**
117      * Sets cell type
118      * @param cellType cell type
119      */
120     public void setCellType(int cellType) {
121         this.cellType = cellType;
122     }
123 
124     /**
125      * Returns border insets
126      * @param c component
127      * @return border insets
128      */
129     public Insets getBorderInsets(Component c)       {
130         return new Insets(topMargin + 1, leftMargin + 1, bottomMargin + 1, rightMargin + 1);
131     }
132 
133     /**
134      * Returns border insets
135      * @param c component
136      * @param insets insets to be used
137      * @return border insets
138      */
139     public Insets getBorderInsets(Component c, Insets insets) {
140         insets.left = leftMargin + 1;
141         insets.top = topMargin + 1;
142         insets.right = rightMargin + 1;
143         insets.bottom = bottomMargin + 1;
144         return insets;
145     }
146 
147     /**
148      * Returns <code>false</code>
149      * @return <code>false</code>
150      */
151     public boolean isBorderOpaque() { return false; }
152 
153     
154     /**
155      * Paints border
156      * @param c component
157      * @param g graphics
158      * @param x x
159      * @param y y 
160      * @param width width
161      * @param height height
162      */
163     public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
164         if (focused) {
165             int w = width - 1;
166             int h = height - 1;
167             
168             Graphics2D g2 = (Graphics2D)g;
169             
170             g.translate(x, y);
171 
172             Stroke oldStroke = g2.getStroke();
173             g2.setStroke(stroke);
174             
175             g.setColor(c.getBackground().brighter());
176 
177             if (cellType == FIRST_CELL_IN_ROW || cellType == ONLY_CELL_IN_ROW) {
178                 g.drawLine(0, 0, 0, h);
179             }
180             if (cellType == LAST_CELL_IN_ROW || cellType == ONLY_CELL_IN_ROW) {
181                 g.drawLine(w, 0, w, h);
182             }
183             g.drawLine(0, 0, w, 0);
184             g.drawLine(0, h, w, h);
185 
186             g2.setStroke(oldStroke);
187             g.translate(-x, -y);
188         }
189     }
190     
191 }