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.crm.report.model.SubReportParameter;
022: import org.pentaho.reportdesigner.crm.report.model.SubReportParameters;
023: import org.pentaho.reportdesigner.crm.report.properties.PropertyTable;
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.GUIUtils;
027: import org.pentaho.reportdesigner.lib.client.util.WindowUtils;
028:
029: import javax.swing.*;
030: import javax.swing.event.ListSelectionEvent;
031: import javax.swing.event.ListSelectionListener;
032: import javax.swing.table.AbstractTableModel;
033: import java.awt.event.ActionEvent;
034: import java.awt.event.ActionListener;
035: import java.util.ArrayList;
036: import java.util.Arrays;
037:
038: /**
039: * User: Martin
040: * Date: 01.11.2005
041: * Time: 10:23:10
042: */
043: public class SubReportParametersChooser {
044:
045: private SubReportParametersChooser() {
046: }
047:
048: @NotNull
049: public static SubReportParameters showSubReportParametersChooser(
050: @NotNull
051: PropertyTable parent, @NotNull
052: String title, @NotNull
053: SubReportParameters origParameters) {
054: final CenterPanelDialog centerPanelDialog = CenterPanelDialog
055: .createDialog(parent, title,
056: CenterPanelDialog.ModalityType.DOCUMENT_MODAL);
057:
058: final boolean[] action = new boolean[] { false };
059:
060: JButton okButton = new JButton(TranslationManager.getInstance()
061: .getTranslation("R", "Button.ok"));
062: okButton.addActionListener(new ActionListener() {
063: public void actionPerformed(@NotNull
064: ActionEvent e) {
065: action[0] = true;
066: centerPanelDialog.dispose();
067: }
068: });
069:
070: JButton cancelButton = new JButton(TranslationManager
071: .getInstance().getTranslation("R", "Button.cancel"));
072: cancelButton.addActionListener(new ActionListener() {
073: public void actionPerformed(@NotNull
074: ActionEvent e) {
075: action[0] = false;
076: centerPanelDialog.dispose();
077: }
078: });
079:
080: centerPanelDialog.setButtons(okButton, cancelButton, okButton,
081: cancelButton);
082:
083: @NonNls
084: FormLayout formLayout = new FormLayout(
085: "4dlu, fill:default:grow, 4dlu", "4dlu, " + "pref, "
086: + "4dlu, " + "fill:10dlu:grow, " + "10dlu, "
087: + "pref, " + "4dlu, " + "fill:10dlu:grow,"
088: + "4dlu");
089: @NonNls
090: CellConstraints cc = new CellConstraints();
091:
092: JPanel centerPanel = new JPanel(formLayout);
093:
094: JLabel importLabel = new JLabel(TranslationManager
095: .getInstance().getTranslation("R",
096: "SubReportParameterChooser.ImportParameters"));
097: JLabel exportLabel = new JLabel(TranslationManager
098: .getInstance().getTranslation("R",
099: "SubReportParameterChooser.ExportParameters"));
100: ParameterPanel importParameterPanel = new ParameterPanel(
101: TranslationManager.getInstance().getTranslation("R",
102: "SubReportParameterChooser.GlobaleImport"),
103: origParameters.isGlobalImport(),
104: new ArrayList<SubReportParameter>(origParameters
105: .getImportParameters().values()));
106: ParameterPanel exportParameterPanel = new ParameterPanel(
107: TranslationManager.getInstance().getTranslation("R",
108: "SubReportParameterChooser.GlobaleExport"),
109: origParameters.isGlobalExport(),
110: new ArrayList<SubReportParameter>(origParameters
111: .getExportParameters().values()));
112:
113: centerPanel.add(importLabel, cc.xy(2, 2));
114: centerPanel.add(importParameterPanel, cc.xy(2, 4));
115: centerPanel.add(exportLabel, cc.xy(2, 6));
116: centerPanel.add(exportParameterPanel, cc.xy(2, 8));
117:
118: centerPanelDialog.setCenterPanel(centerPanel);
119:
120: centerPanelDialog.pack();
121: GUIUtils.ensureMinimumDialogSize(centerPanelDialog, 500, 600);
122: WindowUtils.setLocationRelativeTo(centerPanelDialog, parent);
123: centerPanelDialog.setVisible(true);
124:
125: if (action[0]) {
126: return new SubReportParameters(importParameterPanel
127: .isGlobalValue(), importParameterPanel
128: .getSubReportParameters(), exportParameterPanel
129: .isGlobalValue(), exportParameterPanel
130: .getSubReportParameters());
131: }
132:
133: return origParameters;
134: }
135:
136: private static class ParameterPanel extends JPanel {
137: @NotNull
138: private JCheckBox globalValueCheckBox;
139: @NotNull
140: private JTable subReportParametersTable;
141: @NotNull
142: private JButton addEntryButton;
143: @NotNull
144: private JButton removeEntryButton;
145: @NotNull
146: private SubReportParametersTableModel subReportParametersTableModel;
147:
148: private ParameterPanel(@NotNull
149: String globaleLabel, boolean globalValue, @NotNull
150: ArrayList<SubReportParameter> subReportParameters) {
151: @NonNls
152: FormLayout formLayout = new FormLayout(
153: "pref, 4dlu, fill:default:grow, 4dlu, pref, 4dlu, pref",
154: "pref, 4dlu, fill:default:grow, 4dlu, pref");
155:
156: formLayout.setColumnGroups(new int[][] { { 5, 7 } });
157:
158: @NonNls
159: CellConstraints cc = new CellConstraints();
160:
161: setLayout(formLayout);
162:
163: globalValueCheckBox = new JCheckBox(globaleLabel,
164: globalValue);
165: subReportParametersTableModel = new SubReportParametersTableModel(
166: subReportParameters);
167: subReportParametersTable = new JTable(
168: subReportParametersTableModel);
169: addEntryButton = new JButton(TranslationManager
170: .getInstance().getTranslation("R",
171: "ParameterPanel.ButtonAdd"));
172: removeEntryButton = new JButton(TranslationManager
173: .getInstance().getTranslation("R",
174: "ParameterPanel.ButtonRemove"));
175:
176: add(globalValueCheckBox, cc.xyw(1, 1, 7));
177: add(new JScrollPane(subReportParametersTable), cc.xyw(1, 3,
178: 7));
179: add(addEntryButton, cc.xy(5, 5));
180: add(removeEntryButton, cc.xy(7, 5));
181:
182: globalValueCheckBox.addActionListener(new ActionListener() {
183: public void actionPerformed(@NotNull
184: ActionEvent e) {
185: updateState();
186: }
187: });
188:
189: subReportParametersTable.putClientProperty(
190: "terminateEditOnFocusLost", Boolean.TRUE);//NON-NLS
191:
192: subReportParametersTable.getSelectionModel()
193: .addListSelectionListener(
194: new ListSelectionListener() {
195: public void valueChanged(@NotNull
196: ListSelectionEvent e) {
197: updateState();
198: }
199: });
200:
201: addEntryButton.addActionListener(new ActionListener() {
202: public void actionPerformed(@NotNull
203: ActionEvent e) {
204: SubReportParameter subReportParameter = new SubReportParameter(
205: "", "");
206: subReportParametersTableModel
207: .addEntry(subReportParameter);
208: int index = subReportParametersTableModel
209: .getRowCount() - 1;
210: subReportParametersTable.getSelectionModel()
211: .setSelectionInterval(index, index);
212: }
213: });
214:
215: removeEntryButton.addActionListener(new ActionListener() {
216: public void actionPerformed(@NotNull
217: ActionEvent e) {
218: int[] rows = subReportParametersTable
219: .getSelectedRows();
220: Arrays.sort(rows);
221: for (int i = rows.length - 1; i >= 0; i--) {
222: int row = rows[i];
223: subReportParametersTableModel.removeEntry(row);
224: }
225: int rowCount = subReportParametersTable
226: .getRowCount();
227: if (rowCount > 0) {
228: subReportParametersTable.getSelectionModel()
229: .setSelectionInterval(rowCount - 1,
230: rowCount - 1);
231: }
232: }
233: });
234:
235: updateState();
236: }
237:
238: private void updateState() {
239: subReportParametersTable.setEnabled(!globalValueCheckBox
240: .isSelected());
241: addEntryButton
242: .setEnabled(!globalValueCheckBox.isSelected());
243: removeEntryButton.setEnabled(!globalValueCheckBox
244: .isSelected()
245: && subReportParametersTable.getSelectedRow() != -1);
246: }
247:
248: public boolean isGlobalValue() {
249: return globalValueCheckBox.isSelected();
250: }
251:
252: @NotNull
253: public ArrayList<SubReportParameter> getSubReportParameters() {
254: return subReportParametersTableModel
255: .getSubReportParameters();
256: }
257: }
258:
259: private static class SubReportParametersTableModel extends
260: AbstractTableModel {
261: @NotNull
262: private ArrayList<SubReportParameter> subReportParameters;
263:
264: private SubReportParametersTableModel(@NotNull
265: ArrayList<SubReportParameter> subReportParameters) {
266: //noinspection ConstantConditions
267: if (subReportParameters == null) {
268: throw new IllegalArgumentException(
269: "subReportParameters must not be null");
270: }
271:
272: this .subReportParameters = subReportParameters;
273: }
274:
275: @NotNull
276: public ArrayList<SubReportParameter> getSubReportParameters() {
277: return subReportParameters;
278: }
279:
280: public boolean isCellEditable(int rowIndex, int columnIndex) {
281: return true;
282: }
283:
284: @NotNull
285: public Class<?> getColumnClass(int columnIndex) {
286: return String.class;
287: }
288:
289: @NotNull
290: public String getColumnName(int columnIndex) {
291: if (columnIndex == 0) {
292: return TranslationManager.getInstance().getTranslation(
293: "R", "SubReportParameter.Key");
294: } else if (columnIndex == 1) {
295: return TranslationManager.getInstance().getTranslation(
296: "R", "SubReportParameter.Value");
297: }
298: return "ERROR";//NON-NLS
299: }
300:
301: public int getColumnCount() {
302: return 2;
303: }
304:
305: public int getRowCount() {
306: return subReportParameters.size();
307: }
308:
309: @Nullable
310: public Object getValueAt(int rowIndex, int columnIndex) {
311: if (columnIndex == 0) {
312: return subReportParameters.get(rowIndex).getKey();
313: } else if (columnIndex == 1) {
314: return subReportParameters.get(rowIndex).getValue();
315: }
316: return null;
317: }
318:
319: public void setValueAt(@NotNull
320: Object aValue, int rowIndex, int columnIndex) {
321: if (columnIndex == 0) {
322: SubReportParameter subReportParameter = subReportParameters
323: .remove(rowIndex);
324: SubReportParameter newParameter = new SubReportParameter(
325: aValue.toString(), subReportParameter
326: .getValue());
327: subReportParameters.add(rowIndex, newParameter);
328: } else if (columnIndex == 1) {
329: SubReportParameter subReportParameter = subReportParameters
330: .remove(rowIndex);
331: SubReportParameter newParameter = new SubReportParameter(
332: subReportParameter.getKey(), aValue.toString());
333: subReportParameters.add(rowIndex, newParameter);
334: }
335: fireTableCellUpdated(rowIndex, columnIndex);
336: }
337:
338: public void addEntry(@NotNull
339: SubReportParameter subReportParameter) {
340: //noinspection ConstantConditions
341: if (subReportParameter == null) {
342: throw new IllegalArgumentException(
343: "subReportParameter must not be null");
344: }
345: int index = subReportParameters.size();
346: subReportParameters.add(subReportParameter);
347: fireTableRowsInserted(index, index);
348: }
349:
350: public void removeEntry(int index) {
351: subReportParameters.remove(index);
352: fireTableRowsDeleted(index, index);
353: }
354: }
355:
356: }
|