Skip to content

Commit

Permalink
Fixed "Not properly disposed SWT resource" error message (issue #3)
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandoMagico committed Apr 25, 2021
1 parent d2f37ed commit c67d6cc
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,26 @@ of this software and associated documentation files(the "Software"), to deal
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
***********************************************************************************************************************/

package contextquickie2.plugin;

import org.eclipse.ui.menus.CommandContributionItem;
import org.eclipse.ui.menus.CommandContributionItemParameter;

public class ExplorerCommandContributionItem extends CommandContributionItem {

public class DisposableCommandContributionItem extends CommandContributionItem
{
private EclipseExplorerContextMenuEntry currentEntry;

public ExplorerCommandContributionItem(EclipseExplorerContextMenuEntry entry, CommandContributionItemParameter contributionParameters) {
public DisposableCommandContributionItem(
EclipseExplorerContextMenuEntry entry, CommandContributionItemParameter contributionParameters)
{
super(contributionParameters);
this.currentEntry = entry;
}

@Override
public void dispose() {
public void dispose()
{
super.dispose();
if (this.currentEntry != null)
{
Expand Down
49 changes: 49 additions & 0 deletions Plugin/src/contextquickie2/plugin/DisposableMenuManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/***********************************************************************************************************************
MIT License
Copyright(c) 2020 Roland Reinl
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and /or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
***********************************************************************************************************************/

package contextquickie2.plugin;

import org.eclipse.jface.action.MenuManager;

public class DisposableMenuManager extends MenuManager
{
private EclipseExplorerContextMenuEntry currentEntry;

public DisposableMenuManager(EclipseExplorerContextMenuEntry entry)
{
super(entry.getWrappedEntry().getText(), entry.getImageDescriptor(), null);
this.currentEntry = entry;
}

@Override
public void dispose()
{
super.dispose();
if (this.currentEntry != null)
{
this.currentEntry.dispose();
this.currentEntry = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,10 @@ public ExplorerContextMenuEntry getWrappedEntry()
{
return this.entry;
}

@Override
protected void finalize() throws Throwable
{
this.dispose();
}

/**
* Releases all unmanaged resources (images) which are used by this instance.
*/
public void dispose()
{
if ((this.image != null) && (this.image.isDisposed() == false))
Expand Down
5 changes: 2 additions & 3 deletions Plugin/src/contextquickie2/plugin/MenuBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,8 @@ private IContributionItem createMenuEntry(EclipseExplorerContextMenuEntry entry,
}
else if (entry.getEntries().iterator().hasNext())
{
final MenuManager subMenu = new MenuManager(entry.getWrappedEntry().getText(), null, null);
final MenuManager subMenu = new DisposableMenuManager(entry);
Iterator<EclipseExplorerContextMenuEntry> iterator = entry.getEntries().iterator();
subMenu.setImageDescriptor(entry.getImageDescriptor());
while (iterator.hasNext())
{
subMenu.add(this.createMenuEntry(iterator.next(), selectedResources));
Expand All @@ -212,7 +211,7 @@ else if (entry.getEntries().iterator().hasNext())
commandParameter.label = entry.getWrappedEntry().getText();
commandParameter.tooltip = entry.getWrappedEntry().getHelpText();
commandParameter.icon = entry.getImageDescriptor();
result = new ExplorerCommandContributionItem(entry, commandParameter);
result = new DisposableCommandContributionItem(entry, commandParameter);
}

return result;
Expand Down

0 comments on commit c67d6cc

Please sign in to comment.