XRulesTableModel.java :  » XML-UI » xui32 » com » xoetrope » carousel » survey » Java Open Source

Java Open Source » XML UI » xui32 
xui32 » com » xoetrope » carousel » survey » XRulesTableModel.java
package com.xoetrope.carousel.survey;

import com.xoetrope.survey.QuestionGroup;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;
import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.table.AbstractTableModel;
import javax.swing.event.ListSelectionListener;
import net.xoetrope.xui.XProject;
import net.xoetrope.xui.XProjectManager;

/**
 * A model for rules table.
 *
 * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
 * the GNU Public License (GPL), please see license.txt for more details. If
 * you make commercial use of this software you must purchase a commercial
 * license from Xoetrope.</p>
 * <p> $Revision: 1.5 $</p>
 */
public class XRulesTableModel extends AbstractTableModel implements MouseListener, ListSelectionListener, Observer 
{
  
  public static final int COL_GROUPID = 0;
  public static final int COL_RULEID = 1;
  public static final int COL_RULENAME = 2;
  public static final int COL_TARGETID = 3;

  protected XQuestionGroup currentGroup;
  protected XSurvey survey;
  protected XTable table;
  protected Observable notifier;  
  protected JPopupMenu popupMenu;
  protected NewRuleDialog newRuleDialog;
  
  // actions
  protected ActionListener addRule, deleteRule, moveUpRule, moveDownRule;
  
  protected XTableColumn[] columns = new XTableColumn[ 4 ];
  
  public XRulesTableModel( XTable t ) 
  {
    table = t;
    XProject project = XProjectManager.getCurrentProject();    
    survey = ( XSurvey ) project.getObject( "Survey" );
    notifier = new RuleChangeNotifier();
    currentGroup = survey.getGroup( 0 );
    
    columns[ 0 ] = new XTableColumn( "Group id", 20, JLabel.LEFT,  ( JTextField ) null );
    columns[ 1 ] = new XTableColumn( "Rule id", 20, JLabel.LEFT, new JTextField() );
    columns[ 2 ] = new XTableColumn( "Rule name ", 300, JLabel.LEFT, new JTextField() );        
    columns[ 3 ] = new XTableColumn( "Target", 100, JLabel.LEFT, new JComboBox( survey.getTargetGroups() ));
        
    table.getSelectionModel().addListSelectionListener( this );
    
    addRule = new AddRule();
    deleteRule = new DeleteRule();
    moveUpRule = new MoveUpRule();
    moveDownRule = new MoveDownRule();
    
    createPopupMenu();
    newRuleDialog = new NewRuleDialog();    
    
    validate();
  }
  
  public JTable getTable()
  {
    return table;
  }
  
  public void validate()
  {
    table.setModel( this, columns, this );
  }
  
  public void refresh() 
  {
    table.clearSelection();
    table.revalidate();
    table.repaint();
  }
  
  public ActionListener getAddRule() 
  {
    return addRule;
  }
  
  public ActionListener getDeleteRule() 
  {
    return deleteRule;
  }
  
  public ActionListener getMoveUpRule() 
  {
    return moveUpRule;
  }
  
  public ActionListener getMoveDownRule() 
  {
    return moveDownRule;
  }
  
  private void createPopupMenu() 
  {
    JMenuItem menuItem;
    popupMenu = new JPopupMenu();
    popupMenu.add( new JLabel( "Rule Option") );
    popupMenu.addSeparator();
    menuItem = new JMenuItem( "Move up" );
    popupMenu.add( menuItem );
    menuItem.addActionListener( moveUpRule );
    menuItem = new JMenuItem( "Move down" );
    popupMenu.add( menuItem );
    menuItem.addActionListener( moveDownRule );
    popupMenu.addSeparator();
    menuItem = new JMenuItem( "Add.." );
    popupMenu.add( menuItem );
    menuItem.addActionListener( addRule );
    menuItem = new JMenuItem( "Delete.." );
    popupMenu.add( menuItem );
    menuItem.addActionListener( deleteRule );
  }
  
  public Observable getNotifier() 
  {
    return notifier;
  }
  
  public boolean isCellEditable( int row, int col ) 
  {
    return ( columns[col].getCellEditor() != null );
  }
  
  public int getRowCount() 
  {
    return ( currentGroup != null ? currentGroup.getRules().size() : 0 );
  }
  
  public XTableColumn[] getColumns() 
  {
    return columns;
  }
  
  public int getColumnCount() 
  {
    int cc = 0;
    for ( int i = 0; i < columns.length; i++ )
      if ( columns[ i ].isActive() ) cc++;
    
    return cc;
  }
  
  public String getColumnName( int col ) 
  {
    int nCol = -1, i = 0;
    for ( ; i < columns.length; i++ ) {
      if ( columns[ i ].isActive() ) nCol++;
      if ( nCol == col ) break;
    }
    return columns[ i ].getName();
  }
  
  public XRule getSelectedRule()
  {
    XRule rule = null;
    int row = table.getSelectedRow();
    if ( currentGroup != null && row != -1 ) {
      rule = ( XRule ) currentGroup.getRules().get( row );
    }
    return rule;
  }
  
  public Object getValueAt( int rowIndex, int columnIndex ) 
  {
    if ( currentGroup == null || rowIndex < 0 || rowIndex >= getRowCount() )
      return "";

    Vector rules = currentGroup.getRules();
    XRule rule = ( XRule ) rules.get( rowIndex );
    switch ( columnIndex ) {
      case COL_RULEID:
        return String.valueOf( rule.getId() );
      case COL_RULENAME:
        return rule.getName();
      case COL_GROUPID:        
        QuestionGroup owningGroup = rule.getGroup();
        return owningGroup.toString();
      case COL_TARGETID:
        QuestionGroup targetGroup = rule.getTarget();
        return ( targetGroup != null ? targetGroup.toString() : "none" );
    }
    return "";
  }
  
  public void setValueAt( Object value, int rowIndex, int columnIndex ) 
  {
    if ( currentGroup == null || rowIndex < 0 || rowIndex >= getRowCount() )
      return;
    
    Vector rules = currentGroup.getRules();
    XRule rule = ( XRule ) rules.get( rowIndex );
    switch ( columnIndex ) {
      case COL_RULEID:
        int ruleId = new Integer( value.toString() ).intValue();
        rule.setId( ruleId );
        break;
      case COL_RULENAME:
        rule.setName( value.toString() );
        break;
      case COL_TARGETID:
        XQuestionGroup target = ( ( XTargetGroup ) value ).getGroup();
        rule.changeTarget( target );
        break;
    }
  }
  
  private void showPopupMenu( MouseEvent e ) 
  {
    if ( e.isPopupTrigger()) {
      Component source = (Component)e.getSource();
      boolean isQuestionOption = (( source instanceof XTable ) || ( source instanceof JTextField ));
      Point p = e.getPoint();
      if ( !isQuestionOption ) {
        System.out.println( source.getParent().getParent().getParent().getParent());
        // table->viewport->scrollpane->XPopupPanel
        //((XResponseSetEditor)( ( XPopupPanel )source.getParent().getParent().getParent() ).getOwner()).addPopupActions();
      } 
      else
        popupMenu.show( source, p.x, p. y );
    }
  }
  
  public void mouseClicked(MouseEvent e) 
  {
  }
  
  public void mousePressed(MouseEvent e) 
  {
    showPopupMenu( e );
  }
  
  public void mouseReleased(MouseEvent e) 
  {
    showPopupMenu( e );
  }
  
  public void mouseEntered(MouseEvent e) {
  }
  
  public void mouseExited(MouseEvent e) {
  }
  
  public void valueChanged(ListSelectionEvent e) {
    int row = table.getSelectedRow();
    XRule rule = ( row >= 0 ? currentGroup.getRule( row ) : null );
    notifier.notifyObservers( rule );
  }

  private int getRuleIdx( Vector rules, XRule rule )
  {
    int idx = -1;
    for ( int i = 0; i < rules.size() && idx == -1; i++ ) {
      XRule r = ( XRule ) rules.get( i );
      if ( r.equals( rule ) ) idx = i;
    }
    return idx;
  }
    
  public void selectRule( XRule rule )
  {
    int selectedRow = table.getSelectedRow();
    int row = currentGroup.getRuleIdx( rule );
    if ( row != -1 && selectedRow != row ) 
      table.getSelectionModel().setSelectionInterval( row, row );
    else if ( row == -1 )
      table.clearSelection();
    table.repaint();
  }
  
  public void update(Observable o, Object arg)
  {
    XQuestionGroup newGroup = ( XQuestionGroup ) arg;
    if ( newGroup == null || !newGroup.equals( currentGroup ) ) {
      currentGroup = newGroup;
      table.clearSelection();
      table.revalidate();
      table.repaint();
      notifier.notifyObservers( null );
    }
  }
  
  
  
  private class RuleChangeNotifier extends Observable 
  {
    public void notifyObservers( Object o ) {
      setChanged();
      super.notifyObservers( o );
    }
  }
  
  private class MoveUpRule implements ActionListener 
  {
    public void actionPerformed( ActionEvent e ) 
    {
      /*
      int row = table.getSelectedRow();
      if ( row <= 0 || row >= getRowCount() ) return;
      
      Vector rules = null;
      if ( groupIdx == -1 ) {
        Enumeration enumeration = rulesGroups.elements();
        while ( enumeration.hasMoreElements() ) {
          rules = ( Vector ) enumeration.nextElement();
          if ( row - rules.size() < 0 ) break;
          row -= rules.size();
        }
      } else {
        rules = ( Vector ) rulesGroups.get( groupIdx );
      }
      if ( row == 0 ) return;
      
      XRule rule = ( XRule ) rules.get( row );
      rules.remove( rule );
      rules.add( --row, rule );
      table.getSelectionModel().setSelectionInterval( row, row );
      table.revalidate();
      table.repaint();
      notifier.notifyObservers( rule );
       */
    }       
  }
  
  private class MoveDownRule implements ActionListener 
  {
    public void actionPerformed( ActionEvent e ) 
    {
      /*
      int row = table.getSelectedRow();
      if ( row < 0 || row >= getRowCount() - 1 ) return;
      
      Vector rules = null;
      if ( groupIdx == -1 ) {
        Enumeration enumeration = rulesGroups.elements();
        while ( enumeration.hasMoreElements() ) {
          rules = ( Vector ) enumeration.nextElement();
          if ( row - rules.size() < 0 ) break;
          row -= rules.size();
        }
      } else {
        rules = ( Vector ) rulesGroups.get( groupIdx );
      }
      if ( row == rules.size() - 1 ) return;
      
      XRule rule = ( XRule ) rules.get( row );
      rules.remove( rule );
      rules.add( ++row, rule );
      table.getSelectionModel().setSelectionInterval( row, row );
      table.revalidate();
      table.repaint();
      notifier.notifyObservers( rule );
       */
    }
  }
  
  private class AddRule implements ActionListener 
  {
    public void actionPerformed( ActionEvent e ) 
    {
      if ( newRuleDialog.showDialog( currentGroup ) ) {                
        XQuestionGroup group = ( XQuestionGroup ) newRuleDialog.getGroup();
        int ruleId = newRuleDialog.getRuleId();        
        XQuestionGroup target = ( XQuestionGroup ) newRuleDialog.getTarget();
        String ruleName = newRuleDialog.getRuleName();
                        
        XRule rule = new XRule( ruleId, group, target, ruleName );        
        int row = survey.getGroupIdxById( group.getId() );                
        group.addNewRule( ruleId, target, ruleName );
        
        table.revalidate();
        table.repaint();
      }
    }
  }
  
  private class DeleteRule implements ActionListener 
  {
    public void actionPerformed( ActionEvent e ) 
    {
      int row = table.getSelectedRow();
      if (( row < 0 ) || ( row >= getRowCount() )) 
        return;
      
      Vector rules = currentGroup.getRules();
      XRule rule = ( XRule ) rules.get( row );
      currentGroup.deleteRule( rule );
      table.clearSelection();
      table.revalidate();
      table.repaint();
      notifier.notifyObservers( null );      
    }
  }
  
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.