1 56 package org.objectstyle.cayenne.modeler.pref; 57 58 import java.awt.Component ; 59 import java.awt.event.ComponentAdapter ; 60 import java.awt.event.ComponentEvent ; 61 import java.beans.PropertyChangeEvent ; 62 import java.beans.PropertyChangeListener ; 63 64 import javax.swing.JDialog ; 65 import javax.swing.JFrame ; 66 67 import org.apache.commons.beanutils.PropertyUtils; 68 import org.objectstyle.cayenne.pref.Domain; 69 import org.objectstyle.cayenne.pref.PreferenceException; 70 71 public class ComponentGeometry extends _ComponentGeometry { 72 73 public static final String GEOMETRY_PREF_KEY = "geometry"; 74 75 public static ComponentGeometry getPreference(Domain domain) { 76 return (ComponentGeometry) domain.getDetail( 77 ComponentGeometry.GEOMETRY_PREF_KEY, 78 ComponentGeometry.class, 79 true); 80 } 81 82 86 public void bind( 87 final JFrame frame, 88 int initialWidth, 89 int initialHeight, 90 int maxOffset) { 91 92 updateSize(frame, initialWidth, initialHeight); 93 updateLocation(frame, maxOffset); 94 95 frame.addComponentListener(new ComponentAdapter () { 96 97 public void componentResized(ComponentEvent e) { 98 setWidth(new Integer (frame.getWidth())); 99 setHeight(new Integer (frame.getHeight())); 100 } 101 102 public void componentMoved(ComponentEvent e) { 103 setX(new Integer (frame.getX())); 104 setY(new Integer (frame.getY())); 105 } 106 }); 107 } 108 109 113 public void bind(final JDialog dialog, int initialWidth, int initialHeight) { 114 updateSize(dialog, initialWidth, initialHeight); 115 116 dialog.addComponentListener(new ComponentAdapter () { 117 118 public void componentResized(ComponentEvent e) { 119 setWidth(new Integer (dialog.getWidth())); 120 setHeight(new Integer (dialog.getHeight())); 121 } 122 }); 123 } 124 125 129 public void bindIntProperty( 130 final Component component, 131 final String property, 132 int defaultValue) { 133 134 updateIntProperty(component, property, defaultValue); 135 136 component.addPropertyChangeListener(property, new PropertyChangeListener () { 137 138 public void propertyChange(PropertyChangeEvent e) { 139 Object value = e.getNewValue(); 140 setProperty(property, value != null ? value.toString() : null); 141 } 142 }); 143 } 144 145 void updateIntProperty(Component c, String property, int defaultValue) { 146 int i = getIntProperty(property, defaultValue); 147 try { 148 PropertyUtils.setProperty(c, property, new Integer (i)); 149 } 150 catch (Throwable th) { 151 throw new PreferenceException("Error setting property: " + property, th); 152 } 153 } 154 155 void updateSize(Component c, int initialWidth, int initialHeight) { 156 int w = getIntWidth(initialWidth); 157 int h = getIntHeight(initialHeight); 158 159 if (w > 0 && h > 0) { 160 c.setSize(w, h); 161 } 162 } 163 164 void updateLocation(Component c, int maxOffset) { 165 if (maxOffset != 0) { 166 int xOffset = (int) (Math.random() * maxOffset); 167 int yOffset = (int) (Math.random() * maxOffset); 168 changeX(xOffset); 169 changeY(yOffset); 170 } 171 172 int x = getIntX(-1); 173 int y = getIntY(-1); 174 175 if (x > 0 && y > 0) { 176 c.setLocation(x, y); 177 } 178 } 179 180 public void changeX(int xOffset) { 181 if (xOffset != 0) { 182 setX(new Integer (getIntX(0) + xOffset)); 183 } 184 } 185 186 public void changeY(int yOffset) { 187 if (yOffset != 0) { 188 setY(new Integer (getIntY(0) + yOffset)); 189 } 190 } 191 192 public int getIntWidth(int defaultValue) { 193 return (getWidth() != null) ? getWidth().intValue() : defaultValue; 194 } 195 196 public int getIntHeight(int defaultValue) { 197 return (getHeight() != null) ? getHeight().intValue() : defaultValue; 198 } 199 200 public int getIntX(int defaultValue) { 201 return (getX() != null) ? getX().intValue() : defaultValue; 202 } 203 204 public int getIntY(int defaultValue) { 205 return (getY() != null) ? getY().intValue() : defaultValue; 206 } 207 } 208 209 | Popular Tags |