Package de.netsysit.controller

Source Code of de.netsysit.controller.PolicyController

/*
* 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.controller.DnD.ListMoveTransferHandler;
import de.netsysit.policymanager.Enums.Algorithms;
import de.netsysit.policymanager.Enums.Modes;
import de.netsysit.policymanager.Enums.PolicyLevel;
import de.netsysit.model.ActionModel;
import de.netsysit.model.ActionTupel;
import de.netsysit.model.ChangeListModel;
import de.netsysit.model.DataBaseModel;
import de.netsysit.model.PersonalTreeNode;
import de.netsysit.model.PolicyModel;
import de.netsysit.policymanager.PolicyManager;
import de.netsysit.policymanager.PolicyUtilities;
import de.netsysit.view.PolicyView;
import java.awt.Color;
import java.awt.Cursor;
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.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DropMode;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
*
* @author Ducksoul
*/
public class PolicyController {

    private static DataBaseModel dbModel = PolicyManager.getPolicyModel();
    private static ResourceBundle rb = PolicyUtilities.getResourceBundle();
    private static ResourceBundle mb = PolicyUtilities.getMessageBundle();
    private static PropertyChangeListener policyPcl;
    private static ActionListener policyAl;
    private static ListSelectionListener policyLsl;
    private static DocumentListener policyDl;
    private PolicyModel currentPolicy = new PolicyModel();
    private List<PolicyModel> currentPolicies;
    private PolicyLevel activeLevel = dbModel.getActiveLevel();
    private JList policyAPoliciesLst;
    private JList policyCPoliciesLst;
    private JList policyIPoliciesLst;
    private JTextField policyNameTxt;
    private JLabel policyFallBackLbl2;
    private JComboBox policyAlgorithmCombo;
    private JComboBox policyModeCombo;
    private JCheckBox policyUseTokenCheck;
    private JTree policyTree;
    private JPopupMenu policyAPopup = new JPopupMenu();
    private JPopupMenu policyCPopup = new JPopupMenu();
    private JPopupMenu policyIPopup = new JPopupMenu();

    public PolicyController() {
    }

    public PolicyController(PolicyView pv) {
        this();
        policyAPoliciesLst = pv.getPolicyAPoliciesLst();
        policyCPoliciesLst = pv.getPolicyCPoliciesLst();
        policyIPoliciesLst = pv.getPolicyIPoliciesLst();
        policyNameTxt = pv.getPolicyNameTxt();
        policyFallBackLbl2 = pv.getPolicyFallBackLbl2();
        policyAlgorithmCombo = pv.getPolicyAlgorithmCombo();
        policyModeCombo = pv.getPolicyModeCombo();
        policyUseTokenCheck = pv.getPolicyUseTokenCheck();
        policyTree = pv.getPolicyTree();
        init();
    }

    private void init() {
        ActionFactory.setPc(this);
        ActionFactory.get(ActionKey.POLICY_DELETEA).setEnabled(false);
        ActionFactory.get(ActionKey.POLICY_DELETEC).setEnabled(false);
        ActionFactory.get(ActionKey.POLICY_DELETEI).setEnabled(false);
       
        policyPcl = new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent evt) {
                String propName = evt.getPropertyName();
                //laden der Applikationen in die JList
                if ((propName.equals("dbPoliciesSet")) || (propName.equals("dbNewPolicyAdded")) || (propName.equals("dbPolicyRemoved"))) {
                    policyAPoliciesLst.setModel(dbModel.getAPoliciesAsListModel());
                    currentPolicy.addPropertyChangeListener((ChangeListModel) policyAPoliciesLst.getModel());
                    policyCPoliciesLst.setModel(dbModel.getCPoliciesAsListModel());
                    currentPolicy.addPropertyChangeListener((ChangeListModel) policyCPoliciesLst.getModel());
                    policyIPoliciesLst.setModel(dbModel.getIPoliciesAsListModel());
                    currentPolicy.addPropertyChangeListener((ChangeListModel) policyIPoliciesLst.getModel());
                   
                    switch(dbModel.getActiveLevel()) {
                        case A:
                            policyAPoliciesLst.setSelectedIndex(policyAPoliciesLst.getModel().getSize() -1);
                            break;
                        case C:
                            policyCPoliciesLst.setSelectedIndex(policyCPoliciesLst.getModel().getSize() -1);
                            break;
                        case I:
                            policyIPoliciesLst.setSelectedIndex(policyIPoliciesLst.getModel().getSize() -1);
                            break;
                    }
                } else if (propName.equals("dbCurrentPolicySet")) {
                    policyNameTxt.setEnabled(true);
                    policyAlgorithmCombo.setEnabled(true);
                    policyModeCombo.setEnabled(true);
                    policyUseTokenCheck.setEnabled(true);
                   
                    currentPolicy = dbModel.getCurrentPolicy();
                    if(currentPolicy != null) {
                        ActionFactory.get(ActionKey.POLICY_CHOOSEFB).setEnabled(true);
                        currentPolicy.addPropertyChangeListener(policyPcl);
                        if (activeLevel == PolicyLevel.A) {
                            currentPolicy.addPropertyChangeListener((ChangeListModel) policyAPoliciesLst.getModel());
                            currentPolicies = dbModel.getAPolicies();
                        } else if (activeLevel == PolicyLevel.C) {
                            currentPolicy.addPropertyChangeListener((ChangeListModel) policyCPoliciesLst.getModel());
                            currentPolicies = dbModel.getCPolicies();
                        } else if (activeLevel == PolicyLevel.I) {
                            currentPolicy.addPropertyChangeListener((ChangeListModel) policyIPoliciesLst.getModel());
                            currentPolicies = dbModel.getIPolicies();
                        }
                        PolicyModel prevPolicy = (PolicyModel) evt.getOldValue();
                        if (prevPolicy != null) {
                            prevPolicy.removePropertyChangeListener(policyPcl);
                        }
                        fillPolicyGUI();
                    } else {
                        ActionFactory.get(ActionKey.POLICY_CHOOSEFB).setEnabled(false);
                    }
                } else if (propName.equals("dbActiveLevelSet")) {
                    activeLevel = dbModel.getActiveLevel();
                } else if (propName.equals("policyFallback")) {
                    setFallBack();
                }
            }
        };

        policyAl = new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JComboBox source = (JComboBox) e.getSource();

                if (source == policyAlgorithmCombo) {
                    Algorithms alg = (Algorithms) policyAlgorithmCombo.getSelectedItem();
                    DefaultComboBoxModel compatibleModes = dbModel.getCompatibleModes(alg);
                    currentPolicy.getAction().setAlgorithm(alg);
                    currentPolicy.getAction().setMode((Modes)compatibleModes.getElementAt(0));
                    if (((Modes) compatibleModes.getElementAt(0) == Modes.NONE) && (compatibleModes.getSize() == 1)) {
                        policyModeCombo.setVisible(false);
                    } else {
                        policyModeCombo.setModel(compatibleModes);
                        policyModeCombo.setVisible(true);
                    }
                } else if (source == policyModeCombo) {
                    currentPolicy.getAction().setMode((Modes) policyModeCombo.getSelectedItem());
                }
            }
        };

        policyDl = new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                changeName();
            }

            public void removeUpdate(DocumentEvent e) {
                changeName();
            }

            public void changedUpdate(DocumentEvent e) {
                changeName();
            }
        };

        policyLsl = new ListSelectionListener() {

            public void valueChanged(ListSelectionEvent e) {
                JList source = (JList) e.getSource();
                if (e.getValueIsAdjusting() == false) {
                    if ((source instanceof javax.swing.JList) && (((javax.swing.JList) source).isSelectionEmpty() == false)) {
                        if ((source == policyAPoliciesLst) && (policyAPoliciesLst.isSelectionEmpty() == false)) {
                            policyCPoliciesLst.clearSelection();
                            policyIPoliciesLst.clearSelection();
                            ActionFactory.get(ActionKey.POLICY_DELETEA).setEnabled(true);
                            ActionFactory.get(ActionKey.POLICY_DELETEC).setEnabled(false);
                            ActionFactory.get(ActionKey.POLICY_DELETEI).setEnabled(false);
                            dbModel.setActiveLevel(PolicyLevel.A);
                            dbModel.setCurrentPolicy((PolicyModel) source.getSelectedValue());
                        } else if ((source == policyCPoliciesLst) && (policyCPoliciesLst.isSelectionEmpty() == false)) {
                            policyAPoliciesLst.clearSelection();
                            policyIPoliciesLst.clearSelection();
                            ActionFactory.get(ActionKey.POLICY_DELETEA).setEnabled(false);
                            ActionFactory.get(ActionKey.POLICY_DELETEC).setEnabled(true);
                            ActionFactory.get(ActionKey.POLICY_DELETEI).setEnabled(false);
                            dbModel.setActiveLevel(PolicyLevel.C);
                            dbModel.setCurrentPolicy((PolicyModel) source.getSelectedValue());
                        } else if ((source == policyIPoliciesLst) && (policyIPoliciesLst.isSelectionEmpty() == false)) {
                            policyAPoliciesLst.clearSelection();
                            policyCPoliciesLst.clearSelection();
                            ActionFactory.get(ActionKey.POLICY_DELETEA).setEnabled(false);
                            ActionFactory.get(ActionKey.POLICY_DELETEC).setEnabled(false);
                            ActionFactory.get(ActionKey.POLICY_DELETEI).setEnabled(true);
                            dbModel.setActiveLevel(PolicyLevel.I);
                            dbModel.setCurrentPolicy((PolicyModel) source.getSelectedValue());
                        }
                    } else {
                        ActionFactory.get(ActionKey.POLICY_DELETEA).setEnabled(false);
                        ActionFactory.get(ActionKey.POLICY_DELETEC).setEnabled(false);
                        ActionFactory.get(ActionKey.POLICY_DELETEI).setEnabled(false);
                    }
                }
            }
        };

        policyAPoliciesLst.setModel(new ChangeListModel());
        policyCPoliciesLst.setModel(new ChangeListModel());
        policyIPoliciesLst.setModel(new ChangeListModel());
       
        policyAPoliciesLst.setDragEnabled(true);
        policyCPoliciesLst.setDragEnabled(true);
        policyIPoliciesLst.setDragEnabled(true);
        policyAPoliciesLst.setTransferHandler(new ListMoveTransferHandler());
        policyCPoliciesLst.setTransferHandler(new ListMoveTransferHandler());
        policyIPoliciesLst.setTransferHandler(new ListMoveTransferHandler());
        policyAPoliciesLst.setDropMode(DropMode.INSERT);
        policyCPoliciesLst.setDropMode(DropMode.INSERT);
        policyIPoliciesLst.setDropMode(DropMode.INSERT);
       
             
       
        dbModel.addPropertyChangeListener(policyPcl);
        dbModel.addPropertyChangeListener((ChangeListModel)policyAPoliciesLst.getModel());
        dbModel.addPropertyChangeListener((ChangeListModel)policyCPoliciesLst.getModel());
        dbModel.addPropertyChangeListener((ChangeListModel)policyIPoliciesLst.getModel());
        policyNameTxt.getDocument().addDocumentListener(policyDl);
        policyFallBackLbl2.addPropertyChangeListener(policyPcl);
        policyTree.addPropertyChangeListener(policyPcl);
        policyAPoliciesLst.addListSelectionListener(policyLsl);
        policyCPoliciesLst.addListSelectionListener(policyLsl);
        policyIPoliciesLst.addListSelectionListener(policyLsl);
        policyAlgorithmCombo.addActionListener(policyAl);
        policyModeCombo.addActionListener(policyAl);
        policyFallBackLbl2.setText(rb.getString("notchosen"));
        policyFallBackLbl2.setEnabled(false);
        policyFallBackLbl2 = createHyperLinkLabel(policyFallBackLbl2);

        policyUseTokenCheck.addItemListener(new ItemListener() {

            public void itemStateChanged(ItemEvent e) {
                currentPolicy.setUseToken(policyUseTokenCheck.isSelected());
            }
        });
       
        policyAPopup.add(ActionFactory.get(ActionKey.POLICY_ADDA));
        policyAPopup.add(ActionFactory.get(ActionKey.POLICY_DELETEA));
        policyAPoliciesLst.add(policyAPopup);
       
        policyAPoliciesLst.addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent e) {
                if(e.isPopupTrigger())
                    policyAPopup.show(e.getComponent(), e.getX(), e.getY());
            }
        });
       
        policyCPopup.add(ActionFactory.get(ActionKey.POLICY_ADDC));
        policyCPopup.add(ActionFactory.get(ActionKey.POLICY_DELETEC));
        policyCPoliciesLst.add(policyCPopup);
       
        policyCPoliciesLst.addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent e) {
                if(e.isPopupTrigger())
                    policyCPopup.show(e.getComponent(), e.getX(), e.getY());
            }
        });
       
        policyIPopup.add(ActionFactory.get(ActionKey.POLICY_ADDI));
        policyIPopup.add(ActionFactory.get(ActionKey.POLICY_DELETEI));
        policyIPoliciesLst.add(policyIPopup);
       
        policyIPoliciesLst.addMouseListener(new MouseAdapter() {
            public void mouseReleased(MouseEvent e) {
                if(e.isPopupTrigger())
                    policyIPopup.show(e.getComponent(), e.getX(), e.getY());
            }
        });
    }

    private void fillPolicyGUI() {
        policyNameTxt.setText(currentPolicy.getName());
        setFallBack();

        if (activeLevel != PolicyLevel.NONE) {
            policyAlgorithmCombo.setModel(dbModel.getAlgorithmModel(activeLevel));
            if(currentPolicy.getAction() != null) {
                policyAlgorithmCombo.setSelectedItem(currentPolicy.getAction().getAlgorithm());

                Modes mode = currentPolicy.getAction().getMode();
                policyModeCombo.setSelectedItem(mode);
            }
        }

        policyUseTokenCheck.setSelected(currentPolicy.isUseToken());
    }

    private void fillTree() {
        PersonalTreeNode root = new PersonalTreeNode("root", true);
        DefaultTreeModel tm = new DefaultTreeModel(root);
        PolicyModel p1 = currentPolicy;

        boolean parentexists = true;
        while (parentexists) {
            parentexists = false;
            for (PolicyModel p2 : currentPolicies) {
                if (p2.getFallback() == p1) {
                    p1 = p2;
                    parentexists = true;
                }
            }
        }

        PersonalTreeNode tn1 = null;
        if (p1 == currentPolicy) {
            tn1 = addNode(root, p1, true, tm);
        } else {
            tn1 = addNode(root, p1, false, tm);
        }

        PersonalTreeNode parent = tn1;
        PolicyModel p2 = null;
        PolicyModel fb = p1.getFallback();
        if (fb != null) {
            while (fb != null) {
                for (PolicyModel temp : currentPolicies) {
                    if (temp == fb) {
                        p2 = temp;
                    }
                }

                PersonalTreeNode tn = null;
                if (p2 == currentPolicy) {
                    tn = addNode(parent, p2, true, tm);
                } else {
                    tn = addNode(parent, p2, false, tm);
                }
                parent = tn;
                fb = p2.getFallback();
            }
        }
        root.add(tn1);
        policyTree.setModel(tm);
        expandAll(policyTree, true);
    }

    private PersonalTreeNode addNode(PersonalTreeNode parent,
            PolicyModel child, boolean active, DefaultTreeModel tm) {

        PersonalTreeNode childNode = new PersonalTreeNode(child.getName(), active);

        tm.insertNodeInto(childNode, parent,
                parent.getChildCount());
        //policyTree.scrollPathToVisible(new TreePath(childNode.getPath()));

        return childNode;
    }

    private void expandAll(JTree tree, boolean expand) {
        TreeNode root = (TreeNode) tree.getModel().getRoot();
        expandAll(tree, new TreePath(root), expand);
    }

    private void expandAll(JTree tree, TreePath parent, boolean expand) {
        // Traverse children
        TreeNode node = (TreeNode) parent.getLastPathComponent();
        if (node.getChildCount() >= 0) {
            for (Enumeration e = node.children(); e.hasMoreElements();) {
                TreeNode n = (TreeNode) e.nextElement();
                TreePath path = parent.pathByAddingChild(n);
                expandAll(tree, path, expand);
            }
        }
        // Expansion or collapse must be done bottom-up
        if (expand) {
            tree.expandPath(parent);
        } else {
            tree.collapsePath(parent);
        }
    }

    private JLabel createHyperLinkLabel(JLabel label) {
        label.setForeground(Color.BLUE);
        label.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseEntered(MouseEvent e) {
                e.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }

            @Override
            public void mouseExited(MouseEvent e) {
                e.getComponent().setCursor(Cursor.getDefaultCursor());
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                if (activeLevel == PolicyLevel.A)
                    policyAPoliciesLst.setSelectedValue(currentPolicy.getFallback(), true);
                else if (activeLevel == PolicyLevel.C) {
                    policyCPoliciesLst.setSelectedValue(currentPolicy.getFallback(), true); }
                else if (activeLevel == PolicyLevel.I)
                    policyIPoliciesLst.setSelectedValue(currentPolicy.getFallback(), true);
            }
        });

        return label;
    }
   
    private void setFallBack() {
        PolicyModel fallBack = currentPolicy.getFallback();
        if (fallBack != null) {
            policyFallBackLbl2.setText("<html><u>" + currentPolicy.getFallback().getName() + "</u></html>");
            policyFallBackLbl2.setEnabled(true);
        } else {
            policyFallBackLbl2.setText(rb.getString("notchosen"));
            policyFallBackLbl2.setEnabled(false);
        }
       
        fillTree();
    }
   
    public void createPolicy() {
        //policyTree.setModel(new DefaultTreeModel(new PersonalTreeNode("root", true)));
        Map<ActionTupel, String> actions = null;  
        PolicyModel policy = new PolicyModel();
        policy.setId(PolicyUtilities.getNewPolicyId());
        policy.setName(rb.getString("newpolicy") + "_" + new Integer(policy.getId()).toString());
        policy.setLevel(activeLevel);
        switch(activeLevel) {
            case A:
                 actions = dbModel.getAActions();
                 policy.setAction(new ActionModel(actions, "-1"));
                if(dbModel.addAPolicy(policy)) {
                    PolicyUtilities.setPolicyRanks(dbModel.getAPolicies(), true);
                    dbModel.setCurrentPolicy(policy);
                    policyAPoliciesLst.setModel(dbModel.getAPoliciesAsListModel());
                    currentPolicy.addPropertyChangeListener((ChangeListModel) policyAPoliciesLst.getModel());
                }               
                break;
            case C:
                actions = dbModel.getCActions();
                policy.setAction(new ActionModel(actions, "-1"));
                if(dbModel.addCPolicy(policy)) {
                    PolicyUtilities.setPolicyRanks(dbModel.getCPolicies(), true);
                    dbModel.setCurrentPolicy(policy);
                    policyCPoliciesLst.setModel(dbModel.getCPoliciesAsListModel());
                    currentPolicy.addPropertyChangeListener((ChangeListModel) policyCPoliciesLst.getModel());
                }   
                break;
            case I:
                policy.setRank(policyIPoliciesLst.getModel().getSize()+1);
                actions = dbModel.getIActions();
                policy.setAction(new ActionModel(actions, "-1"));
                if(dbModel.addIPolicy(policy)) {
                    PolicyUtilities.setPolicyRanks(dbModel.getIPolicies(), true);
                    dbModel.setCurrentPolicy(policy);
                    policyIPoliciesLst.setModel(dbModel.getIPoliciesAsListModel());
                    currentPolicy.addPropertyChangeListener((ChangeListModel) policyIPoliciesLst.getModel());
                }
                break;
        }
    }
   
    public void removePolicy(PolicyLevel level) {
        boolean isValid = false;
       
            // prüfen, ob Policy Fallback besitzt oder als Fallback genutzt wird --> !isValid
        switch(level) {
            case A:
                isValid = checkPolicyReferences(currentPolicy, currentPolicies);
                break;              
            case C:
                isValid = checkPolicyReferences(currentPolicy, currentPolicies);
                break;
            case I:
                isValid = checkPolicyReferences(currentPolicy, currentPolicies);
                break;
        }
       
        if(isValid) {
                // Policy besitzt keine Referenzen und kann einfach gelöscht werden
            switch(level) {
                case A:
                    if(!policyAPoliciesLst.isSelectionEmpty()) {
                        if(dbModel.removeAPolicy(currentPolicy)) {
                            if(currentPolicies.isEmpty()) {
                                clearPolicyGUI();
                            }
                        } else {
                            // TODO Fehlerbehebung
                        }                   
                    }
                    break;
                case C:
                    if(!policyCPoliciesLst.isSelectionEmpty()) {
                        if(dbModel.removeCPolicy(currentPolicy)) {
                            if(currentPolicies.isEmpty()) {
                                clearPolicyGUI();
                            }
                        } else {
                            // TODO Fehlerbehebung
                        }                   
                    }               
                    break;
                case I:
                    if(!policyIPoliciesLst.isSelectionEmpty()) {
                        if(dbModel.removeIPolicy(currentPolicy)) {
                            if(currentPolicies.isEmpty()) {
                                clearPolicyGUI();
                            }
                        } else {
                            // TODO Fehlerbehebung
                        }                   
                    }               
                    break;
            }
        } else {
                // Policy besitzt Referenz: entweder nicht löschen, oder mitsamt Refs.
            List<PolicyModel> references = new ArrayList();
           
                // zu löschende Policy hinzufügen
            references.add(currentPolicy);
           
                // alle Fallbacks der zu ermittelnden Policy ermitteln
            for (PolicyModel policy = currentPolicy.getFallback(); policy != null; policy = policy.getFallback()) {
                references.add(policy);
            }
           
                // alle Elternpolicies zur Fallback ermitteln
            PolicyModel parent = PolicyUtilities.getParentPolicy(currentPolicy, currentPolicies);
            if(parent != null)  {
                while(parent != null) {
                    references.add(0, parent);
                    parent = PolicyUtilities.getParentPolicy(parent, currentPolicies);
                }
            }
           
                // Messagestring aufbauen
            String temp = "";
            String message = "";
            for(int x=0; x<references.size(); x++) {
                PolicyModel p = references.get(x);
                if(!p.equals(currentPolicy))
                    temp += "<b>" + references.get(x) + "</b></center><br>";
                else
                    temp += references.get(x) + "</center><br>";
            }
           
                // Policynamen in MessageString einfügen
            mb = PolicyUtilities.getMessageBundle();
            message = PolicyUtilities.replacePlaceHolder(currentPolicy.getName(), mb.getString("referencesdel"));
                //  referenzierte Policies in Messagestring einfügen
            message = PolicyUtilities.replacePlaceHolder(temp, message);
           
            int i = PolicyUtilities.showMessageDialog(message, JOptionPane.QUESTION_MESSAGE);
           
            if(i==0) {
                switch(level) {
                    case A:
                        removeFallBackRelations(references);
                        for (int x=0; x<references.size(); x++)
                            dbModel.removeAPolicy(references.get(x));
                        break;           
                    case C:
                        removeFallBackRelations(references);
                        for (int x=0; x<references.size(); x++)
                            dbModel.removeCPolicy(references.get(x));
                        break;
                    case I:
                        removeFallBackRelations(references);
                        for (int x=0; x<references.size(); x++)
                            dbModel.removeIPolicy(references.get(x));
                        break;
                }
               
                if(currentPolicies.isEmpty()) {
                    clearPolicyGUI();
                }
            }

        }
    }
   
    private void removeFallBackRelations(List<PolicyModel> policies) {
        for(int i=0; i<policies.size(); i++) {
            policies.get(i).setFallback(null);
        }
    }
   
    private boolean checkPolicyReferences(PolicyModel policy, List<PolicyModel> policies) {
        PolicyModel fb = policy.getFallback();
        boolean isChild = false;
       
        for(int i=0; i<policies.size(); i++) {
            if(policies.get(i).getFallback() == policy) {
                isChild = true;
                break;
            }
        }
       
        if((fb == null) && (!isChild)) {
            return true;
        } else {
            return false;
        }
    }
   
    private void clearPolicyGUI() {
        policyNameTxt.setText("");
        policyFallBackLbl2.setText("");
        policyTree.setModel(new DefaultTreeModel(new PersonalTreeNode("root", true)));
       
        switch(dbModel.getActiveLevel()) {
            case A:
                policyAlgorithmCombo.setSelectedItem(Algorithms.NOTDEFINEDA);
                break;           
            case C:
                policyAlgorithmCombo.setSelectedItem(Algorithms.NOTDEFINEDC);
                break;
            case I:
                policyAlgorithmCombo.setSelectedItem(Algorithms.NOTDEFINEDI);
                break;
        }

        policyUseTokenCheck.setSelected(false);
    }
   
    private boolean checkName(String name) {
        List<PolicyModel> policies = null;
        switch(dbModel.getActiveLevel()) {
            case A:
                policies = dbModel.getAPolicies();
                break;
            case C:
                policies = dbModel.getCPolicies();
                break;
            case I:
                policies = dbModel.getIPolicies();
                break;
        }
              
        for(int i=0; i<policies.size(); i++) {
            if((policies.get(i).getName().equals(name)) && (policies.get(i) != currentPolicy))
                return false;
        }
       
        return true;
    }
   
    private void changeName() {
        String name = policyNameTxt.getText();
        if(checkName(name))
            policyNameTxt.setBackground(Color.white);     
        else
            policyNameTxt.setBackground(Color.red);  
        currentPolicy.setName(name);
        fillTree();
   }  
   
   public void refreshPolicyLists() {
       switch(dbModel.getActiveLevel()) {
        case A:
            policyAPoliciesLst.setModel(dbModel.getAPoliciesAsListModel());
            currentPolicy.addPropertyChangeListener((ChangeListModel) policyAPoliciesLst.getModel());
            break;
        case C:
            policyCPoliciesLst.setModel(dbModel.getCPoliciesAsListModel());
            currentPolicy.addPropertyChangeListener((ChangeListModel) policyCPoliciesLst.getModel());
            break;
        case I:
            policyIPoliciesLst.setModel(dbModel.getIPoliciesAsListModel());
            currentPolicy.addPropertyChangeListener((ChangeListModel) policyIPoliciesLst.getModel());
            break;     
        }   
   }
}
TOP

Related Classes of de.netsysit.controller.PolicyController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.