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.model.ReportGroup;
023: import org.pentaho.reportdesigner.crm.report.reportelementinfo.ReportElementInfo;
024: import org.pentaho.reportdesigner.crm.report.reportelementinfo.ReportElementInfoFactory;
025: import org.pentaho.reportdesigner.crm.report.util.ReportGroupUtilities;
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.LinkedHashSet;
037:
038: /**
039: * User: Martin
040: * Date: 11.01.2006
041: * Time: 11:02:54
042: */
043: public class ReportGroupChooser {
044: private ReportGroupChooser() {
045: }
046:
047: @Nullable
048: public static String showReportGroupChooser(@Nullable
049: ReportElement reportElement, @NotNull
050: JComponent parent, @NotNull
051: String title, @Nullable
052: String value) {
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, fill:default:grow, 4dlu",
060: "4dlu, fill:default:grow, 4dlu");
061: final JPanel centerPanel = new JPanel(formLayout);
062:
063: final CellConstraints cc = new CellConstraints();
064:
065: LinkedHashSet<ReportGroup> hashSet = new LinkedHashSet<ReportGroup>();
066: if (reportElement != null) {
067: hashSet = ReportGroupUtilities
068: .getReportGroups(reportElement);
069: }
070: ReportGroup[] reportGroups = hashSet
071: .toArray(new ReportGroup[hashSet.size()]);
072:
073: JList list = new JList(reportGroups);
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 ReportGroup) {
083: ReportGroup reportGroup = (ReportGroup) value;
084: JLabel label = (JLabel) super
085: .getListCellRendererComponent(list,
086: reportGroup.getName(), index,
087: isSelected, cellHasFocus);
088:
089: ReportElementInfo info = ReportElementInfoFactory
090: .getInstance().getReportElementInfo(
091: reportGroup);
092: label.setIcon(info.getIcon());
093:
094: return label;
095: }
096: return super .getListCellRendererComponent(list, value,
097: index, isSelected, cellHasFocus);
098: }
099: });
100:
101: centerPanel.add(new JScrollPane(list), cc.xy(2, 2));
102:
103: final boolean[] action = new boolean[] { false };
104:
105: JButton okButton = new JButton(TranslationManager.getInstance()
106: .getTranslation("R", "Button.ok"));
107: okButton.addActionListener(new ActionListener() {
108: public void actionPerformed(@NotNull
109: ActionEvent e) {
110: action[0] = true;
111: centerPanelDialog.dispose();
112: }
113: });
114:
115: JButton cancelButton = new JButton(TranslationManager
116: .getInstance().getTranslation("R", "Button.cancel"));
117: cancelButton.addActionListener(new ActionListener() {
118: public void actionPerformed(@NotNull
119: ActionEvent e) {
120: action[0] = false;
121: centerPanelDialog.dispose();
122: }
123: });
124:
125: list.addMouseListener(new MouseAdapter() {
126: public void mouseClicked(@NotNull
127: MouseEvent e) {
128: if (e.getClickCount() > 1) {
129: action[0] = true;
130: centerPanelDialog.dispose();
131: }
132: }
133: });
134:
135: for (int i = 0; i < reportGroups.length; i++) {
136: ReportGroup reportGroup = reportGroups[i];
137: if (reportGroup.getName().equalsIgnoreCase(value)) {
138: list.setSelectedIndex(i);
139: break;
140: }
141: }
142:
143: centerPanelDialog.setButtons(okButton, cancelButton, okButton,
144: cancelButton);
145:
146: centerPanelDialog.setCenterPanel(centerPanel);
147: centerPanelDialog.pack();
148: centerPanelDialog.setSize(300, 300);
149: WindowUtils.setLocationRelativeTo(centerPanelDialog, parent);
150: centerPanelDialog.setVisible(true);
151:
152: if (action[0]) {
153: ReportGroup reportGroup = (ReportGroup) list
154: .getSelectedValue();
155: if (reportGroup != null) {
156: return reportGroup.getName();
157: }
158: }
159: return value;
160: }
161:
162: }
|