import com.ulcjava.base.application.AbstractApplication;
import com.ulcjava.base.application.BorderFactory;
import com.ulcjava.base.application.ULCBoxPane;
import com.ulcjava.base.application.ULCButton;
import com.ulcjava.base.application.ULCDialog;
import com.ulcjava.base.application.ULCFrame;
import com.ulcjava.base.application.ULCLabel;
import com.ulcjava.base.application.ULCScrollPane;
import com.ulcjava.base.application.ULCTable;
import com.ulcjava.base.application.ULCWindow;
import com.ulcjava.base.application.event.ActionEvent;
import com.ulcjava.base.application.event.IActionListener;
import com.ulcjava.base.application.event.IWindowListener;
import com.ulcjava.base.application.event.WindowEvent;
import com.ulcjava.base.application.table.AbstractTableModel;
import com.ulcjava.base.application.util.BorderedComponentUtilities;
import com.ulcjava.base.application.util.Dimension;
import com.ulcjava.base.development.DevelopmentRunner;

public class TableAlertSnipped extends AbstractApplication {
  private final ULCFrame frame = new ULCFrame("TableAlertSnipped");
  private final ULCFrame popUpFrame = new ULCFrame("A new Frame");
  
  public void start() {    
    // This is the calling frame
    frame.setDefaultCloseOperation(ULCFrame.TERMINATE_ON_CLOSE);
    frame.setSize(new Dimension(200, 65));
    frame.setLocation(150, 150);
    frame.setVisible(true);
    frame.toFront();

    ULCBoxPane pane = new ULCBoxPane();
    ULCTable table = new ULCTable(new SnippetTableModel());
    pane.add(ULCBoxPane.BOX_EXPAND_EXPAND, new ULCScrollPane(table));
    
    popUpFrame.setDefaultCloseOperation(ULCFrame.DO_NOTHING_ON_CLOSE);
    popUpFrame.setSize(new Dimension(350, 350));
    popUpFrame.setLocation(150, 150);
    popUpFrame.add(BorderedComponentUtilities.createBorderedComponent(pane, ULCBoxPane.BOX_EXPAND_EXPAND, 
        BorderFactory.createEmptyBorder(5, 5, 5, 5)));
    
    popUpFrame.addWindowListener(closeAction); 
    
    ULCButton testBtn = new ULCButton("Click to open a new Frame");
    testBtn.addActionListener(new IActionListener() {
      public void actionPerformed(ActionEvent event) {
        showPopUp();
      }
    });
    
    frame.add(testBtn);
  }
  
  private IWindowListener closeAction = new IWindowListener() {
    public void windowClosing(WindowEvent event) {
      close();
    }
  };
  
  private void close() {
    final AlertBox alert = new AlertBox(popUpFrame, "Alert", "Want to save first?");
    alert.addWindowListener(new IWindowListener() {
      public void windowClosing(WindowEvent event) {
        if (alert.getValue().equals("Ok")) {
          // e.g. saving data ...
          hidePopUp();
        }
        
        // comment dispose for ULC 6.0
        alert.dispose();
      }
    });
  } 
  
  private void hidePopUp() {
    popUpFrame.setVisible(false);
  }
  
  private void showPopUp() {
    popUpFrame.setVisible(true);
    popUpFrame.toFront();
  }
  
  // --
  
  private class SnippetTableModel extends AbstractTableModel {
    public Object getValueAt(int row, int column) {
      return String.valueOf("xxxyyy");
    }
        
    public int getRowCount() {
      return 10;
    }

    public void setValueAt(Object value, int row, int column) {
      System.out.println("Setting: " + value.toString());
    }
    
    public int getColumnCount() {
      return 5;
    }
    
    public boolean isCellEditable(int i, int j) {
      return true;
    }
  }

  public static void main(String[] args) {
    DevelopmentRunner.setApplicationClass(TableAlertSnipped.class);
    DevelopmentRunner.main(args);
  }

  private class AlertBox extends ULCDialog {
    private static final long serialVersionUID = -8426765893446903437L;
    private String value;

    public AlertBox(ULCWindow parent, String title, String message) {
      super(parent);
      
      setTitle(title);
      setSize(new Dimension(150, 90));
      setModal(true);
      setResizable(false);
      setDefaultCloseOperation(ULCDialog.DISPOSE_ON_CLOSE);
      
      ULCBoxPane pane = new ULCBoxPane(true);
      pane.setVerticalGap(5);
      pane.add(ULCBoxPane.BOX_EXPAND_EXPAND, new ULCLabel(message));
      
      ULCBoxPane btnPane = new ULCBoxPane(false);
      
      ULCButton btn1 = new ULCButton("Ok");
      btn1.addActionListener(a);
      btnPane.add(btn1);

      ULCButton btn2 = new ULCButton("Cancel");
      btn2.addActionListener(a);
      btnPane.add(btn2);

      pane.add(ULCBoxPane.BOX_EXPAND_EXPAND, btnPane);
      
      this.add(pane);
      this.setVisible(true);
      this.toFront();
    }
    
    private IActionListener a = new IActionListener() {
      public void actionPerformed(final ActionEvent event) {
        // Sets the buttons text as return value of the dialog.
        setValue(((ULCButton) event.getSource()).getText());
        
        // ULC 6.0
        //distributeToListeners(new WindowEvent(this));
        // ULC 6.1        
        fireWindowClosing(new WindowEvent(this));
        setVisible(false);
      }
    };
    
    public String getValue() {
      return (value != null) ? value : "windowClosing";
    }

    private void setValue(String value) {
      this.value = value;
    }
  }
}
