01: /*
02: * <copyright>
03: *
04: * Copyright 2000-2004 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.tools.csmart.ui.community;
28:
29: import javax.swing.*;
30: import java.awt.event.ActionEvent;
31: import java.awt.event.MouseAdapter;
32: import java.awt.event.MouseEvent;
33:
34: /**
35: * Pops up a menu with the Delete action to delete
36: * the row in which the mouse is located.
37: */
38:
39: public class CommunityTableMouseAdapter extends MouseAdapter {
40: JTable table;
41: JPopupMenu menu;
42: int row;
43:
44: private AbstractAction deleteAction = new AbstractAction("Delete") {
45: public void actionPerformed(ActionEvent e) {
46: delete();
47: }
48: };
49:
50: private Action[] actions = { deleteAction };
51:
52: public CommunityTableMouseAdapter(JTable table) {
53: super ();
54: this .table = table;
55: menu = new JPopupMenu();
56: for (int i = 0; i < actions.length; i++)
57: menu.add(actions[i]);
58: }
59:
60: public void mouseClicked(MouseEvent e) {
61: if (e.isPopupTrigger())
62: doPopup(e);
63: }
64:
65: public void mousePressed(MouseEvent e) {
66: if (e.isPopupTrigger())
67: doPopup(e);
68: }
69:
70: public void mouseReleased(MouseEvent e) {
71: if (e.isPopupTrigger())
72: doPopup(e);
73: }
74:
75: /**
76: * The menu only contains the "Delete" action.
77: * Deletions can only be done from tables that
78: * display the attribute ids and values for
79: * a community or entity, in other words, not for tables
80: * that are a join of information; hence if the table
81: * doesn't have exactly 3 columns, don't display a popup menu.
82: * TODO: should be a better way to detect the join?
83: */
84:
85: private void doPopup(MouseEvent e) {
86: row = table.rowAtPoint(e.getPoint());
87: if (row == -1)
88: return;
89: if (table.getModel().getColumnCount() == 3)
90: menu.show(table, e.getX(), e.getY());
91: }
92:
93: private void delete() {
94: ((CommunityTableUtils) table.getModel()).deleteRow(row);
95: }
96: }
|