Package org.jdesktop.wonderland.modules.securitygroups.client

Source Code of org.jdesktop.wonderland.modules.securitygroups.client.GroupEditorFrame

/**
* Project Wonderland
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., All Rights Reserved
*
* Redistributions in source code form must reproduce the above
* copyright and this condition.
*
* The contents of this file are subject to the GNU General Public
* License, Version 2 (the "License"); you may not use this file
* except in compliance with the License. A copy of the License is
* available at http://www.opensource.org/licenses/gpl-license.php.
*
* Sun designates this particular file as subject to the "Classpath"
* exception as provided by Sun in the License file that accompanied
* this code.
*/
package org.jdesktop.wonderland.modules.securitygroups.client;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.ResourceBundle;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.xml.bind.JAXBException;
import org.jdesktop.wonderland.common.login.CredentialManager;
import org.jdesktop.wonderland.modules.securitygroups.common.GroupDTO;
import org.jdesktop.wonderland.modules.securitygroups.common.GroupUtils;
import org.jdesktop.wonderland.modules.securitygroups.common.MemberDTO;

/**
*
* @author jkaplan
* @author Ronny Standtke <ronny.standtke@fhnw.ch>
*/
public class GroupEditorFrame extends JFrame implements ListSelectionListener {

    private static final ResourceBundle BUNDLE = ResourceBundle.getBundle(
            "org/jdesktop/wonderland/modules/securitygroups/client/Bundle");
    private String baseUrl;
    private CredentialManager cm;
    private DefaultTableModel tableModel;
    private boolean isAdd;
    private GroupManagerFrame parent;

    /** Creates new form GroupEditorFrame */
    public GroupEditorFrame(GroupManagerFrame parent,
            String baseUrl, GroupDTO group,
            CredentialManager cm) {
        this.parent = parent;
        this.baseUrl = baseUrl;
        this.cm = cm;

        initComponents();
        tableModel = new javax.swing.table.DefaultTableModel(
            new Object[][] {},
            new String[]{
                BUNDLE.getString("Username"),
                BUNDLE.getString("Owner")
            }
        ) {
            Class[] types = new Class[]{
                String.class,
                Boolean.class
            };
            boolean[] canEdit = new boolean[]{
                false,
                true
            };

            @Override
            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        };
        memberTable.setModel(tableModel);

        memberTable.getSelectionModel().addListSelectionListener(this);

        // if the group is null, this is a new group we are creating a
        // new group
        if (group == null) {
            isAdd = true;
            okButton.setText(BUNDLE.getString("Create"));
            tableModel.addRow(new Object[]{cm.getUsername(), true});
            setTitle(BUNDLE.getString("New_Group"));
        } else {
            isAdd = false;
            groupnameTF.setText(group.getId());
            groupnameTF.setEditable(false);
            populateTable(group);
            String title = BUNDLE.getString("Edit_Group");
            title = MessageFormat.format(title, group.getId());
            setTitle(title);
        }
    }

    protected void populateTable(GroupDTO group) {
        try {
            // get the full data (including members) for this group
            group = GroupUtils.getGroup(baseUrl, group.getId(), cm);

            // now add each member to the table
            for (MemberDTO member : group.getMembers()) {
                tableModel.addRow(new Object[]{member.getId(),
                            member.isOwner()});
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (JAXBException je) {
            je.printStackTrace();
        }
    }

    protected GroupDTO toGroup() {
        GroupDTO group = new GroupDTO(groupnameTF.getText());

        for (int i = 0; i < tableModel.getRowCount(); i++) {
            String name = (String) tableModel.getValueAt(i, 0);
            Boolean owner = (Boolean) tableModel.getValueAt(i, 1);

            group.getMembers().add(new MemberDTO(name, owner));
        }

        return group;
    }

    public void valueChanged(ListSelectionEvent e) {
        boolean removable = !e.getValueIsAdjusting() && e.getFirstIndex() >= 0;
        removeButton.setEnabled(removable);
    }

    /** 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        memberTable = new javax.swing.JTable();
        addButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        groupnameTF = new javax.swing.JTextField();

        jScrollPane1.setFont(new java.awt.Font("Dialog", 0, 13));

        memberTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(memberTable);

        addButton.setFont(new java.awt.Font("Dialog", 0, 13));
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/jdesktop/wonderland/modules/securitygroups/client/Bundle"); // NOI18N
        addButton.setText(bundle.getString("GroupEditorFrame.addButton.text")); // NOI18N
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        removeButton.setFont(new java.awt.Font("Dialog", 0, 13));
        removeButton.setText(bundle.getString("GroupEditorFrame.removeButton.text")); // NOI18N
        removeButton.setEnabled(false);
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        cancelButton.setFont(new java.awt.Font("Dialog", 0, 13));
        cancelButton.setText(bundle.getString("GroupEditorFrame.cancelButton.text")); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        okButton.setFont(new java.awt.Font("Dialog", 0, 13));
        okButton.setText(bundle.getString("GroupEditorFrame.okButton.text")); // NOI18N
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Dialog", 0, 13));
        jLabel1.setText(bundle.getString("GroupEditorFrame.jLabel1.text")); // NOI18N

        groupnameTF.setFont(new java.awt.Font("Dialog", 0, 13));

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 503, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(groupnameTF, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(addButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(removeButton))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(cancelButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(okButton)))
                .addContainerGap())
        );

        layout.linkSize(new java.awt.Component[] {cancelButton, okButton}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(groupnameTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(removeButton)
                    .add(addButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(okButton)
                    .add(cancelButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        try {
            // if we are adding a group, check to see if we are overwriting
            // an existing group
            if (isAdd) {
                GroupDTO existing = GroupUtils.getGroup(baseUrl,
                        groupnameTF.getText(),
                        cm);
                if (existing != null) {
                    String groupName = groupnameTF.getText();
                    String message = BUNDLE.getString("Group_Exists_Message");
                    message = MessageFormat.format(message, groupName);
                    String title = BUNDLE.getString("Group_Exists_Title");
                    JOptionPane.showMessageDialog(this, message, title,
                            JOptionPane.ERROR_MESSAGE);
                    return;
                }
            }

            GroupUtils.updateGroup(baseUrl, toGroup(), cm);
            dispose();
            parent.loadGroups();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (JAXBException je) {
            je.printStackTrace();
        }
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        String username = JOptionPane.showInputDialog(this,
                BUNDLE.getString("Enter_User_Name"),
                BUNDLE.getString("Add_User"),
                JOptionPane.QUESTION_MESSAGE);
        tableModel.addRow(new Object[]{username, false});
    }//GEN-LAST:event_addButtonActionPerformed

    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
        tableModel.removeRow(memberTable.getSelectedRow());
    }//GEN-LAST:event_removeButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField groupnameTF;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable memberTable;
    private javax.swing.JButton okButton;
    private javax.swing.JButton removeButton;
    // End of variables declaration//GEN-END:variables
}
TOP

Related Classes of org.jdesktop.wonderland.modules.securitygroups.client.GroupEditorFrame

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.