001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.components.line;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Component;
023: import java.awt.Graphics2D;
024: import java.awt.image.BufferedImage;
025: import java.util.Iterator;
026:
027: import javax.swing.DefaultListModel;
028: import javax.swing.Icon;
029: import javax.swing.ImageIcon;
030: import javax.swing.JLabel;
031: import javax.swing.JList;
032: import javax.swing.ListCellRenderer;
033: import javax.swing.UIManager;
034:
035: import com.jeta.forms.components.panel.FormPanel;
036: import com.jeta.forms.store.properties.CompoundLineProperty;
037: import com.jeta.forms.store.properties.LineProperty;
038: import com.jeta.open.gui.framework.JETAPanel;
039: import com.jeta.swingbuilder.gui.utils.FormDesignerUtils;
040:
041: /**
042: * View that is used to create and edit lines
043: *
044: * @author Jeff Tassin
045: */
046: public class CompoundLineView extends JETAPanel {
047: /**
048: * The actual view
049: */
050: private FormPanel m_view;
051:
052: /**
053: * The list model that handles the borders
054: */
055: private DefaultListModel m_lines_model;
056:
057: private int m_old_position = 0;
058:
059: /**
060: * ctor
061: */
062: public CompoundLineView(CompoundLineProperty lines) {
063: setLayout(new BorderLayout());
064: m_view = new FormPanel(
065: "com/jeta/swingbuilder/gui/components/line/compoundLineView.frm");
066: add(m_view, BorderLayout.CENTER);
067: setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10,
068: 10, 10));
069:
070: m_lines_model = new DefaultListModel();
071: JList list = m_view.getList(CompoundLineNames.ID_LINES_LIST);
072: list.setModel(m_lines_model);
073: list.setCellRenderer(new LineCellRenderer());
074:
075: m_old_position = lines.getPosition();
076: Iterator iter = lines.iterator();
077: while (iter.hasNext()) {
078: LineProperty bp = (LineProperty) iter.next();
079: addLine(bp);
080: }
081:
082: setController(new CompoundLineController(this ));
083: }
084:
085: /**
086: * Adds a border to the list
087: */
088: public void addLine(LineProperty lp) {
089: m_lines_model.addElement(new LineInfo(lp));
090: updatePreview();
091: }
092:
093: /**
094: * Ensure the selected border is visible in the list.
095: */
096: void ensureIndexIsVisible() {
097: JList list = m_view.getList(CompoundLineNames.ID_LINES_LIST);
098: int index = list.getSelectedIndex();
099: if (index >= 0)
100: list.ensureIndexIsVisible(index);
101: }
102:
103: /**
104: * Creates a LineProperty based on this view
105: */
106: public CompoundLineProperty createLineProperty() {
107: CompoundLineProperty prop = new CompoundLineProperty();
108: prop.setPosition(m_old_position);
109: for (int index = 0; index < m_lines_model.size(); index++) {
110: LineInfo li = (LineInfo) m_lines_model.elementAt(index);
111: LineProperty bp = li.getLineProperty();
112: prop.addLine(bp);
113: }
114: return prop;
115: }
116:
117: /**
118: * Deletes the selected lin
119: */
120: public void deleteSelectedLine() {
121: LineInfo li = (LineInfo) m_view.getList(
122: CompoundLineNames.ID_LINES_LIST).getSelectedValue();
123: if (li != null) {
124: m_lines_model.removeElement(li);
125: updatePreview();
126: }
127: }
128:
129: /**
130: * @return the selected border in the border list.
131: */
132: public LineProperty getSelectedLine() {
133: LineInfo li = (LineInfo) m_view.getList(
134: CompoundLineNames.ID_LINES_LIST).getSelectedValue();
135: if (li == null)
136: return null;
137: else
138: return li.getLineProperty();
139: }
140:
141: /**
142: * @return the current number of lines that make up this compound line
143: */
144: public int getLineCount() {
145: return m_lines_model.size();
146: }
147:
148: /**
149: * Modifies a given border and sets it to a new border
150: */
151: public void setLine(LineProperty new_prop, LineProperty old_prop) {
152: for (int index = 0; index < m_lines_model.size(); index++) {
153: LineInfo li = (LineInfo) m_lines_model.elementAt(index);
154: if (li.getLineProperty() == old_prop) {
155: m_lines_model.setElementAt(new LineInfo(new_prop),
156: index);
157: updatePreview();
158: return;
159: }
160: }
161:
162: assert (false);
163: }
164:
165: /**
166: * Creates a swing border based on the properties in this view and displays
167: * it.
168: */
169: public void updatePreview() {
170: /*
171: * LineComponent lcomp = (LineComponent)m_view.getComponentByName(
172: * CompoundLineNames.ID_LINE_COMPONENT ); lcomp.setLineDefinition(
173: * createLineProperty() ); revalidate(); doLayout(); lcomp.repaint();
174: */
175: }
176:
177: public static class LineInfo {
178: private LineProperty m_prop;
179: private ImageIcon m_icon;
180: private boolean m_selected = false;
181:
182: public LineInfo(LineProperty prop) {
183: m_prop = prop;
184: }
185:
186: public Icon getIcon(boolean selected) {
187: if (m_icon == null || (m_selected != selected)) {
188: m_selected = selected;
189: int width = 100;
190: int height = 20;
191: BufferedImage bimage = new BufferedImage(width, height,
192: BufferedImage.TYPE_INT_RGB);
193: Graphics2D bg = bimage.createGraphics();
194:
195: if (selected)
196: bg.setColor(UIManager
197: .getColor("List.selectionBackground"));
198: else
199: bg.setColor(UIManager.getColor("List.background"));
200:
201: bg.fillRect(0, 0, width, height);
202: ImageIcon line_icon = FormDesignerUtils
203: .loadImage("forms/line_property.gif");
204: bg.drawImage(line_icon.getImage(), 0, 4, bg.getColor(),
205: null);
206:
207: java.awt.BasicStroke s = (java.awt.BasicStroke) m_prop
208: .getStroke(16);
209: // System.out.println( "CompoundLineView.getIcon stroke
210: // thickness: " + s.getLineWidth() );
211:
212: bg.setColor(m_prop.getColor());
213: bg.setStroke(s);
214: bg.drawLine(20, height / 2, width, height / 2);
215: bg.dispose();
216: m_icon = new ImageIcon(bimage);
217: }
218: return m_icon;
219: }
220:
221: public LineProperty getLineProperty() {
222: return m_prop;
223: }
224:
225: public void setLineProperty(LineProperty prop) {
226: m_prop = prop;
227: m_icon = null;
228: }
229: }
230:
231: /**
232: * Line Renderer
233: */
234: public static class LineCellRenderer extends JLabel implements
235: ListCellRenderer {
236: public LineCellRenderer() {
237: // must set or the background color won't show
238: setOpaque(true);
239: }
240:
241: public Component getListCellRendererComponent(JList list,
242: Object value, int index, boolean isSelected,
243: boolean cellHasFocus) {
244: // LineProperty info = (LineProperty)value;
245: LineInfo info = (LineInfo) value;
246: if (info == null) {
247: assert (false);
248: setIcon(null);
249: } else {
250: setIcon(info.getIcon(isSelected));
251: }
252:
253: if (isSelected) {
254: setBackground(UIManager
255: .getColor("List.selectionBackground"));
256: setForeground(UIManager
257: .getColor("List.selectionForeground"));
258: } else {
259: setBackground(UIManager.getColor("List.background"));
260: setForeground(UIManager.getColor("List.foreground"));
261: }
262: return this;
263: }
264: }
265:
266: }
|