001 /* 002 * Copyright 2002-2004 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.core; 017 018 import java.awt.Image; 019 import java.beans.PropertyChangeListener; 020 import java.beans.PropertyChangeSupport; 021 022 import javax.swing.Icon; 023 import javax.swing.ImageIcon; 024 import javax.swing.KeyStroke; 025 import javax.swing.event.SwingPropertyChangeSupport; 026 027 import org.apache.commons.logging.Log; 028 import org.apache.commons.logging.LogFactory; 029 import org.springframework.core.style.ToStringCreator; 030 import org.springframework.richclient.application.support.ApplicationServicesAccessor; 031 import org.springframework.richclient.command.config.CommandButtonLabelInfo; 032 import org.springframework.richclient.command.config.CommandLabelConfigurable; 033 import org.springframework.richclient.image.config.ImageConfigurable; 034 import org.springframework.util.Assert; 035 import org.springframework.util.ObjectUtils; 036 037 /** 038 * A convenient super class for objects that can be labeled for display in a 039 * GUI. 040 * 041 * @author Keith Donald 042 */ 043 public class LabeledObjectSupport extends ApplicationServicesAccessor implements DescribedElement, VisualizedElement, 044 CommandLabelConfigurable, ImageConfigurable, DescriptionConfigurable, TitleConfigurable { 045 protected final Log logger = LogFactory.getLog(getClass()); 046 047 private CommandButtonLabelInfo label; 048 049 private String title; 050 051 private String caption; 052 053 private String description; 054 055 private Image image; 056 057 private PropertyChangeSupport propertyChangeSupport; 058 059 public void setLabelInfo(CommandButtonLabelInfo label) { 060 String oldDisplayName = null; 061 if (this.title != null || this.label != null) { 062 oldDisplayName = getDisplayName(); 063 } 064 065 int oldMnemonic = getMnemonic(); 066 int oldMnemonicIndex = getMnemonicIndex(); 067 KeyStroke oldAccelerator = getAccelerator(); 068 this.label = label; 069 firePropertyChange(DISPLAY_NAME_PROPERTY, oldDisplayName, getDisplayName()); 070 firePropertyChange("mnemonic", oldMnemonic, getMnemonic()); 071 firePropertyChange("mnemonicIndex", oldMnemonicIndex, getMnemonicIndex()); 072 firePropertyChange("accelerator", oldAccelerator, getAccelerator()); 073 } 074 075 public void setCaption(String caption) { 076 String oldValue = caption; 077 this.caption = caption; 078 firePropertyChange(CAPTION_PROPERTY, oldValue, caption); 079 } 080 081 public void setDescription(String description) { 082 String oldValue = this.description; 083 this.description = description; 084 firePropertyChange(DESCRIPTION_PROPERTY, oldValue, description); 085 } 086 087 public void setTitle(String title) { 088 String oldValue = null; 089 if (this.title != null || this.label != null) { 090 oldValue = getDisplayName(); 091 } 092 093 this.title = title; 094 firePropertyChange(DISPLAY_NAME_PROPERTY, oldValue, getDisplayName()); 095 } 096 097 public void setImage(Image image) { 098 Image oldValue = image; 099 this.image = image; 100 firePropertyChange("image", oldValue, image); 101 } 102 103 public String getDisplayName() { 104 if (title != null) { 105 return title; 106 } 107 108 if (label == null) { 109 if (logger.isInfoEnabled()) { 110 logger.info("This labeled object's display name is not configured; returning 'displayName'"); 111 } 112 return "displayName"; 113 } 114 return label.getText(); 115 } 116 117 public String getCaption() { 118 return caption; 119 } 120 121 public String getDescription() { 122 return description; 123 } 124 125 public Image getImage() { 126 return image; 127 } 128 129 public Icon getIcon() { 130 if (image != null) 131 return new ImageIcon(image); 132 133 return null; 134 } 135 136 public int getMnemonic() { 137 if (label != null) 138 return label.getMnemonic(); 139 140 return 0; 141 } 142 143 public int getMnemonicIndex() { 144 if (label != null) 145 return label.getMnemonicIndex(); 146 147 return 0; 148 } 149 150 public KeyStroke getAccelerator() { 151 if (label != null) 152 return label.getAccelerator(); 153 154 return null; 155 } 156 157 public CommandButtonLabelInfo getLabel() { 158 return label; 159 } 160 161 public void addPropertyChangeListener(PropertyChangeListener l) { 162 getOrCreatePropertyChangeSupport().addPropertyChangeListener(l); 163 } 164 165 public void addPropertyChangeListener(String propertyName, PropertyChangeListener l) { 166 getOrCreatePropertyChangeSupport().addPropertyChangeListener(propertyName, l); 167 } 168 169 public void removePropertyChangeListener(PropertyChangeListener l) { 170 getPropertyChangeSupport().removePropertyChangeListener(l); 171 } 172 173 public void removePropertyChangeListener(String propertyName, PropertyChangeListener l) { 174 getPropertyChangeSupport().removePropertyChangeListener(propertyName, l); 175 } 176 177 private PropertyChangeSupport getPropertyChangeSupport() { 178 Assert.notNull(propertyChangeSupport, 179 "Property change support has not yet been initialized; add a listener first!"); 180 return propertyChangeSupport; 181 } 182 183 private PropertyChangeSupport getOrCreatePropertyChangeSupport() { 184 if (propertyChangeSupport == null) { 185 propertyChangeSupport = new SwingPropertyChangeSupport(this); 186 } 187 return propertyChangeSupport; 188 } 189 190 protected void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { 191 if (propertyChangeSupport != null) { 192 propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue); 193 } 194 } 195 196 protected void firePropertyChange(String propertyName, int oldValue, int newValue) { 197 if (propertyChangeSupport != null) { 198 propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue); 199 } 200 } 201 202 protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { 203 if (propertyChangeSupport != null) { 204 propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue); 205 } 206 } 207 208 protected boolean hasChanged(Object currentValue, Object proposedValue) { 209 return !ObjectUtils.nullSafeEquals(currentValue, proposedValue); 210 } 211 212 protected boolean hasChanged(boolean currentValue, boolean proposedValue) { 213 return currentValue != proposedValue; 214 } 215 216 protected boolean hasChanged(int currentValue, int proposedValue) { 217 return currentValue != proposedValue; 218 } 219 220 public String toString() { 221 return new ToStringCreator(this).toString(); 222 } 223 224 }