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.lib.client.components;
015:
016: import org.jetbrains.annotations.NonNls;
017: import org.jetbrains.annotations.NotNull;
018: import org.pentaho.reportdesigner.lib.client.commands.KeyStrokeUtil;
019: import org.pentaho.reportdesigner.lib.client.i18n.TranslationManager;
020: import org.pentaho.reportdesigner.lib.client.util.UndoHelper;
021:
022: import javax.swing.*;
023: import java.awt.*;
024: import java.awt.event.ActionEvent;
025: import java.awt.event.InputEvent;
026:
027: /**
028: * User: Martin
029: * Date: 10.08.2006
030: * Time: 16:28:10
031: */
032: public class ComponentFactory {
033: private ComponentFactory() {
034: }
035:
036: @NotNull
037: public static JLabel createLabel(@NotNull
038: @NonNls
039: String prefix, @NotNull
040: @NonNls
041: String key, @NotNull
042: Component componentToLabel) {
043: JLabel label = createLabel(prefix, key);
044: label.setLabelFor(componentToLabel);
045:
046: if (componentToLabel instanceof JButton
047: && label.getDisplayedMnemonic() != -1) {
048: final JButton button = (JButton) componentToLabel;
049: int displayedMnemonic = label.getDisplayedMnemonic();
050:
051: String customeMnemonicHandlerKey = "customMnemonicHandler";//NON-NLS
052: label.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
053: KeyStrokeUtil.getKeyStroke(displayedMnemonic,
054: InputEvent.ALT_MASK),
055: customeMnemonicHandlerKey);
056: label.getActionMap().put(customeMnemonicHandlerKey,
057: new AbstractAction() {
058: public void actionPerformed(@NotNull
059: ActionEvent e) {
060: button.doClick();
061: }
062: });
063: }
064:
065: return label;
066: }
067:
068: @NotNull
069: public static JLabel createLabel(@NotNull
070: @NonNls
071: String prefix, @NotNull
072: @NonNls
073: String key) {
074: JLabel label = new JLabel(TranslationManager.getInstance()
075: .getTranslation(prefix, key));
076: label.setDisplayedMnemonic(TranslationManager.getInstance()
077: .getMnemonic(prefix, key));
078: label.setDisplayedMnemonicIndex(TranslationManager
079: .getInstance().getDisplayedMnemonicIndex(prefix, key));
080: return label;
081: }
082:
083: @NotNull
084: public static JButton createButton(@NotNull
085: @NonNls
086: String prefix, @NotNull
087: @NonNls
088: String key) {
089: JButton button = new JButton(TranslationManager.getInstance()
090: .getTranslation(prefix, key));
091: button.setMnemonic(TranslationManager.getInstance()
092: .getMnemonic(prefix, key));
093: button.setDisplayedMnemonicIndex(TranslationManager
094: .getInstance().getDisplayedMnemonicIndex(prefix, key));
095: return button;
096: }
097:
098: @NotNull
099: public static JToggleButton createToggleButton(@NotNull
100: @NonNls
101: String prefix, @NotNull
102: @NonNls
103: String key) {
104: JToggleButton button = new JToggleButton(TranslationManager
105: .getInstance().getTranslation(prefix, key));
106: button.setMnemonic(TranslationManager.getInstance()
107: .getMnemonic(prefix, key));
108: button.setDisplayedMnemonicIndex(TranslationManager
109: .getInstance().getDisplayedMnemonicIndex(prefix, key));
110: return button;
111: }
112:
113: @NotNull
114: public static JCheckBox createCheckBox(@NotNull
115: @NonNls
116: String prefix, @NotNull
117: @NonNls
118: String key) {
119: JCheckBox checkBox = new JCheckBox(TranslationManager
120: .getInstance().getTranslation(prefix, key));
121: checkBox.setMnemonic(TranslationManager.getInstance()
122: .getMnemonic(prefix, key));
123: checkBox.setDisplayedMnemonicIndex(TranslationManager
124: .getInstance().getDisplayedMnemonicIndex(prefix, key));
125: return checkBox;
126: }
127:
128: @NotNull
129: public static JRadioButton createRadioButton(@NotNull
130: @NonNls
131: String prefix, @NotNull
132: @NonNls
133: String key) {
134: JRadioButton radioButton = new JRadioButton(TranslationManager
135: .getInstance().getTranslation(prefix, key));
136: radioButton.setMnemonic(TranslationManager.getInstance()
137: .getMnemonic(prefix, key));
138: radioButton.setDisplayedMnemonicIndex(TranslationManager
139: .getInstance().getDisplayedMnemonicIndex(prefix, key));
140: return radioButton;
141: }
142:
143: @NotNull
144: public static JTextField createTextField(boolean supportUndo,
145: boolean installPopupMenu) {
146: JTextField textField = new JTextField();
147: if (supportUndo) {
148: UndoHelper.installUndoSupport(textField);
149: }
150:
151: if (installPopupMenu) {
152: TextComponentHelper.installDefaultPopupMenu(textField);
153: }
154:
155: return textField;
156: }
157:
158: @NotNull
159: public static JTextArea createTextArea(boolean supportUndo,
160: boolean installPopupMenu) {
161: JTextArea textArea = new JTextArea();
162: if (supportUndo) {
163: UndoHelper.installUndoSupport(textArea);
164: }
165:
166: if (installPopupMenu) {
167: TextComponentHelper.installDefaultPopupMenu(textArea);
168: }
169:
170: return textArea;
171: }
172: }
|