1 56 package org.objectstyle.cayenne.modeler.dialog.datadomain; 57 58 import java.util.HashMap ; 59 import java.util.Map ; 60 61 import org.apache.log4j.Logger; 62 import org.objectstyle.cayenne.access.DataDomain; 63 import org.objectstyle.cayenne.access.DataRowStore; 64 import org.objectstyle.cayenne.map.event.DomainEvent; 65 import org.objectstyle.cayenne.modeler.ProjectController; 66 import org.scopemvc.controller.basic.BasicController; 67 import org.scopemvc.core.Control; 68 import org.scopemvc.core.ControlException; 69 import org.scopemvc.core.ModelChangeEvent; 70 import org.scopemvc.core.ModelChangeListener; 71 import org.scopemvc.core.Selector; 72 import org.scopemvc.view.swing.SPanel; 73 74 83 public class CacheSyncConfigController extends BasicController implements 84 ModelChangeListener { 85 86 private static Logger logObj = Logger.getLogger(CacheSyncConfigController.class); 87 88 private static final String JGROUPS_FACTORY_CLASS = "org.objectstyle.cayenne.event.JavaGroupsBridgeFactory"; 92 private static final String JMS_FACTORY_CLASS = "org.objectstyle.cayenne.event.JMSBridgeFactory"; 93 94 public static final String SAVE_CONFIG_CONTROL = "cayenne.modeler.cacheSyncConfig.save.button"; 95 public static final String CANCEL_CONFIG_CONTROL = "cayenne.modeler.cacheSyncConfig.cancel.button"; 96 97 public static final String JGROUPS_DEFAULT_CONTROL = "cayenne.modeler.jgroupConfig.radio1"; 98 99 public static final String JGROUPS_URL_CONTROL = "cayenne.modeler.jgroupConfig.radio2"; 100 101 protected Map existingCards; 102 protected boolean modified; 103 protected ProjectController eventController; 104 105 public CacheSyncConfigController(ProjectController eventController) { 106 this.eventController = eventController; 107 } 108 109 113 public void startup() { 114 DataDomain domain = eventController.getCurrentDataDomain(); 115 String factory = (String ) domain.getProperties().get( 116 DataRowStore.EVENT_BRIDGE_FACTORY_PROPERTY); 117 118 CacheSyncTypesModel topModel = buildTypesModel(factory); 119 setModel(topModel); 120 setView(new CacheSyncConfigDialog()); 121 122 prepareChildren(factory); 124 super.startup(); 125 } 126 127 131 public void modelChanged(ModelChangeEvent inEvent) { 132 logObj.info("ModelChangeEvent: " + inEvent.getSelector()); 133 134 Selector selector = inEvent.getSelector(); 135 136 if (selector.startsWith(CacheSyncTypesModel.FACTORY_LABEL_SELECTOR)) { 137 changeConfigView(); 138 modified = true; 139 logObj.info("** Factory selection modified.."); 140 } 141 else { 142 modified = true; 143 logObj.info("** Property modified modified.."); 144 } 145 } 146 147 151 protected void doHandleControl(Control control) throws ControlException { 152 logObj.info("Control: " + control); 153 154 if (control.matchesID(CANCEL_CONFIG_CONTROL)) { 155 shutdown(); 156 } 157 else if (control.matchesID(SAVE_CONFIG_CONTROL)) { 158 commitChanges(); 159 } 160 else if (control.matchesID(JGROUPS_DEFAULT_CONTROL)) { 161 jgroupsDefaultConfig(); 162 } 163 else if (control.matchesID(JGROUPS_URL_CONTROL)) { 164 jgroupsURLConfig(); 165 } 166 } 167 168 protected void jgroupsDefaultConfig() { 169 JGroupsConfigPanel view = (JGroupsConfigPanel) existingCards 170 .get(CacheSyncTypesModel.JGROUPS_FACTORY_LABEL); 171 if (view != null) { 172 view.showDefaultConfig(); 173 } 174 } 175 176 protected void jgroupsURLConfig() { 177 JGroupsConfigPanel view = (JGroupsConfigPanel) existingCards 178 .get(CacheSyncTypesModel.JGROUPS_FACTORY_LABEL); 179 if (view != null) { 180 view.showCustomConfig(); 181 } 182 } 183 184 187 protected void commitChanges() { 188 logObj.info("Has changes?: " + modified); 189 190 if (modified) { 191 CacheSyncTypesModel topModel = (CacheSyncTypesModel) getModel(); 193 SPanel card = (SPanel) existingCards.get(topModel.getFactoryLabel()); 194 CacheSyncConfigModel model = (CacheSyncConfigModel) card.getShownModel(); 195 196 DataDomain domain = eventController.getCurrentDataDomain(); 197 198 logObj.warn("domain properties BEFORE: " + domain.getProperties()); 199 model.storeProperties(domain.getProperties()); 200 201 logObj.warn("domain properties: " + domain.getProperties()); 202 203 eventController.fireDomainEvent(new DomainEvent(this, domain)); 204 } 205 206 shutdown(); 207 } 208 209 213 protected void changeConfigView() { 214 CacheSyncTypesModel topModel = (CacheSyncTypesModel) getModel(); 215 CacheSyncConfigModel newModel = buildModel(topModel); 216 217 String label = topModel.getFactoryLabel(); 219 SPanel card = (SPanel) existingCards.get(label); 220 card.setBoundModel(newModel); 221 ((CacheSyncConfigDialog) getView()).showCard(label); 222 } 223 224 protected CacheSyncTypesModel buildTypesModel(String factory) { 225 226 if (factory == null) { 227 factory = DataRowStore.EVENT_BRIDGE_FACTORY_DEFAULT; 228 } 229 230 String label; 231 232 if (JGROUPS_FACTORY_CLASS.equals(factory)) { 233 label = CacheSyncTypesModel.JGROUPS_FACTORY_LABEL; 234 } 235 else if (JMS_FACTORY_CLASS.equals(factory)) { 236 label = CacheSyncTypesModel.JMS_FACTORY_LABEL; 237 } 238 else { 239 label = CacheSyncTypesModel.CUSTOM_FACTORY_LABEL; 240 } 241 242 CacheSyncTypesModel model = new CacheSyncTypesModel(); 243 model.setFactoryLabel(label); 244 model.addModelChangeListener(this); 245 return model; 246 } 247 248 protected CacheSyncConfigModel buildModel(CacheSyncTypesModel topModel) { 249 String label = topModel.getFactoryLabel(); 250 String factory; 251 252 if (label.equals(CacheSyncTypesModel.JGROUPS_FACTORY_LABEL)) { 253 factory = JGROUPS_FACTORY_CLASS; 254 } 255 else if (label.equals(CacheSyncTypesModel.JMS_FACTORY_LABEL)) { 256 factory = JMS_FACTORY_CLASS; 257 } 258 else { 259 factory = null; 261 } 262 263 return buildModel(factory); 264 } 265 266 protected CacheSyncConfigModel buildModel(String factory) { 267 268 CacheSyncConfigModel model; 269 270 if (JGROUPS_FACTORY_CLASS.equals(factory)) { 271 model = new JGroupsConfigModel(); 272 } 273 else if (JMS_FACTORY_CLASS.equals(factory)) { 274 model = new JMSConfigModel(); 275 } 276 else { 277 model = new CacheSyncConfigModel(); 278 } 279 280 model.setMap(new HashMap (eventController.getCurrentDataDomain().getProperties())); 281 model.setFactoryClass(factory); 282 model.addModelChangeListener(this); 283 284 return model; 285 } 286 287 protected void prepareChildren(String factory) { 288 existingCards = new HashMap (); 289 CacheSyncConfigDialog topView = (CacheSyncConfigDialog) getView(); 290 291 294 JGroupsConfigPanel jgroupsPanel = new JGroupsConfigPanel(); 295 existingCards.put(CacheSyncTypesModel.JGROUPS_FACTORY_LABEL, jgroupsPanel); 296 topView.addCard(jgroupsPanel, CacheSyncTypesModel.JGROUPS_FACTORY_LABEL); 297 298 JMSConfigPanel jmsPanel = new JMSConfigPanel(); 299 existingCards.put(CacheSyncTypesModel.JMS_FACTORY_LABEL, jmsPanel); 300 topView.addCard(jmsPanel, CacheSyncTypesModel.JMS_FACTORY_LABEL); 301 302 CustomRemoteEventsConfigPanel customFactoryPanel = new CustomRemoteEventsConfigPanel(); 303 existingCards.put(CacheSyncTypesModel.CUSTOM_FACTORY_LABEL, customFactoryPanel); 304 topView.addCard(customFactoryPanel, CacheSyncTypesModel.CUSTOM_FACTORY_LABEL); 305 306 if (factory == null) { 307 factory = DataRowStore.EVENT_BRIDGE_FACTORY_DEFAULT; 308 } 309 310 Object model = buildModel(factory); 313 314 if (JGROUPS_FACTORY_CLASS.equals(factory)) { 315 jgroupsPanel.setBoundModel(model); 316 ((CacheSyncConfigDialog) getView()) 317 .showCard(CacheSyncTypesModel.JGROUPS_FACTORY_LABEL); 318 } 319 else if (JMS_FACTORY_CLASS.equals(factory)) { 320 jmsPanel.setBoundModel(model); 321 ((CacheSyncConfigDialog) getView()) 322 .showCard(CacheSyncTypesModel.JMS_FACTORY_LABEL); 323 } 324 else { 325 customFactoryPanel.setBoundModel(model); 326 ((CacheSyncConfigDialog) getView()) 327 .showCard(CacheSyncTypesModel.CUSTOM_FACTORY_LABEL); 328 } 329 } 330 } | Popular Tags |