package org.isatools.isacreator.plugins;
import org.isatools.isacreator.plugins.host.service.PluginSpreadsheetWidget;
import javax.swing.*;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.table.TableCellEditor;
import java.awt.*;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;
/**
* Created by the ISA team
*/
public abstract class AbstractPluginSpreadsheetWidget extends JTextField implements PluginSpreadsheetWidget, TableCellEditor {
protected transient List<CellEditorListener> listeners;
protected JTable currentTable;
protected String originalValue;
protected int currentRow;
protected int currentColumn;
protected AbstractPluginSpreadsheetWidget() {
instantiateComponent();
setBorder(null);
listeners = new ArrayList<CellEditorListener>();
}
/**
* Add a CellEditorListener to the list of CelleEditorListeners available
*
* @param cel - A CellEditorListener to add
*/
public void addCellEditorListener(CellEditorListener cel) {
listeners.add(cel);
}
/**
* CancelCellEditing method is required by the TableCellEditor interface.
* Stops editing of the cell and hides the OntologySelectionTool.
*/
public void cancelCellEditing() {
fireEditingCanceled();
}
/**
* Stops editing on a cell. Called when the user wants to commit the change rather than reverting to the
* previous value entered.
*/
protected void fireEditingStopped() {
ChangeEvent ce = new ChangeEvent(this);
for (int i = listeners.size() - 1; i >= 0; i--) {
(listeners.get(i)).editingStopped(ce);
}
}
/**
* Return the value in the editor
*
* @return An object representing the value in the editor.
*/
public Object getCellEditorValue() {
return getText();
}
/**
* Return the value in the editor
*
* @return An object representing the value in the editor.
*/
public String getOriginalValue() {
return originalValue;
}
/**
* FireEditingCanceled method sets text back to its original value, and cancels editing
* by referring to the list of CellEditorListener objects and calling the editingStopped method with
* a ChangeEvent defined using the OntologyCellEditor as a reference.
*/
protected void fireEditingCanceled() {
setText(getCellValue());
ChangeEvent ce = new ChangeEvent(this);
for (int i = listeners.size() - 1; i >= 0; i--) {
(listeners.get(i)).editingCanceled(ce);
}
}
/**
* Always returns true since the date field should always be editable.
*
* @param eo EventObject
* @return true, always.
*/
public boolean isCellEditable(EventObject eo) {
return true;
}
/**
* Removes a CellEditorListener from the list of listeners.
*
* @param cel - CellEditorListener to remove
*/
public void removeCellEditorListener(CellEditorListener cel) {
listeners.remove(cel);
}
/**
* Always returns true. Method indicates whether a user should be able to to select a cell.
*
* @param eo - EventObject.
* @return true
*/
public boolean shouldSelectCell(EventObject eo) {
return true;
}
/**
* Stop Editing of a cell component and set the cell value to be the term(s) selected in the OntologySelectionTool.
*
* @return true
*/
public boolean stopCellEditing() {
fireEditingStopped();
setText(getCellValue());
return true;
}
protected void setCellValue(String value) {
currentTable.setValueAt(value, currentRow, currentColumn);
}
/**
* Gets the component used to edit a cell - in this case, the OntologyCellEditor.
*
* @param table - JTable being edited.
* @param value - /
* @param isSelected - is the cell currently selected
* @param row - the row identifier for the cell
* @param column - the column identifier for the cell
* @return The editing component. A OntologyCellEditor.
*/
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected, int row, int column) {
currentTable = table;
currentRow = row;
currentColumn = column;
table.setRowSelectionInterval(row, row);
table.setColumnSelectionInterval(column, column);
if (table.getValueAt(row, column) != null) {
originalValue = table.getValueAt(row, column).toString();
} else {
originalValue = "";
}
Point p = table.getLocationOnScreen();
Rectangle r = table.getCellRect(row, column, true);
int proposedX = r.x + p.x + getWidth();
int proposedY = r.y + p.y + getHeight();
setOnScreenLocation(new Point(proposedX, proposedY));
showComponent();
return this;
}
public abstract void instantiateComponent();
public abstract void hideComponent();
public abstract void showComponent();
public abstract String getCellValue();
public abstract void setOnScreenLocation(Point proposedLocation);
}