Subversion Repositories DevTools

Rev

Rev 2128 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2094 ghuddy 1
using System;
2104 ghuddy 2
using System.Collections;
3
using System.Text;
4
using System.ComponentModel;
5
using System.Windows.Forms;
2136 brianf 6
using Microsoft.Office.Interop.Word;
2094 ghuddy 7
 
8
namespace EA_DocGen
9
{
10
	/// <summary>
11
	/// Summary description for TabularContent.
12
	/// </summary>
13
	public class TabularContent
14
	{
2136 brianf 15
      private static Range WordRange;
2106 ghuddy 16
      private static object startLocation;
17
      private static object endLocation;
2094 ghuddy 18
 
2106 ghuddy 19
		public static void initialise()
2094 ghuddy 20
		{
21
		}
22
 
2116 ghuddy 23
      /// <summary>
24
      /// This is an attempt to create a table similar in style to those made by the ERG doc template macros.
25
      /// It is not exaclty that same though, and further work could be done to align it more precisely.
26
      /// </summary>
27
      /// <param name="tableTitle"></param>
28
      /// <param name="numRows"></param>
29
      /// <param name="numCols"></param>
30
      /// <returns></returns>
2106 ghuddy 31
      public static int Table_Create(string tableTitle, bool borders, int numRows, int numCols)
2094 ghuddy 32
      {
2106 ghuddy 33
         createWordDoc.WordDocument.Content.InsertParagraphAfter();
2094 ghuddy 34
         SelectInsertionPointAtEndOfDocument();
35
 
36
         Table_InsertCaption(tableTitle);
37
 
2106 ghuddy 38
         startLocation = createWordDoc.WordDocument.Content.End;
39
         createWordDoc.WordDocument.Content.InsertParagraphAfter();
40
         endLocation = createWordDoc.WordDocument.Content.End;
41
         WordRange = createWordDoc.WordDocument.Range(ref startLocation, ref endLocation);
2094 ghuddy 42
 
43
         object defaultTableBehaviour = Type.Missing;
44
         object autofitBehaviour = Type.Missing;
2136 brianf 45
         Table Table = createWordDoc.WordDocument.Tables.Add( WordRange, numRows, numCols, ref defaultTableBehaviour, ref autofitBehaviour );
2094 ghuddy 46
 
2106 ghuddy 47
         if (borders)
48
         {
2136 brianf 49
            Table.Rows[1].Shading.BackgroundPatternColor = WdColor.wdColorGray20;
50
            Table.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
51
            Table.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
2106 ghuddy 52
         }
53
         else
54
         {
2136 brianf 55
            Table.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleNone;
56
            Table.Borders.InsideLineStyle = WdLineStyle.wdLineStyleNone;
2106 ghuddy 57
         }
2094 ghuddy 58
 
59
         Table.Select();
60
         object tableTextStyle = EA_Constants.styleName_TableText;
61
         Table.Range.set_Style( ref tableTextStyle );
62
 
2106 ghuddy 63
         return createWordDoc.WordDocument.Tables.Count;
2094 ghuddy 64
      }
65
 
66
 
67
 
2136 brianf 68
      public static void Table_SetTableColumnTitle(Table table, string title, int column)
2094 ghuddy 69
      {
2136 brianf 70
         table.Rows[1].Shading.BackgroundPatternColor = WdColor.wdColorGray10;
2094 ghuddy 71
         table.Cell(1,column).Range.Text = title;
2136 brianf 72
         table.Cell(1,column).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
2094 ghuddy 73
         table.Cell(1,column).Range.Font.Bold = 1;
74
      }
75
 
76
 
2136 brianf 77
      public static void Table_SetTableCellTitle(Table table, string title, int row, int column)
2094 ghuddy 78
      {
2136 brianf 79
         table.Cell(row,column).Shading.BackgroundPatternColor = WdColor.wdColorGray20;
2094 ghuddy 80
         table.Cell(row,column).Range.Text = title;
2136 brianf 81
         table.Cell(row,column).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
2094 ghuddy 82
         table.Cell(row,column).Range.Font.Bold = 1;
83
      }
84
 
85
 
2136 brianf 86
      public static int Table_InsertNewRowAfterThisRow(Table table, int row)
2094 ghuddy 87
      {
88
         table.Rows[row].Select();
89
         object rowC = 1;
2106 ghuddy 90
         createWordDoc.WordApp.Selection.InsertRowsBelow(ref rowC);
2136 brianf 91
         table.Rows[row+1].Shading.BackgroundPatternColor = WdColor.wdColorWhite;
2094 ghuddy 92
         return row+1;
93
      }
94
 
2136 brianf 95
      public static void Table_DeleteThisRow(Table table, int row)
2104 ghuddy 96
      {
97
         table.Rows[row].Delete();
98
      }
2094 ghuddy 99
 
2136 brianf 100
      public static void Table_DeleteRows(Table table, int row, int count)
2104 ghuddy 101
      {
102
         for (int i=0; i<count; i++)
103
         {
104
            table.Rows[row].Delete();
105
         }
106
      }
107
 
2136 brianf 108
      public static int Table_AppendSeperatorAndNewRowAfterThisRow(Table table, int row)
2094 ghuddy 109
      {
110
         // add two more rows, the first being a seperator
111
         int seperatorRow = Table_InsertNewRowAfterThisRow(table, row);
112
         int newRow       = Table_InsertNewRowAfterThisRow(table, seperatorRow);
113
 
114
         // Make seperator row, one cell wide, and remove its left & right borders
115
         table.Rows[seperatorRow].Select();
2106 ghuddy 116
         createWordDoc.WordApp.Selection.Cells.Merge();
2136 brianf 117
         createWordDoc.WordApp.Selection.Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleNone;
118
         createWordDoc.WordApp.Selection.Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleNone;
2094 ghuddy 119
 
120
         return newRow;
121
      }
122
 
123
 
2136 brianf 124
      public static void Table_MergeCellsInThisRow(Table table, int row)
2094 ghuddy 125
      {
126
         table.Rows[row].Select();
2106 ghuddy 127
         createWordDoc.WordApp.Selection.Cells.Merge();
2094 ghuddy 128
      }
129
 
130
 
2106 ghuddy 131
      public static void Table_InsertCaption(string captionText)
2094 ghuddy 132
      {
2106 ghuddy 133
         if (captionText.Length > 0)
134
         {
135
            object Label = "Table";
136
            object Title = Type.Missing;
137
            object TitleAutoText = Type.Missing;
2136 brianf 138
            object Position = WdCaptionPosition.wdCaptionPositionAbove;
2106 ghuddy 139
            object ExcludeLabel = 0;
140
            createWordDoc.WordApp.Selection.InsertCaption( ref Label, ref Title, ref TitleAutoText, ref Position, ref ExcludeLabel);
141
            createWordDoc.WordApp.Selection.TypeText( ": " + captionText);
2136 brianf 142
            createWordDoc.WordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
2106 ghuddy 143
         }
2094 ghuddy 144
      }
145
 
146
 
2106 ghuddy 147
      private static void SelectInsertionPointAtEndOfDocument()
2094 ghuddy 148
      {
149
         object unit; 
150
         object extend;
151
 
2136 brianf 152
         unit = WdUnits.wdStory; 
153
         extend = WdMovementType.wdMove;
2106 ghuddy 154
         createWordDoc.WordApp.Selection.EndKey(ref unit, ref extend);
2094 ghuddy 155
      }
156
 
2104 ghuddy 157
 
2124 ghuddy 158
      /// <summary>
159
      /// Processes an EA_DocGen table element, taking the notes that it contains, and forming a
160
      /// table in the generated document with them. The notes contain instructions on how to format
161
      /// the table, as well as the cell content. This must all be parsed and tokenised to extract the
162
      /// options and data.
163
      /// </summary>
164
      /// <param name="theElement"></param>
165
      /// <param name="recurse_level"></param>
2106 ghuddy 166
      public static void processTableElement(EA.Element theElement, int recurse_level )
2104 ghuddy 167
      {
2126 ghuddy 168
         processTableElement(theElement.Notes, recurse_level, 0);
169
      }
170
 
171
      public static void processTableElement(string tableDef, int recurse_level, float indent_pts )
172
      {
2104 ghuddy 173
         string [] EA_DocGenTable = null;
174
         string [] cells = null;
175
 
176
         string delimStr = "\r\n";
177
         char [] delim = delimStr.ToCharArray();
178
 
179
         string trimStr = " ";
180
         char [] trimChars = trimStr.ToCharArray();
181
 
2126 ghuddy 182
         EA_DocGenTable = tableDef.Split(delim,200);
2104 ghuddy 183
 
184
         int columnCount = 0;
185
         int rowCount = 0;
186
         char seperator = ',';
187
         string tableTitle = "";
2106 ghuddy 188
         bool borders=true;
189
 
2104 ghuddy 190
         bool gotSeperator = false;
191
         bool gotTitle = false;
2106 ghuddy 192
         bool gotBorders = false;
193
         bool gotWidths = false;
194
         bool gotIndent = false;
2104 ghuddy 195
 
196
         ArrayList colWidths = new ArrayList();
197
         int indent = 0;
198
 
199
         // Scan the notes content line by line looking for table parameters and counting
200
         // the number of table rows (which is not specified as an explicit table parameter).
201
         int s_ElementsConsumedByTableParams = 0;
202
         foreach(string s in EA_DocGenTable)
203
         {
204
            if (s.Length > 0 && s != "\n" && s != "\r" )
205
            {
2106 ghuddy 206
               if (gotTitle == false && s.StartsWith("title="))
2104 ghuddy 207
               {
208
                  s_ElementsConsumedByTableParams++;
2106 ghuddy 209
                  tableTitle = EA_DocGenOptions.getOptionValue(s, tableTitle);
2104 ghuddy 210
                  gotTitle = true;
2106 ghuddy 211
                  //if (tableTitle == "")
212
                  //{
213
                  //   MessageBox.Show( "Table Element Serialisation Failed - Bad Title" );
214
                  //   break;
215
                  //}
2104 ghuddy 216
               }
217
 
2106 ghuddy 218
               else if (columnCount == 0 && s.StartsWith("columns="))
2104 ghuddy 219
               {
220
                  s_ElementsConsumedByTableParams++;
2106 ghuddy 221
                  columnCount = EA_DocGenOptions.getOptionValue(s, columnCount);
2104 ghuddy 222
                  if (columnCount == 0)
223
                  {
224
                     MessageBox.Show( "Table Element Serialisation Failed - bad column count" );
225
                     break;
226
                  }
227
               }
228
 
2106 ghuddy 229
               else if (gotSeperator == false && s.StartsWith("seperator="))
2104 ghuddy 230
               {
231
                  s_ElementsConsumedByTableParams++;
2106 ghuddy 232
                  seperator = EA_DocGenOptions.getOptionValue(s, seperator);
2104 ghuddy 233
                  gotSeperator = true;
234
               }
235
 
2106 ghuddy 236
               else if (gotWidths == false && s.StartsWith("widths="))
2104 ghuddy 237
               {
238
                  s_ElementsConsumedByTableParams++;
2106 ghuddy 239
                  gotWidths = true;
240
                  string optValStr = EA_DocGenOptions.getOptionValue(s, "");
2104 ghuddy 241
 
242
                  string width_delimStr = ",";
243
                  char [] width_delim = width_delimStr.ToCharArray();
244
 
245
                  string [] width_strings = optValStr.Split(width_delim, 50);
246
 
247
                  foreach (string ws in width_strings)
248
                  {
249
                     if (ws.Length > 0 && ws != "\n" && ws != "\r" )
250
                     {
251
                        colWidths.Add( System.Convert.ToDouble(ws) );
252
                     }
253
                  }
254
               }
2106 ghuddy 255
               else if (gotIndent == false && s.StartsWith("indent="))
2104 ghuddy 256
               {
257
                  s_ElementsConsumedByTableParams++;
2106 ghuddy 258
                  gotIndent = true;
259
                  indent = EA_DocGenOptions.getOptionValue(s, 0);
2104 ghuddy 260
               }
2106 ghuddy 261
               else if (gotBorders == false && s.StartsWith("borders="))
262
               {
263
                  s_ElementsConsumedByTableParams++;
264
                  gotBorders = true;
265
                  borders = EA_DocGenOptions.getOptionValue(s, borders);
266
               }
2104 ghuddy 267
               else
268
               {
269
                  rowCount++;
270
               }
271
            }
272
         }
273
 
2106 ghuddy 274
         if (columnCount > 0 && rowCount > 0)
2104 ghuddy 275
         {
276
            if (rowCount < 2)
277
            {
278
               MessageBox.Show( "Table Element Serialisation Failed - Insufficient Rows" );
279
            }
280
            else
281
            {
282
               // create the table in the word doc
2106 ghuddy 283
               int tableNum = Table_Create( tableTitle, borders, rowCount, columnCount );
2136 brianf 284
               Table table = createWordDoc.WordDocument.Tables[tableNum];
285
               object center = WdParagraphAlignment.wdAlignParagraphCenter;
2104 ghuddy 286
 
287
               int col = 1;
288
               foreach (double d in colWidths)
289
               {
290
                  if (col <= columnCount)
2136 brianf 291
                     table.Columns[col].SetWidth( createWordDoc.WordApp.CentimetersToPoints((float)d), WdRulerStyle.wdAdjustNone );
2104 ghuddy 292
                  col++;
293
               }
294
 
295
               if (indent > 0)
296
               {
297
                  table.Select();
298
                  while (indent > 0)
299
                  {
2106 ghuddy 300
                     createWordDoc.WordApp.Selection.Paragraphs.Indent();
2104 ghuddy 301
                     indent--;
302
                  }
303
               }
304
 
305
               // scan the element notes again to extract the cell content and add it to the
306
               // table we just created.
307
               int row = 1;
308
               col = 0;
309
               foreach(string s in EA_DocGenTable)
310
               {
311
                  if (s.Length > 0 && s != "\n" && s != "\r" )
312
                  {
313
                     if (s_ElementsConsumedByTableParams > 0)
314
                     {
315
                        s_ElementsConsumedByTableParams--;
316
                     }
317
                     else
318
                     {
319
                        delimStr = seperator.ToString();
320
                        delim = delimStr.ToCharArray();
321
                        cells = s.Split(delim,columnCount);
322
 
323
                        if (cells.Length != columnCount)
324
                        {
325
                           MessageBox.Show( "Table Element Serialisation Failed - Bad Row" );
326
                           break;
327
                        }
328
                        else
329
                        {
330
                           // serialise table row
331
                           for(col=1; col<=columnCount; col++)
332
                           {
333
                              cells[col-1].TrimStart( trimChars );
334
                              cells[col-1].TrimEnd( trimChars );
335
 
2128 ghuddy 336
                              cells[col-1] = cells[col-1].Replace("<br>","\r\n");
337
 
2104 ghuddy 338
                              table.Cell(row,col).Range.Text = cells[col-1];
339
 
340
                              // special handling for heading row
341
                              if (row == 1)
342
                              {
2136 brianf 343
                                 table.Cell(row,col).Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
2104 ghuddy 344
                                 table.Cell(row,col).Range.Font.Bold = 1;
345
                              }
346
                           }
347
                           row++;
348
                        }                     
349
                     }
350
                  }
351
               }
352
            }
353
         }
354
         else
355
         {
356
            MessageBox.Show( "Table Element Serialisation Failed - Table Parameters Incomplete" );
357
         }
358
      }
359
 
2094 ghuddy 360
	}
361
}