Search in sources :

Example 1 with FileTreeModel

use of com.android.tools.idea.ui.FileTreeModel in project android by JetBrains.

the class ConfirmGenerateIconsStep method onEntering.

@Override
protected void onEntering() {
    // Just in case we're entering this step a second time
    myListeners.release(mySelectedSourceSet);
    myListeners.receiveAndFire(mySelectedSourceSet, sourceSet -> {
        AndroidIconGenerator iconGenerator = getModel().getIconGenerator();
        File resDir = sourceSet.getPaths().getResDirectory();
        if (iconGenerator == null || resDir == null || resDir.getParentFile() == null) {
            return;
        }
        final Map<File, BufferedImage> pathIconMap = iconGenerator.generateIntoFileMap(sourceSet.getPaths());
        myFilesAlreadyExist.set(false);
        int minHeight = Integer.MAX_VALUE;
        int maxHeight = Integer.MIN_VALUE;
        for (BufferedImage image : pathIconMap.values()) {
            minHeight = Math.min(minHeight, image.getHeight());
            maxHeight = Math.max(maxHeight, image.getHeight());
        }
        ImmutableSortedSet.Builder<File> sortedPaths = ImmutableSortedSet.orderedBy(new Comparator<File>() {

            @Override
            public int compare(File file1, File file2) {
                String path1 = file1.getAbsolutePath();
                String path2 = file2.getAbsolutePath();
                Density density1 = CategoryIconMap.pathToDensity(path1);
                Density density2 = CategoryIconMap.pathToDensity(path2);
                if (density1 != null && density2 != null && density1 != density2) {
                    return Ints.compare(density2.ordinal(), density1.ordinal());
                } else {
                    BufferedImage image1 = pathIconMap.get(file1);
                    BufferedImage image2 = pathIconMap.get(file2);
                    int compareValue = Ints.compare(image2.getHeight(), image1.getHeight());
                    return (compareValue != 0) ? compareValue : path2.compareTo(path1);
                }
            }
        });
        sortedPaths.addAll(pathIconMap.keySet());
        FileTreeModel treeModel = new FileTreeModel(resDir.getParentFile(), true);
        for (File path : sortedPaths.build()) {
            Image image = pathIconMap.get(path);
            if (path.exists()) {
                myFilesAlreadyExist.set(true);
            }
            if (maxHeight > MAX_TREE_ROW_HEIGHT) {
                int hCurr = image.getHeight(null);
                int wCurr = image.getWidth(null);
                double hScale;
                if (maxHeight != minHeight) {
                    double hCurrPercent = (double) (hCurr - minHeight) / (double) (maxHeight - minHeight);
                    double scaledDeltaH = hCurrPercent * (MAX_TREE_ROW_HEIGHT - minHeight);
                    double hCurrScaled = minHeight + scaledDeltaH;
                    hScale = hCurrScaled / hCurr;
                } else {
                    hScale = MAX_TREE_ROW_HEIGHT / (double) hCurr;
                }
                int hFinal = (int) (hCurr * hScale);
                int wFinal = (int) (wCurr * hScale);
                image = image.getScaledInstance(wFinal, hFinal, Image.SCALE_SMOOTH);
            }
            treeModel.forceAddFile(path, new ImageIcon(image));
        }
        myOutputPreviewTree.setModel(treeModel);
        for (int i = 0; i < myOutputPreviewTree.getRowCount(); ++i) {
            myOutputPreviewTree.expandRow(i);
        }
    });
}
Also used : AndroidIconGenerator(com.android.tools.idea.npw.assetstudio.icon.AndroidIconGenerator) BufferedImage(java.awt.image.BufferedImage) Density(com.android.resources.Density) BufferedImage(java.awt.image.BufferedImage) FileTreeModel(com.android.tools.idea.ui.FileTreeModel) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) File(java.io.File)

Aggregations

Density (com.android.resources.Density)1 AndroidIconGenerator (com.android.tools.idea.npw.assetstudio.icon.AndroidIconGenerator)1 FileTreeModel (com.android.tools.idea.ui.FileTreeModel)1 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1