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-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.core.ui;

import java.util.*;
import java.text.MessageFormat;
import java.awt.Component;
import java.awt.Container;
import java.io.IOException;
import java.beans.*;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.AWTEvent;
import java.awt.Toolkit;

import org.openide.ErrorManager;
import org.openide.WizardDescriptor;
import org.openide.cookies.InstanceCookie;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.Repository;
import org.openide.loaders.DataFolder;
import org.openide.loaders.DataObject;

import org.netbeans.core.Splash;

/** Setup wizard which uses CompoundIterator. When Module selection panel should be
 * displayed then it is placed at the first position.
 *
 * @author  jrojcek
 */
public class SetupWizard extends WizardFolder implements PropertyChangeListener //, AWTEventListener 
{

    /** See org.openide.WizardDescriptor.PROP_AUTO_WIZARD_STYLE
     */
    private static final String PROP_AUTO_WIZARD_STYLE = "WizardPanel_autoWizardStyle"; // NOI18N
    /** See org.openide.WizardDescriptor.PROP_CONTENT_DISPLAYED
     */
    private static final String PROP_CONTENT_DISPLAYED = "WizardPanel_contentDisplayed"; // NOI18N
    /** See org.openide.WizardDescriptor.PROP_CONTENT_NUMBERED
     */
    private static final String PROP_CONTENT_NUMBERED = "WizardPanel_contentNumbered"; // NOI18N
    /** See org.openide.WizardDescriptor.PROP_CONTENT_SELECTED_INDEX
     */
    private static final String PROP_CONTENT_SELECTED_INDEX = "WizardPanel_contentSelectedIndex"; // NOI18N
    /** See org.openide.WizardDescriptor.PROP_HELP_DISPLAYED
     */
    private static final String PROP_HELP_DISPLAYED = "WizardPanel_helpDisplayed"; // NOI18N
    /** See org.openide.WizardDescriptor.PROP_CONTENT_DATA
     */
    private static final String PROP_CONTENT_DATA = "WizardPanel_contentData"; // NOI18N
    /** 
     * Name of the property attached to file objects in the folder
     * indicating that the panel has already been successfully shown.
     * The type of the property is java.util.Date
     */
    private static final String DATE_SHOWN_PROPERTY = "dateShown"; // NOI18N
    /**
     * Name of the property attached to file objects in the folder
     * indicating module which placed that file object into the folder.
     * The type of the property is java.util.String
     */
    private static final String MODULE_ATTR = "module"; // NOI18N
    /**
     * Name of the property attached to file objects in the folder
     * indicating the panel is hidden and will not be showed in wizard.
     * The type of the property is java.lang.Boolean
     */
    private static final String HIDDEN_ATTR = "hidden"; // NOI18N
    /**
     * Name of the property attached to file objects in the folder
     * indicating whether panel should be displayed in basic or advanced part of the wizard
     * The type of the property is java.lang.Boolean
     */
    private static final String BASIC_ATTR = "basic"; // NOI18N
    /**
     * If showAll is true the wizard will show all the panels regardless
     * whether the user has already seen them.
     */
    private boolean showAll = false;
    /* whether folder can be reacreated or not */
    private boolean lock = false;    
    /* Map (InstanceCookie -> WizardDescriptor.Iterator) */
    private HashMap subIterators;

    /* Setup wizard iterator */
    private CompoundIterator cit;
    /* Setup wizard descriptor */
    private WizardDescriptor wd;
    /* Separator between basic and advanced part of wizard */
    private WizardDescriptor.Iterator separator;
    
    private static ModuleBean.AllModulesBean allModules = ModuleBean.AllModulesBean.getDefault();
    
    public SetupWizard(DataFolder df) {
        super(df);
    }
    
//    /** Called on every focus event when SetupWizard is displayed */
//    public void eventDispatched (AWTEvent ev) {
//        System.out.println("event dispatched..");
//        Component c, root;
//        if (ev.getID() == FocusEvent.FOCUS_GAINED) {
//            FocusEvent fev = (FocusEvent) ev;
//            c = fev.getComponent();
//            System.out.println("focus gained=" + c.getClass());
//            //Bugfix #23588: Try to transfer focus to visible component of the 
//            //same parent if focused component is not visible.
//            if (!c.isVisible()) {
//                Component parent = c.getParent();
//                if (parent != null) {
//                    Component [] comps = ((Container) parent).getComponents();
//                    for (int i = 0; i < comps.length; i++) {
//                        if (comps[i].isVisible()) {
//                            System.out.println("event dispatched:requesting focus");
//                            comps[i].requestFocus();
//                            break;
//                        }
//                    }
//                }
//            }
//        }
//    }
    
    /** Overridden to assure that IDESettingsPanel and ModuleSelectionPanel will be on the first place.
     */ 
    protected Object createInstance(InstanceCookie[] cookies) throws java.io.IOException, ClassNotFoundException {
        // folder can be recreated only if module selection panel is displayed
        synchronized (this) {
            if (lock)
                return this;
            LinkedList basic = new LinkedList();
            LinkedList advanced = new LinkedList();
            if (subIterators == null)
                 subIterators = new HashMap();
            
            for (int i = 0; i < cookies.length; i++) {
                WizardDescriptor.Iterator subIter = (WizardDescriptor.Iterator)subIterators.get(cookies[i]);
                Object obj[] = (Object[])cookies[i].instanceCreate();
                if (subIter == null) {
                    if (obj[0] instanceof org.openide.WizardDescriptor.Iterator) {
                        subIter = (WizardDescriptor.Iterator)obj[0];
                    } else if (obj[0] instanceof org.openide.WizardDescriptor.Panel) {
                        WizardDescriptor.Panel p = (WizardDescriptor.Panel)obj[0];
                        subIter = new PanelWrapper(p, cookies[i].instanceName());
                    } else if (obj[0] instanceof java.awt.Component) {
                        java.awt.Component c = (java.awt.Component)obj[0];
                        subIter = new PanelWrapper(c, cookies[i].instanceName());
                    } else {
                        continue;
                    }
                }
                subIterators.put(cookies[i], subIter);
                if (Boolean.TRUE.equals(((DataObject)obj[1]).getPrimaryFile().getAttribute(BASIC_ATTR)))
                    basic.add(subIter);
                else
                    advanced.add(subIter);
            }
            if (!showAll && !advanced.isEmpty() && !basic.isEmpty())
                basic.add(getSeparatorPanel()); //NOI18N
            basic.addAll(advanced);
            
            if (wd == null) {
                if (basic.isEmpty())
                    cit = null;
                else
                    setIterator(new InitializedIterator(basic));
            } else
                cit.setIterators(basic);
        }
        
        return this;
    }

    /**
     * Overridden to keep track of last visits property. It returns
     * null if DATE_SHOWN_PROPERTY for the primary file of the DataObject
     * is set.
     */
    protected InstanceCookie acceptDataObject(DataObject dob) {
        InstanceCookie cookie = super.acceptDataObject(dob);
        if (cookie != null) {
            FileObject fo = dob.getPrimaryFile();
            Date lastVisit = (Date)fo.getAttribute(DATE_SHOWN_PROPERTY);
            String moduleName = (String)fo.getAttribute(MODULE_ATTR);
            Boolean hidden = (Boolean)fo.getAttribute(HIDDEN_ATTR);
            if (   Boolean.TRUE.equals(hidden)
                || ((lastVisit != null) && (!showAll))
                || skipping(moduleName)/*((toDisable != null) && (toDisable.contains(moduleName)))*/) {
                // [PENDING] last visit is tested here only for presence
                // the fact that it is a date is not used (yet?)
                return null;
            }
        }
        return cookie;
    }
    
    private boolean skipping(String moduleName) {
        if (moduleName == null) return false;
        ModuleBean[] modules = allModules.getModules();
        for (int i = 0; i < modules.length; i++) {
            if (modules[i].getCodeNameBase().equals(moduleName)) {
                return ! modules[i].isEnabled();
            }
        }
        // What is going on?
        return false;
    }

    /**
     * Overriden to return Object[] {Object (instance for cookie), DataObject}. 
     * DataObject's primary file object attributes are used in createInstance.
     */
    protected Object instanceForCookie (DataObject obj, InstanceCookie cookie)
    throws IOException, ClassNotFoundException {
        return new Object[] {cookie.instanceCreate (), obj};
    }

    /**
     * This method modifies the the folder in such a way that
     * panels already shown will not be shown next time.
     * It sets the attribute DATE_SHOWN_PROPERTY of all file objects
     * in the folder.
     */
    protected void markPanelsAsSeen() {
        Date now = new Date();
        FileObject fo[] = folder.getPrimaryFile().getChildren();
        for (int i = 0; i < fo.length; i++) {
            try {
                fo[i].setAttribute(DATE_SHOWN_PROPERTY, now); 
            } catch (IOException ioe) {
                ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
            }
        }
    }

    /** 
     * If showAll is true the wizard will show all the panels regardless
     * whether the user has already seen them.
     * @return Value of property showAll.
     */
    public boolean isShowAll() {
        return showAll;
    }

    /** 
     * If showAll is true the wizard will show all the panels regardless
     * whether the user has already seen them.
     * @param showAll New value of property showAll.
     */
    public void setShowAll(boolean showAll) {
        this.showAll = showAll;
        run();
    }

    /** Helper method to set iterator and its listeners
     */
    private void setIterator(CompoundIterator it) {
        if (cit != null)
            cit.removePropertyChangeListener(this);
        cit = it;
        if (cit != null)
            cit.addPropertyChangeListener(this);
    }
    
    /**
     * @return WizardDescriptor.Iterator auctually used iterator.
     */
    public WizardDescriptor.Iterator getIterator() {
        return cit;
    }

    /** Show setup wizard if in folder is at least one file
     */
    public void show() {
        show(null);
    }
        
    /** Show setup wizard if in folder is at least one file
     */
    public void show(Splash.SplashOutput splash) {
//        Toolkit.getDefaultToolkit().addAWTEventListener(
//            this, 
//            AWTEvent.FOCUS_EVENT_MASK
//        );
        if (cit == null)
            return;
        synchronized (this) {
            lock = true;
        }
        if (splash != null) {
            Splash.hideSplash(splash);
        }
        wd = new InitializedWizardDescriptor(cit);  
        wd.putProperty(PROP_AUTO_WIZARD_STYLE, Boolean.TRUE);
        wd.putProperty(PROP_CONTENT_DISPLAYED, Boolean.TRUE);
        wd.putProperty(PROP_CONTENT_NUMBERED, Boolean.TRUE);
        wd.setTitleFormat(new MessageFormat("{0}")); // NOI18N
        wd.setTitle(org.openide.util.NbBundle.getBundle(WizardFolder.class)
            .getString("CTL_SetupWizardTitle")); // NOI18N
        wd.putProperty(PROP_CONTENT_DATA, cit.getContentData());
        wd.putProperty(PROP_CONTENT_SELECTED_INDEX, new Integer(cit.getContentSelectedIndex()));
        wd.putProperty (PROP_HELP_DISPLAYED, Boolean.FALSE);

        allModules.addPropertyChangeListener(this);
        ModuleBean[] modules = allModules.getModules();
        for (int i = 0; i < modules.length; i++) {
            modules[i].addPropertyChangeListener(this);
        }
        java.awt.Dialog d = org.openide.DialogDisplayer.getDefault().createDialog(wd);
        d.show();
        d.dispose();
        allModules.removePropertyChangeListener(this);
        modules = allModules.getModules();
        for (int i = 0; i < modules.length; i++) {
            modules[i].removePropertyChangeListener(this);
        }
        cit.removePropertyChangeListener(this);
        if (wd.getValue () != wd.CANCEL_OPTION && wd.getValue () != wd.CLOSED_OPTION) {
            markPanelsAsSeen();
        }
//        Toolkit.getDefaultToolkit().removeAWTEventListener(this);
    }
    
    /** Updates wizard not showing panels from modules which will be disabled
     */
    private void updateAdvancedWizard() {
        lock = false;
        run();
        synchronized (this) {
            lock = true;
        }
        wd.putProperty(PROP_CONTENT_DATA, cit.getContentData());
    }
    
    /** Listens on content property changes in Compound iterator. And updates Wizard
     * descriptor properties.
     */
    public void propertyChange(PropertyChangeEvent ev) {
        if (PROP_CONTENT_SELECTED_INDEX.equals(ev.getPropertyName()))
            wd.putProperty(PROP_CONTENT_SELECTED_INDEX,
                new Integer(cit.getContentSelectedIndex()));
        else if (PROP_CONTENT_DATA.equals(ev.getPropertyName()))
            wd.putProperty(PROP_CONTENT_DATA, cit.getContentData());
        else if (allModules == ev.getSource() || "enabled".equals(ev.getPropertyName())) // NOI18N
            // module enablement changed perhaps...
            updateAdvancedWizard();
    }

    /* Getter for separator panel */
    private WizardDescriptor.Iterator getSeparatorPanel() {
        if (separator == null) {
            JPanel panel = new JPanel(new java.awt.BorderLayout());
            JLabel label = new JLabel(org.openide.util.NbBundle.getBundle(SetupWizard.class).getString("LBL_SeparatorPanelInstr")); // NOI18N
            panel.add(label, java.awt.BorderLayout.NORTH);
            panel.setName(org.openide.util.NbBundle.getBundle(SetupWizard.class).getString("LBL_SeparatorPanelName")); // NOI18N
            panel.getAccessibleContext().setAccessibleDescription(label.getText());
            separator = new PanelWrapper(panel, ""); // NOI18N
        }
        return separator;
    }

    /** Shows the setup wizard if there is a folder named Wizards/Setup
     * under system folder. When the user hits the finish button
     * the files in the folder are marked as shown. So the wizard should
     * show each entry exactly once.
     * @param boolean showAll if showAll is true all the panels are shown regardless
     * whether they were shown before or not
     */
    public static void showSetupWizard(boolean showAll) {
        showSetupWizard(showAll, null);
    }
    
    /** Shows the setup wizard if there is a folder named Wizards/Setup
     * under system folder. When the user hits the finish button
     * the files in the folder are marked as shown. So the wizard should
     * show each entry exactly once.
     * @param boolean showAll if showAll is true all the panels are shown regardless
     * whether they were shown before or not
     * @param splash The reference to splash screen used for hiding splash screen
     * when wizard appears
     */
    public static void showSetupWizard(boolean showAll, Splash.SplashOutput splash) {
        org.openide.filesystems.FileSystem fs = Repository.getDefault().getDefaultFileSystem ();
        org.openide.filesystems.FileObject fo = fs.findResource("Wizards/Setup"); // NOI18N
        if (fo != null) {
            SetupWizard f = new SetupWizard(DataFolder.findFolder(fo));
            f.setShowAll(showAll);
            f.show(splash);
        }
    }
    
    /* List of panels already initialized */
    private HashSet initializedPanels = new HashSet();

    private class InitializedIterator extends CompoundIterator {
        
        /* Property which signals to wizard panel that it should initialize its state .
         * Wizard panel should in readSettings() call wizardDescriptor.getProperty() 
         * and if Boolean.TRUE then init its state (new wizard is running)
         * otherwise do nothing (same wizard session).
         */
        private static final String PROP_INITIALIZE = "initializePanel"; // NOI18N
        
        /** It assumes that all elements of the array are non-null. */
        public InitializedIterator(LinkedList iterators) {
            super(iterators);
        }
        
        /** Overriden to allow set PROP_INITIALIZE property. */
        public WizardDescriptor.Panel current() {
            WizardDescriptor.Panel current = super.current();
            wd.putProperty(PROP_INITIALIZE, initializedPanels.contains(current)
                                            ? Boolean.FALSE
                                            : Boolean.TRUE);
            return current;
        }
    }

    private class InitializedWizardDescriptor extends WizardDescriptor {
        
        public InitializedWizardDescriptor(WizardDescriptor.Iterator iter) {
            super(iter);
        }
        
        /** Overriden to insert current panel into set of initialized panels */
        protected void updateState() {
            super.updateState();
            initializedPanels.add(cit.current());
        }
    }
}
... 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.