//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
*
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.framework.dataeditor;
import com.nexirius.framework.datamodel.InvalidValueException;
import com.nexirius.framework.datamodel.SimpleModel;
import com.nexirius.framework.dataviewer.DataViewer;
import com.nexirius.framework.swing.CFJTextField;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.awt.event.*;
/**
* This viewer is designed to edit SimpleModel data. It creates basically a JTextField which
* can be edited. As long as the set text method of the associated data model is not
* throwing an Exception the current editing state value of the model is changed. When the user
* presses the escape key then the original value is restored. When the user presses the ENTER key
* then the actual editing value is accepted and (eventually) a value change event is sent out
* by the data model. When the user is changing the input focus to another component
* then the current editing value also gets the actual value (value change event).
* While somone is editing (text field has input focus) in the text field value updates
* from the data model are ignored.
*
* @author Marcel Baumann
*/
public class SimpleEditor extends DataViewer
{
public static final String editorName = "SimpleEditor";
protected Color normalColor = SystemColor.textText;
protected Color normalBackground = null;
protected boolean isError = false;
public static final int MIN_WIDTH = 50;
public SimpleEditor(SimpleModel model) {
super(model);
}
/**
* Return true
*/
public boolean isEditor() {
return true;
}
/**
* Returns the associated data model
*/
public SimpleModel getModel() {
return (SimpleModel) getDataModel();
}
/**
* Returns the text field component or null (if not yet created)
*/
public JTextComponent getJTextComponent() {
return (JTextComponent) getJComponent();
}
/**
* Create the actual text field
*/
public void create() {
CFJTextField textField = new CFJTextField(this.getFactory().getClientResource(), getDataModel().getFieldName());
textField.setBackground(SystemColor.text);
setJComponent(textField);
initListeners();
update();
Dimension preferredSize = textField.getPreferredSize();
if (preferredSize.width < MIN_WIDTH) {
preferredSize.width = MIN_WIDTH;
textField.setPreferredSize(preferredSize);
}
}
/**
* Attach the needed listeners to the text component
*/
protected void initListeners() {
getJTextComponent().getDocument().addDocumentListener(new MyDocumentListener());
getJTextComponent().addFocusListener(new MyFocusListener());
getJTextComponent().addKeyListener(new KeyAdapter());
}
/**
* Only take over the actual value to the txt component if the component
* does not have the input focus
*/
public void update() {
if (isCreated()/* && !getJTextComponent().hasFocus() */) {
resetTextComponent();
}
}
/**
* this method is called when a struct layout component was created and the background color
* has to be changed to a specific color (setGray)
*/
public void updateUI() {
if (isCreated()/* && !getJTextComponent().hasFocus() */) {
super.updateUI();
if (getModel().isGray()) {
getJTextComponent().setEditable(false);
setDisabledBackgroundColor();
} else {
getJTextComponent().setEditable(true);
setNormalBackgroundColor();
}
}
}
/**
* Resets the text component to the actual value
*/
public void resetTextComponent() {
if (isCreated()) {
if (getModel().isExceptional()) {
getJTextComponent().setText(factory.getText(getModel().getStatus().getExceptionalStringId()));
} else {
String viewerText = getJTextComponent().getText();
String modelText = getModel().getText();
if (!viewerText.equals(modelText)) {
getJTextComponent().setText(modelText);
}
}
updateUI();
}
}
/**
* Only for debugging
*/
public String getViewerName() {
return editorName;
}
/**
* This method is called on value changes in the text field
*/
public void textValueChanged(TextEvent event) {
if (!isCreated() || !getJTextComponent().hasFocus()) {
return;
}
try {
getModel().setText(getJTextComponent().getText());
} catch (Exception ex) {
setErrorColor();
return;
}
setNormalColor();
}
class MyDocumentListener implements DocumentListener {
/**
* This method is called on text change events on the text field associated document.
*/
public void changedUpdate(DocumentEvent e) {
textValueChanged(null);
}
/**
* This method is called on text change events on the text field associated document.
*/
public void insertUpdate(DocumentEvent e) {
textValueChanged(null);
}
/**
* This method is called on text change events on the text field associated document.
*/
public void removeUpdate(DocumentEvent e) {
textValueChanged(null);
}
}
class MyFocusListener implements FocusListener {
/**
* This method calls startEdit() on the associated data model
*/
public void focusGained(FocusEvent ev) {
getModel().startEdit(SimpleEditor.this);
}
/**
* This method calls finishEdit on the associated data model
*/
public void focusLost(FocusEvent ev) {
if (ev.isTemporary()) {
// ignore
return;
}
boolean err = isError;
try {
getModel().finishEdit(SimpleEditor.this);
update();
setNormalColor();
} catch (InvalidValueException ex) {
getModel().cancelEdit(SimpleEditor.this);
}
if (err) {
getModel().beep();
}
}
}
/**
* This method sets back the original foreground color of the text field
*/
public void setNormalColor() {
if (isError) {
getJTextComponent().setForeground(normalColor);
isError = false;
}
}
/**
* This method set the foreground color of the text field to red. Normally this
* method is called when the setText method of the associated data model throws an
* exception. (micro validation)
*/
public void setErrorColor() {
if (!isError) {
isError = true;
normalColor = getJTextComponent().getForeground();
getJTextComponent().setForeground(Color.red);
}
}
/**
* This method sets back the original background color of the text field
*/
public void setNormalBackgroundColor() {
if (normalBackground != null) {
getJTextComponent().setBackground(normalBackground);
}
}
/**
* This method set the background color of the text field to gray.
*/
public void setDisabledBackgroundColor() {
Color currentBG = getJTextComponent().getBackground();
if (currentBG != SystemColor.textInactiveText) {
normalBackground = currentBG;
getJTextComponent().setBackground(SystemColor.textInactiveText);
}
}
public void onAction() {
try {
JRootPane rootpane = getJTextComponent().getRootPane();
if (rootpane != null) {
JButton b = rootpane.getDefaultButton();
if (b != null) {
b.doClick();
}
}
} catch (Throwable ex) {
ex.printStackTrace();
}
}
class KeyAdapter implements KeyListener {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
getModel().cancelEdit(SimpleEditor.this);
getModel().startEdit(SimpleEditor.this);
resetTextComponent();
} else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
// if (isError) {
// getModel().beep();
// }
getModel().finishEdit(SimpleEditor.this);
getModel().startEdit(SimpleEditor.this);
resetTextComponent();
onAction();
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
}