Java tutorial
/* * Copyright (c) 2008-2016 Haulmont. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package com.haulmont.cuba.desktop.gui.components; import com.haulmont.chile.core.datatypes.Datatype; import com.haulmont.cuba.desktop.sys.vcl.Flushable; import com.haulmont.cuba.gui.components.TextArea; import org.apache.commons.lang.StringUtils; import org.jdesktop.swingx.prompt.PromptSupport; import javax.swing.*; import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.Collections; import java.util.Set; import static javax.swing.KeyStroke.getKeyStroke; public class DesktopTextArea extends DesktopAbstractTextField<JTextArea> implements TextArea { protected JComponent composition; protected int rows; protected String inputPrompt; // just stub protected CaseConversion caseConversion; // just stub protected int textChangeTimeout = 0; // just stub protected TextChangeEventMode textChangeEventMode = TextChangeEventMode.LAZY; public DesktopTextArea() { setRows(5); } @Override protected JTextArea createTextComponentImpl() { final JTextArea impl = new TextAreaFlushableField(); if (isTabTraversal()) { Set<KeyStroke> forwardFocusKey = Collections.singleton(getKeyStroke(KeyEvent.VK_TAB, 0)); impl.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, forwardFocusKey); Set<KeyStroke> backwardFocusKey = Collections .singleton(getKeyStroke(KeyEvent.VK_TAB, KeyEvent.SHIFT_MASK)); impl.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, backwardFocusKey); impl.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if (isEnabled() && isEditable() && e.getKeyCode() == KeyEvent.VK_TAB && e.getModifiers() == KeyEvent.CTRL_MASK) { if (StringUtils.isEmpty(impl.getText())) { impl.setText("\t"); } else { impl.append("\t"); } } } }); } impl.setLineWrap(true); impl.setWrapStyleWord(true); int height = (int) impl.getPreferredSize().getHeight(); impl.setMinimumSize(new Dimension(0, height)); composition = new JScrollPane(impl); composition.setPreferredSize(new Dimension(150, height)); composition.setMinimumSize(new Dimension(0, height)); doc.putProperty("filterNewlines", false); return impl; } protected boolean isTabTraversal() { return true; } @Override public JComponent getComposition() { return composition; } @Override protected TextFieldListener createTextListener() { return new TextFieldListener() { @Override public void keyPressed(KeyEvent e) { updateMissingValueState(); } }; } @Override public int getRows() { return this.rows; } @Override public void setRows(int rows) { this.rows = 5; impl.setRows(rows); int minHeight = impl.getPreferredSize().height; int minWidth = impl.getPreferredSize().width; Insets insets = impl.getInsets(); minWidth += insets.left + insets.right; minHeight += insets.bottom + insets.top; composition.setMinimumSize(new Dimension(minWidth, minHeight)); } @Override public void setHeight(String height) { super.setHeight(height); if (getHeight() >= 0) { impl.setRows(1); } else { impl.setRows(this.rows); } int minHeight = impl.getPreferredSize().height; int minWidth = impl.getPreferredSize().width; Insets insets = impl.getInsets(); minWidth += insets.left + insets.right; minHeight += insets.bottom + insets.top; composition.setMinimumSize(new Dimension(minWidth, minHeight)); } @Override public int getColumns() { return impl.getColumns(); } @Override public void setColumns(int columns) { impl.setColumns(columns); if (columns > 1) { int minHeight = impl.getPreferredSize().height; int minWidth = impl.getPreferredSize().width; Insets insets = impl.getInsets(); minWidth += insets.left + insets.right; minHeight += insets.bottom + insets.top; composition.setMinimumSize(new Dimension(minWidth, minHeight)); } else { int minHeight = impl.getPreferredSize().height; Insets insets = impl.getInsets(); minHeight += insets.bottom + insets.top; composition.setMinimumSize(new Dimension(0, minHeight)); } } @Override public boolean isWordwrap() { return impl.getLineWrap(); } @Override public void setWordwrap(boolean wordwrap) { impl.setLineWrap(wordwrap); } @Override public int getMaxLength() { return ((TextComponentDocument) doc).getMaxLength(); } @Override public void setMaxLength(int value) { ((TextComponentDocument) doc).setMaxLength(value); } @Override public boolean isTrimming() { return trimming; } @Override public void setTrimming(boolean trimming) { this.trimming = trimming; } @Override public void setCursorPosition(int position) { impl.setSelectionStart(position); impl.setSelectionEnd(position); } @Override public String getInputPrompt() { return inputPrompt; } @Override public void setInputPrompt(String inputPrompt) { this.inputPrompt = inputPrompt; if (StringUtils.isNotBlank(inputPrompt)) { PromptSupport.setPrompt(inputPrompt, impl); } else { PromptSupport.setPrompt(null, impl); } } @Override public CaseConversion getCaseConversion() { return caseConversion; } @Override public void setCaseConversion(CaseConversion caseConversion) { this.caseConversion = caseConversion; } @Override public void selectAll() { impl.selectAll(); } @Override public void setSelectionRange(int pos, int length) { impl.select(pos, pos + length); } @Override public void addTextChangeListener(TextChangeListener listener) { } @Override public void removeTextChangeListener(TextChangeListener listener) { } @Override public void setTextChangeTimeout(int timeout) { this.textChangeTimeout = timeout; } @Override public int getTextChangeTimeout() { return textChangeTimeout; } @Override public TextChangeEventMode getTextChangeEventMode() { return textChangeEventMode; } @Override public void setTextChangeEventMode(TextChangeEventMode mode) { this.textChangeEventMode = mode; } @Override public void commit() { // do nothing } @Override public void discard() { // do nothing } @Override public boolean isBuffered() { // do nothing return false; } @Override public void setBuffered(boolean buffered) { // do nothing } @Override public boolean isModified() { // do nothing return false; } protected class TextAreaFlushableField extends JTextArea implements Flushable { @Override public void flushValue() { flush(); } } @Override public void setDatatype(Datatype datatype) { this.datatype = datatype; this.valueFormatter.setDatatype(datatype); } @Override public String getRawValue() { return impl.getText(); } @Override public Datatype getDatatype() { return datatype; } }