/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:set ts=4 sw=4 sts=4 et: */
package jp.oist.flint.form;

import jp.oist.flint.desktop.Document;
import jp.oist.flint.desktop.IDesktopListener;
import jp.oist.flint.desktop.ISimulationListener;
import jp.oist.flint.executor.PhspSimulator;
import jp.oist.flint.form.sub.SubFrame;
import java.awt.Container;
import java.io.File;
import javax.swing.DefaultListModel;
import javax.swing.JScrollPane;

public class ProgressPane extends PeripheralPane
    implements IDesktopListener, ISimulationListener {

    public final static String TITLE = "Progresses";

    public final static String STOP_ACTION_COMMAND      = "progress.stop";

    public final static String LOG_ACTION_COMMAND       = "progress.log";

    public final static String JOB_ACTION_COMMAND      = "progress.job";

    private ProgressList mProgressList;

    public ProgressPane() {
        super(TITLE);
        initComponents();
    }

    protected Container createContentPane () {
        mProgressList = new ProgressList();
        JScrollPane scrollPane = new JScrollPane(mProgressList);

        return scrollPane;
    }

    private void initComponents () {
        setContentPane(createContentPane());
    }

    public void addRow (ProgressCell row) {
        DefaultListModel model = (DefaultListModel)mProgressList.getModel();
        model.addElement(row);
    }

    public void setSelectedCell (ProgressCell plcp, boolean selected) {
        mProgressList.setSelectedValue(plcp, selected);
    }

    private ProgressCell[]  getListCells () {
        DefaultListModel model = (DefaultListModel)mProgressList.getModel();
        int size = model.getSize();
        ProgressCell[] retvals = new ProgressCell[size];
        for (int i=0; i<size; i++) 
            retvals[i] = (ProgressCell)model.getElementAt(i);

        return  retvals;
    }

    public void removeListCell (ProgressCell cell) {
        DefaultListModel model = (DefaultListModel)mProgressList.getModel();
        model.removeElement(cell);
    }

    public ProgressCell getListCellOfModel (File modelFile) {
        DefaultListModel model = (DefaultListModel)mProgressList.getModel();
        int size = model.getSize();

        for (int i=0; i<size; i++) {
            ProgressCell cell = (ProgressCell)model.getElementAt(i);
            if (cell.getDocument().getFile().equals(modelFile))
                return cell;
        }

        return null;
    }

    /* IDesktopListener */

    @Override
    public void documentAdded(Document doc) {
        final SubFrame subFrame = doc.getSubFrame();
        ProgressCell plcp = new ProgressCell(doc, mProgressList);
        addRow(plcp);
        subFrame.setStatusComponent(plcp);
    }

    @Override
    public void documentRemoved(Document doc, boolean empty) {
        ProgressCell[] cells = getListCells();
        for (ProgressCell cell : cells) {
            if (doc.equals(cell.getDocument())) {
                removeListCell(cell);
                break;
            }
        }
    }

    /* ISimulationListener */

    @Override
    public void simulationStarted(PhspSimulator simulator) {
        for (ProgressCell cell : getListCells())
            cell.progressStarted();
        repaint();
    }

    @Override
    public void simulationDone() {
        repaint();
    }

    @Override
    public void simulationPaused() {
    }

    @Override
    public void simulationResumed() {
    }
}

@@ -400,16 +400,104 @@
 o.File;%0A
+import java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.Enumeration;%0A
 import j
@@ -946,32 +946,38 @@
 %22;%0A%0A    private 
+final 
 ProgressList mPr
@@ -1051,462 +1051,97 @@
     
-initComponents();%0A    %7D%0A%0A    protected Container createContentPane () %7B%0A        mProgressList = new ProgressList();%0A        JScrollPane scrollPane = new JScrollPane(mProgressList);%0A%0A        return scrollPane;%0A    %7D%0A%0A    private void initComponents () %7B%0A        setContentPane(createContentPane());%0A    %7D%0A%0A    public void addRow (ProgressCell row) %7B%0A        DefaultListModel model = (DefaultListModel)mProgressList.getModel();%0A        model.addElement(row
+mProgressList = new ProgressList();%0A        setContentPane(new JScrollPane(mProgressList)
 );%0A 
@@ -1293,16 +1293,26 @@
 private 
+ArrayList%3C
 Progress
@@ -1315,19 +1315,17 @@
 ressCell
-%5B%5D 
+%3E
  getList
@@ -1329,17 +1329,16 @@
 istCells
- 
 () %7B%0A   
@@ -1423,215 +1423,49 @@
     
-int size = model.getSize();%0A        ProgressCell%5B%5D retvals = new ProgressCell%5Bsize%5D;%0A        for (int i=0; i%3Csize; i++) %0A            retvals%5Bi%5D = (ProgressCell)
+return Collections.list(
 model.
-getE
+e
 lement
-At(i);%0A%0A        return  retvals
+s())
 ;%0A  
@@ -1470,29 +1470,30 @@
     %7D%0A%0A    p
-ublic
+rivate
  void remove
@@ -1496,25 +1496,24 @@
 moveListCell
- 
 (ProgressCel
@@ -1715,219 +1715,50 @@
     
-DefaultListModel model = (DefaultListModel)mProgressList.getModel();%0A        int size = model.getSize();%0A%0A        for (int i=0; i%3Csize; i++) %7B%0A            ProgressCell cell = (ProgressCell)model.getElementAt(i);
+for (ProgressCell cell : getListCells()) %7B
 %0A   
@@ -1853,25 +1853,24 @@
 ;%0A        %7D%0A
-%0A
         retu
@@ -2104,14 +2104,101 @@
     
-addRow
+DefaultListModel model = (DefaultListModel)mProgressList.getModel();%0A        model.addElement
 (plc
@@ -2328,32 +2328,37 @@
 mpty) %7B%0A        
+for (
 ProgressCell%5B%5D c
@@ -2357,18 +2357,15 @@
 Cell
-%5B%5D
  cell
-s =
+ :
  get
@@ -2379,48 +2379,8 @@
 ls()
-;%0A        for (ProgressCell cell : cells
 ) %7B%0A

Refactor ProgressPane's implementation