RoleRangeDisplay.java :  » Content-Management-System » harmonise » org » openharmonise » him » metadata » range » swing » rolehandling » Java Open Source

Java Open Source » Content Management System » harmonise 
harmonise » org » openharmonise » him » metadata » range » swing » rolehandling » RoleRangeDisplay.java
/*
 * The contents of this file are subject to the 
 * Mozilla Public License Version 1.1 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
 * See the License for the specific language governing rights and 
 * limitations under the License.
 *
 * The Initial Developer of the Original Code is Simulacra Media Ltd.
 * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
 *
 * All Rights Reserved.
 *
 * Contributor(s):
 */
package org.openharmonise.him.metadata.range.swing.rolehandling;

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.rmi.*;

import javax.swing.*;
import javax.xml.rpc.*;

import org.openharmonise.him.*;
import org.openharmonise.him.context.StateHandler;
import org.openharmonise.him.harmonise.*;
import org.openharmonise.him.metadata.range.swing.*;
import org.openharmonise.him.metadata.range.swing.valuehandling.*;
import org.openharmonise.him.window.messages.*;
import org.openharmonise.vfs.authentication.*;
import org.openharmonise.vfs.context.*;
import org.openharmonise.vfs.metadata.*;
import org.openharmonise.vfs.servers.*;


/**
 * 
 * @author Matthew Large
 * @version $Revision: 1.2 $
 *
 */
public class RoleRangeDisplay
  extends AbstractRangeDisplay
  implements RangeDisplay, LayoutManager, ActionListener, Runnable {
    
  private ValueRangeDisplay m_valueDisplay = null;
  
  private JCheckBox m_superCheckBox = null;
  
  private JLabel m_superLabel = null;
  
  private static final String WAIT_LABEL = "SET_SUPER-ACTION";

  /**
   * @param propInstance
   */
  public RoleRangeDisplay(PropertyInstance propInstance) {
    super(propInstance);
    this.setup();
  }
  
  private void setup() {
    this.setLayout(this);
    this.m_valueDisplay = new ValueRangeDisplay(super.getPropertyInstance());
    this.add(m_valueDisplay.getPanel());
    
    this.m_superCheckBox = new JCheckBox();
    this.m_superCheckBox.setActionCommand("SUPER_CHECK");
    this.m_superCheckBox.addActionListener(this);
    if(this.isSuperUser()){
      m_superCheckBox.setSelected(true);
      this.m_valueDisplay.setEnabled(false);
    }
    this.add(this.m_superCheckBox);

    String fontName = "Dialog";
    int fontSize = 11;
    Font font = new Font(fontName, Font.PLAIN, fontSize);
    
    this.m_superLabel = new JLabel("Super User");
    this.m_superLabel.setFont(font);
    this.add(this.m_superLabel);
    
    SwingUtilities.invokeLater(this);
  }
  /**
   * Set the super user flag for the User currently being edited. 
   * @param bIsSuper
   */
  private void setIsSuperUser(boolean bIsSuper) {
    Server server = null;
    server = ServerList.getInstance().getHarmoniseServer();
    URI uri = server.getURI();
    
    String sURI = uri.getScheme() + "://" + uri.getHost() + ":" + uri.getPort() + "/webdav/services/HarmoniseService"; 
    URL url = null;
    try {
      url = new URL(sURI);
    } catch (MalformedURLException e2) {
      e2.printStackTrace();
      System.exit(1);  
    }
    
    AuthInfo auth = server.getVFS().getAuthentication();
    
    try {
      UserConfigClient.setIsSuperUser(url, auth.getUsername(), auth.getPassword(), this.getPropertyInstance().getVirtualFile().getFileName(), bIsSuper);
      if(bIsSuper) {
        MessageHandler.getInstance().fireMessageEvent("User \"" + this.getPropertyInstance().getVirtualFile().getVFS().getVirtualFileSystemView().getDisplayName(this.getPropertyInstance().getVirtualFile()) + "\" is now a Super User.", MessageHandler.TYPE_CONFIRM);
      } else {
        MessageHandler.getInstance().fireMessageEvent("User \"" + this.getPropertyInstance().getVirtualFile().getVFS().getVirtualFileSystemView().getDisplayName(this.getPropertyInstance().getVirtualFile()) + "\" is not a Super User anymore.", MessageHandler.TYPE_CONFIRM);
      }
    } catch (RemoteException e) {
      e.printStackTrace();
      MessageHandler.getInstance().fireMessageEvent("Error changing Super User status for user \"" + this.getPropertyInstance().getVirtualFile().getVFS().getVirtualFileSystemView().getDisplayName(this.getPropertyInstance().getVirtualFile()) + "\".There was a problem communicating with the server.", MessageHandler.TYPE_ERROR);
    } catch (ServiceException e) {
      MessageHandler.getInstance().fireMessageEvent("Error changing Super User status for user \"" + this.getPropertyInstance().getVirtualFile().getVFS().getVirtualFileSystemView().getDisplayName(this.getPropertyInstance().getVirtualFile()) + "\".There was a problem communicating with the server.", MessageHandler.TYPE_ERROR);
      e.printStackTrace();
    }  
  }
  /**
   * Check whether the User curently being edited is a super user
   * @return true or false
   */
  private boolean isSuperUser() {
    boolean bRetn = false;
    Server server = null;
    server = ServerList.getInstance().getHarmoniseServer();
    URI uri = server.getURI();
    
    String sURI = uri.getScheme() + "://" + uri.getHost() + ":" + uri.getPort() + "/webdav/services/HarmoniseService"; 
    URL url = null;
    try {
      url = new URL(sURI);
    } catch (MalformedURLException e2) {
      e2.printStackTrace();
      System.exit(1);  
    }
    
    AuthInfo auth = server.getVFS().getAuthentication();
    
    try {
      if( UserConfigClient.isSuperUser(url, auth.getUsername(), auth.getPassword(),getPropertyInstance().getVirtualFile().getFileName()) ) {
        bRetn=true;
      }
    } catch (RemoteException e) {
      e.printStackTrace();
    } catch (ServiceException e) {
      e.printStackTrace();
    }
    return bRetn;
  }
  /* (non-Javadoc)
   * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
   */
  public void actionPerformed(ActionEvent ae) {
    if(ae.getActionCommand().equals("SUPER_CHECK")) {
      boolean bSelected = this.m_superCheckBox.isSelected();
      this.m_valueDisplay.setEnabled(!bSelected);
      StateHandler.getInstance().addWait(WAIT_LABEL);
      this.setIsSuperUser(bSelected);
      StateHandler.getInstance().removeWait(WAIT_LABEL);
    }
  }

  /* (non-Javadoc)
   * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isMetadataValid()
   */
  public boolean isMetadataValid() {
    return this.m_valueDisplay.isMetadataValid();
  }

  /* (non-Javadoc)
   * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
   */
  public JPanel getPanel() {
    return this;
  }

  /* (non-Javadoc)
   * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
   */
  public void layoutContainer(Container arg0) {
    this.m_valueDisplay.setSize(this.m_valueDisplay.getPreferredSize());
    this.m_valueDisplay.setLocation(0,0);
    
    this.m_superCheckBox.setSize(this.m_superCheckBox.getPreferredSize());
    this.m_superCheckBox.setLocation(20, this.m_valueDisplay.getHeight()+10);
    
    this.m_superLabel.setSize(this.m_superLabel.getPreferredSize());
    this.m_superLabel.setLocation(22 + this.m_superCheckBox.getWidth(), this.m_valueDisplay.getHeight()+13);
  }

  /* (non-Javadoc)
   * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
   */
  public Dimension minimumLayoutSize(Container arg0) {
    return this.m_valueDisplay.minimumLayoutSize(arg0);
  }

  /* (non-Javadoc)
   * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
   */
  public Dimension preferredLayoutSize(Container arg0) {
    return this.m_valueDisplay.preferredLayoutSize(arg0);
  }

  /* (non-Javadoc)
   * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
   */
  public void removeLayoutComponent(Component arg0) {
    // NO-OP
  }

  /* (non-Javadoc)
   * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
   */
  public void addLayoutComponent(String arg0, Component arg1) {
    // NO-OP
  }

  /* (non-Javadoc)
   * @see java.awt.Component#getPreferredSize()
   */
  public Dimension getPreferredSize() {    
    int nWidth = this.getParent().getWidth() - 40;
    int nHeight = 200;
    return new Dimension(nWidth, nHeight);
  }

  /* (non-Javadoc)
   * @see java.lang.Runnable#run()
   */
  public void run() {
    if(this.isSuperUser()){
      m_superCheckBox.setSelected(true);
      this.m_valueDisplay.setEnabled(false);
    }
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.