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.crm.report.model.ReportElement;
023: import org.pentaho.reportdesigner.lib.client.components.CenterPanelDialog;
024: import org.pentaho.reportdesigner.lib.client.components.TextComponentHelper;
025: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
026: import org.pentaho.reportdesigner.lib.client.util.UndoHelper;
027: import org.pentaho.reportdesigner.lib.client.util.WindowUtils;
028:
029: import javax.swing.*;
030: import java.awt.*;
031: import java.awt.event.ActionEvent;
032: import java.awt.event.ActionListener;
033: import java.util.ArrayList;
034:
035: /**
036: * User: Martin
037: * Date: 11.01.2006
038: * Time: 11:02:54
039: */
040: public class StringArrayChooser {
041: public enum Type {
042: @NotNull
043: FIELD, @NotNull
044: ELEMENT, @NotNull
045: STRING
046: }
047:
048: private StringArrayChooser() {
049: }
050:
051: @Nullable
052: public static String[] showStringArrayChooser(@NotNull
053: final Type type, @Nullable
054: final ReportElement reportElement, @NotNull
055: JComponent parent, @NotNull
056: String title, @NotNull
057: final String labelPrefix, @Nullable
058: String[] stringArray) {
059: //noinspection ConstantConditions
060: if (type == null) {
061: throw new IllegalArgumentException("type must not be null");
062: }
063: //noinspection ConstantConditions
064: if (parent == null) {
065: throw new IllegalArgumentException(
066: "parent must not be null");
067: }
068: //noinspection ConstantConditions
069: if (title == null) {
070: throw new IllegalArgumentException("title must not be null");
071: }
072: //noinspection ConstantConditions
073: if (labelPrefix == null) {
074: throw new IllegalArgumentException(
075: "labelPrefix must not be null");
076: }
077:
078: final CenterPanelDialog centerPanelDialog = CenterPanelDialog
079: .createDialog(parent, title,
080: CenterPanelDialog.ModalityType.DOCUMENT_MODAL);
081:
082: @NonNls
083: final FormLayout formLayout = new FormLayout(
084: "4dlu, pref, 4dlu, fill:default:grow, 4dlu, pref, 4dlu",
085: "4dlu");
086: final JPanel centerPanel = new JPanel(formLayout);
087:
088: @NonNls
089: FormLayout fl = new FormLayout("4dlu, fill:pref:grow, 4dlu",
090: "4dlu, fill:default, 4dlu, pref, 4dlu:grow");
091: @NonNls
092: final CellConstraints cc = new CellConstraints();
093: final JPanel panel = new JPanel(fl);
094: JScrollPane scrollPane = new JScrollPane(centerPanel);
095: scrollPane.setBorder(BorderFactory.createEmptyBorder());
096: panel.add(scrollPane, cc.xy(2, 2));
097:
098: final ArrayList<Row> rows = new ArrayList<Row>();
099:
100: JButton addButton = new JButton(TranslationManager
101: .getInstance().getTranslation("R",
102: "StringArrayChooser.AddButton"));
103: panel.add(addButton, cc.xy(2, 4, "right, center"));
104:
105: if (stringArray != null) {
106: for (int i = 0; i < stringArray.length; i++) {
107: String s = stringArray[i];
108: JLabel label = new JLabel(labelPrefix + "[" + i + "]");
109: final JTextField textField = new JTextField(s);
110: UndoHelper.installUndoSupport(textField);
111: TextComponentHelper.installDefaultPopupMenu(textField);
112:
113: JButton removeButton = new JButton("-");
114: removeButton.setMargin(new Insets(1, 1, 1, 1));
115:
116: JButton chooseFieldButton = null;
117: if (reportElement != null) {
118: chooseFieldButton = new JButton("...");
119: final JButton chooseFieldButton1 = chooseFieldButton;
120: chooseFieldButton
121: .addActionListener(new ActionListener() {
122: public void actionPerformed(@NotNull
123: ActionEvent e) {
124: String newValue = null;
125: if (type == Type.FIELD) {
126: newValue = FieldChooser
127: .showFieldChooser(
128: reportElement,
129: chooseFieldButton1,
130: TranslationManager
131: .getInstance()
132: .getTranslation(
133: "R",
134: "PropertyEditor.Field.Title"),
135: textField
136: .getText());
137: } else if (type == Type.ELEMENT) {
138: newValue = ElementNameChooser
139: .showElementChooser(
140: reportElement,
141: chooseFieldButton1,
142: TranslationManager
143: .getInstance()
144: .getTranslation(
145: "R",
146: "PropertyEditor.Field.Title"),
147: textField
148: .getText());
149: }
150:
151: if (newValue != null) {
152: textField.setText(newValue);
153: }
154: }
155: });
156: }
157:
158: final Row row = new Row(i, label, textField,
159: chooseFieldButton, removeButton);
160: rows.add(row);
161: removeButton.addActionListener(new ActionListener() {
162: public void actionPerformed(@NotNull
163: ActionEvent e) {
164: rows.remove(row);
165: rebuildPanel(rows, labelPrefix, centerPanel);
166:
167: panel.revalidate();
168: panel.repaint();
169: }
170: });
171: }
172:
173: rebuildPanel(rows, labelPrefix, centerPanel);
174:
175: panel.revalidate();
176: panel.repaint();
177: }
178:
179: addButton.addActionListener(new ActionListener() {
180: public void actionPerformed(@NotNull
181: ActionEvent e) {
182: addRow(type, reportElement, labelPrefix, rows,
183: centerPanel, panel);
184: }
185: });
186:
187: if (stringArray == null || stringArray.length == 0) {
188: addRow(type, reportElement, labelPrefix, rows, centerPanel,
189: panel);
190: }
191:
192: final boolean[] action = new boolean[] { false };
193:
194: JButton okButton = new JButton(TranslationManager.getInstance()
195: .getTranslation("R", "Button.ok"));
196: okButton.addActionListener(new ActionListener() {
197: public void actionPerformed(@NotNull
198: ActionEvent e) {
199: action[0] = true;
200: centerPanelDialog.dispose();
201: }
202: });
203:
204: JButton cancelButton = new JButton(TranslationManager
205: .getInstance().getTranslation("R", "Button.cancel"));
206: cancelButton.addActionListener(new ActionListener() {
207: public void actionPerformed(@NotNull
208: ActionEvent e) {
209: action[0] = false;
210: centerPanelDialog.dispose();
211: }
212: });
213:
214: centerPanelDialog.setButtons(okButton, cancelButton, okButton,
215: cancelButton);
216:
217: centerPanelDialog.setCenterPanel(panel);
218: centerPanelDialog.pack();
219: centerPanelDialog.setSize(300, 300);
220: WindowUtils.setLocationRelativeTo(centerPanelDialog, parent);
221: centerPanelDialog.setVisible(true);
222:
223: if (action[0]) {
224: return getValues(rows);
225: }
226: return stringArray;
227: }
228:
229: private static void addRow(@NotNull
230: final Type type, @Nullable
231: final ReportElement reportElement, @NotNull
232: final String labelPrefix, @NotNull
233: final ArrayList<Row> rows, @NotNull
234: final JPanel centerPanel, @NotNull
235: final JPanel panel) {
236: JLabel label = new JLabel(labelPrefix);
237: final JTextField textField = new JTextField("");
238: UndoHelper.installUndoSupport(textField);
239: TextComponentHelper.installDefaultPopupMenu(textField);
240:
241: JButton removeButton = new JButton("-");
242: removeButton.setMargin(new Insets(1, 1, 1, 1));
243:
244: JButton chooseFieldButton = null;
245: if (reportElement != null) {
246: chooseFieldButton = new JButton("...");
247: final JButton chooseFieldButton1 = chooseFieldButton;
248: chooseFieldButton.addActionListener(new ActionListener() {
249: public void actionPerformed(@NotNull
250: ActionEvent e) {
251: String newValue = null;
252: if (type == Type.FIELD) {
253: newValue = FieldChooser
254: .showFieldChooser(
255: reportElement,
256: chooseFieldButton1,
257: TranslationManager
258: .getInstance()
259: .getTranslation("R",
260: "PropertyEditor.Field.Title"),
261: textField.getText());
262: } else if (type == Type.ELEMENT) {
263: newValue = ElementNameChooser
264: .showElementChooser(
265: reportElement,
266: chooseFieldButton1,
267: TranslationManager
268: .getInstance()
269: .getTranslation("R",
270: "PropertyEditor.Field.Title"),
271: textField.getText());
272: }
273:
274: if (newValue != null) {
275: textField.setText(newValue);
276: }
277: }
278: });
279: }
280:
281: final Row row = new Row(rows.size(), label, textField,
282: chooseFieldButton, removeButton);
283: rows.add(row);
284:
285: removeButton.addActionListener(new ActionListener() {
286: public void actionPerformed(@NotNull
287: ActionEvent e) {
288: rows.remove(row);
289: rebuildPanel(rows, labelPrefix, centerPanel);
290:
291: panel.revalidate();
292: panel.repaint();
293: }
294: });
295: rebuildPanel(rows, labelPrefix, centerPanel);
296:
297: textField.requestFocusInWindow();
298:
299: panel.revalidate();
300: panel.repaint();
301: }
302:
303: @NotNull
304: private static String[] getValues(@NotNull
305: ArrayList<Row> rows) {
306: String[] array = new String[rows.size()];
307: for (int i = 0; i < rows.size(); i++) {
308: Row row = rows.get(i);
309: array[i] = row.getTextField().getText();
310: }
311:
312: return array;
313: }
314:
315: private static void rebuildPanel(@NotNull
316: ArrayList<Row> rows, @NotNull
317: String labelPrefix, @NotNull
318: JPanel panel) {
319: panel.removeAll();
320:
321: @NonNls
322: FormLayout formLayout = new FormLayout(
323: "4dlu, default, 4dlu, fill:10dlu:grow, 2dlu, default, 4dlu, default, 4dlu",
324: "4dlu");
325: @NonNls
326: CellConstraints cc = new CellConstraints();
327:
328: panel.setLayout(formLayout);
329:
330: int currentRow = 1;
331: for (int i = 0; i < rows.size(); i++) {
332: Row row = rows.get(i);
333:
334: formLayout.appendRow(new RowSpec("pref"));//NON-NLS
335: currentRow++;
336: row.setCurrentRow(i);
337: row.getLabel().setText(labelPrefix + "[" + i + "]");
338: panel.add(row.getLabel(), cc.xy(2, currentRow));
339: panel.add(row.getTextField(), cc.xy(4, currentRow));
340: if (row.getChooseFieldButton() != null) {
341: panel.add(row.getChooseFieldButton(), cc.xy(6,
342: currentRow));
343: }
344: panel.add(row.getRemoveButton(), cc.xy(8, currentRow));
345: formLayout.appendRow(new RowSpec("4dlu"));//NON-NLS
346: currentRow++;
347: }
348:
349: panel.revalidate();
350: panel.repaint();
351: }
352:
353: private static class Row {
354: private int currentRow;
355: @NotNull
356: private JLabel label;
357: @NotNull
358: private JTextField textField;
359: @Nullable
360: private JButton chooseFieldButton;
361: @NotNull
362: private JButton removeButton;
363:
364: private Row(int currentRow, @NotNull
365: JLabel label, @NotNull
366: JTextField textField, @Nullable
367: JButton chooseFieldButton, @NotNull
368: JButton removeButton) {
369: this .currentRow = currentRow;
370: this .label = label;
371: this .textField = textField;
372: this .chooseFieldButton = chooseFieldButton;
373: this .removeButton = removeButton;
374: }
375:
376: public int getCurrentRow() {
377: return currentRow;
378: }
379:
380: public void setCurrentRow(int currentRow) {
381: this .currentRow = currentRow;
382: }
383:
384: @NotNull
385: public JLabel getLabel() {
386: return label;
387: }
388:
389: @NotNull
390: public JTextField getTextField() {
391: return textField;
392: }
393:
394: @NotNull
395: public JButton getRemoveButton() {
396: return removeButton;
397: }
398:
399: @Nullable
400: public JButton getChooseFieldButton() {
401: return chooseFieldButton;
402: }
403: }
404: }
|