From 4c176f689d6410c66cd60444617ee5606039307e Mon Sep 17 00:00:00 2001 From: Martin Oberhuber < martin.oberhuber@windriver.com> Date: Mon, 5 May 2008 18:40:36 +0000 Subject: [PATCH] [228577] [rseterminal] Further cleanup --- .../core/TerminalServiceSubSystem.java | 127 ++++++++++-------- .../core/elements/TerminalElement.java | 46 +++++-- 2 files changed, 101 insertions(+), 72 deletions(-) diff --git a/rse/plugins/org.eclipse.rse.subsystems.terminals.core/src/org/eclipse/rse/subsystems/terminals/core/TerminalServiceSubSystem.java b/rse/plugins/org.eclipse.rse.subsystems.terminals.core/src/org/eclipse/rse/subsystems/terminals/core/TerminalServiceSubSystem.java index 795ee208266..371824df9d8 100644 --- a/rse/plugins/org.eclipse.rse.subsystems.terminals.core/src/org/eclipse/rse/subsystems/terminals/core/TerminalServiceSubSystem.java +++ b/rse/plugins/org.eclipse.rse.subsystems.terminals.core/src/org/eclipse/rse/subsystems/terminals/core/TerminalServiceSubSystem.java @@ -1,18 +1,21 @@ /******************************************************************************** - * Copyright (c) 2008 MontaVista Software, Inc. - * This program and the accompanying materials are made available under the terms - * of the Eclipse Public License v1.0 which accompanies this distribution, and is - * available at http://www.eclipse.org/legal/epl-v10.html + * Copyright (c) 2008 MontaVista Software, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Yu-Fen Kuo (MontaVista) - initial API and implementation * Yu-Fen Kuo (MontaVista) - [227572] RSE Terminal doesn't reset the "connected" state when the shell exits * Anna Dushistova (MontaVista) - [228577] [rseterminal] Clean up RSE Terminal impl + * Martin Oberhuber (Wind River) - [228577] [rseterminal] Further cleanup ********************************************************************************/ package org.eclipse.rse.subsystems.terminals.core; import java.util.ArrayList; +import java.util.Iterator; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.rse.core.RSECorePlugin; @@ -28,38 +31,38 @@ import org.eclipse.rse.internal.services.terminals.ITerminalService; import org.eclipse.rse.subsystems.terminals.core.elements.TerminalElement; import org.eclipse.swt.widgets.Display; -public final class TerminalServiceSubSystem extends SubSystem implements +/** + * A Subsystem that has terminal instances as children. + */ +public class TerminalServiceSubSystem extends SubSystem implements ITerminalServiceSubSystem, ICommunicationsListener { - protected ITerminalService _hostService = null; + private ITerminalService _hostService = null; private ArrayList children = new ArrayList(); - public class Refresh implements Runnable { - private TerminalServiceSubSystem _ss; - - public Refresh(TerminalServiceSubSystem ss) { - _ss = ss; - } - - public void run() { - ISystemRegistry registry = RSECorePlugin.getTheSystemRegistry(); - registry.fireEvent(new SystemResourceChangeEvent(_ss, - ISystemResourceChangeEvents.EVENT_REFRESH, _ss)); - } - } - - protected TerminalServiceSubSystem(IHost host, - IConnectorService connectorService) { - super(host, connectorService); - } - + /** + * Constructor. + */ public TerminalServiceSubSystem(IHost host, IConnectorService connectorService, ITerminalService hostService) { super(host, connectorService); _hostService = hostService; } + private void fireAsyncRefresh(final Object target) { + Display.getDefault().asyncExec(new Runnable() { + public void run() { + ISystemRegistry registry = RSECorePlugin.getTheSystemRegistry(); + registry.fireEvent(new SystemResourceChangeEvent(target, ISystemResourceChangeEvents.EVENT_REFRESH, target)); + + } + }); + } + + /** + * Return the Terminal Service associated with this subsystem. + */ public ITerminalService getTerminalService() { return _hostService; } @@ -69,10 +72,12 @@ public final class TerminalServiceSubSystem extends SubSystem implements } public void addChild(TerminalElement element) { - synchronized (children) { - children.add(element); + if (element != null) { + synchronized (children) { + children.add(element); + } + fireAsyncRefresh(this); } - Display.getDefault().asyncExec(new Refresh(this)); } public void removeChild(TerminalElement element) { @@ -80,39 +85,40 @@ public final class TerminalServiceSubSystem extends SubSystem implements synchronized (children) { children.remove(element); } - Display.getDefault().asyncExec(new Refresh(this)); + fireAsyncRefresh(this); } } public void removeChild(String terminalTitle) { removeChild(getChild(terminalTitle)); } + public TerminalElement getChild(String terminalTitle) { - Object[] children = getChildren(); - for (int i = 0, e = children.length; i < e; i++) { - TerminalElement element = (TerminalElement)children[i]; - if (element.getName().equals(terminalTitle)) - return element; - } + synchronized (children) { + for (Iterator it = children.iterator(); it.hasNext();) { + TerminalElement element = (TerminalElement) it.next(); + if (element.getName().equals(terminalTitle)) + return element; + } + } return null; } + public Object[] getChildren() { - Object[] result; synchronized (children) { - result = (Object[]) children - .toArray(new Object[children.size()]); + return children.toArray(); } - return result; } public boolean hasChildren() { synchronized (children) { - if (children.size() > 0) - return true; - return false; + return !children.isEmpty(); } } + /** + * Set the terminal service associated with this subsystem. + */ public void setTerminalService(ITerminalService service) { _hostService = service; } @@ -141,23 +147,27 @@ public final class TerminalServiceSubSystem extends SubSystem implements public boolean isPassiveCommunicationsListener() { return true; } - public void cancelAllTerminals() { - if (children.size() == 0) - return; - Object[] terminals = getChildren(); - for (int i = terminals.length-1; i >= 0; i--) { - TerminalElement element = (TerminalElement)terminals[i]; - try { - removeTerminalElement(element); - } catch (Exception e) { - RSECorePlugin.getDefault().getLogger().logError("Error removing terminal", e); //$NON-NLS-1$ - } - } - synchronized(children){ - children.clear(); - } - Display.getDefault().asyncExec(new Refresh(this)); + /** + * Set the terminal service associated with this subsystem. + */ + public void cancelAllTerminals() { + Object[] terminals; + synchronized (children) { + terminals = getChildren(); + children.clear(); + } + if (terminals.length > 0) { + for (int i = terminals.length - 1; i >= 0; i--) { + TerminalElement element = (TerminalElement) terminals[i]; + try { + removeTerminalElement(element); + } catch (Exception e) { + RSECorePlugin.getDefault().getLogger().logError("Error removing terminal", e); //$NON-NLS-1$ + } + } + fireAsyncRefresh(this); + } } private void removeTerminalElement(TerminalElement element) { @@ -165,6 +175,7 @@ public final class TerminalServiceSubSystem extends SubSystem implements ISystemRegistry registry = RSECorePlugin.getTheSystemRegistry(); registry.fireEvent(new SystemResourceChangeEvent(element, ISystemResourceChangeEvents.EVENT_COMMAND_SHELL_REMOVED, null)); } + public void initializeSubSystem(IProgressMonitor monitor) { super.initializeSubSystem(monitor); getConnectorService().addCommunicationsListener(this); diff --git a/rse/plugins/org.eclipse.rse.subsystems.terminals.core/src/org/eclipse/rse/subsystems/terminals/core/elements/TerminalElement.java b/rse/plugins/org.eclipse.rse.subsystems.terminals.core/src/org/eclipse/rse/subsystems/terminals/core/elements/TerminalElement.java index a90966c24b3..be3d4184eff 100644 --- a/rse/plugins/org.eclipse.rse.subsystems.terminals.core/src/org/eclipse/rse/subsystems/terminals/core/elements/TerminalElement.java +++ b/rse/plugins/org.eclipse.rse.subsystems.terminals.core/src/org/eclipse/rse/subsystems/terminals/core/elements/TerminalElement.java @@ -1,13 +1,15 @@ /******************************************************************************** - * Copyright (c) 2008 MontaVista Software, Inc. - * This program and the accompanying materials are made available under the terms - * of the Eclipse Public License v1.0 which accompanies this distribution, and is - * available at http://www.eclipse.org/legal/epl-v10.html + * Copyright (c) 2008 MontaVista Software, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Yu-Fen Kuo (MontaVista) - initial API and implementation * Anna Dushistova (MontaVista) - initial API and implementation * Yu-Fen Kuo (MontaVista) - [227572] RSE Terminal doesn't reset the "connected" state when the shell exits + * Martin Oberhuber (Wind River) - [228577] [rseterminal] Further cleanup ********************************************************************************/ package org.eclipse.rse.subsystems.terminals.core.elements; @@ -15,34 +17,44 @@ import org.eclipse.rse.core.subsystems.AbstractResource; import org.eclipse.rse.internal.services.terminals.ITerminalShell; import org.eclipse.rse.subsystems.terminals.core.ITerminalServiceSubSystem; +/** + * An element in the RSE Tree that resembles a Terminal connection. + */ public class TerminalElement extends AbstractResource { private String name; private ITerminalShell terminalShell; + /** + * Constructor. + */ public TerminalElement(String name, ITerminalServiceSubSystem terminalServiceSubSystem) { super(terminalServiceSubSystem); this.name = name; } - public String getName() { + /** + * Return the name of this element, which will also be used as the label in + * the tree. + */ + public final String getName() { return name; } - public String toString() { return getName(); } public boolean equals(Object obj) { - if (obj instanceof TerminalElement) { - if (obj == this) - return true; - return name.equals(((TerminalElement) obj).getName()) - && terminalShell == ((TerminalElement) obj) - .getTerminalShell(); - } - return super.equals(obj); + if (obj == this) + return true; + if (!(obj instanceof TerminalElement)) + return false; + TerminalElement other = (TerminalElement) obj; + return name.equals(other.getName()) + && getSubSystem().equals(other.getSubSystem()) + && (terminalShell == null ? other.getTerminalShell() == null + : terminalShell.equals(other.getTerminalShell())); } public int hashCode() { @@ -51,10 +63,16 @@ public class TerminalElement extends AbstractResource { return name.hashCode() ; } + /** + * Return the back-end connection of this terminal instance. + */ public ITerminalShell getTerminalShell() { return terminalShell; } + /** + * Set the back-end connection of this terminal instance. + */ public void setTerminalShell(ITerminalShell terminalShell) { this.terminalShell = terminalShell; }