001: /*
002: * To change this template, choose Tools | Templates
003: * and open the template in the editor.
004: */
005:
006: package it.businesslogic.ireport.gui.box;
007:
008: import java.awt.BasicStroke;
009: import java.awt.Color;
010: import java.awt.Component;
011: import java.awt.Dimension;
012: import java.awt.Graphics;
013: import java.awt.Graphics2D;
014: import java.awt.Stroke;
015: import javax.swing.JComponent;
016: import javax.swing.JList;
017: import javax.swing.ListCellRenderer;
018: import javax.swing.UIManager;
019:
020: /**
021: *
022: * @author gtoffoli
023: */
024: public class LineStyleListCellRenderer extends JComponent implements
025: ListCellRenderer {
026:
027: private Color selectionBackground = null;
028: private Color background = null;
029: private Color selectionForeground = null;
030: private Color foreground = null;
031:
032: private String styleName = "";
033:
034: public LineStyleListCellRenderer() {
035: setOpaque(true);
036: setBackground(Color.WHITE);
037: selectionBackground = UIManager
038: .getColor("List.selectionBackground");
039: background = UIManager.getColor("List.background");
040: selectionForeground = UIManager
041: .getColor("List.selectionForeground");
042: foreground = UIManager.getColor("List.foreground");
043:
044: setMinimumSize(new Dimension(20, 16));
045: setPreferredSize(new Dimension(20, 16));
046: }
047:
048: public Component getListCellRendererComponent(JList list,
049: Object value, int index, boolean isSelected,
050: boolean cellHasFocus) {
051:
052: if (isSelected) {
053: setForeground(selectionForeground);
054: setBackground(selectionBackground);
055: } else {
056: setForeground(foreground);
057: setBackground(background);
058: }
059: styleName = "" + value;
060: repaint();
061: return this ;
062:
063: }
064:
065: @Override
066: public void paint(Graphics g) {
067: super .paint(g);
068:
069: g.setColor(getBackground());
070: g.fillRect(0, 0, getWidth(), getHeight());
071: if (styleName != null) {
072: Stroke oldStroke = ((Graphics2D) g).getStroke();
073:
074: Stroke stroke = null;
075: float penWidth = 1f;
076: if (styleName.equals("Solid")) {
077: stroke = (Stroke) new BasicStroke((float) (penWidth));
078: } else if (styleName.equals("Dashed")) {
079: stroke = (Stroke) new BasicStroke((float) (penWidth),
080: BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL,
081: 0f, new float[] { 5f, 3f }, 0f);
082: } else if (styleName.equals("Dotted")) {
083: stroke = (Stroke) new BasicStroke((float) (penWidth),
084: BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL,
085: 0f,
086: new float[] { 1f * penWidth, 1f * penWidth },
087: 0f);
088: } else if (styleName.equals("Double")) {
089: stroke = (Stroke) new BasicStroke(
090: (float) (penWidth / 3f));
091: }
092:
093: if (stroke != null) {
094: ((Graphics2D) g).setStroke(stroke);
095:
096: g.setColor(getForeground());
097:
098: if (!styleName.equals("Double")) {
099: ((Graphics2D) g).drawLine(5, getHeight() / 2,
100: getWidth() - 5, getHeight() / 2);
101: } else {
102: ((Graphics2D) g).drawLine(5, (getHeight() / 2) - 1,
103: getWidth() - 5, (getHeight() / 2) - 1);
104: ((Graphics2D) g).drawLine(5, (getHeight() / 2) + 1,
105: getWidth() - 5, (getHeight() / 2) + 1);
106: }
107: }
108: ((Graphics2D) g).setStroke(oldStroke);
109: }
110:
111: g.setPaintMode();
112: g.setColor(Color.LIGHT_GRAY);
113: ((Graphics2D) g).drawLine(0, getHeight() - 1, getWidth(),
114: getHeight() - 1);
115: }
116:
117: }
|