Skip to content

Commit 10f6487

Browse files
committed
Add hints for configuration text fields.
1 parent 216f8b0 commit 10f6487

File tree

1 file changed

+195
-175
lines changed

1 file changed

+195
-175
lines changed

src/org/openactive/gitlab/snippet/SnippetCreate.java

Lines changed: 195 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.intellij.openapi.options.ConfigurationException;
1010
import com.intellij.openapi.util.IconLoader;
1111
import com.intellij.openapi.vfs.VirtualFile;
12+
import org.apache.commons.lang.StringUtils;
1213
import org.apache.http.client.methods.CloseableHttpResponse;
1314
import org.apache.http.client.methods.HttpPost;
1415
import org.apache.http.entity.ContentType;
@@ -29,179 +30,198 @@
2930

3031
public class SnippetCreate extends AnAction implements Configurable
3132
{
32-
private JTextField urlField, tokenField;
33-
34-
35-
@Nls
36-
@Override
37-
public String getDisplayName()
38-
{
39-
return "Gitlab Snippets";
40-
}
41-
42-
private String getToken()
43-
{
44-
PropertiesComponent props = PropertiesComponent.getInstance();
45-
String token = props.getValue( "org.openactive.gitlab.snippets.token" );
46-
if( token == null ) token = "";
47-
return token;
48-
}
49-
50-
private String getUrl()
51-
{
52-
PropertiesComponent props = PropertiesComponent.getInstance();
53-
String url = props.getValue( "org.openactive.gitlab.snippets.url" );
54-
if( url == null ) url = "";
55-
return url;
56-
}
57-
58-
private String getUseableUrl()
59-
{
60-
String url = getUrl();
61-
if( url != null )
62-
{
63-
if( url.endsWith( "api/v4/snippets" ) || url.endsWith( "api/v4/snippets/" ) ) return url;
64-
else if( url.endsWith( "/" ) ) return url + "api/v4/snippets";
65-
else return url + "/api/v4/snippets";
66-
}
67-
return "";
68-
}
69-
70-
@Nullable
71-
@Override
72-
public JComponent createComponent()
73-
{
74-
JLabel urlLable = new JLabel( "Gitlab Install URL" );
75-
urlField = new JTextField( getUrl() );
76-
77-
JLabel tokenLable = new JLabel( "Token" );
78-
tokenField = new JTextField( getToken() );
79-
80-
JPanel panel = new JPanel( new GridBagLayout() );
81-
Bag bag = new Bag();
82-
83-
panel.add( urlLable, bag );
84-
panel.add( urlField, bag.nextX().fillX() );
85-
panel.add( tokenLable, bag.nextY().resetX().fillNone());
86-
panel.add( tokenField, bag.nextX().fillX());
87-
panel.add( Bag.spacer(), bag.nextY().resetX().colspan( 2 ).fillBoth());
88-
89-
return panel;
90-
}
91-
92-
@Override
93-
public boolean isModified()
94-
{
95-
if( urlField.getText().trim().length() > 0 && tokenField.getText().trim().length() > 0 )
96-
{
97-
return !(urlField.getText().matches( getUrl() ) &&
98-
tokenField.getText().matches( getToken() ) );
99-
}
100-
return false;
101-
}
102-
103-
@Override
104-
public void apply() throws ConfigurationException
105-
{
106-
PropertiesComponent props = PropertiesComponent.getInstance();
107-
props.setValue( "org.openactive.gitlab.snippets.url", urlField.getText().trim() );
108-
props.setValue( "org.openactive.gitlab.snippets.token", tokenField.getText().trim() );
109-
}
110-
111-
private String post( String text , String fileName ) throws Exception
112-
{
113-
CloseableHttpResponse resp = null;
114-
115-
HttpPost post = new HttpPost( getUseableUrl() );
116-
post.addHeader( "PRIVATE-TOKEN", getToken() );
117-
118-
String data = getData( "Snippet", text, fileName );
119-
StringEntity ent = new StringEntity( data, ContentType.APPLICATION_JSON );
120-
post.setEntity( ent );
121-
try( CloseableHttpClient client = HttpClients.createDefault() )
122-
{
123-
resp = client.execute( post );
124-
byte[] buff = new byte[1024];
125-
int read = 0;
126-
InputStream is = resp.getEntity().getContent();
127-
StringBuilder builder = new StringBuilder( );
128-
while((read = is.read(buff)) != -1 )
129-
{
130-
builder.append( new String( buff, 0 , read ));
131-
}
132-
JSONObject obj = new JSONObject( builder.toString() );
133-
String snippetUrl = obj.getString( "web_url" );
134-
135-
StringSelection stringSelection = new StringSelection(snippetUrl);
136-
Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
137-
clpbrd.setContents(stringSelection, null);
138-
return snippetUrl;
139-
}
140-
finally
141-
{
142-
try{ resp.close(); }catch ( Exception e ){}
143-
}
144-
}
145-
146-
private String getData( String title, String content, String fileName )
147-
{
148-
Map<String, String> data = new HashMap<>();
149-
data.put( "title", title );
150-
data.put( "content", content );
151-
data.put( "file_name", fileName );
152-
data.put( "visibility", "internal");
153-
return new JSONObject( data ).toString();
154-
}
155-
156-
@Override
157-
public void update( AnActionEvent e )
158-
{
159-
// only show this action if some text is selected
160-
Editor editor = FileEditorManager.getInstance( e.getProject() ).getSelectedTextEditor();
161-
String s = editor.getCaretModel().getCurrentCaret().getSelectedText();
162-
if( s == null || s.length() == 0 )
163-
{
164-
e.getPresentation().setVisible( false );
165-
}
166-
}
167-
168-
@Override
169-
public void actionPerformed( AnActionEvent e )
170-
{
171-
try
172-
{
173-
Editor editor = FileEditorManager.getInstance( e.getProject() ).getSelectedTextEditor();
174-
VirtualFile vf = e.getData( PlatformDataKeys.VIRTUAL_FILE );
175-
String s = editor.getCaretModel().getCurrentCaret().getSelectedText();
176-
String url = post( s, vf != null ? vf.getName() : "unknown" );
177-
178-
179-
Notification note = new Notification(
180-
"Gitlab Snippet",
181-
IconLoader.getIcon( "/gl.png" ),
182-
"Gitlab Snippet Created",
183-
null,
184-
"New snippet at <b>"+ url +"</b> has been copied to the clipboard." ,
185-
NotificationType.INFORMATION,
186-
null
187-
);
188-
189-
Notifications.Bus.notify( note );
190-
}
191-
catch ( Exception ex )
192-
{
193-
ex.printStackTrace();
194-
Notification note = new Notification(
195-
"Gitlab Snippet",
196-
IconLoader.getIcon( "/gl.png" ),
197-
"Failed creating Gitlab Snippet",
198-
null,
199-
ex.getMessage() ,
200-
NotificationType.ERROR,
201-
null
202-
);
203-
204-
Notifications.Bus.notify( note );
205-
}
206-
}
33+
private JTextField urlField, tokenField;
34+
35+
36+
@Nls
37+
@Override
38+
public String getDisplayName()
39+
{
40+
return "Gitlab Snippets";
41+
}
42+
43+
44+
private boolean isConfigured()
45+
{
46+
return StringUtils.isNotBlank( getToken() ) &&
47+
StringUtils.isNotBlank( getUrl() );
48+
}
49+
50+
private String getToken()
51+
{
52+
PropertiesComponent props = PropertiesComponent.getInstance();
53+
String token = props.getValue( "org.openactive.gitlab.snippets.token" );
54+
if ( token == null ) token = "";
55+
return token;
56+
}
57+
58+
private String getUrl()
59+
{
60+
PropertiesComponent props = PropertiesComponent.getInstance();
61+
String url = props.getValue( "org.openactive.gitlab.snippets.url" );
62+
if ( url == null ) url = "";
63+
return url;
64+
}
65+
66+
private String getUseableUrl()
67+
{
68+
String url = getUrl();
69+
if ( url != null )
70+
{
71+
if ( url.endsWith( "api/v4/snippets" ) || url.endsWith( "api/v4/snippets/" ) ) return url;
72+
else if ( url.endsWith( "/" ) ) return url + "api/v4/snippets";
73+
else return url + "/api/v4/snippets";
74+
}
75+
return "";
76+
}
77+
78+
@Nullable
79+
@Override
80+
public JComponent createComponent()
81+
{
82+
JLabel urlHint = new JLabel("This url should point at the root of your Gitlab install.\n i.e. https://gitlab.company.com" );
83+
JLabel urlLable = new JLabel( "Gitlab Install URL" );
84+
urlField = new JTextField( getUrl() );
85+
86+
JLabel tokenLable = new JLabel( "Token" );
87+
tokenField = new JTextField( getToken() );
88+
89+
JPanel panel = new JPanel( new GridBagLayout() );
90+
Bag bag = new Bag();
91+
92+
93+
panel.add( urlLable, bag );
94+
panel.add( urlField, bag.nextX().fillX() );
95+
panel.add( urlHint, bag.nextY().fillNone());
96+
97+
JPanel spacer = new JPanel();
98+
spacer.setPreferredSize( new Dimension( 5, 30 ) );
99+
panel.add( spacer, bag.nextY().resetX() );
100+
101+
panel.add( tokenLable, bag.nextY().resetX().fillNone().colspan( 1 ) );
102+
panel.add( tokenField, bag.nextX().fillX() );
103+
104+
JLabel tokenHint = new JLabel( "To create a new token https://gitlab.company.com/profile/personal_access_tokens" );
105+
panel.add( tokenHint, bag.nextY().fillNone() );
106+
107+
panel.add( Bag.spacer(), bag.nextY().resetX().colspan( 2 ).fillBoth() );
108+
109+
return panel;
110+
}
111+
112+
@Override
113+
public boolean isModified()
114+
{
115+
if ( urlField.getText().trim().length() > 0 && tokenField.getText().trim().length() > 0 )
116+
{
117+
return !(urlField.getText().matches( getUrl() ) &&
118+
tokenField.getText().matches( getToken() ));
119+
}
120+
return false;
121+
}
122+
123+
@Override
124+
public void apply() throws ConfigurationException
125+
{
126+
PropertiesComponent props = PropertiesComponent.getInstance();
127+
props.setValue( "org.openactive.gitlab.snippets.url", urlField.getText().trim() );
128+
props.setValue( "org.openactive.gitlab.snippets.token", tokenField.getText().trim() );
129+
}
130+
131+
private String post( String text, String fileName ) throws Exception
132+
{
133+
CloseableHttpResponse resp = null;
134+
135+
HttpPost post = new HttpPost( getUseableUrl() );
136+
post.addHeader( "PRIVATE-TOKEN", getToken() );
137+
138+
String data = getData( "Snippet", text, fileName );
139+
StringEntity ent = new StringEntity( data, ContentType.APPLICATION_JSON );
140+
post.setEntity( ent );
141+
try ( CloseableHttpClient client = HttpClients.createDefault() )
142+
{
143+
resp = client.execute( post );
144+
byte[] buff = new byte[1024];
145+
int read = 0;
146+
InputStream is = resp.getEntity().getContent();
147+
StringBuilder builder = new StringBuilder();
148+
while ( (read = is.read( buff )) != -1 )
149+
{
150+
builder.append( new String( buff, 0, read ) );
151+
}
152+
JSONObject obj = new JSONObject( builder.toString() );
153+
String snippetUrl = obj.getString( "web_url" );
154+
155+
StringSelection stringSelection = new StringSelection( snippetUrl );
156+
Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
157+
clpbrd.setContents( stringSelection, null );
158+
return snippetUrl;
159+
}
160+
finally
161+
{
162+
try { resp.close(); } catch ( Exception e ) {}
163+
}
164+
}
165+
166+
private String getData( String title, String content, String fileName )
167+
{
168+
Map<String, String> data = new HashMap<>();
169+
data.put( "title", title );
170+
data.put( "content", content );
171+
data.put( "file_name", fileName );
172+
data.put( "visibility", "internal" );
173+
return new JSONObject( data ).toString();
174+
}
175+
176+
@Override
177+
public void update( AnActionEvent e )
178+
{
179+
// only show this action if some text is selected
180+
Editor editor = FileEditorManager.getInstance( e.getProject() ).getSelectedTextEditor();
181+
String s = editor.getCaretModel().getCurrentCaret().getSelectedText();
182+
if ( s == null || s.length() == 0 || !isConfigured() )
183+
{
184+
e.getPresentation().setVisible( false );
185+
}
186+
}
187+
188+
@Override
189+
public void actionPerformed( AnActionEvent e )
190+
{
191+
try
192+
{
193+
Editor editor = FileEditorManager.getInstance( e.getProject() ).getSelectedTextEditor();
194+
VirtualFile vf = e.getData( PlatformDataKeys.VIRTUAL_FILE );
195+
String s = editor.getCaretModel().getCurrentCaret().getSelectedText();
196+
String url = post( s, vf != null ? vf.getName() : "unknown" );
197+
198+
199+
Notification note = new Notification(
200+
"Gitlab Snippet",
201+
IconLoader.getIcon( "/gl.png" ),
202+
"Gitlab Snippet Created",
203+
null,
204+
"New snippet at <b>" + url + "</b> has been copied to the clipboard.",
205+
NotificationType.INFORMATION,
206+
null
207+
);
208+
209+
Notifications.Bus.notify( note );
210+
}
211+
catch ( Exception ex )
212+
{
213+
ex.printStackTrace();
214+
Notification note = new Notification(
215+
"Gitlab Snippet",
216+
IconLoader.getIcon( "/gl.png" ),
217+
"Failed creating Gitlab Snippet",
218+
null,
219+
ex.getMessage(),
220+
NotificationType.ERROR,
221+
null
222+
);
223+
224+
Notifications.Bus.notify( note );
225+
}
226+
}
207227
}

0 commit comments

Comments
 (0)