// Copyright 2004-2014 Jim Voris
//
// 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 com.qumasoft.guitools.admin;
import com.qumasoft.guitools.AbstractQVCSCommandDialog;
import com.qumasoft.qvcslib.response.ServerResponseListUserRoles;
import java.awt.Frame;
import java.util.HashSet;
import java.util.Set;
import javax.swing.DefaultListModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
* Maintain user roles dialog.
*
* @author Jim Voris
*/
public class MaintainUserRolesDialog extends AbstractQVCSCommandDialog implements ChangeListener {
private static final long serialVersionUID = 48811094546493399L;
private DefaultListModel<String> assignedRolesModel = null;
private DefaultListModel<String> availableRolesModel = null;
private boolean isOKflag = false;
/**
* Creates new form MaintainUserRolesDialog.
*
* @param parent the parent frame.
* @param modal should this dialog be modal.
*/
public MaintainUserRolesDialog(Frame parent, boolean modal) {
super(parent, modal);
initComponents();
center();
}
/**
* Center this dialog. This allows us to avoid overriding the center() method which has protected access.
*/
void centerDialog() {
center();
}
/**
* 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.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
okButton = new javax.swing.JButton();
cancelButton = new javax.swing.JButton();
availableRolesList = new javax.swing.JList();
assignedRolesList = new javax.swing.JList();
rightFillerPanel = new javax.swing.JPanel();
leftFillerPanel = new javax.swing.JPanel();
availableRolesLabel = new javax.swing.JLabel();
assignedRolesLabel = new javax.swing.JLabel();
addRoleButton = new javax.swing.JButton();
removeRoleButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Maintain User Roles");
setModal(true);
setName("MaintainUserRoles"); // NOI18N
setResizable(false);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeDialog(evt);
}
});
okButton.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
okButton.setText(" OK ");
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okButtonActionPerformed(evt);
}
});
cancelButton.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
cancelButton.setText("Cancel");
cancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelButtonActionPerformed(evt);
}
});
availableRolesList.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
availableRolesList.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
assignedRolesList.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
assignedRolesList.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
availableRolesLabel.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
availableRolesLabel.setText("Available Roles:");
assignedRolesLabel.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
assignedRolesLabel.setText("Assigned Roles:");
addRoleButton.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
addRoleButton.setText(">>");
addRoleButton.setToolTipText("Add selected role");
addRoleButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addRoleButtonActionPerformed(evt);
}
});
removeRoleButton.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
removeRoleButton.setText("<<");
removeRoleButton.setToolTipText("Remove selected role");
removeRoleButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
removeRoleButtonActionPerformed(evt);
}
});
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()
.add(leftFillerPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 20, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(availableRolesLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 130, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(availableRolesList, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 130, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(1, 1, 1)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(addRoleButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 70, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(removeRoleButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 70, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.add(layout.createSequentialGroup()
.add(18, 18, 18)
.add(okButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
.add(10, 10, 10)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(assignedRolesLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 130, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(assignedRolesList, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 130, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(layout.createSequentialGroup()
.add(cancelButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(18, 18, 18)))
.add(rightFillerPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 30, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(leftFillerPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 300, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(layout.createSequentialGroup()
.add(20, 20, 20)
.add(availableRolesLabel)
.add(6, 6, 6)
.add(availableRolesList, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 190, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(18, 18, 18)
.add(okButton))
.add(layout.createSequentialGroup()
.add(100, 100, 100)
.add(addRoleButton)
.add(11, 11, 11)
.add(removeRoleButton))
.add(layout.createSequentialGroup()
.add(20, 20, 20)
.add(assignedRolesLabel)
.add(6, 6, 6)
.add(assignedRolesList, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 190, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(18, 18, 18)
.add(cancelButton))
.add(rightFillerPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 300, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_cancelButtonActionPerformed
{//GEN-HEADEREND:event_cancelButtonActionPerformed
isOKflag = false;
closeDialog(null);
}//GEN-LAST:event_cancelButtonActionPerformed
private void okButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_okButtonActionPerformed
{//GEN-HEADEREND:event_okButtonActionPerformed
isOKflag = true;
closeDialog(null);
}//GEN-LAST:event_okButtonActionPerformed
private void removeRoleButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_removeRoleButtonActionPerformed
{//GEN-HEADEREND:event_removeRoleButtonActionPerformed
// Remove the selected roles
int[] selectedIndices = assignedRolesList.getSelectedIndices();
String[] selectedRoles = new String[selectedIndices.length];
for (int i = 0; i < selectedIndices.length; i++) {
selectedRoles[i] = getAssignedRolesModel().get(selectedIndices[i]);
}
for (String selectedRole : selectedRoles) {
getAssignedRolesModel().removeElement(selectedRole);
getAvailableRolesModel().addElement(selectedRole);
}
}//GEN-LAST:event_removeRoleButtonActionPerformed
private void addRoleButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_addRoleButtonActionPerformed
{//GEN-HEADEREND:event_addRoleButtonActionPerformed
// Add the selected roles
int[] selectedIndices = availableRolesList.getSelectedIndices();
String[] selectedRoles = new String[selectedIndices.length];
for (int i = 0; i < selectedIndices.length; i++) {
selectedRoles[i] = getAvailableRolesModel().get(selectedIndices[i]);
}
for (String selectedRole : selectedRoles) {
getAvailableRolesModel().removeElement(selectedRole);
getAssignedRolesModel().addElement(selectedRole);
}
}//GEN-LAST:event_addRoleButtonActionPerformed
/**
* Closes the dialog
*/
private void closeDialog(java.awt.event.WindowEvent evt)//GEN-FIRST:event_closeDialog
{
setVisible(false);
dispose();
}//GEN-LAST:event_closeDialog
@Override
public void dismissDialog() {
cancelButtonActionPerformed(null);
}
@Override
public void stateChanged(ChangeEvent changeEvent) {
Object change = changeEvent.getSource();
if (change instanceof ServerResponseListUserRoles) {
ServerResponseListUserRoles response = (ServerResponseListUserRoles) change;
String[] userRoles = response.getUserRoles();
String[] availableRoles = response.getAvailableRoles();
assignedRolesModel = new DefaultListModel<>();
// All the roles that are possible...
Set<String> availableRolesSet = new HashSet<>();
for (String availableRole : availableRoles) {
if (0 != availableRole.compareTo("ADMIN")) {
availableRolesSet.add(availableRole);
}
}
for (String userRole : userRoles) {
if (0 != userRole.compareTo("ADMIN")) {
assignedRolesModel.addElement(userRole);
}
// Remove the roles that are already assigned to this user.
availableRolesSet.remove(userRole);
}
// The roles that are left are the ones that are available.
availableRolesModel = new DefaultListModel<>();
for (String role : availableRolesSet) {
if (0 != role.compareTo("ADMIN")) {
availableRolesModel.addElement(role);
}
}
availableRolesList.setModel(availableRolesModel);
assignedRolesList.setModel(assignedRolesModel);
}
}
private DefaultListModel<String> getAvailableRolesModel() {
return availableRolesModel;
}
private DefaultListModel<String> getAssignedRolesModel() {
return assignedRolesModel;
}
String[] getAssignedRoles() {
int size = getAssignedRolesModel().size();
String[] assignedRoles = new String[size];
for (int i = 0; i < size; i++) {
assignedRoles[i] = getAssignedRolesModel().get(i);
}
return assignedRoles;
}
boolean getIsOK() {
return isOKflag;
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton addRoleButton;
private javax.swing.JLabel assignedRolesLabel;
private javax.swing.JList assignedRolesList;
private javax.swing.JLabel availableRolesLabel;
private javax.swing.JList availableRolesList;
private javax.swing.JButton cancelButton;
private javax.swing.JPanel leftFillerPanel;
private javax.swing.JButton okButton;
private javax.swing.JButton removeRoleButton;
private javax.swing.JPanel rightFillerPanel;
// End of variables declaration//GEN-END:variables
}