/*
* 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.policymanager.Enums.ApplicationBase;
import de.netsysit.policymanager.Enums.ItemTypes;
import de.netsysit.model.ApplicationModel;
import de.netsysit.model.ChangeListModel;
import de.netsysit.model.DataBaseModel;
import de.netsysit.model.ReceiverListModel;
import de.netsysit.policymanager.PolicyManager;
import de.netsysit.view.ApplicationView;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
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 ApplicationController {
private static DataBaseModel dbModel = PolicyManager.getPolicyModel();
private static PropertyChangeListener appPcl;
private static ActionListener appAl;
private static DocumentListener appDl;
private ApplicationModel currentApplication = new ApplicationModel();
private JList appApplicationsLst;
private JList appReceiverListsLst;
private JComboBox appALevelCombo;
private JComboBox appCLevelCombo;
private JComboBox appILevelCombo;
private JCheckBox appExpectRequestEncryptedCheck;
private JTextField appNameTxt;
private JPopupMenu appPopup = new JPopupMenu();
public ApplicationController() {
}
public ApplicationController(ApplicationView apv) {
appApplicationsLst = apv.getAppApplicationsLst();
appReceiverListsLst = apv.getAppReceiverListsLst();
appALevelCombo = apv.getAppALevelCombo();
appCLevelCombo = apv.getAppCLevelCombo();
appILevelCombo = apv.getAppILevelCombo();
appNameTxt = apv.getAppNameTxt();
appExpectRequestEncryptedCheck = apv.getAppExpectRequestEncryptedCheck();
init();
}
private void init() {
ActionFactory.get(ActionKey.APP_DELETE).setEnabled(false);
appPcl = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
String propName = evt.getPropertyName();
//laden der Applikationen in die JList
if((propName.equals("dbApplicationsSet")) || (propName.equals("dbNewApplicationAdded")) || propName.equals("dbApplicationRemoved")) {
appApplicationsLst.setModel(dbModel.getApplicationsAsListModel());
appApplicationsLst.setSelectedIndex(appApplicationsLst.getModel().getSize() -1);
// aktuelle Applikation setzen und entsprechende Listener adden
} else if(propName.equals("dbCurrentApplicationSet")) {
appNameTxt.setEnabled(true);
appALevelCombo.setEnabled(true);
appCLevelCombo.setEnabled(true);
appILevelCombo.setEnabled(true);
appExpectRequestEncryptedCheck.setEnabled(true);
currentApplication = dbModel.getCurrentApplication();
currentApplication.addPropertyChangeListener(appPcl);
currentApplication.addPropertyChangeListener((ChangeListModel)appApplicationsLst.getModel());
ApplicationModel prevApplication = (ApplicationModel) evt.getOldValue();
if(prevApplication != null)
prevApplication.removePropertyChangeListener(appPcl);
fillApplicationGUI();
} else if((propName.equals("appReceiverListAdded")) || (propName.equals("appReceiverListRemoved"))) {
appReceiverListsLst.setModel(currentApplication.getReceiverListsAsListModel());
}
}
};
appAl = new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox source = (JComboBox) e.getSource();
ApplicationBase level = (ApplicationBase) source.getSelectedItem();
if(source == appALevelCombo) {
currentApplication.setBaseA(level);
} else if(source == appCLevelCombo) {
currentApplication.setBaseC(level);
} else if(source == appILevelCombo) {
currentApplication.setBaseI(level);
}
}
};
appDl = new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
changeName();
}
public void removeUpdate(DocumentEvent e) {
changeName();
}
public void changedUpdate(DocumentEvent e) {
changeName();
}
};
appApplicationsLst.setModel(dbModel.getApplicationsAsListModel());
appNameTxt.getDocument().addDocumentListener(appDl);
dbModel.addPropertyChangeListener(appPcl);
dbModel.addPropertyChangeListener((ChangeListModel)appApplicationsLst.getModel());
appApplicationsLst.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if(appApplicationsLst.getSelectedIndex() != -1) {
dbModel.setCurrentApplication((ApplicationModel) appApplicationsLst.getSelectedValue());
ActionFactory.get(ActionFactory.ActionKey.APP_ADDLIST).setEnabled(true);
ActionFactory.get(ActionKey.APP_DELETE).setEnabled(true);
} else {
ActionFactory.get(ActionFactory.ActionKey.APP_ADDLIST).setEnabled(false);
ActionFactory.get(ActionKey.APP_DELETE).setEnabled(false);
}
}
});
appReceiverListsLst.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (!appReceiverListsLst.isSelectionEmpty()) {
ActionFactory.get(ActionFactory.ActionKey.APP_JUMPTO).setEnabled(true);
ActionFactory.get(ActionFactory.ActionKey.APP_DELETELIST).setEnabled(true);
} else {
ActionFactory.get(ActionFactory.ActionKey.APP_JUMPTO).setEnabled(false);
ActionFactory.get(ActionFactory.ActionKey.APP_DELETELIST).setEnabled(false);
}
}
});
appExpectRequestEncryptedCheck.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
currentApplication.setExpectRequestEncrypted(appExpectRequestEncryptedCheck.isSelected());
}
});
appALevelCombo.setModel(dbModel.getSecurityLevelAsComboBoxModel());
appALevelCombo.addActionListener(appAl);
appCLevelCombo.setModel(dbModel.getSecurityLevelAsComboBoxModel());
appCLevelCombo.addActionListener(appAl);
appILevelCombo.setModel(dbModel.getSecurityLevelAsComboBoxModel());
appILevelCombo.addActionListener(appAl);
ActionFactory.setAc(this);
appPopup.add(ActionFactory.get(ActionKey.APP_ADD));
appPopup.add(ActionFactory.get(ActionKey.APP_DELETE));
appApplicationsLst.add(appPopup);
appApplicationsLst.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
if(e.isPopupTrigger())
appPopup.show(e.getComponent(), e.getX(), e.getY());
}
});
}
private void fillApplicationGUI() {
appNameTxt.setText(currentApplication.getName());
appALevelCombo.setSelectedItem(currentApplication.getBaseA());
appCLevelCombo.setSelectedItem(currentApplication.getBaseC());
appILevelCombo.setSelectedItem(currentApplication.getBaseI());
appExpectRequestEncryptedCheck.setSelected(currentApplication.isExpectRequestEncrypted());
appReceiverListsLst.setModel(currentApplication.getReceiverListsAsListModel());
appReceiverListsLst.addPropertyChangeListener(appPcl);
}
public void deleteList() {
ReceiverListModel selection = (ReceiverListModel) appReceiverListsLst.getSelectedValue();
currentApplication.removeReceiverList(selection);
}
public void jumpToList() {
ReceiverListModel selection = (ReceiverListModel) appReceiverListsLst.getSelectedValue();
dbModel.setCurrentReceiverList(selection);
ListController cont = new ListController();
dbModel.setActiveItem(ItemTypes.LIST);
appReceiverListsLst.clearSelection();
}
public void removeApplication() {
if(!appApplicationsLst.isSelectionEmpty()) {
// int oldIndex = listReceiverListsLst.getSelectedIndex();
if (dbModel.removeApplication(currentApplication)) {
if(dbModel.getApplications().isEmpty()) {
deleteAppGUI();
}
} else {
// TODO Fehlermeldung
}
}
}
private void deleteAppGUI() {
appReceiverListsLst.setListData(new String[] {});
appALevelCombo.setSelectedItem(ApplicationBase.NONE);
appCLevelCombo.setSelectedItem(ApplicationBase.NONE);
appILevelCombo.setSelectedItem(ApplicationBase.NONE);
appNameTxt.setText("");
appExpectRequestEncryptedCheck.setSelected(false);
}
private boolean checkName(String name) {
List<ApplicationModel> apps = dbModel.getApplications();
for(int i=0; i<apps.size(); i++) {
if((apps.get(i).getName().equals(name)) && (apps.get(i) !=(currentApplication)))
return false;
}
return true;
}
private void changeName() {
String name = appNameTxt.getText();
if(checkName(name)) {
appNameTxt.setBackground(Color.white);
} else {
appNameTxt.setBackground(Color.red);
}
currentApplication.setName(name);
}
}