• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * 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 trebuchet.model.Model
20 import java.awt.Dimension
21 import java.awt.Point
22 import java.awt.Rectangle
23 import java.awt.event.*
24 import javax.swing.*
25 
26 
27 class TimelineView(val model: Model, val renderState: RenderState) : JScrollPane(), MouseListener,
28         MouseMotionListener {
29 
30     private val lastMousePosition = Point()
31 
32     init {
33         val box = object : JPanel(), Scrollable {
34 
getScrollableTracksViewportWidthnull35             override fun getScrollableTracksViewportWidth(): Boolean {
36                 return true
37             }
38 
getScrollableTracksViewportHeightnull39             override fun getScrollableTracksViewportHeight(): Boolean {
40                 return false
41             }
42 
getScrollableBlockIncrementnull43             override fun getScrollableBlockIncrement(visibleRect: Rectangle, orientation: Int,
44                                                      direction: Int): Int {
45                 return visibleRect.height - LayoutConstants.TrackHeight
46             }
47 
getScrollableUnitIncrementnull48             override fun getScrollableUnitIncrement(visibleRect: Rectangle, orientation: Int,
49                                                     direction: Int): Int {
50                 return LayoutConstants.TrackHeight
51             }
52 
getPreferredScrollableViewportSizenull53             override fun getPreferredScrollableViewportSize(): Dimension {
54                 return preferredSize
55             }
56 
57             init {
58                 layout = BoxLayout(this, BoxLayout.PAGE_AXIS)
59             }
60         }
<lambda>null61         model.processes.values.filter { it.hasContent }.forEach {
62             box.add(ProcessPanel(it, renderState))
63         }
64         setViewportView(box)
65         addMouseListener(this)
66         addMouseMotionListener(this)
67         lastMousePosition.setLocation(width / 2, 0)
<lambda>null68         renderState.listeners.add({ repaint() })
69         // TODO: Add workaround for OSX 'defaults write -g ApplePressAndHoldEnabled -bool false'
<lambda>null70         addAction('w', "zoomIn") {
71             renderState.zoomBy(1.5, zoomFocalPointX)
72         }
<lambda>null73         addAction('s', "zoomOut") {
74             renderState.zoomBy(1 / 1.5, zoomFocalPointX)
75         }
<lambda>null76         addAction('a', "panLeft") {
77             renderState.pan((width * -.3).toInt())
78         }
<lambda>null79         addAction('d', "panRight") {
80             renderState.pan((width * .3).toInt())
81         }
82     }
83 
addActionnull84     fun addAction(key: Char, name: String, listener: (ActionEvent) -> Unit) {
85         inputMap.put(KeyStroke.getKeyStroke(key), name)
86         actionMap.put(name, object : AbstractAction(name) {
87             override fun actionPerformed(event: ActionEvent) {
88                 listener.invoke(event)
89             }
90         })
91     }
92 
isFocusablenull93     override fun isFocusable() = true
94 
95     private val zoomFocalPointX: Int get() {
96         return maxOf(0, minOf(this.width, lastMousePosition.x - LayoutConstants.RowLabelWidth))
97     }
98 
mouseReleasednull99     override fun mouseReleased(e: MouseEvent) {
100 
101     }
102 
mouseEnterednull103     override fun mouseEntered(e: MouseEvent) {
104 
105     }
106 
mouseClickednull107     override fun mouseClicked(e: MouseEvent) {
108 
109     }
110 
mouseExitednull111     override fun mouseExited(e: MouseEvent) {
112 
113     }
114 
mousePressednull115     override fun mousePressed(e: MouseEvent) {
116 
117     }
118 
mouseMovednull119     override fun mouseMoved(e: MouseEvent) {
120         lastMousePosition.location = e.point
121     }
122 
mouseDraggednull123     override fun mouseDragged(e: MouseEvent) {
124 
125     }
126 }