package cz.cvut.fel.restauracefel.restauracefel.gui;
import java.io.FileNotFoundException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.HashSet;
import cz.cvut.fel.restauracefel.library.service.EmptyListException;
import cz.cvut.fel.restauracefel.library.service.CommonServiceFacade;
import cz.cvut.fel.restauracefel.library.service.LocalizationManager;
import cz.cvut.fel.restauracefel.restauracefel.main.Main;
/**
* Trida vytvarejici dialog pro vyber pristupovych prav.
*
* @author Tomas Hnizdil
*/
public class ChooseRightDialog extends AbstractDialog {
// Odkaz na frame volajici tento dialog
private AssignRightForm caller = null;
private int role;
private int[] selectedIndices = null;
/**
* Konstruktor tridy ChooseRightDialog
*
* @param parent
* @param modal
* @param caller AssignRightForm, ktery tento dialog zavolal a do ktereho se zapise zvoleny vysledek
* @param role Id role vybrane ve formulari AssignRightForm
*
* @throws service.EmptyListException
* @throws java.rmi.RemoteException
* @throws java.rmi.NotBoundException
* @throws java.io.FileNotFoundException
*/
public ChooseRightDialog(MainFrame parent, boolean modal, AssignRightForm caller, int role) throws EmptyListException, RemoteException, NotBoundException, FileNotFoundException {
super(parent, modal);
this.caller = caller;
this.role = role;
initComponents();
LocalizationManager manager = LocalizationManager.getInstance();
manager.localizeLabel(Main.DEFAULT_BUNDLE_BASE_NAME, "AssignRightsLabel", jLabel1);
manager.localizeLabel(Main.DEFAULT_BUNDLE_BASE_NAME, "AssignRightsNoteLabel", jLabel2);
manager.localizeButton(Main.DEFAULT_BUNDLE_BASE_NAME, "SelectAllButton", jButtonSelectAll);
manager.localizeButton(Main.DEFAULT_BUNDLE_BASE_NAME, "DeselectAllButton", jButtonDeselectAll);
manager.localizeButton(Main.DEFAULT_BUNDLE_BASE_NAME, "OkButton", jButtonOK);
manager.localizeButton(Main.DEFAULT_BUNDLE_BASE_NAME, "BackButton", jButtonBack);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
refresh();
}
/**
* Metoda provadi aktualizaci seznamu pristupovych prav.
*
* @throws service.EmptyListException
* @throws java.rmi.RemoteException
* @throws java.rmi.NotBoundException
* @throws java.io.FileNotFoundException
*/
@Override
protected void refresh() throws EmptyListException, RemoteException, NotBoundException, FileNotFoundException {
String [] rights = null;
if (role == 0) {
rights = CommonServiceFacade.getInstance().getRightNames();
if (rights == null) throw new EmptyListException("Žádná práva", "V systému nejsou zaevidována žádná přístupová práva.");
jList1.setListData(rights);
} else {
rights = CommonServiceFacade.getInstance().getRightNamesByRole(role);
if (rights == null) throw new EmptyListException("Žádná práva", "Vybraná uživatelská role nemá přiřazena žádná uživatelská práva.");
jList1.setListData(rights);
}
}
/** 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() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jList1 = new javax.swing.JList();
jButtonBack = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jButtonOK = new javax.swing.JButton();
jButtonDeselectAll = new javax.swing.JButton();
jButtonSelectAll = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setModal(true);
setResizable(false);
setUndecorated(true);
addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
clicked(evt);
}
});
jPanel1.setBackground(new java.awt.Color(255, 255, 255));
jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
jList1.setFont(new java.awt.Font("Tahoma", 1, 18));
jList1.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jList1.setAlignmentX(2.0F);
jList1.setFixedCellHeight(50);
jList1.setFixedCellWidth(200);
jList1.setFocusCycleRoot(true);
jList1.setSelectionBackground(javax.swing.UIManager.getDefaults().getColor("InternalFrame.inactiveTitleGradient"));
jList1.setVisibleRowCount(-1);
jList1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
clicked(evt);
}
});
jScrollPane1.setViewportView(jList1);
jButtonBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/cvut/fel/restauracefel/buttons/ko.png"))); // NOI18N
jButtonBack.setText(" Zavřít");
jButtonBack.setToolTipText("Vymazat pole stůl");
jButtonBack.setBorder(null);
jButtonBack.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonBackActionPerformed(evt);
}
});
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
jLabel1.setText("Přiřadit přístupová práva");
jLabel2.setText("Vyberte funkce, ke kterým má mít uživatel s danou rolí přístup");
jButtonOK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/cvut/fel/restauracefel/buttons/ok.png"))); // NOI18N
jButtonOK.setText(" OK");
jButtonOK.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonOKActionPerformed(evt);
}
});
jButtonDeselectAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/cvut/fel/restauracefel/buttons/right.png"))); // NOI18N
jButtonDeselectAll.setText(" Zrušit výběr");
jButtonDeselectAll.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonDeselectAllActionPerformed(evt);
}
});
jButtonSelectAll.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cz/cvut/fel/restauracefel/buttons/left.png"))); // NOI18N
jButtonSelectAll.setText(" Vybrat vše");
jButtonSelectAll.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonSelectAllActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 469, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane1)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButtonSelectAll)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonDeselectAll)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonOK, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButtonBack, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)))))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButtonOK, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButtonDeselectAll, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
.addComponent(jButtonSelectAll, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE))
.addComponent(jButtonBack, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(28, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void jButtonBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBackActionPerformed
dispose();
}//GEN-LAST:event_jButtonBackActionPerformed
/**
* Metoda pro hledani hodnoty v poli selectedIndices (pole indexu vybranych prvku v komponente jList)
*
* @param index hledana hodnota
*/
private int contains(int index) {
for (int i=0; i<selectedIndices.length; i++) {
if (selectedIndices[i]==index) return i;
}
return -1;
}
private void clicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_clicked
if (selectedIndices == null) { // prvni vyber
selectedIndices = jList1.getSelectedIndices();
return;
}
int index = jList1.getSelectedIndex();
int i = contains(index);
int[] newIndices;
if (i==-1) {
newIndices = new int[selectedIndices.length+1];
for (int j=0; j<selectedIndices.length; j++) newIndices[j] = selectedIndices[j];
newIndices[selectedIndices.length] = jList1.getSelectedIndex();
} else {
newIndices = new int[selectedIndices.length-1];
for (int j=0; j<newIndices.length; j++) newIndices[j] = selectedIndices[j];
if (i<newIndices.length) newIndices[i] = selectedIndices[selectedIndices.length-1];
}
jList1.setSelectedIndices(newIndices);
selectedIndices = newIndices;
}//GEN-LAST:event_clicked
private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
HashSet<String> hs = new HashSet<String>();
for (int i = 0; i < jList1.getSelectedValues().length; i++) {
hs.add(jList1.getSelectedValues()[i].toString());
}
if (role == 0) {
caller.fillAddedRights(hs);
} else {
caller.fillRemovedRights(hs);
}
dispose();
}//GEN-LAST:event_jButtonOKActionPerformed
private void jButtonDeselectAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeselectAllActionPerformed
jList1.clearSelection();
selectedIndices = null;
}//GEN-LAST:event_jButtonDeselectAllActionPerformed
private void jButtonSelectAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSelectAllActionPerformed
jList1.setSelectionInterval(0, jList1.getModel().getSize() - 1);
selectedIndices = jList1.getSelectedIndices();
}//GEN-LAST:event_jButtonSelectAllActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButtonBack;
private javax.swing.JButton jButtonDeselectAll;
private javax.swing.JButton jButtonOK;
private javax.swing.JButton jButtonSelectAll;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JList jList1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration//GEN-END:variables
}