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.LinearGradientPainter;
029: import com.jeta.forms.gui.effects.Painter;
030: import com.jeta.forms.gui.form.GridView;
031: import com.jeta.forms.store.properties.effects.GradientProperty;
032: import com.jeta.forms.store.properties.effects.PaintProperty;
033: import com.jeta.open.gui.framework.JETAController;
034: import com.jeta.open.gui.framework.JETAPanel;
035:
036: /**
037: * @author Jeff Tassin
038: */
039: public class GradientView extends JETAPanel implements PaintView {
040: /**
041: * The fillProperties form.
042: */
043: private FormPanel m_view;
044:
045: /**
046: * The preview for the gradient settings
047: */
048: private GridView m_preview;
049:
050: /**
051: * Responsible for rendering the gradient in the parent view
052: */
053: private LinearGradientPainter m_painter = new LinearGradientPainter();
054:
055: private GradientProperty m_gp = new GradientProperty();
056:
057: /**
058: * ctor
059: */
060: public GradientView(GridView preview) {
061: this (preview, null);
062: }
063:
064: /**
065: * ctor
066: */
067: public GradientView(GridView preview, PaintProperty gp) {
068: m_preview = preview;
069: setLayout(new BorderLayout());
070: m_view = new FormPanel(
071: "com/jeta/swingbuilder/gui/effects/gradientProperties.frm");
072:
073: add(m_view, BorderLayout.CENTER);
074: setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10,
075: 10, 10));
076:
077: JSpinner sp = (JSpinner) m_view
078: .getSpinner(GradientNames.ID_MAGNITUDE_SPINNER);
079: sp.setModel(new SpinnerNumberModel(1, 1, 1000, 1));
080: sp.setValue(new Integer(100));
081:
082: if (gp != null) {
083: initialize((GradientProperty) gp.getPaintDelegate());
084: }
085: setController(new GradientViewController(this ));
086: }
087:
088: /**
089: * Initializes the view
090: */
091: private void initialize(GradientProperty gp) {
092: JETAController controller = getController();
093: try {
094: controller.enableEvents(false);
095: ColorSelector view = (ColorSelector) m_view
096: .getComponentByName(GradientNames.ID_START_COLOR_SELECTOR);
097: view.setColorProperty(gp.getStartColor());
098: view = (ColorSelector) m_view
099: .getComponentByName(GradientNames.ID_END_COLOR_SELECTOR);
100: view.setColorProperty(gp.getEndColor());
101: setDirection(gp.getDirection());
102: setMagnitude(gp.getMagnitude());
103: } finally {
104: controller.enableEvents(true);
105: }
106: }
107:
108: /**
109: * @return the selection gradient direction (e.g.
110: * GradientProperty.HORIZONTAL, VERTICAL, ... )
111: */
112: public int getDirection() {
113: String sval = (String) m_view
114: .getSelectedItem(GradientNames.ID_DIRECTION_COMBO);
115: if ("TOP_BOTTOM".equals(sval))
116: return GradientProperty.TOP_BOTTOM;
117: else if ("BOTTOM_TOP".equals(sval))
118: return GradientProperty.BOTTOM_TOP;
119: else if ("LEFT_RIGHT".equals(sval))
120: return GradientProperty.LEFT_RIGHT;
121: else if ("RIGHT_LEFT".equals(sval))
122: return GradientProperty.RIGHT_LEFT;
123: else if ("UP_RIGHT".equals(sval))
124: return GradientProperty.UP_RIGHT;
125: else if ("UP_LEFT".equals(sval))
126: return GradientProperty.UP_LEFT;
127: else if ("DOWN_RIGHT".equals(sval))
128: return GradientProperty.DOWN_RIGHT;
129: else if ("DOWN_LEFT".equals(sval))
130: return GradientProperty.DOWN_LEFT;
131: else
132: return GradientProperty.TOP_BOTTOM;
133: }
134:
135: public float getMagnitude() {
136: SpinnerNumberModel spm = (SpinnerNumberModel) m_view
137: .getSpinner(GradientNames.ID_MAGNITUDE_SPINNER)
138: .getModel();
139: Integer ival = (Integer) spm.getValue();
140: float mag = ((float) ival.intValue()) / 100.0f;
141: return mag;
142: }
143:
144: public void setMagnitude(float mag) {
145: SpinnerNumberModel spm = (SpinnerNumberModel) m_view
146: .getSpinner(GradientNames.ID_MAGNITUDE_SPINNER)
147: .getModel();
148: spm.setValue(new Integer((int) (mag * 100.0f)));
149: }
150:
151: /**
152: * @return a painter object for this the properties in this view
153: */
154: public Painter getPainter() {
155: m_painter.setGradientProperty(getGradientProperty());
156: return m_painter;
157: }
158:
159: /**
160: * @return the
161: */
162: public GradientProperty getGradientProperty() {
163: ColorSelector view = (ColorSelector) m_view
164: .getComponentByName(GradientNames.ID_START_COLOR_SELECTOR);
165: GradientProperty gp = new GradientProperty();
166: gp.setStartColor(view.getColorProperty());
167: view = (ColorSelector) m_view
168: .getComponentByName(GradientNames.ID_END_COLOR_SELECTOR);
169: gp.setEndColor(view.getColorProperty());
170: gp.setDirection(getDirection());
171: gp.setMagnitude(getMagnitude());
172: return gp;
173: }
174:
175: /**
176: * @return the
177: */
178: public PaintProperty getPaintProperty() {
179: return new PaintProperty(getGradientProperty());
180: }
181:
182: /**
183: * Sets the property for the view
184: */
185: public void setPaintProperty(PaintProperty pp) {
186: if (pp.getPaintDelegate() instanceof GradientProperty) {
187: GradientProperty gp = (GradientProperty) pp
188: .getPaintDelegate();
189: m_gp.setValue(gp);
190: initialize(m_gp);
191: updatePreview();
192: }
193: }
194:
195: /**
196: * Sets the gradient direction in the direction combo
197: */
198: public void setDirection(int dir) {
199: if (dir == GradientProperty.TOP_BOTTOM)
200: m_view.setSelectedItem(GradientNames.ID_DIRECTION_COMBO,
201: "TOP_BOTTOM");
202: else if (dir == GradientProperty.BOTTOM_TOP)
203: m_view.setSelectedItem(GradientNames.ID_DIRECTION_COMBO,
204: "BOTTOM_TOP");
205: else if (dir == GradientProperty.LEFT_RIGHT)
206: m_view.setSelectedItem(GradientNames.ID_DIRECTION_COMBO,
207: "LEFT_RIGHT");
208: else if (dir == GradientProperty.RIGHT_LEFT)
209: m_view.setSelectedItem(GradientNames.ID_DIRECTION_COMBO,
210: "RIGHT_LEFT");
211: else if (dir == GradientProperty.UP_RIGHT)
212: m_view.setSelectedItem(GradientNames.ID_DIRECTION_COMBO,
213: "UP_RIGHT");
214: else if (dir == GradientProperty.UP_LEFT)
215: m_view.setSelectedItem(GradientNames.ID_DIRECTION_COMBO,
216: "UP_LEFT");
217: else if (dir == GradientProperty.DOWN_RIGHT)
218: m_view.setSelectedItem(GradientNames.ID_DIRECTION_COMBO,
219: "DOWN_RIGHT");
220: else if (dir == GradientProperty.DOWN_LEFT)
221: m_view.setSelectedItem(GradientNames.ID_DIRECTION_COMBO,
222: "DOWN_LEFT");
223: }
224:
225: /**
226: * Updates the preview view
227: */
228: public void updatePreview() {
229: m_preview.setBackgroundPainter(getPainter());
230: }
231:
232: }
|