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 com.jgoodies.forms.layout.CellConstraints;
017: import com.jgoodies.forms.layout.FormLayout;
018: import org.jetbrains.annotations.NonNls;
019: import org.jetbrains.annotations.NotNull;
020: import org.jetbrains.annotations.Nullable;
021: import org.pentaho.reportdesigner.lib.client.components.CenterPanelDialog;
022: import org.pentaho.reportdesigner.lib.client.components.TextComponentHelper;
023: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
024: import org.pentaho.reportdesigner.lib.client.util.UndoHelper;
025: import org.pentaho.reportdesigner.lib.client.util.WindowUtils;
026:
027: import javax.swing.*;
028: import java.awt.event.ActionEvent;
029: import java.awt.event.ActionListener;
030: import java.util.logging.Level;
031: import java.util.logging.Logger;
032:
033: /**
034: * User: Martin
035: * Date: 01.11.2005
036: * Time: 10:23:10
037: */
038: public class DoubleValueChooser {
039: @NonNls
040: @NotNull
041: private static final Logger LOG = Logger
042: .getLogger(DoubleValueChooser.class.getName());
043:
044: public static class Pair<T> {
045: @NotNull
046: private T value1;
047: @NotNull
048: private T value2;
049:
050: public Pair(@NotNull
051: T value1, @NotNull
052: T value2) {
053: this .value1 = value1;
054: this .value2 = value2;
055: }
056:
057: @NotNull
058: public T getValue1() {
059: return value1;
060: }
061:
062: @NotNull
063: public T getValue2() {
064: return value2;
065: }
066: }
067:
068: private DoubleValueChooser() {
069: }
070:
071: @Nullable
072: public static Pair<Double> showValueChooser(@NotNull
073: JComponent parent, @NotNull
074: String title, @NotNull
075: String valueLabel1, @NotNull
076: String valueLabel2, @Nullable
077: Pair<Double> initialValue) {
078: final CenterPanelDialog centerPanelDialog = CenterPanelDialog
079: .createDialog(parent, title,
080: CenterPanelDialog.ModalityType.DOCUMENT_MODAL);
081:
082: @NonNls
083: FormLayout formLayout = new FormLayout(
084: "0dlu, right:max(60dlu;default), 4dlu, fill:default:grow, 0dlu",
085: "0dlu, " + "pref, " + "4dlu, " + "pref, " + "4dlu, "
086: + "0dlu:grow");
087:
088: JPanel centerPanel = new JPanel(formLayout);
089: CellConstraints cc = new CellConstraints();
090:
091: JTextField textField1 = new JTextField(
092: initialValue != null ? String.valueOf(initialValue
093: .getValue1()) : "");
094: UndoHelper.installUndoSupport(textField1);
095: TextComponentHelper.installDefaultPopupMenu(textField1);
096:
097: JTextField textField2 = new JTextField(
098: initialValue != null ? String.valueOf(initialValue
099: .getValue2()) : "");
100: UndoHelper.installUndoSupport(textField2);
101: TextComponentHelper.installDefaultPopupMenu(textField2);
102:
103: centerPanel.add(new JLabel(valueLabel1), cc.xy(2, 2));
104: centerPanel.add(textField1, cc.xy(4, 2));
105: centerPanel.add(new JLabel(valueLabel2), cc.xy(2, 4));
106: centerPanel.add(textField2, cc.xy(4, 4));
107:
108: final boolean[] action = new boolean[] { false };
109:
110: JButton okButton = new JButton(TranslationManager.getInstance()
111: .getTranslation("R", "Button.ok"));
112: okButton.addActionListener(new ActionListener() {
113: public void actionPerformed(@NotNull
114: ActionEvent e) {
115: action[0] = true;
116: centerPanelDialog.dispose();
117: }
118: });
119:
120: JButton cancelButton = new JButton(TranslationManager
121: .getInstance().getTranslation("R", "Button.cancel"));
122: cancelButton.addActionListener(new ActionListener() {
123: public void actionPerformed(@NotNull
124: ActionEvent e) {
125: action[0] = false;
126: centerPanelDialog.dispose();
127: }
128: });
129:
130: centerPanelDialog.setButtons(okButton, cancelButton, okButton,
131: cancelButton);
132:
133: centerPanelDialog.setCenterPanel(centerPanel);
134: centerPanelDialog.pack();
135: WindowUtils.setLocationRelativeTo(centerPanelDialog, parent);
136: centerPanelDialog.setVisible(true);
137:
138: if (action[0]) {
139: Double d1;
140: Double d2;
141: try {
142: d1 = Double.valueOf(textField1.getText());
143: d2 = Double.valueOf(textField2.getText());
144: } catch (NumberFormatException e) {
145: if (LOG.isLoggable(Level.FINE))
146: LOG.log(Level.FINE,
147: "DoubleValueChooser.showValueChooser ", e);
148: return initialValue;
149: }
150:
151: return new Pair<Double>(d1, d2);
152: }
153: return initialValue;
154: }
155: }
|