Skip to content

Commit d3d556b

Browse files
author
emmanue1
committed
Add capability to search source code on maven.org
1 parent 6d3a04a commit d3d556b

File tree

14 files changed

+700
-18
lines changed

14 files changed

+700
-18
lines changed

api/src/main/java/org/jd/gui/api/API.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,14 @@ public interface API {
5050
Map<String, String> getPreferences();
5151

5252
Collection<Future<Indexes>> getCollectionOfFutureIndexes();
53+
54+
interface LoadSourceListener {
55+
void sourceLoaded(String source);
56+
}
57+
58+
String getSource(Container.Entry entry);
59+
60+
void loadSource(Container.Entry entry, LoadSourceListener listener);
61+
62+
File loadSourceFile(Container.Entry entry);
5363
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2008-2019 Emmanuel Dupuy.
3+
* This project is distributed under the GPLv3 license.
4+
* This is a Copyleft license that gives the user the right to use,
5+
* copy and modify the code freely for non-commercial purposes.
6+
*/
7+
8+
package org.jd.gui.spi;
9+
10+
import org.jd.gui.api.API;
11+
import org.jd.gui.api.model.Container;
12+
13+
import java.io.File;
14+
15+
public interface SourceLoader {
16+
String getSource(API api, Container.Entry entry);
17+
18+
String loadSource(API api, Container.Entry entry);
19+
20+
File loadSourceFile(API api, Container.Entry entry);
21+
}

app/src/main/java/org/jd/gui/controller/MainController.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.jd.gui.service.pastehandler.PasteHandlerService;
2222
import org.jd.gui.service.platform.PlatformService;
2323
import org.jd.gui.service.preferencespanel.PreferencesPanelService;
24+
import org.jd.gui.service.sourceloader.SourceLoaderService;
2425
import org.jd.gui.service.sourcesaver.SourceSaverService;
2526
import org.jd.gui.service.treenode.TreeNodeFactoryService;
2627
import org.jd.gui.service.type.TypeFactoryService;
@@ -64,6 +65,7 @@ public class MainController implements API {
6465
protected SaveAllSourcesController saveAllSourcesController;
6566
protected SelectLocationController selectLocationController;
6667
protected AboutController aboutController;
68+
protected SourceLoaderService sourceLoaderService;
6769

6870
protected History history = new History();
6971
protected JComponent currentPage = null;
@@ -147,6 +149,7 @@ public void show(List<File> files) {
147149
preferencesController = new PreferencesController(configuration, mainFrame, PreferencesPanelService.getInstance().getProviders());
148150
selectLocationController = new SelectLocationController(MainController.this, mainFrame);
149151
aboutController = new AboutController(mainFrame);
152+
sourceLoaderService = new SourceLoaderService();
150153
// Add listeners
151154
mainFrame.addComponentListener(new MainFrameListener(configuration));
152155
// Set drop files transfer handler
@@ -677,4 +680,25 @@ public int hashCode() {
677680

678681
return list;
679682
}
683+
684+
@Override
685+
public String getSource(Container.Entry entry) {
686+
return sourceLoaderService.getSource(this, entry);
687+
}
688+
689+
@Override
690+
public void loadSource(Container.Entry entry, LoadSourceListener listener) {
691+
executor.execute(() -> {
692+
String source = sourceLoaderService.loadSource(this, entry);
693+
694+
if ((source != null) && !source.isEmpty()) {
695+
listener.sourceLoaded(source);
696+
}
697+
});
698+
}
699+
700+
@Override
701+
public File loadSourceFile(Container.Entry entry) {
702+
return sourceLoaderService.getSourceFile(this, entry);
703+
}
680704
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2008-2019 Emmanuel Dupuy.
3+
* This project is distributed under the GPLv3 license.
4+
* This is a Copyleft license that gives the user the right to use,
5+
* copy and modify the code freely for non-commercial purposes.
6+
*/
7+
8+
package org.jd.gui.service.sourceloader;
9+
10+
import org.jd.gui.api.API;
11+
import org.jd.gui.api.model.Container;
12+
import org.jd.gui.service.extension.ExtensionService;
13+
import org.jd.gui.spi.SourceLoader;
14+
15+
import java.io.File;
16+
import java.util.Collection;
17+
18+
public class SourceLoaderService {
19+
protected static final SourceLoaderService SOURCE_LOADER_SERVICE = new SourceLoaderService();
20+
21+
public static SourceLoaderService getInstance() { return SOURCE_LOADER_SERVICE; }
22+
23+
protected Collection<SourceLoader> providers = ExtensionService.getInstance().load(SourceLoader.class);
24+
25+
public String getSource(API api, Container.Entry entry) {
26+
for (SourceLoader provider : providers) {
27+
String source = provider.getSource(api, entry);
28+
29+
if ((source != null) && !source.isEmpty()) {
30+
return source;
31+
}
32+
}
33+
34+
return null;
35+
}
36+
37+
public String loadSource(API api, Container.Entry entry) {
38+
for (SourceLoader provider : providers) {
39+
String source = provider.loadSource(api, entry);
40+
41+
if ((source != null) && !source.isEmpty()) {
42+
return source;
43+
}
44+
}
45+
46+
return null;
47+
}
48+
49+
public File getSourceFile(API api, Container.Entry entry) {
50+
for (SourceLoader provider : providers) {
51+
File file = provider.loadSourceFile(api, entry);
52+
53+
if (file != null) {
54+
return file;
55+
}
56+
}
57+
58+
return null;
59+
}
60+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
* Copyright (c) 2008-2019 Emmanuel Dupuy.
3+
* This project is distributed under the GPLv3 license.
4+
* This is a Copyleft license that gives the user the right to use,
5+
* copy and modify the code freely for non-commercial purposes.
6+
*/
7+
8+
package org.jd.gui.service.preferencespanel;
9+
10+
import org.jd.gui.spi.PreferencesPanel;
11+
12+
import javax.swing.*;
13+
import javax.swing.event.DocumentEvent;
14+
import javax.swing.event.DocumentListener;
15+
import java.awt.*;
16+
import java.awt.event.ActionEvent;
17+
import java.awt.event.ActionListener;
18+
import java.util.Map;
19+
import java.util.regex.Pattern;
20+
21+
public class MavenOrgSourceLoaderPreferencesProvider extends JPanel implements PreferencesPanel, DocumentListener, ActionListener {
22+
public static final String ACTIVATED = "MavenOrgSourceLoaderPreferencesProvider.activated";
23+
public static final String FILTERS = "MavenOrgSourceLoaderPreferencesProvider.filters";
24+
25+
public static final String DEFAULT_FILTERS_VALUE =
26+
"+org +com.google +com.ibm +com.jcraft +com.springsource +com.sun -com +java +javax +sun +sunw " +
27+
"+spring +springframework +springmodules +tomcat +maven";
28+
29+
protected static final Pattern CONTROL_PATTERN = Pattern.compile("([+-][a-zA-Z_0-9$_.]+(\\s+[+-][a-zA-Z_0-9$_.]+)*)?\\s*");
30+
31+
protected JCheckBox enableCheckBox;
32+
protected JTextArea filtersTextArea;
33+
protected JButton resetButton;
34+
protected Color errorBackgroundColor = Color.RED;
35+
protected Color defaultBackgroundColor;
36+
37+
protected PreferencesPanel.PreferencesPanelChangeListener listener;
38+
39+
public MavenOrgSourceLoaderPreferencesProvider() {
40+
super(new BorderLayout());
41+
42+
enableCheckBox = new JCheckBox("Search source code on maven.org for:");
43+
enableCheckBox.addActionListener(this);
44+
45+
filtersTextArea = new JTextArea();
46+
filtersTextArea.setFont(getFont());
47+
filtersTextArea.setLineWrap(true);
48+
filtersTextArea.getDocument().addDocumentListener(this);
49+
defaultBackgroundColor = filtersTextArea.getBackground();
50+
51+
JComponent spacer = new JComponent() {};
52+
JScrollPane scrollPane = new JScrollPane(filtersTextArea);
53+
54+
String osName = System.getProperty("os.name").toLowerCase();
55+
56+
if (osName.contains("windows")) {
57+
spacer.setPreferredSize(new Dimension(22, -1));
58+
scrollPane.setPreferredSize(new Dimension(-1, 50));
59+
} else if (osName.contains("mac os")) {
60+
spacer.setPreferredSize(new Dimension(28, -1));
61+
scrollPane.setPreferredSize(new Dimension(-1, 56));
62+
} else {
63+
spacer.setPreferredSize(new Dimension(22, -1));
64+
scrollPane.setPreferredSize(new Dimension(-1, 56));
65+
}
66+
67+
resetButton = new JButton("Reset");
68+
resetButton.addActionListener(this);
69+
70+
JPanel southPanel = new JPanel(new BorderLayout());
71+
southPanel.add(resetButton, BorderLayout.EAST);
72+
73+
add(enableCheckBox, BorderLayout.NORTH);
74+
add(spacer, BorderLayout.WEST);
75+
add(scrollPane, BorderLayout.CENTER);
76+
add(southPanel, BorderLayout.SOUTH);
77+
}
78+
79+
// --- PreferencesPanel --- //
80+
@Override public String getPreferencesGroupTitle() { return "Source loader"; }
81+
@Override public String getPreferencesPanelTitle() { return "maven.org"; }
82+
@Override public JComponent getPanel() { return this; }
83+
84+
@Override
85+
public void init(Color errorBackgroundColor) {
86+
this.errorBackgroundColor = errorBackgroundColor;
87+
}
88+
89+
@Override public boolean isActivated() { return true; }
90+
91+
@Override
92+
public void loadPreferences(Map<String, String> preferences) {
93+
boolean enabled = !"false".equals(preferences.get(ACTIVATED));
94+
95+
enableCheckBox.setSelected(enabled);
96+
filtersTextArea.setEnabled(enabled);
97+
resetButton.setEnabled(enabled);
98+
99+
String filters = preferences.get(FILTERS);
100+
101+
if ((filters == null) || filters.isEmpty()) {
102+
filtersTextArea.setText(DEFAULT_FILTERS_VALUE);
103+
}
104+
}
105+
106+
@Override
107+
public void savePreferences(Map<String, String> preferences) {
108+
preferences.put(ACTIVATED, Boolean.toString(enableCheckBox.isSelected()));
109+
preferences.put(FILTERS, filtersTextArea.getText().trim());
110+
}
111+
112+
@Override public boolean arePreferencesValid() {
113+
return CONTROL_PATTERN.matcher(filtersTextArea.getText()).matches();
114+
}
115+
116+
@Override public void addPreferencesChangeListener(PreferencesPanelChangeListener listener) {
117+
this.listener = listener;
118+
}
119+
120+
121+
// --- DocumentListener --- //
122+
@Override public void insertUpdate(DocumentEvent e) { onTextChange(); }
123+
@Override public void removeUpdate(DocumentEvent e) { onTextChange(); }
124+
@Override public void changedUpdate(DocumentEvent e) { onTextChange(); }
125+
126+
protected void onTextChange() {
127+
filtersTextArea.setBackground(arePreferencesValid() ? defaultBackgroundColor : errorBackgroundColor);
128+
129+
if (listener != null) {
130+
listener.preferencesPanelChanged(this);
131+
}
132+
}
133+
134+
// --- ActionListener --- //
135+
@Override
136+
public void actionPerformed(ActionEvent e) {
137+
if (e.getSource() == enableCheckBox) {
138+
boolean enabled = enableCheckBox.isSelected();
139+
filtersTextArea.setEnabled(enabled);
140+
resetButton.setEnabled(enabled);
141+
} else {
142+
// Reset button
143+
filtersTextArea.setText(DEFAULT_FILTERS_VALUE);
144+
filtersTextArea.requestFocus();
145+
}
146+
}
147+
}

0 commit comments

Comments
 (0)