package graphbrowser;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;

import jow.*;
import jowgraphservice.*;
import jowadmin.*;
import graph.*;

public class GraphAclWin implements ActionListener,WindowListener{//extends Frame
  URL url;
  JOWGraphService ds;

  // frame for ition acl
  Frame paclFrame=new Frame();
  Button addRead=new Button("Add User Read");
  Button delRead=new Button("Del User Read");
  Button addWrite=new Button("Add User Write");
  Button delWrite=new Button("Del User Write");
  Button addAdmin=new Button("Add User Admin");
  Button delAdmin=new Button("Del User Admin");

  Button addGroupRead=new Button("Add Group Read");
  Button delGroupRead=new Button("Del Group Read");
  Button addGroupWrite=new Button("Add Group Write");
  Button delGroupWrite=new Button("Del Group Write");
  Button addGroupAdmin=new Button("Add Group Admin");
  Button delGroupAdmin=new Button("Del Group Admin");

  List   userRead=new List(6,false);
  List   userWrite=new List(6,false);
  List   userAdmin=new List(6,false);
  List   groupRead=new List(6,false);
  List   groupWrite=new List(6,false);
  List   groupAdmin=new List(6,false);
  List   puser=new List(10,false);
  List   pgroup=new List(10,false);

  UserDialog myud;
  GroupDialog mygd;

  Panel  aclp1=new Panel();
  Panel  aclp2=new Panel();
  Panel  aclp3=new Panel();
  Panel  aclp123=new Panel();

  Panel  aclp4=new Panel();
  Panel  aclp5=new Panel();
  Panel  aclp6=new Panel();
  Panel  aclp456=new Panel();

  public void updateGroupAcl() {
    Vector newgacl=new Vector();
    Vector allgroup=new Vector();
    try {
       newgacl=ds.listAclGroupEntry(url);
       allgroup=ds.getGroupList();
    } catch (JOWException e) {
      LabelDialog ld=new LabelDialog(paclFrame,"Alarm","Operation Fail."+e.getMessage());
      ld.show();
        return;
    }

    groupRead.removeAll();
    groupWrite.removeAll();
    groupAdmin.removeAll();

    pgroup.removeAll();

    Enumeration e=newgacl.elements();
    for (;e.hasMoreElements();) {
        MyAclEntryImpl entry=(MyAclEntryImpl ) e.nextElement();
        JOWPermissionImpl perm=entry.getPerm();
        int type=perm.getType();
        String name=entry.getName();
        if ( type== JOWPermissionImpl.READ)
            { groupRead.addItem(name); }
         else if (type== JOWPermissionImpl.WRITE)
            {groupWrite.addItem(name); }
         else if (type== JOWPermissionImpl.ADMIN)
            {groupAdmin.addItem(name); }
        allgroup.removeElement(name);
    }
    Enumeration groupEnum = allgroup.elements();
    for (;groupEnum.hasMoreElements();) {
        //System.out.println( (String) userEnum.nextElement() );
        pgroup.addItem((String) groupEnum.nextElement());
      }

  updateUserAcl();
  }

  public void updateUserAcl() {
    Vector newuacl=new Vector();
    Vector alluser=new Vector();
    try {
       newuacl=ds.listAclUserEntry(url);
       alluser=ds.getUserList();
    } catch (JOWException e) {
      LabelDialog ld=new LabelDialog(paclFrame,"Alarm","Operation Fail."+e.getMessage());
      ld.show();
        System.out.println("Domain Exception" +e.getMessage());
        return;
    }

    userRead.removeAll();
    userWrite.removeAll();
    userAdmin.removeAll();

    puser.removeAll();

    Enumeration e=newuacl.elements();
    for (;e.hasMoreElements();) {
        MyAclEntryImpl entry=(MyAclEntryImpl ) e.nextElement();
        JOWPermissionImpl perm=entry.getPerm();
        int type=perm.getType();
        String name=entry.getName();
        if ( type== JOWPermissionImpl.READ)
            { userRead.addItem(name); }
         else if (type== JOWPermissionImpl.WRITE)
            { userWrite.addItem(name); }
         else if (type== JOWPermissionImpl.ADMIN)
            { userAdmin.addItem(name); }
        alluser.removeElement(name);
    }
    Enumeration userEnum = alluser.elements();
    for (;userEnum.hasMoreElements();) {
        //System.out.println( (String) userEnum.nextElement() );
        puser.addItem((String) userEnum.nextElement());
      }

  }


  public void addUserRead() throws JOWException {
    String user=puser.getSelectedItem();
    if (user==null) return;
    ds.setAclUserEntry(url,user,JOWPermissionImpl.READ);
    updateUserAcl();
  }

  public void addUserWrite() throws JOWException {
    String user=puser.getSelectedItem();
    if (user==null) return;
    ds.setAclUserEntry(url,user,JOWPermissionImpl.WRITE);
    updateUserAcl();
  }

  public void addUserAdmin() throws JOWException {
    String user=puser.getSelectedItem();
    if (user==null) return;
    ds.setAclUserEntry(url,user,JOWPermissionImpl.ADMIN);
    updateUserAcl();
  }

  public void delUserRead() throws JOWException {
    String user=userRead.getSelectedItem();
    if (user==null) return;
    ds.delAclUserEntry(url,user);
    updateUserAcl();
  }

  public void delUserWrite() throws JOWException {
    String user=userWrite.getSelectedItem();
    if (user==null) return;
    ds.delAclUserEntry(url,user);
    updateUserAcl();
  }

  public void delUserAdmin() throws JOWException {
    String user=userAdmin.getSelectedItem();
    if (user==null) return;
    ds.delAclUserEntry(url,user);
    updateUserAcl();
  }

  public void addGroupRead() throws JOWException {
    String group=pgroup.getSelectedItem();
    if (group==null) return;
    ds.setAclGroupEntry(url,group,JOWPermissionImpl.READ);
    updateGroupAcl();

  }

  public void addGroupWrite() throws JOWException {
    String group=pgroup.getSelectedItem();
    if (group==null) return;
    ds.setAclGroupEntry(url,group,JOWPermissionImpl.WRITE);
    updateGroupAcl();

  }

  public void addGroupAdmin() throws JOWException {
    String group=pgroup.getSelectedItem();
    if (group==null) return;
    ds.setAclGroupEntry(url,group,JOWPermissionImpl.ADMIN);
    updateGroupAcl();

  }

  public void delGroupRead() throws JOWException {
    String group=groupRead.getSelectedItem();
    if (group==null) return;
    ds.delAclGroupEntry(url,group);
    updateGroupAcl();

  }

  public void delGroupWrite() throws JOWException {
    String group=groupWrite.getSelectedItem();
    if (group==null) return;
    ds.delAclGroupEntry(url,group);
    updateGroupAcl();
  }

  public void delGroupAdmin() throws JOWException {
    String group=groupAdmin.getSelectedItem();
    if (group==null) return;
    ds.delAclGroupEntry(url,group);
    updateGroupAcl();
  }


  public GraphAclWin(GraphWindow gw,GraphObject gobj) {

    //url=gw.aGraph.getUID();

    if (gobj instanceof Graph) { paclFrame.setTitle("Graph:"+gobj.getLabel());
                                 url=gobj.getUID(); }
        else { paclFrame.setTitle("Content Object:"+gobj.getLabel());
               // node
               url=((Node) gobj).getContent(); // now it is sure that there is content object
        }
    ds=gw.jowGraphService;
    Panel aclp11=new Panel();
    aclp11.setLayout(new GridLayout(3,1,0,0) );
    //aclp1.add(new Label("USER READ"));
    aclp11.add(addRead);
    aclp11.add(delRead);
    aclp1.setLayout(new GridLayout(2,1,0,0));
    aclp1.add("North",userRead);
    aclp1.add("South",aclp11);

    addRead.addActionListener(this);
    delRead.addActionListener(this);

    Panel aclp22=new Panel();
    aclp22.setLayout(new GridLayout(3,1,0,0) );
    aclp22.add(addWrite);
    aclp22.add(delWrite);
    aclp2.setLayout(new GridLayout(2,1,0,0));
    aclp2.add("North",userWrite);
    aclp2.add("South",aclp22);

    addWrite.addActionListener(this);
    delWrite.addActionListener(this);

    Panel aclp33=new Panel();
    aclp33.setLayout(new GridLayout(3,1,0,0) );
    aclp33.add(addAdmin);
    aclp33.add(delAdmin);
    aclp3.setLayout(new GridLayout(2,1,0,0));
    aclp3.add("North",userAdmin);
    aclp3.add("South",aclp33);

    addAdmin.addActionListener(this);
    delAdmin.addActionListener(this);

    aclp123.setLayout(new FlowLayout());
    aclp123.add(aclp1);
    aclp123.add(aclp2);
    aclp123.add(aclp3);
    aclp123.add(puser);


    Panel aclp44=new Panel();
    aclp44.setLayout(new GridLayout(3,1,0,0) );
    aclp44.add(addGroupRead);
    aclp44.add(delGroupRead);
    aclp4.setLayout(new GridLayout(2,1,0,0));
    aclp4.add(groupRead);
    aclp4.add(aclp44);

    addGroupRead.addActionListener(this);
    delGroupRead.addActionListener(this);

    Panel aclp55=new Panel();
    aclp55.setLayout(new GridLayout(3,1,0,0) );
    aclp55.add(addGroupWrite);
    aclp55.add(delGroupWrite);
    aclp5.setLayout(new GridLayout(2,1,0,0));
    aclp5.add(groupWrite);
    aclp5.add(aclp55);

    addGroupWrite.addActionListener(this);
    delGroupWrite.addActionListener(this);

    Panel aclp66=new Panel();
    aclp66.setLayout(new GridLayout(3,1,0,0) );
    aclp66.add(addGroupAdmin);
    aclp66.add(delGroupAdmin);
    aclp6.setLayout(new GridLayout(2,1,0,0));
    aclp6.add(groupAdmin);
    aclp6.add(aclp66);

    addGroupAdmin.addActionListener(this);
    delGroupAdmin.addActionListener(this);


    aclp456.setLayout(new FlowLayout());
    aclp456.add(aclp4);
    aclp456.add(aclp5);
    aclp456.add(aclp6);
    aclp456.add(pgroup);

    paclFrame.setLayout(new GridLayout(2,1,0,0));
    paclFrame.add("North",aclp123);
    paclFrame.add("South",aclp456);

    paclFrame.addWindowListener(this);

    paclFrame.setSize(600,400);
    paclFrame.show();
    updateGroupAcl();
    updateUserAcl();


  }

  public void actionPerformed(ActionEvent e) {
    Object esource = e.getSource();
    try {
     if (esource == addRead) {
            addUserRead();
        }else if (esource == addWrite) {
            addUserWrite();
        }else if (esource == addAdmin) {
            addUserAdmin();
        }else if (esource == delRead) {
            delUserRead();
        }else if (esource == delWrite) {
            delUserWrite();
        }else if (esource == delAdmin) {
            delUserAdmin();

        }else if (esource == addGroupRead) {
            addGroupRead();
        }else if (esource == addGroupWrite) {
            addGroupWrite();
        }else if (esource == addGroupAdmin) {
            addGroupAdmin();
        }else if (esource == delGroupRead) {
            delGroupRead();
        }else if (esource == delGroupWrite) {
            delGroupWrite();
        }else if (esource == delGroupAdmin) {
            delGroupAdmin();

        }
      }catch (JOWException ee) {
      LabelDialog ld=new LabelDialog(paclFrame,"Alarm","Operation Fail."+ee.getMessage());
      ld.show();

        System.out.println("Action:"+ee.getMessage());
       }
  }

// implement the WindowListener interface
public void windowActivated(WindowEvent e) {
}

public void windowClosed(WindowEvent e) {
}

public void windowClosing(WindowEvent e) {
  /*if (e.getSource()==this) {
    //MessageDialog quitdlg=new MessageDialog(this,"Alarm","Really want to quit?");
    //quitdlg.show();
    //if (quitdlg.getValue()) {
    try {
        ds.quit();
    } catch (JOWException e3) {
             System.out.println("request for server to save failed");
    }
        dispose();
        //System.exit(0);
        }
    } else if (e.getSource()==userframe) {
        userframe.setVisible(false);
    }  else if (e.getSource()==groupframe) {
        groupframe.setVisible(false);
    }else  */

    if (e.getSource()==paclFrame) {
        paclFrame.setVisible(false);
    }

}

public void windowDeactivated(WindowEvent e) {
}

public void windowDeiconified(WindowEvent e) {
}

public void windowIconified(WindowEvent e) {
}

public void windowOpened(WindowEvent e) {
}



 }

