summaryrefslogtreecommitdiff
path: root/wizards/com/sun/star/wizards/agenda/AgendaDocument.py
blob: b96fe44cf15046253e9ac2f57e9bed614b0ff9cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This file incorporates work covered by the following license notice:
#
#   Licensed to the Apache Software Foundation (ASF) under one or more
#   contributor license agreements. See the NOTICE file distributed
#   with this work for additional information regarding copyright
#   ownership. The ASF licenses this file to you under the Apache
#   License, Version 2.0 (the "License"); you may not use this file
#   except in compliance with the License. You may obtain a copy of
#   the License at http://www.apache.org/licenses/LICENSE-2.0 .
#
import uno
import traceback
from ..text.TextElement import TextElement
from ..text.TextDocument import TextDocument
from ..text.TextSectionHandler import TextSectionHandler
from ..common.FileAccess import FileAccess

from datetime import datetime

from com.sun.star.text.PlaceholderType import TEXT
from com.sun.star.i18n.NumberFormatIndex import TIME_HHMM, DATE_SYSTEM_LONG

'''
The classes here implement the whole document-functionality of the agenda wizard:
the live-preview and the final "creation" of the document,
when the user clicks "finish". <br/>
<br/>
<h2>Some terminology:<h2/>
items are names or headings. we don't make any distinction.

<br/>
The Agenda Template is used as general "controller"
of the whole document, whereas the two child-classes ItemsTable
and TopicsTable control the item tables (note plural!) and the
topics table (note singular).<br/>
<br/>
Other small classes are used to abstract the handling of cells and text and we
try to use them as components.
<br/><br/>
We tried to keep the Agenda Template as flexible as possible, though there
must be many limitations, because it is generated dynamically.<br/><br/>
To keep the template flexible the following decisions were made:<br/>
1. Item tables.<br/>
1.a. there might be arbitrary number of Item tables.<br/>
1.b. Item tables design (bordewr, background) is arbitrary.<br/>
1.c. Items text styles are individual,
and use stylelist styles with predefined names.<br/>
As result the following limitations:<br/>
Pairs of Name->value for each item.<br/>
Tables contain *only* those pairs.<br/>
2. Topics table.<br/>
2.a. arbitrary structure.<br/>
2.b. design is arbitrary.<br/>
As result the following limitations:<br/>
No column merge is allowed.<br/>
One compulsory Heading row.<br/>
<br/><br/>
To let the template be flexible, we use a kind of "detection": we look where
the items are read the design of each table, re-applying it after writing the
table.self.xTextDocument
<br/><br/>
A note about threads:<br/>
Many methods here are synchronized, in order to avoid collision made by
events fired too often.
'''
class AgendaDocument(TextDocument):

    '''
    constructor. The document is *not* loaded here.
    only some formal members are set.
    '''

    def __init__(self,  xmsf, agenda, resources, templateConsts, listener):
        super(AgendaDocument,self).__init__(xmsf,listener, None,
            "WIZARD_LIVE_PREVIEW")
        self.agenda = agenda
        self.templateConsts = templateConsts
        self.resources = resources
        self.itemsMap = {}
        self.allItems = []

    def load(self, templateURL):
        # Each template is duplicated. aw-XXX.ott is the template itself
        # and XXX.ott is a section link.
        self.template = self.calcTemplateName(templateURL)
        self.loadAsPreview(templateURL, False)
        self.xFrame.ComponentWindow.Enable = False
        self.xTextDocument.lockControllers()
        self.initialize()
        self.initializeData()
        self.xTextDocument.unlockControllers()

    '''
    The agenda templates are in format of aw-XXX.ott
    the templates name is then XXX.ott.
    This method calculates it.
    '''

    def calcTemplateName(self, url):
        return FileAccess.connectURLs(
            FileAccess.getParentDir(url), FileAccess.getFilename(url)[3:])

    '''synchronize the document to the model.<br/>
    this method rewrites all titles, item tables , and the topics table-
    thus synchronizing the document to the data model (CGAgenda).
    information (it is only actualized on save) the given list
    supplies this information.
    '''

    def initializeData(self):
        for i in self.itemsTables:
            try:
                i.write()
            except Exception:
                traceback.print_exc()

        self.redrawTitle("txtTitle")
        self.redrawTitle("txtDate")
        self.redrawTitle("txtTime")
        self.redrawTitle("cbLocation")

    '''
    redraws/rewrites the table which contains the given item
    This method is called when the user checks/unchecks an item.
    The table is being found, in which the item is, and redrawn.
    '''

    def redraw(self, itemName):
        self.xTextDocument.lockControllers()
        try:
            # get the table in which the item is...
            itemsTable = self.itemsMap[itemName]
            # rewrite the table.
            itemsTable.write()
        except Exception:
            traceback.print_exc()
        self.xTextDocument.unlockControllers()

    '''
    checks the data model if the
    item corresponding to the given string should be shown
    '''

    def isShowItem(self, itemName):
        if itemName == self.templateConsts.FILLIN_MEETING_TYPE:
            return self.agenda.cp_ShowMeetingType
        elif itemName == self.templateConsts.FILLIN_READ:
            return self.agenda.cp_ShowRead
        elif itemName == self.templateConsts.FILLIN_BRING:
            return self.agenda.cp_ShowBring
        elif itemName == self.templateConsts.FILLIN_NOTES:
            return self.agenda.cp_ShowNotes
        elif itemName == self.templateConsts.FILLIN_FACILITATOR:
            return self.agenda.cp_ShowFacilitator
        elif itemName == self.templateConsts.FILLIN_TIMEKEEPER:
            return self.agenda.cp_ShowTimekeeper
        elif itemName == self.templateConsts.FILLIN_NOTETAKER:
            return self.agenda.cp_ShowNotetaker
        elif itemName == self.templateConsts.FILLIN_PARTICIPANTS:
            return self.agenda.cp_ShowAttendees
        elif itemName == self.templateConsts.FILLIN_CALLED_BY:
            return self.agenda.cp_ShowCalledBy
        elif itemName == self.templateConsts.FILLIN_OBSERVERS:
            return self.agenda.cp_ShowObservers
        elif itemName == self.templateConsts.FILLIN_RESOURCE_PERSONS:
            return self.agenda.cp_ShowResourcePersons
        else:
            raise ValueError("No such item")

    '''itemsCache is a Map containing all agenda item. These are object which
    "write themselves" to the table, given a table cursor.
    A cache is used in order to reuse the objects, instead of recreate them.
    This method fills the cache will all items objects (names and headings).
    '''

    def initItemsCache(self):
        self.itemsCache = {}
        # Headings
        self.itemsCache[
                self.templateConsts.FILLIN_MEETING_TYPE] = \
            AgendaItem(self.templateConsts.FILLIN_MEETING_TYPE,
                self.resources.itemMeetingType,
                PlaceholderElement(
                    self.resources.reschkMeetingTitle_value,
                    self.resources.resPlaceHolderHint, self.xTextDocument))
        self.itemsCache[
                self.templateConsts.FILLIN_BRING] = \
            AgendaItem(self.templateConsts.FILLIN_BRING,
                self.resources.itemBring,
                PlaceholderElement (
                    self.resources.reschkBring_value,
                    self.resources.resPlaceHolderHint, self.xTextDocument))
        self.itemsCache[
                self.templateConsts.FILLIN_READ] = \
            AgendaItem (self.templateConsts.FILLIN_READ,
                self.resources.itemRead,
                PlaceholderElement (
                    self.resources.reschkRead_value,
                    self.resources.resPlaceHolderHint, self.xTextDocument))
        self.itemsCache[
                self.templateConsts.FILLIN_NOTES] = \
            AgendaItem (self.templateConsts.FILLIN_NOTES,
                self.resources.itemNote,
                PlaceholderElement (
                    self.resources.reschkNotes_value,
                    self.resources.resPlaceHolderHint, self.xTextDocument))

        # Names
        self.itemsCache[
                self.templateConsts.FILLIN_CALLED_BY] = \
            AgendaItem(self.templateConsts.FILLIN_CALLED_BY,
                self.resources.itemCalledBy,
                PlaceholderElement (
                    self.resources.reschkConvenedBy_value,
                    self.resources.resPlaceHolderHint, self.xTextDocument))
        self.itemsCache[
                self.templateConsts.FILLIN_FACILITATOR] = \
            AgendaItem(self.templateConsts.FILLIN_FACILITATOR,
                self.resources.itemFacilitator,
                PlaceholderElement (
                    self.resources.reschkPresiding_value,
                    self.resources.resPlaceHolderHint, self.xTextDocument))
        self.itemsCache[
                self.templateConsts.FILLIN_PARTICIPANTS] = \
            AgendaItem(self.templateConsts.FILLIN_PARTICIPANTS,
                self.resources.itemAttendees,
                PlaceholderElement(
                    self.resources.reschkAttendees_value,
                    self.resources.resPlaceHolderHint, self.xTextDocument))
        self.itemsCache[
                self.templateConsts.FILLIN_NOTETAKER] = \
            AgendaItem(self.templateConsts.FILLIN_NOTETAKER,
                self.resources.itemNotetaker,
                PlaceholderElement(
                    self.resources.reschkNoteTaker_value,
                    self.resources.resPlaceHolderHint, self.xTextDocument))
        self.itemsCache[
                self.templateConsts.FILLIN_TIMEKEEPER] = \
            AgendaItem(self.templateConsts.FILLIN_TIMEKEEPER,
                self.resources.itemTimekeeper,
                PlaceholderElement(
                    self.resources.reschkTimekeeper_value,
                    self.resources.resPlaceHolderHint, self.xTextDocument))
        self.itemsCache[
                self.templateConsts.FILLIN_OBSERVERS] = \
            AgendaItem(self.templateConsts.FILLIN_OBSERVERS,
                self.resources.itemObservers,
                PlaceholderElement(
                    self.resources.reschkObservers_value,
                    self.resources.resPlaceHolderHint, self.xTextDocument))
        self.itemsCache[
                self.templateConsts.FILLIN_RESOURCE_PERSONS] = \
            AgendaItem(self.templateConsts.FILLIN_RESOURCE_PERSONS,
                self.resources.itemResource,
                PlaceholderElement(
                    self.resources.reschkResourcePersons_value,
                    self.resources.resPlaceHolderHint, self.xTextDocument))

    '''Initializes a template.<br/>
    This method does the following tasks:<br/>
    get a Time and Date format for the document, and retrieve the null
    date of the document (which is document-specific).<br/>
    Initializes the Items Cache map.
    Analyses the document:<br/>
    -find all "filled-ins" (appear as &gt;xxx&lt; in the document).
    -analyze all items sections (and the tables in them).
    -locate the titles and actualize them
    -analyze the topics table
    '''

    def initialize(self):
        '''
        Get the default locale of the document,
        and create the date and time formatters.
        '''
        self.dateUtils = self.DateUtils(self.xMSF, self.xTextDocument)
        self.formatter = self.dateUtils.formatter
        self.dateFormat = self.dateUtils.getFormat(DATE_SYSTEM_LONG)
        self.timeFormat = self.dateUtils.getFormat(TIME_HHMM)

        self.initItemsCache()
        self.allItems = self.searchFillInItems(0)
        self.initializeTitles()
        self.initializeItemsSections()
        self.textSectionHandler = TextSectionHandler(
            self.xTextDocument, self.xTextDocument)
        self.topics = Topics(self)

    '''
    locates the titles (name, location, date, time)
    and saves a reference to their Text ranges.
    '''

    def initializeTitles(self):
        auxList = []
        for i in self.allItems:
            text = i.String.lstrip().lower()
            if text == self.templateConsts.FILLIN_TITLE:
                self.teTitle = PlaceholderTextElement(
                    i, self.resources.resPlaceHolderTitle,
                    self.resources.resPlaceHolderHint, self.xTextDocument)
                self.trTitle = i
            elif text == self.templateConsts.FILLIN_DATE:
                self.teDate = PlaceholderTextElement(
                    i, self.resources.resPlaceHolderDate,
                    self.resources.resPlaceHolderHint, self.xTextDocument)
                self.trDate = i
            elif text == self.templateConsts.FILLIN_TIME:
                self.teTime = PlaceholderTextElement(
                    i, self.resources.resPlaceHolderTime,
                    self.resources.resPlaceHolderHint, self.xTextDocument)
                self.trTime = i
            elif text == self.templateConsts.FILLIN_LOCATION:
                self.teLocation = PlaceholderTextElement(
                    i, self.resources.resPlaceHolderLocation,
                    self.resources.resPlaceHolderHint, self.xTextDocument)
                self.trLocation = i
            else:
                auxList.append(i)
        self.allItems = auxList

    '''
    analyze the item sections in the template.
    delegates the analyze of each table to the ItemsTable class.
    '''

    def initializeItemsSections(self):
        sections = self.getSections(
        self.xTextDocument, self.templateConsts.SECTION_ITEMS)
        # for each section - there is a table...
        self.itemsTables = []
        for i in sections:
            try:
                self.itemsTables.append(
                    ItemsTable(self.getSection(i), self.getTable(i), self))
            except Exception:
                traceback.print_exc()
                raise AttributeError (
                    "Fatal Error while initializing \
                    Template: items table in section " + i)


    def getSections(self, document, s):
        allSections = document.TextSections.ElementNames
        return self.getNamesWhichStartWith(allSections, s)

    def getSection(self, name):
        return self.xTextDocument.TextSections.getByName(name)

    def getTable(self, name):
        return self.xTextDocument.TextTables.getByName(name)

    def redrawTitle(self, controlName):
        try:
            if controlName == "txtTitle":
                self.teTitle.placeHolderText = self.agenda.cp_Title
                self.teTitle.write(self.trTitle)
            elif controlName == "txtDate":
                self.teDate.placeHolderText = \
                    self.getDateString(self.agenda.cp_Date)
                self.teDate.write(self.trDate)
            elif controlName == "txtTime":
                self.teTime.placeHolderText = self.agenda.cp_Time
                self.teTime.write(self.trTime)
            elif controlName == "cbLocation":
                self.teLocation.placeHolderText = self.agenda.cp_Location
                self.teLocation.write(self.trLocation)
            else:
                raise Exception("No such title control...")
        except Exception:
            traceback.print_exc()

    def getDateString(self, date):
        if not date:
            return ""
        dateObject = datetime.strptime(date, '%d/%m/%y').date()
        return self.dateUtils.format(self.dateFormat, dateObject)

    def finish(self, topics):
        self.createMinutes(topics)
        self.deleteHiddenSections()
        self.textSectionHandler.removeAllTextSections()

    '''
    hidden sections exist when an item's section is hidden because the
    user specified not to display any items which it contains.
    When finishing the wizard removes this sections
    entirely from the document.
    '''

    def deleteHiddenSections(self):
        allSections = self.xTextDocument.TextSections.ElementNames
        try:
            for i in allSections:
                self.section = self.getSection(i)
                visible = bool(self.section.IsVisible)
                if not visible:
                    self.section.Anchor.String = ""

        except Exception:
            traceback.print_exc()

    '''
    create the minutes for the given topics or remove the minutes
    section from the document.
    If no topics are supplied, or the user specified not to create minutes,
    the minutes section will be removed,
    @param topicsData supplies PropertyValue arrays containing
    the values for the topics.
    '''

    def createMinutes(self, topicsData):
        # if the minutes section should be removed (the
        # user did not check "create minutes")
        if not self.agenda.cp_IncludeMinutes \
                or len(topicsData) <= 1:
            try:
                minutesAllSection = self.getSection(
                    self.templateConsts.SECTION_MINUTES_ALL)
                minutesAllSection.Anchor.String = ""
            except Exception:
                traceback.print_exc()

        # the user checked "create minutes"
        else:
            try:
                topicStartTime = int(self.agenda.cp_Time)
                # first I replace the minutes titles...
                self.items = self.searchFillInItems()
                itemIndex = 0
                for item in self.items:
                    itemText = item.String.lstrip().lower()
                    if itemText == \
                            self.templateConsts.FILLIN_MINUTES_TITLE:
                        self.fillMinutesItem(
                            item, self.agenda.cp_Title,
                            self.resources.resPlaceHolderTitle)
                    elif itemText == \
                            self.templateConsts.FILLIN_MINUTES_LOCATION:
                        self.fillMinutesItem(
                            item, self.agenda.cp_Location,
                            self.resources.resPlaceHolderLocation)
                    elif itemText == \
                            self.templateConsts.FILLIN_MINUTES_DATE:
                        self.fillMinutesItem(
                            item, getDateString(self.agenda.cp_Date),
                            self.resources.resPlaceHolderDate)
                    elif itemText == \
                            self.templateConsts.FILLIN_MINUTES_TIME:
                        self.fillMinutesItem( item, self.agenda.cp_Time,
                            self.resources.resPlaceHolderTime)

                self.items.clear()
                '''
                now add minutes for each topic.
                The template contains *one* minutes section, so
                we first use the one available, and then add a one...
                topics data has *always* an empty topic at the end...
                '''

                for i in xrange(len(topicsData) - 1):
                    topic = topicsData[i]
                    items = self.searchFillInItems()
                    itemIndex = 0
                    for item in items:
                        itemText = item.String.lstrip().lower()
                        if itemText == \
                                self.templateConsts.FILLIN_MINUTE_NUM:
                            self.fillMinutesItem(item, topic[0].Value, "")
                        elif itemText == \
                                self.templateConsts.FILLIN_MINUTE_TOPIC:
                            self.fillMinutesItem(item, topic[1].Value, "")
                        elif itemText == \
                                self.templateConsts.FILLIN_MINUTE_RESPONSIBLE:
                            self.fillMinutesItem(item, topic[2].Value, "")
                        elif itemText == \
                                self.templateConsts.FILLIN_MINUTE_TIME:
                            topicTime = 0
                            try:
                                topicTime = topic[3].Value
                            except Exception:
                                pass

                            '''
                            if the topic has no time, we do not
                            display any time here.
                            '''
                            if topicTime == 0 or topicStartTime == 0:
                                time = topic[3].Value
                            else:
                                time = str(topicStartTime) + " - "
                                topicStartTime += topicTime * 1000
                                time += str(topicStartTime)

                            self.fillMinutesItem(item, time, "")

                    self.textSectionHandler.removeTextSectionbyName(
                        self.templateConsts.SECTION_MINUTES)
                    # after the last section we do not insert a one.
                    if i < len(topicsData) - 2:
                        self.textSectionHandler.insertTextSection(
                            self.templateConsts.SECTION_MINUTES,
                            self.template, False)

            except Exception:
                traceback.print_exc()

    '''given a text range and a text, fills the given
    text range with the given text.
    If the given text is empty, uses a placeholder with the given
    placeholder text.
    @param range text range to fill
    @param text the text to fill to the text range object.
    @param placeholder the placeholder text to use, if the
    text argument is empty (null or "")
    '''

    def fillMinutesItem(self, Range, text, placeholder):
        paraStyle = Range.ParaStyleName
        Range.setString(text)
        Range.ParaStyleName = paraStyle
        if text is None or text == "":
            if placeholder is not None and not placeholder == "":
                placeHolder = self.createPlaceHolder(
                    self.xTextDocument, placeholder,
                    self.resources.resPlaceHolderHint)
                try:
                    Range.Start.Text.insertTextContent(
                        Range.Start, placeHolder, True)
                except Exception:
                    traceback.print_exc()

    '''
    creates a placeholder field with the given text and given hint.
    '''

    @classmethod
    def createPlaceHolder(self, xmsf, ph, hint):
        try:
            placeHolder =  xmsf.createInstance(
                "com.sun.star.text.TextField.JumpEdit")
        except Exception:
            traceback.print_exc()
            return None

        placeHolder.PlaceHolder = ph
        placeHolder.Hint = hint
        placeHolder.PlaceHolderType = uno.Any("short",TEXT)
        return placeHolder

    def getNamesWhichStartWith(self, allNames, prefix):
        v = []
        for i in allNames:
            if i.startswith(prefix):
                v.append(i)
        return v

    '''
    Convenience method for inserting some cells into a table.
    '''

    @classmethod
    def insertTableRows(self, table, start, count):
        rows = table.Rows
        rows.insertByIndex(start, count)

    '''
    returns the rows count of this table, assuming
    there is no vertical merged cells.
    '''

    @classmethod
    def getRowCount(self, table):
        cells = table.getCellNames()
        return int(cells[len(cells) - 1][1:])

class ItemsTable(object):
    '''
    the items in the table.
    '''
    items = []
    table = None

    def __init__(self, section, table, agenda):
        self.agenda = agenda
        ItemsTable.table = table
        self.section = section
        self.items = []
        '''
        go through all <*> items in the document
        and each one if it is in this table.
        If they are, register them to belong here, notice their order
        and remove them from the list of all <*> items, so the next
        search will be faster.
        '''
        aux = []
        for item in self.agenda.allItems:
            t = item.TextTable
            if t == ItemsTable.table:
                iText = item.String.lower().lstrip()
                ai = self.agenda.itemsCache[iText]
                if ai is not None:
                    self.items.append(ai)
                    self.agenda.itemsMap[iText] = self
            else:
                aux.append(item)
        self.agenda.allItems = aux

    '''
    link the section to the template. this will restore the original table
    with all the items.<br/>
    then break the link, to make the section editable.<br/>
    then, starting at cell one, write all items that should be visible.
    then clear the rest and remove obsolete rows.
    If no items are visible, hide the section.
    '''

    def write(self):
        name = self.section.Name
        # link and unlink the section to the template.
        self.agenda.textSectionHandler.linkSectiontoTemplate(
            self.agenda.template, name, self.section)
        self.agenda.textSectionHandler.breakLinkOfTextSection(
            self.section)
        # we need to get an instance after linking

        ItemsTable.table = self.agenda.getTable(name)
        self.section = self.agenda.getSection(name)
        cursor = ItemsTable.table.createCursorByCellName("A1")
        # should this section be visible?
        visible = False
        # write items
        cellName = ""
        '''
        now go through all items that belong to this
        table. Check each one against the model. If it should
        be displayed, call its write method.
        All items are of type AgendaItem which means they write
        two cells to the table: a title (text) and a placeholder.
        see AgendaItem class below.
        '''
        for i in self.items:
            if self.agenda.isShowItem(i.name):
                visible = True
                i.table = ItemsTable.table
                i.write(cursor)
                # I store the cell name which was last written...
                cellName = cursor.RangeName
                cursor.goRight(1, False)

        if visible:
            boolean = True
        else:
            boolean = False
        self.section.IsVisible = boolean
        if not visible:
            return
            '''
            if the cell that was last written is the current cell,
            it means this is the end of the table, so we end here.
            (because after getting the cellName above,
            I call the goRight method.
            If it did not go right, it means it's the last cell.
            '''

        if cellName == cursor.RangeName:
            return
            '''
            if not, we continue and clear all cells until
            we are at the end of the row.
            '''

        while not cellName == cursor.RangeName and \
                not cursor.RangeName.startswith("A"):
            cell = ItemsTable.table.getCellByName(cursor.RangeName)
            cell.String = ""
            cellName = cursor.RangeName
            cursor.goRight(1, False)

        '''
        again: if we are at the end of the table, end here.
        '''
        if cellName == cursor.RangeName:
            return

        '''
        now before deleting i move the cursor up so it
        does not disappear, because it will crash office.
        '''
        cursor.gotoStart(False)

'''
This class handles the preview of the topics table.
You can call it the controller of the topics table.
It differs from ItemsTable in that it has no data model -
the update is done programmatically.<br/>
<br/>
The decision to make this class a class by its own
was done out of logic reasons and not design/functionality reasons,
since there is anyway only one instance of this class at runtime
it could have also be implemented in the AgendaDocument class
but for clarity and separation I decided to make a sub class for it.
'''

class Topics(object):
    '''Analyze the structure of the Topics table.
    The structure Must be as follows:<br>
    -One Header Row. <br>
    -arbitrary number of rows per topic <br>
    -arbitrary content in the topics row <br>
    -only soft formatting will be restored. <br>
    -the topic rows must repeat three times. <br>
    -in the topics rows, placeholders for number, topic, responsible,
    and duration must be placed.<br><br>
    A word about table format: to reconstruct the format of the table we hold
    to the following formats: first row (header), topic, and last row.
    We hold the format of the last row, because one might wish to give it
    a special format, other than the one on the bottom of each topic.
    The left and right borders of the whole table are, on the other side,
    part of the topics rows format, and need not be preserved separately.
    '''
    table = None
    lastRowFormat = []
    rowsPerTopic = None

    def __init__(self, agenda):
        self.firstRowFormat = []
        self.agenda = agenda
        self.writtenTopics = -1
        try:
            Topics.table = self.agenda.getTable(
                self.agenda.templateConsts.SECTION_TOPICS)
        except Exception:
            traceback.print_exc()
            raise AttributeError (
                "Fatal error while loading template: table " + \
                self.agenda.templateConsts.SECTION_TOPICS + " could not load.")

        '''
        first I store all <*> ranges
        which are in the topics table.
        I store each <*> range in this - the key
        is the cell it is in. Later when analyzing the topic,
        cell by cell, I check in this map to know
        if a cell contains a <*> or not.
        '''
        try:
            items = {}
            for i in self.agenda.allItems:
                t = i.TextTable
                if t == Topics.table:
                    cell = i.Cell
                    iText = cell.CellName
                    items[iText] = i

            '''
            in the topics table, there are always one
            title row and three topics defined.
            So no mutter how many rows a topic takes - we
            can restore its structure and format.
            '''
            rows = self.agenda.getRowCount(Topics.table)
            Topics.rowsPerTopic = int((rows - 1) / 3)

            firstCell = "A" + str(1 + Topics.rowsPerTopic + 1)
            afterLastCell = "A" + str(1 + (Topics.rowsPerTopic * 2) + 1)
            # go to the first row of the 2. topic

            cursor = Topics.table.createCursorByCellName(firstCell)
            # analyze the structure of the topic rows.
            while not cursor.RangeName == afterLastCell:
                cell = Topics.table.getCellByName(cursor.RangeName)
                # first I store the content and para style of the cell
                ae = TextElement(cell, cell.String)
                ae.write()

                # goto next cell.
                cursor.goRight(1, False)
        except Exception:
            traceback.print_exc()

    '''rewrites a single cell containing.
    This is used in order to refresh the topic/responsible/duration data
    in the preview document, in response to a change in the gui (by the user)
    Since the structure of the topics table is flexible,
    The Topics object, which analyzed the structure of the topics table upon
    initialization, refreshes the appropriate cell.
    '''
    def writeCell(self, row, column, data):
        # if the whole row should be written...
        if self.writtenTopics < row:
            self.writtenTopics += 1
            rows = self.agenda.getRowCount(Topics.table)
            reqRows = 1 + (row + 1) * Topics.rowsPerTopic
            firstRow = reqRows - Topics.rowsPerTopic + 1
            diff = reqRows - rows
            if diff > 0:
                # set the item's text...
                self.agenda.insertTableRows(Topics.table, rows, diff)
            column = 0
            cursor = Topics.table.createCursorByCellName("A" + str(firstRow))
        else:
            # calculate the table row.
            firstRow = 1 + (row * Topics.rowsPerTopic) + 1
            cursor = Topics.table.createCursorByCellName("A" + str(firstRow))

            # move the cursor to the needed cell...
            cursor.goRight(column, False)

        xc = Topics.table.getCellByName(cursor.RangeName)
        # and write it !
        te = TextElement(xc, data[column].Value)
        te.write()

    '''removes obsolete rows, reducing the
    topics table to the given number of topics.
    Note this method does only reducing - if
    the number of topics given is greater than the
    number of actual topics it does *not* add
    rows!
    Note also that the first topic will never be removed.
    If the table contains no topics, the whole section will
    be removed upon finishing.
    The reason for that is a "table-design" one: the first topic is
    maintained in order to be able to add rows with a design of this topic,
    and not of the header row.
    @param topics the number of topics the table should contain.
    @throws Exception
    '''

    def reduceDocumentTo(self, topics):
        # we never remove the first topic...
        if topics <= 0:
            topics = 1

        tableRows = Topics.table.Rows
        targetNumOfRows = topics * Topics.rowsPerTopic + 1
        if tableRows.Count > targetNumOfRows:
            tableRows.removeByIndex(
                targetNumOfRows, tableRows.Count - targetNumOfRows)

'''
A Text element which, if the text to write is empty (null or "")
inserts a placeholder instead.
'''

class PlaceholderTextElement(TextElement):

    def __init__(self, textRange, placeHolderText_, hint_, xmsf_):
        super(PlaceholderTextElement,self).__init__(textRange, "")

        self.text = placeHolderText_
        self.hint = hint_
        self.xmsf = xmsf_
        self.xTextContentList = []

    def write(self, textRange):
        textRange.String = self.placeHolderText
        if self.placeHolderText is None or self.placeHolderText == "":
            try:
                xTextContent = AgendaDocument.createPlaceHolder(
                    self.xmsf, self.text, self.hint)
                self.xTextContentList.append(xTextContent)
                textRange.Text.insertTextContent(
                    textRange.Start, xTextContent, True)
            except Exception:
                traceback.print_exc()
        else:
            if self.xTextContentList:
                for i in self.xTextContentList:
                    textRange.Text.removeTextContent(i)
                self.xTextContentList = []
'''
An Agenda element which writes no text, but inserts a placeholder, and formats
it using a ParaStyleName.
'''

class PlaceholderElement(object):

    def __init__(self, placeHolderText_, hint_,  textDocument):
        self.placeHolderText = placeHolderText_
        self.hint = hint_
        self.textDocument =  textDocument

    def write(self, textRange):
        try:
            xTextContent = AgendaDocument.createPlaceHolder(
                self.textDocument, self.placeHolderText, self.hint)
            textRange.Text.insertTextContent(
                textRange.Start, xTextContent, True)
        except Exception:
            traceback.print_exc()

'''
An implementation of AgendaElement which
gets as a parameter a table cursor, and writes
a text to the cell marked by this table cursor, and
a place holder to the next cell.
'''

class AgendaItem(object):

    def __init__(self, name_, te, f):
        self.name = name_
        self.field = f
        self.textElement = te

    def write(self, tableCursor):
        cellname = tableCursor.RangeName
        cell = ItemsTable.table.getCellByName(cellname)
        cell.String = self.textElement
        tableCursor.goRight(1, False)
        # second field is actually always null...
        # this is a preparation for adding placeholders.
        if self.field is not None:
            self.field.write(ItemsTable.table.getCellByName(
                tableCursor.RangeName))