/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package de.netsysit.controller;
import de.netsysit.policymanager.Enums;
import de.netsysit.policymanager.Enums.Cards;
import de.netsysit.policymanager.Enums.ItemTypes;
import de.netsysit.model.DataBaseModel;
import de.netsysit.policymanager.PolicyManager;
import de.netsysit.policymanager.PolicyUtilities;
import de.netsysit.view.PersonalListCellRenderer;
import java.awt.CardLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.EnumSet;
import java.util.ResourceBundle;
import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/**
*
* @author Ducksoul
*/
public class MainFrameController {
private static ResourceBundle rb = PolicyUtilities.getResourceBundle();
private static DataBaseModel dbModel = PolicyManager.getPolicyModel();
private JList mainCardsLst;
private JPanel mainRightPnl;
private JCheckBox mainWarningCheck;
DefaultListModel mainCardsLstModel = new DefaultListModel();
public MainFrameController() {
}
public MainFrameController(JPanel mainRightPnl, JList mainCardsLst, JCheckBox mainWarningCheck) {
this.mainCardsLst = mainCardsLst;
this.mainRightPnl = mainRightPnl;
this.mainWarningCheck = mainWarningCheck;
init();
}
private void init() {
dbModel.setActiveItem(ItemTypes.APP);
PropertyChangeListener mfPcl = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
String propName = evt.getPropertyName();
if (propName.equals("dbActiveItemSet")) {
CardLayout layout = (CardLayout) mainRightPnl.getLayout();
ItemTypes item = dbModel.getActiveItem();
Cards card = item.getCard();
String cardString = item.getCardString();
layout.show(mainRightPnl, cardString);
mainCardsLst.setSelectedValue(card, true);
}
}
};
dbModel.addPropertyChangeListener(mfPcl);
PersonalListCellRenderer cr = new PersonalListCellRenderer();
EnumSet cards = Enums.getCards();
for (Object card : cards) {
mainCardsLstModel.addElement((Cards) card);
}
mainCardsLst.setModel(mainCardsLstModel);
// mainCardsLst.addPropertyChangeListener(mfPcl);
mainCardsLst.setCellRenderer(cr);
mainCardsLst.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
mainCardsLst.setSelectedIndex(0);
mainCardsLst.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
Cards eCard = (Cards) mainCardsLst.getSelectedValue();
if (eCard == Cards.APP) {
dbModel.setActiveItem(ItemTypes.APP);
ActionFactory.get(ActionFactory.ActionKey.MAIN_ADD).setName(rb.getString("addapp"));
ActionFactory.get(ActionFactory.ActionKey.MAIN_DELETE).setName(rb.getString("delapp"));
} else if (eCard == Cards.LIST) {
dbModel.setActiveItem(ItemTypes.LIST);
ActionFactory.get(ActionFactory.ActionKey.MAIN_ADD).setName(rb.getString("addlist"));
ActionFactory.get(ActionFactory.ActionKey.MAIN_DELETE).setName(rb.getString("dellist"));
} else if (eCard == Cards.POLICY) {
dbModel.setActiveItem(ItemTypes.POLICY);
ActionFactory.get(ActionFactory.ActionKey.MAIN_ADD).setName(rb.getString("addpolicy"));
ActionFactory.get(ActionFactory.ActionKey.MAIN_DELETE).setName(rb.getString("delpolicy"));
}
}
}
});
mainWarningCheck.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
PolicyUtilities.setShowWarnings(mainWarningCheck.isSelected());
}
});
}
}