/*
* GNU General Public License
*
* Copyright (C) 2006 Matej Zachar <mzachar@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id$
*/
package sk.netmap.gui.controller;
import org.apache.log4j.Logger;
import java.awt.CheckboxMenuItem;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyChangeEvent;
import java.util.LinkedList;
import java.util.List;
import sk.netmap.bo.Group;
import sk.netmap.gui.MainFrame;
import sk.netmap.gui.componets.IPropertyChangeListener;
/**
*
* @author Matej Zachar $Date$
*/
public class PopupMenuController implements ActionListener, ItemListener, IPropertyChangeListener {
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(PopupMenuController.class);
private static PopupMenuController INSTANCE;
private PopupMenu menu = new PopupMenu();
private List<CheckboxMenuItem> menuItems = new LinkedList<CheckboxMenuItem>();
private PopupMenuController(){
DataController.getInstance().addPropertyChangeListener(this);
buildPopupMenu();
}
public static PopupMenuController getInstance(){
if (INSTANCE == null){
INSTANCE = new PopupMenuController();
}
return INSTANCE;
}
public PopupMenu getPopupMenu() {
return menu;
}
public void rebuildPopupMenu(){
menu.removeAll();
menuItems.clear();
buildPopupMenu();
}
public void buildPopupMenu() {
for (Group group : DataController.getInstance().getAllGroups()) {
addGroupMenuItem(group);
}
addBasicMenuItems();
}
public void addGroupMenuItem(Group g){
CheckboxMenuItem item = new CheckboxMenuItem(g.getName());
item.setActionCommand(""+g.getId());
item.addItemListener(this);
menu.add(item);
menuItems.add(item);
}
private void addBasicMenuItems(){
MenuItem defaultItem;
menu.addSeparator();
defaultItem = new MenuItem(resString("ForceUnMapAllNetworkDrives"));
defaultItem.addActionListener(this);
menu.add(defaultItem);
menu.addSeparator();
defaultItem = new MenuItem(resString("End"));
defaultItem.addActionListener(MainFrame.getInstance());
menu.add(defaultItem);
}
private String resString(String key) {
return MainFrame.resString(key);
}
private void checkBoxGroupSetStateOn(CheckboxMenuItem currentItem){
currentItem.setState(true);
for (CheckboxMenuItem item : menuItems){
if (item.getState() && !item.equals(currentItem)){
logger.debug("Item ["+item.getName()+" selected] will by deselected");
item.setState(false);
}
}
}
private void checkBoxGroupSetStateOn(Group group){
for (CheckboxMenuItem item : menuItems){
if (item.getActionCommand().equals(""+group.getId())){
logger.debug("Item ["+item.getName()+"] will by selected");
checkBoxGroupSetStateOn(item);
break;
}
}
}
private void checkBoxGroupSetStateToAllOff(){
for (CheckboxMenuItem item : menuItems){
item.setState(false);
}
}
public void itemStateChanged(ItemEvent e) {
long groupId = Long.parseLong(((MenuItem)e.getSource()).getActionCommand());
if (e.getStateChange() == ItemEvent.SELECTED){
DataController.getInstance().mapGroup(groupId);
} else {
DataController.getInstance().unMapLastMappedGroup();
}
}
/**
* Unmaping All Groups - unselect all Checkbox
*/
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals(resString("ForceUnMapAllNetworkDrives"))) DataController.getInstance().forceUnMapAllSystemNetworkDrives();
}
public void propertyChange(PropertyChangeEvent event) {
String property = event.getPropertyName();
logger.debug("PopupController: "+event.getPropertyName());
if (property.equals(DataController.PROPERTY_GROUP_CHANGE)) rebuildPopupMenu();
if (property.equals(DataController.PROPERTY_MAP_GROUP)) checkBoxGroupSetStateOn((Group)event.getNewValue());
if (property.equals(DataController.PROPERTY_UNMAP_GROUP)) checkBoxGroupSetStateToAllOff();
}
}