Subversion Repositories DevTools

Rev

Rev 2106 | Rev 2124 | Go to most recent revision | 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;
2094 ghuddy 6
using Word;
7
 
8
namespace EA_DocGen
9
{
10
	/// <summary>
11
	/// Summary description for TabularContent.
12
	/// </summary>
13
	public class TabularContent
14
	{
2106 ghuddy 15
      private static Word.Range WordRange;
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;
2106 ghuddy 45
         Word.Table Table = createWordDoc.WordDocument.Tables.Add( WordRange, numRows, numCols, ref defaultTableBehaviour, ref autofitBehaviour );
2094 ghuddy 46
 
2106 ghuddy 47
         if (borders)
48
         {
49
            Table.Rows[1].Shading.BackgroundPatternColor = Word.WdColor.wdColorGray20;
50
            Table.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
51
            Table.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
52
         }
53
         else
54
         {
55
            Table.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleNone;
56
            Table.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleNone;
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
 
2106 ghuddy 68
      public static void Table_SetTableColumnTitle(Word.Table table, string title, int column)
2094 ghuddy 69
      {
70
         table.Rows[1].Shading.BackgroundPatternColor = Word.WdColor.wdColorGray10;
71
         table.Cell(1,column).Range.Text = title;
72
         table.Cell(1,column).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
73
         table.Cell(1,column).Range.Font.Bold = 1;
74
      }
75
 
76
 
2106 ghuddy 77
      public static void Table_SetTableCellTitle(Word.Table table, string title, int row, int column)
2094 ghuddy 78
      {
79
         table.Cell(row,column).Shading.BackgroundPatternColor = Word.WdColor.wdColorGray20;
80
         table.Cell(row,column).Range.Text = title;
81
         table.Cell(row,column).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
82
         table.Cell(row,column).Range.Font.Bold = 1;
83
      }
84
 
85
 
2106 ghuddy 86
      public static int Table_InsertNewRowAfterThisRow(Word.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);
2094 ghuddy 91
         table.Rows[row+1].Shading.BackgroundPatternColor = Word.WdColor.wdColorWhite;
92
         return row+1;
93
      }
94
 
2106 ghuddy 95
      public static void Table_DeleteThisRow(Word.Table table, int row)
2104 ghuddy 96
      {
97
         table.Rows[row].Delete();
98
      }
2094 ghuddy 99
 
2106 ghuddy 100
      public static void Table_DeleteRows(Word.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
 
2106 ghuddy 108
      public static int Table_AppendSeperatorAndNewRowAfterThisRow(Word.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();
117
         createWordDoc.WordApp.Selection.Borders[Word.WdBorderType.wdBorderLeft].LineStyle = Word.WdLineStyle.wdLineStyleNone;
118
         createWordDoc.WordApp.Selection.Borders[Word.WdBorderType.wdBorderRight].LineStyle = Word.WdLineStyle.wdLineStyleNone;
2094 ghuddy 119
 
120
         return newRow;
121
      }
122
 
123
 
2106 ghuddy 124
      public static void Table_MergeCellsInThisRow(Word.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;
138
            object Position = Word.WdCaptionPosition.wdCaptionPositionAbove;
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);
142
            createWordDoc.WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
143
         }
2094 ghuddy 144
      }
145
 
146
 
2106 ghuddy 147
      private static void SelectInsertionPointAtEndOfDocument()
2094 ghuddy 148
      {
149
         object unit; 
150
         object extend;
151
 
152
         unit = Word.WdUnits.wdStory; 
153
         extend = Word.WdMovementType.wdMove;
2106 ghuddy 154
         createWordDoc.WordApp.Selection.EndKey(ref unit, ref extend);
2094 ghuddy 155
      }
156
 
2104 ghuddy 157
 
2106 ghuddy 158
      public static void processTableElement(EA.Element theElement, int recurse_level )
2104 ghuddy 159
      {
160
         string [] EA_DocGenTable = null;
161
         string [] cells = null;
162
 
163
         string delimStr = "\r\n";
164
         char [] delim = delimStr.ToCharArray();
165
 
166
         string trimStr = " ";
167
         char [] trimChars = trimStr.ToCharArray();
168
 
169
         EA_DocGenTable = theElement.Notes.ToString().Split(delim,200);
170
 
171
         int columnCount = 0;
172
         int rowCount = 0;
173
         char seperator = ',';
174
         string tableTitle = "";
2106 ghuddy 175
         bool borders=true;
176
 
2104 ghuddy 177
         bool gotSeperator = false;
178
         bool gotTitle = false;
2106 ghuddy 179
         bool gotBorders = false;
180
         bool gotWidths = false;
181
         bool gotIndent = false;
2104 ghuddy 182
 
183
         ArrayList colWidths = new ArrayList();
184
         int indent = 0;
185
 
186
         // Scan the notes content line by line looking for table parameters and counting
187
         // the number of table rows (which is not specified as an explicit table parameter).
188
         int s_ElementsConsumedByTableParams = 0;
189
         foreach(string s in EA_DocGenTable)
190
         {
191
            if (s.Length > 0 && s != "\n" && s != "\r" )
192
            {
2106 ghuddy 193
               if (gotTitle == false && s.StartsWith("title="))
2104 ghuddy 194
               {
195
                  s_ElementsConsumedByTableParams++;
2106 ghuddy 196
                  tableTitle = EA_DocGenOptions.getOptionValue(s, tableTitle);
2104 ghuddy 197
                  gotTitle = true;
2106 ghuddy 198
                  //if (tableTitle == "")
199
                  //{
200
                  //   MessageBox.Show( "Table Element Serialisation Failed - Bad Title" );
201
                  //   break;
202
                  //}
2104 ghuddy 203
               }
204
 
2106 ghuddy 205
               else if (columnCount == 0 && s.StartsWith("columns="))
2104 ghuddy 206
               {
207
                  s_ElementsConsumedByTableParams++;
2106 ghuddy 208
                  columnCount = EA_DocGenOptions.getOptionValue(s, columnCount);
2104 ghuddy 209
                  if (columnCount == 0)
210
                  {
211
                     MessageBox.Show( "Table Element Serialisation Failed - bad column count" );
212
                     break;
213
                  }
214
               }
215
 
2106 ghuddy 216
               else if (gotSeperator == false && s.StartsWith("seperator="))
2104 ghuddy 217
               {
218
                  s_ElementsConsumedByTableParams++;
2106 ghuddy 219
                  seperator = EA_DocGenOptions.getOptionValue(s, seperator);
2104 ghuddy 220
                  gotSeperator = true;
221
               }
222
 
2106 ghuddy 223
               else if (gotWidths == false && s.StartsWith("widths="))
2104 ghuddy 224
               {
225
                  s_ElementsConsumedByTableParams++;
2106 ghuddy 226
                  gotWidths = true;
227
                  string optValStr = EA_DocGenOptions.getOptionValue(s, "");
2104 ghuddy 228
 
229
                  string width_delimStr = ",";
230
                  char [] width_delim = width_delimStr.ToCharArray();
231
 
232
                  string [] width_strings = optValStr.Split(width_delim, 50);
233
 
234
                  foreach (string ws in width_strings)
235
                  {
236
                     if (ws.Length > 0 && ws != "\n" && ws != "\r" )
237
                     {
238
                        colWidths.Add( System.Convert.ToDouble(ws) );
239
                     }
240
                  }
241
               }
2106 ghuddy 242
               else if (gotIndent == false && s.StartsWith("indent="))
2104 ghuddy 243
               {
244
                  s_ElementsConsumedByTableParams++;
2106 ghuddy 245
                  gotIndent = true;
246
                  indent = EA_DocGenOptions.getOptionValue(s, 0);
2104 ghuddy 247
               }
2106 ghuddy 248
               else if (gotBorders == false && s.StartsWith("borders="))
249
               {
250
                  s_ElementsConsumedByTableParams++;
251
                  gotBorders = true;
252
                  borders = EA_DocGenOptions.getOptionValue(s, borders);
253
               }
2104 ghuddy 254
               else
255
               {
256
                  rowCount++;
257
               }
258
            }
259
         }
260
 
2106 ghuddy 261
         if (columnCount > 0 && rowCount > 0)
2104 ghuddy 262
         {
263
            if (rowCount < 2)
264
            {
265
               MessageBox.Show( "Table Element Serialisation Failed - Insufficient Rows" );
266
            }
267
            else
268
            {
269
               // create the table in the word doc
2106 ghuddy 270
               int tableNum = Table_Create( tableTitle, borders, rowCount, columnCount );
271
               Word.Table table = createWordDoc.WordDocument.Tables[tableNum];
2104 ghuddy 272
               object center = Word.WdParagraphAlignment.wdAlignParagraphCenter;
273
 
274
               int col = 1;
275
               foreach (double d in colWidths)
276
               {
277
                  if (col <= columnCount)
2106 ghuddy 278
                     table.Columns[col].SetWidth( createWordDoc.WordApp.CentimetersToPoints((float)d), Word.WdRulerStyle.wdAdjustNone );
2104 ghuddy 279
                  col++;
280
               }
281
 
282
               if (indent > 0)
283
               {
284
                  table.Select();
285
                  while (indent > 0)
286
                  {
2106 ghuddy 287
                     createWordDoc.WordApp.Selection.Paragraphs.Indent();
2104 ghuddy 288
                     indent--;
289
                  }
290
               }
291
 
292
               // scan the element notes again to extract the cell content and add it to the
293
               // table we just created.
294
               int row = 1;
295
               col = 0;
296
               foreach(string s in EA_DocGenTable)
297
               {
298
                  if (s.Length > 0 && s != "\n" && s != "\r" )
299
                  {
300
                     if (s_ElementsConsumedByTableParams > 0)
301
                     {
302
                        s_ElementsConsumedByTableParams--;
303
                     }
304
                     else
305
                     {
306
                        delimStr = seperator.ToString();
307
                        delim = delimStr.ToCharArray();
308
                        cells = s.Split(delim,columnCount);
309
 
310
                        if (cells.Length != columnCount)
311
                        {
312
                           MessageBox.Show( "Table Element Serialisation Failed - Bad Row" );
313
                           break;
314
                        }
315
                        else
316
                        {
317
                           // serialise table row
318
                           for(col=1; col<=columnCount; col++)
319
                           {
320
                              cells[col-1].TrimStart( trimChars );
321
                              cells[col-1].TrimEnd( trimChars );
322
 
323
                              table.Cell(row,col).Range.Text = cells[col-1];
324
 
325
                              // special handling for heading row
326
                              if (row == 1)
327
                              {
328
                                 table.Cell(row,col).Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
329
                                 table.Cell(row,col).Range.Font.Bold = 1;
330
                              }
331
                           }
332
                           row++;
333
                        }                     
334
                     }
335
                  }
336
               }
337
            }
338
         }
339
         else
340
         {
341
            MessageBox.Show( "Table Element Serialisation Failed - Table Parameters Incomplete" );
342
         }
343
      }
344
 
2094 ghuddy 345
	}
346
}