--- apichanges.xml +++ apichanges.xml @@ -108,6 +108,25 @@ + Added getVisibilityQueryImplementation() method to VersioningSystem + + + + + + Some Versioning systems provide a VisibilityQueryImplementation. This API change has two purposes: + 1. There will be only one registered VQI (Versioning manager) that will delegate the query to the appropriate + versioning system which will speed things up. + 2. It makes it clear and explicit that implementors of a VersioningSystem might want to provide the query. + It would be possible for them to just implement and register their own VisibilityQueryImplementation but + they could easily forget to do that. + + + + + + + Added getCollocationQueryImplementation() method to VersioningSystem --- src/org/netbeans/modules/versioning/VcsVisibilityQueryImplementation.java +++ src/org/netbeans/modules/versioning/VcsVisibilityQueryImplementation.java @@ -0,0 +1,114 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun + * Microsystems, Inc. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ +package org.netbeans.modules.versioning; + +import javax.swing.event.ChangeListener; +import org.netbeans.modules.versioning.spi.VersioningSystem; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import javax.swing.event.ChangeEvent; +import org.netbeans.modules.versioning.spi.VCSVisibilityQuery; +import org.netbeans.spi.queries.VisibilityQueryImplementation2; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; + +/** + * Delegates the work to the owner of files in query. + * + * @author Tomas Stupka + */ +@org.openide.util.lookup.ServiceProvider(service=org.netbeans.spi.queries.VisibilityQueryImplementation.class) +public class VcsVisibilityQueryImplementation implements VisibilityQueryImplementation2 { + + private List listeners = new ArrayList(); + private static VcsVisibilityQueryImplementation instance; + + public VcsVisibilityQueryImplementation() { + instance = this; + } + + public static VcsVisibilityQueryImplementation getInstance() { + return instance; + } + + public boolean isVisible(File file) { + VersioningSystem system = VersioningManager.getInstance().getOwner(file); + if(system == null) { + return true; + } + VCSVisibilityQuery vqi = system.getVisibilityQuery(); + return vqi == null ? true : vqi.isVisible(file); + } + + public boolean isVisible(FileObject fileObject) { + File file = FileUtil.toFile(fileObject); + if(file == null) { + return true; + } + return isVisible(file); + } + + public synchronized void addChangeListener(ChangeListener l) { + ArrayList newList = new ArrayList(listeners); + newList.add(l); + listeners = newList; + } + + public synchronized void removeChangeListener(ChangeListener l) { + ArrayList newList = new ArrayList(listeners); + newList.remove(l); + listeners = newList; + } + + public void fireVisibilityChanged() { + ChangeListener[] ls; + synchronized(this) { + ls = listeners.toArray(new ChangeListener[listeners.size()]); + } + ChangeEvent event = new ChangeEvent(this); + for (ChangeListener l : ls) { + l.stateChanged(event); + } + } + +} --- src/org/netbeans/modules/versioning/spi/VCSVisibilityQuery.java +++ src/org/netbeans/modules/versioning/spi/VCSVisibilityQuery.java @@ -0,0 +1,73 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun + * Microsystems, Inc. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ +package org.netbeans.modules.versioning.spi; + + +import java.io.File; +import org.netbeans.modules.versioning.VcsVisibilityQueryImplementation; +import org.netbeans.spi.queries.VisibilityQueryImplementation2; + +/** + * Provides the visibility servis according to {@link VisibilityQueryImplementation2} + * for a particular VersioningSystem + * + * @author Tomas Stupka + */ +public abstract class VCSVisibilityQuery { + + /** + * Check whether a file is recommended to be visible. + * @param file a file to considered + * @return true if it is recommended to display this file + */ + public abstract boolean isVisible(File file); + + /** + * Notify a visibility change + */ + protected final void fireVisibilityChanged() { + VcsVisibilityQueryImplementation vq = VcsVisibilityQueryImplementation.getInstance(); + if(vq != null) { + // was touched from outside - lets fire the change + vq.fireVisibilityChanged(); + } + } +} --- src/org/netbeans/modules/versioning/spi/VersioningSystem.java +++ src/org/netbeans/modules/versioning/spi/VersioningSystem.java @@ -47,6 +47,7 @@ import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; import java.util.*; +import org.netbeans.spi.queries.VisibilityQueryImplementation2; /** * Base class for a versioning system that integrates into IDE. @@ -180,6 +181,16 @@ } /** + * Retrieves a VCSVisibilityQuery implementation if this versioning system provides one. + * + * @return VCSVisibilityQuery a VCSVisibilityQuery instance or null if the system does not provide the service + * @since 1.10 + */ + public VCSVisibilityQuery getVisibilityQuery() { + return null; + } + + /** * Adds a listener for change events. * * @param listener a PropertyChangeListener --- test/unit/src/org/netbeans/modules/versioning/spi/VCSVisibilityQueryTest.java +++ test/unit/src/org/netbeans/modules/versioning/spi/VCSVisibilityQueryTest.java @@ -0,0 +1,87 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun + * Microsystems, Inc. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ +package org.netbeans.modules.versioning.spi; + +import java.io.IOException; +import org.openide.filesystems.FileUtil; +import org.openide.filesystems.FileStateInvalidException; +import org.openide.filesystems.FileObject; + +import java.io.File; +import org.netbeans.api.queries.VisibilityQuery; +import org.netbeans.junit.NbTestCase; +import org.netbeans.modules.versioning.spi.testvcs.TestVCS; +import org.netbeans.modules.versioning.spi.testvcs.TestVCSVisibilityQuery; + +/** + * Versioning SPI unit tests of VCSVisibilityQuery. + * + * @author Tomas Stupka + */ +public class VCSVisibilityQueryTest extends NbTestCase { + + + public VCSVisibilityQueryTest(String testName) { + super(testName); + } + + protected void setUp() throws Exception { + System.setProperty("netbeans.user", System.getProperty("data.root.dir") + "/userdir"); + super.setUp(); + } + + public void testVQ() throws FileStateInvalidException, IOException { + File folder = new File(getWorkDir(), TestVCS.VERSIONED_FOLDER_SUFFIX); + folder.mkdirs(); + + File visible = new File(folder, "this-file-is-visible"); + visible.createNewFile(); + FileObject visibleFO = FileUtil.toFileObject(visible); + assertTrue(VisibilityQuery.getDefault().isVisible(visible)); + assertTrue(VisibilityQuery.getDefault().isVisible(visibleFO)); + + File invisible = new File(folder, "this-file-is-" + TestVCSVisibilityQuery.INVISIBLE_FILE_SUFFIX); + invisible.createNewFile(); + FileObject invisibleFO = FileUtil.toFileObject(invisible); + assertFalse(VisibilityQuery.getDefault().isVisible(invisible)); + assertFalse(VisibilityQuery.getDefault().isVisible(invisibleFO)); + } +} --- test/unit/src/org/netbeans/modules/versioning/spi/testvcs/TestVCS.java +++ test/unit/src/org/netbeans/modules/versioning/spi/testvcs/TestVCS.java @@ -45,6 +45,7 @@ import org.netbeans.modules.versioning.spi.VCSAnnotator; import java.io.File; +import org.netbeans.modules.versioning.spi.VCSVisibilityQuery; /** * Test versioning system. @@ -57,7 +58,10 @@ private static TestVCS instance; private VCSInterceptor interceptor; private VCSAnnotator annotator; + private VCSVisibilityQuery vq; + public static final String VERSIONED_FOLDER_SUFFIX = "-test-versioned"; + public static TestVCS getInstance() { return instance; } @@ -66,12 +70,13 @@ instance = this; interceptor = new TestVCSInterceptor(); annotator = new TestVCSAnnotator(); + vq = new TestVCSVisibilityQuery(); } public File getTopmostManagedAncestor(File file) { File topmost = null; for (; file != null; file = file.getParentFile()) { - if (file.getName().endsWith("-test-versioned")) { + if (file.getName().endsWith(VERSIONED_FOLDER_SUFFIX)) { topmost = file; } } @@ -85,4 +90,10 @@ public VCSAnnotator getVCSAnnotator() { return annotator; } + + @Override + public VCSVisibilityQuery getVisibilityQuery() { + return vq; } + +} --- test/unit/src/org/netbeans/modules/versioning/spi/testvcs/TestVCSVisibilityQuery.java +++ test/unit/src/org/netbeans/modules/versioning/spi/testvcs/TestVCSVisibilityQuery.java @@ -0,0 +1,58 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2009 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2009 Sun Microsystems, Inc. + */ + +package org.netbeans.modules.versioning.spi.testvcs; + +import java.io.File; +import org.netbeans.modules.versioning.spi.VCSVisibilityQuery; + +/** + * + * @author Tomas Stupka + */ +public class TestVCSVisibilityQuery extends VCSVisibilityQuery { + + public static final String INVISIBLE_FILE_SUFFIX = "invisible"; + + @Override + public boolean isVisible(File file) { + return !file.getName().endsWith(INVISIBLE_FILE_SUFFIX); + } + +}