Subversion Repositories DevTools

Rev

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