├── README.md ├── imgs ├── screenshot.gif └── screenshot1.gif ├── resources └── META-INF │ └── plugin.xml └── src └── com └── royll └── varnamegodie ├── VarNameTranslate.java ├── dialog ├── SelectDialog.form ├── SelectDialog.java └── SelectTextCallBack.java ├── net ├── HttpRequest.java └── TranslateBean.java ├── settings ├── SettingsUI.form └── SettingsUI.java └── utils ├── GenerateCode.java ├── PropertiesUtil.java └── StringTypeUtil.java /README.md: -------------------------------------------------------------------------------- 1 | # varname-go-die 2 | ##### 一个小巧的Android Studio插件,主要有两个功能: 3 | 1. 在代码编辑区输入中文时,可以使用该插件联网翻译成英文并替换成指定变量名称格式。 4 | 2. 在代码编辑区输入英文时,可以使用该插件转换成指定变量名称格式 5 | 6 | ### 优点 7 | 1. 设置里可以根据自己的代码风格设置生成格式,写变量名更方便 8 | 2. 遇到变量名英文不会写不需要打开翻译软件查找,直接在编辑器中即可自动生成 9 | 10 | ### 效果图 11 | ![快速变量格式转换](https://github.com/lololiu/varname-go-die/raw/master/imgs/screenshot.gif) 12 | 13 | ![变量风格设置](https://github.com/lololiu/varname-go-die/raw/master/imgs/screenshot1.gif) 14 | 15 | ### 安装 16 | * Android Studio: 打开Settings → Plugins → Browse repositories 查找 varname-go-die进行安装 17 | 你也可以 18 | * [下载jar包](https://plugins.jetbrains.com/plugin/8479?pr=) 通过Settings → Plugins → Install plugin from disk进行安装 19 | 20 | ### 使用 21 | * 在编辑器中输入单词或中文,Ctrl+W选取,再按Shift+H(默认快捷键,可以在设置中修改)即可弹出选择对话框,也可以选择单词或词组后点击主菜单栏Edit → ChangeVar 22 | * 可以在Settings → Other Settings → VarNameGoDie 中设置自己想要列表弹出的变量格式 23 | 24 | ### 注意 25 | * 使用Shift+H做快捷键会拦截掉输入大写H的事件,建议在Settings → Keymap中搜索插件名改成自己顺手的快捷键 26 | 27 | 28 | -------------------------------------------------------------------------------- /imgs/screenshot.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lololiu/varname-go-die/a0a52cc28a0520544ac191eca93172e233e8d0ed/imgs/screenshot.gif -------------------------------------------------------------------------------- /imgs/screenshot1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lololiu/varname-go-die/a0a52cc28a0520544ac191eca93172e233e8d0ed/imgs/screenshot1.gif -------------------------------------------------------------------------------- /resources/META-INF/plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | Royll.ID 3 | varname-go-die 4 | 1.0 5 | Roy 6 | 7 | 9 | ]]> 10 | 11 | 1.0 (7/13/2016) 13 | 16 | ]]> 17 | 18 | 19 | 20 | 21 | 22 | 24 | 27 | com.intellij.modules.lang 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /src/com/royll/varnamegodie/VarNameTranslate.java: -------------------------------------------------------------------------------- 1 | package com.royll.varnamegodie; 2 | 3 | import com.google.gson.Gson; 4 | import com.intellij.openapi.actionSystem.AnAction; 5 | import com.intellij.openapi.actionSystem.AnActionEvent; 6 | import com.intellij.openapi.actionSystem.PlatformDataKeys; 7 | import com.intellij.openapi.command.WriteCommandAction; 8 | import com.intellij.openapi.editor.Document; 9 | import com.intellij.openapi.editor.Editor; 10 | import com.intellij.openapi.editor.SelectionModel; 11 | import com.intellij.openapi.project.Project; 12 | import com.intellij.openapi.ui.Messages; 13 | import com.royll.varnamegodie.dialog.SelectDialog; 14 | import com.royll.varnamegodie.dialog.SelectTextCallBack; 15 | import com.royll.varnamegodie.net.HttpRequest; 16 | import com.royll.varnamegodie.net.TranslateBean; 17 | import com.royll.varnamegodie.utils.StringTypeUtil; 18 | import org.apache.http.util.TextUtils; 19 | 20 | import java.io.UnsupportedEncodingException; 21 | import java.net.URLEncoder; 22 | 23 | /** 24 | * Created by Roy on 2016/7/11. 25 | */ 26 | public class VarNameTranslate extends AnAction { 27 | 28 | private Editor mEditor; 29 | private Project mProject; 30 | private String mUrl = "http://fanyi.youdao.com/openapi.do"; 31 | 32 | @Override 33 | public void actionPerformed(AnActionEvent e) { 34 | mEditor = e.getData(PlatformDataKeys.EDITOR); 35 | mProject = e.getData(PlatformDataKeys.PROJECT); 36 | String selectText = mEditor.getSelectionModel().getSelectedText(); 37 | if (!TextUtils.isEmpty(selectText)) { 38 | int type = StringTypeUtil.getStringType(selectText); 39 | switch (type) { 40 | case StringTypeUtil.ONLY_HAS_CHINESE: 41 | requestTranslate(selectText); 42 | break; 43 | case StringTypeUtil.ONLY_HAS_ENGLISH: 44 | showChooseDialog(selectText); 45 | break; 46 | case StringTypeUtil.BOTH_CH_ENG: 47 | Messages.showMessageDialog("不支持中英混合", "Information", Messages.getInformationIcon()); 48 | break; 49 | default: 50 | break; 51 | } 52 | } 53 | 54 | 55 | } 56 | 57 | /** 58 | * 中文请求翻译 59 | * 60 | * @param text 61 | */ 62 | private void requestTranslate(String text) { 63 | String params = null; 64 | try { 65 | params = "keyfrom=testtranlate1rrrr&key=1257547986&type=data&doctype=json&version=1.1&only=translate&q=" + URLEncoder.encode(text, "utf-8"); 66 | } catch (UnsupportedEncodingException e) { 67 | e.printStackTrace(); 68 | } 69 | String res = HttpRequest.sendGet(mUrl, params); 70 | Gson gson = new Gson(); 71 | TranslateBean mRes = gson.fromJson(res, TranslateBean.class); 72 | if (mRes.getErrorCode() == 0) { 73 | showChooseDialog(mRes.getTranslation().get(0)); 74 | } else { 75 | showErrorMsg(mRes.getErrorCode()); 76 | } 77 | } 78 | 79 | private void changeSelectText(String text) { 80 | Document document = mEditor.getDocument(); 81 | SelectionModel selectionModel = mEditor.getSelectionModel(); 82 | 83 | final int start = selectionModel.getSelectionStart(); 84 | final int end = selectionModel.getSelectionEnd(); 85 | Runnable runnable = new Runnable() { 86 | @Override 87 | public void run() { 88 | document.replaceString(start, end, text); 89 | } 90 | }; 91 | WriteCommandAction.runWriteCommandAction(mProject, runnable); 92 | selectionModel.removeSelection(); 93 | } 94 | 95 | private void showChooseDialog(String selectText) { 96 | SelectDialog dialog = new SelectDialog(selectText, new SelectTextCallBack() { 97 | @Override 98 | public void onSelectText(String text) { 99 | changeSelectText(text); 100 | } 101 | }); 102 | dialog.setVisible(true); 103 | 104 | } 105 | 106 | private void showErrorMsg(int errorcode) { 107 | String msg; 108 | switch (errorcode) { 109 | case 20: 110 | msg = "要翻译的文本过长"; 111 | break; 112 | case 30: 113 | msg = "无法进行有效的翻译"; 114 | break; 115 | case 40: 116 | msg = "不支持的语言类型"; 117 | break; 118 | case 50: 119 | msg = "无效的key"; 120 | break; 121 | case 60: 122 | msg = "无词典结果"; 123 | break; 124 | default: 125 | msg = "未知错误"; 126 | break; 127 | } 128 | Messages.showMessageDialog(msg, "Information", Messages.getInformationIcon()); 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /src/com/royll/varnamegodie/dialog/SelectDialog.form: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 |
32 | -------------------------------------------------------------------------------- /src/com/royll/varnamegodie/dialog/SelectDialog.java: -------------------------------------------------------------------------------- 1 | package com.royll.varnamegodie.dialog; 2 | 3 | import com.royll.varnamegodie.utils.GenerateCode; 4 | 5 | import javax.swing.*; 6 | import javax.swing.event.ListSelectionEvent; 7 | import javax.swing.event.ListSelectionListener; 8 | import java.awt.*; 9 | import java.awt.event.*; 10 | import java.util.Vector; 11 | 12 | public class SelectDialog extends JDialog { 13 | private JPanel contentPane; 14 | private JList list1; 15 | 16 | private String mSelectedText; 17 | private String mChooseText; 18 | private SelectTextCallBack mCallBack; 19 | 20 | public SelectDialog(String selecttext, SelectTextCallBack callBack) { 21 | mSelectedText = selecttext; 22 | mCallBack = callBack; 23 | setPreferredSize(new Dimension(600, 400)); 24 | setContentPane(contentPane); 25 | setModal(true); 26 | setLocationRelativeTo(null); 27 | Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize(); 28 | int Swing1x = 500; 29 | int Swing1y = 300; 30 | setBounds(screensize.width / 2 - Swing1x / 2, screensize.height / 2 - Swing1y / 2, Swing1x, Swing1y); 31 | Vector vector = GenerateCode.getGenerateCode(selecttext); 32 | list1.setListData(vector); 33 | list1.setSelectedIndex(0); 34 | mChooseText = (String) vector.get(0); 35 | 36 | list1.addListSelectionListener(new ListSelectionListener() { 37 | @Override 38 | public void valueChanged(ListSelectionEvent e) { 39 | mChooseText = "" + list1.getSelectedValue(); 40 | } 41 | }); 42 | 43 | setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); 44 | addWindowListener(new WindowAdapter() { 45 | public void windowClosing(WindowEvent e) { 46 | onCancel(); 47 | } 48 | }); 49 | /** 50 | * 增加键盘处理事件 51 | */ 52 | list1.addKeyListener(new KeyAdapter() { 53 | @Override 54 | public void keyPressed(KeyEvent e) { 55 | if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { 56 | onCancel(); 57 | } else if (e.getKeyCode() == KeyEvent.VK_ENTER) { 58 | onOK(); 59 | } 60 | } 61 | }); 62 | 63 | /** 64 | * 增加鼠标点击处理事件 65 | */ 66 | list1.addMouseListener(new MouseAdapter() { 67 | @Override 68 | public void mouseClicked(MouseEvent e) { 69 | super.mouseClicked(e); 70 | if (e.getClickCount() == 2) { 71 | onOK(); 72 | } 73 | } 74 | }); 75 | 76 | contentPane.registerKeyboardAction(new ActionListener() { 77 | public void actionPerformed(ActionEvent e) { 78 | onCancel(); 79 | } 80 | }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); 81 | } 82 | 83 | private void onOK() { 84 | if (mCallBack != null) { 85 | mCallBack.onSelectText(mChooseText); 86 | } 87 | dispose(); 88 | } 89 | 90 | private void onCancel() { 91 | dispose(); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/com/royll/varnamegodie/dialog/SelectTextCallBack.java: -------------------------------------------------------------------------------- 1 | package com.royll.varnamegodie.dialog; 2 | 3 | /** 4 | * Created by Roy on 2016/7/7. 5 | */ 6 | public interface SelectTextCallBack { 7 | void onSelectText(String text); 8 | } 9 | -------------------------------------------------------------------------------- /src/com/royll/varnamegodie/net/HttpRequest.java: -------------------------------------------------------------------------------- 1 | package com.royll.varnamegodie.net; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.InputStreamReader; 5 | import java.net.URL; 6 | import java.net.URLConnection; 7 | import java.util.List; 8 | import java.util.Map; 9 | 10 | /** 11 | * Created by Roy on 2016/7/11. 12 | */ 13 | public class HttpRequest { 14 | 15 | public static String sendGet(String url, String param) { 16 | String result = ""; 17 | BufferedReader in = null; 18 | try { 19 | String urlNameString = url + "?" + param; 20 | URL realUrl = new URL(urlNameString); 21 | // 打开和URL之间的连接 22 | URLConnection connection = realUrl.openConnection(); 23 | // 设置通用的请求属性 24 | connection.setRequestProperty("accept", "*/*"); 25 | connection.setRequestProperty("connection", "Keep-Alive"); 26 | connection.setRequestProperty("user-agent", 27 | "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); 28 | // 建立实际的连接 29 | connection.connect(); 30 | // 获取所有响应头字段 31 | Map> map = connection.getHeaderFields(); 32 | // 遍历所有的响应头字段 33 | for (String key : map.keySet()) { 34 | System.out.println(key + "--->" + map.get(key)); 35 | } 36 | // 定义 BufferedReader输入流来读取URL的响应 37 | in = new BufferedReader(new InputStreamReader( 38 | connection.getInputStream())); 39 | String line; 40 | while ((line = in.readLine()) != null) { 41 | result += line; 42 | } 43 | } catch (Exception e) { 44 | e.printStackTrace(); 45 | } 46 | // 使用finally块来关闭输入流 47 | finally { 48 | try { 49 | if (in != null) { 50 | in.close(); 51 | } 52 | } catch (Exception e2) { 53 | e2.printStackTrace(); 54 | } 55 | } 56 | return result; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/com/royll/varnamegodie/net/TranslateBean.java: -------------------------------------------------------------------------------- 1 | package com.royll.varnamegodie.net; 2 | 3 | import java.util.ArrayList; 4 | 5 | /** 6 | * Created by Roy on 2016/7/11. 7 | */ 8 | public class TranslateBean { 9 | private String query; 10 | private int errorCode; 11 | private ArrayList translation; 12 | 13 | public String getQuery() { 14 | return query; 15 | } 16 | 17 | public void setQuery(String query) { 18 | this.query = query; 19 | } 20 | 21 | public int getErrorCode() { 22 | return errorCode; 23 | } 24 | 25 | public void setErrorCode(int errorCode) { 26 | this.errorCode = errorCode; 27 | } 28 | 29 | public ArrayList getTranslation() { 30 | return translation; 31 | } 32 | 33 | public void setTranslation(ArrayList translation) { 34 | this.translation = translation; 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return "TranslateBean{" + 40 | "query='" + query + '\'' + 41 | ", errorCode=" + errorCode + 42 | ", translation=" + translation + 43 | '}'; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/com/royll/varnamegodie/settings/SettingsUI.form: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 |
79 | -------------------------------------------------------------------------------- /src/com/royll/varnamegodie/settings/SettingsUI.java: -------------------------------------------------------------------------------- 1 | package com.royll.varnamegodie.settings; 2 | 3 | import com.intellij.openapi.options.Configurable; 4 | import com.intellij.openapi.options.ConfigurationException; 5 | import com.intellij.openapi.ui.Messages; 6 | import com.royll.varnamegodie.utils.PropertiesUtil; 7 | import org.apache.http.util.TextUtils; 8 | import org.jetbrains.annotations.Nls; 9 | import org.jetbrains.annotations.Nullable; 10 | 11 | import javax.swing.*; 12 | import javax.swing.event.TableModelEvent; 13 | import javax.swing.event.TableModelListener; 14 | import javax.swing.table.DefaultTableModel; 15 | import java.awt.*; 16 | import java.awt.event.ActionEvent; 17 | import java.awt.event.ActionListener; 18 | import java.awt.event.MouseAdapter; 19 | import java.awt.event.MouseEvent; 20 | import java.util.ArrayList; 21 | 22 | /** 23 | * Created by Roy on 2016/7/7. 24 | */ 25 | public class SettingsUI implements Configurable { 26 | 27 | private JCheckBox mCheckBox; 28 | private JPanel mPanel; 29 | private JButton mAddButton; 30 | private JButton mDelButton; 31 | private JTable table1; 32 | private JScrollPane mScrollPane; 33 | private String[] mSettingArray; 34 | private String mOldValue = ""; 35 | private DefaultTableModel mDefaultTableMoadel; 36 | private boolean mCheckBoxState; 37 | 38 | @Nls 39 | @Override 40 | public String getDisplayName() { 41 | return "VarNameGoDie"; 42 | } 43 | 44 | @Nullable 45 | @Override 46 | public String getHelpTopic() { 47 | return null; 48 | } 49 | 50 | @Nullable 51 | @Override 52 | public JComponent createComponent() { 53 | reset(); 54 | mAddButton.addActionListener(new ActionListener() { 55 | @Override 56 | public void actionPerformed(ActionEvent e) { 57 | DefaultTableModel model = (DefaultTableModel) table1.getModel(); 58 | model.addRow(new Object[]{"hello", ""}); 59 | 60 | } 61 | }); 62 | mDelButton.addActionListener(new ActionListener() { 63 | @Override 64 | public void actionPerformed(ActionEvent e) { 65 | DefaultTableModel model = (DefaultTableModel) table1.getModel(); 66 | int totalRow = 0; 67 | for (int row : table1.getSelectedRows()) { 68 | model.removeRow(row - totalRow); 69 | totalRow++; 70 | } 71 | } 72 | }); 73 | return mPanel; 74 | } 75 | 76 | @Override 77 | public boolean isModified() { 78 | return true; 79 | } 80 | 81 | @Override 82 | public void apply() throws ConfigurationException { 83 | ArrayList list = new ArrayList<>(); 84 | for (int i = 0; i < table1.getModel().getRowCount(); i++) { 85 | String str = (String) table1.getModel().getValueAt(i, 1); 86 | if (!TextUtils.isEmpty(str)) { 87 | list.add(str); 88 | } 89 | } 90 | PropertiesUtil.saveProperties(list.toArray(new String[list.size()])); 91 | PropertiesUtil.saveCheckBoxSelectState(mCheckBox.isSelected()); 92 | } 93 | 94 | @Override 95 | public void reset() { 96 | mCheckBoxState = PropertiesUtil.getCheckBoxSelectState(); 97 | mCheckBox.setSelected(mCheckBoxState); 98 | setDataInTable(mDefaultTableMoadel); 99 | } 100 | 101 | @Override 102 | public void disposeUIResources() { 103 | 104 | } 105 | 106 | 107 | private void createUIComponents() { 108 | mSettingArray = PropertiesUtil.getProperties(); 109 | mCheckBoxState = PropertiesUtil.getCheckBoxSelectState(); 110 | mCheckBox = new JCheckBox("列表展示全大写,驼峰式,如(HELLO_WORLD_TEXT)"); 111 | mCheckBox.addActionListener(new ActionListener() { 112 | @Override 113 | public void actionPerformed(ActionEvent e) { 114 | 115 | } 116 | }); 117 | mCheckBox.setSelected(mCheckBoxState); 118 | mDefaultTableMoadel = new DefaultTableModel(); 119 | setDataInTable(mDefaultTableMoadel); 120 | table1 = new JTable(mDefaultTableMoadel) { 121 | public void tableChanged(TableModelEvent e) { 122 | super.tableChanged(e); 123 | repaint(); 124 | } 125 | 126 | @Override 127 | public boolean isCellEditable(int row, int column) { 128 | return column == 1 ? true : false; 129 | } 130 | 131 | 132 | }; 133 | table1.getModel().addTableModelListener(new TableModelListener() { 134 | @Override 135 | public void tableChanged(TableModelEvent e) { 136 | if (e.getType() == TableModelEvent.UPDATE) { 137 | if (e.getLastRow() == -1) { 138 | return; 139 | } 140 | String value = table1.getValueAt(e.getLastRow(), e.getColumn()).toString(); 141 | if (!value.contains("hello") && !value.contains("Hello") && !value.equals("")) { 142 | Messages.showMessageDialog("转换字符必须包含hello或Hello", "Information", Messages.getInformationIcon()); 143 | table1.setValueAt(mOldValue, e.getLastRow(), e.getColumn()); 144 | } 145 | for (int i = 0; i < table1.getModel().getRowCount(); i++) { 146 | if (table1.getValueAt(i, 1).toString().equals(value) && e.getLastRow() != i) { 147 | Messages.showMessageDialog("列表已包含该字符", "Information", Messages.getInformationIcon()); 148 | table1.setValueAt("", e.getLastRow(), e.getColumn()); 149 | } 150 | } 151 | } 152 | } 153 | }); 154 | table1.addMouseListener(new MouseAdapter() { 155 | 156 | public void mouseClicked(MouseEvent e) { 157 | //记录进入编辑状态前单元格得数据 158 | mOldValue = table1.getValueAt(table1.getSelectedRow(), table1.getSelectedColumn()).toString(); 159 | } 160 | 161 | }); 162 | table1.getTableHeader().setPreferredSize(new Dimension(table1.getTableHeader().getWidth(), 35)); 163 | table1.getColumnModel().getColumn(0).setPreferredWidth(35); 164 | table1.setRowHeight(25); 165 | 166 | } 167 | 168 | private void setDataInTable(DefaultTableModel dm) { 169 | if (mSettingArray == null) { 170 | return; 171 | } 172 | Object[][] object = new Object[mSettingArray.length][2]; 173 | for (int i = 0; i < mSettingArray.length; i++) { 174 | for (int j = 0; j < 2; j++) { 175 | switch (j) { 176 | case 0: 177 | object[i][j] = "hello"; 178 | break; 179 | case 1: 180 | object[i][j] = mSettingArray[i]; 181 | break; 182 | default: 183 | break; 184 | } 185 | } 186 | } 187 | dm.setDataVector(object, new Object[]{"Before", "After"}); 188 | } 189 | 190 | } 191 | -------------------------------------------------------------------------------- /src/com/royll/varnamegodie/utils/GenerateCode.java: -------------------------------------------------------------------------------- 1 | package com.royll.varnamegodie.utils; 2 | 3 | 4 | import java.util.Vector; 5 | import java.util.regex.Matcher; 6 | import java.util.regex.Pattern; 7 | 8 | /** 9 | * Created by Roy on 2016/7/7. 10 | */ 11 | public class GenerateCode { 12 | public static String[] settingArray; 13 | private static boolean isCheckBoxSelected; 14 | 15 | 16 | public static Vector getGenerateCode(String str) { 17 | settingArray = PropertiesUtil.getProperties(); 18 | isCheckBoxSelected = PropertiesUtil.getCheckBoxSelectState(); 19 | Vector vector = new Vector(); 20 | if (isCheckBoxSelected) { 21 | vector.add(genrateUpperCaseStr(str)); 22 | } 23 | for (int i = 0; i < settingArray.length; i++) { 24 | if (settingArray[i].contains("hello")) { 25 | vector.add(settingArray[i].replace("hello", str.toLowerCase())); 26 | } else if (settingArray[i].contains("Hello")) { 27 | vector.add(settingArray[i].replace("Hello", genrateFirstUpperCaseStr(str))); 28 | } 29 | } 30 | return vector; 31 | 32 | } 33 | 34 | public static String genrateUpperCaseStr(String str) { 35 | return replaceSpace(str).toUpperCase(); 36 | } 37 | 38 | public static String genrateFirstUpperCaseStr(String str) { 39 | StringBuilder sb = new StringBuilder(); 40 | String[] strings = replaceSpace(str).split("_"); 41 | for (String s : strings) { 42 | sb.append(upperCaseFirstWord(s)); 43 | } 44 | return sb.toString(); 45 | } 46 | 47 | /** 48 | * 将字符串中空格替换成下划线 49 | * 50 | * @param str 51 | * @return 52 | */ 53 | public static String replaceSpace(String str) { 54 | String regEx = "[' ']+"; // 一个或多个空格 55 | Pattern p = Pattern.compile(regEx); 56 | Matcher m = p.matcher(str.trim()); 57 | String newStr = m.replaceAll("_"); 58 | return newStr; 59 | } 60 | 61 | public static String upperCaseFirstWord(String str) { 62 | StringBuilder sb = new StringBuilder(str.toLowerCase()); 63 | sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); 64 | return sb.toString(); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/com/royll/varnamegodie/utils/PropertiesUtil.java: -------------------------------------------------------------------------------- 1 | package com.royll.varnamegodie.utils; 2 | 3 | import com.intellij.ide.util.PropertiesComponent; 4 | 5 | /** 6 | * Created by Roy on 2016/7/8. 7 | */ 8 | public class PropertiesUtil { 9 | 10 | private static final String PROPERYIES_NAME_LIST = "varnamesettings_list"; 11 | private static final String PROPERYIES_NAME_CHECKBOX = "varnamesettings_check"; 12 | 13 | /** 14 | * 获取默认参数列表 15 | * 16 | * @return 17 | */ 18 | public static String[] getDefaultProperties() { 19 | return new String[]{"hello", "mHello", "mHelloTextView", "mHelloImageView", "mHelloListView", "mHelloStr", "mHelloBtn", "mHelloView"}; 20 | } 21 | 22 | /** 23 | * 获取设置参数列表 24 | * 25 | * @return 26 | */ 27 | public static String[] getProperties() { 28 | if (PropertiesComponent.getInstance().isValueSet(PROPERYIES_NAME_LIST)) { 29 | return PropertiesComponent.getInstance().getValues(PROPERYIES_NAME_LIST); 30 | } else { 31 | return getDefaultProperties(); 32 | } 33 | } 34 | 35 | /** 36 | * 保存参数列表 37 | * 38 | * @param array 39 | */ 40 | public static void saveProperties(String[] array) { 41 | PropertiesComponent.getInstance().setValues(PROPERYIES_NAME_LIST, array); 42 | } 43 | 44 | /** 45 | * 保存复选框状态 46 | * 47 | * @param selected 48 | */ 49 | public static void saveCheckBoxSelectState(boolean selected) { 50 | PropertiesComponent.getInstance().setValue(PROPERYIES_NAME_CHECKBOX, selected); 51 | } 52 | 53 | /** 54 | * 获取复选框状态 55 | * 56 | * @return 57 | */ 58 | public static boolean getCheckBoxSelectState() { 59 | if (PropertiesComponent.getInstance().isValueSet(PROPERYIES_NAME_CHECKBOX)) { 60 | return Boolean.valueOf(PropertiesComponent.getInstance().getValue(PROPERYIES_NAME_CHECKBOX)); 61 | } else { 62 | return false; 63 | } 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/com/royll/varnamegodie/utils/StringTypeUtil.java: -------------------------------------------------------------------------------- 1 | package com.royll.varnamegodie.utils; 2 | 3 | import java.util.regex.Matcher; 4 | import java.util.regex.Pattern; 5 | 6 | /** 7 | * Created by Roy on 2016/7/6. 8 | */ 9 | public class StringTypeUtil { 10 | 11 | public static final int ONLY_HAS_CHINESE = 1; 12 | public static final int ONLY_HAS_ENGLISH = 2; 13 | public static final int BOTH_CH_ENG = 3; 14 | 15 | public static int getStringType(String str) { 16 | if (hasChinese(str) && hasEnglish(str)) 17 | return BOTH_CH_ENG; 18 | else if (hasChinese(str) && !hasEnglish(str)) 19 | return ONLY_HAS_CHINESE; 20 | else if (!hasChinese(str) && hasEnglish(str)) 21 | return ONLY_HAS_ENGLISH; 22 | else 23 | return -1; 24 | } 25 | 26 | //判断是不是英文字母 27 | public static boolean hasEnglish(String charaString) { 28 | return charaString.matches(".*\\p{Alpha}.*"); 29 | } 30 | 31 | //根据中文unicode范围判断u4e00 ~ u9fa5不全 32 | public static boolean hasChinese(String str) { 33 | String regEx1 = "[\\u4e00-\\u9fa5]+"; 34 | String regEx2 = "[\\uFF00-\\uFFEF]+"; 35 | String regEx3 = "[\\u2E80-\\u2EFF]+"; 36 | String regEx4 = "[\\u3000-\\u303F]+"; 37 | String regEx5 = "[\\u31C0-\\u31EF]+"; 38 | Pattern p1 = Pattern.compile(regEx1); 39 | Pattern p2 = Pattern.compile(regEx2); 40 | Pattern p3 = Pattern.compile(regEx3); 41 | Pattern p4 = Pattern.compile(regEx4); 42 | Pattern p5 = Pattern.compile(regEx5); 43 | Matcher m1 = p1.matcher(str); 44 | Matcher m2 = p2.matcher(str); 45 | Matcher m3 = p3.matcher(str); 46 | Matcher m4 = p4.matcher(str); 47 | Matcher m5 = p5.matcher(str); 48 | if (m1.find() || m2.find() || m3.find() || m4.find() || m5.find()) 49 | return true; 50 | else 51 | return false; 52 | } 53 | } 54 | --------------------------------------------------------------------------------