From fc2b11dab82f7ebdc024bcf5c80eb2d2b9579358 Mon Sep 17 00:00:00 2001 From: liuzhihang Date: Fri, 4 Jun 2021 10:27:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=B3=A8=E9=87=8A=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../liuzhihang/toolkit/ui/ParamPreview.form | 21 +++++- .../toolkit/ui/ParamPreviewForm.java | 70 +++++++++++++++++-- .../liuzhihang/toolkit/ui/ToolkitForm.java | 2 +- .../toolkit/utils/SettingsHandlerUtils.java | 14 ++-- 4 files changed, 89 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/liuzhihang/toolkit/ui/ParamPreview.form b/src/main/java/com/liuzhihang/toolkit/ui/ParamPreview.form index 69c24aa..7bb80c8 100644 --- a/src/main/java/com/liuzhihang/toolkit/ui/ParamPreview.form +++ b/src/main/java/com/liuzhihang/toolkit/ui/ParamPreview.form @@ -1,6 +1,6 @@
- + @@ -13,7 +13,7 @@ - + @@ -21,12 +21,27 @@ - + + + + + + + + + + + + + + + + diff --git a/src/main/java/com/liuzhihang/toolkit/ui/ParamPreviewForm.java b/src/main/java/com/liuzhihang/toolkit/ui/ParamPreviewForm.java index e748b99..c78dd0e 100644 --- a/src/main/java/com/liuzhihang/toolkit/ui/ParamPreviewForm.java +++ b/src/main/java/com/liuzhihang/toolkit/ui/ParamPreviewForm.java @@ -31,6 +31,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Set; +import java.util.concurrent.atomic.AtomicBoolean; /** * @author liuzhihang @@ -41,6 +42,8 @@ public class ParamPreviewForm { @NonNls public static final String TOOLKIT_PREVIEW_POPUP = "com.intellij.toolkit.preview.popup"; + private final AtomicBoolean myIsPinned = new AtomicBoolean(false); + private final Project project; private final PsiFile psiFile; private final PsiClass psiClass; @@ -50,6 +53,8 @@ public class ParamPreviewForm { private JPanel rootPanel; private JPanel tailToolbarPanel; + private JPanel headToolbarPanel; + private JLabel fileReference; private JScrollPane paramScrollPane; @@ -69,6 +74,8 @@ public ParamPreviewForm(@NotNull Project project, @NotNull PsiFile psiFile, this.psiElementFactory = JavaPsiFacade.getElementFactory(project); initUI(); + initTitle(); + initHeadToolbar(); addMouseListeners(); initParamData(); initTailRightToolbar(); @@ -81,11 +88,65 @@ private void initUI() { tailToolbarPanel.setBorder(JBUI.Borders.empty()); } + private void initTitle() { + + if (psiClass != null) { + fileReference.setText(psiClass.getQualifiedName()); + } else if (psiFile != null) { + fileReference.setText(psiFile.getName()); + } else { + fileReference.setText("Toolkit"); + } + } + + private void initHeadToolbar() { + DefaultActionGroup group = new DefaultActionGroup(); + + group.add(new AnAction("Setting", "Toolkit settings", AllIcons.General.GearPlain) { + @Override + public void actionPerformed(@NotNull AnActionEvent e) { + myIsPinned.set(true); + SettingsForm.getInstance(project).popup(); + } + }); + + group.addSeparator(); + + group.add(new ToggleAction("Pin", "Pin window", AllIcons.General.Pin_tab) { + + @Override + public boolean isDumbAware() { + return true; + } + + @Override + public boolean isSelected(@NotNull AnActionEvent e) { + return myIsPinned.get(); + } + + @Override + public void setSelected(@NotNull AnActionEvent e, boolean state) { + myIsPinned.set(state); + } + }); + + ActionToolbarImpl toolbar = (ActionToolbarImpl) ActionManager.getInstance() + .createActionToolbar("ToolkitHeadToolbar", group, true); + toolbar.setTargetComponent(headToolbarPanel); + + toolbar.setForceMinimumSize(true); + toolbar.setLayoutPolicy(ActionToolbar.NOWRAP_LAYOUT_POLICY); + Utils.setSmallerFontForChildren(toolbar); + + headToolbarPanel.add(toolbar.getComponent(), BorderLayout.EAST); + } + private void addMouseListeners() { WindowMoveListener windowMoveListener = new WindowMoveListener(rootPanel); rootPanel.addMouseListener(windowMoveListener); rootPanel.addMouseMotionListener(windowMoveListener); - + headToolbarPanel.addMouseListener(windowMoveListener); + headToolbarPanel.addMouseMotionListener(windowMoveListener); } @@ -143,9 +204,10 @@ public void actionPerformed(@NotNull AnActionEvent e) { javaCodeStyleManager.optimizeImports(psiClass.getContainingFile()); javaCodeStyleManager.shortenClassReferences(psiClass); - CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project); - codeStyleManager.reformat(psiClass); - codeStyleManager.reformat(psiClass, false); + // 格式化注释 + CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(psiClass.getProject()); + + codeStyleManager.reformatText(psiClass.getContainingFile(), 0, psiClass.getTextLength() + 1); }); popup.cancel(); diff --git a/src/main/java/com/liuzhihang/toolkit/ui/ToolkitForm.java b/src/main/java/com/liuzhihang/toolkit/ui/ToolkitForm.java index a63e14e..c61313a 100644 --- a/src/main/java/com/liuzhihang/toolkit/ui/ToolkitForm.java +++ b/src/main/java/com/liuzhihang/toolkit/ui/ToolkitForm.java @@ -122,7 +122,7 @@ private void addMouseListeners() { private void initHeadToolbar() { DefaultActionGroup group = new DefaultActionGroup(); - group.add(new AnAction("Setting", "Doc view settings", AllIcons.General.GearPlain) { + group.add(new AnAction("Setting", "Toolkit settings", AllIcons.General.GearPlain) { @Override public void actionPerformed(@NotNull AnActionEvent e) { // ShowSettingsUtil.getInstance().showSettingsDialog(e.getProject(), SettingsConfigurable.class); diff --git a/src/main/java/com/liuzhihang/toolkit/utils/SettingsHandlerUtils.java b/src/main/java/com/liuzhihang/toolkit/utils/SettingsHandlerUtils.java index 2d3998a..b47f8a0 100644 --- a/src/main/java/com/liuzhihang/toolkit/utils/SettingsHandlerUtils.java +++ b/src/main/java/com/liuzhihang/toolkit/utils/SettingsHandlerUtils.java @@ -92,20 +92,14 @@ public static void commentHandler(@NotNull Settings settings, @NotNull PsiElemen String comment = "/* \n" + "Create from Json string by IDEA plugin —— Toolkit\n\n" - + GsonFormatUtil.gsonFormat(new GsonBuilder().setPrettyPrinting().create(), jsonObject) - + " */"; + + GsonFormatUtil.gsonFormat(new GsonBuilder().serializeNulls().setPrettyPrinting().create(), jsonObject) + + "\n*/"; PsiField psiField = allFields[0]; - PsiComment commentFromText = psiElementFactory.createCommentFromText(comment, null); + PsiComment commentFromText = psiElementFactory.createCommentFromText(comment, psiField); - psiField.addBefore(commentFromText, psiField.getFirstChild()); - - // // 格式化注释 - // CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(psiClass.getProject()); - // int startOffset = commentFromText.getTextOffset(); - // int endOffset = commentFromText.getTextOffset() + commentFromText.getText().length(); - // codeStyleManager.reformatText(psiClass.getContainingFile(), startOffset, endOffset + 1); + psiField.addBefore(commentFromText, psiField); } catch (Exception ignored) { }