View Javadoc

1   package com.explosion.datastream.exql.gui.table;
2   
3   import java.awt.Color;
4   import java.awt.Component;
5   import java.awt.Graphics;
6   
7   import javax.swing.Icon;
8   
9   import org.apache.log4j.LogManager;
10  import org.apache.log4j.Logger;
11  
12  /*
13   * =============================================================================
14   * 
15   * Copyright 2004 Stephen Cowx
16   * 
17   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
18   * use this file except in compliance with the License. You may obtain a copy of
19   * the License at
20   * 
21   * http://www.apache.org/licenses/LICENSE-2.0
22   * 
23   * Unless required by applicable law or agreed to in writing, software
24   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
26   * License for the specific language governing permissions and limitations under
27   * the License.
28   * 
29   * =============================================================================
30   */
31  
32  /***
33   * Copied from Sun class Table Sorter written by Philip Milne, Brendon McLean,
34   * Dan van Enckevort, Parwinder Sekhon
35   * 
36   * @author Stephen Cowx Created on Apr 20, 2004
37   */
38  public class Arrow implements Icon
39  {
40      private static Logger log = LogManager.getLogger(Arrow.class);
41      private boolean descending;
42      private int size;
43      private int priority;
44  
45      public Arrow(String name, boolean descending, int size, int priority)
46      {
47          //log.debug("Arrow: "+name+" descending " + descending + ", size" +size + ", priority"+priority);
48          this.descending = descending;
49          this.size = size;
50          this.priority = priority;
51      }
52  
53      public void paintIcon(Component c, Graphics g, int x, int y)
54      {
55          log.debug("paintIcon :" + c.hashCode() );
56          Color color = c == null ? Color.GRAY : c.getBackground();
57          // In a compound sort, make each succesive triangle 20%
58          // smaller than the previous one.
59          int dx = (int) (size / 2 * Math.pow(0.8, priority));
60          int dy = descending ? dx : -dx;
61          // Align icon (roughly) with font baseline.
62          y = y + 5 * size / 6 + (descending ? -dy : 0);
63          int shift = descending ? 1 : -1;
64          g.translate(x, y);
65  
66          // Right diagonal.
67          g.setColor(color.darker());
68          g.drawLine(dx / 2, dy, 0, 0);
69          g.drawLine(dx / 2, dy + shift, 0, shift);
70  
71          // Left diagonal.
72          g.setColor(color.brighter());
73          g.drawLine(dx / 2, dy, dx, 0);
74          g.drawLine(dx / 2, dy + shift, dx, shift);
75  
76          // Horizontal line.
77          if (descending)
78          {
79              g.setColor(color.darker().darker());
80          } else
81          {
82              g.setColor(color.brighter().brighter());
83          }
84          g.drawLine(dx, 0, 0, 0);
85  
86          g.setColor(color);
87          g.translate(-x, -y);
88      }
89  
90      public int getIconWidth()
91      {
92          return size;
93      }
94  
95      public int getIconHeight()
96      {
97          return size;
98      }
99  }