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.BandToplevelReportElement;
022: import org.pentaho.reportdesigner.crm.report.model.Report;
023: import org.pentaho.reportdesigner.crm.report.model.ReportElement;
024: import org.pentaho.reportdesigner.crm.report.model.ReportGroup;
025: import org.pentaho.reportdesigner.crm.report.model.dataset.DataSetReportElement;
026: import org.pentaho.reportdesigner.crm.report.model.dataset.DataSetsReportElement;
027: import org.pentaho.reportdesigner.crm.report.reportelementinfo.ReportElementInfo;
028: import org.pentaho.reportdesigner.crm.report.reportelementinfo.ReportElementInfoFactory;
029: import org.pentaho.reportdesigner.crm.report.util.ElementNameDefinition;
030: import org.pentaho.reportdesigner.crm.report.util.ReportElementNameUtilities;
031: import org.pentaho.reportdesigner.lib.client.components.CenterPanelDialog;
032: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
033: import org.pentaho.reportdesigner.lib.client.util.WindowUtils;
034:
035: import javax.swing.*;
036: import java.awt.*;
037: import java.awt.event.ActionEvent;
038: import java.awt.event.ActionListener;
039: import java.awt.event.MouseAdapter;
040: import java.awt.event.MouseEvent;
041: import java.util.Arrays;
042: import java.util.Comparator;
043: import java.util.HashSet;
044: import java.util.Iterator;
045:
046: /**
047: * User: Martin
048: * Date: 11.01.2006
049: * Time: 11:02:54
050: */
051: public class ElementNameChooser {
052: private ElementNameChooser() {
053: }
054:
055: @Nullable
056: public static String showElementChooser(@Nullable
057: final ReportElement reportElement, @NotNull
058: JComponent parent, @NotNull
059: String title, @Nullable
060: final String value) {
061: final CenterPanelDialog centerPanelDialog = CenterPanelDialog
062: .createDialog(parent, title,
063: CenterPanelDialog.ModalityType.DOCUMENT_MODAL);
064:
065: @NonNls
066: final FormLayout formLayout = new FormLayout(
067: "4dlu, fill:default:grow, 4dlu",
068: "4dlu, fill:default:grow, 4dlu, default, 4dlu, default, 4dlu");
069: final JPanel centerPanel = new JPanel(formLayout);
070:
071: final CellConstraints cc = new CellConstraints();
072:
073: final JList list = new JList();
074: list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
075:
076: list.setCellRenderer(new DefaultListCellRenderer() {
077: @NotNull
078: public Component getListCellRendererComponent(@NotNull
079: JList list, @Nullable
080: Object value, int index, boolean isSelected,
081: boolean cellHasFocus) {
082: if (value instanceof ElementNameDefinition) {
083: ElementNameDefinition nameDefinition = (ElementNameDefinition) value;
084: JLabel label = (JLabel) super
085: .getListCellRendererComponent(list,
086: nameDefinition.getElementName(),
087: index, isSelected, cellHasFocus);
088:
089: ReportElementInfo info = ReportElementInfoFactory
090: .getInstance()
091: .getReportElementInfo(
092: nameDefinition.getDefiningElement());
093: label.setIcon(info.getIcon());
094:
095: return label;
096: }
097: return super .getListCellRendererComponent(list, value,
098: index, isSelected, cellHasFocus);
099: }
100: });
101:
102: final JCheckBox hideDefaultElementsCheckBox = new JCheckBox(
103: TranslationManager.getInstance().getTranslation("R",
104: "ElementNameChooser.HideDefaultElements"));
105: final JCheckBox hideSpecialElementsCheckBox = new JCheckBox(
106: TranslationManager.getInstance().getTranslation("R",
107: "ElementNameChooser.HideSpecialElements"));
108:
109: hideDefaultElementsCheckBox.setSelected(true);
110: hideSpecialElementsCheckBox.setSelected(true);
111:
112: refillList(list, reportElement, hideDefaultElementsCheckBox
113: .isSelected(),
114: hideSpecialElementsCheckBox.isSelected(), value);
115:
116: hideSpecialElementsCheckBox
117: .addActionListener(new ActionListener() {
118: public void actionPerformed(@NotNull
119: ActionEvent e) {
120: refillList(list, reportElement,
121: hideDefaultElementsCheckBox
122: .isSelected(),
123: hideSpecialElementsCheckBox
124: .isSelected(), value);
125: }
126: });
127:
128: hideDefaultElementsCheckBox
129: .addActionListener(new ActionListener() {
130: public void actionPerformed(@NotNull
131: ActionEvent e) {
132: refillList(list, reportElement,
133: hideDefaultElementsCheckBox
134: .isSelected(),
135: hideSpecialElementsCheckBox
136: .isSelected(), value);
137: }
138: });
139:
140: centerPanel.add(new JScrollPane(list), cc.xy(2, 2));
141: centerPanel.add(hideDefaultElementsCheckBox, cc.xy(2, 4));
142: centerPanel.add(hideSpecialElementsCheckBox, cc.xy(2, 6));
143:
144: final boolean[] action = new boolean[] { false };
145:
146: JButton okButton = new JButton(TranslationManager.getInstance()
147: .getTranslation("R", "Button.ok"));
148: okButton.addActionListener(new ActionListener() {
149: public void actionPerformed(@NotNull
150: ActionEvent e) {
151: action[0] = true;
152: centerPanelDialog.dispose();
153: }
154: });
155:
156: JButton cancelButton = new JButton(TranslationManager
157: .getInstance().getTranslation("R", "Button.cancel"));
158: cancelButton.addActionListener(new ActionListener() {
159: public void actionPerformed(@NotNull
160: ActionEvent e) {
161: action[0] = false;
162: centerPanelDialog.dispose();
163: }
164: });
165:
166: list.addMouseListener(new MouseAdapter() {
167: public void mouseClicked(@NotNull
168: MouseEvent e) {
169: if (e.getClickCount() > 1) {
170: action[0] = true;
171: centerPanelDialog.dispose();
172: }
173: }
174: });
175:
176: centerPanelDialog.setButtons(okButton, cancelButton, okButton,
177: cancelButton);
178:
179: centerPanelDialog.setCenterPanel(centerPanel);
180: centerPanelDialog.pack();
181: centerPanelDialog.setSize(300, 300);
182: WindowUtils.setLocationRelativeTo(centerPanelDialog, parent);
183: centerPanelDialog.setVisible(true);
184:
185: if (action[0]) {
186: ElementNameDefinition elementNameDefinition = (ElementNameDefinition) list
187: .getSelectedValue();
188: if (elementNameDefinition != null) {
189: return elementNameDefinition.getElementName();
190: }
191: }
192: return value;
193: }
194:
195: private static void refillList(@NotNull
196: JList list, @Nullable
197: ReportElement reportElement, boolean hideDefault,
198: boolean hideSpecial, @Nullable
199: String value) {
200: HashSet<ElementNameDefinition> hashSet = new HashSet<ElementNameDefinition>();
201: if (reportElement != null) {
202: hashSet = ReportElementNameUtilities
203: .getElementNameDefinitions(reportElement);
204: }
205:
206: if (hideDefault) {
207: for (Iterator<ElementNameDefinition> it = hashSet
208: .iterator(); it.hasNext();) {
209: ElementNameDefinition elementNameDefinition = it.next();
210: if (elementNameDefinition.getElementName()
211: .contains("@")) {
212: it.remove();
213: }
214: }
215: }
216:
217: if (hideSpecial) {
218: for (Iterator<ElementNameDefinition> it = hashSet
219: .iterator(); it.hasNext();) {
220: ElementNameDefinition elementNameDefinition = it.next();
221: if (elementNameDefinition.getDefiningElement() instanceof Report) {
222: it.remove();
223: } else if (elementNameDefinition.getDefiningElement() instanceof BandToplevelReportElement) {
224: it.remove();
225: }
226: }
227: }
228:
229: for (Iterator<ElementNameDefinition> it = hashSet.iterator(); it
230: .hasNext();) {
231: ElementNameDefinition elementNameDefinition = it.next();
232: if (elementNameDefinition.getDefiningElement() instanceof DataSetsReportElement) {
233: it.remove();
234: } else if (elementNameDefinition.getDefiningElement() instanceof DataSetReportElement) {
235: it.remove();
236: } else if (elementNameDefinition.getDefiningElement() instanceof ReportGroup) {
237: it.remove();
238: }
239: }
240:
241: final ElementNameDefinition[] elementNameDefinitions = hashSet
242: .toArray(new ElementNameDefinition[hashSet.size()]);
243: Arrays.sort(elementNameDefinitions,
244: new Comparator<ElementNameDefinition>() {
245: public int compare(@NotNull
246: ElementNameDefinition o1, @NotNull
247: ElementNameDefinition o2) {
248: return o1.getElementName().compareToIgnoreCase(
249: o2.getElementName());
250: }
251: });
252:
253: list.setModel(new AbstractListModel() {
254: public int getSize() {
255: return elementNameDefinitions.length;
256: }
257:
258: @NotNull
259: public Object getElementAt(int i) {
260: return elementNameDefinitions[i];
261: }
262: });
263:
264: for (int i = 0; i < elementNameDefinitions.length; i++) {
265: ElementNameDefinition elementNameDefinition = elementNameDefinitions[i];
266: if (elementNameDefinition.getElementName()
267: .equalsIgnoreCase(value)) {
268: list.setSelectedIndex(i);
269: break;
270: }
271: }
272:
273: }
274:
275: }
|