Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 
 package org.apache.cayenne.modeler.dialog.datadomain;
 
 import java.util.Map;
 
 import  org.scopemvc.controller.basic.BasicController;
 import  org.scopemvc.core.Control;
 import  org.scopemvc.core.ControlException;
 import  org.scopemvc.core.ModelChangeEvent;
 import  org.scopemvc.core.ModelChangeListener;
 import  org.scopemvc.core.Selector;
 import  org.scopemvc.view.swing.SPanel;

A controller for CacheSyncConfigDialog and its subviews. Builds a model out of a DataDomain properties map, and on save updates DataDomain properties with configuration changes made by the user. This controller manages one main dialog view, and its subviews organized using CardLayout. Each subview as well as the main dialog have their own independent models.

Author(s):
Andrei Adamchik
 
 public class CacheSyncConfigController extends BasicController implements
         ModelChangeListener {
 
     private static Logger logObj = Logger.getLogger(CacheSyncConfigController.class);
 
     // using strings instead of the actioal factory classes, since we
     // JMS and JavaGroups libraries may not be around, and Modeler
     // may throw CNFE
     private static final String JGROUPS_FACTORY_CLASS = "org.apache.cayenne.event.JavaGroupsBridgeFactory";
     private static final String JMS_FACTORY_CLASS = "org.apache.cayenne.event.JMSBridgeFactory";
 
     public static final String SAVE_CONFIG_CONTROL = "cayenne.modeler.cacheSyncConfig.save.button";
     public static final String CANCEL_CONFIG_CONTROL = "cayenne.modeler.cacheSyncConfig.cancel.button";
 
     public static final String JGROUPS_DEFAULT_CONTROL = "cayenne.modeler.jgroupConfig.radio1";
 
     public static final String JGROUPS_URL_CONTROL = "cayenne.modeler.jgroupConfig.radio2";
 
     protected Map existingCards;
     protected boolean modified;
     protected ProjectController eventController;
 
     public CacheSyncConfigController(ProjectController eventController) {
         this. = eventController;
     }

    
Creates and shows a new modal dialog window. Registers as a listener for its own model to update subviews on model changes.
 
     public void startup() {
         DataDomain domain = .getCurrentDataDomain();
         String factory = (Stringdomain.getProperties().get(
                 .);
 
         CacheSyncTypesModel topModel = buildTypesModel(factory);
         setModel(topModel);
         setView(new CacheSyncConfigDialog());
 
         // build cards, showing the one corresponding to DataDomain state
         prepareChildren(factory);
         super.startup();
     }

    
ModelChangeListener implementation that updates "modified" status and changes dialog subview on model changes.
 
     public void modelChanged(ModelChangeEvent inEvent) {
         .info("ModelChangeEvent: " + inEvent.getSelector());
 
         Selector selector = inEvent.getSelector();
 
        if (selector.startsWith(.)) {
            changeConfigView();
             = true;
            .info("** Factory selection modified..");
        }
        else {
             = true;
            .info("** Property modified modified..");
        }
    }

    
Overrides super implementation to process controls from this controller's view and its subviews.
    protected void doHandleControl(Control controlthrows ControlException {
        .info("Control: " + control);
        if (control.matchesID()) {
            shutdown();
        }
        else if (control.matchesID()) {
            commitChanges();
        }
        else if (control.matchesID()) {
            jgroupsDefaultConfig();
        }
        else if (control.matchesID()) {
            jgroupsURLConfig();
        }
    }
    protected void jgroupsDefaultConfig() {
                .get(.);
        if (view != null) {
            view.showDefaultConfig();
        }
    }
    protected void jgroupsURLConfig() {
                .get(.);
        if (view != null) {
            view.showCustomConfig();
        }
    }

    
Stores configuration changes in the data domain properties.
    protected void commitChanges() {
        .info("Has changes?: " + );
        if () {
            // extract model from current card
            CacheSyncTypesModel topModel = (CacheSyncTypesModelgetModel();
            SPanel card = (SPanel) .get(topModel.getFactoryLabel());
            CacheSyncConfigModel model = (CacheSyncConfigModelcard.getShownModel();
            DataDomain domain = .getCurrentDataDomain();
            .warn("domain properties BEFORE: " + domain.getProperties());
            model.storeProperties(domain.getProperties());
            .warn("domain properties: " + domain.getProperties());
            .fireDomainEvent(new DomainEvent(thisdomain));
        }
        shutdown();
    }

    
Changes a subview to a panel specific for the currently selected configuration type.
    protected void changeConfigView() {
        CacheSyncTypesModel topModel = (CacheSyncTypesModelgetModel();
        CacheSyncConfigModel newModel = buildModel(topModel);
        // NOTE: card doesn't have a controller, since it does not need it
        String label = topModel.getFactoryLabel();
        SPanel card = (SPanel) .get(label);
        card.setBoundModel(newModel);
        ((CacheSyncConfigDialoggetView()).showCard(label);
    }
    protected CacheSyncTypesModel buildTypesModel(String factory) {
        if (factory == null) {
            factory = .;
        }
        String label;
        if (.equals(factory)) {
            label = .;
        }
        else if (.equals(factory)) {
            label = .;
        }
        else {
            label = .;
        }
        CacheSyncTypesModel model = new CacheSyncTypesModel();
        model.setFactoryLabel(label);
        model.addModelChangeListener(this);
        return model;
    }
    protected CacheSyncConfigModel buildModel(CacheSyncTypesModel topModel) {
        String label = topModel.getFactoryLabel();
        String factory;
        if (label.equals(.)) {
            factory = ;
        }
        else if (label.equals(.)) {
            factory = ;
        }
        else {
            // reset factory
            factory = null;
        }
        return buildModel(factory);
    }
    protected CacheSyncConfigModel buildModel(String factory) {
        CacheSyncConfigModel model;
        if (.equals(factory)) {
            model = new JGroupsConfigModel();
        }
        else if (.equals(factory)) {
            model = new JMSConfigModel();
        }
        else {
            model = new CacheSyncConfigModel();
        }
        model.setFactoryClass(factory);
        model.addModelChangeListener(this);
        return model;
    }
    protected void prepareChildren(String factory) {
         = new HashMap();
        CacheSyncConfigDialog topView = (CacheSyncConfigDialoggetView();
        // note that none of the panels need a controller
        // if they issue controls, they will use this object taken from parent
        JGroupsConfigPanel jgroupsPanel = new JGroupsConfigPanel();
        topView.addCard(jgroupsPanel.);
        JMSConfigPanel jmsPanel = new JMSConfigPanel();
        topView.addCard(jmsPanel.);
        CustomRemoteEventsConfigPanel customFactoryPanel = new CustomRemoteEventsConfigPanel();
        .put(.customFactoryPanel);
        topView.addCard(customFactoryPanel.);
        if (factory == null) {
            factory = .;
        }
        // display the right initial card
        // can't call "changeConfigView", since it will reset custom factories..
        Object model = buildModel(factory);
        if (.equals(factory)) {
            jgroupsPanel.setBoundModel(model);
            ((CacheSyncConfigDialoggetView())
                    .showCard(.);
        }
        else if (.equals(factory)) {
            jmsPanel.setBoundModel(model);
            ((CacheSyncConfigDialoggetView())
                    .showCard(.);
        }
        else {
            customFactoryPanel.setBoundModel(model);
            ((CacheSyncConfigDialoggetView())
                    .showCard(.);
        }
    }
New to GrepCode? Check out our FAQ X