/*
* Copyright 2011 Research In Motion Limited.
*
* 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 eclserver.panels;
import eclserver.db.objects.GroupObject;
import eclserver.renderers.GroupListRenderer;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.event.EventListenerList;
import javax.swing.JList;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
/**
*
* @author rbalsewich
*/
public class GroupListPanel extends javax.swing.JPanel {
/** Creates new form GroupListPanel */
public GroupListPanel() {
renderer = new GroupListRenderer();
model = new DefaultListModel();
initComponents();
listeners = new EventListenerList();
}
/** 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() {
scrollPane = new javax.swing.JScrollPane();
groupNames = new javax.swing.JList();
jLabel1 = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
btSaveGroup = new javax.swing.JButton();
txtGroupName = new javax.swing.JTextField();
btDeleteGroup = new javax.swing.JButton();
setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.Y_AXIS));
scrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
groupNames.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
groupNames.setModel(model);
groupNames.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
groupNames.setCellRenderer(renderer);
groupNames.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
groupNamesValueChanged(evt);
}
});
scrollPane.setViewportView(groupNames);
add(scrollPane);
jLabel1.setFont(new java.awt.Font("Tahoma", 3, 11));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
jLabel1.setText("Enter Group Name");
jLabel1.setVerticalAlignment(javax.swing.SwingConstants.TOP);
jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
add(jLabel1);
btSaveGroup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/eclserver/images/Add.png"))); // NOI18N
btSaveGroup.setToolTipText("Save Group");
btSaveGroup.setActionCommand("SAVE_GROUP");
btSaveGroup.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
btSaveGroup.setBorderPainted(false);
btSaveGroup.setContentAreaFilled(false);
btSaveGroup.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
btSaveGroup.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
btSaveGroup.setMargin(new java.awt.Insets(2, 3, 2, 3));
btSaveGroup.setSelected(true);
btSaveGroup.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
btSaveGroupMouseReleased(evt);
}
});
btSaveGroup.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btSaveGroupActionPerformed(evt);
}
});
jPanel1.add(btSaveGroup);
txtGroupName.setToolTipText("Enter Group Name");
txtGroupName.setPreferredSize(new java.awt.Dimension(100, 35));
txtGroupName.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtGroupNameActionPerformed(evt);
}
});
jPanel1.add(txtGroupName);
btDeleteGroup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/eclserver/images/action_delete.png"))); // NOI18N
btDeleteGroup.setToolTipText("Delete Group");
btDeleteGroup.setActionCommand("DELETE_GROUP");
btDeleteGroup.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 2));
btDeleteGroup.setContentAreaFilled(false);
btDeleteGroup.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
btDeleteGroup.setMargin(new java.awt.Insets(2, 3, 2, 3));
btDeleteGroup.setMaximumSize(new java.awt.Dimension(35, 35));
btDeleteGroup.setSelected(true);
btDeleteGroup.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btDeleteGroupActionPerformed(evt);
}
});
jPanel1.add(btDeleteGroup);
add(jPanel1);
}// </editor-fold>//GEN-END:initComponents
private void txtGroupNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtGroupNameActionPerformed
// TODO add your handling code here:
fireActionEvent(evt);
}//GEN-LAST:event_txtGroupNameActionPerformed
private void btDeleteGroupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDeleteGroupActionPerformed
// TODO add your handling code here:
fireActionEvent(evt);
}//GEN-LAST:event_btDeleteGroupActionPerformed
private void btSaveGroupMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btSaveGroupMouseReleased
// TODO add your handling code here:
}//GEN-LAST:event_btSaveGroupMouseReleased
private void groupNamesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_groupNamesValueChanged
// TODO add your handling code here:
if (evt.getValueIsAdjusting()) {
return;
}
JList entryList = (JList) evt.getSource();
selectedEntry = entryList.getSelectedIndex();
}//GEN-LAST:event_groupNamesValueChanged
private void btSaveGroupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSaveGroupActionPerformed
// TODO add your handling code here:
String inputValue = this.getGroupName();
// System.out.println("Fetching user input value..." + inputValue);
if (inputValue.length() > 0 ){
fireActionEvent(evt);
} else {
JOptionPane.showMessageDialog(this, "Group Name must be more than 0 characters.");
}
}//GEN-LAST:event_btSaveGroupActionPerformed
public void addListEntry(GroupObject entry) {
model.addElement(entry);
}
public void addListEntries(List<GroupObject> list) {
for(GroupObject entry: list) {
addListEntry(entry);
}
}
public int getSelectedIndex() {
return groupNames.getSelectedIndex();
}
public int setSelectedIndex(int index) {
assert(index >= -1);
DefaultListModel model = (DefaultListModel)groupNames.getModel();
int size = model.getSize();
// System.out.println("Model size: " + size);
if (index < size) {
groupNames.setSelectedIndex(index);
} else {
groupNames.setSelectedIndex(size-1);
index = size -1;
}
return index;
}
public GroupObject getSelectedListEntry() {
//System.out.println("using selecteValue for List Entry: " + groupName.getSelectedValue() );
GroupObject entry = (GroupObject)groupNames.getSelectedValue();
return entry;
}
public void deleteSelectedEntry() {
int selectedIndex = groupNames.getSelectedIndex();
if (selectedIndex >= 0) {
DefaultListModel model = (DefaultListModel)groupNames.getModel();
model.remove(selectedIndex);
}
}
private void fireActionEvent(ActionEvent evt) {
ActionListener[] listenerList = listeners.getListeners(ActionListener.class);
for (int i = listenerList.length-1; i>=0; --i) {
listenerList[i].actionPerformed(evt);
}
}
public void addActionListener(ActionListener listener) {
listeners.add(ActionListener.class, listener);
}
public void removeActionListener(ActionListener listener) {
if (listeners != null) {
listeners.remove(ActionListener.class, listener);
}
}
public String getGroupName() {
String groupName = txtGroupName.getText();
return groupName;
}
public void setGroupName(String value) {
this.txtGroupName.setText(value);
}
public int getdaoSelectedEntry(){
return this.daoSelectedEntry;
}
public int getSelectedEntry(){
return this.selectedEntry;
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btDeleteGroup;
private javax.swing.JButton btSaveGroup;
private javax.swing.JList groupNames;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane scrollPane;
private javax.swing.JTextField txtGroupName;
// End of variables declaration//GEN-END:variables
EventListenerList listeners;
private GroupListRenderer renderer;
private DefaultListModel model;
private int selectedEntry = -1;
private int daoSelectedEntry = 0;
}