自定义eclipse视图和运行程序之间的java通信?

ha5z0ras  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(354)

我在做一个项目,是某种三维可视化的模拟。这个可视化是一个可以运行的独立eclipse插件。问题是,gui被想象成一个新的eclipse透视图,带有自定义视图,在运行时,当可视化正在运行时,在eclipse视图中,在我们自己的透视图中,可视化中的对象属性值需要显示给用户,这样他就可以看到它们。
我怎样才能做到这一点?它应该类似于某种调试模式,但用户可以在对象之间切换,并在可视化运行时检查它们的属性值。你知道怎么做到吗?有什么方法可以将事件发送到eclipse视图或类似的东西吗?
谢谢您

ukqbszuj

ukqbszuj1#

是的,绝对可以让视图侦听其他对象上的更改并进行更新。如果用户的选择是相关的,那么您可以使用标准的eclipse方法来选择对象(您是否希望您的监视视图根据客户在可视化中选择的内容来观看不同的内容?)。更重要的是,您可以设计一个支持侦听器的模型,以便其他对象(如视图)可以侦听可视化模型中的更改并自行更新这些更改。
在视觉上倾听变化。视图可以连接到可视化模型中的任何侦听器接口。然后,当他们得到事件时,他们可以更新曾经更改过的属性并更新它们的显示。
在我的例子中,我有一个模型,它表示牌组中的牌列表(顺便说一句,不是普通的扑克牌,而是魔法收集牌)。这个 ModelElement ,这是我在牌组中使用的所有不同插槽(例如卡片、注解等)的基类 java.beans.PropertyChangeSupport 要帮助实现对侦听属性更改()的支持,请执行以下操作:

  1. public synchronized void addPropertyChangeListener (PropertyChangeListener listener) {
  2. if (listener == null) {
  3. throw new IllegalArgumentException ("Property change listener cannot be null."); //$NON-NLS-1$
  4. }
  5. myPropertyChangeDelegate.addPropertyChangeListener (listener);
  6. }
  7. public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
  8. if (listener != null) {
  9. myPropertyChangeDelegate.removePropertyChangeListener (listener);
  10. }
  11. }
  12. protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
  13. if (myPropertyChangeDelegate.hasListeners (propertyName)) {
  14. myPropertyChangeDelegate.firePropertyChange (propertyName, oldValue, newValue);
  15. }
  16. }
  17. /**Delegate used to implement property change support. */
  18. private transient PropertyChangeSupport myPropertyChangeDelegate;

这样我的视图就可以连接并监听当前deck编辑器模型中的元素。例如,下面是outline视图的deck元素侦听器(称为 DeckSlotTreeEditPart )表示当前牌组中大纲视图的卡片树、注解等中的元素:

  1. public class DeckSlotTreeEditPart extends AbstractTreeEditPart implements PropertyChangeListener {
  2. ... other code snipped
  3. public void activate () {
  4. if (!isActive ()) {
  5. super.activate ();
  6. getDeckSlot ().addPropertyChangeListener (this);
  7. }
  8. }
  9. @Override
  10. public void deactivate () {
  11. if (isActive ()) {
  12. super.deactivate ();
  13. getDeckSlot ().removePropertyChangeListener (this);
  14. }
  15. }
  16. public void propertyChange (PropertyChangeEvent evt) {
  17. refreshVisuals ();
  18. }
  19. protected DeckSlot getDeckSlot () {
  20. return (DeckSlot)getModel ();
  21. }

在您的例子中,您可以让您的视图钩住您想要的可视化上的任何侦听接口,并在来自这些侦听器的事件触发时自行更新它们。你可以用 PropertyChangeSupport 或者设计自己的侦听器接口(如果还没有)。
听选择的变化。下面是我的一个应用程序中的一个查看器的代码片段。此视图侦听编辑器或其他选择提供程序中的选择更改,并更新自身以显示新选择的“card”对象的属性(请参阅eclipse.org帮助以获取iselectionlistener):

  1. public class CardInfo extends ViewPart implements ISelectionListener, ICardHistoryDelegate,
  2. IPinViewDelegate, IToggleSearchBarDelegate, ICardSearchListener {
  3. ... other methods snipped
  4. public void createPartControl (Composite parent) {
  5. ... rest of method that actually creates the view components snipped
  6. // Hook up to listen to selection changes
  7. getSite ().getWorkbenchWindow ().getSelectionService ().addSelectionListener (this);
  8. }
  9. public void selectionChanged (IWorkbenchPart part, ISelection selection) {
  10. if (selection.isEmpty() || !(selection instanceof IStructuredSelection)) {
  11. // Do not clear the view even for empty selections or selections of another
  12. // type - just do nothing. That way the last card looked at will be visible no
  13. // matter what else happens
  14. return;
  15. }
  16. // Display the first selected element
  17. IStructuredSelection structuredSel = (IStructuredSelection)selection;
  18. Object firstSelectedObject = structuredSel.getFirstElement ();
  19. // Get the ICard interface from the selection - using its IAdaptable interface
  20. if (firstSelectedObject == null || !(firstSelectedObject instanceof IAdaptable))
  21. return; // no work to do
  22. ICard selectedCard = (ICard)((IAdaptable)firstSelectedObject).getAdapter(ICard.class);
  23. if (selectedCard == null)
  24. return; // no work to do
  25. ... rest of method that actually does something with new selection snipped

我的编辑器使用gef,它为eclipse工作台提供了一个选择提供者,我的视图可以监听它。
我希望这有帮助。
伊恩

展开查看全部

相关问题