Package org.cafesip.jiplet.console.client

Source Code of org.cafesip.jiplet.console.client.RealmUserAdminView

/*
* Created on Dec 13, 2008
*
* Copyright 2005 CafeSip.org
*
* Licensed 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.cafesip.jiplet.console.client;

import java.util.ArrayList;
import java.util.Arrays;

import org.cafesip.gwtcomp.client.ui.DataEntryPanel;
import org.cafesip.gwtcomp.client.ui.EditableList;
import org.cafesip.gwtcomp.client.ui.MessageBar;
import org.cafesip.gwtcomp.client.ui.TitleBar;
import org.cafesip.gwtcomp.client.utils.HTMLHelper;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
* @author Becky McElroy
*
*/
public class RealmUserAdminView extends DataEntryPanel
{
    private String realm;

    private TextBox name = new TextBox();

    private PasswordTextBox password = new PasswordTextBox();

    private PasswordTextBox confirmPassword = new PasswordTextBox();

    private EditableList roles;

    private Button findButton = new Button();

    private Button createButton = new Button();

    private Button modifyButton = new Button();

    private Button deleteButton = new Button();

    private Button resetButton = new Button();

    private Button addRoleButton = new Button();

    private Button removeRoleButton = new Button();

    public RealmUserAdminView(String realm)
    {
        super();
        this.realm = realm;

        setTitleBar(new TitleBar("User Management for Realm " + realm,
                GWT.getModuleBaseURL()
                + "help/realm_user_admin.html", 1));
        setMessageBar(new MessageBar());
        getMessageBar().setMessage("Enter an User Name to find or create");

        FlowPanel namePanel = new FlowPanel();
        namePanel.setWidth("100%");
        namePanel.add(name);
        findButton.setHTML(HTMLHelper.imageWithText(GWT.getModuleBaseURL()
                + "gwtcomp-icons/viewmag.png", " Find User "));
        namePanel.add(findButton);
        addField(new Label("User Name"), namePanel);

        addField(new Label("Password"), password);
        addField(new Label("Re-type Password"), confirmPassword);

        addField(new HTML(HTMLHelper.newline()));
        addField(new HTML(HTMLHelper.newline()));

        addRoleButton.setHTML(HTMLHelper.imageWithText(GWT.getModuleBaseURL()
                + "gwtcomp-icons/edit_add.png", "Add Role"));
        addRoleButton.setTitle("Add a role to the list");

        removeRoleButton.setHTML(HTMLHelper.imageWithText(GWT
                .getModuleBaseURL()
                + "gwtcomp-icons/editdelete.png", "Remove Role(s)"));
        removeRoleButton.setTitle("Remove selected role(s) from list");

        roles = new EditableList(null, addRoleButton, null, removeRoleButton);
        roles.setMessageBar(getMessageBar());
        addField(new Label("Assigned Roles"), roles);

        createButton.setHTML(HTMLHelper.imageWithText(GWT.getModuleBaseURL()
                + "gwtcomp-icons/edit_add.png", " Create "));
        addButton(createButton);

        modifyButton.setHTML(HTMLHelper.imageWithText(GWT.getModuleBaseURL()
                + "gwtcomp-icons/edit.png", " Modify "));
        addButton(modifyButton);

        deleteButton.setHTML(HTMLHelper.imageWithText(GWT.getModuleBaseURL()
                + "gwtcomp-icons/editdelete.png", " Delete "));
        addButton(deleteButton);

        resetButton.setHTML(HTMLHelper.imageWithText(GWT.getModuleBaseURL()
                + "gwtcomp-icons/reload.png", " Reset "));
        addButton(resetButton);

        initListeners();
    }

    /**
     *
     */
    private void initListeners()
    {
        name.addKeyboardListener(new KeyboardListenerAdapter()
        {
            public void onKeyUp(Widget sender, char keyCode, int modifiers)
            {
                if (name.getText().trim().length() > 0)
                {
                    findButton.setEnabled(true);
                    createButton.setEnabled(true);
                    addRoleButton.setEnabled(true);
                    removeRoleButton.setEnabled(true);
                }
                else
                {
                    findButton.setEnabled(false);
                    createButton.setEnabled(false);
                    addRoleButton.setEnabled(false);
                    removeRoleButton.setEnabled(false);
                }
            }
        });

        findButton.addClickListener(new ClickListener()
        {
            public void onClick(Widget sender)
            {
                RealmController.getInstance().findUser(realm,
                        name.getText().trim());
            }
        });

        createButton.addClickListener(new ClickListener()
        {
            public void onClick(Widget sender)
            {
                ArrayList<String> errors = validateCreate();
                if (errors.size() > 0)
                {
                    ErrorDisplay.formatErrors(errors, getMessageBar());
                    return;
                }

                String[] roleList = roles.getList().toArray(
                        new String[roles.getList().size()]);
                RealmController.getInstance().createUser(realm,
                        name.getText().trim(), password.getText().trim(),
                        roleList);
            }
        });

        modifyButton.addClickListener(new ClickListener()
        {
            public void onClick(Widget sender)
            {
                ArrayList<String> errors = validateModify();
                if (errors.size() > 0)
                {
                    ErrorDisplay.formatErrors(errors, getMessageBar());
                    return;
                }

                String[] roleList = roles.getList().toArray(
                        new String[roles.getList().size()]);
                RealmController.getInstance().modifyUser(realm,
                        name.getText().trim(), password.getText().trim(),
                        roleList);
            }
        });

        deleteButton.addClickListener(new ClickListener()
        {
            public void onClick(Widget sender)
            {
                if (Window.confirm("Are you sure you want to delete user "
                        + name.getText().trim() + '?') == false)
                {
                    return;
                }

                RealmController.getInstance().deleteUser(realm,
                        name.getText().trim());

            }
        });

        resetButton.addClickListener(new ClickListener()
        {
            public void onClick(Widget sender)
            {
                getMessageBar().setMessage(
                        "Enter an User Name to find or create");
                reset();
            }
        });
    }

    private ArrayList<String> validateCreate()
    {
        ArrayList<String> errors = new ArrayList<String>();

        if (name.getText().trim().length() == 0)
        {
            errors.add("The user name must be specified");
        }

        if (password.getText().trim().length() == 0)
        {
            errors.add("A password must be specified");
        }

        if (!password.getText().trim().equals(confirmPassword.getText().trim()))
        {
            errors.add("The passwords must match");
        }

        if (roles.getList().size() == 0)
        {
            errors
                    .add("You must specify at least one role for the Create operation");
        }

        return errors;
    }

    private ArrayList<String> validateModify()
    {
        ArrayList<String> errors = new ArrayList<String>();

        if ((password.getText().length() > 0)
                || (confirmPassword.getText().length() > 0))
        {
            if (!password.getText().trim().equals(
                    confirmPassword.getText().trim()))
            {
                errors.add("The passwords, if specified, must match");
            }
        }

        if (roles.getList().size() == 0)
        {
            errors.add("At least one role must be specified for the user");
        }

        return errors;
    }

    public void reset()
    {
        name.setText("");
        name.setEnabled(true);
        password.setText("");
        confirmPassword.setText("");
        roles.setList(new ArrayList<String>());

        findButton.setEnabled(false);
        createButton.setEnabled(false);
        modifyButton.setEnabled(false);
        deleteButton.setEnabled(false);
        addRoleButton.setEnabled(false);
        removeRoleButton.setEnabled(false);
    }

    public void displayForm()
    {
        reset();
    }

    public void displayRoles(String[] userroles)
    {
        roles.setList(new ArrayList<String>(Arrays.asList(userroles)));
        allowModifyDelete();
    }

    private void allowModifyDelete()
    {
        createButton.setEnabled(false);
        modifyButton.setEnabled(true);
        deleteButton.setEnabled(true);

        name.setEnabled(false);
        getMessageBar().setMessage("User found");
    }
}
TOP

Related Classes of org.cafesip.jiplet.console.client.RealmUserAdminView

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.