001: package com.xoetrope.carousel.catalog.templates.javaclasses;
002:
003: import com.xoetrope.carousel.build.BuildProperties;
004: import com.xoetrope.svg.XSvgImageMap;
005: import com.xoetrope.swing.list.XAltListCellRenderer;
006: import java.awt.AlphaComposite;
007: import java.awt.Color;
008: import java.awt.Component;
009: import java.awt.Composite;
010: import java.awt.Font;
011: import java.awt.FontMetrics;
012: import java.awt.Graphics;
013: import java.awt.Graphics2D;
014: import java.awt.Image;
015: import java.io.File;
016: import java.lang.reflect.Method;
017: import java.net.MalformedURLException;
018: import java.text.BreakIterator;
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.LinkedList;
022: import javax.accessibility.AccessibleContext;
023: import javax.swing.DefaultListCellRenderer;
024: import javax.swing.DefaultListModel;
025: import javax.swing.ImageIcon;
026: import javax.swing.JComponent;
027: import javax.swing.JLabel;
028: import javax.swing.JList;
029: import javax.swing.JPanel;
030: import javax.swing.JTextArea;
031: import javax.swing.ListModel;
032: import javax.swing.UIManager;
033: import javax.swing.event.AncestorListener;
034: import net.xoetrope.optional.svg.XSvgButton;
035: import net.xoetrope.optional.svg.svgsalamander.XSvgPainter;
036: import net.xoetrope.swing.XImage;
037: import net.xoetrope.swing.XList;
038: import net.xoetrope.swing.dnd.XDragInfo;
039: import net.xoetrope.xui.XProject;
040: import net.xoetrope.xui.XProjectManager;
041: import net.xoetrope.xui.helper.XuiUtilities;
042: import net.xoetrope.xui.style.XStyle;
043: import net.xoetrope.xui.style.XStyleManager;
044:
045: /**
046: * Renders a combobox list with an image icon
047: */
048: public class XGraphicListCellRenderer extends XAltListCellRenderer {
049: private XStyle comboStyle;
050: private ArrayList imageNames;
051: private XStyleManager styleManager;
052: private String previousEntry;
053: private boolean first;
054: private HashMap info;
055: private JLabel totalPrice;
056: private JPanel detailsPanel;
057: private XImage image;
058: private XSvgImageMap imageMap;
059: private XSvgButton svgButton;
060: private JLabel infoLabel, priceLabel;
061: private double imageWidth, priceX, priceWidth, infoWidth;
062: private int previousSize;
063:
064: private Color unselectedForeground;
065: private Color unselectedBackground;
066: private Color altUnselectedForeground;
067: private Color altUnselectedBackground;
068:
069: public XGraphicListCellRenderer(JLabel t) {
070: super ();
071:
072: totalPrice = t;
073:
074: XProject currentProject = XProjectManager.getCurrentProject();
075: styleManager = currentProject.getStyleManager();
076: comboStyle = styleManager.getStyle("base/edit");
077:
078: imageWidth = 0.0862;
079: priceX = 0.8154;
080: priceWidth = 0.1847;
081: infoWidth = 0.7262;
082:
083: Font font = new Font("Arial", Font.BOLD, 12);
084:
085: image = new XImage();
086:
087: // painter = new XSvgPainter( image );
088: // image.setPainter( painter );
089:
090: add(image);
091:
092: infoLabel = new JLabel();
093: infoLabel.setFont(font);
094: add(infoLabel);
095:
096: priceLabel = new JLabel();
097: priceLabel.setFont(font);
098: priceLabel.setHorizontalAlignment(JLabel.RIGHT);
099: add(priceLabel);
100:
101: first = true;
102: }
103:
104: /**
105: * Set the colors for alternate ( odd ) row colors
106: * @param frgd the foreground color
107: * @param bkgd the background color
108: */
109: public void setAltUnselectedColors(Color frgd, Color bkgd) {
110: altUnselectedForeground = frgd;
111: altUnselectedBackground = bkgd;
112: }
113:
114: /*
115: * This method finds the image and text corresponding
116: * to the selected value and returns the label, set-up
117: * to display the text and image.
118: */
119: public Component getListCellRendererComponent(JList list,
120: Object value, int index, boolean isSelected,
121: boolean cellHasFocus) {
122: Component renderer = super .getListCellRendererComponent(list,
123: value, index, isSelected, cellHasFocus);
124: renderer.setFont(list.getFont());
125:
126: if (first) {
127: image.setBounds(1, 1, (int) (list.getWidth() * imageWidth),
128: 28);
129: priceLabel.setBounds((int) (list.getWidth() * priceX), 1,
130: (int) (list.getWidth() * priceWidth), 28);
131: infoLabel.setBounds(
132: (int) (list.getWidth() * imageWidth + 5), 1,
133: (int) (list.getWidth() * infoWidth), 28);
134:
135: first = false;
136: }
137:
138: XDragInfo dragInfo = (XDragInfo) value;
139: // painter.setImage( XProjectManager.getCurrentProject().findResource( (String)dragInfo.get( "image" ) ) );
140: String s = (String) dragInfo.get("image");
141: image.setImage(XProjectManager.getCurrentProject().getImage(s));
142:
143: Font font = new Font("Arial", Font.PLAIN, 12);
144: FontMetrics fontMetrics = getFontMetrics(font);
145: String info = clipStringIfNecessary(infoLabel, fontMetrics,
146: (String) dragInfo.get(dragInfo.TITLE), (int) (list
147: .getWidth() * infoWidth) - 15);
148:
149: infoLabel.setText(info);
150: if ((String) dragInfo.get("price") != null)
151: priceLabel.setText((String) dragInfo.get("price") + " ");
152:
153: int percentage = 100;
154: if (!list.isEnabled()) {
155: percentage = 50;
156: }
157:
158: if (unselectedForeground == null) {
159: unselectedForeground = list.getForeground();
160: unselectedBackground = new Color(213, 247, 251);
161: }
162:
163: if (altUnselectedForeground == null) {
164: altUnselectedForeground = list.getForeground();
165: altUnselectedBackground = list.getBackground();
166: }
167:
168: if (isSelected) {
169: renderer.setBackground(XuiUtilities.unsaturateColor(list
170: .getSelectionBackground(), percentage));
171: renderer.setForeground(XuiUtilities.unsaturateColor(list
172: .getSelectionBackground(), percentage));
173:
174: infoLabel.setForeground(XuiUtilities.unsaturateColor(list
175: .getSelectionForeground(), percentage));
176: priceLabel.setForeground(XuiUtilities.unsaturateColor(list
177: .getSelectionForeground(), percentage));
178: } else {
179: XStyle comboStyle = styleManager.getStyle("base/edit");
180:
181: renderer
182: .setBackground(index % 2 == 0 ? XuiUtilities
183: .unsaturateColor((unselectedBackground),
184: percentage) : comboStyle
185: .getStyleAsColor(comboStyle.COLOR_BACK));
186: renderer
187: .setForeground(index % 2 == 0 ? XuiUtilities
188: .unsaturateColor((unselectedBackground),
189: percentage) : comboStyle
190: .getStyleAsColor(comboStyle.COLOR_BACK));
191:
192: infoLabel.setForeground(comboStyle
193: .getStyleAsColor(comboStyle.COLOR_FORE));
194: priceLabel.setForeground(comboStyle
195: .getStyleAsColor(comboStyle.COLOR_FORE));
196: }
197:
198: return renderer;
199: }
200:
201: public String clipStringIfNecessary(JComponent c, FontMetrics fm,
202: String caption, int availTextWidth) {
203: if ((caption == null) || (caption.length() == 0))
204: return "";
205: try {
206: /* This is a hack, using a private class so expect it to break when the JDK
207: * is upgraded */
208: Class klass = Class.forName("sun.swing.SwingUtilities2");
209: Class params[] = new Class[4];
210: params[0] = JComponent.class;
211: params[1] = FontMetrics.class;
212: params[2] = String.class;
213: params[3] = int.class;
214: Method clipIfNecessary = klass.getMethod(
215: "clipStringIfNecessary", params);
216:
217: if (clipIfNecessary != null) {
218: Object args[] = new Object[4];
219: args[0] = this ;
220: args[1] = fm;
221: args[2] = caption;
222: args[3] = new Integer(availTextWidth);
223: caption = (String) clipIfNecessary.invoke(null, args);
224: return caption;
225: }
226: } catch (Exception ex) {
227: //BEGIN_BLOCK_DEBUG
228: if (BuildProperties.DEBUG)
229: ex.printStackTrace();
230: //END_BLOCK_DEBUG
231: }
232: return "";
233: }
234: }
|