AbstractHIMAction.java :  » Content-Management-System » harmonise » org » openharmonise » him » actions » Java Open Source

Java Open Source » Content Management System » harmonise 
harmonise » org » openharmonise » him » actions » AbstractHIMAction.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.actions;

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

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

import org.openharmonise.him.actions.rules.*;
import org.openharmonise.him.harmonise.UserConfigClient;
import org.openharmonise.him.window.session.SessionEventData;
import org.openharmonise.vfs.*;
import org.openharmonise.vfs.authentication.AuthInfo;
import org.openharmonise.vfs.context.*;
import org.openharmonise.vfs.servers.*;


/**
 * Class to make development of Actions within Harmonise Information Manager easier.
 * 
 * @author Matthew Large
 * @version $Revision: 1.4 $
 *
 */
public abstract class AbstractHIMAction implements ActionListener {

  /**
   * Virtual file that is the focus of the action.
   */
  private VirtualFile m_vfFile = null;
  
  /**
   * Button that triggers action.
   */
  private JButton m_button = null;
  
  /**
   * Menu item that triggers action.
   */
  private JMenuItem m_menuItem = null;
  
  /**
   * List of {@link EnableRule} objects.
   */
  private ArrayList m_aEnableRules = new ArrayList();

  /**
   * true if the current user has permissions to see this action.
   */
  protected boolean m_bShow = false;
  
  /**
   * true if the current user's persmissions have been checked.
   */
  protected boolean m_bUserChecked = false;

  /**
   * 
   */
  public AbstractHIMAction() {
    super();
  }
  
  /**
   * Constructs a new abstract action.
   * 
   * @param vfFile Virtual file that action will focus on
   */
  public AbstractHIMAction(VirtualFile vfFile) {
    super();
    m_vfFile = vfFile;
  }

  /* (non-Javadoc)
   * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
   */
  public abstract void actionPerformed(ActionEvent arg0);

  /**
   * Returns the virtual file that the action is focused on.
   * 
   * @return Virtual file
   */
  protected VirtualFile getPrimaryFile() {
    VirtualFile vfFile = null;
    
    if(this.m_vfFile!=null) {
      vfFile = this.m_vfFile;
    } else {
      vfFile = this.getLastContextFile();
    }
    
    return vfFile;
  }
  
  /**
   * Returns the virtual file from the last file context event.
   * 
   * @return Last file context virtual file
   */
  protected VirtualFile getLastContextFile() {
    ContextEvent ce = ContextHandler.getInstance().getLastEvent(ContextType.CONTEXT_FILES);
    return ce.getVFS().getVirtualFile(ce.getPath()).getResource();
  }

  /**
   * Returns the collection that the action is focused on.
   * 
   * @return Collection
   */
  protected VirtualFile getPrimaryDirectory() {
    VirtualFile vfFile = null;
    
    if(this.m_vfFile!=null) {
      vfFile = this.m_vfFile;
    } else {
      vfFile = this.getLastContextDirectory();
    }
    
    return vfFile;
  }
  
  /**
   * Returns the virtual file from the last collection context event.
   * 
   * @return Last collection context virtual file
   */
  protected VirtualFile getLastContextDirectory() {
    ContextEvent ce = ContextHandler.getInstance().getLastEvent(ContextType.CONTEXT_DIRS);
    if(ce!=null && ce.getVFS()!=null) {
      return ce.getVFS().getVirtualFile(ce.getPath()).getResource();  
    } else {
      return null;
    }
  }

  /* (non-Javadoc)
   * @see com.simulacramedia.contentmanager.actions.CMAction#getButton()
   */
  public JButton getButton() {
    if(this.m_button==null) {
      this.m_button = new JButton(this.getText());
      this.m_button.setToolTipText(this.getToolTip());
      this.m_button.setIcon(this.getIcon());
      this.m_button.addActionListener(this);
    
      String fontName = "Dialog";
      int fontSize = 11;
      Font font = new Font(fontName, Font.PLAIN, fontSize);
      this.m_button.setFont(font);
      Dimension dim = new Dimension(this.m_button.getPreferredSize().width, 25);
      this.m_button.setPreferredSize(dim);
    }
    
    return this.m_button;
  }

  /* (non-Javadoc)
   * @see com.simulacramedia.contentmanager.actions.CMAction#getMenuItem()
   */
  public JMenuItem getMenuItem() {
    if(this.m_menuItem==null) {
      this.m_menuItem = new JMenuItem(this.getText());
      this.m_menuItem.setToolTipText(this.getToolTip());
      this.m_menuItem.setIcon(this.getIcon());
      this.m_menuItem.addActionListener(this);
      this.m_menuItem.setMnemonic(this.getMnemonic().charAt(0));
    
      String fontName = "Dialog";
      int fontSize = 11;
      Font font = new Font(fontName, Font.PLAIN, fontSize);
      this.m_menuItem.setFont(font);
    }
    
    return this.m_menuItem;
  }
  
  /**
   * Adds a rule to action.
   * 
   * @param rule Rule to add
   */
  protected void addEnableRule(EnableRule rule) {
    this.m_aEnableRules.add(rule);
  }
  
  /**
   * Checks if an action, and therefore its buttons and menu items,
   * is enabled. This is based on the file that the action is
   * focuesed on, the context and the enable rules.
   * 
   * @param ce Context event to check against
   * @return true if the action is enabled
   */
  public boolean isEnabled(ContextEvent ce) {
    boolean bEnabled = true;
    
    if(ce.CONTEXT_TYPE!=ContextType.CONTEXT_TABS) {
      VirtualFile vfFile = ce.getVFS().getVirtualFile(ce.getPath()).getResource();
      if(this.m_aEnableRules.size()>0) {
        bEnabled = false;
        Iterator itor = this.m_aEnableRules.iterator();
        while(itor.hasNext()) {
          if( ((EnableRule)itor.next()).isEnabled(vfFile) ) {
            bEnabled = true;
          }
        }
      }
    } else if(ce.CONTEXT_TYPE==ContextType.CONTEXT_TABS) { 
      if(this.m_aEnableRules.size()>0) {
        bEnabled = false;
        Iterator itor = this.m_aEnableRules.iterator();
        while(itor.hasNext()) {
          EnableRule tempRule = (EnableRule)itor.next();
          if( tempRule instanceof TabRule && ((TabRule)tempRule).isEnabled(ce.getMessage().trim()) ) {
            bEnabled = true;
          }
        }
      }
    } else {
      bEnabled=true;
    }
    
    this.setEnabled(bEnabled);
    
    return bEnabled;
  }
  
  /**
   * Sets whether the action is enabled or not.
   * 
   * @param bEnabled true to set the action to be enabled
   */
  public void setEnabled(boolean bEnabled) {
    if(this.m_button!=null) {
      this.m_button.setEnabled(bEnabled);
    }
    if(this.m_menuItem!=null) {
      this.m_menuItem.setEnabled(bEnabled);
    }
  }
  
  /**
   * Returns the text name of the action.
   * 
   * @return Name
   */
  public abstract String getText();
  
  /**
   * Returns the tooltip text for the action.
   * 
   * @return Tooltip text
   */
  public abstract String getToolTip();
  
  /**
   * Returns the icon for the action.
   * 
   * @return Icon
   */
  public abstract Icon getIcon();
  
  /**
   * Returns the Mnemonic for the action.
   * 
   * @return Mnemonic
   */
  public abstract String getMnemonic();
  
  /**
   * Utility method to help implementors fire session events.
   * 
   * @param sMessage Message
   * @param vfs Virtual file system
   * @param sPath Full path
   * @param sSessionEventType Event type
   */
  protected void fireSessionEvent(String sMessage, AbstractVirtualFileSystem vfs, String sPath, String sSessionEventType) {
    try {
      SessionEventData sed = new SessionEventData(sSessionEventType);
      ContextEvent ce = new ContextEvent(ContextType.CONTEXT_SESSION_EVENT, sMessage, vfs, sPath);
      ce.setContextData(sed);
      ContextHandler.getInstance().fireContextEvent(ce);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * Checks the current user's permissions to see if they are allowed
   * to see this action.
   *
   */
  protected void checkUser() {
    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()) ) {
        this.m_bShow=true;
      }
    } catch (RemoteException e) {
      e.printStackTrace();
    } catch (ServiceException e) {
      e.printStackTrace();
    }
    this.m_bUserChecked=true;
  }
}
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.