KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > directory > jxplorer > viewer > tableviewer > SmartPopupTableTool


1 package com.ca.directory.jxplorer.viewer.tableviewer;
2
3 import com.ca.commons.cbutil.*;
4 import com.ca.commons.naming.DN;
5 import com.ca.commons.naming.RDN;
6 import com.ca.directory.jxplorer.JXplorer;
7 import com.ca.directory.jxplorer.search.SearchExecute;
8
9 import javax.swing.*;
10 import java.awt.event.ActionEvent JavaDoc;
11 import java.awt.event.ActionListener JavaDoc;
12 import java.util.logging.Logger JavaDoc;
13 import java.util.logging.Level JavaDoc;
14
15 /**
16  * This is the small popup menu that appears when a manager right-clicks (or system-dependant-whatever-s) on the
17  * attribute editing table, allowing them to cut/copy/paste/delete/rename tree elements
18  */

19 public class SmartPopupTableTool extends JPopupMenu
20         implements ActionListener JavaDoc
21 {
22
23     JMenuItem delete, newValue, findDN, makeNaming, removeNaming; // displayable menu options for user input
24

25     JXplorer jx;
26
27     JTable table; // the table displaying the data - NOT CURRENTLY USED
28

29     AttributeTableModel model; // the data model - used to insert values into
30

31     String JavaDoc attributeName = null; // the currently selected attribute class name
32

33     int currentRow; // the currently selected row.
34

35     AttributeValue currentValue; // type of currently selected table row
36

37     AttributeType currentType; // value of currently selected table row
38

39     DN currentDN = null; // used by the cache system?
40
//RDN currentRDN = null; // used for naming attribute magic.
41

42     AttributeValueCellEditor cellEditor = null; //TE: to stop cell editing.
43

44     private static Logger JavaDoc log = Logger.getLogger(SmartPopupTableTool.class.getName());
45
46     /**
47      * Constructor initialises the drop down menu and menu items, and registers 'this' component as being the listener
48      * for all the menu items.
49      */

50     public SmartPopupTableTool(JTable t, AttributeTableModel m, JXplorer jxplorer)
51     {
52         jx = jxplorer;
53         table = t;
54         model = m;
55
56         add(newValue = new JMenuItem(CBIntText.get("Add Another Value")));
57         add(delete = new JMenuItem(CBIntText.get("Delete Value")));
58         add(makeNaming = new JMenuItem(CBIntText.get("Make Naming Value")));
59         add(removeNaming = new JMenuItem(CBIntText.get("Remove Naming Value")));
60         add(new JSeparator());
61         add(findDN = new JMenuItem(CBIntText.get("Find DN")));
62
63         removeNaming.setVisible(false);
64
65         findDN.addActionListener(this);
66         newValue.addActionListener(this);
67         delete.addActionListener(this);
68         makeNaming.addActionListener(this);
69         removeNaming.addActionListener(this);
70
71         setVisible(false);
72     }
73
74     /**
75      * Set the name of the attribute being operated with. That is, for new Value creation.
76      */

77     public void registerCurrentRow(AttributeType type, AttributeValue value, int row, RDN currentRDN)
78     {
79         currentType = type;
80         currentValue = value;
81         currentRow = row;
82
83         if (currentType.toString().equalsIgnoreCase("objectclass"))
84         {
85             newValue.setEnabled(false);
86             delete.setEnabled(false);
87         }
88         else
89         {
90             newValue.setEnabled(true);
91             delete.setEnabled(true);
92         }
93
94         if (value.isNaming())
95         {
96
97             if (currentRDN != null) // which it never should
98
{
99                 if (currentRDN.size() > 1)
100                     removeNaming.setVisible(true);
101                 else
102                     removeNaming.setVisible(false);
103             }
104             makeNaming.setVisible(false);
105         }
106         else
107         {
108             if (currentRDN != null)
109             {
110                 if (currentRDN.toString().indexOf(type.toString() + "=") > -1) // i.e. if we already have a naming att of this type...
111
makeNaming.setVisible(false); // don't let the user add another one.
112
else if (currentType.isMandatory())
113                     makeNaming.setVisible(true);
114                 else
115                     makeNaming.setVisible(false);
116             }
117             removeNaming.setVisible(false);
118         }
119
120     }
121
122     /**
123      * This handles the menu item actions. They rely on the attributeName String being set prior to this method being
124      * called (usually by setAttributeName() above). Most of the action handling is simply tossing arguments to
125      * JTable,
126      * @param ev the active event, i.e. the menu item selected
127      */

128     public void actionPerformed(ActionEvent JavaDoc ev)
129     {
130         setVisible(false);
131
132         Object JavaDoc eventSource = ev.getSource();
133         if (eventSource == newValue)
134         {
135             cellEditor.stopCellEditing(); //TE: bug fix 3107
136
newValue();
137         }
138         else if (eventSource == delete)
139         {
140             delete();
141         }
142         else if (eventSource == removeNaming)
143         {
144             removeRDNComponent();
145         }
146         else if (eventSource == makeNaming)
147         {
148             addRDNComponent();
149         }
150         else if (eventSource == findDN)
151         {
152             findDNComponent();
153         }
154         else // should never happen...
155
{
156             log.log(Level.WARNING, "Unknown event in popup menu:\n", ev);
157         }
158
159         repaint();
160     }
161
162     /**
163      * Performs a search on the attribute value. If the value is a DN, the search result is displayed
164      * in the Search Results tab.
165      */

166     public void findDNComponent()
167     {
168         if ("".equals(currentValue.getStringValue()))
169         {
170             jx.getSearchTree().clearTree();
171             jx.getTreeTabPane().setSelectedComponent(jx.getResultsPanel());
172             return;
173         }
174
175         String JavaDoc filter = "(objectclass=*)";
176         DN dn = new DN(currentValue.getStringValue());
177
178         String JavaDoc aliasOption = "always";
179         log.info("Setting search alias option to: [" + aliasOption + "]");
180         JXplorer.setProperty("option.ldap.searchAliasBehaviour", aliasOption);
181
182         jx.getSearchBroker().setGUIQuiet(true);
183         SearchExecute.run(jx.getSearchTree(), dn, filter, new String JavaDoc[]{"objectClass"}, 0, jx.getSearchBroker());
184
185         jx.getTreeTabPane().setSelectedComponent(jx.getResultsPanel());
186     }
187
188     /**
189      *
190      */

191     public void newValue()
192     {
193         int type = currentType.isMandatory() ? AttributeType.MANDATORY : AttributeType.NORMAL;
194         String JavaDoc attName = currentType.getValue();
195         AttributeValue newVal;
196         if (currentValue.isBinary())
197         {
198             newVal = new AttributeValue(attName, null);
199             newVal.setBinary(true);
200         }
201         else
202             newVal = new AttributeValue(attName, "");
203
204         model.addAttribute(attName, newVal, type, currentRow + 1);
205         model.fireChange();
206     }
207
208     /**
209      *
210      */

211     public void delete()
212     {
213         model.deleteAttribute(currentType.getValue(), currentRow);
214         if (currentValue.isBinary())
215             currentValue.setValue(null);
216         model.fireChange();
217
218         if ((currentType.getValue()).equalsIgnoreCase("jpegPhoto")) //TE: deletes the temporary files associated with the current entry.
219
CBCache.cleanCache(currentDN.toString());
220     }
221
222     /**
223      *
224      */

225     public void removeRDNComponent()
226     {
227         if (model.getRDNSize() == 1)
228             CBUtility.error(CBIntText.get("Cannot remove the last naming component!"));
229         else
230             model.removeNamingComponent(currentType, currentValue);
231     }
232
233     /**
234      *
235      */

236     public void addRDNComponent()
237     {
238         if (currentValue.isBinary())
239             CBUtility.error(CBIntText.get("Binary naming components are not supported."));
240         else if (currentValue.isEmpty())
241             CBUtility.error(CBIntText.get("A Naming Component must have an actual value."));
242         else
243             model.addNamingComponent(currentType, currentValue);
244     }
245
246     /**
247      *
248      * @param dn
249      */

250     public void setDN(DN dn)
251     {
252         currentDN = dn;
253         //currentRDN = dn.getLowestRDN();
254
}
255
256     /**
257      * registers the cell editor. TE: for bug fix 3107.
258      * @param myEditor the cell editor.
259      */

260     public void registerCellEditor(AttributeValueCellEditor myEditor)
261     {
262         cellEditor = myEditor;
263     }
264 }
Popular Tags