1# 2# MyController.rb 3# MailDemo 4# 5# Created by Laurent Sansonetti on 1/8/07. 6# Copyright (c) 2007 Apple Computer. All rights reserved. 7# 8 9class MyController < NSObject 10 11 ib_outlets :mailboxTable, :emailTable, :previewPane, :emailStatusLine, :mailboxStatusLine 12 13 def init 14 if super_init 15 @mailboxes = NSMutableArray.array 16 return self 17 end 18 end 19 20 def awakeFromNib 21 @previewPane.setEditable(false) 22 end 23 24 def currentMailbox 25 idx = @mailboxTable.selectedRow 26 @mailboxes.objectAtIndex(idx) unless idx < 0 27 end 28 29 def currentMailboxAndEmail 30 mailbox = currentMailbox 31 if mailbox 32 idx = @emailTable.selectedRow 33 [mailbox, mailbox.emails.objectAtIndex(idx)] unless idx < 0 34 end 35 end 36 37 def addEmail(sender) 38 # get current mailbox 39 mailbox = currentMailbox 40 return if mailbox.nil? 41 42 # get mutable array of emails 43 # and add new instance 44 mailbox.emails.addObject(Email.alloc.init) 45 46 # reload table and select new item 47 @emailTable.reloadData 48 @emailTable.selectRow_byExtendingSelection(mailbox.emails.count - 1, false) 49 end 50 ib_action :addEmail 51 52 def removeEmail(sender) 53 # get current mailbox 54 mailbox = currentMailbox 55 return if mailbox.nil? 56 57 # get selected email 58 idx = @emailTable.selectedRow 59 return if idx < 0 60 61 # get email list 62 emails = mailbox.emails 63 return if idx > emails.count - 1 64 65 # remove object 66 emails.removeObjectAtIndex(idx) 67 68 # refresh UI 69 @emailTable.reloadData 70 if emails.count > 0 71 @emailTable.selectRow_byExtendingSelection(emails.count - 1, false) 72 end 73 end 74 ib_action :removeEmail 75 76 def addMailbox(sender) 77 # create and add new mailbox 78 mailbox = Mailbox.alloc.init 79 @mailboxes.addObject(mailbox) 80 81 # reload table and select new item 82 @mailboxTable.reloadData 83 @mailboxTable.selectRow_byExtendingSelection(@mailboxes.count - 1, false) 84 end 85 ib_action :addMailbox 86 87 def removeMailbox(sender) 88 # get current mailbox 89 idx = @mailboxTable.selectedRow 90 return if idx < 0 91 return if idx > @mailboxes.count - 1 92 93 @mailboxes.removeObjectAtIndex(idx) 94 95 # reload table and select new item 96 @mailboxTable.reloadData 97 @mailboxTable.selectRow_byExtendingSelection(@mailboxes.count - 1, false) 98 end 99 ib_action :removeMailbox 100 101 def numberOfRowsInTableView(tableView) 102 case tableView 103 when @mailboxTable 104 @mailboxStatusLine.setStringValue("#{@mailboxes.count} Mailboxes") 105 @mailboxes.count 106 107 when @emailTable 108 mailbox = currentMailbox 109 if mailbox.nil? 110 0 111 else 112 @emailStatusLine.setStringValue("#{mailbox.emails.count} Emails") 113 mailbox.emails.count 114 end 115 end 116 end 117 118 def tableView_objectValueForTableColumn_row(tableView, column, row) 119 key = column.identifier 120 case tableView 121 when @mailboxTable 122 @mailboxes.objectAtIndex(row).properties.objectForKey(key) 123 124 when @emailTable 125 mailbox = currentMailbox 126 if mailbox.nil? 127 '' 128 else 129 mailbox.emails.objectAtIndex(row).properties.objectForKey(key) 130 end 131 end 132 end 133 134 def tableView_setObjectValue_forTableColumn_row(tableView, object, column, row) 135 key = column.identifier 136 properties = case tableView 137 when @mailboxTable 138 @mailboxes.objectAtIndex(row).properties 139 140 when @emailTable 141 currentMailbox.emails.objectAtIndex(row).properties 142 end 143 properties.setObject_forKey(object, key) 144 tableView.reloadData 145 end 146 147 def tableViewSelectionDidChange(notification) 148 if notification.object == @mailboxTable 149 @mailboxTable.reloadData 150 return 151 end 152 153 mailbox, email = currentMailboxAndEmail 154 if mailbox.nil? or email.nil? 155 @emailStatusLine.setStringValue('0 Emails') 156 @previewPane.setString('') 157 @previewPane.setEditable(false) 158 return 159 end 160 161 @previewPane.setString(email.properties.objectForKey('body')) 162 @previewPane.setEditable(true) 163 end 164 165 def textDidEndEditing(notification) 166 string = notification.object.string 167 168 mailbox, email = currentMailboxAndEmail 169 return if mailbox.nil? or email.nil? 170 171 email.properties.setObject_forKey(string.copy, 'body') 172 end 173 174=begin 175// Workaround for apparent reload bug in NSTableView. 176// 177// See this post for more info: 178// http://cocoa.mamasam.com/MACOSXDEV/2003/11/1/76580.php 179=end 180 181 def tableViewSelectionIsChanging(notification) 182 if notification.object == @mailboxTable 183 @emailTable.noteNumberOfRowsChanged 184 end 185 end 186 187end 188