View Javadoc
1 /* 2 * 3 * $Revision: 1.11 $ 4 */ 5 6 package com.sun.xml.registry.client.browser; 7 8 import java.awt.*; 9 import java.awt.event.*; 10 11 import javax.swing.*; 12 import javax.swing.event.*; 13 14 import javax.xml.registry.*; 15 16 /*** 17 * A specialization of JList for JAXR Browser. List elements are RIM objects. 18 * 19 * @author <a href="mailto:Farrukh.Najmi@Sun.COM">Farrukh S. Najmi</a> 20 * @version 21 */ 22 public class JBList extends JList { 23 24 25 private JPopupMenu popup = null; 26 private JMenuItem editMenuItem = null; 27 private JMenuItem insertMenuItem = null; 28 private JMenuItem removeMenuItem = null; 29 private MouseListener popupListener; 30 31 private String interfaceName = null; 32 33 protected boolean editable = RegistryBrowser.getInstance().isAuthenticated(); 34 35 //Not usable 36 private JBList() { 37 } 38 39 public JBList(String interfaceName) { 40 super(); 41 42 JBList_initialize(interfaceName); 43 } 44 45 /*** Creates new JBList */ 46 public JBList(String interfaceName, JBListModel model) { 47 super(model); 48 49 JBList_initialize(interfaceName); 50 } 51 52 private void JBList_initialize(String interfaceName) { 53 this.interfaceName = interfaceName; 54 55 this.setToolTipText("Use right mouse button to perform actions (e.g. edit, insert, remove)"); 56 createPopup(); 57 } 58 59 /*** Create popup menu for List */ 60 private void createPopup() { 61 62 popup = new JPopupMenu(); 63 64 if (editable) { 65 editMenuItem = new JMenuItem("Edit"); 66 } 67 else { 68 editMenuItem = new JMenuItem("Show Details"); 69 } 70 popup.add(editMenuItem); 71 72 insertMenuItem = new JMenuItem("Insert"); 73 removeMenuItem = new JMenuItem("Remove"); 74 75 if (editable) { 76 popup.add(insertMenuItem); 77 popup.add(removeMenuItem); 78 } 79 80 editMenuItem.addActionListener(new ActionListener() { 81 public void actionPerformed(ActionEvent ae) { 82 editAction(); 83 } 84 }); 85 86 insertMenuItem.addActionListener(new ActionListener() { 87 public void actionPerformed(ActionEvent ae) { 88 insertAction(); 89 } 90 }); 91 92 removeMenuItem.addActionListener(new ActionListener() { 93 public void actionPerformed(ActionEvent ae) { 94 removeAction(); 95 } 96 }); 97 98 // Add listener to self so that I can bring up popup menus on right mouse click 99 popupListener = new PopupListener(); 100 addMouseListener(popupListener); 101 } 102 103 class PopupListener extends MouseAdapter { 104 105 public void mousePressed(MouseEvent e) { 106 107 if (e.getClickCount() > 1) { 108 editAction(); 109 } else { 110 maybeShowPopup(e); 111 } 112 } 113 114 public void mouseReleased(MouseEvent e) { 115 maybeShowPopup(e); 116 } 117 118 private void maybeShowPopup(MouseEvent e) { 119 if (e.isPopupTrigger()) { 120 int[] selectedIndices = getSelectedIndices(); 121 if (selectedIndices.length == 0) { 122 editMenuItem.setEnabled(false); 123 insertMenuItem.setEnabled(true); 124 removeMenuItem.setEnabled(false); 125 } 126 else if (selectedIndices.length == 1) { 127 editMenuItem.setEnabled(true); 128 insertMenuItem.setEnabled(true); 129 removeMenuItem.setEnabled(true); 130 } 131 else if (selectedIndices.length > 1) { 132 editMenuItem.setEnabled(false); 133 insertMenuItem.setEnabled(true); 134 removeMenuItem.setEnabled(true); 135 } 136 popup.show(e.getComponent(), e.getX(), e.getY()); 137 } 138 } 139 140 } 141 142 protected void editAction() { 143 int[] selectedIndices = getSelectedIndices(); 144 145 if (selectedIndices.length == 1) { 146 Object selectedObject = getSelectedValue(); 147 148 if (selectedObject != null) { 149 JBEditorDialog.showObjectDetails(this, selectedObject, true, editable); 150 } 151 } 152 else { 153 //Only show error if there is at least one item in list 154 //Reason is that a user may inadvertantly double click 155 //on an empty list and then get confused by the message. 156 if (getModel().getSize() > 0) { 157 RegistryBrowser.displayError("Exactly one object must be selected in list for edit action."); 158 } 159 else { 160 RegistryBrowser.displayInfo("Select action on list by clicking the right mouse button once."); 161 } 162 } 163 } 164 165 166 protected void insertAction() { 167 int index = getMaxSelectionIndex(); 168 169 170 171 JBListModel model = (JBListModel)getModel(); 172 if (index < 0) { 173 index = model.getSize(); 174 } 175 176 try { 177 JAXRClient client = RegistryBrowser.getInstance().getClient(); 178 BusinessLifeCycleManager lcm = client.getBusinessLifeCycleManager(); 179 Object elementModel = null; 180 181 try { 182 Class cls = Class.forName(interfaceName); 183 184 //Now create an instance of the cls 185 elementModel = cls.newInstance(); 186 } 187 catch (ClassNotFoundException e) { 188 elementModel = lcm.createObject(interfaceName); 189 } 190 catch (InstantiationException e) { 191 throw new JAXRException(e); 192 } 193 catch (IllegalAccessException e) { 194 throw new JAXRException(e); 195 } 196 197 JBEditorDialog dialog = JBEditorDialog.showObjectDetails(this, elementModel, true, editable); 198 if (dialog.getStatus() == JBEditorDialog.OK_STATUS) { 199 model.add(index, elementModel); 200 } 201 } 202 catch (JAXRException e) { 203 RegistryBrowser.displayError(e); 204 } 205 } 206 207 protected void removeAction() { 208 int[] selectedIndices = getSelectedIndices(); 209 210 if (selectedIndices.length >= 1) { 211 Object selectedObject = getSelectedValue(); 212 213 if (selectedObject != null) { 214 JBListModel model = (JBListModel)getModel(); 215 216 model.removeElementAt(selectedIndices[0]); 217 } 218 } 219 else { 220 RegistryBrowser.displayError("One or more objects must be selected in list for remove action."); 221 } 222 } 223 224 /*** 225 * Sets whether this dialog is read-only or editable. 226 */ 227 public void setEditable(boolean editable) { 228 this.editable = editable; 229 createPopup(); 230 } 231 232 /*** 233 * Tells whether this dialog is read-only or editable. 234 */ 235 public boolean isEditable() { 236 return editable; 237 } 238 239 }

This page was automatically generated by Maven