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.ReportDesignerUtils;
022: import org.pentaho.reportdesigner.crm.report.model.ReportElement;
023: import org.pentaho.reportdesigner.lib.client.components.CenterPanelDialog;
024: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
025: import org.pentaho.reportdesigner.lib.client.util.WindowUtils;
026:
027: import javax.swing.*;
028: import java.awt.event.ActionEvent;
029: import java.awt.event.ActionListener;
030: import java.awt.event.MouseAdapter;
031: import java.awt.event.MouseEvent;
032: import java.util.Arrays;
033:
034: /**
035: * User: Martin
036: * Date: 11.01.2006
037: * Time: 11:02:54
038: */
039: public class QueryChooser {
040: @NotNull
041: private static final String[] EMPTY_STRING_ARRAY = new String[0];
042:
043: private QueryChooser() {
044: }
045:
046: @Nullable
047: public static String showQueryChooser(@Nullable
048: ReportElement reportElement, @NotNull
049: JComponent parent, @NotNull
050: String title, @Nullable
051: String value) {
052: final CenterPanelDialog centerPanelDialog = CenterPanelDialog
053: .createDialog(parent, title,
054: CenterPanelDialog.ModalityType.DOCUMENT_MODAL);
055:
056: @NonNls
057: final FormLayout formLayout = new FormLayout(
058: "4dlu, fill:default:grow, 4dlu",
059: "4dlu, fill:default:grow, 4dlu");
060: final JPanel centerPanel = new JPanel(formLayout);
061:
062: final CellConstraints cc = new CellConstraints();
063:
064: String[] queries = EMPTY_STRING_ARRAY;
065: if (reportElement != null) {
066: queries = ReportDesignerUtils
067: .getPossibleQueriesForSubReport(reportElement
068: .getReport());
069: }
070: Arrays.sort(queries, String.CASE_INSENSITIVE_ORDER);
071:
072: JList list = new JList(queries);
073: list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
074:
075: centerPanel.add(new JScrollPane(list), cc.xy(2, 2));
076:
077: final boolean[] action = new boolean[] { false };
078:
079: JButton okButton = new JButton(TranslationManager.getInstance()
080: .getTranslation("R", "Button.ok"));
081: okButton.addActionListener(new ActionListener() {
082: public void actionPerformed(@NotNull
083: ActionEvent e) {
084: action[0] = true;
085: centerPanelDialog.dispose();
086: }
087: });
088:
089: JButton cancelButton = new JButton(TranslationManager
090: .getInstance().getTranslation("R", "Button.cancel"));
091: cancelButton.addActionListener(new ActionListener() {
092: public void actionPerformed(@NotNull
093: ActionEvent e) {
094: action[0] = false;
095: centerPanelDialog.dispose();
096: }
097: });
098:
099: list.addMouseListener(new MouseAdapter() {
100: public void mouseClicked(@NotNull
101: MouseEvent e) {
102: if (e.getClickCount() > 1) {
103: action[0] = true;
104: centerPanelDialog.dispose();
105: }
106: }
107: });
108:
109: for (int i = 0; i < queries.length; i++) {
110: String query = queries[i];
111: if (query.equalsIgnoreCase(value)) {
112: list.setSelectedIndex(i);
113: break;
114: }
115: }
116:
117: centerPanelDialog.setButtons(okButton, cancelButton, okButton,
118: cancelButton);
119:
120: centerPanelDialog.setCenterPanel(centerPanel);
121: centerPanelDialog.pack();
122: centerPanelDialog.setSize(300, 300);
123: WindowUtils.setLocationRelativeTo(centerPanelDialog, parent);
124: centerPanelDialog.setVisible(true);
125:
126: if (action[0]) {
127: String query = (String) list.getSelectedValue();
128: if (query != null) {
129: return query;
130: }
131: }
132: return value;
133: }
134:
135: }
|