/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package de.netsysit.controller;
import de.netsysit.controller.ActionFactory.ActionKey;
import de.netsysit.model.ChangeListModel;
import de.netsysit.model.DataBaseModel;
import de.netsysit.model.ReceiverListModel;
import de.netsysit.model.ReceiverModel;
import de.netsysit.policymanager.PolicyManager;
import de.netsysit.policymanager.PolicyUtilities;
import de.netsysit.view.DefaultListCellEditor;
import de.netsysit.view.JListMutable;
import de.netsysit.view.ListView;
import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.JList;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/**
*
* @author Ducksoul
*/
public class ListController {
private static DataBaseModel dbModel = PolicyManager.getPolicyModel();
private static PropertyChangeListener listPcl;
private static DocumentListener listDl;
private ResourceBundle rb = PolicyUtilities.getResourceBundle();
private ReceiverListModel currentReceiverList = new ReceiverListModel();
private JList listReceiverListsLst;
private JTextField listNameTxt;
private JListMutable listReceiversLst;
private JTextField editorComp = new JTextField();
private JPopupMenu listPopup = new JPopupMenu();
public ListController() {
}
public ListController(ListView lv) {
this.listReceiverListsLst = lv.getListReceiverListsLst();
this.listNameTxt = lv.getListNameTxt();
this.listReceiversLst = lv.getListReceiversLst();
init();
ActionFactory.setLc(this);
}
private void init() {
ActionFactory.get(ActionKey.LIST_DELETE).setEnabled(false);
listReceiversLst.addPropertyChangeListener(listPcl);
listPcl = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
String propName = evt.getPropertyName();
//laden der Applikationen in die JList
if ((propName.equals("dbReceiverListsSet")) || (propName.equals("dbNewReceiverListAdded")) || (propName.equals("dbReceiverListRemoved"))) {
listReceiverListsLst.setModel(dbModel.getReceiverListsAsListModel());
listReceiverListsLst.setSelectedIndex(listReceiverListsLst.getModel().getSize() -1);
// aktuelle Applikation setzen und entsprechende Listener adden
} else if (propName.equals("dbCurrentReceiverListSet")) {
listNameTxt.setEnabled(true);
currentReceiverList = dbModel.getCurrentReceiverList();
currentReceiverList.addPropertyChangeListener(listPcl);
currentReceiverList.addPropertyChangeListener((ChangeListModel) listReceiverListsLst.getModel());
listReceiverListsLst.setSelectedValue(currentReceiverList, true);
ReceiverListModel prevApplication = (ReceiverListModel) evt.getOldValue();
if (prevApplication != null) {
prevApplication.removePropertyChangeListener(listPcl);
}
fillReceiverListGUI();
} else if (propName.equals("editingStopped")) {
ReceiverModel rm = (ReceiverModel) listReceiversLst.getSelectedValue();
rm.setName(editorComp.getText());
} else if ((propName.equals("listReceiverAdded")) || (propName.equals("listReceiverRemoved"))) {
listReceiversLst.setModel(currentReceiverList.getReceiversAsMutableListModel());
}
}
};
listDl = new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
changeName();
}
public void removeUpdate(DocumentEvent e) {
changeName();
}
public void changedUpdate(DocumentEvent e) {
changeName();
}
};
listReceiverListsLst.setModel(new ChangeListModel());
listNameTxt.getDocument().addDocumentListener(listDl);
dbModel.addPropertyChangeListener(listPcl);
dbModel.addPropertyChangeListener((ChangeListModel)listReceiverListsLst.getModel());
listReceiversLst.setListCellEditor(new DefaultListCellEditor(editorComp));
listReceiversLst.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (listReceiversLst.getSelectedIndex() != -1) {
ActionFactory.get(ActionKey.LIST_RECEIVEREDIT).setEnabled(true);
ActionFactory.get(ActionKey.LIST_RECEIVERDELETE).setEnabled(true);
} else {
ActionFactory.get(ActionKey.LIST_RECEIVEREDIT).setEnabled(false);
ActionFactory.get(ActionKey.LIST_RECEIVERDELETE).setEnabled(false);
}
}
});
listReceiverListsLst.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (listReceiverListsLst.getSelectedIndex() != -1) {
dbModel.setCurrentReceiverList((ReceiverListModel) listReceiverListsLst.getSelectedValue());
ActionFactory.get(ActionKey.LIST_ADDRECEIVER).setEnabled(true);
ActionFactory.get(ActionKey.LIST_DELETE).setEnabled(true);
} else {
ActionFactory.get(ActionKey.LIST_ADDRECEIVER).setEnabled(false);
ActionFactory.get(ActionKey.LIST_DELETE).setEnabled(false);
}
}
});
listPopup.add(ActionFactory.get(ActionKey.LIST_ADD));
listPopup.add(ActionFactory.get(ActionKey.LIST_DELETE));
listReceiverListsLst.add(listPopup);
listReceiverListsLst.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
if(e.isPopupTrigger())
listPopup.show(e.getComponent(), e.getX(), e.getY());
}
});
}
private void fillReceiverListGUI() {
listNameTxt.setText(currentReceiverList.getName());
listReceiversLst.setModel(currentReceiverList.getReceiversAsMutableListModel());
}
public void editReceiver() {
if(!listReceiversLst.isEditing()) {
listReceiversLst.editCellAt(listReceiversLst.getSelectedIndex(), null);
final JTextField listEditorComp = (JTextField) listReceiversLst.getEditorComponent();
listEditorComp.addPropertyChangeListener(listPcl);
listEditorComp.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent e) {
ActionFactory.get(ActionKey.LIST_RECEIVEREDIT).setEnabled(false);
listEditorComp.selectAll();
}
public void focusLost(FocusEvent e) {
if(listReceiversLst.isEditing()) {
listReceiversLst.editingStopped(null);
}
ActionFactory.get(ActionKey.LIST_RECEIVEREDIT).setEnabled(true);
}
});
listEditorComp.requestFocus();
}
}
public void deleteReceiver() {
int index = listReceiversLst.getSelectedIndex();
ReceiverModel selection = (ReceiverModel) listReceiversLst.getSelectedValue();
currentReceiverList.removeReceiver(selection);
if(index <= listReceiversLst.getModel().getSize()-1)
listReceiversLst.setSelectedIndex(index);
else {
int newIndex = listReceiversLst.getModel().getSize() - 1;
listReceiversLst.setSelectedIndex(newIndex);
}
}
public void addReceiver() {
ReceiverModel receiver = new ReceiverModel();
receiver.setId(PolicyUtilities.getNewReceiverId());
receiver.setName(rb.getString("newreceiver") + "_" + new Integer(receiver.getId()).toString());
currentReceiverList.addReceiver(receiver);
int newIndex = listReceiversLst.getModel().getSize() - 1;
listReceiversLst.setSelectedIndex(newIndex);
editReceiver();
}
public void removeReceiverList() {
if(!listReceiverListsLst.isSelectionEmpty()) {
// int oldIndex = listReceiverListsLst.getSelectedIndex();
if (dbModel.removeReceiverList(currentReceiverList)) {
if(dbModel.getReceiverLists().size() == 0) {
deleteListGUI();
}
} else {
// TODO Fehlermeldung
}
}
}
private void deleteListGUI() {
listNameTxt.setText("");
listReceiversLst.setListData(new String[] {});
editorComp.setText("");
}
private boolean checkName(String name) {
List<ReceiverListModel> lists = dbModel.getReceiverLists();
for(int i=0; i<lists.size(); i++) {
if((lists.get(i).getName().equals(name)) && (lists.get(i) != currentReceiverList))
return false;
}
return true;
}
private void changeName() {
String name = listNameTxt.getText();
if(checkName(name))
listNameTxt.setBackground(Color.white);
else
listNameTxt.setBackground(Color.red);
currentReceiverList.setName(name);
}
}