gpt4 book ai didi

java - 自定义eclipse View 和正在运行的程序之间的通信?

转载 作者:行者123 更新时间:2023-11-30 05:15:15 27 4
gpt4 key购买 nike

我正在开发一个项目,该项目是某种模拟的 3D 可视化。该可视化是一个可以运行的单独的 eclipse 插件。问题在于,GUI 被想象为具有自定义 View 的新 Eclipse 透视图,在运行时,当可视化正在运行时,在我们自己的透视图中的 Eclipse View 中,需要向用户显示可视化中对象的属性值,这样他就可以观看它们。

我怎样才能实现这一目标?它应该类似于某种 Debug模式,但用户可以在可视化运行时在对象之间切换并检查其属性值。您知道如何实现这一目标吗?有什么方法可以将事件发送到 Eclipse View 或类似的东西吗?

谢谢

最佳答案

是的,您绝对可以让 View 监听其他对象的更改并更新。如果用户选择相关,您可以使用选择对象的标准 Eclipse 方式(您是否希望监控 View 根据客户在可视化中选择的内容来观看不同的内容?)。更重要的是,您可以设计一个支持监听器的模型,以便其他对象(例如您的 View )可以监听可视化模型中的更改并自行更新。

聆听可视化中的变化。您的 View 可以连接到可视化模型中的任何监听器接口(interface)。然后,当他们收到事件时,他们可以更新已更改的属性并更新其显示。

就我而言,我有一个模型,代表一副牌中的纸牌列表(顺便说一句,不是普通的扑克牌,Magic the Gathering 牌)。 ModelElement 是我在牌组中所有不同插槽(例如卡片、注释等)的基类,它使用 java.beans.PropertyChangeSupport 来帮助实现对监听的支持属性更改():

public synchronized void addPropertyChangeListener (PropertyChangeListener listener)  {
if (listener == null) {
throw new IllegalArgumentException ("Property change listener cannot be null."); //$NON-NLS-1$
}

myPropertyChangeDelegate.addPropertyChangeListener (listener);
}


public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
if (listener != null) {
myPropertyChangeDelegate.removePropertyChangeListener (listener);
}
}


protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
if (myPropertyChangeDelegate.hasListeners (propertyName)) {
myPropertyChangeDelegate.firePropertyChange (propertyName, oldValue, newValue);
}
}


/** Delegate used to implement property change support. */
private transient PropertyChangeSupport myPropertyChangeDelegate;

这样我的 View 就可以连接并监听当前牌组编辑器模型中的元素。例如,这里是来自大纲 View 的牌组元素监听器(称为 DeckSlotTreeEditPart)的一些代码,它表示当前牌组中大纲 View 的卡片树、注释等中的元素:

public class DeckSlotTreeEditPart extends AbstractTreeEditPart implements PropertyChangeListener  {

... other code snipped

public void activate () {
if (!isActive ()) {
super.activate ();
getDeckSlot ().addPropertyChangeListener (this);
}
}


@Override
public void deactivate () {
if (isActive ()) {
super.deactivate ();
getDeckSlot ().removePropertyChangeListener (this);
}
}


public void propertyChange (PropertyChangeEvent evt) {
refreshVisuals ();
}


protected DeckSlot getDeckSlot () {
return (DeckSlot)getModel ();
}

在您的情况下,您可以将 View Hook 到您想要的可视化上的任何监听界面,并在这些监听器触发的事件时自行更新它们。您可以使用 PropertyChangeSupport 或设计自己的监听器接口(interface)(如果尚不存在)。

监听选择变化。这是我的一个应用程序中一位查看者的代码片段。此 View 监听编辑器或其他选择提供程序中的选择更改,并更新自身以显示新选择的“卡片”对象的属性(请参阅 eclipse.org help for ISelectionListener ):

public class CardInfo extends ViewPart implements ISelectionListener, ICardHistoryDelegate, 
IPinViewDelegate, IToggleSearchBarDelegate, ICardSearchListener {

... other methods snipped

public void createPartControl (Composite parent) {
... rest of method that actually creates the view components snipped

// Hook up to listen to selection changes
getSite ().getWorkbenchWindow ().getSelectionService ().addSelectionListener (this);
}

public void selectionChanged (IWorkbenchPart part, ISelection selection) {

if (selection.isEmpty() || !(selection instanceof IStructuredSelection)) {
// Do not clear the view even for empty selections or selections of another
// type - just do nothing. That way the last card looked at will be visible no
// matter what else happens
return;
}

// Display the first selected element
IStructuredSelection structuredSel = (IStructuredSelection)selection;
Object firstSelectedObject = structuredSel.getFirstElement ();

// Get the ICard interface from the selection - using its IAdaptable interface
if (firstSelectedObject == null || !(firstSelectedObject instanceof IAdaptable))
return; // no work to do
ICard selectedCard = (ICard)((IAdaptable)firstSelectedObject).getAdapter(ICard.class);
if (selectedCard == null)
return; // no work to do

... rest of method that actually does something with new selection snipped

我的编辑器使用 GEF,它为我的 View 可以监听的 Eclipse 工作台提供了一个选择提供程序。

希望对您有所帮助。

伊恩

关于java - 自定义eclipse View 和正在运行的程序之间的通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1679281/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com