/**
  * Try to save the peak file
  *
  * @return true, if the operation succeeded and the file was saved, false otherwise
  */
 protected boolean savePeak() {
   // Save peak File
   File p = FileManager.getParallelFile(source, "gmpk");
   OutputStream out;
   try {
     out = new FileOutputStream(p);
   } catch (FileNotFoundException e1) {
     e1.printStackTrace();
     return false;
   }
   WaveForm peak = afWF.getPeakWaveForm();
   ProgressThread saver =
       new SavePeakWaveFormThread(peak, peak.getIntervallSize(), out, source.lastModified());
   ProgressMonitor progress =
       new ProgressMonitor(
           getShell(), saver, "Saving Peak File...", "Saving Peak File " + p.getAbsolutePath());
   try {
     progress.start();
   } catch (NotFinishedException ex) {
     try {
       out.close();
     } catch (IOException e) {
       e.printStackTrace();
     }
     return false;
   }
   try {
     out.close();
   } catch (IOException e4) {
     e4.printStackTrace();
     return false;
   }
   return true;
 }
 /** @see com.groovemanager.sampled.AudioPlayerProvider#stopRec() */
 public void stopRec() {
   modified = true;
   canRec = false;
   recording = false;
   try {
     out.close();
     File tempPeak = File.createTempFile("gmtmp_", ".gmpk");
     ((DynamicPeakWaveForm) afWF.getPeakWaveForm()).close(source.lastModified());
     try {
       AudioFileWaveForm aw = new AudioFileWaveForm(source, afWF.getPeakWaveForm(), 32 * 1024, 25);
       fileFormat = AudioSystem.getAudioFileFormat(source);
       cutList.setSource(new AudioFileSource(source, aw));
       waveDisplay.showAll();
     } catch (UnsupportedAudioFileException e1) {
       e1.printStackTrace();
       editor.errorMessage(e1.getMessage());
     }
     this.editor.player.setProvider(this);
   } catch (IOException e) {
     e.printStackTrace();
     editor.errorMessage(e.getMessage());
   }
   editor.zoomWaveDisplay.setSource(this);
 }
  /**
   * Create a new WaveTab
   *
   * @param editor The SampleEditor to which the WaveTab belongs
   * @param parent The TabFolder to be used as parent for the TabItem that will be created
   * @param style The style to be used for the TabItem
   * @param waveStyle The style to be used for the WaveFormDisplay
   * @param f The source file
   * @param wf The WaveForm corresponding to the file
   * @param format The AudioFormat to be used for recording or <code>null</code>, if not recording
   * @param rec true, if recording should be possible for this WaveTab, false otherwise
   * @throws UnsupportedAudioFileException If the AudioFileFormat of the given file can not be
   *     detected (only in case of not recording)
   * @throws IOException If an I/O error occured
   */
  WaveTab(
      SimpleSampleEditor editor,
      TabFolder parent,
      int style,
      int waveStyle,
      File f,
      AudioFileWaveForm wf,
      AudioFormat format,
      boolean rec)
      throws UnsupportedAudioFileException, IOException {
    this.editor = editor;
    afWF = wf;
    channels = wf.getChannels();
    afSource = new AudioFileSource(f, afWF);
    cutList = new CutList(afSource);
    cutList.addModificationListener(this);
    waveDisplay = new WaveFormDisplay(parent, waveStyle, false);
    waveDisplay.setChannelBackgroundColor(parent.getDisplay().getSystemColor(SWT.COLOR_BLACK));
    waveDisplay.addWaveDisplayListener(editor);
    waveDisplay.setSource(this);
    waveDisplay.setEditMarkers(true);
    waveDisplay.getComposite().addKeyListener(createKeyListener());
    waveDisplay.getComposite().addKeyListener(editor.keyListener);
    waveDisplay.addSelectableListener(
        new SelectableListener() {
          public void selectionPermanentChanged(Selectable s, Selection sel) {}

          public void selectionChanged(Selectable s, Selection sel) {
            WaveTab.this.editor.zoomSel.setEnabled(!sel.isEmpty());
          }

          public void positionChanged(Selectable s, int pos) {}

          public void positionWillChange(Selectable s) {}

          public void positionWontChange(Selectable s) {}
        });
    source = f;
    tabItem = new TabItem(parent, style);
    tabItem.setText(f.getName());
    tabItem.setControl(waveDisplay.getComposite());
    tabItem.setData(WaveTab.this);
    tabItem.addDisposeListener(
        new DisposeListener() {
          public void widgetDisposed(DisposeEvent e) {
            if (WaveTab.this.editor.player.getProvider() == WaveTab.this)
              WaveTab.this.editor.player.removeProvider();
          }
        });
    recording = rec;
    if (rec) {
      this.format = format;
      isNew = true;
      canRec = true;
    } else {
      fileFormat =
          AudioManager.getDefault().getAudioFileFormat(f, null, new String[] {"slices", "RLND"});
      format = fileFormat.getFormat();
      isNew = false;
      canRec = false;

      // For compatibility to 1.4...
      Map properties = AudioManager.getProperties(fileFormat);
      if (properties != null) {
        Object sl = properties.get("slices");
        if (sl != null && sl instanceof int[][]) {
          int[][] slices = (int[][]) sl;
          for (int i = 0; i < slices.length; i++) {
            waveDisplay.addMarker(slices[i][0], "");
          }
        }
      }
    }
  }