Skip to content

Commit 9f61c19

Browse files
committed
refactor toolbar
1 parent 4f49680 commit 9f61c19

File tree

11 files changed

+288
-139
lines changed

11 files changed

+288
-139
lines changed

build.gradle

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ buildscript {
66
}
77

88
plugins {
9-
// id 'java'
10-
// id 'idea'
119
id 'org.jetbrains.intellij' version '0.4.15'
1210
}
1311

@@ -16,10 +14,6 @@ version "${version}"
1614

1715
sourceCompatibility = JavaVersion.VERSION_1_8
1816

19-
//apply plugin: 'java'
20-
//apply plugin: 'idea'
21-
//apply plugin: 'org.jetbrains.intellij'
22-
2317
repositories {
2418
mavenCentral()
2519
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.chuntung.plugin.gistsnippet.action;
2+
3+
import com.intellij.openapi.actionSystem.AnAction;
4+
import com.intellij.openapi.actionSystem.AnActionEvent;
5+
import com.intellij.openapi.actionSystem.DefaultActionGroup;
6+
import com.intellij.openapi.actionSystem.Presentation;
7+
import com.intellij.openapi.actionSystem.ex.ComboBoxAction;
8+
import com.intellij.openapi.project.DumbAware;
9+
import org.jetbrains.annotations.NotNull;
10+
11+
import javax.swing.*;
12+
13+
/**
14+
* ComboBox Action to be used in toolbar.
15+
*/
16+
public class CustomComboBoxAction extends ComboBoxAction implements DumbAware {
17+
private AnAction[] actions;
18+
private String myText;
19+
private Icon myIcon;
20+
21+
public static CustomComboBoxAction create(AnAction... actions) {
22+
return new CustomComboBoxAction(actions);
23+
}
24+
25+
public CustomComboBoxAction(AnAction... actions) {
26+
this.actions = actions;
27+
// set first action
28+
if (actions != null && actions.length > 0) {
29+
reset();
30+
}
31+
}
32+
33+
public void update(@NotNull AnActionEvent e) {
34+
if (e.getPresentation() != null) {
35+
e.getPresentation().setText(myText);
36+
e.getPresentation().setIcon(myIcon);
37+
}
38+
}
39+
40+
public String getText() {
41+
return myText;
42+
}
43+
44+
public void reset() {
45+
Presentation first = actions[0].getTemplatePresentation();
46+
myText = first.getText();
47+
myIcon = first.getIcon();
48+
}
49+
50+
@NotNull
51+
@Override
52+
protected DefaultActionGroup createPopupActionGroup(JComponent button) {
53+
DefaultActionGroup group = new DefaultActionGroup();
54+
for (AnAction action : actions) {
55+
group.add(new DelegatedAction(action));
56+
}
57+
return group;
58+
}
59+
60+
private class DelegatedAction extends AnAction {
61+
private AnAction target;
62+
63+
DelegatedAction(AnAction target) {
64+
this.target = target;
65+
getTemplatePresentation().setText(target.getTemplatePresentation().getText());
66+
getTemplatePresentation().setIcon(target.getTemplatePresentation().getIcon());
67+
}
68+
69+
@Override
70+
public void actionPerformed(@NotNull AnActionEvent e) {
71+
target.actionPerformed(e);
72+
// update combox text and icon
73+
myIcon = e.getPresentation().getIcon();
74+
myText = e.getPresentation().getText();
75+
}
76+
}
77+
}

src/main/java/com/chuntung/plugin/gistsnippet/action/InsertAction.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
import com.intellij.openapi.util.IconLoader;
1717
import org.jetbrains.annotations.NotNull;
1818

19+
/**
20+
* Open dialog to select gist to insert into editor.
21+
*/
1922
public class InsertAction extends AnAction implements DumbAware {
2023
public InsertAction() {
2124
super(IconLoader.getIcon("/images/gist.png"));
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.chuntung.plugin.gistsnippet.action;
2+
3+
import com.chuntung.plugin.gistsnippet.dto.SnippetNodeDTO;
4+
import com.chuntung.plugin.gistsnippet.dto.api.GistFileDTO;
5+
import com.intellij.ide.BrowserUtil;
6+
import com.intellij.openapi.actionSystem.AnAction;
7+
import com.intellij.openapi.actionSystem.AnActionEvent;
8+
import com.intellij.openapi.project.DumbAwareAction;
9+
import org.jetbrains.annotations.NotNull;
10+
11+
import javax.swing.*;
12+
import javax.swing.tree.DefaultMutableTreeNode;
13+
14+
/**
15+
* Open the first gist in the selection in browser.
16+
*/
17+
public class OpenInBrowserAction extends DumbAwareAction {
18+
private JTree tree;
19+
20+
public OpenInBrowserAction(JTree tree) {
21+
super("Open in browser");
22+
this.tree = tree;
23+
}
24+
25+
@Override
26+
public void actionPerformed(@NotNull AnActionEvent e) {
27+
// Note, this will use the first item in the selection.
28+
Object selected = tree.getLastSelectedPathComponent();
29+
Object userObject = ((DefaultMutableTreeNode) selected).getUserObject();
30+
String url = null;
31+
if (userObject instanceof SnippetNodeDTO) {
32+
url = ((SnippetNodeDTO) userObject).getHtmlUrl();
33+
} else if (userObject instanceof GistFileDTO) {
34+
url = ((GistFileDTO) userObject).getRawUrl();
35+
}
36+
if (url != null) {
37+
BrowserUtil.open(url);
38+
}
39+
}
40+
}

src/main/java/com/chuntung/plugin/gistsnippet/dto/SnippetNodeDTO.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,9 @@ static SnippetNodeDTO of(GistDTO dto, ScopeEnum scope) {
221221
}
222222

223223
private static void parseDescription(GistDTO dto, SnippetNodeDTO node) {
224+
node.setTitle(null);
225+
node.setTags(null);
224226
if (dto.getDescription() == null || dto.getDescription().isEmpty()) {
225-
node.setTitle(null);
226-
node.setTags(null);
227227
// set description as first file name if empty
228228
for (GistFileDTO fileDTO : dto.getFiles().values()) {
229229
node.setDescription(fileDTO.getFilename());

src/main/java/com/chuntung/plugin/gistsnippet/dto/SnippetRootNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public SimpleNode[] getChildren() {
2424
return children == null ? NO_CHILDREN : children.toArray(NO_CHILDREN);
2525
}
2626

27-
public void setSetChildren(List<GistDTO> gistList, ScopeEnum scope) {
27+
public void resetChildren(List<GistDTO> gistList, ScopeEnum scope) {
2828
List<SnippetNodeDTO> children = new ArrayList<>(gistList.size());
2929
for (GistDTO gistDTO : gistList) {
3030
children.add(SnippetNodeDTO.of(gistDTO, scope));
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.chuntung.plugin.gistsnippet.service;
2+
3+
public class GistException extends RuntimeException {
4+
GistException(Exception e) {
5+
super(e);
6+
}
7+
}

src/main/java/com/chuntung/plugin/gistsnippet/service/GistSnippetService.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ public List<GistDTO> queryOwnGist(GithubAccount account, boolean forced) {
5050
result.set(gistList);
5151
return putIntoCache(gistList);
5252
} catch (IOException e) {
53-
logger.info("Failed to query own gist, error: " + e.getMessage());
54-
notifyWarn("Failed to load own Gist, " + e.getMessage(), null);
55-
return null;
53+
logger.info("Failed to query own gists, error: " + e.getMessage());
54+
throw new GistException(e);
5655
}
5756
});
5857

@@ -109,9 +108,8 @@ public List<GistDTO> queryStarredGist(GithubAccount account, boolean forced) {
109108
result.set(gistList);
110109
return putIntoCache(gistList);
111110
} catch (IOException e) {
112-
logger.info("Failed to query starred gist, error: " + e.getMessage());
113-
notifyWarn("Failed to load starred Gist, " + e.getMessage(), null);
114-
return null;
111+
logger.info("Failed to query starred gists, error: " + e.getMessage());
112+
throw new GistException(e);
115113
}
116114
});
117115

@@ -137,16 +135,8 @@ public GistDTO getGistDetail(GithubAccount account, String gistId, boolean force
137135
return executor.execute(request);
138136
} catch (IOException e) {
139137
logger.info("Failed to get gist detail, error: " + e.getMessage());
140-
notifyWarn("Failed to get Gist files, " + e.getMessage(), null);
141-
return null;
138+
throw new GistException(e);
142139
}
143140
});
144141
}
145-
146-
// TODO catch exception at invoker side??
147-
public void notifyWarn(String warn, Project project) {
148-
NotificationGroup notificationGroup = new NotificationGroup("GistSnippet.NotificationGroup", NotificationDisplayType.BALLOON, true);
149-
Notification notification = notificationGroup.createNotification(warn, NotificationType.WARNING);
150-
Notifications.Bus.notify(notification, project);
151-
}
152142
}

src/main/java/com/chuntung/plugin/gistsnippet/view/CustomDropDownLink.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import java.util.List;
2020
import java.util.function.Consumer;
2121

22+
/**
23+
* @deprecated use {@link com.chuntung.plugin.gistsnippet.action.CustomComboBoxAction} for toolbar
24+
*/
2225
class CustomDropDownLink extends JBComboBoxLabel {
2326
private final JLabel leftIcon = new JLabel("", null, SwingConstants.CENTER);
2427
private final List<String> items = new SmartList<>();

src/main/java/com/chuntung/plugin/gistsnippet/view/InsertGistDialog.form

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -36,56 +36,15 @@
3636
<properties/>
3737
<border type="none"/>
3838
<children>
39-
<grid id="775bc" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
40-
<margin top="0" left="0" bottom="0" right="0"/>
39+
<grid id="775bc" layout-manager="BorderLayout" hgap="0" vgap="0">
4140
<constraints>
4241
<splitpane position="left"/>
4342
</constraints>
4443
<properties/>
4544
<border type="none"/>
4645
<children>
47-
<grid id="4afde" layout-manager="GridLayoutManager" row-count="1" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
48-
<margin top="0" left="0" bottom="0" right="0"/>
49-
<constraints>
50-
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
51-
<preferred-size width="220" height="-1"/>
52-
</grid>
53-
</constraints>
54-
<properties/>
55-
<border type="none"/>
56-
<children>
57-
<component id="1111d" class="javax.swing.JLabel">
58-
<constraints>
59-
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
60-
</constraints>
61-
<properties>
62-
<horizontalAlignment value="4"/>
63-
<text value="Type:"/>
64-
</properties>
65-
</component>
66-
<component id="5e4c9" class="com.intellij.ui.components.labels.DropDownLink" binding="typeDropDownLink" custom-create="true">
67-
<constraints>
68-
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
69-
</constraints>
70-
<properties/>
71-
</component>
72-
<hspacer id="f655e">
73-
<constraints>
74-
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
75-
</constraints>
76-
</hspacer>
77-
<component id="f5101" class="com.intellij.ui.components.JBComboBoxLabel" binding="scopeDropDownLink" custom-create="true">
78-
<constraints>
79-
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
80-
</constraints>
81-
<properties/>
82-
</component>
83-
</children>
84-
</grid>
8546
<scrollpane id="7c571">
86-
<constraints>
87-
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
88-
</constraints>
47+
<constraints border-constraint="Center"/>
8948
<properties/>
9049
<border type="none"/>
9150
<children>

0 commit comments

Comments
 (0)