KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > refactoring > ui > QueryPanelContainer


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.schema.refactoring.ui;
20
21 import java.awt.GridBagConstraints JavaDoc;
22 import java.awt.GridBagLayout JavaDoc;
23 import java.awt.Image JavaDoc;
24 import java.awt.event.MouseEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import javax.swing.JLabel JavaDoc;
27 import javax.swing.JPanel JavaDoc;
28 import javax.swing.JPopupMenu JavaDoc;
29 import org.openide.awt.MouseUtils;
30 import org.openide.util.HelpCtx;
31 import org.openide.util.Utilities;
32 import org.openide.windows.TopComponent;
33 import org.openide.windows.WindowManager;
34 import java.awt.Component JavaDoc;
35 import java.awt.event.ActionEvent JavaDoc;
36 import javax.swing.AbstractAction JavaDoc;
37 import javax.swing.JTabbedPane JavaDoc;
38 //import org.openide.awt.JPopupMenuPlus;
39
import org.openide.util.NbBundle;
40 //import org.netbeans.modules.xml.refactoring.ui.j.ui.CloseButtonTabbedPane;
41

42 /**
43  *
44  * @author Jan Becicka
45  * @author Jeri Lockhart
46  */

47 public class QueryPanelContainer extends TopComponent
48          {
49     public static final long serialVersionUID = 1L;
50     
51     private static QueryPanelContainer usages = null;
52     private static QueryPanelContainer refactorings = null;
53     private transient boolean isVisible = false;
54     private JPopupMenu JavaDoc pop;
55     /** Popup menu listener */
56     private PopupListener listener;
57     private CloseListener closeL;
58     private boolean isRefactoring;
59     private static Image JavaDoc QUERY_BADGE = Utilities.loadImage(
60             "org/netbeans/modules/refactoring/resources/"+
61             "findusages.png" ); // NOI18N
62
private static final String JavaDoc XML_SCHEMA_QUERY = "xml-schema-query"; // NOI18N
63
// private transient InstanceContent selectedNode;
64
// public static final String SCHEMA_COMPONENT_SELECTION =
65
// "schema-component-selection";
66
public static final String JavaDoc PROP_SCHEMA_COMPONENT_CHANGED =
67             "prop-schema-component-changed";
68     public static final String JavaDoc PROP_REQUEST_HIGHLIGHT_IN_SCHEMA_VIEW =
69             "prop-request-highlight-in-schema-view";
70     
71     
72     private QueryPanelContainer() {
73         this("", false);
74     }
75     /**
76      * Creates new form QueryPanelContainer
77      */

78     private QueryPanelContainer(String JavaDoc name, boolean isRefactoring) {
79         setName(name);
80         setToolTipText(name);
81         setFocusable(true);
82         setLayout(new GridBagLayout JavaDoc());
83         getAccessibleContext().setAccessibleDescription(
84             NbBundle.getMessage(QueryPanelContainer.class,
85                 "ACSD_usagesPanel")
86         );
87         pop = new JPopupMenu JavaDoc();
88         pop.add(new Close());
89         pop.add(new CloseAll());
90         pop.add(new CloseAllButCurrent());
91         listener = new PopupListener();
92         closeL = new CloseListener();
93         this.isRefactoring = isRefactoring;
94         setFocusCycleRoot(true);
95         JLabel JavaDoc label = new JLabel JavaDoc(NbBundle.getMessage(
96                 QueryPanelContainer.class, "LBL_NoUsages"));
97         label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
98         GridBagConstraints JavaDoc gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
99         gridBagConstraints.gridx = 0;
100         gridBagConstraints.gridy = 0;
101         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
102         gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
103         this.add(label, gridBagConstraints);
104     }
105   
106     
107     
108     
109     public void addPanel(JPanel JavaDoc panel) {
110         QueryPanel.checkEventThread();
111         if (panel == null){
112             return;
113         }
114 // panel.addPropertyChangeListener(
115
// QueryPanel.NODE_SELECTION_CHANGE,
116
// this);
117
GridBagConstraints JavaDoc gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
118         gridBagConstraints.gridx = 0;
119         gridBagConstraints.gridy = 0;
120         gridBagConstraints.weightx = 0.2;
121         gridBagConstraints.weighty = 0.2;
122         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
123         gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
124         if (getComponentCount() == 0) {
125             add(panel, gridBagConstraints);
126         } else {
127             Component comp = getComponent(0);
128             if (comp instanceof JTabbedPane JavaDoc) {
129                 ((JTabbedPane JavaDoc) comp).addTab(panel.getName() + " ",
130                         null, panel, panel.getToolTipText()); //NOI18N
131
((JTabbedPane JavaDoc) comp).setSelectedComponent(panel);
132                 comp.validate();
133             } else if (comp instanceof JLabel JavaDoc) {
134                 remove(comp);
135                 add(panel, gridBagConstraints);
136             } else {
137                 remove(comp);
138                 JTabbedPane JavaDoc pane = new CloseButtonTabbedPane();
139                 pane.addMouseListener(listener);
140                 pane.addPropertyChangeListener(closeL);
141                 add(pane, gridBagConstraints);
142                 pane.addTab(comp.getName() + " ", null, comp,
143                         ((JPanel JavaDoc) comp).getToolTipText()); //NOI18N
144
pane.addTab(panel.getName() + " ", null, panel,
145                         panel.getToolTipText()); //NOI18N
146
pane.setSelectedComponent(panel);
147                 pane.validate();
148                 repaint();
149             }
150         }
151         if (!isVisible) {
152             isVisible = true;
153             open();
154         }
155         validate();
156         requestActive();
157     }
158
159     protected void componentActivated () {
160         super.componentActivated();
161         JPanel JavaDoc panel = getCurrentPanel();
162         if (panel!=null)
163             panel.requestFocus();
164     }
165     
166     void removePanel(JPanel JavaDoc panel) {
167 // HighlightManager hm = HighlightManager.getDefault();
168
// Lookup hmLookup = Lookups.singleton(hm);
169
// HighlightProvider.hideResults(hmLookup);
170
QueryPanel.checkEventThread();
171         Component comp = getComponentCount() > 0 ? getComponent(0) : null;
172         if (comp instanceof JTabbedPane JavaDoc) {
173             JTabbedPane JavaDoc tabs = (JTabbedPane JavaDoc) comp;
174             if (panel == null) {
175                 panel = (JPanel JavaDoc) tabs.getSelectedComponent();
176             }
177             tabs.remove(panel);
178             if (tabs.getComponentCount() == 1) {
179                 Component c = tabs.getComponent(0);
180                 tabs.removeMouseListener(listener);
181                 tabs.removePropertyChangeListener(closeL);
182                 remove(tabs);
183                 GridBagConstraints JavaDoc gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
184                 gridBagConstraints.gridx = 0;
185                 gridBagConstraints.gridy = 0;
186                 gridBagConstraints.weightx = 0.2;
187                 gridBagConstraints.weighty = 0.2;
188                 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
189                 gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
190                 add(c, gridBagConstraints);
191             }
192         } else {
193             if (comp != null)
194                 remove(comp);
195             isVisible = false;
196             close();
197         }
198         validate();
199         repaint();
200     }
201     
202     void closeAllButCurrent() {
203         Component comp = getComponent(0);
204         if (comp instanceof JTabbedPane JavaDoc) {
205             JTabbedPane JavaDoc tabs = (JTabbedPane JavaDoc) comp;
206             Component current = tabs.getSelectedComponent();
207             Component[] c = tabs.getComponents();
208             for (int i = 0; i< c.length; i++) {
209                 if (c[i]!=current) {
210                     ((QueryPanel) c[i]).close();
211                 }
212             }
213         }
214     }
215     
216     public static synchronized QueryPanelContainer getUsagesComponent() {
217         if ( usages == null ) {
218             usages =
219                     (QueryPanelContainer)
220                     WindowManager.getDefault().findTopComponent(
221                     XML_SCHEMA_QUERY );
222         }
223         return usages;
224     }
225     
226     public static synchronized QueryPanelContainer
227             createUsagesComponent() {
228         if (usages == null)
229             usages = new QueryPanelContainer(
230                     NbBundle.getMessage(QueryPanelContainer.class,
231                     "LBL_Usages"), false);
232         return usages;
233     }
234     
235     protected void componentClosed() {
236         isVisible = false;
237         if (getComponentCount() == 0) {
238             return ;
239         }
240         Component comp = getComponent(0);
241         if (comp instanceof JTabbedPane JavaDoc) {
242             JTabbedPane JavaDoc pane = (JTabbedPane JavaDoc) comp;
243             Component[] c = pane.getComponents();
244             for (int i = 0; i< c.length; i++) {
245                 ((QueryPanel) c[i]).close();
246             }
247         } else if (comp instanceof QueryPanel) {
248             ((QueryPanel) comp).close();
249         }
250     }
251     
252     protected String JavaDoc preferredID() {
253         return "QueryPanel"; // NOI18N
254
}
255
256     public int getPersistenceType() {
257         return PERSISTENCE_ALWAYS;
258     }
259     
260 // private void initActions() {
261
// ActionMap map = getActionMap();
262
//
263
// map.put("jumpNext", new PrevNextAction (false)); // NOI18N
264
// map.put("jumpPrev", new PrevNextAction (true)); // NOI18N
265
// }
266

267     private QueryPanel getCurrentPanel() {
268         if (getComponentCount() > 0) {
269             Component comp = getComponent(0);
270             if (comp instanceof JTabbedPane JavaDoc) {
271                 JTabbedPane JavaDoc tabs = (JTabbedPane JavaDoc) comp;
272                 return (QueryPanel) tabs.getSelectedComponent();
273             } else {
274                 if (comp instanceof QueryPanel)
275                     return (QueryPanel) comp;
276             }
277         }
278         return null;
279     }
280     
281 // private final class PrevNextAction extends javax.swing.AbstractAction {
282
// public static final long serialVersionUID = 1L;
283
// private boolean prev;
284
//
285
// public PrevNextAction (boolean prev) {
286
// this.prev = prev;
287
// }
288
//
289
// public void actionPerformed (java.awt.event.ActionEvent actionEvent) {
290
// QueryPanel panel = getCurrentPanel();
291
// if (panel != null) {
292
// if (prev) {
293
// panel.selectPrevUsage();
294
// } else {
295
// panel.selectNextUsage();
296
// }
297
// }
298
// }
299
// }
300

301     
302     private class CloseListener implements PropertyChangeListener JavaDoc {
303         
304         public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
305          // if (CloseButtonTabbedPane.PROP_CLOSE.equals(
306
// evt.getPropertyName())) {
307
// removePanel((JPanel) evt.getNewValue());
308
//}
309
}
310         
311     }
312     /**
313     * Class to showing popup menu
314     */

315     private class PopupListener extends MouseUtils.PopupMouseAdapter {
316
317         /**
318          * Called when the sequence of mouse events should lead to
319          * actual showing popup menu
320          */

321         protected void showPopup (MouseEvent JavaDoc e) {
322             pop.show(QueryPanelContainer.this, e.getX(), e.getY());
323         }
324     } // end of PopupListener
325

326     private class Close extends AbstractAction JavaDoc {
327     public static final long serialVersionUID = 1L;
328         
329         public Close() {
330             super(NbBundle.getMessage(QueryPanelContainer.class,
331                     "LBL_CloseWindow"));
332         }
333         
334         public void actionPerformed(ActionEvent JavaDoc e) {
335             removePanel(null);
336         }
337     }
338     
339     private final class CloseAll extends AbstractAction JavaDoc {
340     public static final long serialVersionUID = 1L;
341         
342         public CloseAll() {
343             super(NbBundle.getMessage(QueryPanelContainer.class,
344                     "LBL_CloseAll"));
345         }
346         
347         public void actionPerformed(ActionEvent JavaDoc e) {
348             close();
349         }
350     }
351     
352     private class CloseAllButCurrent extends AbstractAction JavaDoc {
353     public static final long serialVersionUID = 1L;
354         
355         public CloseAllButCurrent() {
356             super(NbBundle.getMessage(QueryPanelContainer.class,
357                     "LBL_CloseAllButCurrent"));
358         }
359         
360         public void actionPerformed(ActionEvent JavaDoc e) {
361             closeAllButCurrent();
362         }
363     }
364     
365     public HelpCtx getHelpCtx() {
366         //return HelpCtx.DEFAULT_HELP;
367
return new HelpCtx(QueryPanelContainer.class.getName()
368         + (isRefactoring ? ".refactoring-preview" : ".find-usages") );//NOI18N
369
}
370
371     public java.awt.Image JavaDoc getIcon() {
372          return QUERY_BADGE;
373     }
374
375
376     
377     /**
378      * Implement PropertyChangeListener
379      * NODE_SELECTION_CHANGE
380      */

381 // public void propertyChange(PropertyChangeEvent evt) {
382
// String propName = evt.getPropertyName();
383
// if (propName.equals(
384
// QueryPanel.NODE_SELECTION_CHANGE)){
385
//// selectedNode.set(Arrays.asList(evt.getNewValue()), null);
386
// if (evt.getNewValue() != null){
387
// Object newVal = evt.getNewValue();
388
// // The newVal could also be a Project, FileObject, SourceGroup
389
//
390
// assert newVal != null:
391
// "RefactoringPanel should not fire NODE_SELECTION_CHANGE" +
392
// " with a null new value";
393
// firePropertyChange(PROP_SCHEMA_COMPONENT_CHANGED, null, newVal);
394
// }
395
// }
396
// }
397

398
399     
400 }
401
Popular Tags