1 /* <lambda>null2 * Copyright 2017 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package traceviewer.ui 18 19 import traceviewer.ui.builders.MenuBar 20 import traceviewer.ui.components.ChooseTrace 21 import trebuchet.io.BufferProducer 22 import trebuchet.io.DataSlice 23 import trebuchet.io.asSlice 24 import trebuchet.model.Model 25 import trebuchet.task.ImportTask 26 import trebuchet.util.PrintlnImportFeedback 27 import java.awt.Dimension 28 import java.io.BufferedInputStream 29 import java.io.File 30 import java.io.FileInputStream 31 import javax.swing.* 32 33 34 class TraceViewerWindow { 35 36 private var _model: Model? = null 37 val frame = JFrame("TraceViewer") 38 39 init { 40 frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE 41 frame.size = Dimension(1400, 1000) 42 frame.jMenuBar = createMenuBar() 43 frame.contentPane = ChooseTrace { file -> import(file) } 44 frame.isLocationByPlatform = true 45 frame.isVisible = true 46 } 47 48 constructor(file: File) { 49 import(file) 50 } 51 52 private fun createMenuBar(): JMenuBar { 53 return MenuBar { 54 menu("File") { 55 item("Open") { 56 action { ChooseTrace.selectFile(frame.location) { import(it) } } 57 } 58 } 59 } 60 } 61 62 fun import(file: File) { 63 object : SwingWorker<Model, Void>() { 64 override fun doInBackground(): Model { 65 val progress = ProgressMonitorInputStream(frame, "Importing ${file.name}", 66 FileInputStream(file)) 67 progress.progressMonitor.millisToDecideToPopup = 0 68 progress.progressMonitor.millisToPopup = 0 69 val stream = BufferedInputStream(progress) 70 71 val task = ImportTask(PrintlnImportFeedback()) 72 val model = task.import(object : BufferProducer { 73 override fun next(): DataSlice? { 74 val buffer = ByteArray(2 * 1024 * 1024) 75 val read = stream.read(buffer) 76 if (read == -1) return null 77 return buffer.asSlice(read) 78 } 79 80 override fun close() { 81 stream.close() 82 } 83 }) 84 println("Import finished") 85 return model 86 } 87 88 override fun done() { 89 println("Done called") 90 model = get() 91 } 92 }.execute() 93 } 94 95 var model: Model? 96 get() = _model 97 set(model) { 98 _model = model 99 if (model != null) { 100 val boost = model.duration * .15 101 val zoom = RenderState(model.beginTimestamp - boost, 102 model.endTimestamp + boost, 103 frame.size.width - LayoutConstants.RowLabelWidth) 104 val timelineView = TimelineView(model, zoom) 105 frame.contentPane = timelineView 106 timelineView.requestFocus() 107 frame.revalidate() 108 frame.repaint() 109 } 110 } 111 }