View Javadoc
1   /*
2    * #%L
3    * JRst :: Api
4    * %%
5    * Copyright (C) 2004 - 2010 CodeLutin
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  
23  package org.nuiton.jrst;
24  
25  import java.awt.Color;
26  import java.awt.Dimension;
27  import java.awt.FontMetrics;
28  import java.awt.Graphics2D;
29  import java.awt.Rectangle;
30  import java.awt.RenderingHints;
31  import java.awt.event.AdjustmentEvent;
32  import java.awt.event.AdjustmentListener;
33  import java.beans.PropertyChangeEvent;
34  import java.beans.PropertyChangeListener;
35  
36  import javax.swing.JLabel;
37  import javax.swing.JScrollPane;
38  import javax.swing.text.JTextComponent;
39  
40  import org.junit.Ignore;
41  
42  /**
43   * A class which adds line numbering to a editor
44   * 
45   * @author Yves Zoundi
46   */
47  @Ignore
48  public final class GutterColor extends JLabel {
49      /** serialVersionUID */
50      private static final long serialVersionUID = -4419253280014127529L;
51  
52      private JTextComponent edit;
53      private int rhWidth = 40;
54      boolean[] bColors;
55  
56      /**
57       * 
58       * @param edit
59       *            the editor which has to display line numbers
60       * @param pane 
61       * @param bColors 
62       */
63      public GutterColor(JTextComponent edit, JScrollPane pane, boolean[] bColors) {
64          this.edit = edit;
65          this.bColors = bColors;
66          // listen for font change
67          edit.addPropertyChangeListener(new PropertyChangeListener() {
68              public void propertyChange(PropertyChangeEvent evt) {
69                  if (evt.getPropertyName().equals("font")) {
70                      revalidate();
71                      repaint();
72                  }
73              }
74          });
75  
76          setBackground(Color.GRAY);
77          setForeground(Color.GRAY.darker());
78  
79          pane.getVerticalScrollBar().addAdjustmentListener(
80                  new AdjustmentListener() {
81                      public void adjustmentValueChanged(AdjustmentEvent e) {
82                          revalidate();
83                          repaint();
84                      }
85                  });
86      }
87  
88      /**
89       * 
90       * @return The preferred size of the line numbering column
91       */
92      public Dimension getPreferredSize() {
93          FontMetrics fm = edit.getFontMetrics(edit.getFont());
94          int h = fm.getHeight();
95          int w = fm.stringWidth(String.valueOf(edit.getHeight() / h)) + 6;
96  
97          rhWidth = w;
98          int hi = (int) edit.getPreferredSize().getHeight();
99          return new Dimension(w, hi);
100     }
101 
102     /**
103      * 
104      * @param g
105      *            A graphic component
106      */
107     public void paintComponent(java.awt.Graphics g) {
108         super.paintComponent(g);
109 
110         Graphics2D g2d = (Graphics2D) g;
111         g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
112                 RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
113 
114         g2d.setFont(edit.getFont());
115         FontMetrics fm = g2d.getFontMetrics();
116 
117         Rectangle rec = edit.getVisibleRect();
118 
119         int h = fm.getHeight();
120         int row = rec.y / h;
121         int i = h - fm.getDescent();
122         i += h * row;
123         int max = row + (rec.height / h) + 2;
124 
125         boolean doWhite = false;
126         while (row < max) {
127             String s = Integer.toString(row + 1) + "  ";
128             boolean white = false;
129             if (bColors.length > row + 1) {
130                 if (bColors[row + 1]) {
131                     g2d.setColor(Color.RED);
132                     g2d.fill3DRect(1, i + 3, rhWidth - 4, 15, false);
133                     white = true;
134 
135                 } else
136                     white = false;
137             } else
138                 white = false;
139             if (doWhite) {
140                 g2d.setColor(Color.WHITE);
141                 g2d.drawString(s, (rhWidth + 9) - fm.stringWidth(s), i);
142             } else {
143                 g2d.setColor(Color.GRAY.darker());
144                 g2d.drawString(s, (rhWidth + 9) - fm.stringWidth(s), i);
145             }
146             doWhite = white;
147 
148             i += h;
149             row++;
150         }
151 
152         g2d.setColor(Color.GRAY.darker());
153         g2d.drawLine(getWidth() - 3, rec.y, getWidth() - 3, rec.height + rec.y);
154     }
155 }