Skip to content

Instantly share code, notes, and snippets.

@firstspring1845
Created May 28, 2015 14:09
Show Gist options
  • Save firstspring1845/4be443343ac3b49e5764 to your computer and use it in GitHub Desktop.
Save firstspring1845/4be443343ac3b49e5764 to your computer and use it in GitHub Desktop.
package net.firsp.twit
import com.sun.javafx.application.LauncherImpl
import com.sun.javafx.scene.control.skin.VirtualFlow
import javafx.application.Application
import javafx.application.Platform
import javafx.collections.FXCollections
import javafx.scene.Scene
import javafx.scene.control.IndexedCell
import javafx.scene.control.ListView
import javafx.scene.input.KeyCode
import javafx.stage.Stage
import net.firsp.test.StatusListCell
import net.firsp.twit.util.Util
import twitter4j.Status
import twitter4j.TwitterStreamFactory
import twitter4j.UserStreamAdapter
import twitter4j.conf.ConfigurationBuilder
import java.util.HashMap
import kotlin.concurrent.thread
fun main(args: Array<String>) {
LauncherImpl.launchApplication(javaClass<TwitApp>(), args)
}
class TwitApp : Application() {
var list = FXCollections.observableArrayList<Status>()
val statuses = HashMap<Long, Status>()
val view = ListView<Status>()
fun getVirtualFlow() = (view.lookupAll(".virtual-flow").firstOrNull() as? VirtualFlow<IndexedCell<Any>>) ?: VirtualFlow<IndexedCell<Any>>()
fun build() = ConfigurationBuilder()
.setOAuthConsumerKey("xxx")
.setOAuthConsumerSecret("xxx")
.setOAuthAccessToken("xxx")
.setOAuthAccessTokenSecret("xxx")
.build();
override fun start(stage: Stage?) {
view.setOnKeyPressed {
if(it.getCode() == KeyCode.O) {
view.getSelectionModel().getSelectedItems().get(0)?.let {
thread {
java.awt.Desktop.getDesktop().browse(java.net.URI("https://twitter.com/" + it.getUser().getScreenName() + "/status/" + it.getId()))
}
}
}
}
val f = VirtualFlow<IndexedCell<Any>>()
if (stage is Stage) {
println(view.lookupAll(".virtual-flow").size())
stage.setOnCloseRequest { Runtime.getRuntime().halt(0) }
stage.setScene(Scene(view, 640.0, 480.0))
stage.show()
println(view.lookupAll(".virtual-flow").size())
}
view.setCellFactory { StatusListCell() }
val ts = TwitterStreamFactory(build()).getInstance()
Util.addListener(ts, object : UserStreamAdapter() {
override fun onStatus(status: Status?) {
if (status is Status) {
statuses.put(status.getId(), status)
list = FXCollections.observableList(statuses.values().sort().reverse())
Platform.runLater {
if (true) {
val a = getVirtualFlow()
val b = a.getFirstVisibleCell()?.getItem() ?: Unit
val c = -(a.getFirstVisibleCell()?.getLayoutY() ?: 0.0)
view.setItems(list)
val d = list.indexOf(b)
(0..1).forEach {
view.scrollTo(d)
//view.
getVirtualFlow().adjustPixels(c)
getVirtualFlow().layout()
println(it)
}
}
//view.setItems(list)
}
}
}
})
ts.user()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment