use of org.apache.pivot.collections.ArrayList in project pivot by apache.
the class CSVSerializerTest method testBasicWriteObject.
@SuppressWarnings("unchecked")
// or it will generate a warning during build with Java 7
@Test
public void testBasicWriteObject() throws IOException {
List<Object> items = new ArrayList<>();
items.add(new HashMap<>(new Dictionary.Pair<String, Object>("A", "a1"), new Dictionary.Pair<String, Object>("B", "b1"), new Dictionary.Pair<String, Object>("C", "c1")));
items.add(new HashMap<>(new Dictionary.Pair<String, Object>("A", "a2"), new Dictionary.Pair<String, Object>("B", "b2"), new Dictionary.Pair<String, Object>("C", "c2")));
StringWriter writer = new StringWriter();
CSVSerializer serializer = new CSVSerializer();
serializer.setKeys("A", "B", "C");
serializer.writeObject(items, writer);
assertEquals("a1,b1,c1\r\na2,b2,c2\r\n", writer.toString());
}
use of org.apache.pivot.collections.ArrayList in project pivot by apache.
the class CSVSerializerTest method testQuotedNewlineWriteObject.
@SuppressWarnings("unchecked")
// or it will generate a warning during build with Java 7
@Test
public void testQuotedNewlineWriteObject() throws IOException {
List<Object> items = new ArrayList<>();
items.add(new HashMap<>(new Dictionary.Pair<String, Object>("A", "a"), new Dictionary.Pair<String, Object>("B", "\nb\n"), new Dictionary.Pair<String, Object>("C", "c")));
StringWriter writer = new StringWriter();
CSVSerializer serializer = new CSVSerializer();
serializer.setKeys("A", "B", "C");
serializer.writeObject(items, writer);
assertEquals("a,\"\nb\n\",c\r\n", writer.toString());
}
use of org.apache.pivot.collections.ArrayList in project pivot by apache.
the class CSVSerializer method readObject.
/**
* Reads values from a comma-separated value stream.
*
* @param reader The reader from which data will be read.
* @return A list containing the data read from the CSV file. The list items
* are instances of <tt>Dictionary<String, Object></tt> populated by mapping columns
* in the CSV file to keys in the key sequence. <p> If no keys have been
* specified when this method is called, they are assumed to be defined in
* the first line of the file.
* @throws IOException for any errors during reading.
* @throws SerializationException for any formatting errors with the data.
* @throws IllegalArgumentException for {@code null} input reader.
*/
public List<?> readObject(Reader reader) throws IOException, SerializationException {
Utils.checkNull(reader, "reader");
LineNumberReader lineNumberReader = new LineNumberReader(reader);
if (keys.getLength() == 0) {
// Read keys from first line
String line = lineNumberReader.readLine();
if (line == null) {
throw new SerializationException("Could not read keys from input.");
}
String[] keysLocal = line.split(",");
this.keys = new ArrayList<>(keysLocal.length);
for (int i = 0; i < keysLocal.length; i++) {
String key = keysLocal[i];
this.keys.add(key.trim());
}
}
// Create the list and notify the listeners
ArrayList<Object> items = new ArrayList<>();
if (csvSerializerListeners != null) {
csvSerializerListeners.beginList(this, items);
}
// Move to the first character
c = lineNumberReader.read();
// Ignore BOM (if present)
if (c == 0xFEFF) {
c = lineNumberReader.read();
}
try {
while (c != -1) {
Object item = readItem(lineNumberReader);
while (item != null) {
items.add(item);
// Move to next line
while (c != -1 && (c == '\r' || c == '\n')) {
c = lineNumberReader.read();
}
// Read the next item
item = readItem(lineNumberReader);
}
}
} catch (SerializationException exception) {
System.err.println("An error occurred while processing input at line number " + (lineNumberReader.getLineNumber() + 1));
throw exception;
}
// Notify the listeners
if (csvSerializerListeners != null) {
csvSerializerListeners.endList(this);
}
return items;
}
use of org.apache.pivot.collections.ArrayList in project pivot by apache.
the class FlowPaneSkin method layout.
@Override
public void layout() {
FlowPane flowPane = (FlowPane) getComponent();
int width = getWidth();
int contentWidth = Math.max(width - padding.getWidth(), 0);
// Break the components into multiple rows
ArrayList<ArrayList<Component>> rows = new ArrayList<>();
ArrayList<Component> row = new ArrayList<>();
int rowWidth = 0;
for (int i = 0, n = flowPane.getLength(); i < n; i++) {
Component component = flowPane.get(i);
if (component.isVisible()) {
Dimensions componentSize = component.getPreferredSize();
component.setSize(componentSize);
if (rowWidth + componentSize.width > contentWidth && rowWidth > 0) {
// The component is too big to fit in the remaining space,
// and it is not the only component in this row
rows.add(row);
row = new ArrayList<>();
rowWidth = 0;
}
// Add the component to the row
row.add(component);
rowWidth += componentSize.width + horizontalSpacing;
}
}
// Add the last row
if (row.getLength() > 0) {
rows.add(row);
}
// Lay out the rows
int rowY = padding.top;
for (int i = 0, n = rows.getLength(); i < n; i++) {
row = rows.get(i);
// Determine the row dimensions
rowWidth = 0;
int rowHeight = 0;
int baseline = -1;
for (Component component : row) {
rowWidth += component.getWidth();
rowHeight = Math.max(rowHeight, component.getHeight());
baseline = Math.max(baseline, component.getBaseline(component.getWidth(), component.getHeight()));
}
rowWidth += horizontalSpacing * (row.getLength() - 1);
int x = 0;
switch(alignment) {
case LEFT:
{
x = padding.left;
break;
}
case CENTER:
{
x = (width - rowWidth) / 2;
break;
}
case RIGHT:
{
x = width - rowWidth - padding.right;
break;
}
default:
{
break;
}
}
for (Component component : row) {
int y;
int componentBaseline = component.getBaseline(component.getWidth(), component.getHeight());
if (alignToBaseline && baseline != -1 && componentBaseline != -1) {
// Align to baseline
y = baseline - componentBaseline;
} else {
// Align to bottom
y = rowHeight - component.getHeight();
}
component.setLocation(x, y + rowY);
x += (component.getWidth() + horizontalSpacing);
}
rowY += (rowHeight + verticalSpacing);
}
}
use of org.apache.pivot.collections.ArrayList in project pivot by apache.
the class TerraTableViewSkin method getVariableRowHeight.
protected int getVariableRowHeight(int rowIndex, ArrayList<Integer> columnWidthsArgument) {
TableView tableView = (TableView) getComponent();
@SuppressWarnings("unchecked") List<Object> tableData = (List<Object>) tableView.getTableData();
TableView.ColumnSequence columns = tableView.getColumns();
Object rowData = tableData.get(rowIndex);
int rowHeight = 0;
for (int i = 0, n = columns.getLength(); i < n; i++) {
TableView.Column column = columns.get(i);
TableView.CellRenderer cellRenderer = column.getCellRenderer();
cellRenderer.render(rowData, rowIndex, i, tableView, column.getName(), false, false, false);
rowHeight = Math.max(rowHeight, cellRenderer.getPreferredHeight(columnWidthsArgument.get(i).intValue()));
}
return rowHeight;
}
Aggregations