1 19 20 package org.netbeans.modules.xml.xam.ui.column; 21 22 import java.awt.Point ; 23 import java.awt.Rectangle ; 24 import java.awt.event.FocusEvent ; 25 import java.awt.event.FocusListener ; 26 import java.awt.event.MouseEvent ; 27 import java.awt.event.MouseListener ; 28 import java.beans.PropertyVetoException ; 29 import javax.accessibility.AccessibleContext ; 30 import javax.swing.Action ; 31 import javax.swing.JList ; 32 import javax.swing.JPopupMenu ; 33 import javax.swing.KeyStroke ; 34 import javax.swing.ListSelectionModel ; 35 import javax.swing.SwingUtilities ; 36 import org.openide.ErrorManager; 37 import org.openide.awt.MouseUtils; 38 import org.openide.explorer.ExplorerManager; 39 import org.openide.explorer.view.ListView; 40 import org.openide.nodes.Node; 41 import org.openide.nodes.NodeOp; 42 import org.openide.util.Lookup; 43 import org.openide.util.Utilities; 44 import org.openide.util.actions.ActionPerformer; 45 import org.openide.util.actions.CallbackSystemAction; 46 import org.openide.util.actions.SystemAction; 47 48 59 public class ColumnListView extends ListView { 60 61 private static final long serialVersionUID = 1L; 62 63 private transient PopupSupport popupSupport; 64 65 private ExplorerManager explorerManager; 66 67 70 public ColumnListView() { 71 super(); 72 } 74 75 80 public ColumnListView(ExplorerManager em) { 81 super(); 82 explorerManager = em; 83 } 84 85 public void addNotify() { 86 super.addNotify(); 87 MouseListener [] l = list.getMouseListeners(); 88 for (MouseListener ml : l) { 89 if (ml instanceof ActionPerformer && 90 ml instanceof FocusListener ) { 91 list.removeMouseListener(ml); 94 list.removeFocusListener((FocusListener ) ml); 95 break; 96 } 97 } 98 popupSupport = new PopupSupport(); 99 list.addFocusListener(popupSupport); 100 list.addMouseListener(popupSupport); 101 } 102 103 protected JList createList() { 104 return new ColumnList(); 105 } 106 107 public void removeNotify() { 108 super.removeNotify(); 109 list.removeFocusListener(popupSupport); 110 list.removeMouseListener(popupSupport); 111 } 112 113 void createPopup(int xpos, int ypos, boolean context) { 114 if (explorerManager == null) { 115 return; 116 } 117 if (!isPopupAllowed()) { 118 return; 119 } 120 121 if (context) { 122 Node[] nodes = new Node[] { explorerManager.getExploredContext() }; 125 try { 126 explorerManager.setSelectedNodes(nodes); 127 } catch (PropertyVetoException pve) { 128 assert false : pve; } 130 } 131 Action [] actions = NodeOp.findActions(explorerManager.getSelectedNodes()); 132 JPopupMenu popup = Utilities.actionsToPopup(actions, this); 133 if (popup != null && popup.getSubElements().length > 0) { 134 popup.show(list, xpos, ypos); 135 } 136 } 137 138 final class PopupSupport extends MouseUtils.PopupMouseAdapter 139 implements ActionPerformer, Runnable , FocusListener { 140 private CallbackSystemAction csa; 141 142 protected void showPopup(MouseEvent e) { 143 Point p = new Point (e.getX(), e.getY()); 144 int i = list.locationToIndex(p); 145 Rectangle r = list.getCellBounds(i, i); 146 boolean contextMenu = (r == null) || !r.contains(p); 147 if (!contextMenu && !list.isSelectedIndex(i)) { 148 list.setSelectedIndex(i); 153 } 154 createPopup(e.getX(), e.getY(), contextMenu); 155 } 156 157 public void performAction(SystemAction act) { 158 SwingUtilities.invokeLater(this); 159 } 160 161 public void run() { 162 boolean multisel = (list.getSelectionMode() != ListSelectionModel.SINGLE_SELECTION); 163 int i = (multisel ? list.getLeadSelectionIndex() : list.getSelectedIndex()); 164 if (i < 0) { 165 return; 166 } 167 Point p = list.indexToLocation(i); 168 if (p == null) { 169 return; 170 } 171 createPopup(p.x, p.y, false); 172 } 173 174 @SuppressWarnings ("deprecation") 175 public void focusGained(FocusEvent ev) { 176 if (csa == null) { 177 try { 178 ClassLoader l = (ClassLoader )Lookup.getDefault().lookup(ClassLoader .class); 179 if (l == null) { 180 l = getClass().getClassLoader(); 181 } 182 Class popup = Class.forName("org.openide.actions.PopupAction", true, l); csa = (CallbackSystemAction) CallbackSystemAction.get(popup); 184 } catch (ClassNotFoundException e) { 185 Error err = new NoClassDefFoundError (); 186 ErrorManager.getDefault().annotate(err, e); 187 throw err; 188 } 189 } 190 csa.setActionPerformer(this); 191 } 192 193 @SuppressWarnings ("deprecation") 194 public void focusLost(FocusEvent ev) { 195 if (csa != null && csa.getActionPerformer() instanceof PopupSupport) { 196 csa.setActionPerformer(null); 197 } 198 } 199 } 200 201 209 private class ColumnList extends JList { 210 211 private static final long serialVersionUID = 1L; 212 213 ColumnList() { 214 super(); 215 216 getInputMap().put(KeyStroke.getKeyStroke("control C"), "none"); getInputMap().put(KeyStroke.getKeyStroke("control V"), "none"); getInputMap().put(KeyStroke.getKeyStroke("control X"), "none"); } 227 228 public boolean getScrollableTracksViewportWidth() { 229 return true; 231 } 232 233 public AccessibleContext getAccessibleContext() { 235 if (accessibleContext == null) { 236 accessibleContext = new AccessibleColumnList(); 237 } 238 239 return accessibleContext; 240 } 241 242 private class AccessibleColumnList extends AccessibleJList { 243 244 private static final long serialVersionUID = 1L; 245 246 AccessibleColumnList() { 247 } 248 249 public String getAccessibleName() { 250 return ColumnListView.this.getAccessibleContext().getAccessibleName(); 251 } 252 253 public String getAccessibleDescription() { 254 return ColumnListView.this.getAccessibleContext().getAccessibleDescription(); 255 } 256 } 257 } 258 } 259 | Popular Tags |