001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.xml.schema.refactoring.ui;
042:
043: import java.awt.GridBagConstraints;
044: import java.awt.GridBagLayout;
045: import java.awt.Image;
046: import java.awt.event.MouseEvent;
047: import java.beans.PropertyChangeListener;
048: import javax.swing.JLabel;
049: import javax.swing.JPanel;
050: import javax.swing.JPopupMenu;
051: import org.openide.awt.MouseUtils;
052: import org.openide.util.HelpCtx;
053: import org.openide.util.Utilities;
054: import org.openide.windows.TopComponent;
055: import org.openide.windows.WindowManager;
056: import java.awt.Component;
057: import java.awt.event.ActionEvent;
058: import javax.swing.AbstractAction;
059: import javax.swing.JTabbedPane; //import org.openide.awt.JPopupMenuPlus;
060: import org.openide.util.NbBundle;
061:
062: //import org.netbeans.modules.xml.refactoring.ui.j.ui.CloseButtonTabbedPane;
063:
064: /**
065: *
066: * @author Jan Becicka
067: * @author Jeri Lockhart
068: */
069: public class QueryPanelContainer extends TopComponent {
070: public static final long serialVersionUID = 1L;
071:
072: private static QueryPanelContainer usages = null;
073: private static QueryPanelContainer refactorings = null;
074: private transient boolean isVisible = false;
075: private JPopupMenu pop;
076: /** Popup menu listener */
077: private PopupListener listener;
078: private CloseListener closeL;
079: private boolean isRefactoring;
080: private static Image QUERY_BADGE = Utilities
081: .loadImage("org/netbeans/modules/refactoring/resources/"
082: + "findusages.png"); // NOI18N
083: private static final String XML_SCHEMA_QUERY = "xml-schema-query"; // NOI18N
084: // private transient InstanceContent selectedNode;
085: // public static final String SCHEMA_COMPONENT_SELECTION =
086: // "schema-component-selection";
087: public static final String PROP_SCHEMA_COMPONENT_CHANGED = "prop-schema-component-changed";
088: public static final String PROP_REQUEST_HIGHLIGHT_IN_SCHEMA_VIEW = "prop-request-highlight-in-schema-view";
089:
090: private QueryPanelContainer() {
091: this ("", false);
092: }
093:
094: /**
095: * Creates new form QueryPanelContainer
096: */
097: private QueryPanelContainer(String name, boolean isRefactoring) {
098: setName(name);
099: setToolTipText(name);
100: setFocusable(true);
101: setLayout(new GridBagLayout());
102: getAccessibleContext().setAccessibleDescription(
103: NbBundle.getMessage(QueryPanelContainer.class,
104: "ACSD_usagesPanel"));
105: pop = new JPopupMenu();
106: pop.add(new Close());
107: pop.add(new CloseAll());
108: pop.add(new CloseAllButCurrent());
109: listener = new PopupListener();
110: closeL = new CloseListener();
111: this .isRefactoring = isRefactoring;
112: setFocusCycleRoot(true);
113: JLabel label = new JLabel(NbBundle.getMessage(
114: QueryPanelContainer.class, "LBL_NoUsages"));
115: label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
116: GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
117: gridBagConstraints.gridx = 0;
118: gridBagConstraints.gridy = 0;
119: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
120: gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
121: this .add(label, gridBagConstraints);
122: }
123:
124: public void addPanel(JPanel panel) {
125: QueryPanel.checkEventThread();
126: if (panel == null) {
127: return;
128: }
129: // panel.addPropertyChangeListener(
130: // QueryPanel.NODE_SELECTION_CHANGE,
131: // this);
132: GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
133: gridBagConstraints.gridx = 0;
134: gridBagConstraints.gridy = 0;
135: gridBagConstraints.weightx = 0.2;
136: gridBagConstraints.weighty = 0.2;
137: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
138: gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
139: if (getComponentCount() == 0) {
140: add(panel, gridBagConstraints);
141: } else {
142: Component comp = getComponent(0);
143: if (comp instanceof JTabbedPane) {
144: ((JTabbedPane) comp).addTab(panel.getName() + " ",
145: null, panel, panel.getToolTipText()); //NOI18N
146: ((JTabbedPane) comp).setSelectedComponent(panel);
147: comp.validate();
148: } else if (comp instanceof JLabel) {
149: remove(comp);
150: add(panel, gridBagConstraints);
151: } else {
152: remove(comp);
153: JTabbedPane pane = new CloseButtonTabbedPane();
154: pane.addMouseListener(listener);
155: pane.addPropertyChangeListener(closeL);
156: add(pane, gridBagConstraints);
157: pane.addTab(comp.getName() + " ", null, comp,
158: ((JPanel) comp).getToolTipText()); //NOI18N
159: pane.addTab(panel.getName() + " ", null, panel, panel
160: .getToolTipText()); //NOI18N
161: pane.setSelectedComponent(panel);
162: pane.validate();
163: repaint();
164: }
165: }
166: if (!isVisible) {
167: isVisible = true;
168: open();
169: }
170: validate();
171: requestActive();
172: }
173:
174: protected void componentActivated() {
175: super .componentActivated();
176: JPanel panel = getCurrentPanel();
177: if (panel != null)
178: panel.requestFocus();
179: }
180:
181: void removePanel(JPanel panel) {
182: // HighlightManager hm = HighlightManager.getDefault();
183: // Lookup hmLookup = Lookups.singleton(hm);
184: // HighlightProvider.hideResults(hmLookup);
185: QueryPanel.checkEventThread();
186: Component comp = getComponentCount() > 0 ? getComponent(0)
187: : null;
188: if (comp instanceof JTabbedPane) {
189: JTabbedPane tabs = (JTabbedPane) comp;
190: if (panel == null) {
191: panel = (JPanel) tabs.getSelectedComponent();
192: }
193: tabs.remove(panel);
194: if (tabs.getComponentCount() == 1) {
195: Component c = tabs.getComponent(0);
196: tabs.removeMouseListener(listener);
197: tabs.removePropertyChangeListener(closeL);
198: remove(tabs);
199: GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
200: gridBagConstraints.gridx = 0;
201: gridBagConstraints.gridy = 0;
202: gridBagConstraints.weightx = 0.2;
203: gridBagConstraints.weighty = 0.2;
204: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
205: gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
206: add(c, gridBagConstraints);
207: }
208: } else {
209: if (comp != null)
210: remove(comp);
211: isVisible = false;
212: close();
213: }
214: validate();
215: repaint();
216: }
217:
218: void closeAllButCurrent() {
219: Component comp = getComponent(0);
220: if (comp instanceof JTabbedPane) {
221: JTabbedPane tabs = (JTabbedPane) comp;
222: Component current = tabs.getSelectedComponent();
223: Component[] c = tabs.getComponents();
224: for (int i = 0; i < c.length; i++) {
225: if (c[i] != current) {
226: ((QueryPanel) c[i]).close();
227: }
228: }
229: }
230: }
231:
232: public static synchronized QueryPanelContainer getUsagesComponent() {
233: if (usages == null) {
234: usages = (QueryPanelContainer) WindowManager.getDefault()
235: .findTopComponent(XML_SCHEMA_QUERY);
236: }
237: return usages;
238: }
239:
240: public static synchronized QueryPanelContainer createUsagesComponent() {
241: if (usages == null)
242: usages = new QueryPanelContainer(NbBundle.getMessage(
243: QueryPanelContainer.class, "LBL_Usages"), false);
244: return usages;
245: }
246:
247: protected void componentClosed() {
248: isVisible = false;
249: if (getComponentCount() == 0) {
250: return;
251: }
252: Component comp = getComponent(0);
253: if (comp instanceof JTabbedPane) {
254: JTabbedPane pane = (JTabbedPane) comp;
255: Component[] c = pane.getComponents();
256: for (int i = 0; i < c.length; i++) {
257: ((QueryPanel) c[i]).close();
258: }
259: } else if (comp instanceof QueryPanel) {
260: ((QueryPanel) comp).close();
261: }
262: }
263:
264: protected String preferredID() {
265: return "QueryPanel"; // NOI18N
266: }
267:
268: public int getPersistenceType() {
269: return PERSISTENCE_ALWAYS;
270: }
271:
272: // private void initActions() {
273: // ActionMap map = getActionMap();
274: //
275: // map.put("jumpNext", new PrevNextAction (false)); // NOI18N
276: // map.put("jumpPrev", new PrevNextAction (true)); // NOI18N
277: // }
278:
279: private QueryPanel getCurrentPanel() {
280: if (getComponentCount() > 0) {
281: Component comp = getComponent(0);
282: if (comp instanceof JTabbedPane) {
283: JTabbedPane tabs = (JTabbedPane) comp;
284: return (QueryPanel) tabs.getSelectedComponent();
285: } else {
286: if (comp instanceof QueryPanel)
287: return (QueryPanel) comp;
288: }
289: }
290: return null;
291: }
292:
293: // private final class PrevNextAction extends javax.swing.AbstractAction {
294: // public static final long serialVersionUID = 1L;
295: // private boolean prev;
296: //
297: // public PrevNextAction (boolean prev) {
298: // this.prev = prev;
299: // }
300: //
301: // public void actionPerformed (java.awt.event.ActionEvent actionEvent) {
302: // QueryPanel panel = getCurrentPanel();
303: // if (panel != null) {
304: // if (prev) {
305: // panel.selectPrevUsage();
306: // } else {
307: // panel.selectNextUsage();
308: // }
309: // }
310: // }
311: // }
312:
313: private class CloseListener implements PropertyChangeListener {
314:
315: public void propertyChange(java.beans.PropertyChangeEvent evt) {
316: // if (CloseButtonTabbedPane.PROP_CLOSE.equals(
317: // evt.getPropertyName())) {
318: // removePanel((JPanel) evt.getNewValue());
319: //}
320: }
321:
322: }
323:
324: /**
325: * Class to showing popup menu
326: */
327: private class PopupListener extends MouseUtils.PopupMouseAdapter {
328:
329: /**
330: * Called when the sequence of mouse events should lead to
331: * actual showing popup menu
332: */
333: protected void showPopup(MouseEvent e) {
334: pop.show(QueryPanelContainer.this , e.getX(), e.getY());
335: }
336: } // end of PopupListener
337:
338: private class Close extends AbstractAction {
339: public static final long serialVersionUID = 1L;
340:
341: public Close() {
342: super (NbBundle.getMessage(QueryPanelContainer.class,
343: "LBL_CloseWindow"));
344: }
345:
346: public void actionPerformed(ActionEvent e) {
347: removePanel(null);
348: }
349: }
350:
351: private final class CloseAll extends AbstractAction {
352: public static final long serialVersionUID = 1L;
353:
354: public CloseAll() {
355: super (NbBundle.getMessage(QueryPanelContainer.class,
356: "LBL_CloseAll"));
357: }
358:
359: public void actionPerformed(ActionEvent e) {
360: close();
361: }
362: }
363:
364: private class CloseAllButCurrent extends AbstractAction {
365: public static final long serialVersionUID = 1L;
366:
367: public CloseAllButCurrent() {
368: super (NbBundle.getMessage(QueryPanelContainer.class,
369: "LBL_CloseAllButCurrent"));
370: }
371:
372: public void actionPerformed(ActionEvent e) {
373: closeAllButCurrent();
374: }
375: }
376:
377: public HelpCtx getHelpCtx() {
378: //return HelpCtx.DEFAULT_HELP;
379: return new HelpCtx(QueryPanelContainer.class.getName()
380: + (isRefactoring ? ".refactoring-preview"
381: : ".find-usages"));//NOI18N
382: }
383:
384: public java.awt.Image getIcon() {
385: return QUERY_BADGE;
386: }
387:
388: /**
389: * Implement PropertyChangeListener
390: * NODE_SELECTION_CHANGE
391: */
392: // public void propertyChange(PropertyChangeEvent evt) {
393: // String propName = evt.getPropertyName();
394: // if (propName.equals(
395: // QueryPanel.NODE_SELECTION_CHANGE)){
396: //// selectedNode.set(Arrays.asList(evt.getNewValue()), null);
397: // if (evt.getNewValue() != null){
398: // Object newVal = evt.getNewValue();
399: // // The newVal could also be a Project, FileObject, SourceGroup
400: //
401: // assert newVal != null:
402: // "RefactoringPanel should not fire NODE_SELECTION_CHANGE" +
403: // " with a null new value";
404: // firePropertyChange(PROP_SCHEMA_COMPONENT_CHANGED, null, newVal);
405: // }
406: // }
407: // }
408:
409: }
|