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 ; 11 import java.awt.event.ActionListener ; 12 import java.util.logging.Logger ; 13 import java.util.logging.Level ; 14 15 19 public class SmartPopupTableTool extends JPopupMenu 20 implements ActionListener 21 { 22 23 JMenuItem delete, newValue, findDN, makeNaming, removeNaming; 25 JXplorer jx; 26 27 JTable table; 29 AttributeTableModel model; 31 String attributeName = null; 33 int currentRow; 35 AttributeValue currentValue; 37 AttributeType currentType; 39 DN currentDN = null; 42 AttributeValueCellEditor cellEditor = null; 44 private static Logger log = Logger.getLogger(SmartPopupTableTool.class.getName()); 45 46 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 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) { 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) makeNaming.setVisible(false); else if (currentType.isMandatory()) 113 makeNaming.setVisible(true); 114 else 115 makeNaming.setVisible(false); 116 } 117 removeNaming.setVisible(false); 118 } 119 120 } 121 122 128 public void actionPerformed(ActionEvent ev) 129 { 130 setVisible(false); 131 132 Object eventSource = ev.getSource(); 133 if (eventSource == newValue) 134 { 135 cellEditor.stopCellEditing(); 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 { 156 log.log(Level.WARNING, "Unknown event in popup menu:\n", ev); 157 } 158 159 repaint(); 160 } 161 162 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 filter = "(objectclass=*)"; 176 DN dn = new DN(currentValue.getStringValue()); 177 178 String 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 []{"objectClass"}, 0, jx.getSearchBroker()); 184 185 jx.getTreeTabPane().setSelectedComponent(jx.getResultsPanel()); 186 } 187 188 191 public void newValue() 192 { 193 int type = currentType.isMandatory() ? AttributeType.MANDATORY : AttributeType.NORMAL; 194 String 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 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")) CBCache.cleanCache(currentDN.toString()); 220 } 221 222 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 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 250 public void setDN(DN dn) 251 { 252 currentDN = dn; 253 } 255 256 260 public void registerCellEditor(AttributeValueCellEditor myEditor) 261 { 262 cellEditor = myEditor; 263 } 264 } | Popular Tags |