Index: /CKEditor/branches/prototype/_source/plugins/table/dialogs/table.js
===================================================================
--- /CKEditor/branches/prototype/_source/plugins/table/dialogs/table.js	(revision 2768)
+++ /CKEditor/branches/prototype/_source/plugins/table/dialogs/table.js	(revision 2769)
@@ -22,13 +22,118 @@
 CKEDITOR.dialog.add( 'table', function( editor )
 {
+	var makeElement = function( name ){ return new CKEDITOR.dom.element( name, editor.document ); },
+		widthPattern = /^(\d+(?:\.\d+)?)(px|%)$/,
+		heightPattern = /^(\d+(?:\.\d+)?)px$/;
+
 	return {
 		title : editor.lang.dlgTableTitle,
 		minWidth : 480,
 		minHeight : 290,
-		onShow : function( )
+		onShow : function()
 		{
+			// Detect if there's a selected table.
+			this.restoreSelection();
+			var selection = editor.getSelection(),
+				ranges = selection.getRanges(),
+				me = this,
+				selectedTable;
+			if ( ranges.length > 0 )
+			{
+				var rangeRoot = ranges[0].getCommonAncestor( true );
+				selectedTable = rangeRoot.getAscendant( 'table', true );
+			}
+
+			// Fill in relevant fields if there's a selected table.
+			if ( selectedTable )
+			{
+				var syncAttr = function( inputName, attrName )
+				{
+					me.setValueOf( 'info', inputName, selectedTable.getAttribute( attrName ) );
+				},
+					widthMatch = widthPattern.exec( selectedTable.$.style.width ),
+					heightMatch = heightPattern.exec( selectedTable.$.style.height );
+				syncAttr( 'txtBorder', 'border' );
+				syncAttr( 'cmbAlign', 'align' );
+				syncAttr( 'txtCellPad', 'cellpadding' );
+				syncAttr( 'txtCellSpace', 'cellspacing' );
+				syncAttr( 'txtSummary', 'summary' );
+
+				if ( widthMatch )
+				{
+					this.setValueOf( 'info', 'txtWidth', widthMatch[1] );
+					this.setValueOf( 'info', 'cmbWidthType', widthMatch[2] == 'px' ? 'pixels' : 'percents' );
+				}
+
+				if ( heightMatch )
+					this.setValueOf( 'info', 'txtHeight', heightMatch[1] );
+			}
+
+			// Disable the width and height fields if a table is already selected.
+			// TODO
+
+			// Put focus into the first relevant text field.
+			this.getContentElement( 'info', selectedTable ? 'txtWidth' : 'txtRows' ).select();
 		},
-		onOk : function( )
+		onOk : function()
 		{
+			var table = this._.selectedElement || makeElement( 'table' ),
+				me = this,
+				caption = this.getValueOf( 'info', 'txtCaption' );
+
+			// Insert the caption, if any.
+			if ( caption != '' )
+				table.append( CKEDITOR.dom.element.createFromHtml( '<caption>' 
+						+ CKEDITOR.tools.htmlEncode( caption ) + '</caption>', editor.document ) );
+
+			// Generate the rows and cols.
+			if ( !this._.selectedElement )
+			{
+				var tbody = table.append( makeElement( 'tbody' ) ),
+					rows = parseInt( this.getValueOf( 'info', 'txtRows' ), 10 ) || 0;
+					cols = parseInt( this.getValueOf( 'info', 'txtCols' ), 10 ) || 0;
+
+				for ( var i = 0 ; i < rows ; i++)
+				{
+					var row = tbody.append( makeElement( 'tr' ) );
+					for ( var j = 0 ; j < cols ; j++ )
+					{
+						var cell = row.append( makeElement( 'td' ) );
+						if ( !CKEDITOR.env.ie )
+							cell.append( makeElement( 'br' ) );
+					}
+				}
+			}
+
+			// Set the basic attributes.
+			var setAttr = function( inputNameOrFunction, attrName )
+			{
+				var value = ( typeof( inputNameOrFunction) == 'function' ) ? inputNameOrFunction()
+					: me.getValueOf( 'info', inputNameOrFunction );
+				if ( value != '' )
+					table.setAttribute( attrName, value );
+			};
+			setAttr( 'txtBorder', 'border' );
+			setAttr( 'cmbAlign', 'align' );
+			setAttr( 'txtCellPad', 'cellpadding' );
+			setAttr( 'txtCellSpace', 'cellspacing' );
+			setAttr( 'txtSummary', 'summary' );
+			setAttr( function()
+				{
+					var styles = [];
+					if ( me.getValueOf( 'info', 'txtHeight' ) != '' )
+						styles.push( 'height:' + me.getValueOf( 'info', 'txtHeight' ) + 'px' );
+					if ( me.getValueOf( 'info', 'txtWidth' ) != '' )
+					{
+						var type = me.getValueOf( 'info', 'cmbWidthType' );
+						styles.push( 'width:' + me.getValueOf( 'info', 'txtWidth' ) + ( type == 'pixels' ? 'px' : '%' ) );
+					}
+					return styles.join( ';' );
+				}, 'style' );
+
+			// Insert the table element.
+			this.restoreSelection();
+			editor.insertElement( table );
+			this.clearSavedSelection();
+
 			return true;
 		},
@@ -147,4 +252,5 @@
 												widths : [ '50%','50%' ],
 												label : editor.lang.dlgTableWidth,
+												'default' : 200,
 												validate: function( ) {
 													if ( this.getValue() != '' )
@@ -168,5 +274,5 @@
 												[
 													[ editor.lang.dlgTableWidthPx , 'pixels'],
-													[ editor.lang.dlgTableWidthPc , 'procents']
+													[ editor.lang.dlgTableWidthPc , 'percents']
 												]
 											}
