001: /*
002: * Created on 23.06.2005 for PIROL
003: *
004: * SVN header information:
005: * $Author: javamap $
006: * $Rev: 856 $
007: * $Date: 2007-06-18 21:15:27 -0700 (Mon, 18 Jun 2007) $
008: * $Id: FormulaEditingPanel.java 856 2007-06-19 04:15:27Z javamap $
009: */
010: package de.fho.jump.pirol.plugins.EditAttributeByFormula;
011:
012: import java.awt.BorderLayout;
013: import java.awt.Color;
014: import java.awt.Dimension;
015: import java.awt.Font;
016: import java.awt.GridLayout;
017: import java.awt.event.ActionEvent;
018: import java.awt.event.ActionListener;
019:
020: import javax.swing.Box;
021: import javax.swing.JButton;
022: import javax.swing.JComboBox;
023: import javax.swing.JLabel;
024: import javax.swing.JPanel;
025: import javax.swing.JScrollPane;
026: import javax.swing.JTextArea;
027: import javax.swing.JTextField;
028:
029: import com.vividsolutions.jump.I18N;
030: import com.vividsolutions.jump.feature.AttributeType;
031: import com.vividsolutions.jump.feature.FeatureSchema;
032: import com.vividsolutions.jump.workbench.ui.GenericNames;
033:
034: import de.fho.jump.pirol.ui.documents.NumberInputDocument;
035: import de.fho.jump.pirol.ui.panels.NewAttributePanel;
036: import de.fho.jump.pirol.ui.tools.ValueChecker;
037: import de.fho.jump.pirol.utilities.FormulaParsing.FormulaParser;
038: import de.fho.jump.pirol.utilities.FormulaParsing.FormulaValue;
039: import de.fho.jump.pirol.utilities.Properties.PropertiesHandler;
040: import de.fho.jump.pirol.utilities.apiTools.FeatureSchemaTools;
041: import de.fho.jump.pirol.utilities.attributes.AttributeInfo;
042: import de.fho.jump.pirol.utilities.debugOutput.DebugUserIds;
043: import de.fho.jump.pirol.utilities.debugOutput.PersonalLogger;
044:
045: /**
046: * Panel to help the user to create a formula that describes how the value of a new attribute will be calculated.
047: *
048: * @author Ole Rahn
049: * <br>
050: * <br>FH Osnabrück - University of Applied Sciences Osnabrück,
051: * <br>Project: PIROL (2005),
052: * <br>Subproject: Daten- und Wissensmanagement
053: *
054: * @version $Rev: 856 $
055: *
056: */
057: public class FormulaEditingPanel extends JPanel implements
058: ActionListener, ValueChecker {
059:
060: protected PersonalLogger logger = new PersonalLogger(
061: DebugUserIds.ALL);
062:
063: private static final long serialVersionUID = -7709755834905111906L;
064:
065: protected JTextArea formulaField = new JTextArea(4, 20),
066: errorMessages = new JTextArea(3, 20);
067: protected FeatureSchema featureSchema = null;
068: protected AttributeInfo[] attributeInfos = null;
069:
070: protected JTextField numberInputField = null;
071: protected JComboBox storedFormulasDropDown = new JComboBox();
072:
073: protected static final String[] mathSigns = new String[] { "+",
074: "-", "*", "/", "(", ")", FormulaParser.KEY_SQRT,
075: FormulaParser.KEY_POW };
076: private PropertiesHandler storedFormulas;
077: private NewAttributePanel newAttributePanel;
078:
079: protected FormulaValue parsedFormula = null;
080:
081: public FormulaEditingPanel(FeatureSchema featureSchema,
082: PropertiesHandler storedFormulas,
083: NewAttributePanel newAttributePanel) {
084: super ();
085: this .featureSchema = featureSchema;
086: this .attributeInfos = FeatureSchemaTools
087: .getAttributesWithTypes(this .featureSchema,
088: new AttributeType[] { AttributeType.DOUBLE,
089: AttributeType.INTEGER });
090:
091: this .formulaField.setWrapStyleWord(true);
092: this .storedFormulas = storedFormulas;
093: this .newAttributePanel = newAttributePanel;
094:
095: this .setupUI();
096: }
097:
098: //-- [sstein] new constructor since we do not assume property-files
099: public FormulaEditingPanel(FeatureSchema featureSchema,
100: NewAttributePanel newAttributePanel) {
101: super ();
102: this .featureSchema = featureSchema;
103: this .attributeInfos = FeatureSchemaTools
104: .getAttributesWithTypes(this .featureSchema,
105: new AttributeType[] { AttributeType.DOUBLE,
106: AttributeType.INTEGER });
107:
108: this .formulaField.setWrapStyleWord(true);
109: //this.storedFormulas = storedFormulas;
110: this .newAttributePanel = newAttributePanel;
111:
112: this .setupUI();
113: }
114:
115: protected void setupUI() {
116: this .setLayout(new BorderLayout());
117: JPanel formulaAndOperators = new JPanel();
118: BorderLayout moreGenerousLayout = new BorderLayout();
119: moreGenerousLayout.setVgap(15);
120: formulaAndOperators.setLayout(moreGenerousLayout);
121:
122: //-- [sstein 23.March.2007] -- disable since we do not assume an existing file
123: JPanel loadedForms = null;
124: if (this .storedFormulas != null) {
125: loadedForms = new JPanel();
126: loadedForms.setLayout(new BorderLayout());
127:
128: loadedForms
129: .add(
130: new JLabel(
131: I18N
132: .get("pirol.plugIns.FormulaEditingPanel.load-formula") + " : "), BorderLayout.WEST); //$NON-NLS-1$
133:
134: String[] formulaNames = (String[]) this .storedFormulas
135: .keySet().toArray(new String[0]);
136:
137: for (int i = 0; i < formulaNames.length; i++) {
138: this .storedFormulasDropDown.addItem(formulaNames[i]);
139: }
140: this .storedFormulasDropDown.setSelectedItem(null);
141: this .storedFormulasDropDown.addActionListener(this );
142:
143: loadedForms.add(this .storedFormulasDropDown,
144: BorderLayout.CENTER);
145: formulaAndOperators.add(loadedForms, BorderLayout.NORTH);
146: }
147: formulaAndOperators.add(this .formulaField, BorderLayout.CENTER);
148:
149: JPanel mathSignsButtonPanel = new JPanel();
150: int gridColumns = FormulaEditingPanel.mathSigns.length / 2;
151: mathSignsButtonPanel.setLayout(new GridLayout(
152: (FormulaEditingPanel.mathSigns.length / gridColumns),
153: gridColumns));
154: JButton button;
155:
156: for (int i = 0; i < FormulaEditingPanel.mathSigns.length; i++) {
157: button = new JButton();
158: button.setAction(new AddFormulaPartToTextArea_Action(
159: FormulaEditingPanel.mathSigns[i],
160: this .formulaField, FormulaEditingPanel.mathSigns,
161: this .featureSchema));
162: mathSignsButtonPanel.add(button);
163: }
164:
165: formulaAndOperators.add(mathSignsButtonPanel,
166: BorderLayout.SOUTH);
167:
168: this .add(formulaAndOperators, BorderLayout.NORTH);
169:
170: Box vbox = Box.createVerticalBox();
171:
172: Box hbox = Box.createHorizontalBox();
173: int sumOfWidthes = 0;
174: //-- [sstein] new
175: int wantedWidth = 0;
176: if (loadedForms != null) {
177: wantedWidth = loadedForms.getPreferredSize().width;
178: }
179: for (int i = 0; i < attributeInfos.length; i++) {
180: button = new JButton();
181: button.setAction(new AddFormulaPartToTextArea_Action(
182: attributeInfos[i].getUniqueAttributeName(),
183: this .formulaField, FormulaEditingPanel.mathSigns,
184: this .featureSchema));
185:
186: hbox.add(button);
187:
188: sumOfWidthes += button.getPreferredSize().width;
189:
190: logger.printDebug("sumOfWidthes: " + sumOfWidthes
191: + ", wantedWidth: " + wantedWidth);
192:
193: if (sumOfWidthes >= wantedWidth
194: || i == attributeInfos.length - 1) {
195: hbox.add(Box.createGlue());
196: vbox.add(hbox);
197: vbox.add(Box.createVerticalStrut(5));
198: hbox = Box.createHorizontalBox();
199: sumOfWidthes = 0;
200: }
201: }
202:
203: vbox.add(Box.createVerticalGlue());
204:
205: JScrollPane scrollPane = new JScrollPane(vbox);
206: scrollPane.setSize(new Dimension(wantedWidth, 200));
207: scrollPane.setMinimumSize(scrollPane.getSize());
208: scrollPane.setPreferredSize(scrollPane.getSize());
209: this .setPreferredSize(new Dimension(wantedWidth, 320));
210: this .setSize(new Dimension(wantedWidth, 320));
211: this .add(scrollPane, BorderLayout.CENTER);
212:
213: JPanel numberInputAndErrorPanel = new JPanel(new BorderLayout());
214: JPanel numberInputPanel = new JPanel(new BorderLayout());
215: this .numberInputField = new JTextField();
216: this .numberInputField.setDocument(new NumberInputDocument());
217:
218: numberInputPanel
219: .add(this .numberInputField, BorderLayout.CENTER);
220: numberInputPanel
221: .add(
222: new JButton(
223: new AddTextFieldTextToTextAreaOnClick_Action(
224: this .numberInputField,
225: this .formulaField,
226: I18N
227: .get("pirol.plugIns.FormulaEditingPanel.copy-value-to-formula"))), BorderLayout.EAST); //$NON-NLS-1$
228:
229: numberInputAndErrorPanel.add(numberInputPanel,
230: BorderLayout.NORTH);
231: numberInputAndErrorPanel.add(this .errorMessages,
232: BorderLayout.SOUTH);
233: this .errorMessages.setEditable(false);
234: this .errorMessages.setWrapStyleWord(true);
235: this .errorMessages.setLineWrap(true);
236: this .errorMessages.setFont(this .errorMessages.getFont()
237: .deriveFont(Font.BOLD));
238: this .errorMessages.setForeground(Color.red);
239: this .errorMessages.setBackground(this .getBackground());
240:
241: this .add(numberInputAndErrorPanel, BorderLayout.SOUTH);
242:
243: }
244:
245: /**
246: *@return formula as String
247: */
248: public String getFormula() {
249: return this .formulaField.getText();
250: }
251:
252: /**
253: *@return parsed formula as FormulaValue object
254: */
255: public FormulaValue getParsedFormula() {
256: return this .parsedFormula;
257: }
258:
259: /**
260: * ... to react on a selction in the stored formula drop down menu ...
261: *@param event
262: */
263: public void actionPerformed(ActionEvent event) {
264: if (this .storedFormulas != null) {
265: String selectedFormName = this .storedFormulasDropDown
266: .getSelectedItem().toString();
267: this .formulaField.setText(this .storedFormulas
268: .getProperty(selectedFormName));
269: this .newAttributePanel.setAttributeName(selectedFormName);
270: }
271: }
272:
273: /**
274: *@inheritDoc
275: */
276: public boolean areValuesOk() {
277: try {
278: this .parsedFormula = FormulaParser.getValue(this
279: .getFormula(), this .featureSchema);
280: } catch (RuntimeException e) {
281: this .parsedFormula = null;
282: this .errorMessages
283: .setText("\n" + GenericNames.ERROR + ": " + e.getMessage()); //$NON-NLS-1$
284: return false;
285: }
286: return true;
287: }
288:
289: }
|