001: package auction.controller;
002:
003: import com.xoetrope.swing.list.XAltListCellRenderer;
004: import java.awt.BorderLayout;
005: import java.awt.Color;
006: import java.awt.Component;
007: import java.awt.Font;
008: import javax.swing.JLabel;
009: import javax.swing.JList;
010: import net.xoetrope.swing.XImage;
011: import net.xoetrope.xui.data.XModel;
012: import net.xoetrope.xui.style.XStyle;
013:
014: /**
015: * Renders a search result list.
016: */
017: public class ResultListCellRenderer extends XAltListCellRenderer {
018: private static final int CELL_HEIGHT = 50;
019: private static final int IMAGE_WIDTH = 40;
020: private static final int IMAGE_GAP = 4;
021: private static final int PRICE_WIDTH = 200;
022: private static final int PRICEVAL_WIDTH = 100;
023: private static final int TITLE_HEIGHT = 14;
024: private static final int AUTHOR_HEIGHT = 11;
025: private static final int PRICE_GAP_X = 20;
026: private static final int PRICE_GAP_Y = 3;
027:
028: private static final Font TITLE_FONT = new Font("Arial", Font.BOLD,
029: 11);
030: private static final Font AUTHOR_FONT = new Font("Arial",
031: Font.PLAIN, 11);
032: private static final Font PRICE_FONT = new Font("Arial",
033: Font.PLAIN, 11);
034: private static final Font PRICEVAL_FONT = new Font("Arail",
035: Font.BOLD, 11);
036: private static final Font DESC_FONT = new Font("Arial", Font.PLAIN,
037: 10);
038:
039: private XModel resultListModel;
040:
041: private JLabel titleLabel;
042: private JLabel authorLabel;
043: private JLabel minPriceLabel;
044: private JLabel reservePriceLabel;
045: private JLabel minPriceValLabel;
046: private JLabel reservePriceValLabel;
047: private JLabel descLabel;
048: private XImage image;
049:
050: private Color unselectedBackground;
051:
052: /**
053: * Creates a new instance of ResultListCellRenderer
054: */
055: public ResultListCellRenderer(XModel searchResultListModel) {
056: resultListModel = searchResultListModel;
057:
058: titleLabel = new JLabel();
059: titleLabel.setFont(TITLE_FONT);
060: titleLabel.setHorizontalAlignment(JLabel.CENTER);
061:
062: authorLabel = new JLabel();
063: authorLabel.setFont(AUTHOR_FONT);
064: authorLabel.setHorizontalAlignment(JLabel.LEFT);
065:
066: minPriceLabel = new JLabel();
067: minPriceLabel.setFont(PRICE_FONT);
068: minPriceLabel.setHorizontalAlignment(JLabel.LEFT);
069:
070: minPriceValLabel = new JLabel();
071: minPriceValLabel.setFont(PRICEVAL_FONT);
072: minPriceValLabel.setHorizontalAlignment(JLabel.RIGHT);
073:
074: reservePriceLabel = new JLabel();
075: reservePriceLabel.setFont(PRICE_FONT);
076: reservePriceLabel.setHorizontalAlignment(JLabel.LEFT);
077:
078: reservePriceValLabel = new JLabel();
079: reservePriceValLabel.setFont(PRICEVAL_FONT);
080: reservePriceValLabel.setHorizontalAlignment(JLabel.RIGHT);
081:
082: descLabel = new JLabel();
083: descLabel.setFont(DESC_FONT);
084: descLabel.setHorizontalAlignment(JLabel.LEFT);
085:
086: image = new XImage();
087:
088: add(image);
089: add(authorLabel);
090: add(titleLabel);
091: add(descLabel);
092: add(minPriceLabel);
093: add(minPriceValLabel);
094: add(reservePriceLabel);
095: add(reservePriceValLabel);
096:
097: unselectedBackground = new Color(213, 247, 251);
098: }
099:
100: /**
101: * Gets the cell renderer component
102: */
103: public Component getListCellRendererComponent(JList list,
104: Object value, int index, boolean isSelected,
105: boolean cellHasFocus) {
106: Component renderer = super .getListCellRendererComponent(list,
107: value, index, isSelected, cellHasFocus);
108: renderer.setFont(list.getFont());
109:
110: if (!isSelected && (index % 2 == 0))
111: renderer.setBackground(unselectedBackground);
112:
113: // set component bounds
114: image.setBounds(IMAGE_GAP, IMAGE_GAP, IMAGE_WIDTH - 2
115: * IMAGE_GAP, CELL_HEIGHT - 2 * IMAGE_GAP);
116:
117: titleLabel.setBounds(IMAGE_WIDTH, 1, list.getWidth()
118: - IMAGE_WIDTH - PRICE_WIDTH, AUTHOR_HEIGHT);
119:
120: authorLabel.setBounds(IMAGE_WIDTH, AUTHOR_HEIGHT + 1, list
121: .getWidth()
122: - IMAGE_WIDTH - PRICE_WIDTH, TITLE_HEIGHT);
123:
124: descLabel.setBounds(IMAGE_WIDTH, AUTHOR_HEIGHT + TITLE_HEIGHT
125: + 1, list.getWidth() - IMAGE_WIDTH - PRICE_WIDTH,
126: CELL_HEIGHT - TITLE_HEIGHT - AUTHOR_HEIGHT - 1);
127:
128: minPriceLabel.setBounds(list.getWidth() - PRICE_WIDTH,
129: 1 + PRICE_GAP_Y, PRICE_WIDTH - PRICEVAL_WIDTH,
130: CELL_HEIGHT / 2 - PRICE_GAP_Y);
131:
132: minPriceValLabel.setBounds(list.getWidth() - PRICEVAL_WIDTH
133: - PRICE_GAP_X, 1 + PRICE_GAP_Y, PRICEVAL_WIDTH,
134: CELL_HEIGHT / 2 - PRICE_GAP_Y);
135:
136: reservePriceLabel
137: .setBounds(list.getWidth() - PRICE_WIDTH, CELL_HEIGHT
138: / 2 + 1 - PRICE_GAP_Y, PRICE_WIDTH
139: - PRICEVAL_WIDTH, CELL_HEIGHT / 2 - PRICE_GAP_Y);
140:
141: reservePriceValLabel.setBounds(list.getWidth() - PRICEVAL_WIDTH
142: - PRICE_GAP_X, CELL_HEIGHT / 2 + 1 - PRICE_GAP_Y,
143: PRICEVAL_WIDTH, CELL_HEIGHT / 2 - PRICE_GAP_Y);
144:
145: // set captions
146: XModel bookModel = (XModel) resultListModel.get(index);
147: String title = (String) ((XModel) bookModel.get("title")).get();
148: String author = (String) ((XModel) bookModel.get("author"))
149: .get();
150: String desc = (String) ((XModel) bookModel.get("description"))
151: .get();
152: String imageName = (String) ((XModel) bookModel.get("image"))
153: .get();
154: String minPrice = (String) ((XModel) bookModel
155: .get("initialPriceEur")).get();
156: String reservePrice = (String) ((XModel) bookModel
157: .get("reservePriceEur")).get();
158:
159: image.setImageName(imageName);
160: titleLabel.setText(title);
161: authorLabel.setText(author);
162: descLabel.setText(desc);
163: minPriceLabel.setText("initial price:");
164: minPriceValLabel.setText(minPrice);
165: reservePriceLabel.setText("reserve price:");
166: reservePriceValLabel.setText(reservePrice);
167:
168: // ???
169: ((JLabel) renderer).setText(null);
170:
171: return renderer;
172: }
173:
174: }
|