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: * Contributor(s): Soot Phengsy
042: */
043:
044: package org.netbeans.swing.dirchooser;
045:
046: import java.beans.PropertyChangeEvent;
047: import java.beans.PropertyChangeListener;
048: import javax.swing.UIDefaults;
049: import javax.swing.UIManager;
050: import javax.swing.plaf.FileChooserUI;
051: import org.openide.modules.ModuleInstall;
052:
053: /**
054: * Registers the directory chooser in NetBeans.
055: *
056: * @author Soot Phengsy
057: */
058: public class Module extends ModuleInstall {
059:
060: private static final String KEY = "FileChooserUI"; // NOI18N
061: private static Class<? extends FileChooserUI> originalImpl;
062: private static PropertyChangeListener pcl;
063:
064: private static final String QUICK_CHOOSER_NAME = "org.netbeans.modules.quickfilechooser.ChooserComponentUI";
065:
066: private static final String FORCE_STANDARD_CHOOSER = "standard-file-chooser"; // NOI18N
067:
068: @Override
069: public void restored() {
070: install();
071: }
072:
073: @Override
074: public void uninstalled() {
075: uninstall();
076: }
077:
078: public static void install() {
079: // don't install directory chooser if standard chooser is desired
080: if (isStandardChooserForced()) {
081: return;
082: }
083: final UIDefaults uid = UIManager.getDefaults();
084: originalImpl = (Class<? extends FileChooserUI>) uid
085: .getUIClass(KEY);
086: Class impl = DelegatingChooserUI.class;
087: final String val = impl.getName();
088: // don't install dirchooser if quickfilechooser is present
089: if (!isQuickFileChooser(uid.get(KEY))) {
090: uid.put(KEY, val);
091: // To make it work in NetBeans too:
092: uid.put(val, impl);
093: }
094: // #61147: prevent NB from switching to a different UI later (under GTK):
095: uid
096: .addPropertyChangeListener(pcl = new PropertyChangeListener() {
097: public void propertyChange(PropertyChangeEvent evt) {
098: String name = evt.getPropertyName();
099: Object className = uid.get(KEY);
100: if ((name.equals(KEY) || name
101: .equals("UIDefaults"))
102: && !val.equals(className)
103: && !isQuickFileChooser(className)) {
104: uid.put(KEY, val);
105: }
106: }
107: });
108: }
109:
110: public static void uninstall() {
111: if (isInstalled()) {
112: assert pcl != null;
113: UIDefaults uid = UIManager.getDefaults();
114: uid.removePropertyChangeListener(pcl);
115: pcl = null;
116: String val = originalImpl.getName();
117: uid.put(KEY, val);
118: uid.put(val, originalImpl);
119: originalImpl = null;
120: }
121: }
122:
123: public static boolean isInstalled() {
124: return originalImpl != null;
125: }
126:
127: static Class<? extends FileChooserUI> getOrigChooser() {
128: return originalImpl;
129: }
130:
131: private static boolean isQuickFileChooser(Object className) {
132: return QUICK_CHOOSER_NAME.equals(className);
133: }
134:
135: private static boolean isStandardChooserForced() {
136: return Boolean.getBoolean(FORCE_STANDARD_CHOOSER);
137: }
138:
139: }
|