単語リストプログラム その13
今回は、単に今までのスクリプトをまとめるだけ。コメントがないので、何をしているかは、ここまでのところを参照してください。
require 'osx/cocoa'
require 'find'
$KCODE = 'UTF-8'
class AppController < OSX::NSObject
include OSX
ib_outlet :window, :tabView, :table1, :table2, :previewCheck, :previewText
ib_outlet :fileAryCtl1, :fileAryCtl2
ib_outlet :accEncView, :encPopupBtn
ib_outlet :startBtn1, :sortChoice1, :sortBtn1, :tokenField1, :typeField1, :wcTable1
ib_outlet :startBtn2, :sortChoice2, :sortBtn2, :tokenField2, :typeField2, :wcTable2
ib_outlet :wcAryCtl1, :wcAryCtl2
EncodingArray = [NSUTF8StringEncoding,NSWindowsCP1252StringEncoding,NSWindowsCP1250StringEncoding,NSASCIIStringEncoding,
NSShiftJISStringEncoding,NSJapaneseEUCStringEncoding,NSISO2022JPStringEncoding]
TableContentType = "TableContentType"
def awakeFromNib
@fileAryCtl = [@fileAryCtl1, @fileAryCtl2]
@wcAryCtl = [@wcAryCtl1, @wcAryCtl2]
@sortChoice = [@sortChoice1, @sortChoice2]
@tokenField = [@tokenField1, @tokenField2]
@typeField = [@typeField1, @typeField2]
@fileExt = Regexp.new("\\.(?:txt|doc|docx|rtf|rtfd|odt|sxw|pdf)",Regexp::IGNORECASE)
@tableID = {@table1.to_s => 0,@table2.to_s => 1}
@table1.registerForDraggedTypes([NSFilenamesPboardType,TableContentType])
@table1.setDraggingSourceOperationMask_forLocal(NSDragOperationEvery,true)
@table2.registerForDraggedTypes([NSFilenamesPboardType,TableContentType])
@table2.setDraggingSourceOperationMask_forLocal(NSDragOperationEvery,true)
end
def applicationDidFinishLaunching(application)
transformer = ArrayExistTransformer.init.alloc
NSValueTransformer.setValueTransformer_forName(transformer,"ArrayExistTransformer")
end
def addFiles(sender)
@addBtn = sender.alternateTitle.to_i # @addBtn = sender.tag.to_i
panel = NSOpenPanel.openPanel
panel.setCanChooseDirectories(true)
panel.setAllowsMultipleSelection(true)
panel.setMessage("Select file(s) to add to the list.")
panel.setAccessoryView(@accEncView)
panel.beginSheetForDirectory_file_types_modalForWindow_modalDelegate_didEndSelector_contextInfo(nil,
nil,
[:txt,:doc,:docx,:rtf,:rtfd,:odt,:sxw,:pdf],
@window,
self,
"addFilePanelDidEnd_returnCode_contextInfo",
nil)
end
ib_action :addFiles
def addFilePanelDidEnd_returnCode_contextInfo(panel,returnCode,info)
if returnCode == 1
addFilesToTable(@addBtn,@encPopupBtn.indexOfSelectedItem,panel.filenames)
end
end
def addFilesToTable(aryID,encode,files)
fileAry = Array.new
files.each do |dir|
Find.find(dir.to_s) do |path|
if @fileExt =~ File.extname(path)
currentEncode = File.extname(path).downcase == ".txt" ? encode : -1
fileAry << {"fileName" => File.basename(path),"encoding" => currentEncode,"fullPath" => path}
end
end
end
@fileAryCtl[aryID].addUniqFiles(fileAry)
end
def clearTable(sender)
clearBtn = sender.alternateTitle.to_i # clearBtn = sender.tag.to_i
alert = NSAlert.alertWithMessageText_defaultButton_alternateButton_otherButton_informativeTextWithFormat("Are you sure you want to clear the table?",
"Yes",
"No",
nil,
"This cannot be undone.")
userChoice = alert.runModal
case userChoice
when 0
return
when 1
@fileAryCtl[clearBtn].removeObjectsAtArrangedObjectIndexes(NSIndexSet.indexSetWithIndexesInRange([0,@fileAryCtl[clearBtn].arrangedObjects.length]))
end
end
ib_action :clearTable
def createWordList(sender)
words = Hash.new(0)
aryID = sender.alternateTitle.to_i # aryID = sender.tag.to_i
@wcAryCtl[aryID].setFilterPredicate(nil)
@wcAryCtl[aryID].removeObjectsAtArrangedObjectIndexes(NSIndexSet.indexSetWithIndexesInRange([0,@wcAryCtl[aryID].arrangedObjects.length]))
token = 0
@fileAryCtl[aryID].arrangedObjects.each do |file|
text = readTextFromFile(file["fullPath"],file["encoding"]).to_s
text.downcase.split(/\W+/).each do |word|
words[word] += 1
token += 1
end
end
type = words.length
wordList = Array.new
words.each do |key,value|
wordList << {"word" => key, "freq" => value}
end
@wcAryCtl[aryID].addObjects(wordList)
@wcAryCtl[aryID].sortWordList(@sortChoice[aryID])
@tokenField[aryID].setIntegerValue(token)
@typeField[aryID].setIntegerValue(type)
end
ib_action :createWordList
def resortWordList(sender)
@wcAryCtl[sender.alternateTitle.to_i].sortWordList(@sortChoice[sender.alternateTitle.to_i])
# @wcAryCtl[sender.tag.to_i].sortWordList(@sortChoice[sender.tag.to_i])
end
ib_action :resortWordList
def tableViewSelectionDidChange(notification)
if @previewCheck.state == 0
@previewText.setString("")
return
end
currentTable = notification.object.to_s
case currentTable
when @table1.to_s,@table2.to_s
selectedIndex = @fileAryCtl[@tableID[currentTable]].selectionIndex
filePath = @fileAryCtl[@tableID[currentTable]].arrangedObjects[selectedIndex]["fullPath"]
encode = @fileAryCtl[@tableID[currentTable]].arrangedObjects[selectedIndex]["encoding"]
@previewText.setString(readTextFromFile(filePath,encode))
@previewText.scrollRangeToVisible([0,0])
end
end
def tableView_validateDrop_proposedRow_proposedDropOperation(table,info,row,operation)
if operation == 1
return NSDragOperationCopy
else
return NSDragOperationNone
end
end
def tableView_acceptDrop_row_dropOperation(tableView,info,row,operation)
pasteBoard = info.draggingPasteboard
aryID = @tableID[tableView.to_s]
if pasteBoard.propertyListForType(TableContentType)
@fileAryCtl[aryID].addUniqFiles(pasteBoard.propertyListForType(TableContentType))
origAryID = aryID == 0 ? 1 : 0
@fileAryCtl[origAryID].removeObjects(pasteBoard.propertyListForType(TableContentType))
true
elsif pasteBoard.propertyListForType(NSFilenamesPboardType)
files = pasteBoard.propertyListForType(NSFilenamesPboardType)
addFilesToTable(aryID,0,files)
true
else
false
end
end
def tableView_writeRowsWithIndexes_toPasteboard(tableView,idx,pasteboard)
pasteboard.declareTypes_owner([TableContentType],self)
draggingItems = Array.new
idx.to_a.each do |index|
draggingItems << @fileAryCtl[@tableID[tableView.to_s]].arrangedObjects[index]
end
pasteboard.setPropertyList_forType(draggingItems,TableContentType)
true
end
def readTextFromFile(filePath,encode)
case File.extname(filePath.to_s).downcase
when ".doc", ".docx", ".rtf", ".rtfd", ".odt", ".sxw"
return NSAttributedString.alloc.initWithPath_documentAttributes(filePath,nil).string
when ".pdf"
return PDFDocument.alloc.initWithURL(NSURL.fileURLWithPath(filePath)).string unless PDFDocument.alloc.initWithURL(NSURL.fileURLWithPath(filePath)) == nil
when ".txt"
return NSString.alloc.initWithContentsOfFile_encoding_error(filePath,EncodingArray[encode.to_i],nil)
end
end
end
class OSX::NSArrayController
include OSX
def addUniqFiles(fileAry)
self.addObjects(fileAry)
uniqFiles = NSCountedSet.alloc.initWithArray(self.arrangedObjects).allObjects
self.removeObjectsAtArrangedObjectIndexes(NSIndexSet.indexSetWithIndexesInRange([0,self.arrangedObjects.length]))
self.addObjects(uniqFiles)
descriptor = [NSSortDescriptor.alloc.initWithKey_ascending_selector("fileName",true,"caseInsensitiveCompare:")]
self.setSortDescriptors(descriptor)
self.rearrangeObjects
end
def sortWordList(sortChoice)
case sortChoice.indexOfSelectedItem
when 0
descriptors = [NSSortDescriptor.alloc.initWithKey_ascending("freq",false),NSSortDescriptor.alloc.initWithKey_ascending("word",true)]
when 1
descriptors = [NSSortDescriptor.alloc.initWithKey_ascending("word",true)]
end
self.setSortDescriptors(descriptors)
self.rearrangeObjects
self.arrangedObjects.each_with_index do |item,idx|
item["order"] = idx + 1
end
end
end
class ArrayExistTransformer < OSX::NSValueTransformer
include OSX
def transformedValueClass
NSNumber.class
end
def allowsReverseTransformation
false
end
def transformedValue(value)
if value.nil? || value.length == 0
return 0
else
return 1
end
end
end