KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > spi > impl > RefactoringPanelContainer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.refactoring.spi.impl;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.Image JavaDoc;
23 import java.awt.event.MouseEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import javax.swing.ActionMap JavaDoc;
26 import javax.swing.JLabel JavaDoc;
27 import javax.swing.JPanel JavaDoc;
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 JavaDoc;
34 import java.awt.event.ActionEvent JavaDoc;
35 import javax.swing.AbstractAction JavaDoc;
36 import javax.swing.JTabbedPane JavaDoc;
37 import org.openide.awt.JPopupMenuPlus;
38 import org.openide.util.NbBundle;
39
40 /**
41  *
42  * @author Jan Becicka
43  */

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     /** Popup menu listener */
51     private PopupListener listener;
52     private CloseListener closeL;
53     private boolean isRefactoring;
54     private static Image JavaDoc REFACTORING_BADGE = Utilities.loadImage( "org/netbeans/modules/refactoring/resources/refactoringpreview.png" ); // NOI18N
55
private static Image JavaDoc USAGES_BADGE = Utilities.loadImage( "org/netbeans/modules/refactoring/resources/findusages.png" ); // NOI18N
56

57     private RefactoringPanelContainer() {
58         this("", false);
59     }
60     /** Creates new form RefactoringPanelContainer */
61     private RefactoringPanelContainer(String JavaDoc name, boolean isRefactoring) {
62         setName(name);
63         setToolTipText(name);
64         setFocusable(true);
65         setLayout(new java.awt.BorderLayout JavaDoc());
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 JavaDoc label = new JLabel JavaDoc(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 JavaDoc 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 JavaDoc) {
90                 ((JTabbedPane JavaDoc) comp).addTab(panel.getName() + " ", null, panel, panel.getToolTipText()); //NOI18N
91
((JTabbedPane JavaDoc) comp).setSelectedComponent(panel);
92                 comp.validate();
93             } else if (comp instanceof JLabel JavaDoc) {
94                 remove(comp);
95                 add(panel, BorderLayout.CENTER);
96             } else {
97                 remove(comp);
98                 JTabbedPane JavaDoc pane = new CloseButtonTabbedPane();
99                 pane.addMouseListener(listener);
100                 pane.addPropertyChangeListener(closeL);
101                 add(pane, BorderLayout.CENTER);
102                 pane.addTab(comp.getName() + " ", null, comp, ((JPanel JavaDoc) comp).getToolTipText()); //NOI18N
103
pane.addTab(panel.getName() + " ", null, panel, panel.getToolTipText()); //NOI18N
104
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 JavaDoc panel = getCurrentPanel();
119         if (panel!=null)
120             panel.requestFocus();
121     }
122     
123     void removePanel(JPanel JavaDoc panel) {
124         RefactoringPanel.checkEventThread();
125         Component comp = getComponentCount() > 0 ? getComponent(0) : null;
126         if (comp instanceof JTabbedPane JavaDoc) {
127             JTabbedPane JavaDoc tabs = (JTabbedPane JavaDoc) comp;
128             if (panel == null) {
129                 panel = (JPanel JavaDoc) 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 JavaDoc) {
151             JTabbedPane JavaDoc tabs = (JTabbedPane JavaDoc) 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" ); //NOI18N
165
}
166         return usages;
167     }
168     
169     public static synchronized RefactoringPanelContainer getRefactoringComponent() {
170         if (refactorings == null) {
171             refactorings = (RefactoringPanelContainer) WindowManager.getDefault().findTopComponent( "refactoring-preview" ); //NOI18N
172
}
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 JavaDoc) {
195             JTabbedPane JavaDoc pane = (JTabbedPane JavaDoc) 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 JavaDoc preferredID() {
206         return "RefactoringPanel"; // NOI18N
207
}
208
209     public int getPersistenceType() {
210         return PERSISTENCE_ALWAYS;
211     }
212     
213     private void initActions() {
214         ActionMap JavaDoc map = getActionMap();
215
216         map.put("jumpNext", new PrevNextAction (false)); // NOI18N
217
map.put("jumpPrev", new PrevNextAction (true)); // NOI18N
218
}
219     
220     public RefactoringPanel getCurrentPanel() {
221         if (getComponentCount() > 0) {
222             Component comp = getComponent(0);
223             if (comp instanceof JTabbedPane JavaDoc) {
224                 JTabbedPane JavaDoc tabs = (JTabbedPane JavaDoc) 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 JavaDoc {
235         private boolean prev;
236         
237         public PrevNextAction (boolean prev) {
238             this.prev = prev;
239         }
240
241         public void actionPerformed (java.awt.event.ActionEvent JavaDoc 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 JavaDoc {
255         
256         public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
257             if (CloseButtonTabbedPane.PROP_CLOSE.equals(evt.getPropertyName())) {
258                 removePanel((JPanel JavaDoc) evt.getNewValue());
259             }
260         }
261         
262     }
263     /**
264     * Class to showing popup menu
265     */

266     private class PopupListener extends MouseUtils.PopupMouseAdapter {
267
268         /**
269          * Called when the sequence of mouse events should lead to actual showing popup menu
270          */

271         protected void showPopup (MouseEvent JavaDoc e) {
272             pop.show(RefactoringPanelContainer.this, e.getX(), e.getY());
273         }
274     } // end of PopupListener
275

276     private class Close extends AbstractAction JavaDoc {
277         
278         public Close() {
279             super(NbBundle.getMessage(RefactoringPanelContainer.class, "LBL_CloseWindow"));
280         }
281         
282         public void actionPerformed(ActionEvent JavaDoc e) {
283             removePanel(null);
284         }
285     }
286     
287     private final class CloseAll extends AbstractAction JavaDoc {
288         
289         public CloseAll() {
290             super(NbBundle.getMessage(RefactoringPanelContainer.class, "LBL_CloseAll"));
291         }
292         
293         public void actionPerformed(ActionEvent JavaDoc e) {
294             close();
295         }
296     }
297     
298     private class CloseAllButCurrent extends AbstractAction JavaDoc {
299         
300         public CloseAllButCurrent() {
301             super(NbBundle.getMessage(RefactoringPanelContainer.class, "LBL_CloseAllButCurrent"));
302         }
303         
304         public void actionPerformed(ActionEvent JavaDoc e) {
305             closeAllButCurrent();
306         }
307     }
308     
309     public HelpCtx getHelpCtx() {
310         return new HelpCtx(RefactoringPanelContainer.class.getName() + (isRefactoring ? ".refactoring-preview" : ".find-usages") ); //NOI18N
311
}
312
313     public java.awt.Image JavaDoc getIcon() {
314         if (isRefactoring)
315             return REFACTORING_BADGE;
316         else
317             return USAGES_BADGE;
318     }
319 }
320
Popular Tags