Subversion Repositories DevTools

Rev

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