001 /* 002 * Copyright 2002-2004 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.springframework.richclient.form.binding.swing; 017 018 import org.jdesktop.xswingx.PromptSupport; 019 import org.springframework.binding.form.FormModel; 020 import org.springframework.binding.value.ValueModel; 021 import org.springframework.binding.value.swing.AsYouTypeTextComponentAdapter; 022 import org.springframework.richclient.form.binding.support.AbstractBinding; 023 import org.springframework.richclient.util.RcpSupport; 024 import org.springframework.richclient.text.SelectAllFocusListener; 025 026 import javax.swing.*; 027 import javax.swing.text.JTextComponent; 028 029 /** 030 * @author Oliver Hutchison 031 */ 032 public class TextComponentBinding extends AbstractBinding 033 { 034 035 private final JTextComponent textComponent; 036 private boolean convertEmptyStringToNull; 037 private String promptKey; 038 private boolean readOnly; 039 private boolean selectAllOnFocus; 040 041 public TextComponentBinding(JTextComponent textComponent, FormModel formModel, String formPropertyPath) 042 { 043 super(formModel, formPropertyPath, String.class); 044 this.textComponent = textComponent; 045 } 046 047 protected JComponent doBindControl() 048 { 049 final ValueModel valueModel = getValueModel(); 050 try 051 { 052 textComponent.setText((String) valueModel.getValue()); 053 } 054 catch (ClassCastException e) 055 { 056 IllegalArgumentException ex = new IllegalArgumentException("Class cast exception converting '" 057 + getProperty() + "' property value to string - did you install a type converter?"); 058 ex.initCause(e); 059 throw ex; 060 } 061 if (getPromptKey() != null) 062 PromptSupport.setPrompt(RcpSupport.getMessage(getPromptKey()), textComponent); 063 new AsYouTypeTextComponentAdapter(textComponent, valueModel, convertEmptyStringToNull); 064 if(selectAllOnFocus) 065 { 066 textComponent.addFocusListener(new SelectAllFocusListener(textComponent)); 067 } 068 return textComponent; 069 } 070 071 public boolean isReadOnly() 072 { 073 return super.isReadOnly() || readOnly; 074 } 075 076 public void setReadOnly(boolean readOnly) 077 { 078 this.readOnly = readOnly; 079 } 080 081 protected void readOnlyChanged() 082 { 083 textComponent.setEditable(!isReadOnly()); 084 } 085 086 protected void enabledChanged() 087 { 088 textComponent.setEnabled(isEnabled()); 089 } 090 091 public String getPromptKey() 092 { 093 return promptKey; 094 } 095 096 public void setPromptKey(String promptKey) 097 { 098 this.promptKey = promptKey; 099 } 100 101 public boolean isConvertEmptyStringToNull() 102 { 103 return convertEmptyStringToNull; 104 } 105 106 public void setConvertEmptyStringToNull(boolean convertEmptyStringToNull) 107 { 108 this.convertEmptyStringToNull = convertEmptyStringToNull; 109 } 110 111 public boolean isSelectAllOnFocus() 112 { 113 return selectAllOnFocus; 114 } 115 116 public void setSelectAllOnFocus(boolean selectAllOnFocus) 117 { 118 this.selectAllOnFocus = selectAllOnFocus; 119 } 120 }