From 47291b0776cff35c018cc23cc6cfad3d10df7ccf Mon Sep 17 00:00:00 2001 From: humblerookie Date: Sun, 26 Mar 2017 19:29:16 +0530 Subject: [PATCH] Version 3.0 - Custom Buckets - Select/Deselect scaling bucket - Detect new buckets. --- dimenify.jar | Bin 11490 -> 0 bytes resources/META-INF/plugin.xml | 61 ++-- src/com/hr/dimenify/Constants.java | 21 -- src/com/hr/dimenify/Field.java | 25 -- src/com/hr/dimenify/GenerateAction.java | 234 ------------- src/com/hr/dimenify/GenerateDialog.java | 85 ----- .../dimenify/action/AbstractDimenAction.java | 132 ++++++++ .../hr/dimenify/action/GenerateAction.java | 176 ++++++++++ .../hr/dimenify/dialogs/GenerateDialog.java | 316 ++++++++++++++++++ src/com/hr/dimenify/model/Dimen.java | 67 ++++ src/com/hr/dimenify/util/Constants.java | 32 ++ src/com/hr/dimenify/util/ModelUtil.java | 24 ++ 12 files changed, 780 insertions(+), 393 deletions(-) delete mode 100644 dimenify.jar delete mode 100644 src/com/hr/dimenify/Constants.java delete mode 100644 src/com/hr/dimenify/Field.java delete mode 100644 src/com/hr/dimenify/GenerateAction.java delete mode 100644 src/com/hr/dimenify/GenerateDialog.java create mode 100644 src/com/hr/dimenify/action/AbstractDimenAction.java create mode 100644 src/com/hr/dimenify/action/GenerateAction.java create mode 100644 src/com/hr/dimenify/dialogs/GenerateDialog.java create mode 100644 src/com/hr/dimenify/model/Dimen.java create mode 100644 src/com/hr/dimenify/util/Constants.java create mode 100644 src/com/hr/dimenify/util/ModelUtil.java diff --git a/dimenify.jar b/dimenify.jar deleted file mode 100644 index b83ced37b5dc4cbef82546ab896241445c00f37d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 11490 zcmai)1yEhhvZ!&_;1GhlI~xt|5Zo=eyIX=g1b270jk|4};I6^lB|%>>Wp)kSV;NZZXENrF0{t!$sC@?uO6(I&Gd2uE=A$cirF=Z7- zIq{bfFtBU6u~AtW2F59583x*kvB`QR=2^DQT}MV~Ie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;BTJ+dBzD!S`+}Y1bD(u_Nh=CSB3zmut945eov5i|GisC^8)qg2CKI27wrW z4MqDIK*3B9gazjqIbCJ?BB>P_q#>-GoJTlXgU!(3322PZFZ3o*THIQik81l;e>lssS zSbwkXsgZ_C?=jaSZ{BJIioY0Vc%0bQn`JUWlP_<0@?XO7 zDEdS4Zn5?##eAikT@JQ#xV=2`_alu9xCo~{-MoY^F5{;%;=NHgnd8J8k_17oaz?wb zf?V{Xi9X(aN|f!PrSG*gON!NObtT${U!#9EKN`JL3(g zcR;~Ix-jGCFd%|KA0&8mo`_nRhiccIexRSc zh=dH7AJomm;3@T7CBy#4tN_VPO?tDbN6jrP#zf+tnjD0QE8L?94Z=)We`OIO*Do5m zXfQ(%-%>W9sbQLlPE9=;ktJuU&#Eepl)YV0LZW%U$Vhk;E6;!s82#0LR$GpXDujufb-Xo@c;fZ1`$jp9MrAM*pv&;U9Z zVD=0V+oK<`td zZ+{;CrXYJ5-H0F{hyafq)m~le$6~wJ=S>2%z%ab!aP*bMHK18)A)RE^qJ?_OlcqlzQ#$%+~QpT$FFW2EO3Jyu7 z*Pr+~YIArB9cp&oo5fAr5T1a?Qa8M}Dll*AU%WTTsXVz3O6x0Ty4r@-F1Gd!Oi{J^kq=I7Y=h_bICY6NA-T7|sD_HExwpxw zt%k-NEO(oJsKL;_GgQ=Scr;><>=v zPeZ@@^e5Nx3*GuH`OyEjp??o@{cZElbo5u!`CD>+e#ZZPx&LkRpA_^@i@&=2XN>sY z7WoPPv&BD?+Fus`d&&GAG=9aF-}0T}j}Y?bi~Oqg=UV<1YkmvK&(Zsz_4+&Ve2{^L U`K5&T^LP0ZoLl*sqJx3`ALTR7IRF3v diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index 8806fea..6c3611e 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -1,10 +1,10 @@ - com.hr.dimenify - Dimenify - 2.1 - Anvith Bhat + com.hr.dimenify + Dimenify + 3.0 + Anvith Bhat - USAGE
- Just point the cursor on the dimension value in your dimen
@@ -15,7 +15,12 @@ ]]>
- v2.2
+ - Added ability to provide custom buckets upto a max of 5. + - User can now choose to exclude any buckets from current dimension generation. + - Auto detect new buckets and prompt user for scale value. + v2.1
- Bugfix for some locales where comma is used as decimal separator. v2.0
@@ -27,27 +32,27 @@ v1.0
Contains hard bound scaling factors. ]]> -
- - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + +
\ No newline at end of file diff --git a/src/com/hr/dimenify/Constants.java b/src/com/hr/dimenify/Constants.java deleted file mode 100644 index 1ce5e90..0000000 --- a/src/com/hr/dimenify/Constants.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.hr.dimenify; - -/** - * Created by anvithbhat on 19/12/16. - */ -public interface Constants { - String SAVE_PREFIX ="com.hr.dimenify."; - String RESOURCES_TEXT = "\n"; - String RESOURCES_TAG = "resources"; - String DIMEN_TAG = "dimen"; - String NAME_TAG = "name"; - String PLACEHOLDER_DIMEN = "{1}{2}"; - float[] SCALE_FACTORS_DP = {1.0f, 1.2f, 1.8f, 2.4f, 3.0f}; - float[] SCALE_FACTORS_SP = {1.0f, 1.2f, 1.8f, 2.4f, 3.0f}; - String FILE_NAME = "dimens.xml"; - String[] DIRECTORIES = {"values", "values-hdpi", "values-xhdpi", "values-xxhdpi", "values-xxxhdpi"}; - String[] BUCKETS = {"mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"}; - String DP = "dp"; - String SP = "sp"; - String TITLE = "Set scale factors"; -} diff --git a/src/com/hr/dimenify/Field.java b/src/com/hr/dimenify/Field.java deleted file mode 100644 index 2b81c4a..0000000 --- a/src/com/hr/dimenify/Field.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.hr.dimenify; - -/** - * Created by anvithbhat on 19/12/16. - */ -public class Field { - private String name; - private String value; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } -} diff --git a/src/com/hr/dimenify/GenerateAction.java b/src/com/hr/dimenify/GenerateAction.java deleted file mode 100644 index 2ba1f23..0000000 --- a/src/com/hr/dimenify/GenerateAction.java +++ /dev/null @@ -1,234 +0,0 @@ -package com.hr.dimenify; - -import com.intellij.ide.util.PropertiesComponent; -import com.intellij.openapi.actionSystem.*; -import com.intellij.openapi.command.WriteCommandAction; -import com.intellij.openapi.editor.Document; -import com.intellij.openapi.editor.Editor; -import com.intellij.openapi.fileTypes.StdFileTypes; -import com.intellij.openapi.project.Project; -import com.intellij.psi.PsiDirectory; -import com.intellij.psi.PsiDocumentManager; -import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiFile; -import com.intellij.psi.impl.source.xml.XmlAttributeImpl; -import com.intellij.psi.impl.source.xml.XmlDocumentImpl; -import com.intellij.psi.impl.source.xml.XmlTagImpl; -import com.intellij.psi.impl.source.xml.XmlTextImpl; - -import javax.swing.*; -import java.text.DecimalFormat; -import java.text.DecimalFormatSymbols; -import java.text.MessageFormat; -import java.text.NumberFormat; -import java.util.Locale; -import java.util.concurrent.atomic.AtomicInteger; - -import static com.hr.dimenify.Constants.*; - - -public class GenerateAction extends AnAction { - - AtomicInteger fileCreationCount = new AtomicInteger(0); - int currentBucketIndex; - Project project; - String values[]; - - @Override - public void actionPerformed(AnActionEvent e) { - Locale.setDefault(new Locale("pt","BR")); - PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE); - - project = e.getRequiredData(CommonDataKeys.PROJECT); - Editor editor = PlatformDataKeys.EDITOR.getData(e.getDataContext()); - - if (psiFile == null || editor == null) { - e.getPresentation().setEnabled(false); - return; - } - - fileCreationCount.set(0); - - int offset = editor.getCaretModel().getOffset(); - PsiElement psiElement = psiFile.findElementAt(offset); - if (psiFile.getFileType() == StdFileTypes.XML) { - currentBucketIndex = getBucketIndex(psiFile); - values = getInsertionValuesElement(psiElement); - if (values == null) { - e.getPresentation().setEnabled(false); - return; - } else { - if (psiFile.getParent().getParent() != null) { - PsiDirectory psiDirectory = psiFile.getParent().getParent(); - createDirectoriesAndFilesIfNeeded(psiDirectory); - - } - } - } else { - e.getPresentation().setEnabled(false); - return; - } - } - - private int getBucketIndex(PsiFile psiFile) { - - PsiDirectory psiDirectory = psiFile.getParent(); - for (int i = 0; i < Constants.DIRECTORIES.length; i++) { - if (psiDirectory.getName().equals(Constants.DIRECTORIES[i])) { - return i; - } - } - return 0; - } - - private void writeScaledValuesToFiles(PsiDirectory directory, int currentBucketIndex, String[] values) { - for (int i = 0; i < values.length; i++) { - if (i != currentBucketIndex) { - PsiFile file = directory.findSubdirectory(Constants.DIRECTORIES[i]).findFile(Constants.FILE_NAME); - - - final int x = i; - WriteCommandAction.runWriteCommandAction(project, new Runnable() { - @Override - public void run() { - Document document = PsiDocumentManager.getInstance(project).getDocument(file); - document.setReadOnly(false); - String text = document.getText(); - int index = text.indexOf(">") + 1; - StringBuilder stringBuilder = new StringBuilder(text); - document.setText(stringBuilder.insert(index, "\n" + values[x]).toString()); - } - }); - - - } - } - } - - private void createDirectoriesAndFilesIfNeeded(PsiDirectory psiParent) { - for (String directory : - Constants.DIRECTORIES) { - - WriteCommandAction.runWriteCommandAction(project, () -> { - PsiDirectory subDirectory = psiParent.findSubdirectory(directory); - if (subDirectory == null) { - subDirectory = psiParent.createSubdirectory(directory); - } - PsiFile file = subDirectory.findFile(Constants.FILE_NAME); - if (file == null) { - PsiFile psiFile = subDirectory.createFile(Constants.FILE_NAME); - - - Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile); - document.setText(Constants.RESOURCES_TEXT); - fileCreationCompleteAndCheck(psiParent); - - - } else { - fileCreationCompleteAndCheck(psiParent); - } - }); - } - } - - private void fileCreationCompleteAndCheck(PsiDirectory psiDirectory) { - int value = fileCreationCount.incrementAndGet(); - if (value == Constants.DIRECTORIES.length) { - writeScaledValuesToFiles(psiDirectory, currentBucketIndex, values); - } - } - - private String[] getInsertionValuesElement( PsiElement psiElement) { - PsiElement selectedNode = psiElement; - PsiElement rootParent; - PsiElement subNode; - if (psiElement.getParent() != null && psiElement.getParent().getParent() != null) { - subNode = psiElement.getParent(); - rootParent = subNode.getParent(); - - while (!(rootParent instanceof XmlDocumentImpl)) { - rootParent = rootParent.getParent(); - subNode = subNode.getParent(); - selectedNode = selectedNode.getParent(); - } - - if (subNode instanceof XmlTagImpl && selectedNode instanceof XmlTagImpl) { - XmlTagImpl currentNode = (XmlTagImpl) selectedNode; - if (((XmlTagImpl) subNode).getName().equals(Constants.RESOURCES_TAG) && currentNode.getName().equals(Constants.DIMEN_TAG)) { - XmlAttributeImpl attribute = null; - XmlTextImpl value = null; - for (PsiElement element : currentNode.getChildren()) { - if (element instanceof XmlAttributeImpl && ((XmlAttributeImpl) element).getLocalName().equals(Constants.NAME_TAG)) { - attribute = (XmlAttributeImpl) element; - } else if (element instanceof XmlTextImpl) { - value = (XmlTextImpl) element; - } - } - - if (attribute != null) { - String attributeName = attribute.getValue(); - String val = value.getValue().toLowerCase().trim(); - - if (val.endsWith(Constants.DP) || val.endsWith(Constants.SP)) { - - return showScaleDialog(attributeName,val,val.endsWith(Constants.DP)); - } - - } - } - } - return null; - - } else { - return null; - } - } - - private String[] showScaleDialog(String attributeName, String val, boolean isDp) { - GenerateDialog generateDialog = new GenerateDialog(project,isDp); - generateDialog.show(); - if(generateDialog.isOK()) { - DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.getDefault()); - otherSymbols.setDecimalSeparator('.'); - otherSymbols.setGroupingSeparator(','); - DecimalFormat formatter = new DecimalFormat("#0.0",otherSymbols); - saveValues(generateDialog,isDp); - - float scaleFactor[] = new float[BUCKETS.length]; - for (int i = 0; i < BUCKETS.length; i++) { - scaleFactor[i]=PropertiesComponent.getInstance().getFloat(SAVE_PREFIX+(isDp? DP:SP)+BUCKETS[i], 0); - } - float mdpiValue = Float.parseFloat(val.substring(0, val.length() - 2)) / scaleFactor[currentBucketIndex]; - float scaledValues[] = new float[scaleFactor.length]; - String elementsScaled[] = new String[scaleFactor.length]; - - for (int i = 0; i < scaledValues.length; i++) { - scaledValues[i] = mdpiValue * scaleFactor[i]; - elementsScaled[i] = MessageFormat.format(Constants.PLACEHOLDER_DIMEN, attributeName - , formatter.format(scaledValues[i]) - , val.endsWith(Constants.DP) ? Constants.DP : Constants.SP); - } - return elementsScaled; - } - return null; - } - - private void saveValues(GenerateDialog generateDialog, boolean isDp) { - for (int i = 0; i < generateDialog.getTextFields().length; i++) { - JTextField textField = generateDialog.getTextFields()[i]; - float factor=isDp? SCALE_FACTORS_DP[i]: SCALE_FACTORS_SP[i]; - if(textField.getText().trim().length() !=0) { - try { - float f= Float.parseFloat(textField.getText().trim()); - if(f >0) { - factor = f; - } - }catch (NumberFormatException ex) { - } - } - PropertiesComponent.getInstance().setValue(SAVE_PREFIX+(isDp? DP:SP)+BUCKETS[i], factor,-1); - } - } - - -} diff --git a/src/com/hr/dimenify/GenerateDialog.java b/src/com/hr/dimenify/GenerateDialog.java deleted file mode 100644 index a05f67b..0000000 --- a/src/com/hr/dimenify/GenerateDialog.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.hr.dimenify; - -import com.intellij.ide.util.PropertiesComponent; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.ui.DialogWrapper; -import com.intellij.openapi.ui.LabeledComponent; -import org.jetbrains.annotations.Nullable; - -import javax.swing.*; -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; - -import static com.hr.dimenify.Constants.*; - -public class GenerateDialog extends DialogWrapper { - - private List fields; - private JPanel controlPanel; - private JLabel labels[]; - private JTextField textFields[]; - - LabeledComponent component; - protected GenerateDialog(@Nullable Project project, boolean isDp) { - super(project); - - setTitle(Constants.TITLE); - fields = new ArrayList(); - labels= new JLabel[BUCKETS.length]; - textFields= new JTextField[BUCKETS.length]; - initializePanel(isDp); - init(); - } - - private void initializePanel(boolean isDp) { - controlPanel= new JPanel(); - GroupLayout layout = new GroupLayout(controlPanel); - controlPanel.setLayout(layout); - // Turn on automatically adding gaps between components - layout.setAutoCreateGaps(true); - // Create a sequential group for the horizontal axis. - GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup(); - GroupLayout.Group yLabelGroup = layout.createParallelGroup(GroupLayout.Alignment.TRAILING); - hGroup.addGroup(yLabelGroup); - GroupLayout.Group yFieldGroup = layout.createParallelGroup(); - hGroup.addGroup(yFieldGroup); - layout.setHorizontalGroup(hGroup); - // Create a sequential group for the vertical axis. - GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup(); - layout.setVerticalGroup(vGroup); - - int preferredSize = GroupLayout.PREFERRED_SIZE; - // add the components to the group - - for (int i = 0; i < BUCKETS.length; i++) { - String bucket= BUCKETS[i]; - labels[i]= new JLabel(); - labels[i].setText(bucket); - textFields[i]= new JTextField(); - textFields[i].setColumns(20); - float value = PropertiesComponent.getInstance().getFloat(SAVE_PREFIX+(isDp? DP:SP)+BUCKETS[i],isDp? SCALE_FACTORS_DP[i] :SCALE_FACTORS_SP[i]); - textFields[i].setText(value+""); - - yLabelGroup.addComponent(labels[i]); - yFieldGroup.addComponent(textFields[i],preferredSize,preferredSize,preferredSize); - } - - for (int ii = 0; ii < labels.length; ii++) { - vGroup.addGroup(layout.createParallelGroup(). - addComponent(labels[ii]). - addComponent(textFields[ii], preferredSize, preferredSize, preferredSize)); - } - component = LabeledComponent.create(controlPanel,""); - } - - @Nullable - @Override - protected JComponent createCenterPanel() { - return component; - } - - public JTextField[] getTextFields() { - return textFields; - } -} diff --git a/src/com/hr/dimenify/action/AbstractDimenAction.java b/src/com/hr/dimenify/action/AbstractDimenAction.java new file mode 100644 index 0000000..32b3700 --- /dev/null +++ b/src/com/hr/dimenify/action/AbstractDimenAction.java @@ -0,0 +1,132 @@ +package com.hr.dimenify.action; + +import com.hr.dimenify.model.Dimen; +import com.hr.dimenify.util.Constants; +import com.hr.dimenify.util.ModelUtil; +import com.intellij.ide.util.PropertiesComponent; +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.CommonDataKeys; +import com.intellij.openapi.command.WriteCommandAction; +import com.intellij.openapi.editor.Document; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiDirectory; +import com.intellij.psi.PsiDocumentManager; +import com.intellij.psi.PsiFile; + +import javax.swing.*; +import java.util.ArrayList; +import java.util.Locale; +import java.util.concurrent.atomic.AtomicInteger; + +import static com.hr.dimenify.util.Constants.*; + +public abstract class AbstractDimenAction extends AnAction { + + protected ArrayList data; + protected Project project; + protected String values[]; + protected AtomicInteger fileCreationCount = new AtomicInteger(0); + protected int currentBucketIndex; + + @Override + public void actionPerformed(AnActionEvent e) { + Locale.setDefault(new Locale("pt", "BR")); + project = e.getRequiredData(CommonDataKeys.PROJECT); + data = ModelUtil.fromJson(PropertiesComponent.getInstance().getValue(Constants.SAVE_PREFIX_V2, Constants.INIT_MODEL_JSON)); + fileCreationCount.set(0); + } + + protected void migrateData() { + if (!PropertiesComponent.getInstance().getBoolean(MIGRATION_FLAG)) { + for (Dimen datum : data) { + datum.setFactorDp(PropertiesComponent.getInstance().getFloat(Constants.SAVE_PREFIX + Constants.DP + datum.getBucket(), datum.getFactorDp())); + datum.setFactorSp(PropertiesComponent.getInstance().getFloat(Constants.SAVE_PREFIX + Constants.SP + datum.getBucket(), datum.getFactorSp())); + } + PropertiesComponent.getInstance().setValue(MIGRATION_FLAG, true); + } + saveValues(data); + } + + protected void saveValues(ArrayList data) { + String value = ModelUtil.toJson(data); + PropertiesComponent.getInstance().setValue(SAVE_PREFIX_V2, value); + } + + + protected int getBucketIndex(PsiFile psiFile) { + + PsiDirectory psiDirectory = psiFile.getParent(); + + String value = PropertiesComponent.getInstance().getValue(SAVE_PREFIX_V2, Constants.INIT_MODEL_JSON); + ArrayList data = ModelUtil.fromJson(value); + for (int i = 0; i < data.size(); i++) { + if (psiDirectory.getName().equals(data.get(i).getDirectory())) { + return i; + } + } + return -1; + } + + protected void showAlert(int errorIndex) { + JOptionPane optionPane = new JOptionPane(ERROR_MESSAGES[errorIndex], JOptionPane.WARNING_MESSAGE); + JDialog dialog = optionPane.createDialog(Constants.ERROR_TITLE); + dialog.setAlwaysOnTop(true); + dialog.setVisible(true); + } + + protected void createDirectoriesAndFilesIfNeeded(PsiDirectory psiParent) { + for (Dimen datum : data) { + WriteCommandAction.runWriteCommandAction(project, () -> { + PsiDirectory subDirectory = psiParent.findSubdirectory(datum.getDirectory()); + if (subDirectory == null) { + subDirectory = psiParent.createSubdirectory(datum.getDirectory()); + } + PsiFile file = subDirectory.findFile(Constants.FILE_NAME); + if (file == null) { + PsiFile psiFile = subDirectory.createFile(Constants.FILE_NAME); + + + Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile); + document.setText(Constants.RESOURCES_TEXT); + fileCreationCompleteAndCheck(psiParent); + + + } else { + fileCreationCompleteAndCheck(psiParent); + } + }); + } + } + + protected void fileCreationCompleteAndCheck(PsiDirectory psiDirectory) { + int value = fileCreationCount.incrementAndGet(); + if (value == data.size()) { + writeScaledValuesToFiles(psiDirectory, currentBucketIndex, values); + } + } + + protected void writeScaledValuesToFiles(PsiDirectory directory, int currentBucketIndex, String[] values) { + for (int i = 0; i < values.length; i++) { + if (i != currentBucketIndex && data.get(i).isSelected()) { + PsiFile file = directory.findSubdirectory(data.get(i).getDirectory()).findFile(Constants.FILE_NAME); + + + final int x = i; + WriteCommandAction.runWriteCommandAction(project, new Runnable() { + @Override + public void run() { + Document document = PsiDocumentManager.getInstance(project).getDocument(file); + document.setReadOnly(false); + String text = document.getText(); + int index = text.indexOf(">") + 1; + StringBuilder stringBuilder = new StringBuilder(text); + document.setText(stringBuilder.insert(index, "\n" + values[x]).toString()); + } + }); + + + } + } + } +} diff --git a/src/com/hr/dimenify/action/GenerateAction.java b/src/com/hr/dimenify/action/GenerateAction.java new file mode 100644 index 0000000..badfe59 --- /dev/null +++ b/src/com/hr/dimenify/action/GenerateAction.java @@ -0,0 +1,176 @@ +package com.hr.dimenify.action; + +import com.hr.dimenify.dialogs.GenerateDialog; +import com.hr.dimenify.model.Dimen; +import com.hr.dimenify.util.Constants; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.LangDataKeys; +import com.intellij.openapi.actionSystem.PlatformDataKeys; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.fileTypes.StdFileTypes; +import com.intellij.psi.PsiDirectory; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiFile; +import com.intellij.psi.impl.source.xml.XmlAttributeImpl; +import com.intellij.psi.impl.source.xml.XmlDocumentImpl; +import com.intellij.psi.impl.source.xml.XmlTagImpl; +import com.intellij.psi.impl.source.xml.XmlTextImpl; + +import javax.swing.*; +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Locale; + + +public class GenerateAction extends AbstractDimenAction { + + @Override + public void actionPerformed(AnActionEvent e) { + super.actionPerformed(e); + PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE); + migrateData(); + Editor editor = PlatformDataKeys.EDITOR.getData(e.getDataContext()); + + if (psiFile == null || editor == null) { + e.getPresentation().setEnabled(false); + return; + } + + int offset = editor.getCaretModel().getOffset(); + PsiElement psiElement = psiFile.findElementAt(offset); + if (psiFile.getFileType() == StdFileTypes.XML) { + currentBucketIndex = getBucketIndex(psiFile); + if (currentBucketIndex == -1) { + if (data.size() == 10) { + showAlert(1); + return; + } + String directoryName = psiFile.getParent().getName(); + if (directoryName.startsWith(Constants.VALUES_PREFIX)) { + String bucket = directoryName.substring(Constants.VALUES_PREFIX.length()); + String val = JOptionPane.showInputDialog(null, Constants.SCALE_TEXT_PREFIX + bucket, Constants.NEW_BUCKET, JOptionPane.INFORMATION_MESSAGE); + try { + float value = Float.parseFloat(val); + Dimen dimen = new Dimen().setBucket(bucket).setFactorDp(value).setFactorSp(value).setMandatory(false).setSelected(true); + data.add(dimen); + saveValues(data); + currentBucketIndex = data.size() - 1; + } catch (NullPointerException ex) { + return; + } catch (NumberFormatException ex) { + showAlert(3); + return; + } + } else { + showAlert(2); + return; + } + + } + values = getInsertionValuesElement(psiElement); + if (values == null) { + e.getPresentation().setEnabled(false); + return; + } else { + if (psiFile.getParent().getParent() != null) { + PsiDirectory psiDirectory = psiFile.getParent().getParent(); + createDirectoriesAndFilesIfNeeded(psiDirectory); + + } + } + } else { + e.getPresentation().setEnabled(false); + return; + } + } + + + private String[] getInsertionValuesElement(PsiElement psiElement) { + PsiElement selectedNode = psiElement; + PsiElement rootParent; + PsiElement subNode; + if (psiElement != null && psiElement.getParent() != null && psiElement.getParent().getParent() != null) { + subNode = psiElement.getParent(); + rootParent = subNode.getParent(); + + while (!(rootParent instanceof XmlDocumentImpl)) { + rootParent = rootParent.getParent(); + subNode = subNode.getParent(); + selectedNode = selectedNode.getParent(); + } + + if (subNode instanceof XmlTagImpl && selectedNode instanceof XmlTagImpl) { + XmlTagImpl currentNode = (XmlTagImpl) selectedNode; + if (((XmlTagImpl) subNode).getName().equals(Constants.RESOURCES_TAG) && currentNode.getName().equals(Constants.DIMEN_TAG)) { + XmlAttributeImpl attribute = null; + XmlTextImpl value = null; + for (PsiElement element : currentNode.getChildren()) { + if (element instanceof XmlAttributeImpl && ((XmlAttributeImpl) element).getLocalName().equals(Constants.NAME_TAG)) { + attribute = (XmlAttributeImpl) element; + } else if (element instanceof XmlTextImpl) { + value = (XmlTextImpl) element; + } + } + + if (attribute != null) { + String attributeName = attribute.getValue(); + String val = value.getValue().toLowerCase().trim(); + + if (val.endsWith(Constants.DP) || val.endsWith(Constants.SP)) { + + return showScaleDialog(attributeName, val, val.endsWith(Constants.DP)); + } + + } + } + } + return null; + + } else { + return null; + } + } + + private String[] showScaleDialog(String attributeName, String val, boolean isDp) { + GenerateDialog generateDialog = new GenerateDialog(project, isDp, data); + generateDialog.show(); + int invalidIndex = generateDialog.invalidBucketIndex(); + if (generateDialog.isOK() && invalidIndex == 0) { + DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.getDefault()); + otherSymbols.setDecimalSeparator('.'); + otherSymbols.setGroupingSeparator(','); + DecimalFormat formatter = new DecimalFormat("#0.0", otherSymbols); + ArrayList data = generateDialog.getConversionValues(); + saveValues(data); + + + float scaleFactor[] = new float[data.size()]; + for (int i = 0; i < data.size(); i++) { + scaleFactor[i] = isDp ? data.get(i).getFactorDp() : data.get(i).getFactorSp(); + } + float mdpiValue = 0; + try { + mdpiValue = Float.parseFloat(val.substring(0, val.length() - 2)) / scaleFactor[currentBucketIndex]; + } catch (NullPointerException | NumberFormatException e) { + return null; + } + float scaledValues[] = new float[scaleFactor.length]; + String elementsScaled[] = new String[scaleFactor.length]; + + for (int i = 0; i < scaledValues.length; i++) { + scaledValues[i] = mdpiValue * scaleFactor[i]; + elementsScaled[i] = MessageFormat.format(Constants.PLACEHOLDER_DIMEN, attributeName + , formatter.format(scaledValues[i]) + , val.endsWith(Constants.DP) ? Constants.DP : Constants.SP); + } + return elementsScaled; + } else if (invalidIndex != 0) { + generateDialog.showAlert(invalidIndex); + } + return null; + } + + +} diff --git a/src/com/hr/dimenify/dialogs/GenerateDialog.java b/src/com/hr/dimenify/dialogs/GenerateDialog.java new file mode 100644 index 0000000..58b326f --- /dev/null +++ b/src/com/hr/dimenify/dialogs/GenerateDialog.java @@ -0,0 +1,316 @@ +package com.hr.dimenify.dialogs; + +import com.hr.dimenify.model.Dimen; +import com.hr.dimenify.util.Constants; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.DialogWrapper; +import com.intellij.openapi.ui.LabeledComponent; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.*; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import static com.hr.dimenify.util.Constants.ERROR_CODE; +import static com.hr.dimenify.util.Constants.ERROR_MESSAGES; + +public class GenerateDialog extends DialogWrapper { + private JPanel controlPanel; + private List bucketLabels = new ArrayList<>(); + private List selectionValues = new ArrayList<>(); + private List bucketScaleFactors = new ArrayList<>(); + private List removeButtons = new ArrayList<>(); + private ArrayList data = new ArrayList<>(); + GroupLayout layout; + + //float value = PropertiesComponent.getInstance().getFloat(SAVE_PREFIX + (isDp ? DP : SP) + dimen.getBucket(), ); + LabeledComponent component; + + boolean isDp; + + public GenerateDialog(@Nullable Project project, boolean isDp, ArrayList data) { + super(project); + this.isDp = isDp; + this.data = data; + setTitle(Constants.TITLE + (isDp ? Constants.DP : Constants.SP) + Constants.METRIC); + initializePanel(isDp); + init(); + } + + private void addInitialFields(boolean isDp) { + for (int i = 0; i < data.size(); i++) { + final Dimen dimen = data.get(i); + JLabel bucketLabel = new JLabel(); + bucketLabels.add(bucketLabel); + bucketLabel.setText(dimen.getBucket()); + final JTextField scaleFactor = new JTextField(); + bucketScaleFactors.add(scaleFactor); + scaleFactor.setColumns(20); + scaleFactor.getDocument().addDocumentListener(new DocumentListener() { + private void setData() { + float val = 0; + try { + val = Float.parseFloat(scaleFactor.getText()); + } catch (NullPointerException | NumberFormatException ex) { + + } + if (isDp) { + dimen.setFactorDp(val); + + } else { + dimen.setFactorSp(val); + } + } + + @Override + public void insertUpdate(DocumentEvent e) { + setData(); + } + + @Override + public void removeUpdate(DocumentEvent e) { + setData(); + } + + @Override + public void changedUpdate(DocumentEvent e) { + setData(); + } + }); + bucketScaleFactors.get(i).setText((isDp ? dimen.getFactorDp() : dimen.getFactorSp()) + ""); + + JCheckBox selectedCheckBox = new JCheckBox(); + selectedCheckBox.setSelected(dimen.isSelected()); + selectedCheckBox.addChangeListener(new ChangeListener() { + @Override + public void stateChanged(ChangeEvent e) { + dimen.setSelected(selectedCheckBox.isSelected()); + } + }); + if (!dimen.isMandatory()) { + JButton removeButton = new JButton("-"); + removeButton.addActionListener(new AbstractAction() { + @Override + public void actionPerformed(ActionEvent e) { + removeLayoutValues(dimen, bucketLabel, scaleFactor, selectedCheckBox, removeButton); + setLayoutConstraints(); + layout.invalidateLayout(controlPanel); + } + }); + removeButtons.add(removeButton); + + } + selectionValues.add(selectedCheckBox); + } + + } + + private void initializePanel(boolean isDp) { + controlPanel = new JPanel(); + + + addInitialFields(isDp); + layout = new GroupLayout(controlPanel); + controlPanel.setLayout(layout); + layout.setAutoCreateGaps(true); + layout.setAutoCreateContainerGaps(true); + setLayoutConstraints(); + component = LabeledComponent.create(controlPanel, ""); + } + + private void setLayoutConstraints() { + GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup(); + + GroupLayout.ParallelGroup group1 = layout.createParallelGroup(); + GroupLayout.ParallelGroup group2 = layout.createParallelGroup(); + GroupLayout.ParallelGroup group3 = layout.createParallelGroup(); + GroupLayout.ParallelGroup group4 = layout.createParallelGroup(); + for (int i = 0; i < bucketLabels.size(); i++) { + group1.addComponent(bucketLabels.get(i)); + group2.addComponent(bucketScaleFactors.get(i)); + group3.addComponent(selectionValues.get(i)); + if (i < removeButtons.size()) { + + group4.addComponent(removeButtons.get(i)); + } + } + hGroup.addGroup(group1); + hGroup.addGroup(group2); + hGroup.addGroup(group3); + if (removeButtons.size() > 0) { + hGroup.addGroup(group4); + } + layout.setHorizontalGroup(hGroup); + + GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup(); + for (int i = 0, k = 0; i < bucketLabels.size(); i++) { + GroupLayout.ParallelGroup parallelGroup = layout.createParallelGroup(GroupLayout.Alignment.BASELINE); + parallelGroup.addComponent(bucketLabels.get(i)).addComponent(bucketScaleFactors.get(i)).addComponent(selectionValues.get(i)); + if (!data.get(i).isMandatory()) { + parallelGroup.addComponent(removeButtons.get(k++)); + } + vGroup.addGroup(parallelGroup); + } + layout.setVerticalGroup(vGroup); + + } + + @NotNull + @Override + protected Action[] createActions() { + Action[] actions = super.createActions(); + Action[] actionsAdd = new Action[actions.length + 1]; + for (int i = 0; i < actions.length; i++) { + actionsAdd[i] = actions[i]; + } + actionsAdd[actionsAdd.length - 1] = new DialogWrapperAction("Add Bucket") { + @Override + protected void doAction(ActionEvent actionEvent) { + + int invalidIndex = invalidBucketIndex(); + if (invalidIndex == 0) { + JTextField bucketValue = new JTextField(); + bucketValue.setColumns(15); + bucketValue.setText(Constants.DEFAULT_BUCKET); + bucketValue.selectAll(); + + bucketLabels.add(bucketValue); + JTextField scaleFactor = new JTextField(); + scaleFactor.setColumns(20); + scaleFactor.setText("1"); + bucketScaleFactors.add(scaleFactor); + final JCheckBox selectedBucket = new JCheckBox(); + selectedBucket.setSelected(true); + selectionValues.add(selectedBucket); + Dimen dimen = new Dimen() + .setBucket(Constants.DEFAULT_BUCKET) + .setFactorDp(Constants.DEFAULT_SCALE_FACTOR) + .setFactorSp(Constants.DEFAULT_SCALE_FACTOR) + .setMandatory(false) + .setSelected(true); + data.add(dimen); + bucketValue.getDocument().addDocumentListener(new DocumentListener() { + @Override + public void insertUpdate(DocumentEvent e) { + dimen.setBucket(bucketValue.getText()); + + } + + @Override + public void removeUpdate(DocumentEvent e) { + dimen.setBucket(bucketValue.getText()); + } + + @Override + public void changedUpdate(DocumentEvent e) { + dimen.setBucket(bucketValue.getText()); + + } + }); + scaleFactor.getDocument().addDocumentListener(new DocumentListener() { + private void setData() { + float val = 0; + try { + val = Float.parseFloat(scaleFactor.getText()); + } catch (NullPointerException | NumberFormatException ex) { + + } + if (isDp) { + dimen.setFactorDp(val); + + } else { + dimen.setFactorSp(val); + } + } + + @Override + public void insertUpdate(DocumentEvent e) { + setData(); + + } + + @Override + public void removeUpdate(DocumentEvent e) { + setData(); + } + + @Override + public void changedUpdate(DocumentEvent e) { + setData(); + } + }); + JButton removeButton = new JButton("-"); + removeButton.addActionListener(new AbstractAction() { + @Override + public void actionPerformed(ActionEvent e) { + removeLayoutValues(dimen, bucketValue, scaleFactor, selectedBucket, removeButton); + setLayoutConstraints(); + layout.invalidateLayout(controlPanel); + } + }); + removeButtons.add(removeButton); + setLayoutConstraints(); + layout.invalidateLayout(controlPanel); + } else { + showAlert(invalidIndex); + } + + } + + }; + return actionsAdd; + } + + private void removeLayoutValues(Dimen dimen, Component bucketValue, JTextField scaleFactor, JCheckBox selectedBucket, JButton removeButton) { + data.remove(dimen); + bucketValue.setVisible(false); + bucketLabels.remove(bucketValue); + scaleFactor.setVisible(false); + bucketScaleFactors.remove(scaleFactor); + selectedBucket.setVisible(false); + selectionValues.remove(selectedBucket); + removeButton.setVisible(false); + removeButtons.remove(removeButton); + } + + public int invalidBucketIndex() { + HashMap containedBuckets = new HashMap<>(); + if (data.size() > 9) { + return ERROR_CODE[1]; + } + for (Dimen dimen : data) { + if (containedBuckets.containsKey(dimen.getBucket())) { + return ERROR_CODE[0]; + } + containedBuckets.put(dimen.getBucket(), true); + } + + return 0; + } + + + public void showAlert(int option) { + JOptionPane optionPane = new JOptionPane(ERROR_MESSAGES[option - 1], JOptionPane.WARNING_MESSAGE); + JDialog dialog = optionPane.createDialog(Constants.ERROR_TITLE); + dialog.setAlwaysOnTop(true); + dialog.setVisible(true); + } + + @Nullable + @Override + protected JComponent createCenterPanel() { + return component; + } + + public ArrayList getConversionValues() { + return data; + } +} diff --git a/src/com/hr/dimenify/model/Dimen.java b/src/com/hr/dimenify/model/Dimen.java new file mode 100644 index 0000000..d844362 --- /dev/null +++ b/src/com/hr/dimenify/model/Dimen.java @@ -0,0 +1,67 @@ +package com.hr.dimenify.model; + +public class Dimen { + + private String bucket; + private String directory; + private float factorSp; + private float factorDp; + private boolean isSelected; + private boolean isMandatory; + + public String getBucket() { + return bucket; + } + + public Dimen setBucket(String bucket) { + this.bucket = bucket; + if (this.bucket.equals("mdpi")) { + this.directory = "values"; + } else { + this.directory = "values-" + bucket; + } + return this; + } + + public float getFactorSp() { + return factorSp; + } + + public Dimen setFactorSp(float factorSp) { + this.factorSp = factorSp; + return this; + } + + public boolean isSelected() { + return isSelected; + } + + public Dimen setSelected(boolean selected) { + isSelected = selected; + return this; + } + + public boolean isMandatory() { + return isMandatory; + } + + public Dimen setMandatory(boolean mandatory) { + isMandatory = mandatory; + return this; + } + + public float getFactorDp() { + return factorDp; + } + + public Dimen setFactorDp(float factorDp) { + this.factorDp = factorDp; + return this; + } + + + public String getDirectory() { + return directory; + } + +} diff --git a/src/com/hr/dimenify/util/Constants.java b/src/com/hr/dimenify/util/Constants.java new file mode 100644 index 0000000..ea7ce35 --- /dev/null +++ b/src/com/hr/dimenify/util/Constants.java @@ -0,0 +1,32 @@ +package com.hr.dimenify.util; + + +public interface Constants { + String SAVE_PREFIX = "com.hr.dimenify."; + String MIGRATION_FLAG = "v2Tov3"; + String SAVE_PREFIX_V2 = "com.hr.dimenify.v2"; + String INIT_MODEL_JSON = "[{\"bucket\":\"mdpi\",\"directory\":\"values\",\"factorSp\":1,\"factorDp\":1,\"isSelected\":true,\"isMandatory\":true},{\"bucket\":\"hdpi\",\"factorSp\":1.2,\"factorDp\":1.2,\"directory\":\"values-hdpi\",\"isSelected\":true,\"isMandatory\":true},{\"bucket\":\"xhdpi\",\"factorSp\":1.8,\"factorDp\":1.8,\"directory\":\"values-xhdpi\",\"isSelected\":true,\"isMandatory\":true},{\"bucket\":\"xxhdpi\",\"factorSp\":2.4,\"factorDp\":2.4,\"directory\":\"values-xxhdpi\",\"isSelected\":true,\"isMandatory\":true},{\"bucket\":\"xxxhdpi\",\"factorSp\":3,\"factorDp\":3,\"directory\":\"values-xxxhdpi\",\"isSelected\":true,\"isMandatory\":true}]"; + String RESOURCES_TEXT = "\n"; + String RESOURCES_TAG = "resources"; + String DIMEN_TAG = "dimen"; + String NAME_TAG = "name"; + String PLACEHOLDER_DIMEN = "{1}{2}"; + String FILE_NAME = "dimens.xml"; + String VALUES_PREFIX = "values-"; + String DP = "dp"; + String SP = "sp"; + String TITLE = "Set scale factors for "; + String ERROR_MESSAGES[] = { + "There are duplicate buckets please fix before adding more." + , "Custom buckets are restricted to 5" + , "Could not map the resource folder to a density value" + , "Could not convert the value into a number" + }; + int ERROR_CODE[] = {1, 2}; + String ERROR_TITLE = "Error"; + String SCALE_TEXT_PREFIX = "Please a scale value for "; + String NEW_BUCKET = "Add new bucket"; + String METRIC = " metric"; + String DEFAULT_BUCKET = "sw600dp-land"; + float DEFAULT_SCALE_FACTOR = 1.2f; +} diff --git a/src/com/hr/dimenify/util/ModelUtil.java b/src/com/hr/dimenify/util/ModelUtil.java new file mode 100644 index 0000000..b101afa --- /dev/null +++ b/src/com/hr/dimenify/util/ModelUtil.java @@ -0,0 +1,24 @@ +package com.hr.dimenify.util; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import com.hr.dimenify.model.Dimen; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by anvithbhat on 25/03/17. + */ +public class ModelUtil { + + static Gson gson = new Gson(); + + public static String toJson(List dimenList) { + return gson.toJson(dimenList); + } + public static ArrayList fromJson(String data) { + ArrayList dimenList = gson.fromJson(data, new TypeToken>(){}.getType()); + return dimenList; + } +}