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-2006 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.refactoring.spi.impl;
042:
043: import java.awt.BorderLayout;
044: import java.awt.Image;
045: import java.awt.event.MouseEvent;
046: import java.beans.PropertyChangeListener;
047: import javax.swing.ActionMap;
048: import javax.swing.JLabel;
049: import javax.swing.JPanel;
050: import org.openide.awt.MouseUtils;
051: import org.openide.util.HelpCtx;
052: import org.openide.util.Utilities;
053: import org.openide.windows.TopComponent;
054: import org.openide.windows.WindowManager;
055: import java.awt.Component;
056: import java.awt.event.ActionEvent;
057: import javax.swing.AbstractAction;
058: import javax.swing.JPopupMenu;
059: import javax.swing.JTabbedPane;
060: import org.openide.util.NbBundle;
061:
062: /**
063: *
064: * @author Jan Becicka
065: */
066: public class RefactoringPanelContainer extends TopComponent {
067:
068: private static RefactoringPanelContainer usages = null;
069: private static RefactoringPanelContainer refactorings = null;
070: private transient boolean isVisible = false;
071: private JPopupMenu pop;
072: /** Popup menu listener */
073: private PopupListener listener;
074: private CloseListener closeL;
075: private boolean isRefactoring;
076: private static Image REFACTORING_BADGE = Utilities
077: .loadImage("org/netbeans/modules/refactoring/api/resources/refactoringpreview.png"); // NOI18N
078: private static Image USAGES_BADGE = Utilities
079: .loadImage("org/netbeans/modules/refactoring/api/resources/findusages.png"); // NOI18N
080:
081: private RefactoringPanelContainer() {
082: this ("", false);
083: }
084:
085: /** Creates new form RefactoringPanelContainer */
086: private RefactoringPanelContainer(String name, boolean isRefactoring) {
087: setName(name);
088: setToolTipText(name);
089: setFocusable(true);
090: setLayout(new java.awt.BorderLayout());
091: getAccessibleContext().setAccessibleDescription(
092: NbBundle.getMessage(RefactoringPanelContainer.class,
093: "ACSD_usagesPanel"));
094: pop = new JPopupMenu();
095: pop.add(new Close());
096: pop.add(new CloseAll());
097: pop.add(new CloseAllButCurrent());
098: listener = new PopupListener();
099: closeL = new CloseListener();
100: this .isRefactoring = isRefactoring;
101: setFocusCycleRoot(true);
102: JLabel label = new JLabel(NbBundle.getMessage(
103: RefactoringPanelContainer.class, "LBL_NoUsages"));
104: label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
105: this .add(label, BorderLayout.CENTER);
106: initActions();
107: }
108:
109: void addPanel(JPanel panel) {
110: RefactoringPanel.checkEventThread();
111: if (getComponentCount() == 0) {
112: add(panel, BorderLayout.CENTER);
113: } else {
114: Component comp = getComponent(0);
115: if (comp instanceof JTabbedPane) {
116: ((JTabbedPane) comp).addTab(panel.getName() + " ",
117: null, panel, panel.getToolTipText()); //NOI18N
118: ((JTabbedPane) comp).setSelectedComponent(panel);
119: comp.validate();
120: } else if (comp instanceof JLabel) {
121: remove(comp);
122: add(panel, BorderLayout.CENTER);
123: } else {
124: remove(comp);
125: JTabbedPane pane = new CloseButtonTabbedPane();
126: pane.addMouseListener(listener);
127: pane.addPropertyChangeListener(closeL);
128: add(pane, BorderLayout.CENTER);
129: pane.addTab(comp.getName() + " ", null, comp,
130: ((JPanel) comp).getToolTipText()); //NOI18N
131: pane.addTab(panel.getName() + " ", null, panel, panel
132: .getToolTipText()); //NOI18N
133: pane.setSelectedComponent(panel);
134: pane.validate();
135: }
136: }
137: if (!isVisible) {
138: isVisible = true;
139: open();
140: }
141: validate();
142: requestActive();
143: }
144:
145: protected void componentActivated() {
146: super .componentActivated();
147: JPanel panel = getCurrentPanel();
148: if (panel != null)
149: panel.requestFocus();
150: }
151:
152: void removePanel(JPanel panel) {
153: RefactoringPanel.checkEventThread();
154: Component comp = getComponentCount() > 0 ? getComponent(0)
155: : null;
156: if (comp instanceof JTabbedPane) {
157: JTabbedPane tabs = (JTabbedPane) comp;
158: if (panel == null) {
159: panel = (JPanel) tabs.getSelectedComponent();
160: }
161: tabs.remove(panel);
162: if (tabs.getComponentCount() == 1) {
163: Component c = tabs.getComponent(0);
164: tabs.removeMouseListener(listener);
165: tabs.removePropertyChangeListener(closeL);
166: remove(tabs);
167: add(c, BorderLayout.CENTER);
168: }
169: } else {
170: if (comp != null)
171: remove(comp);
172: isVisible = false;
173: close();
174: }
175: validate();
176: }
177:
178: void closeAllButCurrent() {
179: Component comp = getComponent(0);
180: if (comp instanceof JTabbedPane) {
181: JTabbedPane tabs = (JTabbedPane) comp;
182: Component current = tabs.getSelectedComponent();
183: Component[] c = tabs.getComponents();
184: for (int i = 0; i < c.length; i++) {
185: if (c[i] != current) {
186: ((RefactoringPanel) c[i]).close();
187: }
188: }
189: }
190: }
191:
192: public static synchronized RefactoringPanelContainer getUsagesComponent() {
193: if (usages == null) {
194: usages = (RefactoringPanelContainer) WindowManager
195: .getDefault().findTopComponent("find-usages"); //NOI18N
196: }
197: return usages;
198: }
199:
200: public static synchronized RefactoringPanelContainer getRefactoringComponent() {
201: if (refactorings == null) {
202: refactorings = (RefactoringPanelContainer) WindowManager
203: .getDefault().findTopComponent(
204: "refactoring-preview"); //NOI18N
205: }
206: return refactorings;
207: }
208:
209: public static synchronized RefactoringPanelContainer createRefactoringComponent() {
210: if (refactorings == null)
211: refactorings = new RefactoringPanelContainer(
212: org.openide.util.NbBundle.getMessage(
213: RefactoringPanelContainer.class,
214: "LBL_Refactoring"), true);
215: return refactorings;
216: }
217:
218: public static synchronized RefactoringPanelContainer createUsagesComponent() {
219: if (usages == null)
220: usages = new RefactoringPanelContainer(
221: org.openide.util.NbBundle.getMessage(
222: RefactoringPanelContainer.class,
223: "LBL_Usages"), false);
224: return usages;
225: }
226:
227: protected void closeNotify() {
228: isVisible = false;
229: if (getComponentCount() == 0) {
230: return;
231: }
232: Component comp = getComponent(0);
233: if (comp instanceof JTabbedPane) {
234: JTabbedPane pane = (JTabbedPane) comp;
235: Component[] c = pane.getComponents();
236: for (int i = 0; i < c.length; i++) {
237: ((RefactoringPanel) c[i]).close();
238: }
239: } else if (comp instanceof RefactoringPanel) {
240: ((RefactoringPanel) comp).close();
241: }
242: }
243:
244: protected String preferredID() {
245: return "RefactoringPanel"; // NOI18N
246: }
247:
248: public int getPersistenceType() {
249: return PERSISTENCE_ALWAYS;
250: }
251:
252: private void initActions() {
253: ActionMap map = getActionMap();
254:
255: map.put("jumpNext", new PrevNextAction(false)); // NOI18N
256: map.put("jumpPrev", new PrevNextAction(true)); // NOI18N
257: }
258:
259: public RefactoringPanel getCurrentPanel() {
260: if (getComponentCount() > 0) {
261: Component comp = getComponent(0);
262: if (comp instanceof JTabbedPane) {
263: JTabbedPane tabs = (JTabbedPane) comp;
264: return (RefactoringPanel) tabs.getSelectedComponent();
265: } else {
266: if (comp instanceof RefactoringPanel)
267: return (RefactoringPanel) comp;
268: }
269: }
270: return null;
271: }
272:
273: private final class PrevNextAction extends
274: javax.swing.AbstractAction {
275: private boolean prev;
276:
277: public PrevNextAction(boolean prev) {
278: this .prev = prev;
279: }
280:
281: public void actionPerformed(
282: java.awt.event.ActionEvent actionEvent) {
283: RefactoringPanel panel = getCurrentPanel();
284: if (panel != null) {
285: if (prev) {
286: panel.selectPrevUsage();
287: } else {
288: panel.selectNextUsage();
289: }
290: }
291: }
292: }
293:
294: private class CloseListener implements PropertyChangeListener {
295:
296: public void propertyChange(java.beans.PropertyChangeEvent evt) {
297: if (CloseButtonTabbedPane.PROP_CLOSE.equals(evt
298: .getPropertyName())) {
299: removePanel((JPanel) evt.getNewValue());
300: }
301: }
302:
303: }
304:
305: /**
306: * Class to showing popup menu
307: */
308: private class PopupListener extends MouseUtils.PopupMouseAdapter {
309:
310: /**
311: * Called when the sequence of mouse events should lead to actual showing popup menu
312: */
313: protected void showPopup(MouseEvent e) {
314: pop
315: .show(RefactoringPanelContainer.this , e.getX(), e
316: .getY());
317: }
318: } // end of PopupListener
319:
320: private class Close extends AbstractAction {
321:
322: public Close() {
323: super (NbBundle.getMessage(RefactoringPanelContainer.class,
324: "LBL_CloseWindow"));
325: }
326:
327: public void actionPerformed(ActionEvent e) {
328: removePanel(null);
329: }
330: }
331:
332: private final class CloseAll extends AbstractAction {
333:
334: public CloseAll() {
335: super (NbBundle.getMessage(RefactoringPanelContainer.class,
336: "LBL_CloseAll"));
337: }
338:
339: public void actionPerformed(ActionEvent e) {
340: close();
341: }
342: }
343:
344: private class CloseAllButCurrent extends AbstractAction {
345:
346: public CloseAllButCurrent() {
347: super (NbBundle.getMessage(RefactoringPanelContainer.class,
348: "LBL_CloseAllButCurrent"));
349: }
350:
351: public void actionPerformed(ActionEvent e) {
352: closeAllButCurrent();
353: }
354: }
355:
356: public HelpCtx getHelpCtx() {
357: return new HelpCtx(RefactoringPanelContainer.class.getName()
358: + (isRefactoring ? ".refactoring-preview"
359: : ".find-usages")); //NOI18N
360: }
361:
362: @Override
363: public java.awt.Image getIcon() {
364: if (isRefactoring)
365: return REFACTORING_BADGE;
366: else
367: return USAGES_BADGE;
368: }
369: }
|