Search in sources :

Example 1 with GridTable

use of org.compiere.model.GridTable in project adempiere by adempiere.

the class GridTabDataBinder method valueChange.

/**
	 * @param e
	 */
public void valueChange(ValueChangeEvent e) {
    if (//  only active records
    gridTab.isProcessed()) {
        Object source = e.getSource();
        if (source instanceof WEditor) {
            // Elaine 2009/05/06
            WEditor editor = (WEditor) source;
            GridField gridField = editor.getGridField();
            if (gridField != null) {
                if (!gridField.isEditable(true)) {
                    logger.config("(" + gridTab.toString() + ") " + e.getPropertyName());
                    return;
                }
            } else if (!editor.isReadWrite()) {
                logger.config("(" + gridTab.toString() + ") " + e.getPropertyName());
                return;
            }
        } else {
            logger.config("(" + gridTab.toString() + ") " + e.getPropertyName());
            return;
        }
    }
    //  processed
    logger.config("(" + gridTab.toString() + ") " + e.getPropertyName() + "=" + e.getNewValue() + " (" + e.getOldValue() + ") " + (e.getOldValue() == null ? "" : e.getOldValue().getClass().getName()));
    //  Get Row/Col Info
    GridTable mTable = gridTab.getTableModel();
    int row = gridTab.getCurrentRow();
    int col = mTable.findColumn(e.getPropertyName());
    //
    if (e.getNewValue() == null && e.getOldValue() != null && //  some editors return "" instead of null
    e.getOldValue().toString().length() > 0)
        //        	  this is the original code from GridController, don't know what it does there but it breaks ignore button for web ui        
        //            mTable.setChanged (true);  
        mTable.setValueAt(e.getNewValue(), row, col);
    else {
        Object newValue = e.getNewValue();
        Integer[] newValues = null;
        if (newValue instanceof Integer[]) {
            newValues = ((Integer[]) newValue);
            newValue = newValues[0];
            if (newValues.length > 1) {
                Integer[] valuesCopy = new Integer[newValues.length - 1];
                System.arraycopy(newValues, 1, valuesCopy, 0, valuesCopy.length);
                newValues = valuesCopy;
            } else {
                newValues = null;
            }
        } else if (newValue instanceof Object[]) {
            logger.severe("Multiple values can only be processed for IDs (Integer)");
            throw new IllegalArgumentException("Multiple Selection values not available for this field. " + e.getPropertyName());
        }
        mTable.setValueAt(newValue, row, col);
        //  Force Callout
        if (e.getPropertyName().equals("S_ResourceAssignment_ID")) {
            GridField mField = gridTab.getField(col);
            if (mField != null && mField.getCallout().length() > 0) {
                //  Dependencies & Callout
                gridTab.processFieldChange(mField);
            }
        }
        if (newValues != null && newValues.length > 0) {
            // Save data, since record need to be used for generating clones.
            if (!gridTab.dataSave(false)) {
                throw new AdempiereException("SaveError");
            }
            // Retrieve the current record ID
            int recordId = gridTab.getKeyID(gridTab.getCurrentRow());
            Trx trx = Trx.get(Trx.createTrxName(), true);
            trx.start();
            try {
                saveMultipleRecords(Env.getCtx(), gridTab.getTableName(), e.getPropertyName(), recordId, newValues, trx.getTrxName());
                trx.commit();
                gridTab.dataRefreshAll();
            } catch (Exception ex) {
                trx.rollback();
                logger.severe(ex.getMessage());
                throw new AdempiereException("SaveError");
            } finally {
                trx.close();
            }
        }
    }
}
Also used : AdempiereException(org.adempiere.exceptions.AdempiereException) GridTable(org.compiere.model.GridTable) WEditor(org.adempiere.webui.editor.WEditor) GridField(org.compiere.model.GridField) Trx(org.compiere.util.Trx) AdempiereException(org.adempiere.exceptions.AdempiereException)

Example 2 with GridTable

use of org.compiere.model.GridTable in project adempiere by adempiere.

the class GridController method vetoableChange.

/**************************************************************************
	 *  Vetoable Change Listener.
	 * 	Called from VEditor
	 *  <pre>
	 *  - for Save Confirmation dialog
	 *  - for Single Row from VEditor: Update MTable
	 *  </pre>
	 *  @param e event
	 *  @throws PropertyVetoException
	 */
public void vetoableChange(PropertyChangeEvent e) throws PropertyVetoException {
    if (//	only active records
    m_mTab.isProcessed() || !m_mTab.isActive()) {
        Object source = e.getSource();
        if (source instanceof VEditor) {
            if (!((VEditor) source).isReadWrite()) {
                log.config("(" + m_mTab.toString() + ") " + e.getPropertyName());
                return;
            }
        } else {
            log.config("(" + m_mTab.toString() + ") " + e.getPropertyName());
            return;
        }
    }
    //	processed
    log.config("(" + m_mTab.toString() + ") " + e.getPropertyName() + "=" + e.getNewValue() + " (" + e.getOldValue() + ") " + (e.getOldValue() == null ? "" : e.getOldValue().getClass().getName()));
    //  Save Confirmation dialog    MTable-RowSave
    if (e.getPropertyName().equals(GridTable.PROPERTY)) {
        //  throw new PropertyVetoException will call this listener again to revert to old value
        if (m_vetoActive) {
            //ignore
            m_vetoActive = false;
            return;
        }
        if (!Env.isAutoCommit(Env.getCtx(), m_WindowNo) || m_mTab.getCommitWarning().length() > 0) {
            if (!ADialog.ask(m_WindowNo, this, "SaveChanges?", m_mTab.getCommitWarning())) {
                m_vetoActive = true;
                throw new PropertyVetoException("UserDeniedSave", e);
            }
        }
        return;
    }
    //  saveConfirmation
    //  Get Row/Col Info
    GridTable mTable = m_mTab.getTableModel();
    int row = m_mTab.getCurrentRow();
    int col = mTable.findColumn(e.getPropertyName());
    //
    if ((e.getNewValue() == null || e.getNewValue().toString().isEmpty()) && e.getOldValue() != null && //	some editors return "" instead of null
    e.getOldValue().toString().length() > 0) {
        //  #283 Set value to null
        GridField gridField = m_mTab.getField(col);
        if (!gridField.getVO().IsMandatory)
            //	-> dataStatusChanged -> dynamicDisplay
            mTable.setValueAt(null, row, col);
        mTable.setChanged(true);
    } else {
        //	mTable.setValueAt (e.getNewValue(), row, col, true);
        /*
         	 * Changes: Added the logic below to handle multiple values for a single field
         	 *          due to multiple selection in Lookup (Info).
         	 * @author ashley
         	 */
        Object newValue = e.getNewValue();
        Integer[] newValues = null;
        if (newValue instanceof Integer[]) {
            newValues = ((Integer[]) newValue);
            newValue = newValues[0];
            if (newValues.length > 1) {
                Integer[] valuesCopy = new Integer[newValues.length - 1];
                System.arraycopy(newValues, 1, valuesCopy, 0, valuesCopy.length);
                newValues = valuesCopy;
            } else {
                newValues = null;
            }
        } else if (newValue instanceof Object[]) {
            log.severe("Multiple values can only be processed for IDs (Integer)");
            throw new PropertyVetoException("Multiple Selection values not available for this field", e);
        }
        //	-> dataStatusChanged -> dynamicDisplay
        mTable.setValueAt(newValue, row, col);
        //	Force Callout
        if (e.getPropertyName().equals("S_ResourceAssignment_ID")) {
            GridField mField = m_mTab.getField(col);
            if (mField != null && mField.getCallout().length() > 0)
                //  Dependencies & Callout
                m_mTab.processFieldChange(mField);
        }
        if (newValues != null && newValues.length > 0) {
            // Save data, since record need to be used for generating clones.
            if (!m_mTab.dataSave(false)) {
                throw new PropertyVetoException("SaveError", e);
            }
            // Retrieve the current record ID
            int recordId = m_mTab.getKeyID(m_mTab.getCurrentRow());
            Trx trx = Trx.get(Trx.createTrxName(), true);
            trx.start();
            try {
                saveMultipleRecords(Env.getCtx(), mTable.getTableName(), e.getPropertyName(), recordId, newValues, trx.getTrxName());
                trx.commit();
                m_mTab.dataRefreshAll();
            } catch (Exception ex) {
                trx.rollback();
                log.severe(ex.getMessage());
                throw new PropertyVetoException("SaveError", e);
            } finally {
                trx.close();
            }
        }
    }
//	log.config( "GridController.vetoableChange (" + m_mTab.toString() + ") - fini", e.getPropertyName() + "=" + e.getNewValue());
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) GridTable(org.compiere.model.GridTable) VEditor(org.compiere.grid.ed.VEditor) GridField(org.compiere.model.GridField) Trx(org.compiere.util.Trx) PropertyVetoException(java.beans.PropertyVetoException)

Example 3 with GridTable

use of org.compiere.model.GridTable in project adempiere by adempiere.

the class APanel method doZoomToDetail.

private boolean doZoomToDetail(GridTab gTab, MQuery query, int tabIndex) {
    GridField[] fields = gTab.getFields();
    for (GridField field : fields) {
        if (field.getColumnName().equalsIgnoreCase(query.getZoomColumnName())) {
            m_mWorkbench.getMWindow(0).initTab(tabIndex);
            int parentId = DB.getSQLValue(null, "SELECT " + gTab.getLinkColumnName() + " FROM " + gTab.getTableName() + " WHERE " + query.getWhereClause());
            if (parentId > 0) {
                Map<Integer, Object[]> parentMap = new TreeMap<Integer, Object[]>();
                int index = tabIndex;
                int oldpid = parentId;
                GridTab currentTab = gTab;
                while (index > 0) {
                    index--;
                    GridTab pTab = m_mWorkbench.getMWindow(0).getTab(index);
                    if (pTab.getTabLevel() < currentTab.getTabLevel()) {
                        m_mWorkbench.getMWindow(0).initTab(index);
                        if (index > 0) {
                            if (pTab.getLinkColumnName() != null && pTab.getLinkColumnName().trim().length() > 0) {
                                int pid = DB.getSQLValue(null, "SELECT " + pTab.getLinkColumnName() + " FROM " + pTab.getTableName() + " WHERE " + currentTab.getLinkColumnName() + " = ?", oldpid);
                                if (pid > 0) {
                                    parentMap.put(index, new Object[] { currentTab.getLinkColumnName(), oldpid });
                                    oldpid = pid;
                                    currentTab = pTab;
                                } else {
                                    parentMap.clear();
                                    break;
                                }
                            }
                        } else {
                            parentMap.put(index, new Object[] { currentTab.getLinkColumnName(), oldpid });
                        }
                    }
                }
                for (Map.Entry<Integer, Object[]> entry : parentMap.entrySet()) {
                    GridTab pTab = m_mWorkbench.getMWindow(0).getTab(entry.getKey());
                    Object[] value = entry.getValue();
                    MQuery pquery = new MQuery(pTab.getAD_Table_ID());
                    pquery.addRestriction((String) value[0], "=", value[1]);
                    pTab.setQuery(pquery);
                    GridController gc = (GridController) tabPanel.getComponentAt(entry.getKey());
                    gc.activate();
                    gc.query(false, 0, 0);
                }
                MQuery targetQuery = new MQuery(gTab.getAD_Table_ID());
                targetQuery.addRestriction(gTab.getLinkColumnName(), "=", parentId);
                gTab.setQuery(targetQuery);
                GridController gc = null;
                if (!includedMap.containsKey(gTab.getAD_Tab_ID())) {
                    int target = tabPanel.findTabindex(gTab);
                    gc = (GridController) tabPanel.getComponentAt(target);
                } else {
                    GridController parent = includedMap.get(gTab.getAD_Tab_ID());
                    gc = parent.findChild(gTab);
                }
                gc.activate();
                gc.query(false, 0, 0);
                GridTable table = gTab.getTableModel();
                int count = table.getRowCount();
                for (int i = 0; i < count; i++) {
                    int id = table.getKeyID(i);
                    if (id == ((Integer) query.getZoomValue()).intValue()) {
                        if (!includedMap.containsKey(gTab.getAD_Tab_ID())) {
                            tabPanel.setSelectedIndex(tabPanel.findTabindex(gTab));
                        } else {
                            GridController parent = includedMap.get(gTab.getAD_Tab_ID());
                            int pindex = tabPanel.findTabindex(parent.getMTab());
                            if (pindex >= 0)
                                tabPanel.setSelectedIndex(pindex);
                        }
                        gTab.navigate(i);
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : GridTable(org.compiere.model.GridTable) MQuery(org.compiere.model.MQuery) GridField(org.compiere.model.GridField) TreeMap(java.util.TreeMap) Point(java.awt.Point) GridTab(org.compiere.model.GridTab) GridController(org.compiere.grid.GridController) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Example 4 with GridTable

use of org.compiere.model.GridTable in project adempiere by adempiere.

the class VTable method sort.

//  getColorCode
/**
	 *  Sort Table
	 *  @param modelColumnIndex model column sort index
	 */
protected void sort(int modelColumnIndex) {
    int rows = getRowCount();
    if (rows == 0)
        return;
    //
    TableModel model = getModel();
    if (!(model instanceof GridTable)) {
        super.sort(modelColumnIndex);
        return;
    }
    sorting = true;
    //  other sort column
    if (modelColumnIndex != p_lastSortIndex)
        p_asc = true;
    else
        p_asc = !p_asc;
    p_lastSortIndex = modelColumnIndex;
    //
    log.config("#" + modelColumnIndex + " - rows=" + rows + ", asc=" + p_asc);
    ((GridTable) model).sort(modelColumnIndex, p_asc);
    sorting = false;
//  table model fires "Sorted" DataStatus event which causes MTab to position to row 0
}
Also used : GridTable(org.compiere.model.GridTable) TableModel(javax.swing.table.TableModel)

Example 5 with GridTable

use of org.compiere.model.GridTable in project adempiere by adempiere.

the class VTableExcelAdapter method actionPerformed.

/**
	 * This method is activated on the Keystrokes we are listening to
	 * in this implementation. Here it listens for Copy and Paste ActionCommands.
	 * 
	 * @param e event 
	 */
public void actionPerformed(ActionEvent e) {
    // Only GridTable model is supported
    if (!(table.getModel() instanceof GridTable)) {
        if (CLogMgt.isLevelFine())
            log.fine("Not supported - " + table.getModel());
        return;
    }
    boolean isCopy = CMD_Copy.equals(e.getActionCommand());
    boolean isCopyWithHeaders = CMD_CopyWithHeaders.equals(e.getActionCommand());
    if (isCopy || isCopyWithHeaders) {
        try {
            int[] selectedRows = table.getSelectedRows();
            if (selectedRows == null || selectedRows.length == 0) {
                return;
            }
            int colscount = table.getColumnCount();
            StringBuffer sb = new StringBuffer();
            GridTable model = (GridTable) table.getModel();
            GridField[] fields = model.getFields();
            // Header
            if (isCopyWithHeaders) {
                for (int col = 0; col < colscount; col++) {
                    String value = "";
                    try {
                        GridField field = fields[col];
                        if (!field.isDisplayed(false)) {
                            continue;
                        }
                        value = field.getHeader();
                    } catch (Exception ex) {
                        log.log(Level.WARNING, "Copy-headers", ex);
                    }
                    value = fixString(value);
                    sb.append(value).append("\t");
                }
                sb.append(Env.NL);
            }
            // Selected rows
            for (int row : selectedRows) {
                for (int col = 0; col < colscount; col++) {
                    Lookup lookup = null;
                    String value = null;
                    Object key = null;
                    GridField field = null;
                    try {
                        key = table.getValueAt(row, col);
                        field = fields[col];
                        if (!field.isDisplayed(false))
                            continue;
                        if (field.isEncryptedColumn() || field.isEncryptedField()) {
                            value = "*";
                        } else if (key instanceof Boolean) {
                            value = Msg.getMsg(Env.getCtx(), ((Boolean) key).booleanValue() ? "Yes" : "No");
                        } else if (key instanceof BigDecimal) {
                            try {
                                value = sysNumberFormat.format(key != null ? key : Env.ZERO);
                            } catch (Exception ex) {
                            }
                        } else if (key instanceof Date) {
                            try {
                                value = sysDateFormat.format(key);
                            } catch (Exception ex) {
                            }
                        } else {
                            lookup = (field != null ? field.getLookup() : null);
                            value = (lookup != null && key != null ? lookup.getDisplay(key) : null);
                            if (value == null && key != null)
                                value = key.toString();
                        }
                    } catch (Exception ex) {
                        log.log(Level.WARNING, "Copy-rows", ex);
                    }
                    value = fixString(value);
                    sb.append(value).append("\t");
                    if (CLogMgt.isLevelFinest())
                        log.finest("col=" + col + ", row=" + row + ": key=" + key + " => value=" + value + ", " + field + ", " + lookup);
                }
                sb.append(Env.NL);
            }
            StringSelection stsel = new StringSelection(sb.toString());
            system = Toolkit.getDefaultToolkit().getSystemClipboard();
            system.setContents(stsel, stsel);
        } catch (Exception ex) {
            log.log(Level.WARNING, "Copy", ex);
        }
    }
}
Also used : GridTable(org.compiere.model.GridTable) GridField(org.compiere.model.GridField) BigDecimal(java.math.BigDecimal) Date(java.util.Date) StringSelection(java.awt.datatransfer.StringSelection) Lookup(org.compiere.model.Lookup)

Aggregations

GridTable (org.compiere.model.GridTable)6 GridField (org.compiere.model.GridField)5 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 GridTab (org.compiere.model.GridTab)2 MQuery (org.compiere.model.MQuery)2 Trx (org.compiere.util.Trx)2 Point (java.awt.Point)1 StringSelection (java.awt.datatransfer.StringSelection)1 PropertyVetoException (java.beans.PropertyVetoException)1 BigDecimal (java.math.BigDecimal)1 Date (java.util.Date)1 TableModel (javax.swing.table.TableModel)1 AdempiereException (org.adempiere.exceptions.AdempiereException)1 WEditor (org.adempiere.webui.editor.WEditor)1 GridController (org.compiere.grid.GridController)1 VEditor (org.compiere.grid.ed.VEditor)1 Lookup (org.compiere.model.Lookup)1