Programmatically split an editor area to show two editors side by side

本文介绍了一种在Eclipse RCP应用中通过编程方式拆分编辑器区域的方法。当有两个编辑器打开时,可以将当前活动编辑器置于新的工作簿中,并与另一个编辑器并排显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

.Last time a workmate of mine asked me if it is possible, to programmatically split the editor area of an eclipse RCP application.

We all know that if you have two editors opened in the workbench, you can drag one of the editors and drop it in one of the regions of the editor area (bottom, top, left or right) so that both editors are side by side.

I started to analyze the eclipse code and look for a public API that I can use to achieve this task. I didn’t find anything. My first idea was to simulate the Drag&Drop behavior, but it came out to be a very challenging task.

So I’ve decided to use some of the API calls, which the eclipse team discourages to use. And it works. I bundled my code into a plug-in. Maybe someone will find this useful.


package spliteditorarea.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.EditorSashContainer;
import org.eclipse.ui.internal.EditorStack;
import org.eclipse.ui.internal.ILayoutContainer;
import org.eclipse.ui.internal.LayoutPart;
import org.eclipse.ui.internal.PartPane;
import org.eclipse.ui.internal.PartSashContainer;
import org.eclipse.ui.internal.PartSite;
import org.eclipse.ui.internal.PartStack;
import org.eclipse.ui.internal.WorkbenchPage;

/**
 * Our sample action implements workbench action delegate. The action proxy will
 * be created by the workbench and shown in the UI. When the user tries to use
 * the action, this delegate will be created and execution will be delegated to
 * it.
 * 
 * @see IWorkbenchWindowActionDelegate
 */
@SuppressWarnings("restriction")
public class SplitEditorAreaAction implements IWorkbenchWindowActionDelegate {
	private IWorkbenchWindow window;

	/**
	 * The constructor.
	 */
	public SplitEditorAreaAction() {
	}

	/**
	 * The action has been activated. The argument of the method represents the
	 * 'real' action sitting in the workbench UI.
	 * 
	 * @see IWorkbenchWindowActionDelegate#run
	 */
	public void run(IAction action) {
		splitEditorArea();
	}

	/**
	 * Split the editor area if there is at least two editor in it.
	 */
	private void splitEditorArea() {
		IWorkbenchPage workbenchPage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		IWorkbenchPart part = workbenchPage.getActivePart();
		PartPane partPane = ((PartSite) part.getSite()).getPane();
		LayoutPart layoutPart = partPane.getPart();

		IEditorReference[] editorReferences = workbenchPage.getEditorReferences();
		// Do it only if we have more that one editor
		if (editorReferences.length > 1) {
			// Get PartPane that correspond to the active editor
			PartPane currentEditorPartPane = ((PartSite) workbenchPage.getActiveEditor().getSite()).getPane();
			EditorSashContainer editorSashContainer = null;
			ILayoutContainer rootLayoutContainer = layoutPart.getContainer();
			if (rootLayoutContainer instanceof LayoutPart) {
				ILayoutContainer editorSashLayoutContainer = ((LayoutPart) rootLayoutContainer).getContainer();
				if (editorSashLayoutContainer instanceof EditorSashContainer) {
					editorSashContainer = ((EditorSashContainer) editorSashLayoutContainer);
				}
			}
			/*
			 * Create a new part stack (i.e. a workbook) to home the currentEditorPartPane
			 * which hold the active editor
			 * */
			PartStack newPart = createStack(editorSashContainer);
			editorSashContainer.stack(currentEditorPartPane, newPart);
			if (rootLayoutContainer instanceof LayoutPart) {
				ILayoutContainer cont = ((LayoutPart) rootLayoutContainer).getContainer();
				if (cont instanceof PartSashContainer) {
					// "Split" the editor area by adding the new part
					((PartSashContainer) cont).add(newPart);
				}
			}
		}
	}

	/**
	 * A method to create a part stack container (a new workbook)
	 * 
	 * @param editorSashContainer the <code>EditorSashContainer</code> to set for the returned <code>PartStack</code>
	 * @return a new part stack container
	 */
	private PartStack createStack(EditorSashContainer editorSashContainer) {
		WorkbenchPage workbenchPage = (WorkbenchPage) PlatformUI.getWorkbench().getActiveWorkbenchWindow()
				.getActivePage();
		EditorStack newWorkbook = EditorStack.newEditorWorkbook(editorSashContainer, workbenchPage);
		return newWorkbook;
	}

	/**
	 * Selection in the workbench has been changed. We can change the state of
	 * the 'real' action here if we want, but this can only happen after the
	 * delegate has been created.
	 * 
	 * @see IWorkbenchWindowActionDelegate#selectionChanged
	 */
	public void selectionChanged(IAction action, ISelection selection) {
	}

	/**
	 * We can use this method to dispose of any system resources we previously
	 * allocated.
	 * 
	 * @see IWorkbenchWindowActionDelegate#dispose
	 */
	public void dispose() {
	}

	/**
	 * We will cache window object in order to be able to provide parent shell
	 * for the message dialog.
	 * 
	 * @see IWorkbenchWindowActionDelegate#init
	 */
	public void init(IWorkbenchWindow window) {
		this.window = window;
	}
}


内容概要:本文深入探讨了软件项目配置管理在汽车开发领域的应用及其重要性,强调配置管理不仅是版本控制,更是涵盖标识、追溯、结构化等多方面的深度管控。文章通过对比机械产品和软件产品的标签管理,揭示了软件配置管理的独特挑战。配置管理构建了一个“网”状体系,确保软件产品在复杂多变的开发环境中保持稳定和有序。文中还讨论了配置管理在实际工作中的困境,如命名混乱、文档更新不及时、发布流程冗长等问题,并提出了通过结构可视化、信息同源化、痕迹自动化和基线灵活化等手段优化配置管理的具体方法。 适合人群:具备一定软件开发和项目管理经验的工程师及项目经理,尤其是从事汽车电子软件开发的相关人员。 使用场景及目标:①理解配置管理在汽车软件项目中的核心作用;②学习如何通过工具链(如Polarion、JIRA、飞书等)优化配置管理流程;③掌握结构可视化、信息同源化、痕迹自动化和基线灵活化等关键技术手段,提升项目管理水平。 其他说明:配置管理不仅是技术问题,更涉及到项目管理和团队协作。文中强调了工具链的应用和优化的重要性,但同时也指出,工具本身并不能解决所有问题,关键在于如何合理使用工具并不断优化管理流程。文章呼吁读者成为长期主义者,相信时间的力量,持续改进配置管理工作。
### AWS VPC Side-to-Side Connection Overview Amazon Virtual Private Cloud (VPC) allows users to create isolated network environments within the AWS cloud. To enable communication between two separate VPCs, a feature known as **VPC Peering** can be utilized. This method facilitates direct routing of traffic between two VPCs without requiring gateways, hardware appliances, or even traversing the public internet[^1]. #### Key Features of VPC Peering - It supports full mesh connectivity where each instance in one VPC can communicate with any instance in the peered VPC using private IP addresses. - The connection does not incur additional charges beyond standard data transfer costs. To implement this functionality effectively: #### Steps for Implementation Although step-based instructions are prohibited here, it is essential to highlight that configuring side-to-side connections involves setting up specific rules and configurations such as route tables adjustments, security group settings, and ensuring DNS resolution compatibility across both networks. Here’s an example snippet demonstrating how you might adjust your Route Table entries programmatically via Python SDK (`boto3`): ```python import boto3 def configure_vpc_peering_route(vpc_id, peer_connection_id, cidr_block): ec2_client = boto3.client('ec2') response = ec2_client.create_tags( Resources=[peer_connection_id], Tags=[ { 'Key': 'Name', 'Value': f'Peering-{vpc_id}' } ] ) # Add CIDR block into main route table associated with given VPC ID ec2_client.create_route( DestinationCidrBlock=cidr_block, VpcPeeringConnectionId=peer_connection_id, RouteTableId=vpc_id # Assuming vpc_id refers directly to its default RT id here logically ) configure_vpc_peering_route('vpc-abcde', 'pcx-fghij', '192.168.0.0/16') ``` This script uses `boto3`, which interacts with AWS services through APIs allowing automation over manual console operations when managing resources like routes inside specified VPC contexts. Additionally, tools built around microservices architectures may also benefit from integrated solutions provided by platforms similar to Nacos mentioned earlier; however, they serve different purposes compared to networking setups discussed above[^2].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值