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 com.jgoodies.forms.layout.RowSpec;
019: import org.jetbrains.annotations.NonNls;
020: import org.jetbrains.annotations.NotNull;
021: import org.jetbrains.annotations.Nullable;
022: import org.pentaho.reportdesigner.lib.client.components.CenterPanelDialog;
023: import org.pentaho.reportdesigner.lib.client.components.TextComponentHelper;
024: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
025: import org.pentaho.reportdesigner.lib.client.util.UndoHelper;
026: import org.pentaho.reportdesigner.lib.client.util.WindowUtils;
027:
028: import javax.swing.*;
029: import java.awt.*;
030: import java.awt.event.ActionEvent;
031: import java.awt.event.ActionListener;
032: import java.util.ArrayList;
033: import java.util.logging.Level;
034: import java.util.logging.Logger;
035:
036: /**
037: * User: Martin
038: * Date: 11.01.2006
039: * Time: 11:02:54
040: */
041: public class NumberArrayChooser {
042: @NonNls
043: @NotNull
044: private static final Logger LOG = Logger
045: .getLogger(NumberArrayChooser.class.getName());
046:
047: private NumberArrayChooser() {
048: }
049:
050: @Nullable
051: public static Number[] showNumberArrayChooser(@NotNull
052: JComponent parent, @NotNull
053: String title, @NotNull
054: final String labelPrefix, @Nullable
055: Number[] numberArray) {
056: final CenterPanelDialog centerPanelDialog = CenterPanelDialog
057: .createDialog(parent, title,
058: CenterPanelDialog.ModalityType.DOCUMENT_MODAL);
059:
060: @NonNls
061: final FormLayout formLayout = new FormLayout(
062: "4dlu, pref, 4dlu, fill:default:grow, 4dlu, pref, 4dlu",
063: "4dlu");
064: final JPanel centerPanel = new JPanel(formLayout);
065:
066: @NonNls
067: FormLayout fl = new FormLayout("4dlu, fill:pref:grow, 4dlu",
068: "4dlu, fill:default, 4dlu, pref, 4dlu:grow");
069: @NonNls
070: final CellConstraints cc = new CellConstraints();
071: final JPanel panel = new JPanel(fl);
072: JScrollPane scrollPane = new JScrollPane(centerPanel);
073: scrollPane.setBorder(BorderFactory.createEmptyBorder());
074: panel.add(scrollPane, cc.xy(2, 2));
075:
076: final ArrayList<Row> rows = new ArrayList<Row>();
077:
078: JButton addButton = new JButton(TranslationManager
079: .getInstance().getTranslation("R",
080: "StringArrayChooser.AddButton"));
081: panel.add(addButton, cc.xy(2, 4, "right, center"));
082:
083: if (numberArray != null) {
084: for (int i = 0; i < numberArray.length; i++) {
085: Number d = numberArray[i];
086: JLabel label = new JLabel(labelPrefix + "[" + i + "]");
087: JTextField textField = new JTextField(String.valueOf(d));
088: UndoHelper.installUndoSupport(textField);
089: TextComponentHelper.installDefaultPopupMenu(textField);
090:
091: JButton removeButton = new JButton("-");
092: removeButton.setMargin(new Insets(1, 1, 1, 1));
093: final Row row = new Row(i, label, textField,
094: removeButton);
095: rows.add(row);
096: removeButton.addActionListener(new ActionListener() {
097: public void actionPerformed(@NotNull
098: ActionEvent e) {
099: rows.remove(row);
100: rebuildPanel(rows, labelPrefix, centerPanel);
101:
102: panel.revalidate();
103: panel.repaint();
104: }
105: });
106: }
107:
108: rebuildPanel(rows, labelPrefix, centerPanel);
109:
110: panel.revalidate();
111: panel.repaint();
112: }
113:
114: addButton.addActionListener(new ActionListener() {
115: public void actionPerformed(@NotNull
116: ActionEvent e) {
117: addRow(labelPrefix, rows, centerPanel, panel);
118: }
119: });
120:
121: if (numberArray == null || numberArray.length == 0) {
122: addRow(labelPrefix, rows, centerPanel, panel);
123: }
124:
125: final boolean[] action = new boolean[] { false };
126:
127: JButton okButton = new JButton(TranslationManager.getInstance()
128: .getTranslation("R", "Button.ok"));
129: okButton.addActionListener(new ActionListener() {
130: public void actionPerformed(@NotNull
131: ActionEvent e) {
132: action[0] = true;
133: centerPanelDialog.dispose();
134: }
135: });
136:
137: JButton cancelButton = new JButton(TranslationManager
138: .getInstance().getTranslation("R", "Button.cancel"));
139: cancelButton.addActionListener(new ActionListener() {
140: public void actionPerformed(@NotNull
141: ActionEvent e) {
142: action[0] = false;
143: centerPanelDialog.dispose();
144: }
145: });
146:
147: centerPanelDialog.setButtons(okButton, cancelButton, okButton,
148: cancelButton);
149:
150: centerPanelDialog.setCenterPanel(panel);
151: centerPanelDialog.pack();
152: centerPanelDialog.setSize(300, 300);
153: WindowUtils.setLocationRelativeTo(centerPanelDialog, parent);
154: centerPanelDialog.setVisible(true);
155:
156: if (action[0]) {
157: return getValues(rows);
158: }
159: return numberArray;
160: }
161:
162: private static void addRow(@NotNull
163: final String labelPrefix, @NotNull
164: final ArrayList<Row> rows, @NotNull
165: final JPanel centerPanel, @NotNull
166: final JPanel panel) {
167: JLabel label = new JLabel(labelPrefix);
168: JTextField textField = new JTextField("");
169: UndoHelper.installUndoSupport(textField);
170: TextComponentHelper.installDefaultPopupMenu(textField);
171:
172: JButton removeButton = new JButton("-");
173: removeButton.setMargin(new Insets(1, 1, 1, 1));
174:
175: final Row row = new Row(rows.size(), label, textField,
176: removeButton);
177: rows.add(row);
178:
179: removeButton.addActionListener(new ActionListener() {
180: public void actionPerformed(@NotNull
181: ActionEvent e) {
182: rows.remove(row);
183: rebuildPanel(rows, labelPrefix, centerPanel);
184:
185: panel.revalidate();
186: panel.repaint();
187: }
188: });
189: rebuildPanel(rows, labelPrefix, centerPanel);
190:
191: textField.requestFocusInWindow();
192:
193: panel.revalidate();
194: panel.repaint();
195: }
196:
197: @NotNull
198: private static Number[] getValues(@NotNull
199: ArrayList<Row> rows) {
200: Number[] array = new Number[rows.size()];
201: for (int i = 0; i < rows.size(); i++) {
202: Row row = rows.get(i);
203: try {
204: array[i] = new Double(row.getTextField().getText());
205: } catch (Exception e) {
206: if (LOG.isLoggable(Level.FINE))
207: LOG.log(Level.FINE,
208: "NumberArrayChooser.getValues ", e);
209: array[i] = new Double(0);
210: }
211: }
212:
213: return array;
214: }
215:
216: private static void rebuildPanel(@NotNull
217: ArrayList<Row> rows, @NotNull
218: String labelPrefix, @NotNull
219: JPanel panel) {
220: panel.removeAll();
221:
222: @NonNls
223: FormLayout formLayout = new FormLayout(
224: "4dlu, pref, 4dlu, fill:default:grow, 4dlu, pref, 4dlu",
225: "4dlu");
226: @NonNls
227: CellConstraints cc = new CellConstraints();
228:
229: panel.setLayout(formLayout);
230:
231: int currentRow = 1;
232: for (int i = 0; i < rows.size(); i++) {
233: Row row = rows.get(i);
234:
235: formLayout.appendRow(new RowSpec("pref"));//NON-NLS
236: currentRow++;
237: row.setCurrentRow(i);
238: row.getLabel().setText(labelPrefix + "[" + i + "]");
239: panel.add(row.getLabel(), cc.xy(2, currentRow));
240: panel.add(row.getTextField(), cc.xy(4, currentRow));
241: panel.add(row.getRemoveButton(), cc.xy(6, currentRow));
242: formLayout.appendRow(new RowSpec("4dlu"));//NON-NLS
243: currentRow++;
244: }
245:
246: panel.revalidate();
247: panel.repaint();
248: }
249:
250: private static class Row {
251: private int currentRow;
252: @NotNull
253: private JLabel label;
254: @NotNull
255: private JTextField textField;
256: @NotNull
257: private JButton removeButton;
258:
259: private Row(int currentRow, @NotNull
260: JLabel label, @NotNull
261: JTextField textField, @NotNull
262: JButton removeButton) {
263: this .currentRow = currentRow;
264: this .label = label;
265: this .textField = textField;
266: this .removeButton = removeButton;
267: }
268:
269: public int getCurrentRow() {
270: return currentRow;
271: }
272:
273: public void setCurrentRow(int currentRow) {
274: this .currentRow = currentRow;
275: }
276:
277: @NotNull
278: public JLabel getLabel() {
279: return label;
280: }
281:
282: @NotNull
283: public JTextField getTextField() {
284: return textField;
285: }
286:
287: @NotNull
288: public JButton getRemoveButton() {
289: return removeButton;
290: }
291: }
292: }
|