2016-03-01 48 views
2

我有一种感觉,即将要在这里提出一些基本的/基本的东西,但对于技术而言,我是新手,并没有找到“正确”的方法做事情就这样走了。在简单的Eclipse RAP Web应用程序中导航

背景

我目前正在开发一个Web应用程序排序的业余爱好项目。我决定使用Eclipse RAP(RWT Standalone)作为UI。应用程序将部署在Eclipse Virgo服务器(Tomcat变体)上,因为我打算利用OSGI框架(使某些业务逻辑更容易实现,但这不在此问题的范围内)

问题

应用程序将有多个部分,只有这些人应该在同一时间可见。虽然设置RAP相对容易,但我无法理解如何在应用程序的不同部分之间设置导航。

我的意思是,例如在应用程序标题中有诸如“数据”和“设置”之类的按钮,并且这些按钮下方的内容根据用户点击哪个按钮而改变。

目前的解决方案

目前,我想出了一个最简单的解决方法是

  1. 使用活动Shell为可见部分
  2. 创建副无形壳牌作为父父对于所有非活动部分
  3. 如果可见部分更改,则使用currentlyVisibleSection.setParent(backgroundShell)将当前可见部分移动到背景壳牌和newVisibleSection.setParent(activeShell),使选定的部分可见

这个想法被下一个剥离下来的例子所示:

public class TestUI extends AbstractEntryPoint { 
    private Composite visibleSection; 
    private Shell backgroundShell; 

    public void createContents(Composite parent) { 
     backgroundShell = new Shell(); 

     //Implementation comes from other OSGI bundles 
     Composite dataSection = createDataSection(parent); 
     Composite settingsSection = createSettingsSection(backgroundShell); 
     visibleSection = dataSection; 

     Button dataButton = new Button(parent, SWT.PUSH); 
     dataButton.setText("Data"); 
     dataButton.addSelectionListener(new SelectionAdapter() { 
      public void widgetSelected(SelectionEvent event) { 
       if (visibleSection!=dataSection) { 
        visibleSection.setParent(backgroundShell); 
        dataSection.setParent(parent); 
        visibleSection=dataSection; 
        parent.layout(); 
       } 
      } 
     }); 

     Button settingsButton = new Button(parent, SWT.PUSH); 
     settingsButton.setText("Data"); 
     settingsButton.addSelectionListener(new SelectionAdapter() { 
      public void widgetSelected(SelectionEvent event) { 
       if (visibleSection!=settingsSection) { 
        visibleSection.setParent(backgroundShell); 
        settingsSection.setParent(parent); 
        visibleSection=settingsSection; 
        parent.layout(); 
       } 
      } 
     }); 
    } 
} 

很显然,我已经剥夺了所有的布局和样式代码,pluss会仍然需要一些工作才能让最终用户体面。它确实要求我手动跟踪所有不同的部分,创建并管理它们,但这对我来说是可以接受的。总体而言,这非常符合我需要做的事情,但感觉有点“骇人听闻”。

问题

  • 是否做事情的这种方式有一些显著的缺点或根本缺陷?
  • 纯粹使用Eclipse RAP有没有更好的方法来完成工作?

我也查了一下到使用Eclipse工作区/观点(工作台),但这似乎复杂的显著量添加到应用程序(更不用说最初的学习曲线),我宁愿避免它。特别是因为我不需要大部分的功能。

回答

2

StackLayout是显示控制堆栈的推荐方式,一次只能看到一个控件。

下面的示例将创建按钮区域,以显示父级顶部的选择按钮(“数据”,“设置”,...),容器占用剩余空间。所有部分均在使用StackLayout的容器内创建。

parent.setLayout(new GridLayout(1, false)); 
Composite buttonArea = new Composite(parent, SWT.NONE); 
buttonArea.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false)); 

Composite container = new Composite(parent, SWT.NONE); 
container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 
StackLayout containerLayout = new StackLayout(); 
container.setLayout(containerLayout); 
Composite dataSection = createDataSection(container); 
Composite settingsSection = createDataSection(container); 

选择监听器代码告诉布局要显示的部分。

dataButton.addSelectionListener(new SelectionAdapter() { 
    public void widgetSelected(SelectionEvent event) { 
    containerLayout.topControl = dataSection; 
    container.layout(); 
    } 
}); 

富勒更多的布局也看到这篇文章:https://www.eclipse.org/articles/article.php?file=Article-Understanding-Layouts/index.html

你是在正确的轨道上。不要使用工作台(您称之为“工作空间/视角”),它会比您可能需要的要多得多。此外,该堆栈的某些部分可以在需要时独立使用。例如JFace查看器或核心命令。

+0

奇怪的是,您所引用的文章中并未提及StackLayout(并且文章被标记为已弃用)。也没有我见过的教程提到了这种布局。不过,这似乎是我正在寻找的,明天我会试一试。 – FableBlaze

+0

经过测试,它按照我的希望工作。 StackLayout方法似乎更快,感觉更具响应性。作为一个侧面提示,调用'parent.setLayout(new GridLayout(1,false));'是多余的,因为无论如何,相同的布局被框架赋给了'parent'。该框架在'AbstractEntryPoint#createUI()'中执行此操作。但是,如果将来框架的这部分发生变化,明确设置它可能是一个好主意。 – FableBlaze

+0

你说得对,但是'GridLayout'是有意设定的,因为目前的情况是一个实现细节,框架没有声明'父'具有或没有哪个布局。 –