001 /*
002 * Copyright 2002-2006 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * 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, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016 package org.springframework.richclient.form.binding.swing;
017
018 import java.awt.event.ItemEvent;
019 import java.awt.event.ItemListener;
020
021 import javax.swing.JComponent;
022 import javax.swing.JToggleButton;
023
024 import org.springframework.binding.form.FormModel;
025 import org.springframework.richclient.form.binding.support.CustomBinding;
026
027 /**
028 * @author Mathias Broekelmann
029 *
030 */
031 public class ToggleButtonBinding extends CustomBinding {
032
033 private final JToggleButton toggleButton;
034
035 private ItemListener selectionListener = new SelectionListener();
036
037 private boolean configureFace = true;
038
039 public ToggleButtonBinding(JToggleButton toggleButton, FormModel formModel, String formPropertyPath) {
040 super(formModel, formPropertyPath, Boolean.class);
041 this.toggleButton = toggleButton;
042 }
043
044 protected JComponent doBindControl() {
045 if(configureFace) {
046 getFieldFace().configure(toggleButton);
047 }
048 toggleButton.getModel().addItemListener(selectionListener);
049 toggleButton.setSelected(Boolean.TRUE.equals(getValue()));
050 return toggleButton;
051 }
052
053 void setConfigureFace(boolean configureFace) {
054 this.configureFace = configureFace;
055 }
056
057 protected void readOnlyChanged() {
058 toggleButton.setEnabled(isEnabled() && !isReadOnly());
059 }
060
061 protected void enabledChanged() {
062 toggleButton.setEnabled(isEnabled() && !isReadOnly());
063 }
064
065 protected void valueModelChanged(Object newValue) {
066 toggleButton.setSelected(Boolean.TRUE.equals(newValue));
067 }
068
069 protected class SelectionListener implements ItemListener {
070
071 public void itemStateChanged(ItemEvent e) {
072 controlValueChanged(Boolean.valueOf(toggleButton.isSelected()));
073 }
074
075 }
076 }