Skip to content

Commit 01cd4aa

Browse files
committed
2 parents aa6260e + 94850c0 commit 01cd4aa

File tree

8 files changed

+400
-477
lines changed

8 files changed

+400
-477
lines changed

src/ServiceStackIDEA/.idea/workspace.xml

Lines changed: 159 additions & 291 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ServiceStackIDEA/META-INF/plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
</action>
4949
<action id="UpdateServiceStackReference" class="UpdateServiceStackReference" text="Update ServiceStack Reference" icon="/icons/logo-16.png"
5050
description="Updates the selected ServiceStack reference.">
51-
<add-to-group group-id="ProjectViewPopupMenu"/>
51+
<add-to-group group-id="ProjectViewPopupMenu" anchor="last"/>
5252
</action>
5353
</actions>
5454
</idea-plugin>
1.06 KB
Binary file not shown.

src/ServiceStackIDEA/src/AddRef.form

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
44
<margin top="10" left="10" bottom="10" right="10"/>
55
<constraints>
6-
<xy x="48" y="54" width="650" height="350"/>
6+
<xy x="48" y="102" width="650" height="302"/>
77
</constraints>
88
<properties>
9-
<maximumSize width="550" height="220"/>
9+
<maximumSize width="550" height="320"/>
1010
<minimumSize width="400" height="220"/>
1111
<opaque value="true"/>
12-
<preferredSize width="550" height="300"/>
12+
<preferredSize width="550" height="220"/>
1313
<requestFocusEnabled value="true"/>
1414
</properties>
1515
<border type="none"/>
@@ -65,10 +65,25 @@
6565
</properties>
6666
<border type="none"/>
6767
<children>
68-
<component id="bc9e3" class="javax.swing.JTextPane" binding="infoTextPane">
68+
<component id="e88c4" class="javax.swing.JTextPane" binding="errorTextPane">
6969
<constraints>
70-
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false">
71-
<preferred-size width="150" height="50"/>
70+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="2" hsize-policy="2" anchor="0" fill="3" indent="0" use-parent-layout="false">
71+
<preferred-size width="150" height="25"/>
72+
</grid>
73+
</constraints>
74+
<properties>
75+
<editable value="false"/>
76+
<focusCycleRoot value="false"/>
77+
<focusable value="true"/>
78+
<font name="Arial"/>
79+
<opaque value="false"/>
80+
<visible value="false"/>
81+
</properties>
82+
</component>
83+
<component id="4af0b" class="javax.swing.JTextPane" binding="infoTextPane">
84+
<constraints>
85+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="2" hsize-policy="2" anchor="0" fill="3" indent="0" use-parent-layout="false">
86+
<preferred-size width="150" height="25"/>
7287
</grid>
7388
</constraints>
7489
<properties>
@@ -81,31 +96,6 @@
8196
<text value="To generate the DTO Service Model types for a specific ServiceStack instance, enter the base URI for the remote ServiceStack server and click OK."/>
8297
</properties>
8398
</component>
84-
<grid id="76624" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
85-
<margin top="0" left="0" bottom="0" right="0"/>
86-
<constraints>
87-
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
88-
</constraints>
89-
<properties/>
90-
<border type="none"/>
91-
<children>
92-
<component id="e88c4" class="javax.swing.JTextPane" binding="errorTextPane">
93-
<constraints>
94-
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="2" hsize-policy="2" anchor="0" fill="3" indent="0" use-parent-layout="false">
95-
<preferred-size width="150" height="25"/>
96-
</grid>
97-
</constraints>
98-
<properties>
99-
<editable value="false"/>
100-
<focusCycleRoot value="false"/>
101-
<focusable value="true"/>
102-
<font name="Arial"/>
103-
<opaque value="false"/>
104-
<visible value="false"/>
105-
</properties>
106-
</component>
107-
</children>
108-
</grid>
10999
</children>
110100
</grid>
111101
<grid id="d865d" layout-manager="GridLayoutManager" row-count="3" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">

src/ServiceStackIDEA/src/AddRef.java

Lines changed: 87 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
1+
import com.google.gson.FieldNamingPolicy;
12
import com.google.gson.Gson;
3+
import com.google.gson.GsonBuilder;
24
import com.intellij.ide.util.PackageChooserDialog;
35
import com.intellij.openapi.editor.Document;
4-
import com.intellij.openapi.fileChooser.FileSystemTree;
56
import com.intellij.openapi.fileEditor.FileDocumentManager;
67
import com.intellij.openapi.fileEditor.FileEditorManager;
78
import com.intellij.openapi.module.Module;
89
import com.intellij.openapi.project.Project;
910
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
10-
import com.intellij.openapi.util.io.FileSystemUtil;
1111
import com.intellij.openapi.vfs.LocalFileSystem;
1212
import com.intellij.openapi.vfs.VirtualFile;
1313
import com.intellij.openapi.vfs.VirtualFileManager;
14+
import com.intellij.openapi.vfs.newvfs.impl.VirtualDirectoryImpl;
15+
import com.intellij.openapi.vfs.newvfs.impl.VirtualFileImpl;
16+
import com.intellij.psi.JavaPsiFacade;
17+
import com.intellij.psi.PsiDirectory;
18+
import com.intellij.psi.PsiManager;
19+
import com.intellij.psi.PsiPackage;
20+
import com.intellij.psi.impl.file.PsiDirectoryImpl;
21+
import com.intellij.psi.util.ClassUtil;
1422
import com.intellij.ui.JBColor;
23+
import com.sun.javafx.fxml.builder.URLBuilder;
1524
import org.apache.http.client.utils.URIBuilder;
1625
import org.jetbrains.annotations.NotNull;
1726
import org.jetbrains.annotations.Nullable;
1827

1928
import javax.swing.*;
2029
import javax.swing.event.DocumentEvent;
2130
import javax.swing.event.DocumentListener;
22-
import javax.swing.text.Style;
23-
import javax.swing.text.StyleConstants;
24-
import java.awt.*;
2531
import java.awt.event.*;
2632
import java.io.*;
2733
import java.net.MalformedURLException;
@@ -44,6 +50,8 @@ public class AddRef extends JDialog {
4450
private Module module;
4551

4652
private String errorMessage;
53+
private PsiPackage selectedPackage;
54+
private String selectedDirectory;
4755

4856
public AddRef(@NotNull Module module) {
4957
this.module = module;
@@ -185,6 +193,15 @@ public void actionPerformed(ActionEvent e) {
185193
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
186194
}
187195

196+
public void setSelectedPackage(@NotNull PsiPackage selectedPackage) {
197+
this.selectedPackage = selectedPackage;
198+
setPackageBrowseText(selectedPackage.getQualifiedName());
199+
}
200+
201+
public void setSelectedDirectory(@NotNull String selectedDirectory) {
202+
this.selectedDirectory = selectedDirectory;
203+
}
204+
188205
private void processOK() {
189206
buttonOK.setEnabled(false);
190207
buttonCancel.setEnabled(false);
@@ -195,9 +212,9 @@ private void processOK() {
195212
public void run() {
196213
try {
197214
onOK();
198-
} catch (IOException e1) {
215+
} catch (Exception e1) {
199216
e1.printStackTrace();
200-
errorMessage = errorMessage != null ? errorMessage : "An error occurred saving the file - " + e1.getMessage();
217+
errorMessage = errorMessage != null ? errorMessage : "An error occurred adding reference - " + e1.getMessage();
201218
}
202219
if (errorMessage != null) {
203220
errorTextPane.setVisible(true);
@@ -211,7 +228,7 @@ public void run() {
211228
SwingUtilities.invokeLater(r);
212229
}
213230

214-
public void setPackageBrowseText(String packageName) {
231+
private void setPackageBrowseText(String packageName) {
215232
packageBrowse.setText(packageName);
216233
}
217234

@@ -225,36 +242,53 @@ private ImageIcon createImageIcon(String path, String description) {
225242
}
226243
}
227244

228-
private void onOK() throws IOException {
245+
private void onOK() {
246+
String url;
247+
List<String> javaCodeLines = new ArrayList<>();
229248
try {
230-
if (!ValidateEndpoint()) {
231-
errorMessage = errorMessage != null ? errorMessage : "Invalid ServiceStack endpoint provided - " + addressUrlTextField.getText();
249+
URIBuilder urlBuilder = createUrl(addressUrlTextField.getText());
250+
urlBuilder.addParameter("Package", packageBrowse.getText());
251+
String name = getDtoNameWithoutExtention().replaceAll("\\.", "_");
252+
urlBuilder.addParameter("GlobalNamespace", name);
253+
url = urlBuilder.build().toString();
254+
255+
URL serviceUrl = new URL(url);
256+
URLConnection javaResponseConnection = serviceUrl.openConnection();
257+
BufferedReader javaResponseReader = new BufferedReader(
258+
new InputStreamReader(
259+
javaResponseConnection.getInputStream()));
260+
String metadataInputLine;
261+
262+
while ((metadataInputLine = javaResponseReader.readLine()) != null)
263+
javaCodeLines.add(metadataInputLine);
264+
265+
javaResponseReader.close();
266+
267+
if(!javaCodeLines.get(0).startsWith("/* Options:")) {
268+
//Invalid endpoint
269+
errorMessage = "The address url is not a valid ServiceStack endpoint.";
232270
return;
233271
}
234-
} catch (Exception exception) {
235-
errorMessage = errorMessage != null ? errorMessage : "Invalid ServiceStack endpoint provided - " + addressUrlTextField.getText();
272+
273+
} catch (URISyntaxException | MalformedURLException e) {
274+
e.printStackTrace();
275+
errorMessage = e.getClass().getName() + " - Invalid ServiceStack endpoint provided - " + addressUrlTextField.getText();
276+
return;
277+
} catch (IOException e) {
278+
e.printStackTrace();
279+
errorMessage = e.getClass().getName() + " - Failed to read response - " + addressUrlTextField.getText();
236280
return;
237281
}
238282

283+
239284
GradleBuildFileHelper gradleBuildFileHelper = new GradleBuildFileHelper(this.module);
240285
boolean showDto = false;
241286
if(gradleBuildFileHelper.addDependency("net.servicestack", "android", "0.0.1")) {
242287
refreshBuildFile();
243288
} else {
244289
showDto = true;
245290
}
246-
String url = createUrl(addressUrlTextField.getText());
247-
URL serviceUrl = new URL(url);
248-
URLConnection javaResponseConnection = serviceUrl.openConnection();
249-
BufferedReader javaResponseReader = new BufferedReader(
250-
new InputStreamReader(
251-
javaResponseConnection.getInputStream()));
252-
String metadataInputLine;
253-
List<String> javaCodeLines = new ArrayList<>();
254-
while ((metadataInputLine = javaResponseReader.readLine()) != null)
255-
javaCodeLines.add(metadataInputLine);
256291

257-
javaResponseReader.close();
258292
String dtoPath;
259293
try {
260294
dtoPath = getDtoPath();
@@ -299,54 +333,28 @@ private String getDtoPath() throws FileNotFoundException {
299333
if(moduleFile == null) {
300334
throw new FileNotFoundException("Module file not found. Unable to add DTO to project.");
301335
}
302-
String moduleSourcePath = null;
303-
if(moduleFile.getParent() == null) {
304-
moduleSourcePath = moduleFile.getPath() + "/main/java";
305-
} else {
306-
moduleSourcePath = moduleFile.getParent().getPath() + "/src/main/java";
307-
}
308-
309-
String packagePath = packageBrowse.getText().replace(".", "/");
310-
File assumedPackageDirectory = new File(moduleSourcePath + "/" + packagePath);
311336
String fullDtoPath;
312-
if (assumedPackageDirectory.exists()) {
313-
File dtoFile = new File(assumedPackageDirectory.getPath() + "/" + getDtoFileName());
314-
fullDtoPath = dtoFile.getPath();
315-
} else {
316-
File dtoFile = new File(moduleSourcePath + "/" + getDtoFileName());
317-
fullDtoPath = dtoFile.getPath();
318-
}
319-
return fullDtoPath;
320-
}
321337

322-
private boolean ValidateEndpoint() throws IOException {
323-
String typeMetadataUrl = createUrl(addressUrlTextField.getText()).replace("types/java/", "types/metadata/?format=json");
324-
URL metadataUrl = new URL(typeMetadataUrl);
325-
URLConnection metadataConnection = metadataUrl.openConnection();
326-
metadataConnection.setRequestProperty("content-type", "application/json; charset=utf-8");
327-
BufferedReader metadataBufferReader = new BufferedReader(
328-
new InputStreamReader(
329-
metadataConnection.getInputStream()));
330-
String metadataInputLine;
331-
StringBuilder metadataResponse = new StringBuilder();
332-
while ((metadataInputLine = metadataBufferReader.readLine()) != null)
333-
metadataResponse.append(metadataInputLine);
334-
335-
metadataBufferReader.close();
336-
String metadataJson = metadataResponse.toString();
337-
Gson gson = new Gson();
338-
try {
339-
ServiceStackMetadata metadata = gson.fromJson(metadataJson, ServiceStackMetadata.class);
340-
if (metadata == null || metadata.getConfig() == null || metadata.getConfig().getBaseUrl() == null) {
341-
errorMessage = "The address url is not a valid ServiceStack endpoint.";
342-
return false;
338+
PsiPackage mainPackage = JavaPsiFacade.getInstance(module.getProject()).findPackage(packageBrowse.getText());
339+
if(mainPackage != null && mainPackage.isValid() && mainPackage.getDirectories().length > 0) {
340+
File foo = new File(selectedDirectory);
341+
VirtualFile selectedFolder = LocalFileSystem.getInstance().findFileByIoFile(foo);
342+
if(selectedFolder == null) {
343+
errorMessage = "Unable to determine path for DTO file.";
344+
throw new FileNotFoundException();
343345
}
344-
} catch (Exception e) {
345-
errorMessage = "The address url is not a valid ServiceStack endpoint.";
346-
return false;
346+
PsiDirectory rootPackageDir = PsiManager.getInstance(module.getProject()).findDirectory(selectedFolder);
347+
fullDtoPath = rootPackageDir.getVirtualFile().getPath() + "/" + getDtoFileName();
348+
} else {
349+
String moduleSourcePath;
350+
if(moduleFile.getParent() == null) {
351+
moduleSourcePath = moduleFile.getPath() + "/main/java";
352+
} else {
353+
moduleSourcePath = moduleFile.getParent().getPath() + "/src/main/java";
354+
}
355+
fullDtoPath = moduleSourcePath + "/" + getDtoFileName();
347356
}
348-
349-
return true;
357+
return fullDtoPath;
350358
}
351359

352360
private void refreshBuildFile() {
@@ -378,7 +386,7 @@ private void refreshFile(String filePath, boolean openFile) {
378386
VirtualFileManager.getInstance().syncRefresh();
379387
}
380388

381-
private static class BrowsePackageListener implements ActionListener {
389+
private class BrowsePackageListener implements ActionListener {
382390
private TextFieldWithBrowseButton _textField;
383391
private Project _project;
384392
private String _title;
@@ -397,33 +405,30 @@ public void actionPerformed(ActionEvent e) {
397405
if (dialog.getExitCode() == PackageChooserDialog.CANCEL_EXIT_CODE) {
398406
return;
399407
}
400-
408+
selectedPackage = dialog.getSelectedPackage();
401409
_textField.setText(dialog.getSelectedPackage().getQualifiedName());
402410
}
403411
}
404412

405-
private String createUrl(String text) throws MalformedURLException {
413+
private URIBuilder createUrl(String text) throws MalformedURLException, URISyntaxException {
406414
String serverUrl = text.endsWith("/") ? text : (text + "/");
407415
serverUrl = (serverUrl.startsWith("http://") || serverUrl.startsWith("https://")) ? serverUrl : ("http://" + serverUrl);
408416
URL url = new URL(serverUrl);
409417
String path = url.getPath().contains("?") ? url.getPath().split("\\?", 2)[0] : url.getPath();
410418
if (!path.endsWith("types/java/")) {
411419
serverUrl += "types/java/";
412420
}
421+
URIBuilder builder;
413422

414-
if (packageBrowse.getText() != null && !packageBrowse.getText().isEmpty()) {
415-
try {
416-
URIBuilder builder = new URIBuilder(serverUrl);
417-
builder.addParameter("Package", packageBrowse.getText());
418-
String name = getDtoNameWithoutExtention().replaceAll("\\.", "_");
419-
builder.addParameter("GlobalNamespace", name);
420-
serverUrl = builder.build().toString();
421-
} catch (URISyntaxException e) {
422-
e.printStackTrace();
423-
}
423+
try {
424+
builder = new URIBuilder(serverUrl);
425+
} catch (URISyntaxException e) {
426+
e.printStackTrace();
427+
throw e;
424428
}
425429

426-
return serverUrl;
430+
431+
return builder;
427432
}
428433

429434
private String getDtoFileName() {

0 commit comments

Comments
 (0)