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.main;
020:
021: import java.awt.Dimension;
022:
023: import javax.swing.JSpinner;
024: import javax.swing.SpinnerNumberModel;
025:
026: import com.jeta.forms.components.panel.FormPanel;
027: import com.jeta.forms.gui.common.FormSpecAdapter;
028: import com.jeta.forms.gui.common.FormSpecDefinition;
029: import com.jeta.forms.gui.form.GridComponent;
030: import com.jeta.forms.gui.form.GridView;
031: import com.jeta.forms.gui.form.GridViewEvent;
032: import com.jeta.forms.gui.form.GridViewListener;
033: import com.jeta.forms.store.memento.FormGroupSet;
034: import com.jeta.open.gui.framework.JETAPanel;
035: import com.jeta.open.registry.JETARegistry;
036: import com.jeta.swingbuilder.gui.components.FloatDocument;
037: import com.jeta.swingbuilder.gui.editor.FormEditor;
038: import com.jeta.swingbuilder.gui.formmgr.EditorManager;
039: import com.jgoodies.forms.layout.CellConstraints;
040: import com.jgoodies.forms.layout.ColumnSpec;
041: import com.jgoodies.forms.layout.FormLayout;
042: import com.jgoodies.forms.layout.RowSpec;
043:
044: /**
045: * Displays the column and row settings.
046: *
047: * @author Jeff Tassin
048: */
049: public class SpecView extends JETAPanel implements GridViewListener,
050: FormSpecDefinition {
051: /**
052: * The panel that contains the form.
053: */
054: private FormPanel m_spec_panel;
055:
056: /**
057: * Set to true if this flag indicates this view shows a RowSpec. False if
058: * this view is for a ColumnSpec;
059: */
060: private boolean m_rowview;
061:
062: /**
063: * The grid component we are currently displaying values for
064: */
065: private GridComponent m_current_comp;
066:
067: private EditorManager m_editor_mgr;
068:
069: public SpecView(String formPath, boolean rowView) {
070: m_rowview = rowView;
071:
072: m_editor_mgr = (EditorManager) JETARegistry
073: .lookup(EditorManager.COMPONENT_ID);
074: FormLayout layout = new FormLayout("fill:pref:grow", "pref");
075: CellConstraints cc = new CellConstraints();
076:
077: setLayout(layout);
078:
079: m_spec_panel = new FormPanel(formPath);
080: add(m_spec_panel, cc.xy(1, 1));
081:
082: String def = m_spec_panel
083: .getText(SpecViewNames.ID_CONST_SIZE_AMT_FIELD);
084: m_spec_panel
085: .getTextField(SpecViewNames.ID_CONST_SIZE_AMT_FIELD)
086: .setDocument(new FloatDocument(false));
087: if (def != null)
088: m_spec_panel.setText(SpecViewNames.ID_CONST_SIZE_AMT_FIELD,
089: def);
090:
091: def = m_spec_panel.getText(SpecViewNames.ID_RESIZE_GROW_WEIGHT);
092: m_spec_panel.getTextField(SpecViewNames.ID_RESIZE_GROW_WEIGHT)
093: .setDocument(new FloatDocument(false));
094: if (def != null)
095: m_spec_panel.setText(SpecViewNames.ID_RESIZE_GROW_WEIGHT,
096: def);
097:
098: m_spec_panel.getButton(SpecViewNames.ID_GROUP_APPLY_BTN)
099: .setPreferredSize(new Dimension(32, 10));
100: JSpinner sp = m_spec_panel
101: .getSpinner(SpecViewNames.ID_GROUP_NUMBER_SPINNER);
102: sp.setModel(new SpinnerNumberModel(0, 0, 100, 1));
103:
104: setController(new SpecViewController(this ));
105: }
106:
107: /**
108: * @return the alignment string depending on the specification type column:
109: * LEFT, CENTER, RIGHT, RILL row: TOP, CENTER, BOTTOM, RILL
110: */
111: public String getAlignment() {
112: return (String) m_spec_panel.getComboBox(
113: SpecViewNames.ID_ALIGNMENT_COMBO).getSelectedItem();
114: }
115:
116: /**
117: * @return the bounded size MIN, MAX
118: */
119: public String getBoundedSize() {
120: if (m_spec_panel.getButton(SpecViewNames.ID_BOUNDED_MIN_RADIO)
121: .isSelected())
122: return "MIN";
123: else
124: return "MAX";
125: }
126:
127: /**
128: * @return the component size: MIN, PREF, DEFAULT
129: */
130: public String getComponentSize() {
131: return (String) m_spec_panel.getComboBox(
132: SpecViewNames.ID_COMP_SIZE_COMBO).getSelectedItem();
133: }
134:
135: /**
136: * @return the units (integer) (double) PX, PT, DLU IN, MM, CM
137: */
138: public String getConstantUnits() {
139: return (String) m_spec_panel.getComboBox(
140: SpecViewNames.ID_CONST_SIZE_UNITS_COMBO)
141: .getSelectedItem();
142: }
143:
144: /**
145: * @return the size.
146: */
147: public double getConstantSize() {
148: try {
149: String result = m_spec_panel.getTextField(
150: SpecViewNames.ID_CONST_SIZE_AMT_FIELD).getText();
151: return Double.parseDouble(result);
152: } catch (Exception e) {
153: /** what else can we do here? */
154: return 10.0;
155: }
156: }
157:
158: /**
159: * @return the component that we are currently displaying values for
160: */
161: GridComponent getCurrentComponent() {
162: return m_current_comp;
163: }
164:
165: /**
166: * @return the underlying form panel
167: */
168: FormPanel getFormPanel() {
169: return m_spec_panel;
170: }
171:
172: /**
173: * @return the current group that this column/row is assigned to
174: */
175: int getGroupId(GridComponent gc) {
176: GridView view = gc.getParentView();
177: if (isRowView()) {
178: FormGroupSet gset = view.getRowGroups();
179: Integer id = gset.getGroupId(gc.getRow());
180: return (id == null ? 0 : id.intValue());
181: } else {
182: FormGroupSet gset = view.getColumnGroups();
183: Integer id = gset.getGroupId(gc.getColumn());
184: return (id == null ? 0 : id.intValue());
185: }
186: }
187:
188: /**
189: * @return the resize behavior NONE, GROW
190: */
191: public String getResize() {
192: if (m_spec_panel.getButton(SpecViewNames.ID_RESIZE_NONE_RADIO)
193: .isSelected())
194: return "NONE";
195: else
196: return "GROW";
197: }
198:
199: /**
200: * @return the resize weight (0.0-1.0)
201: */
202: public double getResizeWeight() {
203: try {
204: String result = m_spec_panel.getTextField(
205: SpecViewNames.ID_RESIZE_GROW_WEIGHT).getText();
206: double weight = Double.parseDouble(result);
207: if (weight < 0.0)
208: weight = 0.0;
209:
210: if (weight > 1.0)
211: weight = 1.0;
212: return weight;
213: } catch (Exception e) {
214: /** what else can we do here? */
215: return 1.0;
216: }
217: }
218:
219: /**
220: * @return the size type for the FormSpec
221: */
222: public String getSizeType() {
223: if (m_spec_panel
224: .isSelected(SpecViewNames.ID_CONSTANT_SIZE_RADIO))
225: return "CONSTANT";
226: else if (m_spec_panel
227: .isSelected(SpecViewNames.ID_BOUNDED_SIZE_RADIO))
228: return "BOUNDED";
229: else
230: return "COMPONENT";
231: }
232:
233: /** GridViewListener implementation */
234: public void gridChanged(GridViewEvent evt) {
235: FormEditor editor = m_editor_mgr.getCurrentEditor();
236: if (editor != null) {
237: GridComponent comp = editor.getSelectedComponent();
238: update(comp);
239: } else {
240: update((GridComponent) null);
241: }
242: }
243:
244: /**
245: * @return true if the size type is bounded
246: */
247: public boolean isBoundedSize() {
248: return "BOUNDED".equals(getSizeType());
249: }
250:
251: /**
252: * @return true if the size type is component
253: */
254: public boolean isComponentSize() {
255: return "COMPONENT".equals(getSizeType());
256: }
257:
258: /**
259: * @return true if the size type is constant
260: */
261: public boolean isConstantSize() {
262: return "CONSTANT".equals(getSizeType());
263: }
264:
265: /**
266: * @return true if the resize grow radio is selected
267: */
268: public boolean isResizeGrow() {
269: return m_spec_panel.getButton(
270: SpecViewNames.ID_RESIZE_GROW_RADIO).isSelected();
271: }
272:
273: /**
274: * @return true if this spec view is for a row. False if this view is for a
275: * column.
276: */
277: public boolean isRowView() {
278: return m_rowview;
279: }
280:
281: void setGroup(int groupNumber) {
282: JSpinner sp = m_spec_panel
283: .getSpinner(SpecViewNames.ID_GROUP_NUMBER_SPINNER);
284: sp.setValue(new Integer(groupNumber));
285: }
286:
287: /**
288: * Selects the size type radio that corresponds to the given size type
289: */
290: public void setSizeType(String sizeType) {
291: if ("CONSTANT".equalsIgnoreCase(sizeType))
292: m_spec_panel.setSelected(
293: SpecViewNames.ID_CONSTANT_SIZE_RADIO, true);
294: else if ("BOUNDED".equalsIgnoreCase(sizeType))
295: m_spec_panel.setSelected(
296: SpecViewNames.ID_BOUNDED_SIZE_RADIO, true);
297: else
298: m_spec_panel.setSelected(
299: SpecViewNames.ID_COMPONENT_SIZE_RADIO, true);
300: }
301:
302: /**
303: * Updates the panel using the specs from the currently selected cell in the
304: * given editor
305: */
306: public void update(GridComponent gc) {
307: m_current_comp = gc;
308:
309: if (gc == null) {
310: setEnabled(false);
311: } else {
312: GridView view = gc.getParentView();
313: if (view != null) {
314: setEnabled(true);
315: int row = gc.getRow();
316: int col = gc.getColumn();
317: if (isRowView()) {
318: RowSpec spec = view.getRowSpec(row);
319: updateView(spec, gc);
320: } else {
321: ColumnSpec spec = view.getColumnSpec(col);
322: updateView(spec, gc);
323: }
324: } else {
325: setEnabled(false);
326: }
327: }
328: }
329:
330: /**
331: * Updates the view given the column spec
332: */
333: void updateView(ColumnSpec spec, GridComponent gc) {
334: assert (!isRowView());
335: FormSpecAdapter fspec = new FormSpecAdapter(spec);
336: updateView(fspec, gc);
337: }
338:
339: /**
340: * Updates the view given the column spec
341: */
342: void updateView(RowSpec spec, GridComponent gc) {
343: assert (isRowView());
344: FormSpecAdapter fspec = new FormSpecAdapter(spec);
345: updateView(fspec, gc);
346: }
347:
348: /**
349: * Updates the view given the column spec
350: */
351: void updateView(FormSpecAdapter fspec, GridComponent gc) {
352: SpecViewController controller = (SpecViewController) getController();
353:
354: try {
355: controller.enableEvents(false);
356:
357: m_spec_panel.getComboBox(SpecViewNames.ID_ALIGNMENT_COMBO)
358: .setSelectedItem(fspec.getAlignment());
359: setSizeType(fspec.getSizeType());
360:
361: if (isConstantSize() || isBoundedSize()) {
362: m_spec_panel.getComboBox(
363: SpecViewNames.ID_CONST_SIZE_UNITS_COMBO)
364: .setSelectedItem(fspec.getConstantUnits());
365: m_spec_panel.getButton(
366: SpecViewNames.ID_BOUNDED_MIN_RADIO)
367: .setSelected(fspec.isBoundedMinimum());
368: m_spec_panel.getButton(
369: SpecViewNames.ID_BOUNDED_MAX_RADIO)
370: .setSelected(fspec.isBoundedMaximum());
371:
372: if (fspec.isIntegralUnits()) {
373: m_spec_panel.getTextField(
374: SpecViewNames.ID_CONST_SIZE_AMT_FIELD)
375: .setText(
376: String.valueOf(Math.round(fspec
377: .getConstantSize())));
378: } else {
379: m_spec_panel.getTextField(
380: SpecViewNames.ID_CONST_SIZE_AMT_FIELD)
381: .setText(
382: String.valueOf(fspec
383: .getConstantSize()));
384: }
385: }
386:
387: if (isComponentSize() || isBoundedSize()) {
388: m_spec_panel.getComboBox(
389: SpecViewNames.ID_COMP_SIZE_COMBO)
390: .setSelectedItem(fspec.getComponentSize());
391: }
392:
393: m_spec_panel.getButton(SpecViewNames.ID_RESIZE_NONE_RADIO)
394: .setSelected(fspec.isResizeNone());
395: m_spec_panel.getButton(SpecViewNames.ID_RESIZE_GROW_RADIO)
396: .setSelected(fspec.isResizeGrow());
397: m_spec_panel.getTextField(
398: SpecViewNames.ID_RESIZE_GROW_WEIGHT).setText(
399: String.valueOf(fspec.getResizeWeight()));
400:
401: setGroup(getGroupId(gc));
402: } finally {
403: controller.enableEvents(true);
404: }
405: }
406: }
|