1 56 57 package org.objectstyle.cayenne.modeler.editor; 58 59 import java.awt.BorderLayout ; 60 import java.awt.event.ActionEvent ; 61 import java.awt.event.ActionListener ; 62 import java.util.Map ; 63 64 import javax.swing.JButton ; 65 import javax.swing.JCheckBox ; 66 import javax.swing.JPanel ; 67 import javax.swing.JTextField ; 68 69 import org.objectstyle.cayenne.access.DataDomain; 70 import org.objectstyle.cayenne.access.DataRowStore; 71 import org.objectstyle.cayenne.conf.Configuration; 72 import org.objectstyle.cayenne.map.event.DomainEvent; 73 import org.objectstyle.cayenne.modeler.Application; 74 import org.objectstyle.cayenne.modeler.ProjectController; 75 import org.objectstyle.cayenne.modeler.dialog.datadomain.CacheSyncConfigController; 76 import org.objectstyle.cayenne.modeler.event.DomainDisplayEvent; 77 import org.objectstyle.cayenne.modeler.event.DomainDisplayListener; 78 import org.objectstyle.cayenne.modeler.util.ProjectUtil; 79 import org.objectstyle.cayenne.modeler.util.TextAdapter; 80 import org.objectstyle.cayenne.pref.Domain; 81 import org.objectstyle.cayenne.project.ApplicationProject; 82 import org.objectstyle.cayenne.util.Util; 83 import org.objectstyle.cayenne.validation.ValidationException; 84 85 import com.jgoodies.forms.builder.DefaultFormBuilder; 86 import com.jgoodies.forms.layout.FormLayout; 87 88 91 public class DataDomainView extends JPanel implements DomainDisplayListener { 92 93 protected ProjectController projectController; 94 95 protected TextAdapter name; 96 protected TextAdapter cacheSize; 97 protected JCheckBox objectValidation; 98 protected JCheckBox externalTransactions; 99 protected JCheckBox sharedCache; 100 protected JCheckBox remoteUpdates; 101 protected JButton configRemoteUpdates; 102 103 public DataDomainView(ProjectController projectController) { 104 this.projectController = projectController; 105 106 initView(); 108 109 initController(); 111 } 112 113 protected void initView() { 114 115 this.name = new TextAdapter(new JTextField ()) { 117 118 protected void updateModel(String text) { 119 setDomainName(text); 120 } 121 }; 122 123 this.cacheSize = new TextAdapter(new JTextField (10)) { 124 125 protected void updateModel(String text) { 126 setCacheSize(text); 127 } 128 }; 129 130 this.objectValidation = new JCheckBox (); 131 this.externalTransactions = new JCheckBox (); 132 133 this.sharedCache = new JCheckBox (); 134 this.remoteUpdates = new JCheckBox (); 135 this.configRemoteUpdates = new JButton ("Configure"); 136 configRemoteUpdates.setEnabled(false); 137 138 140 FormLayout layout = new FormLayout( 141 "right:max(50dlu;pref), 3dlu, left:max(20dlu;pref), 3dlu, left:150", 142 ""); 143 DefaultFormBuilder builder = new DefaultFormBuilder(layout); 144 builder.setDefaultDialogBorder(); 145 146 builder.appendSeparator("DataDomain Configuration"); 147 builder.append("DataDomain Name:", name.getComponent(), 3); 148 builder.append("Child DataContexts Validate Objects:", objectValidation, 3); 149 builder.append("Container-Managed Transactions:", externalTransactions, 3); 150 151 builder.appendSeparator("Cache Configuration"); 152 builder.append("Max. Number of Objects:", cacheSize.getComponent(), 3); 153 builder.append("Use Shared Cache:", sharedCache, 3); 154 builder 155 .append( 156 "Remote Change Notifications:", 157 remoteUpdates, 158 configRemoteUpdates); 159 160 this.setLayout(new BorderLayout ()); 161 this.add(builder.getPanel(), BorderLayout.CENTER); 162 } 163 164 protected void initController() { 165 projectController.addDomainDisplayListener(this); 166 167 objectValidation.addActionListener(new ActionListener () { 169 170 public void actionPerformed(ActionEvent e) { 171 String value = objectValidation.isSelected() ? "true" : "false"; 172 setDomainProperty( 173 DataDomain.VALIDATING_OBJECTS_ON_COMMIT_PROPERTY, 174 value, 175 Boolean.toString(DataDomain.VALIDATING_OBJECTS_ON_COMMIT_DEFAULT)); 176 } 177 }); 178 179 externalTransactions.addActionListener(new ActionListener () { 180 181 public void actionPerformed(ActionEvent e) { 182 String value = externalTransactions.isSelected() ? "true" : "false"; 183 setDomainProperty( 184 DataDomain.USING_EXTERNAL_TRANSACTIONS_PROPERTY, 185 value, 186 Boolean.toString(DataDomain.USING_EXTERNAL_TRANSACTIONS_DEFAULT)); 187 } 188 }); 189 190 sharedCache.addActionListener(new ActionListener () { 191 192 public void actionPerformed(ActionEvent e) { 193 String value = sharedCache.isSelected() ? "true" : "false"; 194 setDomainProperty( 195 DataDomain.SHARED_CACHE_ENABLED_PROPERTY, 196 value, 197 Boolean.toString(DataDomain.SHARED_CACHE_ENABLED_DEFAULT)); 198 199 201 remoteUpdates.setEnabled(sharedCache.isSelected()); 202 203 if (!sharedCache.isSelected()) { 204 remoteUpdates.setSelected(false); 206 } 207 208 configRemoteUpdates.setEnabled(remoteUpdates.isSelected()); 210 } 211 }); 212 213 remoteUpdates.addActionListener(new ActionListener () { 214 215 public void actionPerformed(ActionEvent e) { 216 String value = remoteUpdates.isSelected() ? "true" : "false"; 217 218 configRemoteUpdates.setEnabled(remoteUpdates.isSelected()); 220 221 setDomainProperty( 222 DataRowStore.REMOTE_NOTIFICATION_PROPERTY, 223 value, 224 Boolean.toString(DataRowStore.REMOTE_NOTIFICATION_DEFAULT)); 225 } 226 }); 227 228 configRemoteUpdates.addActionListener(new ActionListener () { 229 230 public void actionPerformed(ActionEvent e) { 231 new CacheSyncConfigController(projectController).startup(); 232 } 233 }); 234 } 235 236 240 protected void setDomainProperty(String property, String value, String defaultValue) { 241 242 DataDomain domain = projectController.getCurrentDataDomain(); 243 if (domain == null) { 244 return; 245 } 246 247 if ("".equals(value)) { 249 value = null; 250 } 251 252 if (value != null && value.equals(defaultValue)) { 254 value = null; 255 } 256 257 Map properties = domain.getProperties(); 258 Object oldValue = properties.get(property); 259 if (!Util.nullSafeEquals(value, oldValue)) { 260 properties.put(property, value); 261 262 DomainEvent e = new DomainEvent(this, domain); 263 projectController.fireDomainEvent(e); 264 } 265 } 266 267 public String getDomainProperty(String property, String defaultValue) { 268 DataDomain domain = projectController.getCurrentDataDomain(); 269 if (domain == null) { 270 return null; 271 } 272 273 String value = (String ) domain.getProperties().get(property); 274 return value != null ? value : defaultValue; 275 } 276 277 public boolean getDomainBooleanProperty(String property, String defaultValue) { 278 return "true".equalsIgnoreCase(getDomainProperty(property, defaultValue)); 279 } 280 281 285 public void currentDomainChanged(DomainDisplayEvent e) { 286 DataDomain domain = e.getDomain(); 287 if (null == domain) { 288 return; 289 } 290 291 name.setText(domain.getName()); 293 294 cacheSize.setText(getDomainProperty( 295 DataRowStore.SNAPSHOT_CACHE_SIZE_PROPERTY, 296 Integer.toString(DataRowStore.SNAPSHOT_CACHE_SIZE_DEFAULT))); 297 298 objectValidation.setSelected(getDomainBooleanProperty( 299 DataDomain.VALIDATING_OBJECTS_ON_COMMIT_PROPERTY, 300 Boolean.toString(DataDomain.VALIDATING_OBJECTS_ON_COMMIT_DEFAULT))); 301 302 externalTransactions.setSelected(getDomainBooleanProperty( 303 DataDomain.USING_EXTERNAL_TRANSACTIONS_PROPERTY, 304 Boolean.toString(DataDomain.USING_EXTERNAL_TRANSACTIONS_DEFAULT))); 305 306 sharedCache.setSelected(getDomainBooleanProperty( 307 DataDomain.SHARED_CACHE_ENABLED_PROPERTY, 308 Boolean.toString(DataDomain.SHARED_CACHE_ENABLED_DEFAULT))); 309 310 remoteUpdates.setSelected(getDomainBooleanProperty( 311 DataRowStore.REMOTE_NOTIFICATION_PROPERTY, 312 Boolean.toString(DataRowStore.REMOTE_NOTIFICATION_DEFAULT))); 313 remoteUpdates.setEnabled(sharedCache.isSelected()); 314 configRemoteUpdates.setEnabled(remoteUpdates.isEnabled() 315 && remoteUpdates.isSelected()); 316 } 317 318 void setDomainName(String newName) { 319 if (newName == null || newName.trim().length() == 0) { 320 throw new ValidationException("Enter name for DataDomain"); 321 } 322 323 Configuration configuration = ((ApplicationProject) Application.getProject()) 324 .getConfiguration(); 325 DataDomain domain = projectController.getCurrentDataDomain(); 326 327 DataDomain matchingDomain = configuration.getDomain(newName); 328 329 if (matchingDomain == null) { 330 Domain prefs = projectController.getPreferenceDomainForDataDomain(); 331 332 DomainEvent e = new DomainEvent(this, domain, domain.getName()); 333 ProjectUtil.setDataDomainName(configuration, domain, newName); 334 prefs.rename(newName); 335 projectController.fireDomainEvent(e); 336 } 337 else if (matchingDomain != domain) { 338 throw new ValidationException("There is another DataDomain named '" 339 + newName 340 + "'. Use a different name."); 341 } 342 } 343 344 void setCacheSize(String text) { 345 if (text.length() > 0) { 346 try { 347 Integer.parseInt(text); 348 } 349 catch (NumberFormatException ex) { 350 throw new ValidationException("Cache size must be an integer: " + text); 351 } 352 } 353 354 setDomainProperty(DataRowStore.SNAPSHOT_CACHE_SIZE_PROPERTY, text, Integer 355 .toString(DataRowStore.SNAPSHOT_CACHE_SIZE_DEFAULT)); 356 } 357 } | Popular Tags |