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:
042: package org.netbeans.spi.palette;
043:
044: import java.awt.BorderLayout;
045: import java.awt.Dimension;
046: import java.beans.PropertyChangeEvent;
047: import java.beans.PropertyChangeListener;
048: import org.netbeans.modules.palette.Utils;
049: import org.netbeans.modules.palette.ui.PalettePanel;
050: import org.openide.util.HelpCtx;
051: import org.openide.util.RequestProcessor;
052: import org.openide.util.Utilities;
053: import org.openide.windows.TopComponent;
054:
055: /**
056: * <p>Top component which displays component palette.</p>
057: *
058: *
059: * @author S. Aubrecht
060: */
061:
062: final class PaletteTopComponent extends TopComponent implements
063: PropertyChangeListener {
064:
065: static final long serialVersionUID = 4248268998485315735L;
066:
067: private static PaletteTopComponent instance;
068: /** holds currently scheduled/running task for set of activated node */
069: private RequestProcessor.Task nodeSetterTask;
070: private final Object NODE_SETTER_LOCK = new Object();
071:
072: private TopComponent paletteSource;
073:
074: /** Creates new PaletteTopComponent */
075: private PaletteTopComponent() {
076: setName(Utils.getBundleString("CTL_Component_palette")); // NOI18N
077: setToolTipText(Utils.getBundleString("HINT_PaletteComponent"));
078: setIcon(Utilities
079: .loadImage("org/netbeans/modules/palette/resources/palette.png")); // NOI18N
080:
081: setLayout(new BorderLayout());
082: setPreferredSize(new Dimension(505, 88));
083: add(PalettePanel.getDefault().getScrollPane(),
084: BorderLayout.CENTER);
085:
086: putClientProperty("keepPreferredSizeWhenSlideIn", Boolean.TRUE); // NOI18N
087: }
088:
089: public void requestActive() {
090: super .requestActive();
091: PalettePanel.getDefault().requestFocusInWindow();
092: }
093:
094: /** Gets default instance. Don't use directly, it reserved for '.settings' file only,
095: * i.e. deserialization routines, otherwise you can get non-deserialized instance. */
096: public static synchronized PaletteTopComponent getDefault() {
097: if (instance == null) {
098: instance = new PaletteTopComponent();
099: }
100: return instance;
101: }
102:
103: /** Overriden to explicitely set persistence type of PaletteTopComponent
104: * to PERSISTENCE_ALWAYS */
105: public int getPersistenceType() {
106: return TopComponent.PERSISTENCE_NEVER;
107: }
108:
109: public void componentOpened() {
110: PaletteSwitch switcher = PaletteSwitch.getDefault();
111:
112: switcher.addPropertyChangeListener(this );
113: PaletteController pc = switcher.getCurrentPalette();
114: setPaletteController(pc);
115: if (Utils.isOpenedByUser(this )) {
116: //only change the flag when the Palette window was opened from ShowPaletteAction
117: //i.e. user clicked the menu item or used keyboard shortcut - ignore window system load & restore
118: PaletteVisibility.setVisible(pc, true);
119: }
120: }
121:
122: public void componentClosed() {
123: // palette is closed so reset its contents
124: PaletteSwitch switcher = PaletteSwitch.getDefault();
125:
126: switcher.removePropertyChangeListener(this );
127: PaletteController pc = switcher.getCurrentPalette();
128: PaletteVisibility.setVisible(pc, false);
129: // if( null != pc )
130: // PaletteVisibility.setVisible( null, false );
131: }
132:
133: /** replaces this in object stream */
134: public Object writeReplace() {
135: return new ResolvableHelper();
136: }
137:
138: protected String preferredID() {
139: return getClass().getName();
140: }
141:
142: public void propertyChange(PropertyChangeEvent e) {
143: if (PaletteSwitch.PROP_PALETTE_CONTENTS.equals(e
144: .getPropertyName())) {
145: PaletteController pc = (PaletteController) e.getNewValue();
146:
147: setPaletteController(pc);
148: }
149: }
150:
151: private void setPaletteController(PaletteController pc) {
152: if (null != pc) {
153: PalettePanel.getDefault().setContent(pc, pc.getModel(),
154: pc.getSettings());
155: } else {
156: PalettePanel.getDefault().setContent(null, null, null);
157: }
158: }
159:
160: public HelpCtx getHelpCtx() {
161: return PalettePanel.getDefault().getHelpCtx();
162: }
163:
164: final static class ResolvableHelper implements java.io.Serializable {
165: static final long serialVersionUID = 7424646018839457788L;
166:
167: public Object readResolve() {
168: return PaletteTopComponent.getDefault();
169: }
170: }
171: }
|