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.url;


import java.awt.Component;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import javax.swing.event.ChangeListener;
import javax.swing.JComponent;

import org.openide.loaders.DataFolder;
import org.openide.loaders.DataObject;
import org.openide.loaders.TemplateWizard;
import org.openide.util.NbBundle;
import org.openide.WizardDescriptor;


/**
 * Wizard iterator used for new url objects. Has panel which allow
 * uset to set URL for new URLDataObject.
 *
 * @author  Peter Zavadsky
 * @see org.openide.loaders.TemplateWizard.Iterator
 * @see URLDataObject
 */
public class URLWizardIterator implements TemplateWizard.Iterator {


    /** Array of items. */
    private WizardDescriptor.Panel[] panels;

    /** Index into the array */
    private int index;

    /** Panel where url was set. It's used for retrieving the actual
     * url string value. */
    private URLPanel urlPanel;
    
    
    /** Creates new URLWizardIterator */
    public URLWizardIterator() {
    }
    

    /** Initialized this wizard iterator. Implements TemplateWizard.Iterator. */
    public void initialize(TemplateWizard wizard) {

        WizardDescriptor.Panel wizardPanel1 = wizard.targetChooser();
        WizardDescriptor.Panel wizardPanel2 = new URLPanel.URLWizardPanel();

        Component component1 = wizardPanel1.getComponent();
        urlPanel = (URLPanel)wizardPanel2.getComponent();
        
        if(component1 instanceof JComponent) {
            String[] contentData = (String[])((JComponent)component1).getClientProperty("WizardPanel_contentData"); // NOI18N
            
            if(contentData != null) {
                List list = new ArrayList(Arrays.asList(contentData));
                
                String urlDescription = NbBundle.getBundle(URLWizardIterator.class).getString("LBL_SetURL"); // NOI18N
                
                if(!list.contains(urlDescription))
                    list.add(urlDescription);
                
                ((JComponent)component1).putClientProperty("WizardPanel_contentData", list.toArray(new String[list.size()])); // NOI18N
            }   
                
        }
        
        panels = new WizardDescriptor.Panel[] {
            wizardPanel1,
            wizardPanel2
        };
    }

    /** Instantiates new url data object. Implements TemplateWizard.Iterator. */
    public Set instantiate(TemplateWizard wizard) throws IOException {
        String name = wizard.getTargetName ();
        DataFolder folder = wizard.getTargetFolder();
        DataObject template = wizard.getTemplate();
        DataObject dataObject = name == null ?
                         template.createFromTemplate(folder) :
                         template.createFromTemplate(folder, name);


        // Set URL.
        ((URLDataObject)dataObject).setURLString(urlPanel.getURLString());
                         
        return Collections.singleton(dataObject);
    }

    /** Uninitializes this wizard iterator. Implements TemplateWizard.Iteratot. */
    public void uninitialize(TemplateWizard wizard) {
        urlPanel = null;
    }

    
    /** Gets name of the current panel. Implements TemplateWizard.Iterator. */
    public String name() {
        Object[] args = {
            new Integer(index + 1),
            new Integer(panels.length)
        };
        
        MessageFormat mf = new MessageFormat(NbBundle.getBundle(URLWizardIterator.class).getString("CTL_ArrayIteratorName"));
        return mf.format(args);
    }
    
    
    /** Gets current panel. Implements TemplateWizard.Iterator. */
    public WizardDescriptor.Panel current() {
        return panels[index];
    }

    /** Tests if there a next panel. Implements TemplateWizard.Iterator. */
    public boolean hasNext () {
        return index < panels.length - 1;
    }

    /** Tests if there a previous panel. Implements TemplateWizard.Iterator. */
    public boolean hasPrevious () {
        return index > 0;
    }

    /** Moves to the next panel. Implements TemplateWizard.Iterator.
     * @exception NoSuchElementException if the panel does not exist */
    public synchronized void nextPanel () {
        if (index + 1 == panels.length) throw new NoSuchElementException();
        index++;
    }

    /** Moves to previous panel. Implements TemplateWizard.Iterator.
     * @exception NoSuchElementException if the panel does not exist */
    public synchronized void previousPanel () {
        if (index == 0) throw new NoSuchElementException();
        index--;
    }

    /** Dummy implementaion of TemplateWizard.Iterator method. */
    public void addChangeListener (ChangeListener l) {}

    /** Dummy implementaion of TemplateWizard.Iterator method. */
    public void removeChangeListener (ChangeListener l) {}
    
}
... 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.