3 import jalview.io.packed.DataProvider.JvDataType;
4 import jalview.jbgui.GRestServiceEditorPane;
5 import jalview.ws.rest.InputType;
6 import jalview.ws.rest.RestServiceDescription;
8 import java.awt.BorderLayout;
9 import java.awt.FlowLayout;
10 import java.awt.event.ActionEvent;
11 import java.awt.event.ActionListener;
12 import java.awt.event.ComponentEvent;
13 import java.awt.event.ComponentListener;
14 import java.awt.event.KeyEvent;
15 import java.awt.event.KeyListener;
16 import java.awt.event.MouseEvent;
17 import java.util.HashMap;
18 import java.util.List;
20 import java.util.Vector;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
24 import javax.swing.JFrame;
25 import javax.swing.JMenuItem;
26 import javax.swing.JPanel;
27 import javax.swing.JPopupMenu;
28 import javax.swing.event.ChangeEvent;
29 import javax.swing.event.ChangeListener;
31 public class RestServiceEditorPane extends GRestServiceEditorPane
34 * the latest version of the service definition.
36 jalview.ws.rest.RestServiceDescription currentservice = null;
39 * original service passed to editor if we are modifying an existing service
42 jalview.ws.rest.RestServiceDescription oldservice = null;
44 public RestServiceEditorPane()
47 // begin with initial text description box enabled.
48 urldesc.addKeyListener(new KeyListener()
51 public void keyTyped(KeyEvent e)
56 public void keyReleased(KeyEvent e)
58 refreshCutnPaste(true);
62 public void keyPressed(KeyEvent e)
67 panels.addChangeListener(new ChangeListener()
71 * last panel selected - used to decide whether the service or the GUI has the latest info
75 public void stateChanged(ChangeEvent e)
77 if (lastComp!=paste) {
78 updateServiceFromGui();
79 refreshCutnPaste(false);
81 refreshCutnPaste(true);
83 lastComp = panels.getSelectedComponent();
87 currentservice = new RestServiceDescription("Analysis", "service description", "service name", "http://localhost/", "", null, false, false, '-');
88 initGuiWith(currentservice);
89 refreshCutnPaste(false);
93 public RestServiceEditorPane(RestServiceDescription toedit)
98 { currentservice = new RestServiceDescription(toedit);
100 currentservice = new RestServiceDescription("Analysis", "service description", "service name", "http://localhost/", "", null, false, false, '-');
102 initGuiWith(currentservice);
103 refreshCutnPaste(false);
108 * refresh the buttons based on model state
110 public void updateButtons()
112 cancelButton.setEnabled(true);
113 okButton.setEnabled(currentservice != null && currentservice.isValid());
117 Vector<String> _iparam = new Vector<String>();
119 Vector<String> _rparam = new Vector<String>();
122 * generate an editable URL service string and parameter list using the
125 * @param currentservice2
127 private void initGuiWith(RestServiceDescription currentservice)
131 action.removeAllItems();
132 action.addItem("Alignment");
133 action.addItem("Analysis");
134 gapChar.removeAllItems();
135 gapChar.addItem(".");
136 gapChar.addItem(" ");
137 gapChar.addItem("-");
138 if (currentservice==null)
144 action.setSelectedItem("Analysis");
145 gapChar.setSelectedItem("-");
147 name.setText(currentservice.getName());
148 descr.setText(currentservice.getDescription());
149 url.setText(currentservice.getPostUrl());
150 urlsuff.setText(currentservice.getUrlSuffix());
151 for (Map.Entry<String, InputType> inparam : currentservice
152 .getInputParams().entrySet())
154 _iparam.add(inparam.getKey() + " "
155 + inparam.getValue().getURLtokenPrefix() + ":"
156 + inparam.getValue().getURLEncodedParameter().toString());
159 for (JvDataType oparam : currentservice.getResultDataTypes())
161 _rparam.add(oparam.name());
163 iprms.setListData(_iparam);
164 rdata.setListData(_rparam);
166 action.setSelectedItem(currentservice.getAction());
168 gapChar.setSelectedItem(""+currentservice.getGapCharacter());
172 private String getSelectedInputToken()
174 if (iprms.getSelectedIndex()>-1)
176 String toktoedit = (String) iprms.getSelectedValue();
177 toktoedit=toktoedit.substring(0, toktoedit.indexOf(" "));
183 protected void iprmListSelection_doubleClicked()
185 String toktoedit = getSelectedInputToken();
188 InputType toedit = currentservice.getInputParams().get(toktoedit);
189 String oldParam=toktoedit;
190 RestInputParamEditDialog dialog=new RestInputParamEditDialog(this, currentservice, toedit);
191 if (dialog.wasUpdated()) {
192 currentservice.getInputParams().remove(oldParam);
193 currentservice.getInputParams().put(dialog.current.token, dialog.current);
194 initGuiWith(currentservice);
200 protected void iprmsAdd_actionPerformed(ActionEvent e)
202 RestInputParamEditDialog dialog=new RestInputParamEditDialog(this, currentservice, "param"+(1+currentservice.getInputParams().size()));
203 if (dialog.wasUpdated()) {
204 currentservice.getInputParams().put(dialog.current.token, dialog.current);
205 initGuiWith(currentservice);
210 protected void iprmsRem_actionPerformed(ActionEvent e)
212 String toktoedit = getSelectedInputToken();
215 currentservice.getInputParams().remove(toktoedit);
216 initGuiWith(currentservice);
221 protected void rdata_rightClicked(MouseEvent mouse)
223 final int rdatasel = rdata.getSelectedIndex();
226 JPopupMenu popup = new JPopupMenu("Select return type");
227 for (final JvDataType type:JvDataType.values()) {
228 popup.add(new JMenuItem(type.name())).addActionListener(new ActionListener()
232 public void actionPerformed(ActionEvent e)
234 currentservice.getResultDataTypes().set(rdatasel, type);
235 initGuiWith(currentservice);
236 rdata.setSelectedIndex(rdatasel);
240 popup.show(rdata, mouse.getX(), mouse.getY());
244 protected void rdataAdd_actionPerformed(ActionEvent e)
247 if ((p=rdata.getSelectedIndex())>-1)
249 currentservice.getResultDataTypes().add(p+1, JvDataType.ANNOTATION);
251 currentservice.addResultDatatype(JvDataType.ANNOTATION);
253 initGuiWith(currentservice);
254 rdata.setSelectedIndex(p==-1 ? currentservice.getResultDataTypes().size()-1 : p+1);
257 protected void rdataNdown_actionPerformed(ActionEvent e)
260 if ((p=rdata.getSelectedIndex())>-1 && p<_rparam.size()-1)
262 List<JvDataType> rtypes = currentservice.getResultDataTypes();
263 JvDataType below = rtypes.get(p+1);
264 rtypes.set(p+1, rtypes.get(p));
265 rtypes.set(p, below);
266 initGuiWith(currentservice);
267 rdata.setSelectedIndex(p+1);
271 protected void rdataNup_actionPerformed(ActionEvent e)
274 if ((p=rdata.getSelectedIndex())>0)
276 List<JvDataType> rtypes = currentservice.getResultDataTypes();
277 JvDataType above = rtypes.get(p-1);
278 rtypes.set(p-1, rtypes.get(p));
279 rtypes.set(p, above);
280 initGuiWith(currentservice);
281 rdata.setSelectedIndex(p-1);
285 protected void rdataRem_actionPerformed(ActionEvent e)
287 if (rdata.getSelectedIndex()>-1)
289 currentservice.getResultDataTypes().remove(rdata.getSelectedIndex());
290 initGuiWith(currentservice);
294 private boolean updateServiceFromGui() {
295 Map<String,InputType>inputTypes = new HashMap<String, InputType>();
296 StringBuffer warnings=new StringBuffer();
297 for (String its:_iparam)
299 Matcher mtch = Pattern.compile("(\\S+)\\s(\\S+):\\[(.+)]").matcher(its);
301 if (!RestServiceDescription.parseTypeString(mtch.group(2)+":"+mtch.group(3), mtch.group(1), mtch.group(2),mtch.group(3), inputTypes, warnings))
303 System.err.println("IMPLEMENTATION PROBLEM: Cannot parse RestService input parameter string '"+its+"'"+"\n"+warnings);
307 char gc = gapChar.getSelectedItem()==null ? ' ' : ((String)gapChar.getSelectedItem()).charAt(0);
308 RestServiceDescription newService = new RestServiceDescription((String) action.getSelectedItem(),
309 descr.getText().trim(), name.getText().trim(), url.getText().trim(), urlsuff.getText().trim(), inputTypes, hSeparable.isSelected(), vSeparable.isSelected(), gc);
311 if (newService.isValid())
313 for (String its:_rparam)
317 dtype = JvDataType.valueOf(its);
318 newService.addResultDatatype(dtype);
323 System.err.println("IMPLEMENTATION PROBLEM: Cannot parse RestService output parameter string '"+its+"'"+"\n"+warnings);
326 currentservice = newService;
329 System.err.println("IMPLEMENTATION PROBLEM: Restservice generated from GUI is invalid\n"+warnings);
334 protected void refreshCutnPaste(boolean reparse)
336 if (!reparse && currentservice.isValid())
338 urldesc.setText(currentservice.toString());
339 parseWarnings.setVisible(false);
345 String txt = urldesc.getText().trim();
346 if (txt.length() > 0)
348 RestServiceDescription rsd = null;
351 rsd = new RestServiceDescription(txt);
354 parseWarnings.setVisible(false);
355 parseRes.setText("");
356 initGuiWith(currentservice=rsd);
360 parseRes.setText("Parsing failed. Syntax errors shown below\n"
361 + rsd.getInvalidMessage());
362 parseWarnings.setVisible(true);
364 } catch (Throwable e)
367 parseRes.setText("\nParsing failed. An unrecoverable exception was thrown:\n"
369 parseWarnings.setVisible(true);
378 public static void main(String[] args)
380 if (args.length == 0)
382 new Thread(new Runnable()
384 boolean visible = true;
388 boolean nulserv=true;
391 final Thread runner = Thread.currentThread();
392 JFrame df = new JFrame();
393 df.getContentPane().setLayout(new BorderLayout());
394 df.getContentPane().add(
395 (nulserv=!nulserv) ? new RestServiceEditorPane(jalview.ws.rest.RestClient
396 .makeShmmrRestClient().getRestDescription()) : new RestServiceEditorPane(),
397 BorderLayout.CENTER);
398 df.setBounds(100, 100, 600, 400);
399 df.addComponentListener(new ComponentListener()
403 public void componentShown(ComponentEvent e)
409 public void componentResized(ComponentEvent e)
415 public void componentMoved(ComponentEvent e)
421 public void componentHidden(ComponentEvent e)
434 } catch (Exception x)
446 String finalService=null;
447 public void showDialog(String title)
449 if (oldservice!=null)
451 finalService = oldservice.toString();
453 JalviewDialog jvd = new JalviewDialog()
457 protected void raiseClosed()
459 // TODO Auto-generated method stub
464 protected void okPressed()
466 updateServiceFromGui();
467 finalService = currentservice.toString();
471 protected void cancelPressed()
476 JPanel pane = new JPanel(new BorderLayout()),okcancel=new JPanel(new FlowLayout());
477 pane.add(this,BorderLayout.CENTER);
478 okcancel.add(jvd.ok);
479 okcancel.add(jvd.cancel);
480 pane.add(okcancel, BorderLayout.SOUTH);
481 jvd.initDialogFrame(pane, true, true, title, 600,350);
485 public String getEditedRestService()