001/* 002 * Copyright (c) 2005 Einar Pehrson <einar@pehrson.nu>. 003 * 004 * This file is part of 005 * CleanSheets - a spreadsheet application for the Java platform. 006 * 007 * CleanSheets is free software; you can redistribute it and/or modify 008 * it under the terms of the GNU General Public License as published by 009 * the Free Software Foundation; either version 2 of the License, or 010 * (at your option) any later version. 011 * 012 * CleanSheets is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * along with CleanSheets; if not, write to the Free Software 019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 020 */ 021package csheets.ui.ctrl; 022 023import java.util.ArrayList; 024import java.util.HashMap; 025import java.util.List; 026import java.util.Map; 027 028import csheets.CleanSheets; 029import csheets.SpreadsheetAppEvent; 030import csheets.SpreadsheetAppListener; 031import csheets.ui.FileChooser; 032 033/** 034 * A manager for UI actions. 035 * @author Einar Pehrson 036 */ 037public class ActionManager { 038 039 /** The map of file actions */ 040 private Map<String, BaseAction> actions = new HashMap<String, BaseAction>(); 041 042 /** The action that should only be enabled when the active workbook is modified */ 043 private List<BaseAction> modificationActions = new ArrayList<BaseAction>(); 044 045 /** The action that should only be enabled when the active workbook is stored in a file */ 046 private List<BaseAction> fileActions = new ArrayList<BaseAction>(); 047 048 /** 049 * Creates a new action manager. 050 * @param app the CleanSheets application 051 * @param uiController the user interface controller 052 * @param chooser a file chooser 053 */ 054 public ActionManager(CleanSheets app, UIController uiController, FileChooser chooser) { 055 ActionEnabler enabler = new ActionEnabler(); 056 app.addSpreadsheetAppListener(enabler); 057 uiController.addEditListener(enabler); 058 } 059 060 /** 061 * Returns the action with the given identifier 062 * @param identifier the unique identifier of the action 063 * @return the action of the given type 064 */ 065 public BaseAction getAction(String identifier) { 066 return actions.get(identifier); 067 } 068 069 /** 070 * Registers the given action with the manager 071 * @param identifier the unique identifier of the action 072 * @param action the action to register 073 */ 074 public void registerAction(String identifier, BaseAction action) { 075 actions.put(identifier, action); 076 if (action.requiresModification()) 077 modificationActions.add(action); 078 if (action.requiresFile()) 079 fileActions.add(action); 080 } 081 082 /** 083 * A workbook listener that sets the enabled state of actoins depending 084 * on whether the current workbook has been modified and/or is stored in 085 * a file. 086 */ 087 public class ActionEnabler implements SpreadsheetAppListener, EditListener { 088 089 /** 090 * Creates a new action enabler. 091 */ 092 public ActionEnabler() {} 093 094 public void workbookCreated(SpreadsheetAppEvent event) { 095 for (BaseAction action : modificationActions) 096 action.setEnabled(false); 097 for (BaseAction action : fileActions) 098 action.setEnabled(false); 099 } 100 101 public void workbookLoaded(SpreadsheetAppEvent event) { 102 for (BaseAction action : modificationActions) 103 action.setEnabled(false); 104 for (BaseAction action : fileActions) 105 action.setEnabled(true); 106 } 107 108 public void workbookUnloaded(SpreadsheetAppEvent event) {} 109 110 public void workbookSaved(SpreadsheetAppEvent event) { 111 for (BaseAction action : modificationActions) 112 action.setEnabled(false); 113 for (BaseAction action : fileActions) 114 action.setEnabled(true); 115 } 116 117 public void workbookModified(EditEvent event) { 118 for (BaseAction action : modificationActions) 119 action.setEnabled(true); 120 } 121 } 122}