Skip to content

Commit 7641864

Browse files
authored
Merge pull request #121 from Caltech-IPAC/DM-6952_table_problems
DM-6952: Table problems
2 parents b6deca5 + 1b0e2cd commit 7641864

File tree

10 files changed

+82
-50
lines changed

10 files changed

+82
-50
lines changed

src/firefly/java/edu/caltech/ipac/firefly/server/util/ipactable/DataGroupFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ void start() throws IOException {
9393
StopWatch.getInstance().start("DataGroupFilter");
9494

9595
TableDef tableMeta = IpacTableUtil.getMetaInfo(source);
96-
List<DataGroup.Attribute> attributes = tableMeta.getAttributes();
96+
List<DataGroup.Attribute> attributes = tableMeta.getAllAttributes();
9797
List<DataType> headers = tableMeta.getCols();
9898

9999
writer = new PrintWriter(new BufferedWriter(new FileWriter(this.outf), IpacTableUtil.FILE_IO_BUFFER_SIZE));

src/firefly/java/edu/caltech/ipac/firefly/server/util/ipactable/DataGroupReader.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ public static DataGroup read(File inf, boolean isFixedLength, boolean readAsStri
7575

7676
public static DataGroup read(File inf, boolean isFixedLength, boolean readAsString, boolean saveFormattedData, String... onlyColumns) throws IOException {
7777

78-
TableDef tableMeta = IpacTableUtil.getMetaInfo(inf);
79-
List<DataGroup.Attribute> attributes = tableMeta.getAttributes();
80-
List<DataType> cols = tableMeta.getCols();
78+
TableDef tableDef = IpacTableUtil.getMetaInfo(inf);
79+
List<DataGroup.Attribute> attributes = tableDef.getAllAttributes();
80+
List<DataType> cols = tableDef.getCols();
8181

8282
if (readAsString) {
8383
for (DataType dt : cols) {

src/firefly/java/edu/caltech/ipac/firefly/server/util/ipactable/IpacTableParser.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ public static MappedData getData(File inf, Collection<Integer> indices, String..
8282

8383

8484
public static DataGroupPart getData(File inf, int start, int rows) throws IOException {
85-
TableDef meta = IpacTableUtil.getMetaInfo(inf);
85+
TableDef tableDef = IpacTableUtil.getMetaInfo(inf);
8686

87-
DataGroup dg = new DataGroup(null, meta.getCols());
87+
DataGroup dg = new DataGroup(null, tableDef.getCols());
8888
dg.setRowIdxOffset(start);
8989

90-
dg.setAttributes(meta.getAttributes());
90+
dg.setAttributes(tableDef.getAllAttributes());
9191

9292
RandomAccessFile reader = new RandomAccessFile(inf, "r");
93-
long skip = ((long)start * (long)meta.getLineWidth()) + (long)meta.getRowStartOffset();
93+
long skip = ((long)start * (long)tableDef.getLineWidth()) + (long)tableDef.getRowStartOffset();
9494
int count = 0;
9595
try {
9696
reader.seek(skip);
@@ -108,9 +108,9 @@ public static DataGroupPart getData(File inf, int start, int rows) throws IOExce
108108
reader.close();
109109
}
110110

111-
long totalRow = meta.getLineWidth() == 0 ? 0 :
112-
(inf.length() - meta.getRowStartOffset())/meta.getLineWidth();
113-
return new DataGroupPart(meta, dg, start, (int) totalRow);
111+
long totalRow = tableDef.getLineWidth() == 0 ? 0 :
112+
(inf.length() - tableDef.getRowStartOffset())/tableDef.getLineWidth();
113+
return new DataGroupPart(tableDef, dg, start, (int) totalRow);
114114
}
115115

116116
//====================================================================

src/firefly/java/edu/caltech/ipac/firefly/server/util/ipactable/JsonTableUtil.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,13 @@ public static JSONObject toJsonTableData(DataGroup data, TableDef tableDef) {
123123
/**
124124
* convert to JSON TableMeta
125125
*
126-
* @param meta
126+
* @param tableDef
127127
* @return
128128
*/
129-
public static JSONObject toJsonTableMeta(TableDef meta) {
129+
public static JSONObject toJsonTableMeta(TableDef tableDef) {
130130
JSONObject tmeta = new JSONObject();
131-
for (DataGroup.Attribute att : meta.getAttributes()) {
132-
if (!att.isComment()) {
133-
tmeta.put(att.getKey(), att.getValue());
134-
}
131+
for (DataGroup.Attribute att : tableDef.getAttributes()) {
132+
tmeta.put(att.getKey(), att.getValue());
135133
}
136134
return tmeta;
137135
}
@@ -140,9 +138,9 @@ public static JSONObject toJsonTableMeta(TableDef meta) {
140138
//
141139
//====================================================================
142140

143-
private static List<JSONObject> toJsonTableColumn(DataGroup dataGroup, TableDef meta) {
141+
private static List<JSONObject> toJsonTableColumn(DataGroup dataGroup, TableDef tableDef) {
144142

145-
DataType[] dataTypes = meta.getCols().size() > 0 ? meta.getCols().toArray(new DataType[0]) : dataGroup.getDataDefinitions();
143+
DataType[] dataTypes = tableDef.getCols().size() > 0 ? tableDef.getCols().toArray(new DataType[0]) : dataGroup.getDataDefinitions();
146144

147145
ArrayList<JSONObject> cols = new ArrayList<JSONObject>();
148146
for (DataType dt :dataTypes) {
@@ -162,48 +160,48 @@ private static List<JSONObject> toJsonTableColumn(DataGroup dataGroup, TableDef
162160
}
163161

164162
// modify column's attributes based on meta
165-
String label = getColAttr(meta, LABEL_TAG, cname);
163+
String label = getColAttr(tableDef, LABEL_TAG, cname);
166164
if (!StringUtils.isEmpty(label)) {
167165
c.put("label", label);
168166
}
169-
String desc = getColAttr(meta, DESC_TAG, cname);
167+
String desc = getColAttr(tableDef, DESC_TAG, cname);
170168
if (!StringUtils.isEmpty(desc)) {
171169
c.put("desc", desc);
172170
}
173-
String visibility = getColAttr(meta, VISI_TAG, cname);
171+
String visibility = getColAttr(tableDef, VISI_TAG, cname);
174172
if (!StringUtils.isEmpty(visibility)) {
175173
c.put("visibility", visibility);
176174
}
177-
String width = getColAttr(meta, WIDTH_TAG, cname);
175+
String width = getColAttr(tableDef, WIDTH_TAG, cname);
178176
if (!StringUtils.isEmpty(width)) {
179177
c.put("width", width);
180178
}
181-
String prefWidth = getColAttr(meta, PREF_WIDTH_TAG, cname);
179+
String prefWidth = getColAttr(tableDef, PREF_WIDTH_TAG, cname);
182180
if (!StringUtils.isEmpty(prefWidth)) {
183181
c.put("prefWidth", prefWidth);
184182
}
185-
String sortable = getColAttr(meta, SORTABLE_TAG, cname);
183+
String sortable = getColAttr(tableDef, SORTABLE_TAG, cname);
186184
if (!StringUtils.isEmpty(sortable)) {
187185
c.put("sortable", sortable);
188186
}
189-
String units = getColAttr(meta, UNIT_TAG, cname);
187+
String units = getColAttr(tableDef, UNIT_TAG, cname);
190188
if (!StringUtils.isEmpty(units)) {
191189
c.put("units", units);
192190
}
193-
String items = getColAttr(meta, ITEMS_TAG, cname);
191+
String items = getColAttr(tableDef, ITEMS_TAG, cname);
194192
if (!StringUtils.isEmpty(items)) {
195193
c.put("items", items);
196194
}
197-
String sortBy = getColAttr(meta, SORT_BY_TAG, cname);
195+
String sortBy = getColAttr(tableDef, SORT_BY_TAG, cname);
198196
if (!StringUtils.isEmpty(sortBy)) {
199197
c.put("sortByCols", sortBy);
200198
}
201199
cols.add(c);
202200
}
203-
for (DataGroup.Attribute att : meta.getAttributes()) {
201+
for (DataGroup.Attribute att : tableDef.getAttributes()) {
204202
// clean up all of the column's attributes since we already set it to the columns
205203
if (att.getKey().startsWith("col.")) {
206-
meta.removeAttribute(att.getKey());
204+
tableDef.removeAttribute(att.getKey());
207205
}
208206
}
209207
return cols;

src/firefly/java/edu/caltech/ipac/firefly/server/util/ipactable/TableDef.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
public class TableDef {
2020
private List<DataType> cols = new ArrayList<DataType>();
21+
private ArrayList<DataGroup.Attribute> allAttributes = new ArrayList<>();
2122
private HashMap<String, DataGroup.Attribute> attributes = new HashMap<>();
2223
private int lineWidth;
2324
private int rowCount;
@@ -27,28 +28,47 @@ public class TableDef {
2728
public void addAttributes(DataGroup.Attribute... attributes) {
2829
if (attributes != null) {
2930
for(DataGroup.Attribute a : attributes) {
30-
this.attributes.put(a.getKey(), a);
31+
if (contains(a.getKey())) {
32+
removeAttribute(a.getKey());
33+
}
34+
allAttributes.add(a);
35+
if (!a.isComment()) {
36+
this.attributes.put(a.getKey(), a);
37+
}
3138
}
3239
}
3340
}
41+
3442
public void setCols(List<DataType> cols) { this.cols = cols; }
3543

3644
public List<DataType> getCols() {
3745
return cols;
3846
}
3947

48+
/**
49+
* returns all of the attributes including comments. This function returns the attributes
50+
* in the order it was added.
51+
* @return
52+
*/
53+
public List<DataGroup.Attribute> getAllAttributes() { return allAttributes; }
54+
55+
/**
56+
* return non-comment attributes.
57+
* @return
58+
*/
4059
public List<DataGroup.Attribute> getAttributes() {
4160
return new ArrayList<>(attributes.values());
4261
}
4362

4463
public boolean contains(String name) { return attributes.containsKey(name);};
4564

46-
public void setAttribute(String name, String value) {
47-
attributes.put(name, new DataGroup.Attribute(name, value));
48-
}
65+
public void setAttribute(String name, String value) { addAttributes(new DataGroup.Attribute(name, value)); }
4966

5067
public void removeAttribute(String name) {
51-
attributes.remove(name);
68+
DataGroup.Attribute removed = attributes.remove(name);
69+
if (removed != null) {
70+
allAttributes.remove(removed);
71+
}
5272
}
5373

5474
public void setStatus(DataGroupPart.State status) {
@@ -154,6 +174,7 @@ public TableDef clone() {
154174
TableDef copy = new TableDef();
155175
copy.cols = new ArrayList<>(cols);
156176
copy.attributes = new HashMap<>(attributes);
177+
copy.allAttributes = new ArrayList<>(allAttributes);
157178
copy.lineWidth = lineWidth;
158179
copy.rowCount = rowCount;
159180
copy.rowStartOffset = rowStartOffset;

src/firefly/java/edu/caltech/ipac/util/IpacTableUtil.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,20 @@ public class IpacTableUtil {
4040

4141
public static void writeAttributes(PrintWriter writer, Collection<DataGroup.Attribute> attribs, String... ignoreList) {
4242
if (attribs == null) return;
43-
43+
// write attributes first
4444
for (DataGroup.Attribute kw : attribs) {
4545
if (ignoreList == null || !CollectionUtil.exists(ignoreList, kw.getKey())) {
46-
writer.println(kw.toString());
46+
if (!kw.isComment()) {
47+
writer.println(kw.toString());
48+
}
49+
}
50+
}
51+
// then write comments
52+
for (DataGroup.Attribute kw : attribs) {
53+
if (ignoreList == null || !CollectionUtil.exists(ignoreList, kw.getKey())) {
54+
if (kw.isComment()) {
55+
writer.println(kw.toString());
56+
}
4757
}
4858
}
4959
}
@@ -306,10 +316,6 @@ public static TableDef getMetaInfo(File inf) throws IOException {
306316

307317
public static TableDef getMetaInfo(BufferedReader reader, File src) throws IOException {
308318
TableDef meta = new TableDef();
309-
if (src != null) {
310-
meta.setSource(src.getAbsolutePath());
311-
}
312-
313319
int nlchar = findLineSepLength(reader);
314320
meta.setLineSepLength(nlchar);
315321

@@ -376,6 +382,9 @@ public static TableDef getMetaInfo(BufferedReader reader, File src) throws IOExc
376382
(src.length() - (long) meta.getRowStartOffset()) / meta.getLineWidth();
377383
meta.setRowCount((int) totalRow);
378384
}
385+
if (src != null) {
386+
meta.setSource(src.getAbsolutePath());
387+
}
379388
return meta;
380389
}
381390

src/firefly/js/tables/TableUtil.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,13 +468,13 @@ export function getTableSourceUrl(columns, request, filename) {
468468
return col.name;
469469
} );
470470
if (visiCols.length !== columns.length) {
471-
request['inclCols'] = visiCols.toString();
471+
Request['inclCols'] = visiCols.toString();
472472
}
473473
Request.startIdx = 0;
474474
Request.pageSize = Number.MAX_SAFE_INTEGER;
475475
Reflect.deleteProperty(Request, 'tbl_id');
476476
const file_name = filename || Request.file_name;
477-
return encodeServerUrl(SAVE_TABLE_URL, {file_name, Request: request});
477+
return encodeServerUrl(SAVE_TABLE_URL, {file_name, Request});
478478
}
479479

480480
/**

src/firefly/js/tables/ui/FilterEditor.jsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {BasicTableView} from './BasicTableView.jsx';
1010
import {SelectInfo} from '../SelectInfo.js';
1111
import {createInputCell} from './TableRenderer.js';
1212
import {FILTER_CONDITION_TTIPS, FILTER_TTIPS, FilterInfo} from '../FilterInfo.js';
13-
import {sortTableData} from '../TableUtil.js';
13+
import {sortTableData, calcColumnWidths} from '../TableUtil.js';
1414
import {InputAreaField} from '../../ui/InputAreaField.jsx';
1515
// import {deepDiff} from '../../util/WebUtil.js';
1616

@@ -85,9 +85,9 @@ FilterEditor.defaultProps = {
8585
function prepareOptionData(columns, sortInfo, filterInfo) {
8686

8787
var cols = [
88-
{name: 'Column', visibility: 'show', prefWidth: 12, fixed: true},
88+
{name: 'Column', visibility: 'show', fixed: true},
8989
{name: 'Filter', visibility: 'show', prefWidth: 12},
90-
{name: 'Units', visibility: 'show', prefWidth: 6},
90+
{name: 'Units', visibility: 'show'},
9191
{name: 'Description', visibility: 'show', prefWidth: 60},
9292
{name: 'Selected', visibility: 'hidden'}
9393
];
@@ -98,6 +98,9 @@ function prepareOptionData(columns, sortInfo, filterInfo) {
9898
const filter = filterInfoCls.getFilter(v.name) || '';
9999
return [v.name||'', filter, v.units||'', v.desc||'', v.visibility !== 'hide'];
100100
} );
101+
const widths = calcColumnWidths(cols, data);
102+
cols[0].prefWidth = Math.min(widths['Columns'], 12); // adjust width of column for optimum display.
103+
cols[2].prefWidth = Math.min(widths['Units'], 12);
101104
sortTableData(data, cols, sortInfo);
102105

103106
var selectInfoCls = SelectInfo.newInstance({rowCount: data.length});

src/firefly/js/tables/ui/TablePanelOptions.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export class TablePanelOptions extends React.Component {
5050
</div>
5151
</div>
5252
<div style={{display: 'inline-block'}}>
53-
<div style={{float: 'left'}}>
53+
<div style={{marginTop: 17}}>
5454
<InputField
5555
validator={intValidator(1,10000)}
5656
tooltip='Set page size'
@@ -66,6 +66,7 @@ export class TablePanelOptions extends React.Component {
6666
<span>
6767
<div style={{ position: 'relative',
6868
display: 'block',
69+
height: 16,
6970
right: -42,
7071
top: -4}}
7172
className='btn-close'

src/firefly/js/tables/ui/TableRenderer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ const html_regex = /<.+>/;
2424
/* eslint-disable react/prop-types */
2525

2626
/*---------------------------- COLUMN HEADER RENDERERS ----------------------------*/
27-
function Label({sortable, title, name, sortByCols, sortInfoCls, onSort}) {
27+
function Label({sortable, label, name, sortByCols, sortInfoCls, onSort}) {
2828
const sortDir = sortInfoCls.getDirection(name);
2929
sortByCols = sortByCols || name;
3030

3131
if (toBoolean(sortable, true)) {
3232
return (
33-
<div style={{width: '100%', cursor: 'pointer'}} onClick={() => onSort(sortByCols)}>{title || name}
33+
<div style={{width: '100%', cursor: 'pointer'}} onClick={() => onSort(sortByCols)}>{label || name}
3434
{ sortDir !== UNSORTED && <SortSymbol sortDir={sortDir}/> }
3535
</div>
3636
);
3737
} else {
38-
return <div>{title || name}</div>;
38+
return <div>{label || name}</div>;
3939
}
4040
}
4141

0 commit comments

Comments
 (0)