alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.xml.core.cookies;

import java.awt.Dialog;
import java.io.Serializable;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

import org.netbeans.modules.xml.api.scenario.Scenario;
import org.netbeans.modules.xml.core.scenario.ScenarioPanel;
import org.openide.DialogDescriptor;
import org.openide.NotifyDescriptor;
import org.openide.DialogDisplayer;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObject;
import org.openide.util.HelpCtx;
import org.netbeans.modules.xml.api.cookies.ScenarioCookie;

/**
 * Implements the ScenarioCookie.
 * This class manages the scenarios for a particular DataObject.
 * The Scenarios are stored persistently in attribute of the primary FileObject
 * of this data object.
 *
 * @author  asgeir@dimonsoftware.com
 */
public class ScenarioSupport implements ScenarioCookie {
    
//     public static final String OLD_SCENARIO_DATA_ATTRIBUTE = "org.netbeans.modules.xsl.scenario.ScenarioSupport.Data";
    public static final String SCENARIO_DATA_ATTRIBUTE = "org.netbeans.modules.xml.core.cookies.ScenarioData";
    
    /** Owning dataObject */
    private DataObject dataObject;
    
    /** The ComboBoxModel model which describes the available scenarios and the active scenario */
    private DefaultComboBoxModel scenarioModel;
    
    /** The ScenarioData which will be stored persistently
     * @link aggregation*/
    private ScenarioData data;
    
    /** @link dependency */
    /*# ScenarioPanel lnkScenarioPanel; */
    
    /** @link dependency */
    /*# Scenario lnkScenario; */
    
    /** Creates a new instance of ScenarioSupport */
    public ScenarioSupport(DataObject dataObject) {
        this.dataObject = dataObject;
    }
    
    /**
     * Displays the Customize scenarios dialog
     * @return true if the Customize dialog was pressed with the OK button,
     *         but false if it was closed with the Cancel button.
     */
    public boolean customizeScenarios() {
        
        DefaultComboBoxModel model = getModel();
        ScenarioPanel scenarioPanel = new ScenarioPanel(dataObject, model);
        DialogDescriptor scenariosDD = new DialogDescriptor
        (scenarioPanel,
        Util.THIS.getString("NAME_customize_scenarios_title"), true,
        DialogDescriptor.OK_CANCEL_OPTION, DialogDescriptor.OK_OPTION,
        DialogDescriptor.BOTTOM_ALIGN,
        new HelpCtx(ScenarioSupport.class), null);
        scenariosDD.setClosingOptions(new Object[] { DialogDescriptor.OK_OPTION, DialogDescriptor.CANCEL_OPTION });
        
        Dialog dialog = DialogDisplayer.getDefault().createDialog(scenariosDD);
        
        // inlined ScenarioPanel.addNotify() content
        if (model.getSize() == 0) {
            // Displays the add-scenario dialog is no scenarios are found.
            if (!scenarioPanel.createScenario()) {
                return false;
            }
        }
        
        dialog.show();
        
        if (scenariosDD.getValue() == DialogDescriptor.OK_OPTION) {
            for (int ind = 0; ind < model.getSize(); ind++) {
                Scenario scenario = (Scenario)model.getElementAt(ind);
                scenario.saveChanges();
            }
            saveData();
            return true;
        } else {
            // cancel all changes made
            scenarioPanel.rollBackAddedAndRemovedScenarios();
        }
        
        return false;
    }
    
    /**
     * Returns the ComboBoxModel that represents the a list of scenarios.
     * The active scenario is the selected item in that ComboBoxModel
     * @return The ComboBoxModel that represents the a list of scenarios.
     *         The active scenario is the selected item in that ComboBoxModel.
     */
    public DefaultComboBoxModel getModel() {
        if (scenarioModel == null) {
            // Create a new DefaultComboBoxModel which listens to selection changes
            // and saves the data when selection is changed.
            scenarioModel = new DefaultComboBoxModel(getData().getScenarios()) {
                public void setSelectedItem(Object anItem) {
                    super.setSelectedItem(anItem);
                    saveData();
                }
            };
            
            // Set the initial selected item
            scenarioModel.setSelectedItem(scenarioModel.getElementAt(getData().getActiveScenarioIndex()));
            
            // Listener to change events in the scenarioModel and save the
            // data when the model changes.
            scenarioModel.addListDataListener(new ListDataListener() {
                public void contentsChanged(ListDataEvent e) {
                    saveData();
                }
                public void intervalAdded(ListDataEvent e) {
                    saveData();
                }
                public void intervalRemoved(ListDataEvent e) {
                    saveData();
                }
            });
        }
        return scenarioModel;
    }
    
    /**
     * Executes an object using the active scenario.
     * If no scenario is active, the Customize scenarios dialog is displayed
     * before execution.
     */
    public void executeActiveScenario() {
        DefaultComboBoxModel model = getModel();
        Scenario activeScenario = (Scenario)model.getSelectedItem();
        
        if (activeScenario == null) {
            // No scenario active -> let's display the customize dialog
            if (customizeScenarios() == true) {
                activeScenario = (Scenario)model.getSelectedItem();
            }
        }
        
        if (activeScenario != null) {
            activeScenario.execute(dataObject);
        }
    }
    
    /**
     * Serialize the scenario model to the an attribute of the primary FileObject
     * of the DataObject.
     */
    private void saveData() {
        if (scenarioModel == null || data == null) {
            return; // Nothing to save
        }
        
        try {
            data.setActiveScenarioIndex(scenarioModel.getIndexOf(scenarioModel.getSelectedItem()));
            
            FileObject fileObject = dataObject.getPrimaryFile();
            if (fileObject != null) {
                fileObject.setAttribute(SCENARIO_DATA_ATTRIBUTE, data);
            }
        } catch(java.io.IOException e) {
            String message = Util.THIS.getString("MSG_Problems_saving_scenarios_persistently") + "\n" + e.getMessage();
            NotifyDescriptor nd = new NotifyDescriptor.Message(message, NotifyDescriptor.WARNING_MESSAGE);
            DialogDisplayer.getDefault().notify(nd);
        }
    }
    
    /**
     * Retuns the ScenarioData object which describes the scenarios for this
     * DataObject.  If it has not been loaded yet, it is loaded from an
     * attribute of the primary FileObject.
     * @return the ScenarioData object, never null.
     */
    private ScenarioData getData() {
        if (data == null) {
            FileObject fileObject = dataObject.getPrimaryFile();
            if (fileObject != null) {
                Object objData = fileObject.getAttribute(SCENARIO_DATA_ATTRIBUTE);
                if (objData instanceof ScenarioData) {
                    data = (ScenarioData) fileObject.getAttribute(SCENARIO_DATA_ATTRIBUTE);
                }
            }
            if (data == null ) {
                data = new ScenarioData();
            }
        }
        return data;
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.