Search in sources :

Example 1 with IJaretTableSelection

use of de.jaret.util.ui.table.model.IJaretTableSelection in project translationstudio8 by heartsome.

the class DefaultCCPStrategy method cutOrCopy.

/**
     * Do the actual copy or cut operation.
     * 
     * @param table table
     * @param cut if set to true cells we be emptied
     */
protected void cutOrCopy(JaretTable table, boolean cut) {
    IJaretTableSelection selection = table.getSelectionModel().getSelection();
    Clipboard cb = getClipboard();
    if (!selection.isEmpty()) {
        Set<IJaretTableCell> cells = selection.getAllSelectedCells(table.getTableModel());
        int minx = -1;
        int maxx = -1;
        int miny = -1;
        int maxy = -1;
        // line is the outer map
        Map<Integer, Map<Integer, IJaretTableCell>> cellMap = new HashMap<Integer, Map<Integer, IJaretTableCell>>();
        for (IJaretTableCell cell : cells) {
            Point p = table.getCellDisplayIdx(cell);
            Map<Integer, IJaretTableCell> lineMap = cellMap.get(p.y);
            if (lineMap == null) {
                lineMap = new HashMap<Integer, IJaretTableCell>();
                cellMap.put(p.y, lineMap);
            }
            if (miny == -1 || p.y < miny) {
                miny = p.y;
            }
            if (maxy == -1 || p.y > maxy) {
                maxy = p.y;
            }
            lineMap.put(p.x, cell);
            if (minx == -1 || p.x < minx) {
                minx = p.x;
            }
            if (maxx == -1 || p.x > maxx) {
                maxx = p.x;
            }
        }
        StringBuilder buf = new StringBuilder();
        if (_includeHeadersInCopy) {
            for (int x = minx; x <= maxx; x++) {
                String headerLabel = table.getColumn(x).getHeaderLabel();
                buf.append(headerLabel);
                buf.append(COPY_DELIMITER);
            }
            buf.append("\n");
        }
        for (int y = miny; y <= maxy; y++) {
            Map<Integer, IJaretTableCell> lineMap = cellMap.get(y);
            // empty lines are ommitted
            if (lineMap != null) {
                for (int x = minx; x <= maxx; x++) {
                    IJaretTableCell cell = lineMap.get(x);
                    String value = null;
                    if (cell != null) {
                        Object val = cell.getColumn().getValue(cell.getRow());
                        value = val != null ? val.toString() : null;
                        if (cut) {
                            emptyCell(cell);
                        }
                    }
                    if (value != null) {
                        buf.append(value);
                    }
                    buf.append(COPY_DELIMITER);
                }
                buf.append("\n");
            }
        }
        TextTransfer textTransfer = TextTransfer.getInstance();
        cb.setContents(new Object[] { buf.toString() }, new Transfer[] { textTransfer });
    }
}
Also used : HashMap(java.util.HashMap) IJaretTableSelection(de.jaret.util.ui.table.model.IJaretTableSelection) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) Clipboard(org.eclipse.swt.dnd.Clipboard) IJaretTableCell(de.jaret.util.ui.table.model.IJaretTableCell) HashMap(java.util.HashMap) Map(java.util.Map) TextTransfer(org.eclipse.swt.dnd.TextTransfer)

Example 2 with IJaretTableSelection

use of de.jaret.util.ui.table.model.IJaretTableSelection in project translationstudio8 by heartsome.

the class JaretTable method getSelectedRectangle.

/**
     * Retrieve the index rectangle of selected cells.
     * 
     * @return rectangel made from the indizes of the selected cells if a rectangular area is selected (all cells)
     */
private Rectangle getSelectedRectangle() {
    IJaretTableSelection selection = getSelectionModel().getSelection();
    if (!selection.isEmpty() && _selectedIdxRectangle == null) {
        Set<IJaretTableCell> cells = selection.getAllSelectedCells(getTableModel());
        int minx = -1;
        int maxx = -1;
        int miny = -1;
        int maxy = -1;
        // line is the outer map
        Map<Integer, Map<Integer, IJaretTableCell>> cellMap = new HashMap<Integer, Map<Integer, IJaretTableCell>>();
        for (IJaretTableCell cell : cells) {
            Point p = getCellDisplayIdx(cell);
            Map<Integer, IJaretTableCell> lineMap = cellMap.get(p.y);
            if (lineMap == null) {
                lineMap = new HashMap<Integer, IJaretTableCell>();
                cellMap.put(p.y, lineMap);
            }
            if (miny == -1 || p.y < miny) {
                miny = p.y;
            }
            if (maxy == -1 || p.y > maxy) {
                maxy = p.y;
            }
            lineMap.put(p.x, cell);
            if (minx == -1 || p.x < minx) {
                minx = p.x;
            }
            if (maxx == -1 || p.x > maxx) {
                maxx = p.x;
            }
        }
        // check if all cells are selected
        boolean everythingSelected = true;
        for (int y = miny; y <= maxy && everythingSelected; y++) {
            Map<Integer, IJaretTableCell> lineMap = cellMap.get(y);
            if (lineMap != null) {
                for (int x = minx; x <= maxx; x++) {
                    IJaretTableCell cell = lineMap.get(x);
                    if (cell == null) {
                        everythingSelected = false;
                        break;
                    }
                }
            } else {
                everythingSelected = false;
                break;
            }
        }
        if (everythingSelected) {
            _selectedIdxRectangle = new Rectangle(minx, miny, maxx - minx + 1, maxy - miny + 1);
        } else {
            _selectedIdxRectangle = null;
        }
    }
    return _selectedIdxRectangle;
}
Also used : HashMap(java.util.HashMap) IJaretTableSelection(de.jaret.util.ui.table.model.IJaretTableSelection) Rectangle(org.eclipse.swt.graphics.Rectangle) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) IJaretTableCell(de.jaret.util.ui.table.model.IJaretTableCell) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with IJaretTableSelection

use of de.jaret.util.ui.table.model.IJaretTableSelection in project translationstudio8 by heartsome.

the class DefaultSelectionProvider method getISelection.

/**
     * {@inheritDoc}. Returns a structured selection containig rows and columns and cells that have been selected.
     */
@SuppressWarnings("unchecked")
protected ISelection getISelection() {
    IJaretTableSelection selection = _table.getSelectionModel().getSelection();
    if (selection != null && !selection.isEmpty()) {
        List list = new ArrayList();
        for (IRow row : selection.getSelectedRows()) {
            list.add(row);
        }
        for (IColumn col : selection.getSelectedColumns()) {
            list.add(col);
        }
        for (IJaretTableCell cell : selection.getSelectedCells()) {
            list.add(cell);
        }
        StructuredSelection sselection = new StructuredSelection(list);
        return sselection;
    }
    return new StructuredSelection();
}
Also used : IRow(de.jaret.util.ui.table.model.IRow) IColumn(de.jaret.util.ui.table.model.IColumn) IJaretTableSelection(de.jaret.util.ui.table.model.IJaretTableSelection) ArrayList(java.util.ArrayList) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) List(java.util.List) ArrayList(java.util.ArrayList) IJaretTableCell(de.jaret.util.ui.table.model.IJaretTableCell)

Aggregations

IJaretTableCell (de.jaret.util.ui.table.model.IJaretTableCell)3 IJaretTableSelection (de.jaret.util.ui.table.model.IJaretTableSelection)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Point (org.eclipse.swt.graphics.Point)2 IColumn (de.jaret.util.ui.table.model.IColumn)1 IRow (de.jaret.util.ui.table.model.IRow)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)1 Clipboard (org.eclipse.swt.dnd.Clipboard)1 TextTransfer (org.eclipse.swt.dnd.TextTransfer)1 Rectangle (org.eclipse.swt.graphics.Rectangle)1