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.ReportElement;
022: import org.pentaho.reportdesigner.crm.report.reportelementinfo.ReportElementInfo;
023: import org.pentaho.reportdesigner.crm.report.reportelementinfo.ReportElementInfoFactory;
024: import org.pentaho.reportdesigner.crm.report.util.FieldDefinition;
025: import org.pentaho.reportdesigner.crm.report.util.ReportFieldUtilities;
026: import org.pentaho.reportdesigner.lib.client.components.CenterPanelDialog;
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 java.awt.*;
032: import java.awt.event.ActionEvent;
033: import java.awt.event.ActionListener;
034: import java.awt.event.MouseAdapter;
035: import java.awt.event.MouseEvent;
036: import java.util.Arrays;
037: import java.util.Comparator;
038: import java.util.HashSet;
039:
040: /**
041: * User: Martin
042: * Date: 11.01.2006
043: * Time: 11:02:54
044: */
045: public class FieldChooser {
046: private FieldChooser() {
047: }
048:
049: @Nullable
050: public static String showFieldChooser(@Nullable
051: ReportElement reportElement, @NotNull
052: JComponent parent, @NotNull
053: String title, @Nullable
054: String value) {
055: final CenterPanelDialog centerPanelDialog = CenterPanelDialog
056: .createDialog(parent, title,
057: CenterPanelDialog.ModalityType.DOCUMENT_MODAL);
058:
059: @NonNls
060: final FormLayout formLayout = new FormLayout(
061: "4dlu, fill:default:grow, 4dlu",
062: "4dlu, fill:default:grow, 4dlu");
063: final JPanel centerPanel = new JPanel(formLayout);
064:
065: final CellConstraints cc = new CellConstraints();
066:
067: HashSet<FieldDefinition> hashSet = new HashSet<FieldDefinition>();
068: if (reportElement != null) {
069: hashSet = ReportFieldUtilities
070: .getFieldDefinitions(reportElement);
071: }
072: FieldDefinition[] fieldDefinitions = hashSet
073: .toArray(new FieldDefinition[hashSet.size()]);
074: Arrays.sort(fieldDefinitions,
075: new Comparator<FieldDefinition>() {
076: public int compare(@NotNull
077: FieldDefinition o1, @NotNull
078: FieldDefinition o2) {
079: return o1.getField().compareToIgnoreCase(
080: o2.getField());
081: }
082: });
083:
084: JList list = new JList(fieldDefinitions);
085: list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
086:
087: list.setCellRenderer(new DefaultListCellRenderer() {
088: @NotNull
089: public Component getListCellRendererComponent(@NotNull
090: JList list, @Nullable
091: Object value, int index, boolean isSelected,
092: boolean cellHasFocus) {
093: if (value instanceof FieldDefinition) {
094: FieldDefinition fieldDefinition = (FieldDefinition) value;
095: JLabel label = (JLabel) super
096: .getListCellRendererComponent(list,
097: fieldDefinition.getField(), index,
098: isSelected, cellHasFocus);
099:
100: ReportElementInfo info = ReportElementInfoFactory
101: .getInstance().getReportElementInfo(
102: fieldDefinition
103: .getDefiningElement());
104: label.setIcon(info.getIcon());
105:
106: return label;
107: }
108: return super .getListCellRendererComponent(list, value,
109: index, isSelected, cellHasFocus);
110: }
111: });
112:
113: centerPanel.add(new JScrollPane(list), cc.xy(2, 2));
114:
115: final boolean[] action = new boolean[] { false };
116:
117: JButton okButton = new JButton(TranslationManager.getInstance()
118: .getTranslation("R", "Button.ok"));
119: okButton.addActionListener(new ActionListener() {
120: public void actionPerformed(@NotNull
121: ActionEvent e) {
122: action[0] = true;
123: centerPanelDialog.dispose();
124: }
125: });
126:
127: JButton cancelButton = new JButton(TranslationManager
128: .getInstance().getTranslation("R", "Button.cancel"));
129: cancelButton.addActionListener(new ActionListener() {
130: public void actionPerformed(@NotNull
131: ActionEvent e) {
132: action[0] = false;
133: centerPanelDialog.dispose();
134: }
135: });
136:
137: list.addMouseListener(new MouseAdapter() {
138: public void mouseClicked(@NotNull
139: MouseEvent e) {
140: if (e.getClickCount() > 1) {
141: action[0] = true;
142: centerPanelDialog.dispose();
143: }
144: }
145: });
146:
147: for (int i = 0; i < fieldDefinitions.length; i++) {
148: FieldDefinition fieldDefinition = fieldDefinitions[i];
149: if (fieldDefinition.getField().equalsIgnoreCase(value)) {
150: list.setSelectedIndex(i);
151: break;
152: }
153: }
154:
155: centerPanelDialog.setButtons(okButton, cancelButton, okButton,
156: cancelButton);
157:
158: centerPanelDialog.setCenterPanel(centerPanel);
159: centerPanelDialog.pack();
160: centerPanelDialog.setSize(300, 300);
161: WindowUtils.setLocationRelativeTo(centerPanelDialog, parent);
162: centerPanelDialog.setVisible(true);
163:
164: if (action[0]) {
165: FieldDefinition fieldDefinition = (FieldDefinition) list
166: .getSelectedValue();
167: if (fieldDefinition != null) {
168: return fieldDefinition.getField();
169: }
170: }
171: return value;
172: }
173:
174: }
|