001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.crm.report.properties.editors;
015:
016: import org.jetbrains.annotations.NotNull;
017: import org.jfree.formula.parser.ParseException;
018: import org.jfree.formula.parser.TokenMgrError;
019: import org.jfree.report.function.FormulaExpression;
020: import org.pentaho.reportdesigner.crm.report.model.Formula;
021: import org.pentaho.reportdesigner.crm.report.properties.PropertyTable;
022: import org.pentaho.reportdesigner.lib.client.components.CenterPanelDialog;
023: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
024: import org.pentaho.reportdesigner.lib.client.util.GUIUtils;
025: import org.pentaho.reportdesigner.lib.client.util.WindowUtils;
026:
027: import javax.swing.*;
028: import javax.swing.event.DocumentEvent;
029: import javax.swing.event.DocumentListener;
030: import java.awt.*;
031: import java.awt.event.ActionEvent;
032: import java.awt.event.ActionListener;
033:
034: /**
035: * User: Martin
036: * Date: 01.11.2005
037: * Time: 10:23:10
038: */
039: public class FormulaChooser {
040:
041: private FormulaChooser() {
042: }
043:
044: @NotNull
045: public static Formula showFormulaChooser(@NotNull
046: PropertyTable parent, @NotNull
047: String title, @NotNull
048: Formula origFormula) {
049: final CenterPanelDialog centerPanelDialog = CenterPanelDialog
050: .createDialog(parent, title,
051: CenterPanelDialog.ModalityType.DOCUMENT_MODAL);
052:
053: final boolean[] action = new boolean[] { false };
054:
055: JButton okButton = new JButton(TranslationManager.getInstance()
056: .getTranslation("R", "Button.ok"));
057: okButton.addActionListener(new ActionListener() {
058: public void actionPerformed(@NotNull
059: ActionEvent e) {
060: action[0] = true;
061: centerPanelDialog.dispose();
062: }
063: });
064:
065: JButton cancelButton = new JButton(TranslationManager
066: .getInstance().getTranslation("R", "Button.cancel"));
067: cancelButton.addActionListener(new ActionListener() {
068: public void actionPerformed(@NotNull
069: ActionEvent e) {
070: action[0] = false;
071: centerPanelDialog.dispose();
072: }
073: });
074:
075: centerPanelDialog.setButtons(okButton, cancelButton, okButton,
076: cancelButton);
077:
078: final JTextArea formulaTextArea = new JTextArea(origFormula
079: .getText());
080: final JLabel parseStatusLine = new JLabel(" ");
081: parseStatusLine.setPreferredSize(new Dimension(300,
082: parseStatusLine.getPreferredSize().height));
083: parseStatusLine.setForeground(Color.RED);
084: formulaTextArea.getDocument().addDocumentListener(
085: new DocumentListener() {
086: public void insertUpdate(@NotNull
087: DocumentEvent e) {
088: updateFormulaTextArea(formulaTextArea,
089: parseStatusLine);
090: }
091:
092: public void removeUpdate(@NotNull
093: DocumentEvent e) {
094: updateFormulaTextArea(formulaTextArea,
095: parseStatusLine);
096: }
097:
098: public void changedUpdate(@NotNull
099: DocumentEvent e) {
100: updateFormulaTextArea(formulaTextArea,
101: parseStatusLine);
102: }
103: });
104:
105: updateFormulaTextArea(formulaTextArea, parseStatusLine);
106:
107: JPanel centerPanel = new JPanel(new BorderLayout());
108: centerPanel.add(new JScrollPane(formulaTextArea),
109: BorderLayout.CENTER);
110: centerPanel.add(parseStatusLine, BorderLayout.SOUTH);
111: centerPanelDialog.setCenterPanel(centerPanel);
112:
113: centerPanelDialog.pack();
114: GUIUtils.ensureMinimumDialogSize(centerPanelDialog, 500, 400);
115: WindowUtils.setLocationRelativeTo(centerPanelDialog, parent);
116: centerPanelDialog.setVisible(true);
117:
118: if (action[0]) {
119: return new Formula(formulaTextArea.getText());
120: }
121: //return font;
122: return origFormula;
123: }
124:
125: private static void updateFormulaTextArea(@NotNull
126: JTextArea formulaTextArea, @NotNull
127: JLabel parseStatusLine) {
128: //noinspection ErrorNotRethrown
129: try {
130: String s = formulaTextArea.getText();
131: FormulaExpression formulaExpression = new FormulaExpression();
132: formulaExpression.setFormula(s);
133: formulaExpression.getFormulaExpression();
134:
135: //noinspection ResultOfObjectAllocationIgnored
136: if (formulaExpression.getFormulaExpression() != null) {
137: //noinspection ResultOfObjectAllocationIgnored
138: new org.jfree.formula.Formula(formulaExpression
139: .getFormulaExpression());
140: parseStatusLine.setText(" ");
141: }
142: } catch (TokenMgrError e) {
143: parseStatusLine.setText(e.getMessage());
144: } catch (ParseException e) {
145: parseStatusLine.setText(e.getMessage());
146: }
147: }
148:
149: }
|