001 /* 002 * Copyright 2002-2004 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016 package org.springframework.richclient.image; 017 018 import java.awt.Color; 019 import java.awt.Component; 020 import java.awt.Graphics; 021 import java.awt.SystemColor; 022 023 import javax.swing.Icon; 024 025 import org.springframework.util.Assert; 026 027 /** 028 * A directional arrow icon; the direction can either be UP or DOWN. 029 * 030 * @author Keith Donald 031 * @see ArrowIcon.Direction 032 */ 033 public class ArrowIcon implements Icon { 034 035 /** 036 * Type-safe enum for the ArrowIcon's directional capability. 037 * <p> 038 * Currently only up and down are supported since this is primarily designed 039 * to be a table column sort indicator. 040 * 041 * @author Keith Donald 042 */ 043 public static class Direction { 044 public static final Direction DOWN = new Direction(0); 045 046 public static final Direction UP = new Direction(1); 047 048 private int value; 049 050 private Direction(int value) { 051 this.value = value; 052 } 053 054 public int getValue() { 055 return value; 056 } 057 058 public String toString() { 059 return String.valueOf(value); 060 } 061 } 062 063 private Direction direction; 064 065 private int size; 066 067 private Color color; 068 069 /** 070 * Creates a ArrowIcon in the specified direction with the default size and 071 * color. 072 * 073 * @param direction 074 * The icon direction. 075 */ 076 public ArrowIcon(Direction direction) { 077 setDirection(direction); 078 setSize(4); 079 setColor(SystemColor.controlDkShadow); 080 } 081 082 /** 083 * Creates a ArrowIcon pointing in the specified direction with the 084 * specified size and color. 085 * 086 * @param direction 087 * the direction the arrow should point. 088 * @param size 089 * the size of the arrow in pixels (4 is a good one) 090 * @param color 091 * the color of the arrow (consider using UIDefaults for current 092 * L&F) 093 */ 094 public ArrowIcon(Direction direction, int size, Color color) { 095 setDirection(direction); 096 setSize(size); 097 setColor(color); 098 } 099 100 public void paintIcon(Component c, Graphics g, int x, int y) { 101 g.setColor(color); 102 for (int i = 0; i < size; i++) { 103 if (direction == Direction.UP) { 104 g.drawLine(x + (size - (i + 1)), y + i, x + (size + i), y + i); 105 } 106 else { 107 g.drawLine(x + i, y + i, x + (size * 2 - (i + 1)), y + i); 108 } 109 } 110 } 111 112 public int getIconWidth() { 113 return size * 2; 114 } 115 116 public int getIconHeight() { 117 return size; 118 } 119 120 /** 121 * @param size 122 */ 123 private void setSize(int size) { 124 this.size = size; 125 } 126 127 /** 128 * @param color 129 */ 130 private void setColor(Color color) { 131 Assert.notNull(color); 132 this.color = color; 133 } 134 135 /** 136 * @param direction 137 */ 138 private void setDirection(Direction direction) { 139 this.direction = direction; 140 } 141 142 public String toString() { 143 StringBuffer buf = new StringBuffer("[ArrowIcon").append(" direction=").append(direction).append(" size=") 144 .append(size).append(" color=").append(color); 145 return buf.toString(); 146 } 147 148 }