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 });
}
}
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;
}
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();
}
Aggregations