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 org.jetbrains.annotations.NotNull;
017: import org.jetbrains.annotations.Nullable;
018: import org.pentaho.reportdesigner.lib.client.components.CenterPanelDialog;
019: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
020: import org.pentaho.reportdesigner.lib.client.util.GUIUtils;
021: import org.pentaho.reportdesigner.lib.client.util.WindowUtils;
022:
023: import javax.swing.*;
024: import javax.swing.event.ListSelectionEvent;
025: import javax.swing.event.ListSelectionListener;
026: import java.awt.*;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029: import java.awt.event.MouseAdapter;
030: import java.awt.event.MouseEvent;
031:
032: /**
033: * User: Martin
034: * Date: 05.01.2007
035: * Time: 10:42:27
036: */
037: public class ArrayElementChooser {
038: private ArrayElementChooser() {
039: }
040:
041: @Nullable
042: public static <T> T showOneElementChooser(@NotNull
043: JComponent parent, @NotNull
044: T[] ts, @NotNull
045: String title) {
046: final CenterPanelDialog centerPanelDialog = CenterPanelDialog
047: .createDialog(parent, title,
048: CenterPanelDialog.ModalityType.DOCUMENT_MODAL);
049:
050: final boolean[] action = new boolean[] { false };
051:
052: final JButton okButton = new JButton(TranslationManager
053: .getInstance().getTranslation("R", "Button.ok"));
054: okButton.addActionListener(new ActionListener() {
055: public void actionPerformed(@NotNull
056: ActionEvent e) {
057: action[0] = true;
058: centerPanelDialog.dispose();
059: }
060: });
061:
062: JButton cancelButton = new JButton(TranslationManager
063: .getInstance().getTranslation("R", "Button.cancel"));
064: cancelButton.addActionListener(new ActionListener() {
065: public void actionPerformed(@NotNull
066: ActionEvent e) {
067: action[0] = false;
068: centerPanelDialog.dispose();
069: }
070: });
071:
072: centerPanelDialog.setButtons(okButton, cancelButton, okButton,
073: cancelButton);
074:
075: okButton.setEnabled(false);
076:
077: final JList list = new JList(ts);
078: JPanel centerPanel = new JPanel(new BorderLayout());
079: centerPanel.add(new JScrollPane(list), BorderLayout.CENTER);
080:
081: list.addListSelectionListener(new ListSelectionListener() {
082: public void valueChanged(@NotNull
083: ListSelectionEvent e) {
084: if (!e.getValueIsAdjusting()) {
085: okButton.setEnabled(list.getSelectedIndex() != -1);
086: }
087: }
088: });
089:
090: list.addMouseListener(new MouseAdapter() {
091: public void mouseClicked(@NotNull
092: MouseEvent e) {
093: if (e.getClickCount() == 2
094: && list.locationToIndex(e.getPoint()) != -1) {
095: okButton.doClick();
096: }
097: }
098: });
099:
100: centerPanelDialog.setCenterPanel(centerPanel);
101: centerPanelDialog.pack();
102: GUIUtils.ensureMinimumDialogSize(centerPanelDialog, 200, 300);
103: WindowUtils.setLocationRelativeTo(centerPanelDialog, parent);
104: centerPanelDialog.setVisible(true);
105:
106: if (action[0]) {
107: //noinspection unchecked
108: return (T) list.getSelectedValue();
109: }
110: return null;
111: }
112:
113: }
|