Skip to content

Commit 4b282a4

Browse files
committed
fix: added a SnykIcon class for all imagedescriptors and added collaps/expand treeviewer.
1 parent cb1c8dc commit 4b282a4

23 files changed

Lines changed: 204 additions & 118 deletions

plugin/plugin.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
</command>
7272
<command
7373
defaultHandler="io.snyk.eclipse.plugin.views.snyktoolview.handlers.ClearCacheHandler"
74-
id="io.snyk.eclipse.plugin.commands.snykEmptyScanResults"
74+
id="io.snyk.eclipse.plugin.commands.clearCacheHandler"
7575
name="%command.clearCache">
7676
</command>
7777
<command
@@ -228,7 +228,7 @@
228228
tooltip="%tooltip.expandAll">
229229
</command>
230230
<command
231-
commandId="io.snyk.eclipse.plugin.commands.snykEmptyScanResults"
231+
commandId="io.snyk.eclipse.plugin.commands.clearCacheHandler"
232232
icon="platform:/plugin/org.eclipse.ui.console/icons/full/clcl16/clear_co.png"
233233
style="push"
234234
tooltip="%tooltip.emptyScanResults">
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.snyk.eclipse.plugin.utils;
2+
3+
import org.eclipse.jface.resource.ImageDescriptor;
4+
5+
import io.snyk.eclipse.plugin.Activator;
6+
7+
public class ActivatorImageDescriptorProvider implements ImageDescriptorProvider {
8+
9+
@Override
10+
public ImageDescriptor getImageDescriptor(String path) {
11+
return Activator.getImageDescriptor(path);
12+
}
13+
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.snyk.eclipse.plugin.utils;
2+
3+
import org.eclipse.jface.resource.ImageDescriptor;
4+
5+
public interface ImageDescriptorProvider {
6+
ImageDescriptor getImageDescriptor(String path);
7+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package io.snyk.eclipse.plugin.utils;
2+
3+
import org.eclipse.jface.resource.ImageDescriptor;
4+
5+
public class SnykIcons {
6+
private static ImageDescriptorProvider imageProvider;
7+
8+
static {
9+
imageProvider = new ActivatorImageDescriptorProvider();
10+
}
11+
12+
public static final ImageDescriptor CODE = imageProvider.getImageDescriptor("/icons/code.png");
13+
public static final ImageDescriptor CODE_DISABLED = imageProvider.getImageDescriptor("/icons/code_disabled.png");
14+
15+
public static final ImageDescriptor OSS = imageProvider.getImageDescriptor("/icons/oss.png");
16+
public static final ImageDescriptor OSS_DISABLED = imageProvider.getImageDescriptor("/icons/oss_disabled.png");
17+
18+
public static final ImageDescriptor IAC = imageProvider.getImageDescriptor("/icons/iac.png");
19+
public static final ImageDescriptor IAC_DISABLED = imageProvider.getImageDescriptor("/icons/iac_disabled.png");
20+
21+
public static final ImageDescriptor SEVERITY_CRITICAL = imageProvider.getImageDescriptor("/icons/severity-critical.png");
22+
public static final ImageDescriptor SEVERITY_HIGH = imageProvider.getImageDescriptor("/icons/severity-high.png");
23+
public static final ImageDescriptor SEVERITY_MEDIUM = imageProvider.getImageDescriptor("/icons/severity-medium.png");
24+
public static final ImageDescriptor SEVERITY_LOW = imageProvider.getImageDescriptor("/icons/severity-low.png");
25+
26+
public static final ImageDescriptor ENABLED = imageProvider.getImageDescriptor("/icons/enabled.png");
27+
28+
// You can add a method to set a custom ImageDescriptorProvider if needed
29+
public static void setImageDescriptorProvider(ImageDescriptorProvider provider) {
30+
imageProvider = provider;
31+
}
32+
}

plugin/src/main/java/io/snyk/eclipse/plugin/views/snyktoolview/BaseTreeNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public ImageDescriptor getImageDescriptor() {
5050
public String getText() {
5151
return this.text;
5252
}
53-
53+
5454
public void setText(String text) {
5555
this.text = text;
5656
}
57-
57+
5858
@Override
5959
public String toString() {
6060
return this.value.toString();

plugin/src/main/java/io/snyk/eclipse/plugin/views/snyktoolview/ISnykToolView.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.snyk.eclipse.plugin.views.snyktoolview;
22

33
import org.eclipse.jface.resource.ImageDescriptor;
4+
import org.eclipse.jface.viewers.TreeViewer;
45

56
/**
67
* This interface captures the externally used methods with the tool window.
@@ -12,7 +13,7 @@ public interface ISnykToolView {
1213
String NO_FIXABLE_ISSUES = "There are no issues automatically fixable.";
1314
String IGNORED_ISSUES_FILTERED_BUT_AVAILABLE = "Adjust your Issue View Options to see ignored issues.";
1415
String OPEN_ISSUES_FILTERED_BUT_AVAILABLE = "Adjust your Issue View Options to open issues.";
15-
16+
1617
String NODE_TEXT_SCANNING = "Scanning...";
1718
String NODE_TEXT_NO_ISSUES_FOUND = "No issues found";
1819
String NODE_TEXT_EROR = "An error occurred";
@@ -63,27 +64,35 @@ public interface ISnykToolView {
6364
* @return
6465
*/
6566
abstract BaseTreeNode getProductNode(String product);
66-
67+
6768
/**
6869
* Resets a product node
6970
*/
7071
abstract void resetNode(BaseTreeNode node);
7172

72-
7373
/**
7474
* Refreshes the tree display
7575
*/
7676
abstract void refreshTree();
77-
77+
7878
/**
7979
* Returns the tree root
8080
*
8181
* @return
8282
*/
8383
abstract BaseTreeNode getRoot();
8484

85+
/**
86+
* Clears all nodes in the tree
87+
*
88+
* @return
89+
*/
90+
abstract void clearTree();
91+
8592
static String getPlural(long count) {
8693
return count > 1 ? "s" : "";
8794
}
88-
95+
96+
abstract TreeViewer getTreeViewer();
97+
8998
}

plugin/src/main/java/io/snyk/eclipse/plugin/views/snyktoolview/ProductTreeNode.java

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,46 @@
11
package io.snyk.eclipse.plugin.views.snyktoolview;
22

3-
import org.eclipse.jface.resource.ImageDescriptor;
4-
5-
import io.snyk.eclipse.plugin.Activator;
63
import io.snyk.eclipse.plugin.domain.ProductConstants;
4+
import io.snyk.eclipse.plugin.utils.SnykIcons;
5+
6+
public class ProductTreeNode extends BaseTreeNode {
77

8-
public class ProductTreeNode extends BaseTreeNode {
9-
public static final ImageDescriptor OSS = Activator.getImageDescriptor("/icons/oss.png");
10-
public static final ImageDescriptor CODE = Activator.getImageDescriptor("/icons/code.png");
11-
public static final ImageDescriptor IAC = Activator.getImageDescriptor("/icons/iac.png");
12-
138
private String product;
149

1510
public ProductTreeNode(Object value) {
1611
super(value);
1712
this.setProduct(value.toString());
18-
13+
1914
switch (value.toString()) {
2015
case ProductConstants.DISPLAYED_OSS:
21-
setImageDescriptor(OSS);
16+
setImageDescriptor(SnykIcons.OSS);
2217
break;
2318
case ProductConstants.DISPLAYED_IAC:
24-
setImageDescriptor(IAC);
19+
setImageDescriptor(SnykIcons.IAC);
2520
break;
2621
case ProductConstants.DISPLAYED_CODE_QUALITY:
27-
setImageDescriptor(CODE);
22+
setImageDescriptor(SnykIcons.CODE);
2823
break;
2924
case ProductConstants.DISPLAYED_CODE_SECURITY:
30-
setImageDescriptor(CODE);
25+
setImageDescriptor(SnykIcons.CODE);
3126
break;
3227
}
3328
}
34-
35-
3629

3730
@Override
3831
public void setText(String text) {
3932
this.setValue(text);
4033
}
4134

42-
43-
4435
@Override
4536
public void setValue(Object value) {
46-
if (!(value instanceof String)) throw new IllegalArgumentException("value of product node must be a string");
37+
if (!(value instanceof String))
38+
throw new IllegalArgumentException("value of product node must be a string");
4739
var cleanedValue = removePrefix(value.toString());
48-
40+
4941
// we don't want to override the product text
5042
if (!cleanedValue.isBlank()) {
51-
cleanedValue = product + " - " + cleanedValue;
43+
cleanedValue = product + " - " + cleanedValue;
5244
} else {
5345
cleanedValue = product;
5446
}
@@ -68,14 +60,10 @@ public void reset() {
6860
this.setValue(product);
6961
}
7062

71-
72-
7363
public String getProduct() {
7464
return product;
7565
}
7666

77-
78-
7967
public void setProduct(String product) {
8068
this.product = product;
8169
}

plugin/src/main/java/io/snyk/eclipse/plugin/views/snyktoolview/SnykToolView.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import org.eclipse.core.runtime.Platform;
77
import org.eclipse.jface.action.Action;
88
import org.eclipse.jface.action.MenuManager;
9-
import org.eclipse.jface.preference.PreferenceDialog;
109
import org.eclipse.jface.resource.ImageDescriptor;
1110
import org.eclipse.jface.viewers.ISelectionChangedListener;
1211
import org.eclipse.jface.viewers.IStructuredSelection;
@@ -25,7 +24,6 @@
2524
import org.eclipse.swt.widgets.Shell;
2625
import org.eclipse.swt.widgets.Tree;
2726
import org.eclipse.ui.PlatformUI;
28-
import org.eclipse.ui.dialogs.PreferencesUtil;
2927
import org.eclipse.ui.part.ViewPart;
3028
import org.osgi.framework.Bundle;
3129

@@ -149,34 +147,34 @@ public void setFocus() {
149147
public void setNodeText(BaseTreeNode node, String text) {
150148
node.setText(text);
151149
Display.getDefault().asyncExec(() -> {
152-
this.treeViewer.refresh(node, true);
150+
this.treeViewer.refresh(node, true);
153151
});
154152
}
155153

156154
@Override
157155
public void setNodeIcon(ImageDescriptor icon) {
158156
// TODO Auto-generated method stub
159-
157+
160158
}
161159

162160
@Override
163161
public void addIssueNode(BaseTreeNode parent, BaseTreeNode toBeAdded) {
164162
// TODO Auto-generated method stub
165-
163+
166164
}
167165

168166
@Override
169167
public void addFileNode(BaseTreeNode parent, BaseTreeNode toBeAdded) {
170168
// TODO Auto-generated method stub
171-
169+
172170
}
173171

174172
@Override
175173
public void addInfoNode(BaseTreeNode parent, BaseTreeNode toBeAdded) {
176174
toBeAdded.setParent(parent);
177175
parent.addChild(toBeAdded);
178176
Display.getDefault().asyncExec(() -> {
179-
this.treeViewer.refresh(parent, true);
177+
this.treeViewer.refresh(parent, true);
180178
});
181179
}
182180

@@ -203,15 +201,38 @@ public BaseTreeNode getRoot() {
203201
@Override
204202
public void refreshTree() {
205203
Display.getDefault().asyncExec(() -> {
206-
this.treeViewer.refresh(true);
204+
this.treeViewer.refresh(true);
207205
});
208206
}
209207

210208
@Override
211209
public void resetNode(BaseTreeNode node) {
212210
node.reset();
213211
Display.getDefault().asyncExec(() -> {
214-
this.treeViewer.refresh(node, true);
212+
this.treeViewer.refresh(node, true);
213+
});
214+
}
215+
216+
public void clearTree() {
217+
clearRoot();
218+
Display.getDefault().asyncExec(() -> {
219+
if (this.treeViewer != null && !this.treeViewer.getTree().isDisposed()) {
220+
this.treeViewer.refresh();
221+
this.treeViewer.expandAll(); // Optional: if you want to expand all nodes after clearing
222+
}
215223
});
216224
}
225+
226+
private void clearRoot() {
227+
if (this.rootObject != null) {
228+
this.rootObject.removeChildren();
229+
this.rootObject.reset();
230+
}
231+
}
232+
233+
@Override
234+
public TreeViewer getTreeViewer() {
235+
return this.treeViewer;
236+
}
237+
217238
}

plugin/src/main/java/io/snyk/eclipse/plugin/views/snyktoolview/handlers/BaseHandler.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
public class BaseHandler extends AbstractHandler implements IElementUpdater {
1717

18-
// TODO should we replace the filter button with a filter applied button icon?
1918
protected ImageDescriptor iconEnabled = null;
2019
protected ImageDescriptor iconDisabled = null;
2120
protected String preferenceKey = null;
@@ -29,25 +28,16 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
2928
commandService.refreshElements(commandId, null);
3029
}
3130

31+
Preferences.getInstance().store(preferenceKey,
32+
Boolean.valueOf(!Preferences.getInstance().getBooleanPref(preferenceKey)).toString());
3233
return null;
3334
}
3435

3536
@Override
3637
public void updateElement(UIElement element, @SuppressWarnings("rawtypes") Map map) {
37-
38-
String preference = Preferences.getInstance().getPref(preferenceKey);
39-
boolean bool = Boolean.parseBoolean(preference);
40-
41-
// Toggle the value, if it was true, it should be set to false
42-
if (bool) {
43-
element.setIcon(iconDisabled);
44-
Preferences.getInstance().store(preferenceKey, "false");
45-
46-
} else {
47-
element.setIcon(iconEnabled);
48-
Preferences.getInstance().store(preferenceKey, "true");
49-
50-
}
51-
38+
boolean enabled = Preferences.getInstance().getBooleanPref(preferenceKey);
39+
ImageDescriptor icon = enabled ? iconEnabled : iconDisabled;
40+
element.setIcon(icon);
5241
}
42+
5343
}

0 commit comments

Comments
 (0)