From 0d5eb7e2d45d52f752e92e56071d9dc9f2bed15e Mon Sep 17 00:00:00 2001 From: Jakub Herkel Date: Fri, 19 Sep 2025 18:51:16 +0200 Subject: [PATCH] [NETBEANS-3878] add delete buton for the SSH Connection Dialog fix problem when connection isn't saved in a list of recent connections --- .../api/util/ConnectionManager.java | 49 ++++++++++++++++++- .../api/util/ConnectionManagerTest.java | 37 +++++++------- .../terminal/action/TerminalSupportImpl.java | 3 ++ .../dlight/terminal/ui/Bundle.properties | 1 + .../dlight/terminal/ui/RemoteInfoDialog.form | 33 ++++++++++--- .../dlight/terminal/ui/RemoteInfoDialog.java | 48 +++++++++++++++--- 6 files changed, 135 insertions(+), 36 deletions(-) diff --git a/ide/dlight.nativeexecution/src/org/netbeans/modules/nativeexecution/api/util/ConnectionManager.java b/ide/dlight.nativeexecution/src/org/netbeans/modules/nativeexecution/api/util/ConnectionManager.java index 5cfc2a7fe4c3..152e3a1e4f27 100644 --- a/ide/dlight.nativeexecution/src/org/netbeans/modules/nativeexecution/api/util/ConnectionManager.java +++ b/ide/dlight.nativeexecution/src/org/netbeans/modules/nativeexecution/api/util/ConnectionManager.java @@ -34,6 +34,7 @@ import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ExecutionException; import java.util.logging.Level; +import java.util.prefs.BackingStoreException; import java.util.prefs.Preferences; import javax.swing.AbstractAction; import javax.swing.Action; @@ -183,6 +184,38 @@ public void removeConnectionListener(ConnectionListener listener) { // No need to lock - use thread-safe collection connectionListeners.remove(listener); } + + /** + * Remove a connection from list of recent connections. Any stored settings will be removed + * @param execEnv environment + */ + public void deleteConnectionFromRecentConnections(ExecutionEnvironment execEnv) { + synchronized (recentConnections) { + recentConnections.remove(execEnv); + forget(execEnv); + storeRecentConnectionsList(true); + } + } + + + /** + * Add a connection to a recent connection list. + * @param execEnv environment + * @return true if a connection was added to a list + */ + public boolean addConnectionToRecentConnections(ExecutionEnvironment execEnv) { + if (execEnv.isLocal()) { + return false; + } + synchronized (recentConnections) { + if (recentConnections.contains(execEnv)) { + return false; + } + recentConnections.add(0, execEnv); + storeRecentConnectionsList(false); + return true; + } + } public List getRecentConnections() { synchronized (recentConnections) { @@ -194,13 +227,24 @@ public List getRecentConnections() { synchronized (recentConnections) { recentConnections.remove(execEnv); recentConnections.add(0, execEnv); - storeRecentConnectionsList(); + storeRecentConnectionsList(false); } } - /*package-local for test purposes*/ void storeRecentConnectionsList() { + /** + * Store recent connection list. + * @param clear true if settings is cleared before stored + */ + /*package-local for test purposes*/ void storeRecentConnectionsList(boolean clear) { Preferences prefs = NbPreferences.forModule(ConnectionManager.class); synchronized (recentConnections) { + if (clear) { + try { + prefs.clear(); + } catch (BackingStoreException ex) { + log.log(Level.WARNING,"Cannot clear ConnectionManager preferences", ex); + } + } for (int i = 0; i < recentConnections.size(); i++) { prefs.put(getConnectoinsHistoryKey(i), ExecutionEnvironmentFactory.toUniqueID(recentConnections.get(i))); } @@ -302,6 +346,7 @@ public boolean isConnectedTo(final ExecutionEnvironment execEnv) { if (checkHostInfo) { return HostInfoUtils.isHostInfoAvailable(execEnv); } else { + updateRecentConnectionsList(execEnv); return true; } } else { diff --git a/ide/dlight.nativeexecution/test/unit/src/org/netbeans/modules/nativeexecution/api/util/ConnectionManagerTest.java b/ide/dlight.nativeexecution/test/unit/src/org/netbeans/modules/nativeexecution/api/util/ConnectionManagerTest.java index 11efbfae93a8..8e17b89ceaec 100644 --- a/ide/dlight.nativeexecution/test/unit/src/org/netbeans/modules/nativeexecution/api/util/ConnectionManagerTest.java +++ b/ide/dlight.nativeexecution/test/unit/src/org/netbeans/modules/nativeexecution/api/util/ConnectionManagerTest.java @@ -22,14 +22,13 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; -import junit.framework.Test; -import org.junit.AfterClass; -import org.junit.BeforeClass; +import org.junit.Test; import org.netbeans.junit.RandomlyFails; import org.netbeans.modules.nativeexecution.ConcurrentTasksSupport; import org.netbeans.modules.nativeexecution.ConcurrentTasksSupport.Counters; import org.netbeans.modules.nativeexecution.test.NativeExecutionBaseTestCase; import org.netbeans.modules.nativeexecution.api.ExecutionEnvironment; +import org.netbeans.modules.nativeexecution.api.ExecutionEnvironmentFactory; import org.netbeans.modules.nativeexecution.test.ForAllEnvironments; import org.netbeans.modules.nativeexecution.test.NativeExecutionBaseTestSuite; import org.netbeans.modules.nativeexecution.test.NativeExecutionTestSupport; @@ -50,26 +49,22 @@ public ConnectionManagerTest(String name, ExecutionEnvironment testExecutionEnvi super(name, testExecutionEnvironment); } - @BeforeClass - public static void setUpClass() throws Exception { - } - - @AfterClass - public static void tearDownClass() throws Exception { - } - - @Override - public void setUp() throws Exception { - } - - @Override - public void tearDown() throws Exception { - } - - public static Test suite() { + public static junit.framework.Test suite() { return new NativeExecutionBaseTestSuite(ConnectionManagerTest.class); } + + @Test + public void testDeleteConnection() throws Exception { + ExecutionEnvironment env = ExecutionEnvironmentFactory.createNew("test","127.0.0.1"); + ConnectionManager.getInstance().updateRecentConnectionsList(env); + List ret = ConnectionManager.getInstance().getRecentConnections(); + assertTrue(!ret.isEmpty()); + ConnectionManager.getInstance().deleteConnectionFromRecentConnections(env); + List ret2 = ConnectionManager.getInstance().getRecentConnections(); + assertTrue(ret2.isEmpty()); + } + @Test public void testGetRecentConnections() throws Exception { String section = "remote.platforms"; ExecutionEnvironment[] envs = NativeExecutionTestSupport.getTestExecutionEnvironmentsFromSection(section); @@ -90,6 +85,7 @@ public void testGetRecentConnections() throws Exception { @RandomlyFails @ForAllEnvironments(section = "remote.platforms") + @Test public void testConnectDisconnect() throws Exception { final ExecutionEnvironment execEnv = getTestExecutionEnvironment(); assert (execEnv != null); @@ -122,6 +118,7 @@ public void testConnectDisconnect() throws Exception { System.out.println(getName() + " finished"); } + @Test public void testGetConnectToAction() throws Exception { final int threadsNum = 10; RcFile rcFile = NativeExecutionTestSupport.getRcFile(); diff --git a/ide/dlight.terminal/src/org/netbeans/modules/dlight/terminal/action/TerminalSupportImpl.java b/ide/dlight.terminal/src/org/netbeans/modules/dlight/terminal/action/TerminalSupportImpl.java index 8f3229d5e3b6..52097b2f28a5 100644 --- a/ide/dlight.terminal/src/org/netbeans/modules/dlight/terminal/action/TerminalSupportImpl.java +++ b/ide/dlight.terminal/src/org/netbeans/modules/dlight/terminal/action/TerminalSupportImpl.java @@ -201,6 +201,9 @@ private void doWork() { // (exception supressed in FetchHostInfoTask.compute) if (!ConnectionManager.getInstance().isConnectedTo(env)) { return; + } else { + // because we can reuse an existing connection we need to try update a recent connection list + ConnectionManager.getInstance().addConnectionToRecentConnections(env); } try { diff --git a/ide/dlight.terminal/src/org/netbeans/modules/dlight/terminal/ui/Bundle.properties b/ide/dlight.terminal/src/org/netbeans/modules/dlight/terminal/ui/Bundle.properties index 4b0204b234fb..a2d4dde34134 100644 --- a/ide/dlight.terminal/src/org/netbeans/modules/dlight/terminal/ui/Bundle.properties +++ b/ide/dlight.terminal/src/org/netbeans/modules/dlight/terminal/ui/Bundle.properties @@ -33,3 +33,4 @@ RemoteInfoDialog.hostField.AccessibleContext.accessibleName=Remote Host Name NewHostInfoTitle=Connection Information: RemoteInfoDialog.btnKnownHosts.text=&Known Hosts RemoteInfoDialog.btnNewHost.text=&Other Host +RemoteInfoDialog.btnDeleteHost.text=Delete diff --git a/ide/dlight.terminal/src/org/netbeans/modules/dlight/terminal/ui/RemoteInfoDialog.form b/ide/dlight.terminal/src/org/netbeans/modules/dlight/terminal/ui/RemoteInfoDialog.form index 949d14210927..9e5cd4403b0f 100644 --- a/ide/dlight.terminal/src/org/netbeans/modules/dlight/terminal/ui/RemoteInfoDialog.form +++ b/ide/dlight.terminal/src/org/netbeans/modules/dlight/terminal/ui/RemoteInfoDialog.form @@ -1,4 +1,4 @@ - +