org.openthinclient.console.wizards.newdirobject.SelectSchemaPanel.java Source code

Java tutorial

Introduction

Here is the source code for org.openthinclient.console.wizards.newdirobject.SelectSchemaPanel.java

Source

/*******************************************************************************
 * openthinclient.org ThinClient suite
 * 
 * Copyright (C) 2004, 2007 levigo holding GmbH. All Rights Reserved.
 * 
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA.
 ******************************************************************************/
package org.openthinclient.console.wizards.newdirobject;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import org.openide.ErrorManager;
import org.openide.WizardDescriptor;
import org.openide.util.HelpCtx;
import org.openthinclient.common.model.DirectoryObject;
import org.openthinclient.common.model.Profile;
import org.openthinclient.common.model.Realm;
import org.openthinclient.common.model.schema.Schema;
import org.openthinclient.common.model.schema.provider.SchemaLoadingException;
import org.openthinclient.console.Messages;
import org.openthinclient.console.ValidateNames;
import org.openthinclient.ldap.DirectoryException;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;

@SuppressWarnings("serial")
public class SelectSchemaPanel extends JPanel implements WizardDescriptor.Panel {

    private javax.swing.JTextField descriptionTextField;

    private final Set<ChangeListener> listeners = new HashSet<ChangeListener>(1);

    private javax.swing.JTextField nameField;

    private javax.swing.JComboBox typeComboBox;

    private String[] types;

    private WizardDescriptor wd;

    private Class dirObjectClass;

    private ArrayList<String> existingNames;

    private static HashMap<Schema, String> typeKeyBySchema = new HashMap<Schema, String>();

    public SelectSchemaPanel() {
        setName(Messages.getString("NewDirObject.SelectSchemaPanel.name")); //$NON-NLS-1$
        initComponents();
    }

    public final void addChangeListener(ChangeListener l) {
        synchronized (listeners) {
            listeners.add(l);
        }
    }

    protected final void fireChangeEvent() {
        Iterator<ChangeListener> it;
        synchronized (listeners) {
            it = new HashSet<ChangeListener>(listeners).iterator();
        }
        final ChangeEvent ev = new ChangeEvent(this);
        while (it.hasNext())
            it.next().stateChanged(ev);
    }

    // Get the visual component for the panel. In this template, the component
    // is kept separate. This can be more efficient: if the wizard is created
    // but never displayed, or not all panels are displayed, it is better to
    // create only those which really need to be visible.
    public Component getComponent() {
        return this;
    }

    public HelpCtx getHelp() {
        // Show no Help button for this panel:
        return HelpCtx.DEFAULT_HELP;
        // If you have context help:
        // return new HelpCtx(SampleWizardPanel1.class);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    private void initComponents() {
        final DocumentListener dl = new DocumentListener() {
            public void changedUpdate(DocumentEvent e) {
                fireChangeEvent();
            }

            public void insertUpdate(DocumentEvent e) {
                fireChangeEvent();
            }

            public void removeUpdate(DocumentEvent e) {
                fireChangeEvent();
            }
        };
        final DefaultFormBuilder dfb = new DefaultFormBuilder(new FormLayout("r:p,3dlu,f:p:g,3dlu,p,3dlu,p"), //$NON-NLS-1$
                Messages.getBundle(), this);

        nameField = new javax.swing.JTextField();
        nameField.getDocument().addDocumentListener(dl);
        dfb.appendI15d("NewDirObject.SelectSchemaPanel.name_label", nameField); //$NON-NLS-1$
        dfb.nextLine();

        descriptionTextField = new javax.swing.JTextField();
        descriptionTextField.getDocument().addDocumentListener(dl);
        dfb.appendI15d("NewDirObject.SelectSchemaPanel.description_label", descriptionTextField); //$NON-NLS-1$
        dfb.nextLine();

        typeComboBox = new javax.swing.JComboBox();
        typeComboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                fireChangeEvent();
            }
        });

        dfb.appendI15d("NewDirObject.SelectSchemaPanel.type_label", typeComboBox); //$NON-NLS-1$
        dfb.nextLine();
    }

    @Override
    @SuppressWarnings({ "unchecked", "unchecked" })
    public boolean isValid() {
        if (null == wd)
            return false;

        wd.putProperty("WizardPanel_errorMessage", null); //$NON-NLS-1$

        if (nameField.getText().length() == 0) {
            wd.putProperty("WizardPanel_errorMessage", Messages //$NON-NLS-1$
                    .getString("NewDirObject.SelectSchemaPanel.name_error")); //$NON-NLS-1$
            return false;
        }

        if (typeComboBox.getSelectedIndex() == -1) {
            wd.putProperty("WizardPanel_errorMessage", Messages //$NON-NLS-1$
                    .getString("NewDirObject.SelectSchemaPanel.type_error")); //$NON-NLS-1$
            return false;
        }

        final ValidateNames validate = new ValidateNames();
        final String result = validate.validate(nameField.getText(), dirObjectClass);

        if (result != null) {
            wd.putProperty("WizardPanel_errorMessage", result); //$NON-NLS-1$
            return false;
        }

        final Realm realm = (Realm) wd.getProperty("realm"); //$NON-NLS-1$

        if (existingNames == null) {
            existingNames = new ArrayList<String>();
            try {
                if (realm != null && realm.getDirectory() != null) {
                    final Set<? extends DirectoryObject> existingObjects = realm.getDirectory()
                            .list(dirObjectClass);
                    for (final DirectoryObject existingObject : existingObjects)
                        existingNames.add(existingObject.getName());
                }
            } catch (final DirectoryException e) {
                ErrorManager.getDefault().annotate(e,
                        Messages.getString("NewDirObject.SelectSchemaPanel.list_existing_failed_error")); //$NON-NLS-1$
                ErrorManager.getDefault().notify(e);
            }
        }

        if (existingNames.contains(nameField.getText())) {
            wd.putProperty("WizardPanel_errorMessage", Messages //$NON-NLS-1$
                    .getString("NewDirObject.SelectSchemaPanel.name_exists_error")); //$NON-NLS-1$
            return false;
        }
        return true;
    }

    // You can use a settings object to keep track of state. Normally the
    // settings object will be the WizardDescriptor, so you can use
    // WizardDescriptor.getProperty & putProperty to store information entered
    // by the user.
    public void readSettings(Object settings) {
        wd = (WizardDescriptor) settings;

        dirObjectClass = (Class) wd.getProperty("type"); //$NON-NLS-1$
        final Realm realm = (Realm) wd.getProperty("realm"); //$NON-NLS-1$

        existingNames = null;

        // if (Profile.class.isAssignableFrom(type)) {
        try {

            types = realm.getSchemaProvider().getSchemaNames(dirObjectClass);
            typeKeyBySchema.clear();
            for (final String type : types) {
                final Schema schema = realm.getSchemaProvider().getSchema(dirObjectClass, type).getSchema();
                if (null != schema)
                    typeKeyBySchema.put(realm.getSchemaProvider().getSchema(dirObjectClass, type).getSchema(),
                            type);
            }

            final Schema[] schemas = typeKeyBySchema.keySet().toArray(new Schema[typeKeyBySchema.size()]);
            Arrays.sort(schemas);
            typeComboBox.setModel(new DefaultComboBoxModel(schemas));
        } catch (final SchemaLoadingException e) {
            ErrorManager.getDefault().annotate(e, ErrorManager.EXCEPTION,
                    Messages.getString("NewDirObject.schema_not_loaded_error"), null, //$NON-NLS-1$
                    null, null);
            ErrorManager.getDefault().notify(e);
        }
        typeComboBox.setEnabled(Profile.class.isAssignableFrom(dirObjectClass));
    }

    public final void removeChangeListener(ChangeListener l) {
        synchronized (listeners) {
            listeners.remove(l);
        }
    }

    public void storeSettings(Object settings) {
        wd = (WizardDescriptor) settings;

        wd.putProperty("name", nameField.getText()); //$NON-NLS-1$
        wd.putProperty("description", descriptionTextField.getText()); //$NON-NLS-1$
        wd.putProperty("schemaType", typeKeyBySchema.get(typeComboBox.getSelectedItem())); //$NON-NLS-1$
    }
}