class Launcher(Runnable,SNTListener):

  def __init__(self, imp, roi, layers):
    self.imp = imp
    self.roi = roi
    self.layers = layers

  def run(self):
    self.plugin = Simple_Neurite_Tracer()
    self.plugin.addListener(self)
    self.plugin.run("")

  def onEvent(self, event):
    global front
    if event.getType() != SNTEvent.SEND_TO_TRAKEM2:
      return
    d = tempfile.mkdtemp()
    pafm = self.plugin.getPathAndFillManager()
    pixel_width = self.imp.getCalibration().pixelWidth
    pafm.downsampleAll(pixel_width)
    output_prefix = os.path.join(d,'snt-export')
    if not pafm.exportAllAsSWC(output_prefix):
      IJ.error('Exporting SNT paths as SWC files to "%s" failed' % (output_prefix,))
      return
    tlines = []
    cal = self.imp.getCalibration().copy()
    lset = front.getLayerSet()
    for e in os.listdir(d):
      filename = os.path.join(d, e)
      tl = Treeline(front.project, filename)
      nodes = {}
      offset = self.roi.getBounds()
      fp = open(filename)
      root = None
      for line in fp:
        line = re.sub('\s*#.*$', '', line)
        line = line.strip()
        if not line:
          continue
        point_id, point_type, x, y, z, radius, parent_id = re.split('\s+', line)
        node = tl.newNode(offset.x + float(x) / cal.pixelWidth,
                          offset.y + float(y) / cal.pixelHeight,
                          self.layers[int(float(z) / cal.pixelDepth + 0.5)],
                          None)
        nodes[int(point_id)] = node
        pid = int(parent_id)
        parent = nodes.get(pid, None)
        if parent:
          parent.add(node, 5)
        elif -1 == pid:
          root = node
      if root:
        tl.setRoot(root) # caches all subtree nodes in the Treeline
        tl.calculateBoundingBox(None)
        tlines.append(tl)
    lset.addAll(tlines)
    front.project.getProjectTree().insertSegmentations(tlines)
    shutil.rmtree(d)
 def run(self):
   self.plugin = Simple_Neurite_Tracer()
   self.plugin.addListener(self)
   self.plugin.run("")