From d17a4593104c69002ceed2c7c40ff975753dcce8 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Wed, 7 Oct 2026 09:51:21 +0200 Subject: [PATCH] Setting none group always switches to empty list of projects --- .../modules/project/ui/groups/Group.java | 6 +- .../modules/project/ui/groups/GroupTest.java | 73 +++++++++++++++++++ 2 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 ide/projectui/test/unit/src/org/netbeans/modules/project/ui/groups/GroupTest.java diff --git a/ide/projectui/src/org/netbeans/modules/project/ui/groups/Group.java b/ide/projectui/src/org/netbeans/modules/project/ui/groups/Group.java index 42d7b6d10d7b..484bdd425cff 100644 --- a/ide/projectui/src/org/netbeans/modules/project/ui/groups/Group.java +++ b/ide/projectui/src/org/netbeans/modules/project/ui/groups/Group.java @@ -184,7 +184,7 @@ public static void setActiveGroup(Group nue, boolean isNewGroup) { } } else { OpenProjectListSettings settings = OpenProjectListSettings.getInstance(); - settings.setOpenProjectsURLsAsStrings(nue != null ? nue.projectPaths() : getProjectPathsByPreferences(noneGroupPref)); + settings.setOpenProjectsURLsAsStrings(nue != null ? nue.projectPaths() : Collections.emptyList()); settings.setMainProjectURL(nue != null ? nue.prefs().get(KEY_MAIN, null) : null); WindowManager.getDefault().addWindowSystemListener(new WindowSystemListener() { @@ -476,6 +476,7 @@ protected static Project projectForPath(String path) { public Set getProjects() { return getProjects(null, 0, 0); } + private Set getProjects(ProgressHandle h, int start, int end) { if (h != null) { h.progress("", start); @@ -577,8 +578,7 @@ static void open(final Group g, String oldGroupName, boolean isNewGroup, Prefere oldOpen.add(real != null ? real : open); } - //TODO switching to no group always clears the opened project list. - Set newOpen = g != null ? g.getProjects(h, 10, 100) : getProjectsByPreferences(noneGroupPref, h, 10, 100); + Set newOpen = g != null ? g.getProjects(h, 10, 100) : Collections.emptySet(); final Set toClose = new HashSet(oldOpen); toClose.removeAll(newOpen); final Set toOpen = new HashSet(newOpen); diff --git a/ide/projectui/test/unit/src/org/netbeans/modules/project/ui/groups/GroupTest.java b/ide/projectui/test/unit/src/org/netbeans/modules/project/ui/groups/GroupTest.java new file mode 100644 index 000000000000..cf65c183243f --- /dev/null +++ b/ide/projectui/test/unit/src/org/netbeans/modules/project/ui/groups/GroupTest.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.project.ui.groups; + +import org.netbeans.api.project.Project; +import org.netbeans.api.project.ProjectManager; +import org.netbeans.api.project.ui.OpenProjects; +import org.netbeans.junit.MockServices; +import org.netbeans.junit.NbTestCase; +import org.netbeans.modules.project.ui.actions.TestSupport; +import org.openide.filesystems.FileUtil; + +public class GroupTest extends NbTestCase { + public GroupTest(String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + clearWorkDir(); + MockServices.setServices(TestSupport.TestProjectFactory.class); + Group.setActiveGroup(null, false); + assertEquals("No group is active", null, OpenProjects.getDefault().getActiveProjectGroup()); + } + + public void testSwitchingToAgroup() { + var g = AdHocGroup.create("adHoc1", false); + Group.setActiveGroup(g, true); + var active = OpenProjects.getDefault().getActiveProjectGroup().getName(); + assertEquals("Group was switched", g.getName(), active); + } + + public void testSwitchingBackToNoGroupClearsOpenedProjects() throws Exception { + var root = FileUtil.toFileObject(getWorkDir()); + var fo1 = TestSupport.createTestProject(root, "prj1"); + var prj1 = ProjectManager.getDefault().findProject(fo1); + var fo2 = TestSupport.createTestProject(root, "prj2"); + var prj2 = ProjectManager.getDefault().findProject(fo2); + + OpenProjects.getDefault().open(new Project[] { prj1, prj2 }, false); + + var twoOrig = OpenProjects.getDefault().openProjects().get(); + assertEquals(2, twoOrig.length); + assertEquals("1st", twoOrig[0], prj1); + assertEquals("2nd", twoOrig[1], prj2); + + var g = AdHocGroup.create("empty", false); + Group.setActiveGroup(g, true); + var none = OpenProjects.getDefault().openProjects().get(); + assertEquals("Now there are no projects open", 0, none.length); + + Group.setActiveGroup(null, false); + var backToNoGroup = OpenProjects.getDefault().openProjects().get(); + assertEquals("Fresh empty group is opened", 0, backToNoGroup.length); + } + +}