001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032: package com.vividsolutions.jump.workbench.ui.renderer.style;
033:
034: import com.vividsolutions.jump.util.CollectionUtil;
035: import com.vividsolutions.jump.workbench.model.Layer;
036: import com.vividsolutions.jump.workbench.model.LayerManager;
037: import com.vividsolutions.jump.workbench.ui.ColorPanel;
038: import java.awt.*;
039: import java.util.ArrayList;
040: import java.util.Collection;
041: import java.util.Iterator;
042: import java.util.List;
043:
044: import javax.swing.*;
045:
046: public class ColorSchemeListCellRenderer extends JPanel implements
047: ListCellRenderer {
048:
049: private ColorPanel colorPanel1 = new ColorPanel();
050: private ColorPanel colorPanel2 = new ColorPanel();
051: private ColorPanel colorPanel3 = new ColorPanel();
052: private ColorPanel colorPanel4 = new ColorPanel();
053: private ColorPanel colorPanel5 = new ColorPanel();
054: private GridBagLayout gridBagLayout1 = new GridBagLayout();
055: private JLabel label = new JLabel();
056:
057: public Component getListCellRendererComponent(JList list,
058: Object value, int index, boolean isSelected,
059: boolean cellHasFocus) {
060: String name = (String) value;
061: Collection colors = colorScheme(name).getColors();
062: Iterator i = CollectionUtil.stretch(colors, new ArrayList(), 5)
063: .iterator();
064: label.setText("(" + colors.size() + ") " + name);
065: color(colorPanel1, (Color) i.next());
066: color(colorPanel2, (Color) i.next());
067: color(colorPanel3, (Color) i.next());
068: color(colorPanel4, (Color) i.next());
069: color(colorPanel5, (Color) i.next());
070: if (isSelected) {
071: label.setForeground(list.getSelectionForeground());
072: label.setBackground(list.getSelectionBackground());
073: setForeground(list.getSelectionForeground());
074: setBackground(list.getSelectionBackground());
075: } else {
076: label.setForeground(list.getForeground());
077: label.setBackground(list.getBackground());
078: setForeground(list.getForeground());
079: setBackground(list.getBackground());
080: }
081: return this ;
082: }
083:
084: protected ColorScheme colorScheme(String name) {
085: return ColorScheme.create(name);
086: }
087:
088: private void color(ColorPanel colorPanel, Color fillColor) {
089: color(colorPanel, fillColor, Layer.defaultLineColor(fillColor));
090: }
091:
092: protected void color(ColorPanel colorPanel, Color fillColor,
093: Color lineColor) {
094: colorPanel.setFillColor(fillColor);
095: colorPanel.setLineColor(lineColor);
096: }
097:
098: public ColorSchemeListCellRenderer() {
099: try {
100: jbInit();
101: } catch (Exception e) {
102: e.printStackTrace();
103: }
104: }
105:
106: private void jbInit() throws Exception {
107: this .setLayout(gridBagLayout1);
108: label.setText("jLabel1");
109: this .add(colorPanel1, new GridBagConstraints(0, 0, 1, 1, 0.0,
110: 0.0, GridBagConstraints.CENTER,
111: GridBagConstraints.NONE, new Insets(0, 2, 0, 0), 0, 0));
112: this .add(colorPanel2, new GridBagConstraints(1, 0, 1, 1, 0.0,
113: 0.0, GridBagConstraints.CENTER,
114: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
115: this .add(colorPanel3, new GridBagConstraints(2, 0, 1, 1, 0.0,
116: 0.0, GridBagConstraints.CENTER,
117: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
118: this .add(colorPanel4, new GridBagConstraints(3, 0, 1, 1, 0.0,
119: 0.0, GridBagConstraints.CENTER,
120: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
121: this .add(colorPanel5, new GridBagConstraints(4, 0, 1, 1, 0.0,
122: 0.0, GridBagConstraints.CENTER,
123: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
124: this .add(label, new GridBagConstraints(5, 0, 1, 1, 1.0, 0.0,
125: GridBagConstraints.CENTER,
126: GridBagConstraints.HORIZONTAL, new Insets(0, 2, 0, 0),
127: 0, 0));
128: }
129:
130: /**
131: * Workaround for bug 4238829 in the Java bug database:
132: * "JComboBox containing JPanel fails to display selected item at creation time"
133: */
134: public void setBounds(int x, int y, int w, int h) {
135: super.setBounds(x, y, w, h);
136: validate();
137: }
138:
139: }
|