1 19 package org.netbeans.modules.refactoring.spi.impl; 20 21 import java.awt.BorderLayout ; 22 import java.awt.Image ; 23 import java.awt.event.MouseEvent ; 24 import java.beans.PropertyChangeListener ; 25 import javax.swing.ActionMap ; 26 import javax.swing.JLabel ; 27 import javax.swing.JPanel ; 28 import org.openide.awt.MouseUtils; 29 import org.openide.util.HelpCtx; 30 import org.openide.util.Utilities; 31 import org.openide.windows.TopComponent; 32 import org.openide.windows.WindowManager; 33 import java.awt.Component ; 34 import java.awt.event.ActionEvent ; 35 import javax.swing.AbstractAction ; 36 import javax.swing.JTabbedPane ; 37 import org.openide.awt.JPopupMenuPlus; 38 import org.openide.util.NbBundle; 39 40 44 public class RefactoringPanelContainer extends TopComponent { 45 46 private static RefactoringPanelContainer usages = null; 47 private static RefactoringPanelContainer refactorings = null; 48 private transient boolean isVisible = false; 49 private JPopupMenuPlus pop; 50 51 private PopupListener listener; 52 private CloseListener closeL; 53 private boolean isRefactoring; 54 private static Image REFACTORING_BADGE = Utilities.loadImage( "org/netbeans/modules/refactoring/resources/refactoringpreview.png" ); private static Image USAGES_BADGE = Utilities.loadImage( "org/netbeans/modules/refactoring/resources/findusages.png" ); 57 private RefactoringPanelContainer() { 58 this("", false); 59 } 60 61 private RefactoringPanelContainer(String name, boolean isRefactoring) { 62 setName(name); 63 setToolTipText(name); 64 setFocusable(true); 65 setLayout(new java.awt.BorderLayout ()); 66 getAccessibleContext().setAccessibleDescription( 67 NbBundle.getMessage(RefactoringPanelContainer.class, "ACSD_usagesPanel") 68 ); 69 pop = new JPopupMenuPlus(); 70 pop.add(new Close()); 71 pop.add(new CloseAll()); 72 pop.add(new CloseAllButCurrent()); 73 listener = new PopupListener(); 74 closeL = new CloseListener(); 75 this.isRefactoring = isRefactoring; 76 setFocusCycleRoot(true); 77 JLabel label = new JLabel (NbBundle.getMessage(RefactoringPanelContainer.class, "LBL_NoUsages")); 78 label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); 79 this.add(label, BorderLayout.CENTER); 80 initActions(); 81 } 82 83 void addPanel(JPanel panel) { 84 RefactoringPanel.checkEventThread(); 85 if (getComponentCount() == 0) { 86 add(panel, BorderLayout.CENTER); 87 } else { 88 Component comp = getComponent(0); 89 if (comp instanceof JTabbedPane ) { 90 ((JTabbedPane ) comp).addTab(panel.getName() + " ", null, panel, panel.getToolTipText()); ((JTabbedPane ) comp).setSelectedComponent(panel); 92 comp.validate(); 93 } else if (comp instanceof JLabel ) { 94 remove(comp); 95 add(panel, BorderLayout.CENTER); 96 } else { 97 remove(comp); 98 JTabbedPane pane = new CloseButtonTabbedPane(); 99 pane.addMouseListener(listener); 100 pane.addPropertyChangeListener(closeL); 101 add(pane, BorderLayout.CENTER); 102 pane.addTab(comp.getName() + " ", null, comp, ((JPanel ) comp).getToolTipText()); pane.addTab(panel.getName() + " ", null, panel, panel.getToolTipText()); pane.setSelectedComponent(panel); 105 pane.validate(); 106 } 107 } 108 if (!isVisible) { 109 isVisible = true; 110 open(); 111 } 112 validate(); 113 requestActive(); 114 } 115 116 protected void componentActivated () { 117 super.componentActivated(); 118 JPanel panel = getCurrentPanel(); 119 if (panel!=null) 120 panel.requestFocus(); 121 } 122 123 void removePanel(JPanel panel) { 124 RefactoringPanel.checkEventThread(); 125 Component comp = getComponentCount() > 0 ? getComponent(0) : null; 126 if (comp instanceof JTabbedPane ) { 127 JTabbedPane tabs = (JTabbedPane ) comp; 128 if (panel == null) { 129 panel = (JPanel ) tabs.getSelectedComponent(); 130 } 131 tabs.remove(panel); 132 if (tabs.getComponentCount() == 1) { 133 Component c = tabs.getComponent(0); 134 tabs.removeMouseListener(listener); 135 tabs.removePropertyChangeListener(closeL); 136 remove(tabs); 137 add(c, BorderLayout.CENTER); 138 } 139 } else { 140 if (comp != null) 141 remove(comp); 142 isVisible = false; 143 close(); 144 } 145 validate(); 146 } 147 148 void closeAllButCurrent() { 149 Component comp = getComponent(0); 150 if (comp instanceof JTabbedPane ) { 151 JTabbedPane tabs = (JTabbedPane ) comp; 152 Component current = tabs.getSelectedComponent(); 153 Component[] c = tabs.getComponents(); 154 for (int i = 0; i< c.length; i++) { 155 if (c[i]!=current) { 156 ((RefactoringPanel) c[i]).close(); 157 } 158 } 159 } 160 } 161 162 public static synchronized RefactoringPanelContainer getUsagesComponent() { 163 if ( usages == null ) { 164 usages = (RefactoringPanelContainer) WindowManager.getDefault().findTopComponent( "find-usages" ); } 166 return usages; 167 } 168 169 public static synchronized RefactoringPanelContainer getRefactoringComponent() { 170 if (refactorings == null) { 171 refactorings = (RefactoringPanelContainer) WindowManager.getDefault().findTopComponent( "refactoring-preview" ); } 173 return refactorings; 174 } 175 176 public static synchronized RefactoringPanelContainer createRefactoringComponent() { 177 if (refactorings == null) 178 refactorings = new RefactoringPanelContainer(org.openide.util.NbBundle.getMessage(RefactoringPanelContainer.class, "LBL_Refactoring"), true); 179 return refactorings; 180 } 181 182 public static synchronized RefactoringPanelContainer createUsagesComponent() { 183 if (usages == null) 184 usages = new RefactoringPanelContainer(org.openide.util.NbBundle.getMessage(RefactoringPanelContainer.class, "LBL_Usages"), false); 185 return usages; 186 } 187 188 protected void closeNotify() { 189 isVisible = false; 190 if (getComponentCount() == 0) { 191 return ; 192 } 193 Component comp = getComponent(0); 194 if (comp instanceof JTabbedPane ) { 195 JTabbedPane pane = (JTabbedPane ) comp; 196 Component[] c = pane.getComponents(); 197 for (int i = 0; i< c.length; i++) { 198 ((RefactoringPanel) c[i]).close(); 199 } 200 } else if (comp instanceof RefactoringPanel) { 201 ((RefactoringPanel) comp).close(); 202 } 203 } 204 205 protected String preferredID() { 206 return "RefactoringPanel"; } 208 209 public int getPersistenceType() { 210 return PERSISTENCE_ALWAYS; 211 } 212 213 private void initActions() { 214 ActionMap map = getActionMap(); 215 216 map.put("jumpNext", new PrevNextAction (false)); map.put("jumpPrev", new PrevNextAction (true)); } 219 220 public RefactoringPanel getCurrentPanel() { 221 if (getComponentCount() > 0) { 222 Component comp = getComponent(0); 223 if (comp instanceof JTabbedPane ) { 224 JTabbedPane tabs = (JTabbedPane ) comp; 225 return (RefactoringPanel) tabs.getSelectedComponent(); 226 } else { 227 if (comp instanceof RefactoringPanel) 228 return (RefactoringPanel) comp; 229 } 230 } 231 return null; 232 } 233 234 private final class PrevNextAction extends javax.swing.AbstractAction { 235 private boolean prev; 236 237 public PrevNextAction (boolean prev) { 238 this.prev = prev; 239 } 240 241 public void actionPerformed (java.awt.event.ActionEvent actionEvent) { 242 RefactoringPanel panel = getCurrentPanel(); 243 if (panel != null) { 244 if (prev) { 245 panel.selectPrevUsage(); 246 } else { 247 panel.selectNextUsage(); 248 } 249 } 250 } 251 } 252 253 254 private class CloseListener implements PropertyChangeListener { 255 256 public void propertyChange(java.beans.PropertyChangeEvent evt) { 257 if (CloseButtonTabbedPane.PROP_CLOSE.equals(evt.getPropertyName())) { 258 removePanel((JPanel ) evt.getNewValue()); 259 } 260 } 261 262 } 263 266 private class PopupListener extends MouseUtils.PopupMouseAdapter { 267 268 271 protected void showPopup (MouseEvent e) { 272 pop.show(RefactoringPanelContainer.this, e.getX(), e.getY()); 273 } 274 } 276 private class Close extends AbstractAction { 277 278 public Close() { 279 super(NbBundle.getMessage(RefactoringPanelContainer.class, "LBL_CloseWindow")); 280 } 281 282 public void actionPerformed(ActionEvent e) { 283 removePanel(null); 284 } 285 } 286 287 private final class CloseAll extends AbstractAction { 288 289 public CloseAll() { 290 super(NbBundle.getMessage(RefactoringPanelContainer.class, "LBL_CloseAll")); 291 } 292 293 public void actionPerformed(ActionEvent e) { 294 close(); 295 } 296 } 297 298 private class CloseAllButCurrent extends AbstractAction { 299 300 public CloseAllButCurrent() { 301 super(NbBundle.getMessage(RefactoringPanelContainer.class, "LBL_CloseAllButCurrent")); 302 } 303 304 public void actionPerformed(ActionEvent e) { 305 closeAllButCurrent(); 306 } 307 } 308 309 public HelpCtx getHelpCtx() { 310 return new HelpCtx(RefactoringPanelContainer.class.getName() + (isRefactoring ? ".refactoring-preview" : ".find-usages") ); } 312 313 public java.awt.Image getIcon() { 314 if (isRefactoring) 315 return REFACTORING_BADGE; 316 else 317 return USAGES_BADGE; 318 } 319 } 320 | Popular Tags |