src/Tools/Graphview/popups.scala
changeset 59202 711c2446dc9d
parent 55618 995162143ef4
child 59218 eadd82d440b0
equal deleted inserted replaced
59201:702e0971d617 59202:711c2446dc9d
       
     1 /*  Title:      Tools/Graphview/popups.scala
       
     2     Author:     Markus Kaiser, TU Muenchen
       
     3 
       
     4 PopupMenu generation for graph components.
       
     5 */
       
     6 
       
     7 package isabelle.graphview
       
     8 
       
     9 
       
    10 import isabelle._
       
    11 import isabelle.graphview.Mutators._
       
    12 
       
    13 import javax.swing.JPopupMenu
       
    14 import scala.swing.{Action, Menu, MenuItem, Separator}
       
    15 
       
    16 
       
    17 object Popups
       
    18 {
       
    19   def apply(panel: Graph_Panel, under_mouse: Option[String], selected: List[String])
       
    20     : JPopupMenu =
       
    21   {
       
    22     val visualizer = panel.visualizer
       
    23 
       
    24     val add_mutator = visualizer.model.Mutators.add _
       
    25     val curr = visualizer.model.current
       
    26 
       
    27     def filter_context(ls: List[String], reverse: Boolean,
       
    28                        caption: String, edges: Boolean) =
       
    29       new Menu(caption) {
       
    30         contents += new MenuItem(new Action("This") {
       
    31             def apply =
       
    32               add_mutator(
       
    33                 Mutators.create(visualizer,
       
    34                   Node_List(ls, reverse, false, false)
       
    35                 )
       
    36               )
       
    37           })
       
    38 
       
    39         contents += new MenuItem(new Action("Family") {
       
    40             def apply =
       
    41               add_mutator(
       
    42                 Mutators.create(visualizer,
       
    43                   Node_List(ls, reverse, true, true)
       
    44                 )
       
    45               )
       
    46           })
       
    47 
       
    48         contents += new MenuItem(new Action("Parents") {
       
    49             def apply =
       
    50               add_mutator(
       
    51                 Mutators.create(visualizer,
       
    52                   Node_List(ls, reverse, false, true)
       
    53                 )
       
    54               )
       
    55           })
       
    56 
       
    57         contents += new MenuItem(new Action("Children") {
       
    58             def apply =
       
    59               add_mutator(
       
    60                 Mutators.create(visualizer,
       
    61                   Node_List(ls, reverse, true, false)
       
    62                 )
       
    63               )
       
    64           })
       
    65 
       
    66 
       
    67         if (edges) {
       
    68           val outs = ls.map(l => (l, curr.imm_succs(l)))  // FIXME iterator
       
    69                       .filter(_._2.size > 0).sortBy(_._1)
       
    70           val ins = ls.map(l => (l, curr.imm_preds(l)))  // FIXME iterator
       
    71                       .filter(_._2.size > 0).sortBy(_._1)
       
    72 
       
    73           if (outs.nonEmpty || ins.nonEmpty) {
       
    74             contents += new Separator()
       
    75 
       
    76             contents += new Menu("Edge") {
       
    77               if (outs.nonEmpty) {
       
    78                 contents += new MenuItem("From...") {
       
    79                   enabled = false
       
    80                 }
       
    81 
       
    82                 outs.map( e => {
       
    83                   val (from, tos) = e
       
    84                   contents += new Menu(from) {
       
    85                     contents += new MenuItem("To..."){
       
    86                       enabled = false
       
    87                     }
       
    88 
       
    89                     tos.toList.sorted.distinct.map( to => {
       
    90                       contents += new MenuItem(new Action(to) {
       
    91                         def apply =
       
    92                           add_mutator(
       
    93                             Mutators.create(visualizer, Edge_Endpoints(from, to))
       
    94                           )
       
    95                       })
       
    96                     })
       
    97                   }
       
    98                 })
       
    99               }
       
   100               if (outs.nonEmpty && ins.nonEmpty) {
       
   101                 contents += new Separator()
       
   102               }
       
   103               if (ins.nonEmpty) {
       
   104                 contents += new MenuItem("To...") {
       
   105                   enabled = false
       
   106                 }
       
   107 
       
   108                 ins.map( e => {
       
   109                   val (to, froms) = e
       
   110                   contents += new Menu(to) {
       
   111                     contents += new MenuItem("From..."){
       
   112                       enabled = false
       
   113                     }
       
   114 
       
   115                     froms.toList.sorted.distinct.map( from => {
       
   116                       contents += new MenuItem(new Action(from) {
       
   117                         def apply =
       
   118                           add_mutator(
       
   119                             Mutators.create(visualizer, Edge_Endpoints(from, to))
       
   120                           )
       
   121                       })
       
   122                     })
       
   123                   }
       
   124                 })
       
   125               }
       
   126             }
       
   127           }
       
   128         }
       
   129     }
       
   130 
       
   131     val popup = new JPopupMenu()
       
   132 
       
   133     popup.add(new MenuItem(new Action("Remove all filters") {
       
   134           def apply = visualizer.model.Mutators(Nil)
       
   135         }).peer
       
   136     )
       
   137     popup.add(new JPopupMenu.Separator)
       
   138 
       
   139     if (under_mouse.isDefined) {
       
   140       val v = under_mouse.get
       
   141       popup.add(new MenuItem("Mouseover: %s".format(visualizer.Caption(v))) {
       
   142         enabled = false
       
   143       }.peer)
       
   144 
       
   145       popup.add(filter_context(List(v), true, "Hide", true).peer)
       
   146       popup.add(filter_context(List(v), false, "Show only", false).peer)
       
   147 
       
   148       popup.add(new JPopupMenu.Separator)
       
   149     }
       
   150     if (!selected.isEmpty) {
       
   151       val text = {
       
   152         if (selected.length > 1) {
       
   153           "Multiple"
       
   154         } else {
       
   155           visualizer.Caption(selected.head)
       
   156         }
       
   157       }
       
   158 
       
   159       popup.add(new MenuItem("Selected: %s".format(text)) {
       
   160         enabled = false
       
   161       }.peer)
       
   162 
       
   163       popup.add(filter_context(selected, true, "Hide", true).peer)
       
   164       popup.add(filter_context(selected, false, "Show only", false).peer)
       
   165       popup.add(new JPopupMenu.Separator)
       
   166     }
       
   167 
       
   168 
       
   169     popup.add(new MenuItem(new Action("Fit to Window") {
       
   170         def apply = panel.fit_to_window()
       
   171       }).peer
       
   172     )
       
   173 
       
   174     popup
       
   175   }
       
   176 }