001: /*
002: * <copyright>
003: *
004: * Copyright 2000-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.tools.csmart.ui.viewer;
028:
029: import org.cougaar.tools.csmart.experiment.Experiment;
030: import org.cougaar.tools.csmart.recipe.RecipeComponent;
031: import org.cougaar.tools.csmart.society.SocietyComponent;
032:
033: import javax.swing.*;
034: import javax.swing.tree.DefaultMutableTreeNode;
035: import javax.swing.tree.TreePath;
036: import java.awt.*;
037: import java.awt.event.MouseAdapter;
038: import java.awt.event.MouseEvent;
039:
040: public class OrganizerMouseListener extends MouseAdapter {
041: private Organizer organizer;
042: private OrganizerTree workspace;
043: private JPopupMenu societyMenu = new JPopupMenu();
044: private JPopupMenu recipeMenu = new JPopupMenu();
045: private JPopupMenu experimentMenu = new JPopupMenu();
046: private JPopupMenu folderMenu = new JPopupMenu();
047: private JPopupMenu rootMenu = new JPopupMenu();
048:
049: private JMenu newExperimentMenu = new JMenu(
050: ActionUtil.NEW_EXPERIMENT_ACTION);
051: private JMenu newExperimentInFolderMenu = new JMenu(
052: ActionUtil.NEW_EXPERIMENT_ACTION);
053: private JMenu newRecipeMenu = new JMenu(
054: ActionUtil.NEW_RECIPE_ACTION);
055: private JMenu newRecipeInFolderMenu = new JMenu(
056: ActionUtil.NEW_RECIPE_ACTION);
057:
058: /**
059: * Construct a mouse listener for the workspace tree.
060: * @param organizer the organizer for which this is the mouse listener
061: * @param workspace the workspace tree for which this is the mouse listener
062: */
063: public OrganizerMouseListener(Organizer organizer,
064: OrganizerTree workspace) {
065: this .organizer = organizer;
066: this .workspace = workspace;
067:
068: // define pop-up menus
069: Object[] rootMenuItems = { newExperimentMenu, newRecipeMenu,
070: organizer.newFolderAction,
071: organizer.renameWorkspaceAction,
072: organizer.deleteExperimentFromDatabaseAction,
073: organizer.deleteRecipeFromDatabaseAction };
074:
075: Object[] experimentMenuItems = {
076: organizer.buildExperimentAction,
077: organizer.runExperimentAction,
078: organizer.duplicateAction,
079: organizer.deleteExperimentAction,
080: organizer.renameExperimentAction,
081: organizer.configureAction, organizer.saveAction };
082:
083: Object[] societyMenuItems = { organizer.configureAction,
084: organizer.buildExperimentAction,
085: organizer.duplicateAction,
086: organizer.deleteSocietyAction,
087: organizer.renameSocietyAction, organizer.saveAction };
088:
089: Object[] recipeMenuItems = { organizer.configureAction,
090: organizer.duplicateAction,
091: organizer.deleteRecipeAction,
092: organizer.renameRecipeAction, organizer.saveAction };
093:
094: Object[] folderMenuItems = { newExperimentInFolderMenu,
095: newRecipeInFolderMenu, organizer.newFolderAction,
096: organizer.deleteFolderAction,
097: organizer.renameFolderAction };
098:
099: // set up experiment submenus
100: for (int i = 0; i < organizer.newExperimentActions.length; i++) {
101: newExperimentMenu.add(organizer.newExperimentActions[i]);
102: newExperimentInFolderMenu
103: .add(organizer.newExperimentActions[i]);
104: }
105: // set up recipe submenus
106: for (int i = 0; i < organizer.newRecipeActions.length; i++) {
107: newRecipeMenu.add(organizer.newRecipeActions[i]);
108: newRecipeInFolderMenu.add(organizer.newRecipeActions[i]);
109: }
110: for (int i = 0; i < rootMenuItems.length; i++) {
111: if (rootMenuItems[i] instanceof Action)
112: rootMenu.add((Action) rootMenuItems[i]);
113: else
114: rootMenu.add((JMenuItem) rootMenuItems[i]);
115: }
116: for (int i = 0; i < experimentMenuItems.length; i++)
117: experimentMenu.add((Action) experimentMenuItems[i]);
118: for (int i = 0; i < societyMenuItems.length; i++)
119: societyMenu.add((Action) societyMenuItems[i]);
120: for (int i = 0; i < recipeMenuItems.length; i++)
121: recipeMenu.add((Action) recipeMenuItems[i]);
122: for (int i = 0; i < folderMenuItems.length; i++) {
123: if (folderMenuItems[i] instanceof Action)
124: folderMenu.add((Action) folderMenuItems[i]);
125: else
126: folderMenu.add((JMenuItem) folderMenuItems[i]);
127: }
128: }
129:
130: /**
131: * If the event is a popup trigger, then display the appropriate
132: * popup menu which depends on the type of object selected in the workspace
133: * (i.e. an experiment, society, recipe, etc.)
134: * @param e the mouse event
135: */
136: public void mouseClicked(MouseEvent e) {
137: if (e.isPopupTrigger())
138: doPopup(e);
139: }
140:
141: /**
142: * If the event is a popup trigger, then display the appropriate
143: * popup menu which depends on the type of object selected in the workspace
144: * (i.e. an experiment, society, recipe, etc.)
145: * @param e the mouse event
146: */
147: public void mousePressed(MouseEvent e) {
148: if (e.isPopupTrigger())
149: doPopup(e);
150: }
151:
152: /**
153: * If the event is a popup trigger, then display the appropriate
154: * popup menu which depends on the type of object selected in the workspace
155: * (i.e. an experiment, society, recipe, etc.)
156: * @param e the mouse event
157: */
158: public void mouseReleased(MouseEvent e) {
159: if (e.isPopupTrigger())
160: doPopup(e);
161: }
162:
163: /**
164: * Popup appropriate menu, but first disable appropriate commands
165: * if component is not editable.
166: */
167: private void doPopup(MouseEvent e) {
168: TreePath selPath = workspace.getPathForLocation(e.getX(), e
169: .getY());
170: if (selPath == null)
171: return;
172: // set the selected node to be the node the mouse is pointing at
173: workspace.setSelectionPath(selPath);
174: DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) selPath
175: .getLastPathComponent();
176: Object o = selectedNode.getUserObject();
177: if (organizer.isNodeInUse(selectedNode))
178: return; // can't do anything with these
179: if (selectedNode.isRoot()) {
180: configureMenu(rootMenu);
181: rootMenu.show(workspace, e.getX(), e.getY());
182: } else if (o instanceof SocietyComponent) {
183: configureMenu(societyMenu);
184: societyMenu.show(workspace, e.getX(), e.getY());
185: } else if (o instanceof RecipeComponent) {
186: configureMenu(recipeMenu);
187: recipeMenu.show(workspace, e.getX(), e.getY());
188: } else if (o instanceof Experiment) {
189: configureMenu(experimentMenu);
190: experimentMenu.show(workspace, e.getX(), e.getY());
191: } else if (o instanceof String) {
192: configureMenu(folderMenu);
193: folderMenu.show(workspace, e.getX(), e.getY());
194: }
195: }
196:
197: private void configureMenu(JPopupMenu menu) {
198: int n = menu.getComponentCount();
199: for (int i = 0; i < n; i++) {
200: Component c = menu.getComponent(i);
201: if (c != null) {
202: if (c instanceof JMenu)
203: enableActions((JMenu) c);
204: else if (c instanceof JMenuItem) {
205: Action action = ((JMenuItem) c).getAction();
206: if (action != null)
207: ActionUtil.setActionAllowed(action, organizer);
208: }
209: }
210: }
211: }
212:
213: private boolean enableActions(JMenu menu) {
214: boolean haveEnabledActions = false;
215: int n = menu.getItemCount();
216: for (int i = 0; i < n; i++) {
217: JMenuItem menuItem = menu.getItem(i);
218: if (menuItem == null)
219: continue;
220: if (menuItem instanceof JMenu) {
221: if (enableActions((JMenu) menuItem)) {
222: menuItem.setEnabled(true);
223: haveEnabledActions = true;
224: } else
225: menuItem.setEnabled(false);
226: } else {
227: Action action = menuItem.getAction();
228: if (action != null) {
229: ActionUtil.setActionAllowed(action, organizer);
230: if (action.isEnabled())
231: haveEnabledActions = true;
232: }
233: }
234: }
235: return haveEnabledActions;
236: }
237:
238: private boolean enableActions(JMenuItem c) {
239: boolean haveEnabledActions = false;
240: if (c instanceof JMenu) {
241: if (enableActions(c)) {
242: c.setEnabled(true);
243: haveEnabledActions = true;
244: } else
245: c.setEnabled(false);
246: } else {
247: Action action = ((JMenuItem) c).getAction();
248: if (action != null) {
249: ActionUtil.setActionAllowed(action, organizer);
250: if (action.isEnabled())
251: haveEnabledActions = true;
252: }
253: }
254: return haveEnabledActions;
255: }
256: }
|