Example usage for java.util Collections swap

List of usage examples for java.util Collections swap

Introduction

In this page you can find the example usage for java.util Collections swap.

Prototype

private static void swap(Object[] arr, int i, int j) 

Source Link

Document

Swaps the two specified elements in the specified array.

Usage

From source file:hydrograph.ui.propertywindow.widgets.customwidgets.operational.OperationClassDialog.java

private void moveRowDown(TableViewer nameValueTableViewer) {
    Table table = nameValueTableViewer.getTable();
    int[] indexes = table.getSelectionIndices();
    for (int i = indexes.length - 1; i > -1; i--) {

        if (indexes[i] < mappingSheetRow.getNameValueProperty().size() - 1) {
            Collections.swap(mappingSheetRow.getNameValueProperty(), indexes[i], indexes[i] + 1);
            nameValueTableViewer.refresh();
            applyButton.setEnabled(true);

        }/*from w  w  w .ja v  a 2s  . c om*/
    }

}

From source file:com.ubershy.streamsis.project.CuteProject.java

/**
 * Moves up the SisScene in CuteProject's list of SisScenes. <br>
 * Can be useful only for GUI./*from   w  ww.j  a  va2 s .c  o  m*/
 *
 * @param sisScene
 *            the SisScene to move
 */
public void moveUpSisScene(SisScene sisScene) {
    int index = sisScenes.indexOf(sisScene);
    if (index != 0)
        Collections.swap(sisScenes, index, index - 1);
}

From source file:org.silverpeas.attachment.web.SimpleDocumentResource.java

/**
 * Moves the specified document down in the list.
 *
 * @return JSON status to true if the document was locked successfully - JSON status to false
 * otherwise../*  www.  j a v  a2s.  c o  m*/
 */
@PUT
@Path("moveDown")
@Produces(MediaType.APPLICATION_JSON)
public String moveSimpleDocumentDown() {
    SimpleDocument document = getSimpleDocument(defaultLanguage);
    List<SimpleDocument> docs = getListDocuments(document);
    int position = docs.indexOf(document);
    Collections.swap(docs, position, position + 1);
    String message = reorderDocuments(docs);
    return message;
}

From source file:org.silverpeas.core.webapi.attachment.SimpleDocumentResource.java

private String moveSimpleDocument(boolean up) {
    SimpleDocument document = getSimpleDocument(defaultLanguage);
    List<SimpleDocument> docs = getListDocuments(document);
    int position = docs.indexOf(document);
    Collections.swap(docs, position, up ? (position - 1) : (position + 1));
    return reorderDocuments(docs);
}

From source file:com.nextep.datadesigner.dbgm.gui.ColumnEditorGUI.java

/**
 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
 *//*from www  .j  a va 2 s . com*/
public void widgetSelected(SelectionEvent e) {
    IBasicTable t = (IBasicTable) getModel();
    // Ensuring we are allowed to modify our model
    t = VCSUIPlugin.getVersioningUIService().ensureModifiable(t);
    if (e.getSource() == group.getAddNewButton()) {
        getController().newInstance(getModel());
    } else if (e.getSource() == group.getUpButton()) {
        TableItem[] sel = group.getColumnsTable().getSelection();
        if (sel.length > 0) {
            IBasicColumn c = (IBasicColumn) sel[0].getData();
            int pos = c.getParent().getColumns().indexOf(c);
            if (pos > 0) {
                final IBasicColumn otherCol = c.getParent().getColumns().get(pos - 1);
                Collections.swap(c.getParent().getColumns(), pos, pos - 1);
                // Disposing to force recreation
                removeTableItem(c);
                // clean(group.getColumnsTable());
                refreshConnector();
                TableItem i = getTableItem(c);
                group.getColumnsTable().setFocus();
                group.getColumnsTable().setSelection(i);
                group.getUpButton().setFocus();
                c.setRank(pos - 1);
                otherCol.setRank(pos);
                CorePlugin.getIdentifiableDao().save(c.getParent());
            }
        }
    } else if (e.getSource() == group.getDownButton()) {
        TableItem[] sel = group.getColumnsTable().getSelection();
        if (sel.length > 0) {
            IBasicColumn c = (IBasicColumn) sel[0].getData();
            int pos = c.getParent().getColumns().indexOf(c);
            if (pos < c.getParent().getColumns().size() - 1) {
                final IBasicColumn otherCol = c.getParent().getColumns().get(pos + 1);
                Collections.swap(c.getParent().getColumns(), pos, pos + 1);
                // Disposing to force recreation
                removeTableItem(c);
                // clean(group.getColumnsTable());
                refreshConnector();
                TableItem i = getTableItem(c);
                group.getColumnsTable().setSelection(i);
                group.getDownButton().setFocus();
                c.setRank(pos + 1);
                otherCol.setRank(pos);
                CorePlugin.getIdentifiableDao().save(c.getParent());
            }
        }
    }

}

From source file:org.jahia.test.services.render.filter.cache.base.CacheFilterHttpTest.java

static void permute(List<String> arr, int k, List<List<String>> res) {
    for (int i = k; i < arr.size(); i++) {
        Collections.swap(arr, i, k);
        permute(arr, k + 1, res);/* ww  w.jav  a 2 s.  co  m*/
        Collections.swap(arr, k, i);
    }
    if (k == arr.size() - 1) {
        res.add(new ArrayList<String>(arr));
    }
}

From source file:io.plaidapp.ui.FeedAdapter.java

private void expandPopularItems() {
    // for now just expand the first dribbble image per page which should be
    // the most popular according to #sort.
    // TODO make this smarter & handle other item types
    List<Integer> expandedPositions = new ArrayList<>();
    int page = -1;
    final int count = items.size();
    for (int i = 0; i < count; i++) {
        PlaidItem item = getItem(i);/* w w  w  .j  ava 2s . c om*/
        if (item instanceof Shot && item.page > page) {
            item.colspan = columns;
            page = item.page;
            expandedPositions.add(i);
        } else {
            item.colspan = 1;
        }
    }

    // make sure that any expanded items are at the start of a row
    // so that we don't leave any gaps in the grid
    for (int expandedPos = 0; expandedPos < expandedPositions.size(); expandedPos++) {
        int pos = expandedPositions.get(expandedPos);
        int extraSpannedSpaces = expandedPos * (columns - 1);
        int rowPosition = (pos + extraSpannedSpaces) % columns;
        if (rowPosition != 0) {
            int swapWith = pos + (columns - rowPosition);
            if (swapWith < items.size()) {
                Collections.swap(items, pos, swapWith);
            }
        }
    }
}

From source file:com.hannesdorfmann.FeedAdapter.java

private void expandPopularItems() {
    // for now just expand the first dribbble image per page which should be
    // the most popular according to #sort.
    // TODO make this smarter & handle other item types
    List<Integer> expandedPositions = new ArrayList<>();
    int page = -1;
    final int count = items.size();
    for (int i = 0; i < count; i++) {
        PlaidItem item = getItem(i);/*from  w ww.j  a  v a 2 s.  c o  m*/
        if (item instanceof Shot && item.page > page) {
            item.colspan = columns;
            page = item.page;
            expandedPositions.add(i);
        } else {
            item.colspan = 1;
        }
    }

    // make sure that any expanded items are at the start of a row
    // so that we don't leave any gaps in the grid
    for (int expandedPos = 0; expandedPos < expandedPositions.size(); expandedPos++) {
        int pos = expandedPositions.get(expandedPos);
        int extraSpannedSpaces = expandedPos * (columns - 1);
        int rowPosition = (pos + extraSpannedSpaces) % columns;
        if (rowPosition != 0) {
            int swapWith = pos + (columns - rowPosition);
            Collections.swap(items, pos, swapWith);
        }
    }
}

From source file:com.github.chenxiaolong.dualbootpatcher.patcher.PatchFileFragment.java

/**
 * {@inheritDoc}// w  w  w . j a v a2 s.c  o m
 */
@Override
public void onItemMoved(int fromPosition, int toPosition) {
    // Update index map
    PatchFileItem fromItem = mItems.get(fromPosition);
    PatchFileItem toItem = mItems.get(toPosition);
    mItemsMap.put(fromItem.taskId, toPosition);
    mItemsMap.put(toItem.taskId, fromPosition);

    Collections.swap(mItems, fromPosition, toPosition);
    mAdapter.notifyItemMoved(fromPosition, toPosition);
}

From source file:hydrograph.ui.propertywindow.widgets.dialogs.ELTOperationClassDialog.java

private void moveRowUp(TableViewer nameValueTableViewer) {

    Table table = nameValueTableViewer.getTable();
    int[] indexes = table.getSelectionIndices();
    for (int index : indexes) {

        if (index > 0) {
            Collections.swap(operationClassProperty.getNameValuePropertyList(), index, index - 1);
            nameValueTableViewer.refresh();
            applyButton.setEnabled(true);
        }/*from   w w  w.ja  va  2  s. c  o m*/
    }

}