Ignore:
Timestamp:
2015-06-20T14:36:00+02:00 (11 years ago)
Author:
Don-vip
Message:

fix many checkstyle violations

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/preferences/ToolbarPreferences.java

    r8461 r8509  
    491491    public class Settings extends DefaultTabPreferenceSetting {
    492492
     493        private final class SelectedListTransferHandler extends TransferHandler {
     494            @Override
     495            @SuppressWarnings("unchecked")
     496            protected Transferable createTransferable(JComponent c) {
     497                List<ActionDefinition> actions = new ArrayList<>();
     498                for (ActionDefinition o: ((JList<ActionDefinition>)c).getSelectedValuesList()) {
     499                    actions.add(o);
     500                }
     501                return new ActionTransferable(actions);
     502            }
     503
     504            @Override
     505            public int getSourceActions(JComponent c) {
     506                return TransferHandler.MOVE;
     507            }
     508
     509            @Override
     510            public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
     511                for (DataFlavor f : transferFlavors) {
     512                    if (ACTION_FLAVOR.equals(f))
     513                        return true;
     514                }
     515                return false;
     516            }
     517
     518            @Override
     519            public void exportAsDrag(JComponent comp, InputEvent e, int action) {
     520                super.exportAsDrag(comp, e, action);
     521                movingComponent = "list";
     522            }
     523
     524            @Override
     525            public boolean importData(JComponent comp, Transferable t) {
     526                try {
     527                    int dropIndex = selectedList.locationToIndex(selectedList.getMousePosition(true));
     528                    @SuppressWarnings("unchecked")
     529                    List<ActionDefinition> draggedData = (List<ActionDefinition>) t.getTransferData(ACTION_FLAVOR);
     530
     531                    Object leadItem = dropIndex >= 0 ? selected.elementAt(dropIndex) : null;
     532                    int dataLength = draggedData.size();
     533
     534                    if (leadItem != null) {
     535                        for (Object o: draggedData) {
     536                            if (leadItem.equals(o))
     537                                return false;
     538                        }
     539                    }
     540
     541                    int dragLeadIndex = -1;
     542                    boolean localDrop = "list".equals(movingComponent);
     543
     544                    if (localDrop) {
     545                        dragLeadIndex = selected.indexOf(draggedData.get(0));
     546                        for (Object o: draggedData) {
     547                            selected.removeElement(o);
     548                        }
     549                    }
     550                    int[] indices = new int[dataLength];
     551
     552                    if (localDrop) {
     553                        int adjustedLeadIndex = selected.indexOf(leadItem);
     554                        int insertionAdjustment = dragLeadIndex <= adjustedLeadIndex ? 1 : 0;
     555                        for (int i = 0; i < dataLength; i++) {
     556                            selected.insertElementAt(draggedData.get(i), adjustedLeadIndex + insertionAdjustment + i);
     557                            indices[i] = adjustedLeadIndex + insertionAdjustment + i;
     558                        }
     559                    } else {
     560                        for (int i = 0; i < dataLength; i++) {
     561                            selected.add(dropIndex, draggedData.get(i));
     562                            indices[i] = dropIndex + i;
     563                        }
     564                    }
     565                    selectedList.clearSelection();
     566                    selectedList.setSelectedIndices(indices);
     567                    movingComponent = "";
     568                    return true;
     569                } catch (Exception e) {
     570                    Main.error(e);
     571                }
     572                return false;
     573            }
     574
     575            @Override
     576            protected void exportDone(JComponent source, Transferable data, int action) {
     577                if ("list".equals(movingComponent)) {
     578                    try {
     579                        List<?> draggedData = (List<?>) data.getTransferData(ACTION_FLAVOR);
     580                        boolean localDrop = selected.contains(draggedData.get(0));
     581                        if (localDrop) {
     582                            int[] indices = selectedList.getSelectedIndices();
     583                            Arrays.sort(indices);
     584                            for (int i = indices.length - 1; i >= 0; i--) {
     585                                selected.remove(indices[i]);
     586                            }
     587                        }
     588                    } catch (Exception e) {
     589                        Main.error(e);
     590                    }
     591                    movingComponent = "";
     592                }
     593            }
     594        }
     595
    493596        private final class Move implements ActionListener {
    494597            @Override
     
    662765
    663766            selectedList.setDragEnabled(true);
    664             selectedList.setTransferHandler(new TransferHandler() {
    665                 @Override
    666                 @SuppressWarnings("unchecked")
    667                 protected Transferable createTransferable(JComponent c) {
    668                     List<ActionDefinition> actions = new ArrayList<>();
    669                     for (ActionDefinition o: ((JList<ActionDefinition>)c).getSelectedValuesList()) {
    670                         actions.add(o);
    671                     }
    672                     return new ActionTransferable(actions);
    673                 }
    674 
    675                 @Override
    676                 public int getSourceActions(JComponent c) {
    677                     return TransferHandler.MOVE;
    678                 }
    679 
    680                 @Override
    681                 public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
    682                     for (DataFlavor f : transferFlavors) {
    683                         if (ACTION_FLAVOR.equals(f))
    684                             return true;
    685                     }
    686                     return false;
    687                 }
    688 
    689                 @Override
    690                 public void exportAsDrag(JComponent comp, InputEvent e, int action) {
    691                     super.exportAsDrag(comp, e, action);
    692                     movingComponent = "list";
    693                 }
    694 
    695                 @Override
    696                 public boolean importData(JComponent comp, Transferable t) {
    697                     try {
    698                         int dropIndex = selectedList.locationToIndex(selectedList.getMousePosition(true));
    699                         @SuppressWarnings("unchecked")
    700                         List<ActionDefinition> draggedData = (List<ActionDefinition>) t.getTransferData(ACTION_FLAVOR);
    701 
    702                         Object leadItem = dropIndex >= 0 ? selected.elementAt(dropIndex) : null;
    703                         int dataLength = draggedData.size();
    704 
    705                         if (leadItem != null) {
    706                             for (Object o: draggedData) {
    707                                 if (leadItem.equals(o))
    708                                     return false;
    709                             }
    710                         }
    711 
    712                         int dragLeadIndex = -1;
    713                         boolean localDrop = "list".equals(movingComponent);
    714 
    715                         if (localDrop) {
    716                             dragLeadIndex = selected.indexOf(draggedData.get(0));
    717                             for (Object o: draggedData) {
    718                                 selected.removeElement(o);
    719                             }
    720                         }
    721                         int[] indices = new int[dataLength];
    722 
    723                         if (localDrop) {
    724                             int adjustedLeadIndex = selected.indexOf(leadItem);
    725                             int insertionAdjustment = dragLeadIndex <= adjustedLeadIndex ? 1 : 0;
    726                             for (int i = 0; i < dataLength; i++) {
    727                                 selected.insertElementAt(draggedData.get(i), adjustedLeadIndex + insertionAdjustment + i);
    728                                 indices[i] = adjustedLeadIndex + insertionAdjustment + i;
    729                             }
    730                         } else {
    731                             for (int i = 0; i < dataLength; i++) {
    732                                 selected.add(dropIndex, draggedData.get(i));
    733                                 indices[i] = dropIndex + i;
    734                             }
    735                         }
    736                         selectedList.clearSelection();
    737                         selectedList.setSelectedIndices(indices);
    738                         movingComponent = "";
    739                         return true;
    740                     } catch (Exception e) {
    741                         Main.error(e);
    742                     }
    743                     return false;
    744                 }
    745 
    746                 @Override
    747                 protected void exportDone(JComponent source, Transferable data, int action) {
    748                     if ("list".equals(movingComponent)) {
    749                         try {
    750                             List<?> draggedData = (List<?>) data.getTransferData(ACTION_FLAVOR);
    751                             boolean localDrop = selected.contains(draggedData.get(0));
    752                             if (localDrop) {
    753                                 int[] indices = selectedList.getSelectedIndices();
    754                                 Arrays.sort(indices);
    755                                 for (int i = indices.length - 1; i >= 0; i--) {
    756                                     selected.remove(indices[i]);
    757                                 }
    758                             }
    759                         } catch (Exception e) {
    760                             Main.error(e);
    761                         }
    762                         movingComponent = "";
    763                     }
    764                 }
    765             });
     767            selectedList.setTransferHandler(new SelectedListTransferHandler());
    766768
    767769            actionsTree.setTransferHandler(new TransferHandler() {
Note: See TracChangeset for help on using the changeset viewer.