/*******************************************************************************
 * Copyright (c) 2009-2012, University of Manchester
 * 
 * Licensed under the New BSD License. 
 * Please see LICENSE file that is distributed with the source code
 ******************************************************************************/
package uk.ac.manchester.cs.owl.semspreadsheets.ui;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Stroke;

import javax.swing.JTable;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import uk.ac.manchester.cs.owl.semspreadsheets.model.OntologyTermValidation;
import uk.ac.manchester.cs.owl.semspreadsheets.model.Range;
import uk.ac.manchester.cs.owl.semspreadsheets.model.Sheet;
import uk.ac.manchester.cs.owl.semspreadsheets.model.WorkbookManager;

/**
 * @author Stuart Owen
 * @author Matthew Horridge
 */
@SuppressWarnings("serial")
public class SheetTable extends JTable {
    
    private Sheet sheet;

    private WorkbookManager workbookManager;
    
    private SpreadSheetCellEditor editor;    

	public SheetTable(WorkbookManager ss, Sheet sheet) {
        this.workbookManager = ss;
        this.sheet = sheet;        

        setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        TableModel model = new SheetTableModel(sheet);
        setModel(model);                      

        setCellSelectionEnabled(true);

        SheetCellRenderer ren = new SheetCellRenderer();
        editor = new SpreadSheetCellEditor();

        for (int col = 0; col < sheet.getMaxColumns(); col++) {        	
            TableColumn column = getColumnModel().getColumn(col);
            column.setCellRenderer(ren);
            column.setCellEditor(editor);
            column.setPreferredWidth(sheet.getColumnWidth(col));            
        }

        setShowGrid(true);
        setRowMargin(1);
        setGridColor(new Color(206, 206, 206));
    }    

    public Sheet getSheet() {
        return sheet;
    }
    
    public boolean stopCellEditing() {
		return editor.stopCellEditing();
	}

    private Stroke stroke = new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);

    private AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.15f);

    private AlphaComposite alphaComposite2 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.70f);

    private Color color = new Color(50, 150, 60);

    private Color emptyValidationColor = Color.DARK_GRAY;

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Color oldColor = g.getColor();
        Graphics2D g2 = (Graphics2D) g;
        Stroke oldStroke = g2.getStroke();
        g2.setStroke(stroke);
        for (OntologyTermValidation ontologyTermValidation : workbookManager.getOntologyManager().getOntologyTermValidations()) {
            if (ontologyTermValidation.getRange().getSheet().equals(sheet)) {
                Range validation = ontologyTermValidation.getRange();
                if(ontologyTermValidation.getValidationDescriptor().getTerms().isEmpty()) {
                    g.setColor(emptyValidationColor);
                }
                else {
                    g.setColor(color);
                }
                Rectangle startRect = getCellRect(validation.getFromRow(), validation.getFromColumn(), false);
                Rectangle endRect = getCellRect(validation.getToRow(), validation.getToColumn(), false);
                int x1 = startRect.x + 1;
                int y1 = startRect.y + 1;
                int width = endRect.width + endRect.x - startRect.x - 2;
                int height = endRect.y + endRect.height - startRect.y - 2;
                Rectangle rect = new Rectangle(x1, y1, width, height);
                Composite oldComposite = g2.getComposite();
                g2.setComposite(alphaComposite2);
                g2.drawRoundRect(rect.x, rect.y, rect.width, rect.height, 5, 5);
                g2.setComposite(alphaComposite);
                g2.fill(rect);
                g2.setComposite(oldComposite);
            }
        }

        g2.setStroke(oldStroke);
        g.setColor(oldColor);
    }
}

@@ -770,16 +770,103 @@
 dation;%0A
+import uk.ac.manchester.cs.owl.semspreadsheets.model.OntologyTermValidationDescriptor;%0A
 import u
@@ -2618,20 +2618,38 @@
 e Color 
-c
+validationAppliedC
 olo
+u
 r = new 
@@ -2702,24 +2702,25 @@
 lidationColo
+u
 r = Color.DA
@@ -3260,59 +3260,195 @@
     
-if(ontologyTermValidation.getValidationDescriptor()
+OntologyTermValidationDescriptor validationDescriptor = ontologyTermValidation.getValidationDescriptor();%0A                if(!validationDescriptor.definesLiteral() && validationDescriptor
 .get
@@ -3514,24 +3514,25 @@
 lidationColo
+u
 r);%0A        
@@ -3599,12 +3599,30 @@
 lor(
-c
+validationAppliedC
 olo
+u
 r);%0A

make the cell green (in RF) if a property is applied as free text - other than grey indicating missing terms. Still shows grey if subclasses or instances have been selected but there are no terms