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.Toolkit;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ActionListener;
024: import java.util.HashMap;
025:
026: import javax.swing.JSpinner;
027:
028: import com.jeta.forms.gui.common.FormUtils;
029: import com.jeta.forms.gui.form.GridComponent;
030: import com.jeta.forms.gui.form.GridView;
031: import com.jeta.forms.gui.formmgr.FormManager;
032: import com.jeta.open.gui.framework.JETAController;
033: import com.jeta.open.gui.framework.UIDirector;
034: import com.jeta.open.registry.JETARegistry;
035: import com.jeta.swingbuilder.gui.commands.ChangeGroupCommand;
036: import com.jeta.swingbuilder.gui.commands.CommandUtils;
037: import com.jeta.swingbuilder.gui.commands.EditColumnSpecCommand;
038: import com.jeta.swingbuilder.gui.commands.EditRowSpecCommand;
039: import com.jeta.swingbuilder.gui.editor.FormEditor;
040: import com.jgoodies.forms.layout.ColumnSpec;
041: import com.jgoodies.forms.layout.RowSpec;
042:
043: /**
044: * The controller for this panel
045: */
046: public class SpecViewController extends JETAController {
047: /**
048: * The view we are handling events for.
049: */
050: private SpecView m_view;
051:
052: /**
053: * The form manager.
054: */
055: private FormManager m_formmgr;
056:
057: /**
058: * A map of component names to ActionListeners for components in the view.
059: * m_listeners<String,ActionListener>
060: */
061: private HashMap m_listeners = new HashMap();
062:
063: /**
064: * ctor
065: */
066: public SpecViewController(SpecView view) {
067: super (view);
068: m_view = view;
069: m_formmgr = (FormManager) JETARegistry
070: .lookup(FormManager.COMPONENT_ID);
071: assignAction(SpecViewNames.ID_ALIGNMENT_COMBO,
072: new SpecChangeListener());
073: assignAction(SpecViewNames.ID_CONST_SIZE_AMT_FIELD,
074: new ConstantSizeListener());
075: assignAction(SpecViewNames.ID_CONST_SIZE_UNITS_COMBO,
076: new SpecChangeListener());
077: assignAction(SpecViewNames.ID_COMP_SIZE_COMBO,
078: new SpecChangeListener());
079: assignAction(SpecViewNames.ID_BOUNDED_MIN_RADIO,
080: new SpecChangeListener());
081: assignAction(SpecViewNames.ID_BOUNDED_MAX_RADIO,
082: new SpecChangeListener());
083: assignAction(SpecViewNames.ID_RESIZE_NONE_RADIO,
084: new SpecChangeListener());
085: assignAction(SpecViewNames.ID_RESIZE_GROW_RADIO,
086: new SpecChangeListener());
087: assignAction(SpecViewNames.ID_RESIZE_GROW_WEIGHT,
088: new WeightAction());
089:
090: assignAction(SpecViewNames.ID_CONSTANT_SIZE_RADIO,
091: new SpecChangeListener());
092: assignAction(SpecViewNames.ID_COMPONENT_SIZE_RADIO,
093: new SpecChangeListener());
094: assignAction(SpecViewNames.ID_BOUNDED_SIZE_RADIO,
095: new SpecChangeListener());
096: assignAction(SpecViewNames.ID_GROUP_APPLY_BTN,
097: new GroupChangedAction());
098:
099: view.setUIDirector(new SpecViewUIDirector(m_view));
100: }
101:
102: /**
103: * Gets the latest settings from the view and updates the Form.
104: */
105: private void updateForm() {
106: String newspec = FormUtils.toEncodedString(m_view);
107: FormEditor editor = (FormEditor) m_formmgr.getCurrentEditor();
108: if (editor != null) {
109: GridComponent gc = editor.getSelectedComponent();
110: if (gc != null) {
111: GridView view = gc.getParentView();
112: int row = gc.getRow();
113: int col = gc.getColumn();
114: if (m_view.isRowView()) {
115: RowSpec oldspec = view.getRowSpec(row);
116: EditRowSpecCommand cmd = new EditRowSpecCommand(
117: view.getParentForm(), row, new RowSpec(
118: newspec), oldspec);
119: CommandUtils.invoke(cmd, editor);
120: } else {
121: ColumnSpec oldspec = view.getColumnSpec(col);
122: EditColumnSpecCommand cmd = new EditColumnSpecCommand(
123: view.getParentForm(), col, new ColumnSpec(
124: newspec), oldspec);
125: CommandUtils.invoke(cmd, editor);
126: }
127: }
128: }
129: }
130:
131: /**
132: * Changes the group in the current view.
133: */
134: public class GroupChangedAction implements ActionListener {
135: public void actionPerformed(ActionEvent evt) {
136: GridComponent gc = m_view.getCurrentComponent();
137: if (gc != null) {
138: GridView view = gc.getParentView();
139: if (view != null) {
140: int current_group = m_view.getGroupId(gc);
141: JSpinner sp = m_view
142: .getSpinner(SpecViewNames.ID_GROUP_NUMBER_SPINNER);
143: int new_group = ((Integer) sp.getValue())
144: .intValue();
145: if (new_group != current_group) {
146: int index = m_view.isRowView() ? gc.getRow()
147: : gc.getColumn();
148: ChangeGroupCommand cmd = new ChangeGroupCommand(
149: view.getParentForm(), new_group,
150: current_group, index, m_view
151: .isRowView());
152: CommandUtils.invoke(cmd, FormEditor
153: .getEditor(view));
154: }
155: }
156: }
157: }
158: }
159:
160: /**
161: * Listener for most controls on SpecView
162: */
163: public class SpecChangeListener implements ActionListener {
164: public void actionPerformed(ActionEvent evt) {
165: updateForm();
166: }
167: }
168:
169: private void constantSizeChanged() {
170: String result = m_view.getTextField(
171: SpecViewNames.ID_CONST_SIZE_AMT_FIELD).getText();
172: try {
173: double sz = Double.parseDouble(result);
174: updateForm();
175: } catch (Exception e) {
176: Toolkit toolkit = Toolkit.getDefaultToolkit();
177: toolkit.beep();
178: System.out
179: .println("SpecViewController.constantSize exception");
180: }
181: }
182:
183: /**
184: * Listener for constant size amount field on SpecView
185: */
186: public class ConstantSizeListener implements ActionListener {
187: public void actionPerformed(ActionEvent evt) {
188: constantSizeChanged();
189: }
190: }
191:
192: /**
193: * Listener for weight field on SpecView
194: */
195: public class WeightAction implements ActionListener {
196: public void actionPerformed(ActionEvent evt) {
197: String result = m_view
198: .getText(SpecViewNames.ID_RESIZE_GROW_WEIGHT);
199: try {
200: double sz = Double.parseDouble(result);
201: if (sz > 1.0) {
202: m_view.setText(SpecViewNames.ID_RESIZE_GROW_WEIGHT,
203: "1.0");
204: }
205: updateForm();
206: } catch (Exception e) {
207: Toolkit toolkit = Toolkit.getDefaultToolkit();
208: toolkit.beep();
209: System.out
210: .println("SpecViewController.weight exception");
211: }
212: }
213: }
214:
215: /**
216: * The UIDirector for the SpecView
217: */
218: public static class SpecViewUIDirector implements UIDirector {
219: /**
220: * The view we are handling events for.
221: */
222: private SpecView m_view;
223:
224: /**
225: * ctor
226: */
227: public SpecViewUIDirector(SpecView view) {
228: m_view = view;
229: }
230:
231: /**
232: * Updates the components in the view
233: */
234: public void updateComponents(java.util.EventObject evt) {
235: if (m_view.isConstantSize()) {
236: m_view.enableComponent(
237: SpecViewNames.ID_CONST_SIZE_AMT_FIELD, true);
238: m_view.enableComponent(
239: SpecViewNames.ID_CONST_SIZE_UNITS_COMBO, true);
240: m_view.enableComponent(
241: SpecViewNames.ID_COMP_SIZE_COMBO, false);
242: m_view.enableComponent(
243: SpecViewNames.ID_BOUNDED_MIN_RADIO, false);
244: m_view.enableComponent(
245: SpecViewNames.ID_BOUNDED_MAX_RADIO, false);
246: } else if (m_view.isComponentSize()) {
247: m_view.enableComponent(
248: SpecViewNames.ID_CONST_SIZE_AMT_FIELD, false);
249: m_view.enableComponent(
250: SpecViewNames.ID_CONST_SIZE_UNITS_COMBO, false);
251: m_view.enableComponent(
252: SpecViewNames.ID_COMP_SIZE_COMBO, true);
253: m_view.enableComponent(
254: SpecViewNames.ID_BOUNDED_MIN_RADIO, false);
255: m_view.enableComponent(
256: SpecViewNames.ID_BOUNDED_MAX_RADIO, false);
257:
258: } else if (m_view.isBoundedSize()) {
259: m_view.enableComponent(
260: SpecViewNames.ID_CONST_SIZE_AMT_FIELD, true);
261: m_view.enableComponent(
262: SpecViewNames.ID_CONST_SIZE_UNITS_COMBO, true);
263: m_view.enableComponent(
264: SpecViewNames.ID_COMP_SIZE_COMBO, true);
265: m_view.enableComponent(
266: SpecViewNames.ID_BOUNDED_MIN_RADIO, true);
267: m_view.enableComponent(
268: SpecViewNames.ID_BOUNDED_MAX_RADIO, true);
269: }
270: m_view.enableComponent(SpecViewNames.ID_RESIZE_GROW_WEIGHT,
271: m_view.isResizeGrow());
272: }
273: }
274: }
|