use AppleScript version "2.8"

use scripting additions

use framework "Foundation"

use framework "AppKit"


global myApp

global theWindow, mainView

global closeFlg

global minXMargin, maxXMargin, minYMargin, maxYMargin, widthSizable, heightSizable


property theDataSource : {}

global sourceList


(* 各オブジェクトの配置 *)

on makeObject()

set mainView to theWindow's contentView

set theWindowFrame to mainView's frame()

set {{windowX, windowY}, {windowWidth, windowHeight}} to theWindowFrame

--

try

-- 新しくオブジェクトを配置する部分 --------------------

(* データーソースを用意する *)

set sourceList to {{data1:{"CheckButton", "item 1", true}, data2:{"TextField", "りんご", ""}}}

set sourceList to sourceList & {{data1:{"CheckButton", "item 2", false}, data2:{"TextField", "すいか", myApp's class "NSColor"'s redColor()}}}

set sourceList to sourceList & {{data1:{"CheckButton", "item 3", false}, data2:{"TextField", "みかん", ""}}}

set theDataSource to current application's class "NSMutableArray"'s alloc()'s init()

theDataSource's addObjectsFromArray:sourceList

(* コラムデータ(列)を作成 *)

set aColumn1 to current application's class "NSTableColumn"'s alloc()'s initWithIdentifier:"data1"

tell aColumn1

headerCell()'s setTitle:"データ 1"

setWidth_(80)

end tell

set aColumn2 to current application's class "NSTableColumn"'s alloc()'s initWithIdentifier:"data2"

tell aColumn2

headerCell()'s setTitle:"データ 2"

setWidth_(200)

end tell

(* コラムデータをテーブルデータにのせる *)

set {x, y, w, h} to {0, 0, windowWidth, windowHeight}

set aTableView to current application's class "NSTableView"'s alloc()'s initWithFrame:(current application's NSMakeRect(0, 0, w, h))

tell aTableView

addTableColumn_(aColumn1)

addTableColumn_(aColumn2)

setDelegate_(me)

setDataSource_(me)

setBackgroundColor_(current application's class "NSColor"'s whiteColor)

end tell

(* スクロールビューのコンテンツビューにテーブルデータをのせて配置 *)

set aScroll to current application's class "NSScrollView"'s alloc()'s initWithFrame:(current application's NSMakeRect(x, y, w, h))

tell aScroll

setDocumentView_(aTableView)

setAutoresizingMask_(widthSizable + heightSizable)

end tell

mainView's addSubview:aScroll

aTableView's reloadData()

----------------------------------------------------------

on error errText

log errText

end try

end makeObject



(* TableViewの表示行数処理(必須) *)

on numberOfRowsInTableView:aTableView

set c to count of (my theDataSource)

return c

end numberOfRowsInTableView:


(* TableViewデータの処理(必須) *)

on tableView:aTableView viewForTableColumn:aColumn row:aRow

set aRec to (my theDataSource)'s objectAtIndex:(aRow as number)

set aIdentifier to (aColumn's identifier()) as string

set columnData to (aRec's valueForKey:aIdentifier) as list

set aClass to item 1 of columnData

if aClass = "TextField" then -- TextField

try

set aColor to item 3 of columnData

if aColor = "" then set aColor to myApp's class "NSColor"'s blackColor()

on error

set aColor to myApp's class "NSColor"'s blackColor()

end try

set aTextField to myApp's class "NSTextField"'s alloc()'s init()

tell aTextField

setBordered_(false)

setFont_(myApp's class "NSFont"'s systemFontOfSize:14.0)

setStringValue_(item 2 of columnData)

setTextColor_(aColor)

end tell

set aRes to aTextField

else if aClass = "CheckButton" then -- CheckButton

set aCheckButton to myApp's class "NSButton"'s alloc()'s init()

tell aCheckButton

setButtonType_(myApp's NSButtonTypeSwitch)

setTitle_(item 2 of columnData)

setState_(item 3 of columnData)

end tell

set aRes to aCheckButton

else -- 該当せず

set aRes to missing value

end if

return aRes

end tableView:viewForTableColumn:row:



----------

on run

my performSelectorOnMainThread:"mainMakeObject:" withObject:(missing value) waitUntilDone:true

--

log "End"

end run


on mainMakeObject:inputObj -- メインルーチン

set myApp to current application

set closeFlg to false

--

set aTitle to "TableView View-Based テスト"

set aRect to {0, 0, 380, 250}

set moveCenter to true

set theWindow to my makeNewWindow(aTitle, aRect, moveCenter)

--

my makeObject()

--

theWindow's setAlphaValue:1.0

--

repeat

if closeFlg then

my closeWin:theWindow

exit repeat

end if

delay 0.2

end repeat

end mainMakeObject:


on makeNewWindow(aTitle, aRect, moveCenter)

set {windowX, windowY, windowW, windowH} to aRect

set theRect to myApp's NSMakeRect(windowX, windowY, windowW, windowH)

set aScreen to myApp's class "NSScreen"'s mainScreen()

set aStyle to (myApp's NSWindowStyleMaskTitled as integer)

set aStyle to aStyle + (myApp's NSWindowStyleMaskClosable as integer)

set aStyle to aStyle + (myApp's NSWindowStyleMaskMiniaturizable as integer)

set aStyle to aStyle + (myApp's NSWindowStyleMaskResizable as integer)

set aBacking to myApp's NSBackingStoreBuffered

set aWindow to myApp's class "NSWindow"'s alloc()'s initWithContentRect:theRect styleMask:aStyle backing:aBacking defer:false screen:aScreen

tell aWindow

setAlphaValue_(0.0)

setDelegate_(me)

setTitle_(aTitle)

setMinSize_(myApp's NSMakeSize(300, 200))

setMaxSize_(myApp's NSMakeSize(10000, 10000))

setBackgroundColor_(myApp's class "NSColor"'s colorWithCalibratedRed:0.95 green:0.95 blue:0.95 alpha:1.0)

setDisplaysWhenScreenProfileChanges_(true)

setReleasedWhenClosed_(true)

makeKeyAndOrderFront_(me)

end tell

--

if moveCenter then aWindow's |center|()

--

set minXMargin to (myApp's NSViewMinXMargin) as integer -- 左の余白を柔軟に

set maxXMargin to (myApp's NSViewMaxXMargin) as integer -- 右の余白を柔軟に

set minYMargin to (myApp's NSViewMinYMargin) as integer -- 下の余白を柔軟に

set maxYMargin to (myApp's NSViewMaxYMargin) as integer -- 上の余白を柔軟に

set widthSizable to (myApp's NSViewWidthSizable) as integer -- 横方向の幅を柔軟に

set heightSizable to (myApp's NSViewHeightSizable) as integer -- 縦方向の幅を柔軟に

--

return aWindow

end makeNewWindow


(* ウインドウバーの閉じるボタンアクションを受けた時 *)

on windowShouldClose:sender

set closeFlg to true

end windowShouldClose:


(* ウインドウを閉じるための実際のアクション *)

on closeWin:aWindow

aWindow's orderOut:me

if (name of myApp) ≠ "Script Editor" then quit me

end closeWin:

inserted by FC2 system