initial import
[iDMC.git] / src / java / org / tsho / dmc2 / ui / FormHelper.java
blob9e54bf5da920a844766644b1aaf023dc0d24b422
1 /*
2 * iDMC the interactive Dynamical Model Calculator simulates and performs
3 * graphical and numerical analysis of systems of differential and
4 * difference equations.
6 * Copyright (C) 2004 Marji Lines and Alfredo Medio.
8 * Written by Daniele Pizzoni <auouo@tin.it>.
9 * Extended by Alexei Grigoriev <alexei_grigoriev@libero.it>.
13 * The software program was developed within a research project financed
14 * by the Italian Ministry of Universities, the Universities of Udine and
15 * Ca'Foscari of Venice, the Friuli-Venezia Giulia Region.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or any
20 * later version.
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
27 package org.tsho.dmc2.ui;
29 import java.awt.Component;
31 import javax.swing.JPanel;
33 import org.tsho.dmc2.core.VariableDoubles;
34 import org.tsho.dmc2.core.VariableItems;
35 import org.tsho.dmc2.ui.components.GetFloat;
37 import com.jgoodies.forms.builder.DefaultFormBuilder;
38 import com.jgoodies.forms.debug.FormDebugPanel;
39 import com.jgoodies.forms.layout.CellConstraints;
40 import com.jgoodies.forms.layout.FormLayout;
41 import com.jgoodies.forms.layout.RowSpec;
43 public class FormHelper {
45 public static String JGOODIES_SHORT_COLUMN_SPECS =
46 "l:11dlu:n, r:max(30dlu;pref):g, l:4dlu:n, c:32dlu:n, l:9dlu:n";
48 public static String JGOODIES_LONG_COLUMN_SPECS =
49 "l:11dlu:n, r:max(30dlu;pref):g, l:4dlu:n, r:17dlu:n, l:3dlu:n, r:13dlu:n, l:9dlu:n";
51 public static final int FIELD_LENGTH = 6;
53 private FormHelper() {
54 super();
57 public interface FormBuilder {
58 void addTitle(String s);
59 void addSubtitle(String s);
60 void addRow(String label, Component field);
61 void addRow(String label, Component field, Component vField);
62 void addGap();
63 JPanel getPanel();
66 // Normal builder
68 private static class FormBuilderImpl implements FormBuilder {
69 JPanel panel;
70 DefaultFormBuilder builder;
71 AbstractControlForm controlForm=null;
72 int id=0;//field identifier
74 protected FormBuilderImpl() {
77 protected FormBuilderImpl(AbstractControlForm controlForm, boolean debug){
78 this.controlForm=controlForm;
79 controlForm.removeAllEntries();
80 FormLayout layout = new FormLayout(JGOODIES_LONG_COLUMN_SPECS, "");
82 if (debug) {
83 panel = new FormDebugPanel();
84 builder = new DefaultFormBuilder(panel, layout);
85 } else {
86 panel = new JPanel();
87 panel.setOpaque(true);
88 builder = new DefaultFormBuilder(panel, layout);
91 builder.setDefaultDialogBorder();
92 builder.setLeadingColumnOffset(1);
95 FormBuilderImpl(boolean debug) {
96 FormLayout layout = new FormLayout(JGOODIES_LONG_COLUMN_SPECS, "");
98 if (debug) {
99 panel = new FormDebugPanel();
100 builder = new DefaultFormBuilder(panel, layout);
101 } else {
102 panel = new JPanel();
103 panel.setOpaque(true);
104 builder = new DefaultFormBuilder(panel, layout);
107 builder.setDefaultDialogBorder();
108 builder.setLeadingColumnOffset(1);
111 public void addTitle(String s) {
112 builder.appendSeparator(s);
113 builder.nextLine();
114 builder.appendRow(builder.getLineGapSpec());
115 builder.nextLine();
118 public void addRow(String label, Component field) {
119 builder.append(label, field, 3);
120 if (controlForm!=null){
121 String stringId="#"+id;
122 id++;
123 controlForm.addEntry(label,stringId,field);
125 builder.nextLine();
128 public void addRow(String label, Component field, Component vField) {
129 builder.append(label, field, vField);
130 builder.nextLine();
133 public void addSubtitle(String s) {
134 CellConstraints cc = new CellConstraints();
135 builder.appendRow(builder.getLineGapSpec());
136 builder.nextLine();
138 builder.appendRow(new RowSpec("c:p:n"));
139 builder.addSeparator(s, cc.xywh(2, builder.getRow(),
140 5, 1, "fill, fill"));
141 builder.nextLine();
142 builder.appendRow(builder.getLineGapSpec());
143 builder.nextLine();
146 public void addGap() {
147 builder.appendRow(builder.getLineGapSpec());
148 builder.nextLine();
151 public JPanel getPanel() {
152 return panel;
156 public static FormBuilder controlFormBuilder(boolean debug) {
157 return new FormBuilderImpl(debug);
161 public static FormBuilder controlFormBuilder(AbstractControlForm controlForm,boolean debug) {
162 return new FormBuilderImpl(controlForm,debug);
165 // "Variation" builder
167 // private static class VFormBuilderImpl extends FormBuilderImpl {
169 // VFormBuilderImpl(boolean debug) {
170 // FormLayout layout = new FormLayout(JGOODIES_LONG_COLUMN_SPECS, "");
172 // if (debug) {
173 // panel = new FormDebugPanel();
174 // builder = new DefaultFormBuilder(panel, layout);
175 // } else {
176 // panel = new JPanel();
177 // panel.setOpaque(true);
178 // builder = new DefaultFormBuilder(panel, layout);
179 // }
181 // builder.setDefaultDialogBorder();
182 // builder.setLeadingColumnOffset(1);
183 // }
185 // public void addRow(String label, Component field) {
186 // builder.append(label, field, 3);
187 // builder.nextLine();
188 // }
190 // public void addRow(String label, Component field, Component vField) {
191 // builder.append(label, field, vField);
192 // builder.nextLine();
193 // }
195 // public void addSubtitle(String s) {
196 // CellConstraints cc = new CellConstraints();
197 // builder.appendRow(builder.getLineGapSpec());
198 // builder.nextLine();
200 // builder.appendRow(new RowSpec("c:p:n"));
201 // builder.addSeparator(s, cc.xywh(2, builder.getRow(),
202 // 5, 1, "fill, fill"));
203 // builder.nextLine();
204 // builder.appendRow(builder.getLineGapSpec());
205 // builder.nextLine();
206 // }
207 // }
209 // public static FormBuilder controlVFormBuilder(boolean debug) {
210 // return new VFormBuilderImpl(debug);
211 // }
214 // helpers
216 public static VariableItems createFields(String[] names, String label) {
217 VariableItems.Iterator i;
218 VariableItems fields = new VariableItems(names);
219 i = fields.iterator();
220 while (i.hasNext()) {
221 String text = "\"" + i.nextLabel() + "\" " + label;
222 GetFloat field = new GetFloat(text, FormHelper.FIELD_LENGTH);
223 fields.put(i.label(), field);
225 return fields;
228 public static VariableDoubles collectFieldValues(
229 VariableItems fields) throws InvalidData {
231 VariableDoubles result;
232 VariableDoubles.Iterator i;
234 result = new VariableDoubles(fields.labelsArray());
235 i = result.iterator();
237 while (i.hasNext()) {
238 String label = i.nextLabel();
239 result.put(label, ((GetFloat) fields.get(label)).getValue());
242 return result;
245 public static void setFieldValues(
246 VariableItems fields, VariableDoubles init) {
248 VariableItems.Iterator i = fields.iterator();
249 while (i.hasNext()) {
250 ((GetFloat) i.nextValue()).setValue(init.get(i.label()));