Package com.cedarsoft.spring.rcp.editor

Source Code of com.cedarsoft.spring.rcp.editor.HierarchyEditor

package com.cedarsoft.spring.rcp.editor;

import com.cedarsoft.spring.rcp.events.DetailsTreeSelectionEvent;
import com.cedarsoft.spring.rcp.hierarchy.DetailsPanel;
import com.cedarsoft.spring.rcp.hierarchy.HierachyTreeModel;
import com.cedarsoft.spring.rcp.hierarchy.HierarchyDetailsModel;
import com.cedarsoft.spring.rcp.hierarchy.HierarchyModelFactory;
import com.cedarsoft.spring.rcp.hierarchy.HierarchyTree;
import com.jidesoft.swing.JideBoxLayout;
import com.jidesoft.swing.JideSplitPane;
import com.cedarsoft.lookup.DynamicLookup;
import com.cedarsoft.lookup.Lookup;
import com.cedarsoft.lookup.Lookups;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.ApplicationEvent;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.tree.TreePath;
import java.awt.Color;

/**
* An editor with a hierarchy tree on the left side
*
* @param <T> the type the editor is made for
* @param <R> the type of the root object
*/
public abstract class HierarchyEditor<T, R> extends ExtendedEditor<T> {
  @NotNull
  private HierarchyModelFactory hierarchyModelFactory;

  private HierarchyDetailsModel detailsModel;
  private HierarchyTree hierarchyTree;
  protected HierachyTreeModel treeModel;

  public void setHierarchyModelFactory( @NotNull HierarchyModelFactory hierarchyModelFactory ) {
    this.hierarchyModelFactory = hierarchyModelFactory;
  }

  @Override
  @NotNull
  protected JComponent createControl() {
    JideSplitPane splitPane = new JideSplitPane();

    R rootObject = getRootObject();

    treeModel = hierarchyModelFactory.createHierarchyTreeModel( rootObject );
    detailsModel = hierarchyModelFactory.createDetailsModel( rootObject );

    detailsModel.setContext( getContext() );

    //wire the models
    hierarchyTree = new HierarchyTree( treeModel );
    hierarchyTree.addSelectionListener( detailsModel );

    detailsModel.select( new DetailsTreeSelectionEvent( DetailsTreeSelectionEvent.Type.SELECTED, getEditorObject() ) );
    TreePath initialSelectionPath = getInitialSelectionPath();
    hierarchyTree.getSelectionModel().setSelectionPath( initialSelectionPath );
    hierarchyTree.addSelectionListener( detailsModel );

    splitPane.add( new JScrollPane( hierarchyTree ), JideBoxLayout.FLEXIBLE );
    DetailsPanel detailsPanel = new DetailsPanel( detailsModel );
    detailsPanel.setBorder( BorderFactory.createLineBorder( Color.BLACK ) );
    splitPane.add( detailsPanel, JideBoxLayout.VARY );
    //    splitPane.add( new JScrollPane( new DetailsPanel( detailsModel ) ), JideBoxLayout.VARY );

    splitPane.setBorder( BorderFactory.createEmptyBorder( 5, 5, 5, 5 ) );
    return splitPane;
  }

  /**
   * Returns the initial selection path
   *
   * @return the initial selection path
   */
  @NotNull
  protected abstract TreePath getInitialSelectionPath();


  @Override
  @NotNull
  protected Lookup getSelectedObjects() {
    DynamicLookup dynamicLookup = Lookups.dynamicLookup( detailsModel.getCurrentlySelectedObject(), treeModel.getRoot() );
    TreePath path = hierarchyTree.getSelectionPath();
    if ( path != null ) {
      for ( Object element : path.getPath() ) {
        dynamicLookup.addValue( element );
      }
    }
    return dynamicLookup;
  }

  @Override
  protected void handleApplicationEvent( @NotNull ApplicationEvent event ) {
    if ( detailsModel != null ) {
      detailsModel.onApplicationEvent( event );
    }
  }

  /**
   * Returns the root object (based on the editor object)
   *
   * @return the root object
   */
  @NotNull
  protected abstract R getRootObject();
}
TOP

Related Classes of com.cedarsoft.spring.rcp.editor.HierarchyEditor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.