1 56 package org.objectstyle.cayenne.modeler.util; 57 58 import java.awt.Component ; 59 import java.awt.Dimension ; 60 import java.awt.Point ; 61 import java.awt.Window ; 62 import java.awt.event.ActionEvent ; 63 import java.awt.event.ActionListener ; 64 import java.awt.event.KeyEvent ; 65 import java.beans.PropertyChangeListener ; 66 import java.beans.PropertyChangeSupport ; 67 68 import javax.swing.JComponent ; 69 import javax.swing.JDialog ; 70 import javax.swing.JFrame ; 71 import javax.swing.JOptionPane ; 72 import javax.swing.KeyStroke ; 73 74 import org.apache.log4j.Logger; 75 import org.objectstyle.cayenne.modeler.Application; 76 import org.objectstyle.cayenne.modeler.pref.FSPath; 77 import org.objectstyle.cayenne.pref.Domain; 78 import org.objectstyle.cayenne.swing.BoundComponent; 79 import org.objectstyle.cayenne.util.Util; 80 81 86 public abstract class CayenneController implements BoundComponent { 87 88 private static final Logger logObj = Logger.getLogger(CayenneController.class); 89 90 protected CayenneController parent; 91 protected Application application; 92 protected PropertyChangeSupport propertyChangeSupport; 93 94 public CayenneController(CayenneController parent) { 95 this.application = (parent != null) ? parent.getApplication() : null; 96 this.parent = parent; 97 } 98 99 public CayenneController(Application application) { 100 this.application = application; 101 } 102 103 public Application getApplication() { 104 return application; 105 } 106 107 public CayenneController getParent() { 108 return parent; 109 } 110 111 114 public abstract Component getView(); 115 116 121 public FSPath getLastDirectory() { 122 FSPath path = (FSPath) getViewDomain().getDetail("lastDir", FSPath.class, true); 124 125 if (path.getPath() == null) { 126 127 String pathString = (getParent() != null) ? getParent() 128 .getLastDirectory() 129 .getPath() : System.getProperty("user.home"); 130 path.setPath(pathString); 131 } 132 133 return path; 134 } 135 136 139 protected Domain getViewDomain() { 140 return getApplication().getPreferenceDomain().getSubdomain(getView().getClass()); 141 } 142 143 147 protected void reportError(String title, Throwable th) { 148 th = Util.unwindException(th); 149 logObj.info("Error in " + getClass().getName(), th); 150 151 JOptionPane.showMessageDialog(getView(), 152 th.getMessage(), 153 title, 154 JOptionPane.ERROR_MESSAGE); 155 } 156 157 160 protected void centerView() { 161 Window parentWindow = parent.getWindow(); 162 163 Dimension parentSize = parentWindow.getSize(); 164 Dimension windowSize = getView().getSize(); 165 Point parentLocation = new Point (0, 0); 166 if (parentWindow.isShowing()) { 167 parentLocation = parentWindow.getLocationOnScreen(); 168 } 169 170 int x = parentLocation.x + parentSize.width / 2 - windowSize.width / 2; 171 int y = parentLocation.y + parentSize.height / 2 - windowSize.height / 2; 172 173 getView().setLocation(x, y); 174 } 175 176 181 protected void makeCloseableOnEscape() { 182 183 Window window = getWindow(); 184 if (!(window instanceof JDialog )) { 185 return; 186 } 187 188 final JDialog dialog = (JDialog ) window; 189 190 KeyStroke escReleased = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true); 191 ActionListener closeAction = new ActionListener () { 192 193 public void actionPerformed(ActionEvent e) { 194 if (dialog.isVisible()) { 195 switch (dialog.getDefaultCloseOperation()) { 196 case JDialog.HIDE_ON_CLOSE: 197 dialog.setVisible(false); 198 break; 199 case JDialog.DISPOSE_ON_CLOSE: 200 dialog.setVisible(false); 201 dialog.dispose(); 202 break; 203 case JDialog.DO_NOTHING_ON_CLOSE: 204 default: 205 break; 206 } 207 } 208 } 209 }; 210 dialog.getRootPane().registerKeyboardAction(closeAction, 211 escReleased, 212 JComponent.WHEN_IN_FOCUSED_WINDOW); 213 } 214 215 218 public Window getWindow() { 219 Component view = getView(); 220 while (view != null) { 221 if (view instanceof Window ) { 222 return (Window ) view; 223 } 224 225 view = view.getParent(); 226 } 227 228 return null; 229 } 230 231 234 public JFrame getFrame() { 235 Component view = getView(); 236 while (view != null) { 237 if (view instanceof JFrame ) { 238 return (JFrame ) view; 239 } 240 241 view = view.getParent(); 242 } 243 244 return null; 245 } 246 247 250 protected void firePropertyChange( 251 String propertyName, 252 Object oldValue, 253 Object newValue) { 254 if (propertyChangeSupport != null) { 255 propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue); 256 } 257 } 258 259 262 public void addPropertyChangeListener( 263 String expression, 264 PropertyChangeListener listener) { 265 266 if (propertyChangeSupport == null) { 267 propertyChangeSupport = new PropertyChangeSupport (this); 268 } 269 270 propertyChangeSupport.addPropertyChangeListener(expression, listener); 271 } 272 273 276 public void bindingUpdated(String expression, Object newValue) { 277 } 279 } | Popular Tags |