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.effects;
020:
021: import java.awt.BorderLayout;
022:
023: import javax.swing.JSpinner;
024: import javax.swing.SpinnerNumberModel;
025:
026: import com.jeta.forms.components.colors.ColorSelector;
027: import com.jeta.forms.components.panel.FormPanel;
028: import com.jeta.forms.gui.effects.Painter;
029: import com.jeta.forms.gui.effects.RadialGradientPainter;
030: import com.jeta.forms.gui.form.GridView;
031: import com.jeta.forms.store.properties.effects.PaintProperty;
032: import com.jeta.forms.store.properties.effects.RadialGradientProperty;
033: import com.jeta.open.gui.framework.JETAPanel;
034:
035: /**
036: * @author Jeff Tassin
037: */
038: public class RadialView extends JETAPanel implements PaintView {
039: /**
040: * The fillProperties form.
041: */
042: private FormPanel m_view;
043:
044: /**
045: * The preview for the gradient settings
046: */
047: private GridView m_preview;
048:
049: /**
050: * Responsible for rendering the gradient in the parent view
051: */
052: private RadialGradientPainter m_painter = new RadialGradientPainter();
053:
054: private RadialGradientProperty m_gp = new RadialGradientProperty();
055:
056: /**
057: * ctor
058: */
059: public RadialView(GridView preview) {
060: this (preview, null);
061: }
062:
063: /**
064: * ctor
065: */
066: public RadialView(GridView preview, PaintProperty pp) {
067: m_preview = preview;
068: setLayout(new BorderLayout());
069: m_view = new FormPanel(
070: "com/jeta/swingbuilder/gui/effects/radialGradientProperties.frm");
071: add(m_view, BorderLayout.CENTER);
072: setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10,
073: 10, 10));
074:
075: JSpinner sp = (JSpinner) m_view
076: .getSpinner(RadialNames.ID_MAGNITUDE_SPINNER);
077: sp.setModel(new SpinnerNumberModel(1, 1, 1000, 1));
078: sp.setValue(new Integer(100));
079:
080: if (pp != null) {
081: initialize((RadialGradientProperty) pp.getPaintDelegate());
082: }
083:
084: setController(new RadialViewController(this ));
085: }
086:
087: /**
088: * Initializes the view
089: */
090: private void initialize(RadialGradientProperty gp) {
091: ColorSelector view = (ColorSelector) m_view
092: .getComponentByName(RadialNames.ID_START_COLOR_SELECTOR);
093: view.setColorProperty(gp.getStartColor());
094: view = (ColorSelector) m_view
095: .getComponentByName(RadialNames.ID_END_COLOR_SELECTOR);
096: view.setColorProperty(gp.getEndColor());
097:
098: // setColorProperty( RadialNames.ID_START_COLOR, gp.getStartColor() );
099: // setColorProperty( RadialNames.ID_END_COLOR, gp.getEndColor() );
100: setPosition(gp.getPosition());
101: setMagnitude(gp.getMagnitude());
102: }
103:
104: public int getMagnitude() {
105: JSpinner sp = (JSpinner) m_view
106: .getSpinner(RadialNames.ID_MAGNITUDE_SPINNER);
107: SpinnerNumberModel model = (SpinnerNumberModel) sp.getModel();
108: Integer ival = (Integer) model.getValue();
109: return ival.intValue();
110: }
111:
112: public void setPosition(int pos) {
113: String pos_name = "CENTER";
114: if (pos == RadialGradientProperty.TOP_LEFT) {
115: pos_name = "TOP_LEFT";
116: } else if (pos == RadialGradientProperty.TOP_CENTER) {
117: pos_name = "TOP_CENTER";
118: } else if (pos == RadialGradientProperty.TOP_RIGHT) {
119: pos_name = "TOP_RIGHT";
120: } else if (pos == RadialGradientProperty.BOTTOM_LEFT) {
121: pos_name = "BOTTOM_LEFT";
122: } else if (pos == RadialGradientProperty.BOTTOM_CENTER) {
123: pos_name = "BOTTOM_CENTER";
124: } else if (pos == RadialGradientProperty.BOTTOM_RIGHT) {
125: pos_name = "BOTTOM_RIGHT";
126: } else if (pos == RadialGradientProperty.LEFT_CENTER) {
127: pos_name = "LEFT_CENTER";
128: } else if (pos == RadialGradientProperty.RIGHT_CENTER) {
129: pos_name = "RIGHT_CENTER";
130: }
131: m_view.setSelectedItem(RadialNames.ID_POSITION_COMBO, pos_name);
132: }
133:
134: public void setMagnitude(int mag) {
135: JSpinner sp = (JSpinner) m_view
136: .getSpinner(RadialNames.ID_MAGNITUDE_SPINNER);
137: sp.setModel(new SpinnerNumberModel(1, 1, 1000, 1));
138: sp.setValue(new Integer(mag));
139: }
140:
141: /**
142: * @return a painter object for this the properties in this view
143: */
144: public Painter getPainter() {
145: m_painter.setGradientProperty(getGradientProperty());
146: return m_painter;
147: }
148:
149: /**
150: * @return the
151: */
152: public RadialGradientProperty getGradientProperty() {
153: RadialGradientProperty rp = new RadialGradientProperty();
154: rp.setPosition(getPosition());
155:
156: ColorSelector view = (ColorSelector) m_view
157: .getComponentByName(RadialNames.ID_START_COLOR_SELECTOR);
158: rp.setStartColor(view.getColorProperty());
159: view = (ColorSelector) m_view
160: .getComponentByName(RadialNames.ID_END_COLOR_SELECTOR);
161: rp.setEndColor(view.getColorProperty());
162: rp.setMagnitude(getMagnitude());
163: return rp;
164: }
165:
166: /**
167: * @return the
168: */
169: public PaintProperty getPaintProperty() {
170: return new PaintProperty(getGradientProperty());
171: }
172:
173: /**
174: * @return the position of the gradient center
175: */
176: private int getPosition() {
177: String pos = (String) m_view
178: .getSelectedItem(RadialNames.ID_POSITION_COMBO);
179: if ("TOP_LEFT".equals(pos))
180: return RadialGradientProperty.TOP_LEFT;
181: if ("TOP_CENTER".equals(pos))
182: return RadialGradientProperty.TOP_CENTER;
183: if ("TOP_RIGHT".equals(pos))
184: return RadialGradientProperty.TOP_RIGHT;
185:
186: if ("BOTTOM_LEFT".equals(pos))
187: return RadialGradientProperty.BOTTOM_LEFT;
188: if ("BOTTOM_CENTER".equals(pos))
189: return RadialGradientProperty.BOTTOM_CENTER;
190: if ("BOTTOM_RIGHT".equals(pos))
191: return RadialGradientProperty.BOTTOM_RIGHT;
192:
193: if ("LEFT_CENTER".equals(pos))
194: return RadialGradientProperty.LEFT_CENTER;
195: if ("RIGHT_CENTER".equals(pos))
196: return RadialGradientProperty.RIGHT_CENTER;
197:
198: return RadialGradientProperty.CENTER;
199: }
200:
201: /**
202: * Sets the property for the view
203: */
204: public void setPaintProperty(PaintProperty pp) {
205: if (pp.getPaintDelegate() instanceof RadialGradientProperty) {
206: RadialGradientProperty gp = (RadialGradientProperty) pp
207: .getPaintDelegate();
208: m_gp.setValue(gp);
209: initialize(m_gp);
210: updatePreview();
211: }
212: }
213:
214: /**
215: * Updates the preview view
216: */
217: public void updatePreview() {
218: m_preview.setBackgroundPainter(getPainter());
219: }
220:
221: }
|