[jboss-svn-commits] JBL Code SVN: r6888 - labs/jbossrules/trunk/drools-jbrms/src/main/java/org/drools/brms/client/decisiontable

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Wed Oct 18 09:17:53 EDT 2006


Author: stevearoonie
Date: 2006-10-18 09:17:49 -0400 (Wed, 18 Oct 2006)
New Revision: 6888

Added:
   labs/jbossrules/trunk/drools-jbrms/src/main/java/org/drools/brms/client/decisiontable/InsertAction.java
Modified:
   labs/jbossrules/trunk/drools-jbrms/src/main/java/org/drools/brms/client/decisiontable/EditActions.java
   labs/jbossrules/trunk/drools-jbrms/src/main/java/org/drools/brms/client/decisiontable/EditableDTGrid.java
Log:
Add ability to add a new row to a decision table

Modified: labs/jbossrules/trunk/drools-jbrms/src/main/java/org/drools/brms/client/decisiontable/EditActions.java
===================================================================
--- labs/jbossrules/trunk/drools-jbrms/src/main/java/org/drools/brms/client/decisiontable/EditActions.java	2006-10-18 13:17:25 UTC (rev 6887)
+++ labs/jbossrules/trunk/drools-jbrms/src/main/java/org/drools/brms/client/decisiontable/EditActions.java	2006-10-18 13:17:49 UTC (rev 6888)
@@ -1,5 +1,7 @@
 package org.drools.brms.client.decisiontable;
 
+import org.drools.brms.client.decisiontable.EditableDTGrid.RowClickListener;
+
 import com.google.gwt.user.client.ui.ClickListener;
 import com.google.gwt.user.client.ui.Composite;
 import com.google.gwt.user.client.ui.HorizontalPanel;
@@ -15,6 +17,7 @@
     private HorizontalPanel panel = new HorizontalPanel();
     private Image edit;
     private Image ok;
+	private int row;
     
 
     
@@ -24,16 +27,17 @@
      * @param editClickListener
      * @param okClickListener
      */
-    public EditActions(final ClickListener editClickListener, 
-                       final ClickListener okClickListener) {
-        
+    public EditActions(final int currentRow,
+    				   final RowClickListener editClickListener, 
+                       final RowClickListener okClickListener) {
+        row = currentRow;
         edit = new Image("images/edit.gif");
         edit.setTitle( "Edit this row" );
         edit.addClickListener( new ClickListener() {
 
             public void onClick(Widget w) {
                 makeEditable();
-                editClickListener.onClick( w );
+                editClickListener.onClick( w, row );
             }
             
         });
@@ -44,7 +48,7 @@
 
             public void onClick(Widget w) {
                 makeReadOnly();
-                okClickListener.onClick( w );                
+                okClickListener.onClick( w, row );                
             }
             
         });
@@ -66,6 +70,14 @@
         edit.setVisible( true );
         ok.setVisible( false );
     }
+
+	public int getRow() {
+		return row;
+	}
+
+	public void setRow(int row) {
+		this.row = row;
+	}
     
     
     

Modified: labs/jbossrules/trunk/drools-jbrms/src/main/java/org/drools/brms/client/decisiontable/EditableDTGrid.java
===================================================================
--- labs/jbossrules/trunk/drools-jbrms/src/main/java/org/drools/brms/client/decisiontable/EditableDTGrid.java	2006-10-18 13:17:25 UTC (rev 6887)
+++ labs/jbossrules/trunk/drools-jbrms/src/main/java/org/drools/brms/client/decisiontable/EditableDTGrid.java	2006-10-18 13:17:49 UTC (rev 6888)
@@ -1,6 +1,5 @@
 package org.drools.brms.client.decisiontable;
 
-import com.google.gwt.user.client.ui.ClickListener;
 import com.google.gwt.user.client.ui.Composite;
 import com.google.gwt.user.client.ui.FlexTable;
 import com.google.gwt.user.client.ui.HorizontalPanel;
@@ -10,145 +9,193 @@
 import com.google.gwt.user.client.ui.VerticalPanel;
 import com.google.gwt.user.client.ui.Widget;
 import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
-import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
 
 /**
  * The decision table viewer and editor.
+ * 
  * @author Michael Neale
- * @author Stephen Williams
+ * @author Steven Williams
  * 
- * TODO: add editors for header stuff,
- * and ability to add rows/cols and shift rows around
- * This probably can be done from a seperate "editor" such that it is re-rendered 
- * when you need to add/move a col or row.
+ * TODO: add editors for header stuff, and ability to add rows/cols and shift
+ * rows around This probably can be done from a seperate "editor" such that it
+ * is re-rendered when you need to add/move a col or row.
  * 
- * Should be able to add/shift stuff around by entering a row number to deal with.
+ * Should be able to add/shift stuff around by entering a row number to deal
+ * with.
  * 
  */
 public class EditableDTGrid extends Composite {
 
-    private static final int START_DATA_ROW = 1;
-    private FlexTable table = new FlexTable();
+	private static final int START_DATA_ROW = 1;
 
-    public EditableDTGrid(String dtName) {
+	private FlexTable table = new FlexTable();
 
-//for if I switch to a Grid
-//        table.resizeColumns( numCols() + 1 );
-//        table.resizeRows( numRows() );
+	private FlexCellFormatter cellFormatter = table.getFlexCellFormatter();
 
-        VerticalPanel vert = new VerticalPanel();
-        
-        Label title = new Label(dtName);
-        title.setStyleName( "dt-editor-Title" );
-        
-        HorizontalPanel header = new HorizontalPanel();
-        header.add( new Image("images/decision_table.gif") );
-        header.add( title );
-        
-        vert.add( header );
-        vert.add( table );
-        
-        FlexCellFormatter cellFormatter = table.getFlexCellFormatter();
-        
-        table.setStyleName( "dt-editor-Grid" );
+	public EditableDTGrid(String dtName) {
 
-        //set up the header
-        populateHeader( cellFormatter );
-        
-        //and the data follows
-        populateDataGrid( cellFormatter );
-        
-        //and this is how you span/merge things, FYI
-        //table.getFlexCellFormatter().setColSpan( 2, 3, 4 );
+		// for if I switch to a Grid
+		// table.resizeColumns( numCols() + 1 );
+		// table.resizeRows( numRows() );
 
-        //needed for Composite
-        initWidget( vert );
-    }
+		VerticalPanel vert = new VerticalPanel();
 
+		Label title = new Label(dtName);
+		title.setStyleName("dt-editor-Title");
 
-    private void populateHeader(FlexCellFormatter cellFormatter) {
-        
-        //for the count column
-        cellFormatter.setStyleName( 0, 0, "dt-editor-DescriptionCell" );
-        
-        for (int col = 1; col < numCols() + 1; col++) {
-            table.setText( 0, col, "some header " + col );
-            cellFormatter.setStyleName( 0, col, "dt-editor-DescriptionCell" );
-        }
-    }
+		HorizontalPanel header = new HorizontalPanel();
+		header.add(new Image("images/decision_table.gif"));
+		header.add(title);
 
+		vert.add(header);
+		vert.add(table);
 
-    /**
-     * This populates the "data" part of the decision table (not the header bits).
-     * It starts at the row offset. 
-     * @param cellFormatter So it can set the style of each cell that is created.
-     */
-    private void populateDataGrid(FlexCellFormatter cellFormatter) {
+		table.setStyleName("dt-editor-Grid");
 
-        for ( int i = 0; i < numRows(); i++ ) {
-            
-            int rowCount = i + 1;
-            
-            int column = 1;
-            int row = i + START_DATA_ROW;
-            
-            //now do the count column
-            table.setText( row, 0, Integer.toString( rowCount) );
-            cellFormatter.setStyleName( row, 0, "dt-editor-CountColumn" );
-            for ( ; column < numCols() + 1; column++ ) {
-                table.setText( row,
-                               column,
-                               "boo " + column );
-                cellFormatter.setStyleName( row, column, "dt-editor-Cell" );
-            }
-                        
-            final int currentRow = row;
-            
-            //the action magic
-            final EditActions actions = new EditActions(new ClickListener() {
-                    public void onClick(Widget w) {editRow( currentRow );}               
-                },
-                new ClickListener() {
-                    public void onClick(Widget w) {updateRow( currentRow );}                
-                });
-            table.setWidget( currentRow, column, actions );
-            
-        }
-    }
-    
-    /**
-     * Apply the changes to the row.
-     */
-    private void updateRow(int row) {
-        for (int column = 1; column < numCols() + 1; column++) {
-            TextBox text = (TextBox) table.getWidget( row, column );
-            table.setText( row, column, text.getText() );                
-        }
-    }
+		// set up the header
+		populateHeader();
 
+		// and the data follows
+		populateDataGrid();
 
-    /**
-     * This switches the given row into edit mode.
-     * @param row
-     */
-    private void editRow(int row) {
-        for (int column = 1; column < numCols() + 1; column++) {
-            String text = table.getText( row, column );
-                TextBox box = new TextBox();
-                box.setText( text );
-                box.setStyleName( "dsl-field-TextBox" );
-                box.setVisibleLength( 3 );                
-                table.setWidget( row, column, box );   
-                
-        }
-    }
+		// and this is how you span/merge things, FYI
+		// table.getFlexCellFormatter().setColSpan( 2, 3, 4 );
 
-    private int numCols() {
-        return 6;
-    }
+		// needed for Composite
+		initWidget(vert);
+	}
 
-    private int numRows() {
-        return 14;
-    }
+	private void populateHeader() {
 
+		// for the count column
+		cellFormatter.setStyleName(0, 0, "dt-editor-DescriptionCell");
+
+		for (int col = 1; col < numCols() + 1; col++) {
+			table.setText(0, col, "some header " + col);
+			cellFormatter.setStyleName(0, col, "dt-editor-DescriptionCell");
+		}
+	}
+
+	/**
+	 * This populates the "data" part of the decision table (not the header
+	 * bits). It starts at the row offset.
+	 * 
+	 * @param cellFormatter
+	 *            So it can set the style of each cell that is created.
+	 */
+	private void populateDataGrid() {
+
+		for (int i = 0; i < numRows(); i++) {
+
+			int rowCount = i + 1;
+
+			int column = 1;
+			int row = i + START_DATA_ROW;
+
+			// now do the count column
+			table.setText(row, 0, Integer.toString(rowCount));
+			cellFormatter.setStyleName(row, 0, "dt-editor-CountColumn");
+			for (; column < numCols() + 1; column++) {
+				table.setText(row, column, "boo " + column);
+				cellFormatter.setStyleName(row, column, "dt-editor-Cell");
+			}
+
+			final int currentRow = row;
+
+			addActions(column, currentRow, false);
+		}
+	}
+
+	private void addActions(int column, final int currentRow, boolean newRow) {
+		// the action magic
+		final EditActions actions = new EditActions(currentRow, new RowClickListener() {
+			public void onClick(Widget w, int row) {
+				editRow(row);
+			}
+		}, new RowClickListener() {
+			public void onClick(Widget w, int row) {
+				updateRow(row);
+			}
+		});
+		if (newRow) actions.makeEditable();
+		table.setWidget(currentRow, column++, actions);
+
+		final InsertAction insertAction = new InsertAction(currentRow, new RowClickListener() {
+			public void onClick(Widget w, int row) {
+				insertRow(row);
+			}
+		});
+		table.setWidget(currentRow, column, insertAction);
+	}
+	
+	/**
+	 * Listener for click events that affect a whole row
+	 */
+	interface RowClickListener {
+		void onClick(Widget w, int row);
+	}
+	
+	//counter for dummy new row data 
+	private int cellId;
+	
+	/**
+	 * Add a new row after the specified row
+	 * @param row the row to insert after
+	 */
+	private void insertRow(int row) {
+		int newRow = table.insertRow(row + 1);
+		table.setText(newRow, 0, Integer.toString(newRow));
+		cellFormatter.setStyleName(newRow, 0, "dt-editor-CountColumn");
+		int column = 1;
+		for (; column < numCols() + 1; column++) {
+			table.setText(newRow, column, "new " + cellId++);
+			cellFormatter.setStyleName(newRow, column, "dt-editor-Cell");
+		}
+		addActions(column, newRow, true);
+		editRow(newRow);
+		for (int r = newRow + 1; r  < table.getRowCount(); r++) {
+			table.setText(r, 0, Integer.toString(r));
+			EditActions editActions = (EditActions) table.getWidget(r, column);
+			editActions.setRow(r);
+			InsertAction insertAction = (InsertAction) table.getWidget(r, column + 1);
+			insertAction.setRow(r);
+		}
+			
+	}
+
+	/**
+	 * Apply the changes to the row.
+	 */
+	private void updateRow(int row) {
+		for (int column = 1; column < numCols() + 1; column++) {
+			TextBox text = (TextBox) table.getWidget(row, column);
+			table.setText(row, column, text.getText());
+		}
+	}
+
+	/**
+	 * This switches the given row into edit mode.
+	 * 
+	 * @param row
+	 */
+	private void editRow(int row) {
+		for (int column = 1; column < numCols() + 1; column++) {
+			String text = table.getText(row, column);
+			TextBox box = new TextBox();
+			box.setText(text);
+			box.setStyleName("dsl-field-TextBox");
+			box.setVisibleLength(3);
+			table.setWidget(row, column, box);
+
+		}
+	}
+
+	private int numCols() {
+		return 6;
+	}
+
+	private int numRows() {
+		return 14;
+	}
+
 }

Added: labs/jbossrules/trunk/drools-jbrms/src/main/java/org/drools/brms/client/decisiontable/InsertAction.java
===================================================================
--- labs/jbossrules/trunk/drools-jbrms/src/main/java/org/drools/brms/client/decisiontable/InsertAction.java	2006-10-18 13:17:25 UTC (rev 6887)
+++ labs/jbossrules/trunk/drools-jbrms/src/main/java/org/drools/brms/client/decisiontable/InsertAction.java	2006-10-18 13:17:49 UTC (rev 6888)
@@ -0,0 +1,52 @@
+package org.drools.brms.client.decisiontable;
+
+import org.drools.brms.client.decisiontable.EditableDTGrid.RowClickListener;
+
+import com.google.gwt.user.client.ui.ClickListener;
+import com.google.gwt.user.client.ui.Composite;
+import com.google.gwt.user.client.ui.HorizontalPanel;
+import com.google.gwt.user.client.ui.Image;
+import com.google.gwt.user.client.ui.Widget;
+
+/**
+ * This shows the widget for inserting a row.
+ * @author Steven Williams
+ */
+public class InsertAction extends Composite {
+
+    private HorizontalPanel panel = new HorizontalPanel();
+    private Image insert;
+    private int row;
+    
+
+    
+    
+    /**
+     * Pass in the click listener delegate for when the respective action is clicked
+     * @param clickListener
+     * @param okClickListener
+     */
+    public InsertAction(final int currentRow, final RowClickListener clickListener) {
+        row = currentRow;
+        insert = new Image("images/new_item.gif");
+        insert.setTitle( "Insert row after this row" );
+        insert.addClickListener( new ClickListener() {
+			public void onClick(final Widget w) {
+				clickListener.onClick(w, row);
+			}
+        });
+        
+        panel.add( insert);
+        
+        initWidget( panel );
+    }
+    
+    public void setRow(final int row) {
+    	this.row = row;
+    }
+    
+    public int getRow() {
+    	return row;
+    }
+    
+}




More information about the jboss-svn-commits mailing list