package net.wotonomy.test; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.AbstractButton; import javax.swing.JDialog; import net.wotonomy.foundation.NSArray; import net.wotonomy.ui.EOAssociation; import net.wotonomy.ui.EODisplayGroup; import net.wotonomy.ui.MasterDetailAssociation; import net.wotonomy.ui.swing.ListAssociation; import net.wotonomy.ui.swing.RadioPanelAssociation; import net.wotonomy.ui.swing.TextAssociation; import net.wotonomy.ui.swing.TreeAssociation; import net.wotonomy.ui.swing.util.WindowUtilities; /** * A simple editor panel with a few textfields. */ public class TreeInspectorController { public TreeInspectorController( EODisplayGroup titlesGroup, EODisplayGroup childrenGroup ) { final EODisplayGroup group = childrenGroup; // final EODisplayGroup group = titlesGroup; final TreePanel treePanel = new TreePanel(); // text associations EOAssociation ta; ta = new TreeAssociation( treePanel.tree, "People" ); ta.bindAspect( EOAssociation.TitlesAspect, titlesGroup, "lastName" ); ta.bindAspect( EOAssociation.ChildrenAspect, group, "childList" ); ta.bindAspect( EOAssociation.IsLeafAspect, titlesGroup, "childCount" ); // ta.bindAspect( EOAssociation.IsLeafAspect, null, "false" ); ta.establishConnection(); treePanel.tree.setEditable( true ); ta = new TextAssociation( treePanel.editPanel.firstNameField ); ta.bindAspect( EOAssociation.ValueAspect, group, "firstName" ); ta.establishConnection(); ta = new TextAssociation( treePanel.editPanel.middleNameField ); ta.bindAspect( EOAssociation.ValueAspect, group, "middleName" ); ta.establishConnection(); ta = new TextAssociation( treePanel.editPanel.lastNameField ); ta.bindAspect( EOAssociation.ValueAspect, group, "lastName" ); ta.establishConnection(); ta = new RadioPanelAssociation( treePanel.editPanel.yearRadioPanel ); ta.bindAspect( EOAssociation.ValueAspect, group, "createDate.year" ); EODisplayGroup yearTitles = new EODisplayGroup(); yearTitles.setObjectArray( new NSArray( new Object[] { "1999", "2000", "2001" } ) ); ta.bindAspect( EOAssociation.TitlesAspect, yearTitles, "" ); EODisplayGroup yearObjects = new EODisplayGroup(); yearObjects.setObjectArray( new NSArray( new Object[] { new Integer( 99 ), new Integer( 100 ), new Integer( 101 ) } ) ); ta.bindAspect( EOAssociation.ObjectsAspect, yearObjects, "" ); ta.establishConnection(); // detail group final EODisplayGroup detailGroup = new EODisplayGroup(); ta = new MasterDetailAssociation( detailGroup ); ta.bindAspect( EOAssociation.ParentAspect, group, "childList" ); ta.establishConnection(); ta = new ListAssociation( treePanel.editPanel.list ); ta.bindAspect( EOAssociation.TitlesAspect, detailGroup, "fullName" ); ta.establishConnection(); // display group action associations AbstractButton addButton = (AbstractButton) treePanel.editPanel.addPanel.getButton( "Add" ); addButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent evt ) { detailGroup.insertObjectAtIndex( new TestObject(), 0 ); } } ); AbstractButton removeButton = (AbstractButton) treePanel.editPanel.addPanel.getButton( "Remove" ); removeButton.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent evt ) { if ( detailGroup.selectedObject() != null ) detailGroup.deleteSelection(); } } ); /* AbstractButton refreshButton = (AbstractButton) treePanel.editPanel.addPanel.getButton( "Refresh" ); refreshButton.addActionListener( new ActionListener() { int count = 0; public void actionPerformed( ActionEvent evt ) { EODisplayGroup displayGroup = (EODisplayGroup) treePanel.tree.getSelectionPath().getLastPathComponent(); // displayGroup.insertObjectAtIndex( new TestObject(), 0 ); // displayGroup.deleteObjectAtIndex( 0 ); List sortOrderings = new LinkedList(); if ( count++ % 2 == 0 ) { sortOrderings.add( new EOSortOrdering( "lastName", EOSortOrdering.CompareAscending ) ); } else { sortOrderings.add( new EOSortOrdering( "lastName", EOSortOrdering.CompareDescending ) ); } displayGroup.setSortOrderings( sortOrderings ); displayGroup.updateDisplayedObjects(); } } ); */ /* ta = new DisplayGroupActionAssociation( treePanel.editPanel.addPanel.getButton( "Remove" ) ); ta.bindAspect( EOAssociation.ActionAspect, detailGroup, "deleteSelection" ); ta.establishConnection(); ta = new DisplayGroupActionAssociation( treePanel.editPanel.addPanel.getButton( "Refresh" ) ); ta.bindAspect( EOAssociation.ActionAspect, group, "updateDisplayedObjects" ); ta.establishConnection(); */ // add mouse listener for list treePanel.editPanel.list.addMouseListener( new MouseAdapter() { public void mouseClicked(MouseEvent e) { if ( e.getClickCount() == 2 ) { Object item = detailGroup.selectedObject(); if ( item != null ) { new InspectorController( item ); } } } }); // launch JDialog d = new JDialog(); d.getContentPane().add( treePanel ); d.setTitle( "Tree Panel" ); d.pack(); WindowUtilities.cascade( d ); d.show(); // workaround for memory issues on jdk1.2.2 d.addWindowListener( new WindowAdapter() { // exit on close public void windowClosing(WindowEvent e) { ((JDialog)e.getWindow()).getContentPane().removeAll(); } }); } }