Class: JLDrill::MainContext

Inherits:
Context::Context show all
Defined in:
lib/jldrill/contexts/MainContext.rb

Defined Under Namespace

Classes: MainWindowView

Instance Attribute Summary collapse

Attributes inherited from Context::Context

#mainView, #parent, #viewBridge

Instance Method Summary collapse

Methods inherited from Context::Context

#addView, #isEntered?, #onExit, #peekAtView, #setupViews

Constructor Details

#initialize(viewBridge) ⇒ MainContext

Returns a new instance of MainContext.



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
# File 'lib/jldrill/contexts/MainContext.rb', line 55

def initialize(viewBridge)
	super(viewBridge)
	@runCommandContext = RunCommandContext.new(viewBridge)
	@loadReferenceContext = LoadReferenceContext.new(viewBridge)
	@setOptionsContext = SetOptionsContext.new(viewBridge)
	@showStatisticsContext = ShowStatisticsContext.new(viewBridge)
	@getFilenameContext = GetFilenameContext.new(viewBridge)
	@getFilenameContext.directory = Config::resolveDataFile(Config::QUIZ_DIR)
	@addNewVocabularyContext = AddNewVocabularyContext.new(viewBridge)
	@editVocabularyContext = EditVocabularyContext.new(viewBridge)
	@displayQuizStatusContext = DisplayQuizStatusContext.new(viewBridge)
	@displayProblemContext = DisplayProblemContext.new(viewBridge)
	@showInfoContext = ShowInfoContext.new(viewBridge)
	@showAllVocabularyContext = ShowAllVocabularyContext.new(viewBridge)
	@showAboutContext = ShowAboutContext.new(viewBridge)
	@loadTanakaContext = LoadTanakaContext.new(viewBridge)
          @loadTatoebaContext = LoadTatoebaContext.new(viewBridge)
	@showExamplesContext = ShowExamplesContext.new(viewBridge)
          @loadQuizContext = LoadQuizContext.new(viewBridge)
          @loadKanjiContext = LoadKanjiContext.new(viewBridge)
          @appendFileContext = AppendFileContext.new(viewBridge)
	@reference = JEDictionary.new
	@kanji = KanjiFile.new
	@radicals = RadicalFile.new
          @kana = KanaFile.new
	@quiz = Quiz.new
          # The quiz doesn't need to be saved
          @quiz.setNeedsSave(false)
          @inTests = false
	@tanaka = Tanaka::Reference.new
          @tatoebaDB = Tatoeba::Database.new
          @deinflect = DeinflectionRulesFile.new

          @longEventPublisher = Context::Publisher.new(self)
end

Instance Attribute Details

#addNewVocabularyContextObject (readonly)

Returns the value of attribute addNewVocabularyContext.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def addNewVocabularyContext
  @addNewVocabularyContext
end

#appendFileContextObject (readonly)

Returns the value of attribute appendFileContext.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def appendFileContext
  @appendFileContext
end

#deinflectObject (readonly)

Returns the value of attribute deinflect.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def deinflect
  @deinflect
end

#displayProblemContextObject (readonly)

Returns the value of attribute displayProblemContext.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def displayProblemContext
  @displayProblemContext
end

#displayQuizStatusContextObject (readonly)

Returns the value of attribute displayQuizStatusContext.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def displayQuizStatusContext
  @displayQuizStatusContext
end

#editVocabularyContextObject (readonly)

Returns the value of attribute editVocabularyContext.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def editVocabularyContext
  @editVocabularyContext
end

#getFilenameContextObject (readonly)

Returns the value of attribute getFilenameContext.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def getFilenameContext
  @getFilenameContext
end

#inTestsObject

Returns the value of attribute inTests.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def inTests
  @inTests
end

#kanaObject (readonly)

Returns the value of attribute kana.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def kana
  @kana
end

#kanjiObject (readonly)

Returns the value of attribute kanji.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def kanji
  @kanji
end

#loadKanjiContextObject (readonly)

Returns the value of attribute loadKanjiContext.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def loadKanjiContext
  @loadKanjiContext
end

#loadQuizContextObject (readonly)

Returns the value of attribute loadQuizContext.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def loadQuizContext
  @loadQuizContext
end

#loadReferenceContextObject (readonly)

Returns the value of attribute loadReferenceContext.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def loadReferenceContext
  @loadReferenceContext
end

#loadTanakaContextObject (readonly)

Returns the value of attribute loadTanakaContext.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def loadTanakaContext
  @loadTanakaContext
end

#loadTatoebaContextObject (readonly)

Returns the value of attribute loadTatoebaContext.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def loadTatoebaContext
  @loadTatoebaContext
end

#longEventPublisherObject (readonly)

Returns the value of attribute longEventPublisher.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def longEventPublisher
  @longEventPublisher
end

#quizObject

Returns the value of attribute quiz.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def quiz
  @quiz
end

#radicalsObject (readonly)

Returns the value of attribute radicals.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def radicals
  @radicals
end

#referenceObject

Returns the value of attribute reference.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def reference
  @reference
end

#runCommandContextObject (readonly)

Returns the value of attribute runCommandContext.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def runCommandContext
  @runCommandContext
end

#setOptionsContextObject (readonly)

Returns the value of attribute setOptionsContext.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def setOptionsContext
  @setOptionsContext
end

#showAboutContextObject (readonly)

Returns the value of attribute showAboutContext.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def showAboutContext
  @showAboutContext
end

#showAllVocabularyContextObject (readonly)

Returns the value of attribute showAllVocabularyContext.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def showAllVocabularyContext
  @showAllVocabularyContext
end

#showExamplesContextObject (readonly)

Returns the value of attribute showExamplesContext.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def showExamplesContext
  @showExamplesContext
end

#showInfoContextObject (readonly)

Returns the value of attribute showInfoContext.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def showInfoContext
  @showInfoContext
end

#showStatisticsContextObject (readonly)

Returns the value of attribute showStatisticsContext.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def showStatisticsContext
  @showStatisticsContext
end

#tanakaObject (readonly)

Returns the value of attribute tanaka.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def tanaka
  @tanaka
end

#tatoebaDBObject (readonly)

Returns the value of attribute tatoebaDB.



40
41
42
# File 'lib/jldrill/contexts/MainContext.rb', line 40

def tatoebaDB
  @tatoebaDB
end

Instance Method Details

#addNewVocabularyObject



314
315
316
# File 'lib/jldrill/contexts/MainContext.rb', line 314

def addNewVocabulary
    @addNewVocabularyContext.enter(self) unless @addNewVocabularyContext.isEntered?
end

#appendFileObject



189
190
191
192
193
194
195
196
197
198
# File 'lib/jldrill/contexts/MainContext.rb', line 189

def appendFile
          if !@appendFileContext.isEntered?
              @appendFileContext.onExit do
                  if quiz.currentProblem.nil?
                      quiz.drill
                  end
              end
              @appendFileContext.enter(self, @quiz)
          end
end

#correctObject



393
394
395
396
397
398
# File 'lib/jldrill/contexts/MainContext.rb', line 393

def correct
          if !@quiz.currentProblem.nil? && !@quiz.currentProblem.displayOnly?
              @quiz.correct
              @quiz.drill
          end
end

#createNewObject



217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/jldrill/contexts/MainContext.rb', line 217

def createNew
    promptForSaveAnd do
        @quiz.reset
        # We need to resubscribe to the options in the new quiz
        # and realize that the options may have changed.
        @quiz.options.subscribe(self)
        optionsUpdated(@quiz.options)
        # New quizes don't need to be saved.
        @quiz.setNeedsSave(false)
        @quiz.updateLoad
    end
end

#createViewsObject



115
116
117
118
# File 'lib/jldrill/contexts/MainContext.rb', line 115

def createViews
	@mainWindowView = @viewBridge.MainWindowView.new(self)
	@mainView = @mainWindowView		    
end

#deleteItem(item) ⇒ Object



370
371
372
373
374
375
# File 'lib/jldrill/contexts/MainContext.rb', line 370

def deleteItem(item)
    if !item.nil?
        displayItem(item)
        deleteVocabulary
    end
end

#deleteVocabularyObject



327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/jldrill/contexts/MainContext.rb', line 327

def deleteVocabulary
		    if !@quiz.currentProblem.nil? &&
            !@quiz.currentProblem.preview?
        # Always show the answer before deleting an item
        showAnswer
        prompt = PromptForDeleteContext.new(@viewBridge)
        if prompt.enter(self) == prompt.yes
            item = @quiz.currentProblem.item
            @quiz.deleteItem(item)
            drill
        end
    end
end

#destroyViewsObject



120
121
122
123
124
# File 'lib/jldrill/contexts/MainContext.rb', line 120

def destroyViews
    @mainWindowView.destroy unless @mainWindowView.nil?
    @mainWindowView = nil
    @mainView = nil
end

#displayItem(item) ⇒ Object

Display the problem if it isn’t the current one



342
343
344
345
346
347
348
349
350
# File 'lib/jldrill/contexts/MainContext.rb', line 342

def displayItem(item)
    if !item.nil?
        if @quiz.currentProblem.nil? || 
                !@quiz.currentProblem.item.eql?(item)
            @quiz.displayProblem(item)
            showAnswer
        end
    end
end

#drillObject

Get a new problem in the drill without answering the current problem



389
390
391
# File 'lib/jldrill/contexts/MainContext.rb', line 389

def drill
    @quiz.drill
end

#editItem(item) ⇒ Object



363
364
365
366
367
368
# File 'lib/jldrill/contexts/MainContext.rb', line 363

def editItem(item)
    if !item.nil?
        displayItem(item)
        editVocabulary
    end
end

#editVocabularyObject



318
319
320
321
322
323
324
325
# File 'lib/jldrill/contexts/MainContext.rb', line 318

def editVocabulary
    if !@quiz.currentProblem.nil? &&
                  !@quiz.currentProblem.preview?
              # Always show the answer before editing the problem
              showAnswer
  		    @editVocabularyContext.enter(self) unless @editVocabularyContext.isEntered?
  		end
end

#enter(parent) ⇒ Object



132
133
134
135
136
137
138
139
140
# File 'lib/jldrill/contexts/MainContext.rb', line 132

def enter(parent)
	super(parent)
	@runCommandContext.enter(self)
	@displayProblemContext.enter(self)
	@displayQuizStatusContext.enter(self)
          parseCommandLineOptions
          @quiz.options.subscribe(self)
          loadKanji unless @inTests
end

#exampleDBObject



249
250
251
252
253
254
255
# File 'lib/jldrill/contexts/MainContext.rb', line 249

def exampleDB
    if @tanaka.loaded?
        return @tanaka
    else
        return @tatoebaDB
    end
end

#exitObject



142
143
144
145
146
147
148
149
# File 'lib/jldrill/contexts/MainContext.rb', line 142

def exit
	@runCommandContext.exit
    @displayQuizStatusContext.exit 
    @displayProblemContext.exit 
          @quiz.options.unsubscribe(self)
	@parent.exit
	super
end

#incorrectObject



400
401
402
403
404
405
# File 'lib/jldrill/contexts/MainContext.rb', line 400

def incorrect
          if !@quiz.currentProblem.nil? && !@quiz.currentProblem.displayOnly?
              @quiz.incorrect
              @quiz.drill
          end
end

#learnObject



407
408
409
410
411
412
# File 'lib/jldrill/contexts/MainContext.rb', line 407

def learn
    if !@quiz.currentProblem.nil? && !@quiz.currentProblem.displayOnly?
        @quiz.learn
        @quiz.drill
    end
end

#loadExamplesObject



291
292
293
# File 'lib/jldrill/contexts/MainContext.rb', line 291

def loadExamples
    loadTatoeba
end

#loadKanjiObject



296
297
298
299
300
# File 'lib/jldrill/contexts/MainContext.rb', line 296

def loadKanji
          if !@loadKanjiContext.isEntered?
              @loadKanjiContext.enter(self, @kanji, @radicals, @kana)
          end
end

#loadReferenceObject



242
243
244
245
246
247
# File 'lib/jldrill/contexts/MainContext.rb', line 242

def loadReference
          if !@loadReferenceContext.isEntered?
              @loadReferenceContext.enter(self, @reference, @deinflect, 
                                          @quiz.options)
          end
end

#loadTanakaObject



257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/jldrill/contexts/MainContext.rb', line 257

def loadTanaka
	if @tanaka.loaded?
              if !@showExamplesContext.isEntered?
                  @showExamplesContext.enter(self)
              end
	else
              if !@loadTanakaContext.isEntered?
                  @loadTanakaContext.onExit do
                      @showExamplesContext.enter(self)
                  end
                  @loadTanakaContext.enter(self, @tanaka, @quiz.options)
              end
	end
end

#loadTatoebaObject



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/jldrill/contexts/MainContext.rb', line 272

def loadTatoeba
	if @tatoebaDB.loaded?(@quiz.options)
              if !@showExamplesContext.isEntered?
                  @showExamplesContext.enter(self)
              end
	else
              if !@loadTatoebaContext.isEntered?
                  @loadTatoebaContext.onExit do
                      if !@showExamplesContext.isEntered?
                          @showExamplesContext.enter(self)
                      else
                          @showExamplesContext.newProblemUpdated(@quiz.currentProblem)
                      end
                  end
                  @loadTatoebaContext.enter(self, @tatoebaDB)
              end
	end
end

#openObject



230
231
232
233
234
# File 'lib/jldrill/contexts/MainContext.rb', line 230

def open
    promptForSaveAnd do
        openFile
    end
end

#openFile(filename = nil) ⇒ Object



178
179
180
181
182
183
184
185
186
187
# File 'lib/jldrill/contexts/MainContext.rb', line 178

def openFile(filename = nil)
          if !@loadQuizContext.isEntered?
              @loadQuizContext.onExit do
                  @quiz.options.subscribe(self)
                  optionsUpdated(@quiz.options)
                  @quiz.drill
              end
              @loadQuizContext.enter(self, @quiz, filename)
          end
end

#optionsUpdated(options) ⇒ Object



151
152
153
154
155
# File 'lib/jldrill/contexts/MainContext.rb', line 151

def optionsUpdated(options)
    if options.autoloadDic
        loadReference
    end
end

#parseCommandLineOptionsObject



126
127
128
129
130
# File 'lib/jldrill/contexts/MainContext.rb', line 126

def parseCommandLineOptions
    if ARGV.size == 1
        openFile(ARGV[0])
    end
end

#previewItem(item) ⇒ Object

Preview an item that doesn’t currently exist in the quiz



353
354
355
356
357
358
359
360
361
# File 'lib/jldrill/contexts/MainContext.rb', line 353

def previewItem(item)
    if !item.nil?
        if @quiz.currentProblem.nil? || 
                !@quiz.currentProblem.item.eql?(item)
            @quiz.previewProblem(item)
            showAnswer
        end
    end
end

#promptForSaveAnd(&block) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/jldrill/contexts/MainContext.rb', line 200

def promptForSaveAnd(&block)
    if @quiz.needsSave?
        promptForSave = PromptForSaveContext.new(@viewBridge) 
              result = promptForSave.enter(self)
        if result == promptForSave.yes
                  save
            block.call
              elsif result == promptForSave.no
                  block.call
              else
                  # Cancel... Do nothing
        end
    else
        block.call
    end
end

#quitObject



236
237
238
239
240
# File 'lib/jldrill/contexts/MainContext.rb', line 236

def quit
    promptForSaveAnd do
        exit
    end
end

#removeDupsObject



414
415
416
417
418
# File 'lib/jldrill/contexts/MainContext.rb', line 414

def removeDups
    if !quiz.nil?
        @quiz.contents.removeDuplicates
    end
end

#resetObject



420
421
422
# File 'lib/jldrill/contexts/MainContext.rb', line 420

def reset
    @quiz.resetContents
end

#saveObject



157
158
159
160
161
162
163
164
165
166
# File 'lib/jldrill/contexts/MainContext.rb', line 157

def save
    if @quiz.file.empty?
        saveAs
    else
  		    if !@quiz.save
                  print "Error: Can't save.  Try again.\n"
                  saveAs
              end
  		end
end

#saveAsObject



168
169
170
171
172
173
174
175
176
# File 'lib/jldrill/contexts/MainContext.rb', line 168

def saveAs
    filename = @getFilenameContext.enter(self, GetFilenameContext::SAVE)
    if !filename.nil?
        @quiz.file = filename
        while !@quiz.save
                  print "Error: Can't save.  Try again.\n"
              end
    end
end

#setOptionsObject



302
303
304
# File 'lib/jldrill/contexts/MainContext.rb', line 302

def setOptions
    @setOptionsContext.enter(self) unless @setOptionsContext.isEntered?
end

#setReviewMode(bool) ⇒ Object



310
311
312
# File 'lib/jldrill/contexts/MainContext.rb', line 310

def setReviewMode(bool)
    @quiz.options.reviewMode = bool unless @quiz.nil?
end

#showAboutObject



436
437
438
# File 'lib/jldrill/contexts/MainContext.rb', line 436

def showAbout
    @showAboutContext.enter(self) unless @showAboutContext.isEntered?
end

#showAcknowlegementsObject



428
429
430
# File 'lib/jldrill/contexts/MainContext.rb', line 428

def showAcknowlegements
    @showInfoContext.enter(self, Acknowlegements) unless @showInfoContext.isEntered?
end

#showAllVocabularyObject



432
433
434
# File 'lib/jldrill/contexts/MainContext.rb', line 432

def showAllVocabulary
    @showAllVocabularyContext.enter(self) unless @showAllVocabularyContext.isEntered?
end

#showAnswerObject



381
382
383
384
385
386
# File 'lib/jldrill/contexts/MainContext.rb', line 381

def showAnswer
          if !@quiz.currentProblem.nil?
              @displayProblemContext.showAnswer if @displayProblemContext.isEntered?
              @showExamplesContext.showAnswer if @showExamplesContext.isEntered?
          end
end

#showQuizInfoObject



424
425
426
# File 'lib/jldrill/contexts/MainContext.rb', line 424

def showQuizInfo
    @showInfoContext.enter(self, @quiz.info) unless @showInfoContext.isEntered?
end

#showStatisticsObject



306
307
308
# File 'lib/jldrill/contexts/MainContext.rb', line 306

def showStatistics
    @showStatisticsContext.enter(self) unless @showStatisticsContext.isEntered?
end

#startLongEventObject



440
441
442
443
# File 'lib/jldrill/contexts/MainContext.rb', line 440

def startLongEvent()
    @mainView.showBusy(true)
    @longEventPublisher.update("startLongEvent")
end

#stopLongEventObject



445
446
447
448
# File 'lib/jldrill/contexts/MainContext.rb', line 445

def stopLongEvent()
    @mainView.showBusy(false)
    @longEventPublisher.update("stopLongEvent")
end

#updateQuizStatusObject



377
378
379
# File 'lib/jldrill/contexts/MainContext.rb', line 377

def updateQuizStatus
    @displayQuizStatusContext.quizUpdated(@quiz) if @displayQuizStatusContext.isEntered?
end