Last active
March 18, 2016 13:16
-
-
Save gitbricho/4d35cb2182b67eefd7bb to your computer and use it in GitHub Desktop.
テンプレート:PDF表示
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
import Quartz | |
@NSApplicationMain | |
class AppDelegate: NSObject, NSApplicationDelegate { | |
var pdfView: PDFView? | |
func applicationDidFinishLaunching(aNotification: NSNotification) { | |
} | |
func applicationWillTerminate(aNotification: NSNotification) { | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
/** | |
* タイプクラス | |
*/ | |
class Cタイプ: NSObject { | |
var z名前:String | |
var zグループリスト: [Cグループ] = [] | |
init (名前:String){ | |
z名前 = 名前 | |
} | |
} | |
/** | |
* グループクラス | |
*/ | |
class Cグループ: NSObject { | |
var z名前:String | |
var zコンテンツリスト: [Cコンテンツ] = [] | |
var zアイコン:NSImage? | |
init (名前:String, アイコン:NSImage?){ | |
z名前 = 名前 | |
zアイコン = アイコン | |
} | |
} | |
/** | |
* コンテンツクラス | |
*/ | |
class Cコンテンツ: NSObject { | |
var zタイトル:String | |
var zアイコン:NSImage? | |
var zパス:String | |
init (タイトル:String, パス:String, アイコン:NSImage?){ | |
zタイトル = タイトル | |
zパス = パス | |
zアイコン = アイコン | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
import Quartz | |
class PDFViewController: NSViewController { | |
@IBOutlet weak var pdfView: PDFView! | |
let app : AppDelegate = (NSApplication.sharedApplication().delegate | |
as? AppDelegate)! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
pdfView.setAutoScales(true) | |
pdfView.setDisplaysPageBreaks(false) | |
app.pdfView = self.pdfView | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
import Quartz | |
class PDFViewController: NSViewController { | |
@IBOutlet weak var pdfView: PDFView! | |
.... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
class ViewController: NSViewController , | |
NSOutlineViewDelegate, | |
NSOutlineViewDataSource { | |
@IBOutlet weak var outlineView: NSOutlineView! | |
let lユーザーホーム = NSHomeDirectory() | |
var vタイプ1 = Cタイプ(名前:"プログラミング") | |
var vタイプ2 = Cタイプ(名前:"譜面") | |
let lFolderImg = NSImage(named: "folder") | |
let lNoteImg = NSImage(named: "note") | |
let app : AppDelegate = (NSApplication.sharedApplication().delegate | |
as? AppDelegate)! | |
var zPath:String = "" | |
var zFileUrl: NSURL? = nil | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
fサンプルデータの作成() | |
outlineView.setDataSource(self) | |
outlineView.setDelegate(self) | |
self.outlineView?.expandItem(nil, expandChildren: true) | |
} | |
func fサンプルデータの作成() { | |
let lグループ1 = Cグループ(名前:"Java", | |
アイコン: lFolderImg) | |
let lコンテンツ10 = Cコンテンツ(タイトル: "オブジェクトの生成", | |
パス: "/_wsp/_pdf/オブジェクトの生成001.pdf", | |
アイコン: lNoteImg) | |
let lコンテンツ11 = Cコンテンツ(タイトル: "ジェネリックス", | |
パス: "/_wsp/_pdf/ジェネリックス001.pdf", | |
アイコン: lNoteImg) | |
lグループ1.zコンテンツリスト.append(lコンテンツ10) | |
lグループ1.zコンテンツリスト.append(lコンテンツ11) | |
let lグループ2 = Cグループ(名前:"Swift", | |
アイコン: lFolderImg) | |
let lコンテンツ20 = Cコンテンツ(タイトル: "Swiftで作成したPDFファイル", | |
パス: "/_wsp/_pdf/sample.pdf", | |
アイコン: lNoteImg) | |
lグループ2.zコンテンツリスト.append(lコンテンツ20) | |
vタイプ1.zグループリスト.append(lグループ1) | |
vタイプ1.zグループリスト.append(lグループ2) | |
} | |
func outlineView(outlineView: NSOutlineView, child index: Int, ofItem item: AnyObject?) -> AnyObject { | |
var result: AnyObject | |
if let item: AnyObject = item { | |
switch item { | |
case let typ as Cタイプ: | |
result = typ.zグループリスト[index] | |
case let grp as Cグループ: | |
result = grp.zコンテンツリスト[index] | |
default: | |
result = self | |
} | |
} else { | |
switch index { | |
case 0: | |
result = vタイプ1 | |
default: | |
result = vタイプ2 | |
} | |
} | |
return result | |
} | |
//表示項目(タイプ|グループ)が展開可能かどうかチェック. | |
//対象項目の子項目の件数で判定. | |
func outlineView(outlineView: NSOutlineView, isItemExpandable item: AnyObject) -> Bool { | |
var v展開可能 = false | |
switch item { | |
case let typ as Cタイプ: | |
v展開可能 = (typ.zグループリスト.count > 0) ? true : false | |
case let grp as Cグループ: | |
v展開可能 = (grp.zコンテンツリスト.count > 0) ? true : false | |
default: | |
v展開可能 = false | |
} | |
return v展開可能 | |
} | |
//表示項目の子項目の件数を返す. | |
func outlineView(outlineView: NSOutlineView, numberOfChildrenOfItem item: AnyObject?) -> Int { | |
var v子項目の件数: Int = 0 | |
if let item: AnyObject = item { | |
switch item { | |
case let typ as Cタイプ: | |
v子項目の件数 = typ.zグループリスト.count | |
case let grp as Cグループ: | |
v子項目の件数 = grp.zコンテンツリスト.count | |
default: | |
v子項目の件数 = 0 | |
} | |
} else { | |
//ルート項目 | |
v子項目の件数 = 2 //タイプ1 , タイプ2 | |
} | |
print("子項目の件数=\(v子項目の件数)") | |
return v子項目の件数 | |
} | |
// NSOutlineViewDelegate | |
// アウトラインビューに表示項目を設定. | |
func outlineView(outlineView: NSOutlineView, viewForTableColumn: NSTableColumn?, item: AnyObject) -> NSView? { | |
switch item { | |
case let typ as Cタイプ: | |
let view = outlineView.makeViewWithIdentifier( | |
"HeaderCell", owner: self) as! NSTableCellView | |
//アウトラインビューのセルのテキストフィールドにタイプ名をセット | |
if let textField = view.textField { | |
textField.stringValue = typ.z名前 | |
} | |
return view | |
case let grp as Cグループ: | |
let view = outlineView.makeViewWithIdentifier( | |
"DataCell", owner: self) as! NSTableCellView | |
if let textField = view.textField { | |
textField.stringValue = grp.z名前 | |
} | |
if let image = grp.zアイコン { | |
view.imageView!.image = image | |
} | |
return view | |
case let v as Cコンテンツ: | |
let view = outlineView.makeViewWithIdentifier( | |
"DataCell", owner: self) as! NSTableCellView | |
if let textField = view.textField { | |
textField.stringValue = v.zタイトル | |
} | |
if let image = v.zアイコン { | |
view.imageView!.image = image | |
} | |
return view | |
default: | |
return nil | |
} | |
} | |
//表示項目がタイプ型か判定する. | |
func outlineView(outlineView: NSOutlineView, isGroupItem item: AnyObject) -> Bool { | |
var vタイプ? = false | |
switch item { | |
case _ as Cタイプ: | |
vタイプ? = true | |
default: | |
vタイプ? = false | |
} | |
return vタイプ? | |
} | |
// アウトラインビューの項目の選択が変わった場合の処理. | |
func outlineViewSelectionDidChange(notification: NSNotification){ | |
let l選択idx = notification.object?.selectedRow | |
let l選択項目:AnyObject? = | |
notification.object?.itemAtRow(l選択idx!) | |
if (l選択項目 is Cコンテンツ){ | |
zPath = lユーザーホーム + (l選択項目 as! Cコンテンツ).zパス | |
print("path=\(zPath)") | |
} | |
else{ | |
print("タイプ、グループが選択された場合は何もしない") | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
import Quartz | |
class ViewController: NSViewController , | |
NSOutlineViewDelegate, | |
NSOutlineViewDataSource { | |
.... | |
// アウトラインビューの項目の選択が変わった場合の処理. | |
func outlineViewSelectionDidChange(notification: NSNotification){ | |
let l選択idx = notification.object?.selectedRow | |
let l選択項目:AnyObject? = | |
notification.object?.itemAtRow(l選択idx!) | |
if (l選択項目 is Cコンテンツ){ | |
zPath = lユーザーホーム + (l選択項目 as! Cコンテンツ).zパス | |
print("path=\(zPath)") | |
//## 選択されたPDFをPDFビューに表示 | |
let pdfView = app.pdfView | |
let url = NSURL.fileURLWithPath(zPath) | |
let pdf = PDFDocument(URL: url) | |
print("page count=\(pdf?.pageCount())") | |
pdfView!.setAutoScales(true) | |
pdfView!.setDisplaysPageBreaks(false) | |
pdfView!.setDocument(pdf) | |
} | |
else{ | |
print("タイプ、グループが選択された場合は何もしない") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment