OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Compare Revisions

Regard whitespace Rev 141 → Rev 142

/trunk/OpenConcerto/src/org/openconcerto/ui/light/LightUITable.java
13,6 → 13,7
package org.openconcerto.ui.light;
 
import org.openconcerto.utils.NumberUtils;
import org.openconcerto.utils.io.JSONConverter;
 
import java.awt.event.ActionEvent;
20,15 → 21,29
import java.util.ArrayList;
import java.util.List;
 
import javax.xml.parsers.ParserConfigurationException;
 
import org.jdom2.Document;
import org.jdom2.Element;
 
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
 
public class LightUITable extends LightUIElement implements IUserControlContainer {
public class LightUITable extends LightUserControlContainer {
 
public static final int DEFAULT_LINE_HEIGHT = 40;
 
private static final String LINE_PER_ROW = "line-per-row";
private static final String TABLE_SPEC = "table-spec";
private static final String ALLOW_SELECTION = "allow-selection";
private static final String ALLOW_MULTI_SELECTION = "allow-multi-selection";
private static final String DYNAMIC_LOAD = "dynamic-load";
private static final String AUTO_SELECT_FIRST_LINE = "auto-select-first-line";
private Boolean dynamicLoad = false;
private Boolean verticallyScrollable = false;
private Boolean allowSelection = false;
private Boolean allowMultiSelection = false;
private Boolean autoSelectFirstLine = true;
private TableSpec tableSpec = null;
private String elementCode = null;
 
private List<ActionListener> selectionListeners = new ArrayList<ActionListener>();
 
35,6 → 50,8
// Nombre de ligne à afficher par Row
private int linePerRow = 1;
 
private int lineHeight = DEFAULT_LINE_HEIGHT;
 
// Init from json constructor
public LightUITable(final JSONObject json) {
super(json);
43,9 → 60,7
// Clone constructor
public LightUITable(final LightUITable tableElement) {
super(tableElement);
this.verticallyScrollable = tableElement.verticallyScrollable;
this.tableSpec = tableElement.tableSpec;
this.elementCode = tableElement.elementCode;
this.allowSelection = tableElement.allowSelection;
}
 
55,82 → 70,187
 
this.setWeightX(1);
this.setFillWidth(true);
}
 
public int getIndexfromRowID(int rowID) {
final TableContent content = this.getTableSpec().getContent();
final RowSelectionSpec selection = new RowSelectionSpec(this.getId());
final ColumnsSpec columnsSpec = new ColumnsSpec(this.getId(), new ArrayList<ColumnSpec>(), new ArrayList<String>(), new ArrayList<String>());
final TableSpec tableSpec = new TableSpec(this.getId(), selection, columnsSpec);
tableSpec.setContent(new TableContent(this.getId()));
 
for (int i = 0; i < content.getRows().size(); i++) {
Row r = content.getRows().get(i);
if (r.getId() == rowID) {
return i;
this.setTableSpec(tableSpec);
}
}
return -1;
}
 
@Override
public void setId(final String id) {
super.setId(id);
 
if (this.tableSpec != null) {
this.tableSpec.setId(id);
 
if (this.tableSpec.getSelection() != null) {
this.tableSpec.getSelection().setTableId(id);
}
 
public String getElementCode() {
return this.elementCode;
if (this.tableSpec.getContent() != null) {
this.tableSpec.getContent().setTableId(id);
}
 
public void setElementCode(final String elementCode) {
this.elementCode = elementCode;
}
}
 
public void setLinePerRow(int linePerRow) {
public final void setLinePerRow(int linePerRow) {
this.linePerRow = linePerRow;
}
 
public int getLinePerRow() {
public final int getLinePerRow() {
return this.linePerRow;
}
 
public TableSpec getTableSpec() {
public final void setLineHeight(int lineHeight) {
this.lineHeight = lineHeight;
}
 
public final int getLineHeight() {
return this.lineHeight;
}
 
public final TableSpec getTableSpec() {
return this.tableSpec;
}
 
public void setTableSpec(final TableSpec tableSpec) {
public final void setTableSpec(final TableSpec tableSpec) {
this.tableSpec = tableSpec;
}
 
public Boolean isAllowSelection() {
public final Boolean isAllowSelection() {
return this.allowSelection;
}
 
public void setAllowSelection(final boolean allowSelection) {
public final void setAllowSelection(final boolean allowSelection) {
this.allowSelection = allowSelection;
}
 
public Boolean isDynamicLoad() {
public final Boolean isAllowMultiSelection() {
return this.allowMultiSelection;
}
 
public final void setAllowMultiSelection(final boolean allowMultiSelection) {
this.allowMultiSelection = allowMultiSelection;
}
 
public final Boolean isDynamicLoad() {
return this.dynamicLoad;
}
 
public void setDynamicLoad(final boolean dynamicLoad) {
public final void setDynamicLoad(final boolean dynamicLoad) {
this.dynamicLoad = dynamicLoad;
}
 
public Boolean isVerticallyScrollable() {
return this.verticallyScrollable;
public final Boolean isAutoSelectFirstLine() {
return this.autoSelectFirstLine;
}
 
public void setVerticallyScrollable(final Boolean verticallyScrollable) {
this.verticallyScrollable = verticallyScrollable;
public final void setAutoSelectFirstLine(final boolean autoSelectFirstLine) {
this.autoSelectFirstLine = autoSelectFirstLine;
}
 
public boolean replaceChild(final LightUIElement pChild) {
final List<Row> tableRows = this.getTableSpec().getContent().getRows();
final int tableRowsCount = tableRows.size();
public final Row removeRow(final int index) {
return this.tableSpec.getContent().removeRow(index);
}
 
public final boolean removeRow(final Row row) {
final TableContent content = this.getTableSpec().getContent();
return content.removeRow(row);
}
 
public final boolean hasRow() {
return (this.tableSpec != null && this.tableSpec.getContent() != null && this.tableSpec.getContent().getRowsCount() > 0);
}
 
public final Row getRow(final int index) {
return this.getTableSpec().getContent().getRow(index);
}
 
public Row getRowById(final Number rowId) {
final int size = this.getTableSpec().getContent().getRowsCount();
for (int i = 0; i < size; i++) {
final Row row = this.getRow(i);
if (NumberUtils.areNumericallyEqual(row.getId(), rowId)) {
return row;
} else {
System.err.println("LightUITable.getSelectedRows() - Null selectedRow");
}
}
return null;
}
 
public final Row setRow(final int index, final Row row) {
return this.getTableSpec().getContent().setRow(index, row);
}
 
public final boolean addRow(final Row row) {
return this.getTableSpec().getContent().addRow(row);
}
 
public final int getRowsCount() {
return this.getTableSpec().getContent().getRowsCount();
}
 
public final void clearRows() {
this.getTableSpec().getContent().clearRows();
}
 
/**
* Get Ids of SQLRowAccessor store in selected rows
*
* @return The list of selected DB Ids
*/
public final List<Number> getSelectedIds() {
return this.getTableSpec().getSelection().getIds();
}
 
public final Number getFirstSelectedId() {
final List<Number> selectedIds = this.getTableSpec().getSelection().getIds();
if (selectedIds.isEmpty()) {
return null;
} else {
return selectedIds.get(0);
}
}
 
public final void setSelectedIds(final List<Number> selectedIds, final boolean fire) {
this.getTableSpec().getSelection().setIds(selectedIds);
if (fire) {
this.fireSelectionChange();
}
}
 
public final void clearSelection(final boolean fire) {
this.getTableSpec().getSelection().getIds().clear();
if (fire) {
this.fireSelectionChange();
}
}
 
public final List<Row> getSelectedRows() {
final List<Row> selectedRows = new ArrayList<Row>();
 
if (this.getTableSpec().getSelection() != null) {
final List<Number> selectedIds = this.getSelectedIds();
for (final Number selectedId : selectedIds) {
final Row selectedRow = this.getRowById(selectedId);
if (selectedRow != null) {
selectedRows.add(selectedRow);
}
}
}
 
return selectedRows;
}
 
public final boolean replaceChild(final LightUIElement pChild) {
pChild.setReadOnly(this.isReadOnly());
 
for (int i = 0; i < tableRowsCount; i++) {
final Row tableRow = tableRows.get(i);
for (int i = 0; i < this.getRowsCount(); i++) {
final Row tableRow = this.getTableSpec().getContent().getRow(i);
final List<Object> tableRowValues = tableRow.getValues();
final int tableRowValuesCount = tableRowValues.size();
 
160,17 → 280,15
return false;
}
 
public LightUIElement findElement(final String searchParam, final boolean byUUID) {
public final LightUIElement findElement(final String searchParam, final boolean byUUID) {
return this.findElement(searchParam, byUUID, LightUIElement.class);
}
 
public <T extends LightUIElement> T findElement(final String searchParam, final boolean byUUID, final Class<T> objectClass) {
if (this.tableSpec != null) {
final TableContent content = this.tableSpec.getContent();
if (content != null) {
final List<Row> listRows = content.getRows();
if (listRows != null) {
for (final Row row : listRows) {
public final <T extends LightUIElement> T findElement(final String searchParam, final boolean byUUID, final Class<T> objectClass) {
if (this.hasRow()) {
 
for (int i = 0; i < this.getRowsCount(); i++) {
final Row row = this.getRow(i);
final List<Object> rowValues = row.getValues();
for (final Object value : rowValues) {
if (value instanceof LightUIContainer) {
184,19 → 302,19
if (byUUID) {
if (element.getUUID().equals(searchParam)) {
if (objectClass.isAssignableFrom(element.getClass())) {
return (T) element;
return objectClass.cast(element);
} else {
throw new IllegalArgumentException("Element found at is not an instance of " + objectClass.getName() + ", element class: " + element.getClass().getName()
+ " element ID: " + element.getId());
throw new IllegalArgumentException(
"Element found at is not an instance of " + objectClass.getName() + ", element class: " + element.getClass().getName() + " element ID: " + element.getId());
}
}
} else {
if (element.getId().equals(searchParam)) {
if (objectClass.isAssignableFrom(element.getClass())) {
return (T) element;
return objectClass.cast(element);
} else {
throw new IllegalArgumentException("Element found at is not an instance of " + objectClass.getName() + ", element class: " + element.getClass().getName()
+ " element ID: " + element.getId());
throw new IllegalArgumentException(
"Element found at is not an instance of " + objectClass.getName() + ", element class: " + element.getClass().getName() + " element ID: " + element.getId());
}
}
}
213,37 → 331,128
} else {
System.out.println("LightUITable.getElementById() - No rows for table: " + this.getId());
}
} else {
System.out.println("LightUITable.getElementById() - Null TableContent for table: " + this.getId());
return null;
}
 
public <T extends LightUIElement> List<T> findChildren(final Class<T> expectedClass, final boolean recursively) {
final List<T> result = new ArrayList<T>();
 
if (this.hasRow()) {
final int size = this.getRowsCount();
for (int i = 0; i < size; i++) {
final Row row = this.getRow(i);
final List<Object> rowValues = row.getValues();
for (final Object value : rowValues) {
if (recursively) {
if (value instanceof LightUIContainer) {
result.addAll(((LightUIContainer) value).findChildren(expectedClass, recursively));
} else if (value instanceof LightUITable) {
result.addAll(((LightUITable) value).findChildren(expectedClass, recursively));
}
}
if (expectedClass.isAssignableFrom(value.getClass())) {
result.add(expectedClass.cast(value));
}
}
}
} else {
System.out.println("LightUITable.getElementById() - Null TableSpec for table: " + this.getId());
System.out.println("LightUITable.getElementById() - No rows for table: " + this.getId());
}
return null;
 
return result;
}
 
public void addSelectionListener(final ActionListener selectionListener) {
public final void addSelectionListener(final ActionListener selectionListener) {
this.selectionListeners.add(selectionListener);
}
 
public void removeSelectionListeners() {
public final void removeSelectionListeners() {
this.selectionListeners.clear();
}
 
public void fireSelectionChange() {
public final void fireSelectionChange() {
for (final ActionListener listener : this.selectionListeners) {
listener.actionPerformed(new ActionEvent(this, 1, "selection"));
}
}
 
// TODO: garder l'ordre des colonnes invisibles
/**
* Create columns preferences with the current ColumnsSpec
*
* @return XML document with columns preferences
*/
public final Document createXmlPreferences(final Document userPrefs, final ColumnsSpec columnsSpec) throws ParserConfigurationException {
 
final Element rootElement = new Element("list");
final Document xmlConf = new Document();
 
final int columnSpecCount = columnsSpec.getColumnCount();
final List<String> visibleIds = new ArrayList<String>();
for (int i = 0; i < columnSpecCount; i++) {
final ColumnSpec columnSpec = columnsSpec.getColumn(i);
final Element xmlColumn = this.createXmlColumn(columnSpec.getId(), columnSpec.getMaxWidth(), columnSpec.getMinWidth(), columnSpec.getWidth());
rootElement.addContent(xmlColumn);
visibleIds.add(columnSpec.getId());
}
 
final Element rootUserPrefs = userPrefs.getRootElement();
final List<Element> xmlColumns = rootUserPrefs.getChildren();
final int columnsSize = xmlColumns.size();
for (int i = 0; i < columnsSize; i++) {
final Element xmlColumn = xmlColumns.get(i);
final String columnId = xmlColumn.getAttribute("id").getValue();
if (!visibleIds.contains(columnId)) {
final int maxWidth = Integer.parseInt(xmlColumn.getAttribute("max-width").getValue());
final int minWidth = Integer.parseInt(xmlColumn.getAttribute("min-width").getValue());
final int width = Integer.parseInt(xmlColumn.getAttribute("width").getValue());
final Element newXmlColumn = this.createXmlColumn(columnId, maxWidth, minWidth, width);
rootElement.addContent(newXmlColumn);
}
}
xmlConf.setRootElement(rootElement);
return xmlConf;
}
 
/**
* Create default columns preferences from the SQLTableModelLinesSourceOnline
*
* @return XML document with columns preferences
*/
public Document createDefaultXmlPreferences() {
final Element rootElement = new Element("list");
 
if (this.getTableSpec() != null && this.getTableSpec().getColumns() != null) {
final int sqlColumnsCount = this.getTableSpec().getColumns().getColumnCount();
for (int i = 0; i < sqlColumnsCount; i++) {
final ColumnSpec column = this.getTableSpec().getColumns().getColumn(i);
final String columnId = column.getId();
final Element columnElement = this.createXmlColumn(columnId, column.getMaxWidth(), column.getMinWidth(), column.getWidth());
rootElement.addContent(columnElement);
}
}
final Document xmlConf = new Document(rootElement);
 
return xmlConf;
}
 
protected final Element createXmlColumn(final String columnId, final double maxWidth, final double minWidth, final double width) {
final Element columnElement = new Element("column");
columnElement.setAttribute("id", columnId);
columnElement.setAttribute("max-width", String.valueOf(maxWidth));
columnElement.setAttribute("min-width", String.valueOf(minWidth));
columnElement.setAttribute("width", String.valueOf(width));
return columnElement;
}
 
@Override
public void setReadOnly(final boolean readOnly) {
super.setReadOnly(readOnly);
final List<Row> rows = this.tableSpec.getContent().getRows();
if (rows != null) {
final int rowCount = rows.size();
for (int i = 0; i < rowCount; i++) {
final Row row = rows.get(i);
 
if (this.hasRow()) {
final int size = this.getRowsCount();
for (int i = 0; i < size; i++) {
final Row row = this.getRow(i);
final List<Object> values = row.getValues();
for (final Object value : values) {
if (value != null && value instanceof LightUIElement) {
265,7 → 474,7
}
 
@Override
public void setValueFromContext(final Object value) {
public void _setValueFromContext(final Object value) {
if (value != null) {
final JSONArray jsonContext = (JSONArray) JSONConverter.getObjectFromJSON(value, JSONArray.class);
final ColumnsSpec columnsSpec = this.getTableSpec().getColumns();
280,21 → 489,24
}
}
 
final TableContent tableContent = this.getTableSpec().getContent();
if (tableContent != null) {
final List<Row> rows = tableContent.getRows();
for (int i = 0; i < rows.size(); i++) {
final Row row = rows.get(i);
if (this.hasRow()) {
final int size = this.getRowsCount();
if (jsonContext.size() != size) {
System.err.println("LightUITable.setValueFromContext() - Incorrect line count in JSON");
} else {
 
for (int i = 0; i < size; i++) {
final Row row = this.getRow(i);
final JSONObject jsonLineContext = (JSONObject) JSONConverter.getObjectFromJSON(jsonContext.get(i), JSONObject.class);
final Long rowId = (Long) JSONConverter.getParameterFromJSON(jsonLineContext, "row.id", Long.class);
final Number rowId = JSONConverter.getParameterFromJSON(jsonLineContext, "row.id", Number.class);
final String rowExtendId = (String) JSONConverter.getParameterFromJSON(jsonLineContext, "row.extend.id", String.class);
if (rowId == row.getId() && (row.getExtendId() == null || (row.getExtendId() != null && rowExtendId.equals(row.getExtendId())))) {
if (NumberUtils.areNumericallyEqual(rowId, row.getId()) && (row.getExtendId() == null || (row.getExtendId() != null && rowExtendId.equals(row.getExtendId())))) {
if (row.isFillWidth()) {
if (!row.getValues().isEmpty() && row.getValues().get(0) instanceof IUserControl) {
if (!row.getValues().isEmpty() && row.getValues().get(0) instanceof LightUserControl) {
final LightUIElement element = (LightUIElement) row.getValues().get(0);
if (element instanceof IUserControl) {
if (element instanceof LightUserControl) {
if (jsonLineContext.containsKey(element.getUUID())) {
((IUserControl) element).setValueFromContext(jsonLineContext.get(element.getUUID()));
((LightUserControl) element)._setValueFromContext(jsonLineContext.get(element.getUUID()));
} else {
System.out.println("LightUITable.setValueFromContext() - Unable to find element : id - " + element.getId() + " uuid - " + element.getUUID());
System.out.println("LightUITable.setValueFromContext() - In JSON : " + jsonLineContext.toJSONString());
304,13 → 516,13
} else {
for (int k = 0; k < editorsIndex.size(); k++) {
final Object objEditor = row.getValues().get(editorsIndex.get(k));
if (!(objEditor instanceof IUserControl)) {
if (!(objEditor instanceof LightUserControl)) {
throw new IllegalArgumentException("Impossible to find editor for row: " + rowId.toString() + " at position: " + String.valueOf(k));
}
final LightUIElement editor = (LightUIElement) objEditor;
 
if (editor instanceof IUserControl && jsonLineContext.containsKey(editor.getUUID())) {
((IUserControl) editor).setValueFromContext(jsonLineContext.get(editor.getUUID()));
if (editor instanceof LightUserControl && jsonLineContext.containsKey(editor.getUUID())) {
((LightUserControl) editor)._setValueFromContext(jsonLineContext.get(editor.getUUID()));
} else {
throw new IllegalArgumentException(
"Impossible to find value for editor: " + editor.getId() + " for row: " + rowId.toString() + " at position: " + String.valueOf(k));
324,6 → 536,7
}
}
}
}
 
@Override
public LightUIElement clone() {
334,21 → 547,22
public JSONObject toJSON() {
final JSONObject json = super.toJSON();
if (this.allowSelection) {
json.put("allow-selection", true);
json.put(ALLOW_SELECTION, true);
}
if (this.allowMultiSelection) {
json.put(ALLOW_MULTI_SELECTION, true);
}
if (this.dynamicLoad) {
json.put("dynamic-load", true);
json.put(DYNAMIC_LOAD, true);
}
if (this.verticallyScrollable) {
json.put("vertically-scrollable", true);
if (!this.autoSelectFirstLine) {
json.put(AUTO_SELECT_FIRST_LINE, false);
}
if (this.tableSpec != null) {
json.put("table-spec", this.tableSpec.toJSON());
json.put(TABLE_SPEC, this.tableSpec.toJSON());
}
if (this.elementCode != null) {
json.put("element-code", this.elementCode);
}
json.put("line-per-row", this.linePerRow);
 
json.put(LINE_PER_ROW, this.linePerRow);
return json;
}
 
355,16 → 569,22
@Override
public void fromJSON(final JSONObject json) {
super.fromJSON(json);
this.allowSelection = JSONConverter.getParameterFromJSON(json, "allow-selection", Boolean.class, false);
this.dynamicLoad = JSONConverter.getParameterFromJSON(json, "dynamic-load", Boolean.class, false);
this.verticallyScrollable = JSONConverter.getParameterFromJSON(json, "vertically-scrollable", Boolean.class, false);
this.elementCode = JSONConverter.getParameterFromJSON(json, "element-code", String.class);
this.linePerRow = JSONConverter.getParameterFromJSON(json, "line-per-row", Integer.class);
this.allowSelection = JSONConverter.getParameterFromJSON(json, ALLOW_SELECTION, Boolean.class, false);
this.allowSelection = JSONConverter.getParameterFromJSON(json, ALLOW_MULTI_SELECTION, Boolean.class, false);
this.dynamicLoad = JSONConverter.getParameterFromJSON(json, DYNAMIC_LOAD, Boolean.class, false);
this.autoSelectFirstLine = JSONConverter.getParameterFromJSON(json, AUTO_SELECT_FIRST_LINE, Boolean.class, true);
this.linePerRow = JSONConverter.getParameterFromJSON(json, LINE_PER_ROW, Integer.class);
 
final JSONObject jsonRawContent = (JSONObject) JSONConverter.getParameterFromJSON(json, "table-spec", JSONObject.class);
final JSONObject jsonRawContent = (JSONObject) JSONConverter.getParameterFromJSON(json, TABLE_SPEC, JSONObject.class);
 
if (jsonRawContent != null) {
this.tableSpec = new TableSpec(jsonRawContent);
}
}
 
@Override
public void destroy() {
super.destroy();
this.selectionListeners.clear();
}
}