forked from java-decompiler/jd-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes java-decompiler#43, adds "Copy Qualified Name" contextual menu
- Loading branch information
Showing
41 changed files
with
550 additions
and
278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
api/src/main/java/jd/gui/spi/ContextualActionsFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (c) 2008-2015 Emmanuel Dupuy | ||
* This program is made available under the terms of the GPLv3 License. | ||
*/ | ||
|
||
package jd.gui.spi; | ||
|
||
import jd.gui.api.API; | ||
import jd.gui.api.model.Container; | ||
|
||
import javax.swing.*; | ||
import java.util.Collection; | ||
|
||
public interface ContextualActionsFactory { | ||
|
||
public static final String GROUP_NAME = "GroupNameKey"; | ||
|
||
/** | ||
* Build a collection of actions for 'entry' and 'fragment', grouped by GROUP_NAME and sorted by NAME. Null values | ||
* are added for separators. | ||
* | ||
* @param fragment @see jd.gui.api.feature.UriOpenable | ||
* @return a collection of actions | ||
*/ | ||
public Collection<Action> make(API api, Container.Entry entry, String fragment); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
app/src/main/groovy/jd/gui/service/actions/ContextualActionsFactoryService.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright (c) 2008-2015 Emmanuel Dupuy | ||
* This program is made available under the terms of the GPLv3 License. | ||
*/ | ||
|
||
package jd.gui.service.actions | ||
|
||
import groovy.transform.CompileStatic | ||
import jd.gui.api.API | ||
import jd.gui.api.model.Container | ||
import jd.gui.spi.ContextualActionsFactory | ||
|
||
import javax.swing.Action | ||
|
||
@Singleton(lazy = true) | ||
class ContextualActionsFactoryService { | ||
static final ActionNameComparator COMPARATOR = new ActionNameComparator() | ||
|
||
final List<ContextualActionsFactory> providers = ServiceLoader.load(ContextualActionsFactory).toList() | ||
|
||
@CompileStatic | ||
Collection<Action> get(API api, Container.Entry entry, String fragment) { | ||
Map<String, ArrayList<Action>> mapActions = [:].withDefault { [] } | ||
|
||
for (def provider : providers) { | ||
def actions = provider.make(api, entry, fragment) | ||
|
||
for (def action : actions) { | ||
mapActions.get(action.getValue(ContextualActionsFactory.GROUP_NAME)).add(action) | ||
} | ||
} | ||
|
||
if (mapActions) { | ||
def result = new ArrayList<Action>() | ||
|
||
// Sort by group names | ||
for (def groupName : mapActions.keySet().sort()) { | ||
if (! result.isEmpty()) { | ||
// Add 'null' to mark a separator | ||
result.add(null) | ||
} | ||
|
||
// Sort by names | ||
def actions = mapActions.get(groupName) | ||
Collections.sort(actions, COMPARATOR) | ||
result.addAll(actions) | ||
} | ||
|
||
return result | ||
} else { | ||
return Collections.emptyList() | ||
} | ||
} | ||
|
||
static class ActionNameComparator implements Comparator<Action> { | ||
|
||
int compare(Action a1, Action a2) { | ||
String n1 = a1.getValue(Action.NAME) ?: '' | ||
String n2 = a2.getValue(Action.NAME) ?: '' | ||
return n1.compareTo(n2) | ||
} | ||
|
||
boolean equals(Object other) { this == other } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...s/src/main/groovy/jd/gui/service/actions/CopyQualifiedNameContextualActionsFactory.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright (c) 2008-2015 Emmanuel Dupuy | ||
* This program is made available under the terms of the GPLv3 License. | ||
*/ | ||
|
||
package jd.gui.service.actions | ||
|
||
import com.sun.media.sound.InvalidFormatException | ||
import jd.gui.api.API | ||
import jd.gui.api.model.Container | ||
import jd.gui.spi.ContextualActionsFactory | ||
|
||
import javax.swing.AbstractAction | ||
import javax.swing.Action | ||
import javax.swing.ImageIcon | ||
import java.awt.Toolkit | ||
import java.awt.datatransfer.StringSelection | ||
import java.awt.event.ActionEvent | ||
|
||
class CopyQualifiedNameContextualActionsFactory implements ContextualActionsFactory { | ||
|
||
Collection<Action> make(API api, Container.Entry entry, String fragment) { | ||
return Collections.singletonList(new CopyQualifiedNameAction(api, entry, fragment)) | ||
} | ||
|
||
static class CopyQualifiedNameAction extends AbstractAction { | ||
static final ImageIcon ICON = new ImageIcon(CopyQualifiedNameAction.class.classLoader.getResource('images/cpyqual_menu.png')) | ||
|
||
protected API api | ||
protected Container.Entry entry | ||
protected String fragment | ||
|
||
CopyQualifiedNameAction(API api, Container.Entry entry, String fragment) { | ||
this.api = api | ||
this.entry = entry | ||
this.fragment = fragment | ||
|
||
putValue(GROUP_NAME, 'Edit > CutCopyPaste') | ||
putValue(NAME, 'Copy Qualified Name') | ||
putValue(SMALL_ICON, ICON) | ||
} | ||
|
||
void actionPerformed(ActionEvent e) { | ||
def type = api.getTypeFactory(entry)?.make(api, entry, fragment) | ||
|
||
if (type) { | ||
def sb = new StringBuffer(type.displayPackageName) | ||
int dashIndex = fragment.indexOf('-') | ||
|
||
if (sb.length() > 0) { | ||
sb.append('.') | ||
} | ||
|
||
sb.append(type.displayTypeName) | ||
|
||
if (dashIndex != -1) { | ||
int lastDashIndex = fragment.lastIndexOf('-') | ||
|
||
if (dashIndex == lastDashIndex) { | ||
// See jd.gui.api.feature.UriOpenable | ||
throw new InvalidFormatException('fragment: ' + fragment) | ||
} else { | ||
def name = fragment.substring(dashIndex+1, lastDashIndex) | ||
def descriptor = fragment.substring(lastDashIndex+1) | ||
|
||
if (descriptor.startsWith('(')) { | ||
for (def method : type.methods) { | ||
if (method.name.equals(name) && method.descriptor.equals(descriptor)) { | ||
sb.append('.').append(method.displayName) | ||
break | ||
} | ||
} | ||
} else { | ||
for (def field : type.fields) { | ||
if (field.name.equals(name) && field.descriptor.equals(descriptor)) { | ||
sb.append('.').append(field.displayName) | ||
break | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
Toolkit.defaultToolkit.systemClipboard.setContents(new StringSelection(sb.toString()), null) | ||
} else { | ||
// Copy path of entry | ||
def path = new File(entry.uri).absolutePath | ||
Toolkit.defaultToolkit.systemClipboard.setContents(new StringSelection(path), null) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.