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.ColorIcon;
024: import org.pentaho.reportdesigner.lib.client.components.CenterPanelDialog;
025: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
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:
034: /**
035: * User: Martin
036: * Date: 11.01.2006
037: * Time: 11:02:54
038: */
039: public class ColorArrayChooser {
040: private ColorArrayChooser() {
041: }
042:
043: @Nullable
044: public static Color[] showColorArrayChooser(@NotNull
045: JComponent parent, @NotNull
046: String title, @NotNull
047: final String labelPrefix, @Nullable
048: Color[] colorArray) {
049: final CenterPanelDialog centerPanelDialog = CenterPanelDialog
050: .createDialog(parent, title,
051: CenterPanelDialog.ModalityType.DOCUMENT_MODAL);
052:
053: @NonNls
054: final FormLayout formLayout = new FormLayout(
055: "4dlu, pref, 4dlu, fill:default:grow, 4dlu, pref, 4dlu",
056: "4dlu");
057: final JPanel centerPanel = new JPanel(formLayout);
058:
059: @NonNls
060: FormLayout fl = new FormLayout("4dlu, fill:pref:grow, 4dlu",
061: "4dlu, fill:default, 4dlu, pref, 4dlu:grow");
062: @NonNls
063: final CellConstraints cc = new CellConstraints();
064: final JPanel panel = new JPanel(fl);
065: JScrollPane scrollPane = new JScrollPane(centerPanel);
066: scrollPane.setBorder(BorderFactory.createEmptyBorder());
067: panel.add(scrollPane, cc.xy(2, 2));
068:
069: final ArrayList<Row> rows = new ArrayList<ColorArrayChooser.Row>();
070:
071: JButton addButton = new JButton(TranslationManager
072: .getInstance().getTranslation("R",
073: "StringArrayChooser.AddButton"));
074: panel.add(addButton, cc.xy(2, 4, "right, center"));
075:
076: if (colorArray != null) {
077: for (int i = 0; i < colorArray.length; i++) {
078: Color color = colorArray[i];
079: JLabel label = new JLabel(labelPrefix + "[" + i + "]");
080: ColorButton colorButton = new ColorButton(color);
081:
082: JButton removeButton = new JButton("-");
083: removeButton.setMargin(new Insets(1, 1, 1, 1));
084: final ColorArrayChooser.Row row = new ColorArrayChooser.Row(
085: i, label, colorButton, removeButton);
086: rows.add(row);
087: removeButton.addActionListener(new ActionListener() {
088: public void actionPerformed(@NotNull
089: ActionEvent e) {
090: rows.remove(row);
091: rebuildPanel(rows, labelPrefix, centerPanel);
092:
093: panel.revalidate();
094: panel.repaint();
095: }
096: });
097: }
098:
099: rebuildPanel(rows, labelPrefix, centerPanel);
100:
101: panel.revalidate();
102: panel.repaint();
103: }
104:
105: addButton.addActionListener(new ActionListener() {
106: public void actionPerformed(@NotNull
107: ActionEvent e) {
108: addRow(labelPrefix, rows, centerPanel, panel);
109: }
110: });
111:
112: if (colorArray == null || colorArray.length == 0) {
113: addRow(labelPrefix, rows, centerPanel, panel);
114: }
115:
116: final boolean[] action = new boolean[] { false };
117:
118: JButton okButton = new JButton(TranslationManager.getInstance()
119: .getTranslation("R", "Button.ok"));
120: okButton.addActionListener(new ActionListener() {
121: public void actionPerformed(@NotNull
122: ActionEvent e) {
123: action[0] = true;
124: centerPanelDialog.dispose();
125: }
126: });
127:
128: JButton cancelButton = new JButton(TranslationManager
129: .getInstance().getTranslation("R", "Button.cancel"));
130: cancelButton.addActionListener(new ActionListener() {
131: public void actionPerformed(@NotNull
132: ActionEvent e) {
133: action[0] = false;
134: centerPanelDialog.dispose();
135: }
136: });
137:
138: centerPanelDialog.setButtons(okButton, cancelButton, okButton,
139: cancelButton);
140:
141: centerPanelDialog.setCenterPanel(panel);
142: centerPanelDialog.pack();
143: centerPanelDialog.setSize(300, 300);
144: WindowUtils.setLocationRelativeTo(centerPanelDialog, parent);
145: centerPanelDialog.setVisible(true);
146:
147: if (action[0]) {
148: return getValues(rows);
149: }
150: return colorArray;
151: }
152:
153: private static void addRow(@NotNull
154: final String labelPrefix, @NotNull
155: final ArrayList<ColorArrayChooser.Row> rows, @NotNull
156: final JPanel centerPanel, @NotNull
157: final JPanel panel) {
158: JLabel label = new JLabel(labelPrefix);
159: ColorButton colorButton = new ColorButton(Color.WHITE);
160:
161: JButton removeButton = new JButton("-");
162: removeButton.setMargin(new Insets(1, 1, 1, 1));
163:
164: final ColorArrayChooser.Row row = new ColorArrayChooser.Row(
165: rows.size(), label, colorButton, removeButton);
166: rows.add(row);
167:
168: removeButton.addActionListener(new ActionListener() {
169: public void actionPerformed(@NotNull
170: ActionEvent e) {
171: rows.remove(row);
172: rebuildPanel(rows, labelPrefix, centerPanel);
173:
174: panel.revalidate();
175: panel.repaint();
176: }
177: });
178: rebuildPanel(rows, labelPrefix, centerPanel);
179:
180: colorButton.requestFocusInWindow();
181:
182: panel.revalidate();
183: panel.repaint();
184: }
185:
186: @NotNull
187: private static Color[] getValues(@NotNull
188: ArrayList<ColorArrayChooser.Row> rows) {
189: Color[] array = new Color[rows.size()];
190: for (int i = 0; i < rows.size(); i++) {
191: ColorArrayChooser.Row row = rows.get(i);
192: array[i] = row.getColorButton().getColor();
193: }
194:
195: return array;
196: }
197:
198: private static void rebuildPanel(@NotNull
199: ArrayList<ColorArrayChooser.Row> rows, @NotNull
200: String labelPrefix, @NotNull
201: JPanel panel) {
202: panel.removeAll();
203:
204: @NonNls
205: FormLayout formLayout = new FormLayout(
206: "4dlu, pref, 4dlu, fill:default:grow, 4dlu, pref, 4dlu",
207: "4dlu");
208: @NonNls
209: CellConstraints cc = new CellConstraints();
210:
211: panel.setLayout(formLayout);
212:
213: int currentRow = 1;
214: for (int i = 0; i < rows.size(); i++) {
215: ColorArrayChooser.Row row = rows.get(i);
216:
217: formLayout.appendRow(new RowSpec("pref"));//NON-NLS
218: currentRow++;
219: row.setCurrentRow(i);
220: row.getLabel().setText(labelPrefix + "[" + i + "]");
221: panel.add(row.getLabel(), cc.xy(2, currentRow));
222: panel.add(row.getColorButton(), cc.xy(4, currentRow));
223: panel.add(row.getRemoveButton(), cc.xy(6, currentRow));
224: formLayout.appendRow(new RowSpec("4dlu"));//NON-NLS
225: currentRow++;
226: }
227:
228: panel.revalidate();
229: panel.repaint();
230: }
231:
232: private static class Row {
233: private int currentRow;
234: @NotNull
235: private JLabel label;
236: @NotNull
237: private ColorButton colorButton;
238: @NotNull
239: private JButton removeButton;
240:
241: private Row(int currentRow, @NotNull
242: JLabel label, @NotNull
243: ColorButton colorButton, @NotNull
244: JButton removeButton) {
245: this .currentRow = currentRow;
246: this .label = label;
247: this .colorButton = colorButton;
248: this .removeButton = removeButton;
249: }
250:
251: public int getCurrentRow() {
252: return currentRow;
253: }
254:
255: public void setCurrentRow(int currentRow) {
256: this .currentRow = currentRow;
257: }
258:
259: @NotNull
260: public JLabel getLabel() {
261: return label;
262: }
263:
264: @NotNull
265: public ColorButton getColorButton() {
266: return colorButton;
267: }
268:
269: @NotNull
270: public JButton getRemoveButton() {
271: return removeButton;
272: }
273:
274: }
275:
276: private static class ColorButton extends JButton {
277: @NotNull
278: private Color color;
279:
280: private ColorButton(@NotNull
281: Color color) {
282: //noinspection ConstantConditions
283: if (color == null) {
284: throw new IllegalArgumentException(
285: "color must not be null");
286: }
287:
288: this .color = color;
289: setColor(color);
290:
291: addActionListener(new ActionListener() {
292: public void actionPerformed(@NotNull
293: ActionEvent e) {
294: Color newColor = PaletteColorChooser
295: .showDialog(
296: ColorButton.this ,
297: TranslationManager
298: .getInstance()
299: .getTranslation("R",
300: "PropertyEditor.Color.Title"),
301: ColorButton.this .color);
302: if (newColor != null) {
303: setColor(newColor);
304: }
305: }
306: });
307: }
308:
309: @NotNull
310: public Color getColor() {
311: return color;
312: }
313:
314: public void setColor(@NotNull
315: Color color) {
316: this .color = color;
317: setIcon(new ColorIcon(Color.BLACK, color, 100, 16, true));
318: }
319: }
320: }
|