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.components.PaletteColorChooser;
023: import org.pentaho.reportdesigner.crm.report.util.ColorHelper;
024: import org.pentaho.reportdesigner.crm.report.util.ColorIcon;
025: import org.pentaho.reportdesigner.lib.client.components.CenterPanelDialog;
026: import org.pentaho.reportdesigner.lib.client.components.ComponentFactory;
027: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
028: import org.pentaho.reportdesigner.lib.client.util.WindowUtils;
029:
030: import javax.swing.*;
031: import javax.swing.event.DocumentEvent;
032: import javax.swing.event.DocumentListener;
033: import java.awt.*;
034: import java.awt.event.ActionEvent;
035: import java.awt.event.ActionListener;
036: import java.util.ArrayList;
037:
038: /**
039: * User: Martin
040: * Date: 11.01.2006
041: * Time: 11:02:54
042: */
043: public class StringColorArrayChooser {
044: private StringColorArrayChooser() {
045: }
046:
047: @Nullable
048: public static String[] showColorArrayChooser(@NotNull
049: JComponent parent, @NotNull
050: String title, @NotNull
051: final String labelPrefix, @Nullable
052: String[] colorArray) {
053: final CenterPanelDialog centerPanelDialog = CenterPanelDialog
054: .createDialog(parent, title,
055: CenterPanelDialog.ModalityType.DOCUMENT_MODAL);
056:
057: @NonNls
058: final FormLayout formLayout = new FormLayout(
059: "4dlu, pref, 4dlu, fill:default:grow, 4dlu, default, 4dlu, pref, 4dlu",
060: "4dlu");
061: final JPanel centerPanel = new JPanel(formLayout);
062:
063: @NonNls
064: FormLayout fl = new FormLayout("4dlu, fill:pref:grow, 4dlu",
065: "4dlu, fill:default, 4dlu, pref, 4dlu:grow");
066: @NonNls
067: final CellConstraints cc = new CellConstraints();
068: final JPanel panel = new JPanel(fl);
069: JScrollPane scrollPane = new JScrollPane(centerPanel);
070: scrollPane.setBorder(BorderFactory.createEmptyBorder());
071: panel.add(scrollPane, cc.xy(2, 2));
072:
073: final ArrayList<Row> rows = new ArrayList<StringColorArrayChooser.Row>();
074:
075: JButton addButton = new JButton(TranslationManager
076: .getInstance().getTranslation("R",
077: "StringArrayChooser.AddButton"));
078: panel.add(addButton, cc.xy(2, 4, "right, center"));
079:
080: if (colorArray != null) {
081: for (int i = 0; i < colorArray.length; i++) {
082: String color = colorArray[i];
083: JLabel label = new JLabel(labelPrefix + "[" + i + "]");
084:
085: JTextField colorTextField = ComponentFactory
086: .createTextField(true, true);
087: StringColorButton stringColorButton = new StringColorButton(
088: color);
089:
090: JButton removeButton = new JButton("-");
091: removeButton.setMargin(new Insets(1, 1, 1, 1));
092: final StringColorArrayChooser.Row row = new StringColorArrayChooser.Row(
093: i, color, label, colorTextField,
094: stringColorButton, 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 (colorArray == null || colorArray.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 colorArray;
160: }
161:
162: private static void addRow(@NotNull
163: final String labelPrefix, @NotNull
164: final ArrayList<StringColorArrayChooser.Row> rows, @NotNull
165: final JPanel centerPanel, @NotNull
166: final JPanel panel) {
167: JLabel label = new JLabel(labelPrefix);
168: JTextField colorTextField = ComponentFactory.createTextField(
169: true, true);
170: StringColorButton stringColorButton = new StringColorButton("");
171:
172: JButton removeButton = new JButton("-");
173: removeButton.setMargin(new Insets(1, 1, 1, 1));
174:
175: final StringColorArrayChooser.Row row = new StringColorArrayChooser.Row(
176: rows.size(), "", label, colorTextField,
177: stringColorButton, removeButton);
178: rows.add(row);
179:
180: removeButton.addActionListener(new ActionListener() {
181: public void actionPerformed(@NotNull
182: ActionEvent e) {
183: rows.remove(row);
184: rebuildPanel(rows, labelPrefix, centerPanel);
185:
186: panel.revalidate();
187: panel.repaint();
188: }
189: });
190: rebuildPanel(rows, labelPrefix, centerPanel);
191:
192: stringColorButton.requestFocusInWindow();
193:
194: panel.revalidate();
195: panel.repaint();
196: }
197:
198: @NotNull
199: private static String[] getValues(@NotNull
200: ArrayList<StringColorArrayChooser.Row> rows) {
201: String[] array = new String[rows.size()];
202: for (int i = 0; i < rows.size(); i++) {
203: StringColorArrayChooser.Row row = rows.get(i);
204: array[i] = row.getColorTextField().getText();
205: }
206:
207: return array;
208: }
209:
210: private static void rebuildPanel(@NotNull
211: ArrayList<StringColorArrayChooser.Row> rows, @NotNull
212: String labelPrefix, @NotNull
213: JPanel panel) {
214: panel.removeAll();
215:
216: @NonNls
217: FormLayout formLayout = new FormLayout(
218: "4dlu, pref, 4dlu, fill:default:grow, 4dlu, default, 4dlu, pref, 4dlu",
219: "4dlu");
220: @NonNls
221: CellConstraints cc = new CellConstraints();
222:
223: panel.setLayout(formLayout);
224:
225: int currentRow = 1;
226: for (int i = 0; i < rows.size(); i++) {
227: StringColorArrayChooser.Row row = rows.get(i);
228:
229: formLayout.appendRow(new RowSpec("pref"));//NON-NLS
230: currentRow++;
231: row.setCurrentRow(i);
232: row.getLabel().setText(labelPrefix + "[" + i + "]");
233: panel.add(row.getLabel(), cc.xy(2, currentRow));
234: panel.add(row.getColorTextField(), cc.xy(4, currentRow));
235: panel.add(row.getColorButton(), cc.xy(6, currentRow));
236: panel.add(row.getRemoveButton(), cc.xy(8, currentRow));
237: formLayout.appendRow(new RowSpec("4dlu"));//NON-NLS
238: currentRow++;
239: }
240:
241: panel.revalidate();
242: panel.repaint();
243: }
244:
245: private static class Row {
246: private int currentRow;
247: @NotNull
248: private String color;
249: @NotNull
250: private JLabel label;
251: @NotNull
252: private JTextField colorTextField;
253: @NotNull
254: private StringColorButton stringColorButton;
255: @NotNull
256: private JButton removeButton;
257:
258: private Row(int currentRow, @NotNull
259: String color, @NotNull
260: JLabel label, @NotNull
261: final JTextField colorTextField, @NotNull
262: final StringColorButton stringColorButton, @NotNull
263: JButton removeButton) {
264: this .currentRow = currentRow;
265: this .color = color;
266: this .label = label;
267: this .colorTextField = colorTextField;
268: this .stringColorButton = stringColorButton;
269: this .removeButton = removeButton;
270:
271: colorTextField.setText(color);
272: stringColorButton.setColor(color);
273:
274: stringColorButton.addActionListener(new ActionListener() {
275: public void actionPerformed(@NotNull
276: ActionEvent e) {
277: Color newColor = PaletteColorChooser
278: .showDialog(
279: stringColorButton,
280: TranslationManager
281: .getInstance()
282: .getTranslation("R",
283: "PropertyEditor.Color.Title"),
284: ColorHelper
285: .lookupColor(Row.this .color));
286: if (newColor != null) {
287: Row.this .color = "#"
288: + Integer.toHexString(
289: newColor.getRGB() & 0x00FFFFFF)
290: .toUpperCase();
291: stringColorButton.setColor(Row.this .color);
292: colorTextField.setText(Row.this .color);
293: }
294: }
295: });
296:
297: colorTextField.getDocument().addDocumentListener(
298: new DocumentListener() {
299: public void insertUpdate(@NotNull
300: DocumentEvent e) {
301: update();
302: }
303:
304: public void removeUpdate(@NotNull
305: DocumentEvent e) {
306: update();
307: }
308:
309: public void changedUpdate(@NotNull
310: DocumentEvent e) {
311: update();
312: }
313:
314: private void update() {
315: Row.this .color = colorTextField.getText();
316: stringColorButton.setColor(Row.this .color);
317: }
318: });
319: }
320:
321: public int getCurrentRow() {
322: return currentRow;
323: }
324:
325: public void setCurrentRow(int currentRow) {
326: this .currentRow = currentRow;
327: }
328:
329: @NotNull
330: public JTextField getColorTextField() {
331: return colorTextField;
332: }
333:
334: @NotNull
335: public JLabel getLabel() {
336: return label;
337: }
338:
339: @NotNull
340: public StringColorButton getColorButton() {
341: return stringColorButton;
342: }
343:
344: @NotNull
345: public JButton getRemoveButton() {
346: return removeButton;
347: }
348:
349: }
350:
351: private static class StringColorButton extends JButton {
352:
353: private StringColorButton(@Nullable
354: String stringColor) {
355: setColor(stringColor);
356: }
357:
358: public void setColor(@Nullable
359: String stringColor) {
360: Color color = ColorHelper.lookupColor(stringColor);
361: if (color != null) {
362: setIcon(new ColorIcon(Color.BLACK, color, 20, 16, true));
363: } else {
364: setIcon(new ColorIcon(Color.RED, new Color(0, 0, 0, 0),
365: 20, 16, false));
366: }
367: }
368: }
369: }
|