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