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.NonNls;
017: import org.jetbrains.annotations.NotNull;
018: import org.jetbrains.annotations.Nullable;
019: import org.pentaho.reportdesigner.lib.client.commands.KeyStrokeUtil;
020: import org.pentaho.reportdesigner.lib.client.components.TextComponentHelper;
021: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
022: import org.pentaho.reportdesigner.lib.client.util.UndoHelper;
023:
024: import javax.swing.*;
025: import java.awt.*;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.KeyEvent;
028: import java.util.logging.Level;
029: import java.util.logging.Logger;
030:
031: /**
032: * User: Martin
033: * Date: 24.10.2005
034: * Time: 09:23:33
035: */
036: public abstract class CellEditorJTextFieldWithEllipsis<T> extends
037: JPanel {
038: @NonNls
039: @NotNull
040: private static final Logger LOG = Logger
041: .getLogger(CellEditorJTextFieldWithEllipsisPoint2D.class
042: .getName());
043:
044: @NotNull
045: protected FocusTextField textField;
046: @NotNull
047: protected JButton ellipsisButton;
048: @Nullable
049: protected T origValue;
050:
051: private boolean nullable;
052:
053: public CellEditorJTextFieldWithEllipsis() {
054: setLayout(new BorderLayout());
055:
056: ellipsisButton = new JButton("...");
057: ellipsisButton.setDefaultCapable(false);
058: ellipsisButton.setMargin(new Insets(0, 0, 0, 0));
059:
060: textField = new FocusTextField(ellipsisButton);
061: UndoHelper.installUndoSupport(textField);
062: TextComponentHelper.installDefaultPopupMenu(textField);
063: textField.setBorder(BorderFactory.createEmptyBorder());
064:
065: add(textField, BorderLayout.CENTER);
066: add(ellipsisButton, BorderLayout.EAST);
067:
068: nullable = false;
069: }
070:
071: public boolean isNullable() {
072: return nullable;
073: }
074:
075: public void setNullable(boolean nullable) {
076: this .nullable = nullable;
077: }
078:
079: public void requestFocus() {
080: textField.requestFocus();
081: }
082:
083: protected boolean processKeyBinding(@NotNull
084: KeyStroke ks, @NotNull
085: KeyEvent e, int condition, boolean pressed) {
086: return textField.processKeyBinding(ks, e, condition, pressed);
087: }
088:
089: @NotNull
090: public JTextField getTextField() {
091: return textField;
092: }
093:
094: @NotNull
095: public JButton getEllipsisButton() {
096: return ellipsisButton;
097: }
098:
099: public void setValue(@Nullable
100: T value, boolean commit) {
101: textField.setText(convertToText(value));
102:
103: if (commit) {
104: origValue = value;
105: }
106: }
107:
108: @Nullable
109: public T getValue() {
110: try {
111: if (nullable && textField.getText().length() == 0) {
112: return null;
113: }
114: return convertFromText(textField.getText());
115: } catch (IllegalArgumentException e) {
116: if (LOG.isLoggable(Level.FINE))
117: LOG
118: .log(
119: Level.FINE,
120: "CellEditorJTextFieldWithEllipsis.getValue ",
121: e);
122: }
123:
124: return origValue;
125: }
126:
127: @Nullable
128: public abstract T convertFromText(@NotNull
129: String text);
130:
131: @NotNull
132: public abstract String convertToText(@Nullable
133: T obj);
134:
135: private static class FocusTextField extends JTextField {
136: @NotNull
137: private static final String POPUP_EDITOR = "popupEditor";
138:
139: private FocusTextField(@NotNull
140: final JButton button) {
141: getInputMap()
142: .put(
143: KeyStrokeUtil
144: .getKeyStroke(TranslationManager
145: .getInstance()
146: .getTranslation("R",
147: "CellEditorJTextFieldWithEllipsis.PopupEditor.Accelerator")),
148: POPUP_EDITOR);
149:
150: getActionMap().put(POPUP_EDITOR, new AbstractAction() {
151: public void actionPerformed(@NotNull
152: ActionEvent e) {
153: button.doClick();
154: }
155: });
156: }
157:
158: @SuppressWarnings({"EmptyMethod"})
159: public boolean processKeyBinding(@NotNull
160: KeyStroke ks, @NotNull
161: KeyEvent e, int condition, boolean pressed) {
162: return super.processKeyBinding(ks, e, condition, pressed);
163: }
164: }
165: }
|