Search in sources :

Example 1 with AttributeDataType

use of org.cytoscape.tableimport.internal.util.AttributeDataType in project cytoscape-impl by cytoscape.

the class PreviewTablePanel method showEditDialog.

private void showEditDialog(final int colIdx) {
    if (colIdx == lastDialogIndex && System.currentTimeMillis() - lastDialogTime < 100)
        return;
    lastDialogIndex = -1;
    lastDialogTime = 0;
    final Window parent = SwingUtilities.getWindowAncestor(PreviewTablePanel.this);
    final PreviewTableModel model = (PreviewTableModel) getPreviewTable().getModel();
    final String attrName = model.getColumnName(colIdx);
    final List<SourceColumnSemantic> availableTypes = TypeUtil.getAvailableTypes(importType);
    final AttributeEditorPanel attrEditorPanel = new AttributeEditorPanel(parent, attrName, availableTypes, types[colIdx], dataTypes[colIdx], listDelimiters[colIdx], iconManager);
    if (LookAndFeelUtil.isWinLAF()) {
        attrEditorPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, UIManager.getColor("activeCaptionBorder")));
        attrEditorPanel.setBackground(UIManager.getColor("TableHeader.background"));
    }
    editDialog = new EditDialog(parent, ModalityType.MODELESS, colIdx, attrEditorPanel);
    editDialog.setUndecorated(true);
    editDialog.add(attrEditorPanel);
    final ActionMap actionMap = attrEditorPanel.getActionMap();
    final InputMap inputMap = attrEditorPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "VK_ESCAPE");
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "VK_ENTER");
    actionMap.put("VK_ESCAPE", new AbstractAction("VK_ESCAPE") {

        @Override
        public void actionPerformed(ActionEvent e) {
            disposeEditDialog();
        }
    });
    actionMap.put("VK_ENTER", new AbstractAction("VK_ENTER") {

        @Override
        public void actionPerformed(ActionEvent e) {
            disposeEditDialog();
        }
    });
    attrEditorPanel.addPropertyChangeListener("attributeName", new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            final String attrName = (String) evt.getNewValue();
            if (attrName != null && !attrName.trim().isEmpty()) {
                ((PreviewTableModel) getPreviewTable().getModel()).setColumnName(colIdx, attrName);
                getPreviewTable().getColumnModel().getColumn(colIdx).setHeaderValue(attrName);
                updatePreviewTable();
            }
        }
    });
    attrEditorPanel.addPropertyChangeListener("attributeType", new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            setType(colIdx, (SourceColumnSemantic) evt.getNewValue());
            updatePreviewTable();
        }
    });
    attrEditorPanel.addPropertyChangeListener("attributeDataType", new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            final AttributeDataType newDataType = (AttributeDataType) evt.getNewValue();
            if (newDataType.isList())
                setListDelimiter(colIdx, attrEditorPanel.getListDelimiter());
            setDataType(colIdx, newDataType);
            updatePreviewTable();
        }
    });
    attrEditorPanel.addPropertyChangeListener("listDelimiter", new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            setListDelimiter(colIdx, (String) evt.getNewValue());
            updatePreviewTable();
        }
    });
    positionEditDialog();
    editDialog.addWindowListener(new WindowAdapter() {

        @Override
        public void windowOpened(WindowEvent e) {
            getPreviewTable().getTableHeader().repaint();
            attrEditorPanel.getAttributeNameTextField().requestFocusInWindow();
        }

        @Override
        public void windowClosed(WindowEvent e) {
            getPreviewTable().getTableHeader().repaint();
        }
    });
    editDialog.addWindowFocusListener(new WindowFocusListener() {

        @Override
        public void windowLostFocus(WindowEvent e) {
            if (editDialog != null) {
                lastDialogIndex = editDialog.index;
                lastDialogTime = System.currentTimeMillis();
            }
            disposeEditDialog();
        }

        @Override
        public void windowGainedFocus(WindowEvent e) {
        }
    });
    editDialog.pack();
    editDialog.setVisible(true);
}
Also used : Window(java.awt.Window) PropertyChangeEvent(java.beans.PropertyChangeEvent) PropertyChangeListener(java.beans.PropertyChangeListener) ActionMap(javax.swing.ActionMap) ActionEvent(java.awt.event.ActionEvent) WindowAdapter(java.awt.event.WindowAdapter) SourceColumnSemantic(org.cytoscape.tableimport.internal.util.SourceColumnSemantic) AttributeDataType(org.cytoscape.tableimport.internal.util.AttributeDataType) WindowEvent(java.awt.event.WindowEvent) InputMap(javax.swing.InputMap) AbstractAction(javax.swing.AbstractAction) WindowFocusListener(java.awt.event.WindowFocusListener)

Example 2 with AttributeDataType

use of org.cytoscape.tableimport.internal.util.AttributeDataType in project cytoscape-impl by cytoscape.

the class AttributeLineParser method mapAttribute.

/**
 * Based on the attribute types, map the entry to CyAttributes.<br>
 */
private void mapAttribute(final CyTable table, final Object key, final String entry, final int index) {
    final AttributeDataType type = mapping.getDataTypes()[index];
    try {
        if (type.isList()) {
            final String[] delimiters = mapping.getListDelimiters();
            String delimiter = delimiters != null && delimiters.length > index ? delimiters[index] : AbstractMappingParameters.DEF_LIST_DELIMITER;
            if (delimiter == null || delimiter.isEmpty())
                delimiter = AbstractMappingParameters.DEF_LIST_DELIMITER;
            Object value = parse(entry, type, delimiter);
            setListAttribute(table, type, key, mapping.getAttributeNames()[index], value);
        } else {
            setAttribute(table, type, key, mapping.getAttributeNames()[index], entry);
        }
    } catch (Exception e) {
        invalid.put(key.toString(), entry);
    }
}
Also used : AttributeDataType(org.cytoscape.tableimport.internal.util.AttributeDataType)

Example 3 with AttributeDataType

use of org.cytoscape.tableimport.internal.util.AttributeDataType in project cytoscape-impl by cytoscape.

the class AttributeLineParser method parseAll.

/**
 * Import everything regardless associated nodes/edges exist or not.
 * @param parts entries in a line.
 */
public void parseAll(final CyTable table, final String[] parts) {
    // Get key
    final Object primaryKey;
    final int partsLen = parts.length;
    final AttributeDataType typeKey = mapping.getDataTypes()[mapping.getKeyIndex()];
    switch(typeKey) {
        case TYPE_BOOLEAN:
            primaryKey = Boolean.valueOf(parts[mapping.getKeyIndex()].trim());
            break;
        case TYPE_INTEGER:
            primaryKey = Integer.valueOf(parts[mapping.getKeyIndex()].trim());
            break;
        case TYPE_LONG:
            primaryKey = Long.valueOf(parts[mapping.getKeyIndex()].trim());
            break;
        case TYPE_FLOATING:
            primaryKey = Double.valueOf(parts[mapping.getKeyIndex()].trim());
            break;
        default:
            primaryKey = parts[mapping.getKeyIndex()].trim();
    }
    if (partsLen == 1) {
        table.getRow(parts[0]);
    } else {
        final SourceColumnSemantic[] types = mapping.getTypes();
        for (int i = 0; i < partsLen; i++) {
            if (i != mapping.getKeyIndex() && types[i] != SourceColumnSemantic.NONE) {
                if (parts[i] == null)
                    continue;
                else
                    mapAttribute(table, primaryKey, parts[i].trim(), i);
            }
        }
    }
}
Also used : SourceColumnSemantic(org.cytoscape.tableimport.internal.util.SourceColumnSemantic) AttributeDataType(org.cytoscape.tableimport.internal.util.AttributeDataType)

Example 4 with AttributeDataType

use of org.cytoscape.tableimport.internal.util.AttributeDataType in project cytoscape-impl by cytoscape.

the class NetworkLineParser method mapAttribute.

/**
 * Based on the attribute types, map the entry to Node or Edge tables.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
private <T extends CyIdentifiable> void mapAttribute(final T element, final String entry, final int index) {
    if (entry == null || entry.length() == 0)
        return;
    final AttributeDataType type = mapping.getDataTypes()[index];
    if (type.isList()) {
        final CyTable table = network.getRow(element).getTable();
        if (table.getColumn(mapping.getAttributeNames()[index]) == null)
            table.createListColumn(mapping.getAttributeNames()[index], type.getListType(), false);
        final String[] delimiters = mapping.getListDelimiters();
        String delimiter = delimiters != null && delimiters.length > index ? delimiters[index] : AbstractMappingParameters.DEF_LIST_DELIMITER;
        if (delimiter == null || delimiter.isEmpty())
            delimiter = AbstractMappingParameters.DEF_LIST_DELIMITER;
        Object value = parse(entry, type, delimiter);
        if (value instanceof List) {
            // In case of list, do not overwrite the attribute. Get the existing list, and add it to the list.
            List<Object> curList = network.getRow(element).get(mapping.getAttributeNames()[index], List.class);
            if (curList == null)
                curList = new ArrayList<>();
            curList.addAll((List) value);
            value = curList;
        }
        network.getRow(element).set(mapping.getAttributeNames()[index], value);
    } else {
        createColumn(element, mapping.getAttributeNames()[index], type.getType());
        final Object value = parse(entry, type, null);
        network.getRow(element).set(mapping.getAttributeNames()[index], value);
    }
}
Also used : CyTable(org.cytoscape.model.CyTable) AttributeDataType(org.cytoscape.tableimport.internal.util.AttributeDataType) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with AttributeDataType

use of org.cytoscape.tableimport.internal.util.AttributeDataType in project cytoscape-impl by cytoscape.

the class ImportTablePanel method getAttributeMappingParameters.

public AttributeMappingParameters getAttributeMappingParameters() throws Exception {
    final String sourceName = getPreviewPanel().getSourceName();
    final String[] attrNames = getPreviewPanel().getAttributeNames();
    final SourceColumnSemantic[] types = getPreviewPanel().getTypes();
    if (!isAttributeNamesValid(attrNames, types))
        return null;
    final SourceColumnSemantic[] typesCopy = Arrays.copyOf(types, types.length);
    final AttributeDataType[] dataTypes = getPreviewPanel().getDataTypes();
    final AttributeDataType[] dataTypesCopy = Arrays.copyOf(dataTypes, dataTypes.length);
    final String[] listDelimiters = getPreviewPanel().getListDelimiters();
    final String[] listDelimitersCopy = Arrays.copyOf(listDelimiters, listDelimiters.length);
    int startLineNumber = getStartLineNumber();
    String commentChar = null;
    if (!getCommentLinePrefix().isEmpty())
        commentChar = getCommentLinePrefix();
    // Build mapping parameter object.
    final List<String> del = checkDelimiter();
    final int keyInFile = getPreviewPanel().getColumnIndex(KEY);
    final AttributeMappingParameters mapping = new AttributeMappingParameters(sourceName, del, listDelimitersCopy, keyInFile, attrNames, dataTypesCopy, typesCopy, startLineNumber, commentChar);
    return mapping;
}
Also used : SourceColumnSemantic(org.cytoscape.tableimport.internal.util.SourceColumnSemantic) AttributeDataType(org.cytoscape.tableimport.internal.util.AttributeDataType) AttributeMappingParameters(org.cytoscape.tableimport.internal.reader.AttributeMappingParameters)

Aggregations

AttributeDataType (org.cytoscape.tableimport.internal.util.AttributeDataType)12 SourceColumnSemantic (org.cytoscape.tableimport.internal.util.SourceColumnSemantic)8 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 AttributeMappingParameters (org.cytoscape.tableimport.internal.reader.AttributeMappingParameters)3 JToggleButton (javax.swing.JToggleButton)2 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)2 Sheet (org.apache.poi.ss.usermodel.Sheet)2 Workbook (org.apache.poi.ss.usermodel.Workbook)2 CyTable (org.cytoscape.model.CyTable)2 NetworkTableMappingParameters (org.cytoscape.tableimport.internal.reader.NetworkTableMappingParameters)2 Window (java.awt.Window)1 ActionEvent (java.awt.event.ActionEvent)1 WindowAdapter (java.awt.event.WindowAdapter)1 WindowEvent (java.awt.event.WindowEvent)1 WindowFocusListener (java.awt.event.WindowFocusListener)1 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 FileInputStream (java.io.FileInputStream)1 List (java.util.List)1