2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
20 import jalview.io.packed.DataProvider.JvDataType;
21 import jalview.jbgui.GRestServiceEditorPane;
22 import jalview.ws.rest.InputType;
23 import jalview.ws.rest.RestServiceDescription;
25 import java.awt.BorderLayout;
26 import java.awt.FlowLayout;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29 import java.awt.event.ComponentEvent;
30 import java.awt.event.ComponentListener;
31 import java.awt.event.KeyEvent;
32 import java.awt.event.KeyListener;
33 import java.awt.event.MouseEvent;
34 import java.util.HashMap;
35 import java.util.List;
37 import java.util.Vector;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
41 import javax.swing.JFrame;
42 import javax.swing.JMenuItem;
43 import javax.swing.JPanel;
44 import javax.swing.JPopupMenu;
45 import javax.swing.event.ChangeEvent;
46 import javax.swing.event.ChangeListener;
48 public class RestServiceEditorPane extends GRestServiceEditorPane
51 * the latest version of the service definition.
53 jalview.ws.rest.RestServiceDescription currentservice = null;
56 * original service passed to editor if we are modifying an existing service
59 jalview.ws.rest.RestServiceDescription oldservice = null;
61 public RestServiceEditorPane()
64 // begin with initial text description box enabled.
65 urldesc.addKeyListener(new KeyListener()
68 public void keyTyped(KeyEvent e)
73 public void keyReleased(KeyEvent e)
75 refreshCutnPaste(true);
79 public void keyPressed(KeyEvent e)
84 panels.addChangeListener(new ChangeListener()
88 * last panel selected - used to decide whether the service or the GUI has
94 public void stateChanged(ChangeEvent e)
96 if (lastComp != paste)
98 updateServiceFromGui();
99 refreshCutnPaste(false);
103 refreshCutnPaste(true);
105 lastComp = panels.getSelectedComponent();
109 currentservice = new RestServiceDescription("Analysis",
110 "service description", "service name", "http://localhost/", "",
111 null, false, false, '-');
112 initGuiWith(currentservice);
113 refreshCutnPaste(false);
117 public RestServiceEditorPane(RestServiceDescription toedit)
121 if (oldservice != null)
123 currentservice = new RestServiceDescription(toedit);
127 currentservice = new RestServiceDescription("Analysis",
128 "service description", "service name", "http://localhost/",
129 "", null, false, false, '-');
131 initGuiWith(currentservice);
132 refreshCutnPaste(false);
137 * refresh the buttons based on model state
139 public void updateButtons()
141 cancelButton.setEnabled(true);
142 okButton.setEnabled(currentservice != null && currentservice.isValid());
146 Vector<String> _iparam = new Vector<String>();
148 Vector<String> _rparam = new Vector<String>();
151 * generate an editable URL service string and parameter list using the
154 * @param currentservice2
156 private void initGuiWith(RestServiceDescription currentservice)
160 action.removeAllItems();
161 action.addItem("Alignment");
162 action.addItem("Analysis");
163 gapChar.removeAllItems();
164 gapChar.addItem(".");
165 gapChar.addItem(" ");
166 gapChar.addItem("-");
167 if (currentservice == null)
173 action.setSelectedItem("Analysis");
174 gapChar.setSelectedItem("-");
178 name.setText(currentservice.getName());
179 descr.setText(currentservice.getDescription());
180 url.setText(currentservice.getPostUrl());
181 urlsuff.setText(currentservice.getUrlSuffix());
182 for (Map.Entry<String, InputType> inparam : currentservice
183 .getInputParams().entrySet())
185 _iparam.add(inparam.getKey() + " "
186 + inparam.getValue().getURLtokenPrefix() + ":"
187 + inparam.getValue().getURLEncodedParameter().toString());
190 for (JvDataType oparam : currentservice.getResultDataTypes())
192 _rparam.add(oparam.name());
194 iprms.setListData(_iparam);
195 rdata.setListData(_rparam);
197 action.setSelectedItem(currentservice.getAction());
199 gapChar.setSelectedItem("" + currentservice.getGapCharacter());
204 private String getSelectedInputToken()
206 if (iprms.getSelectedIndex() > -1)
208 String toktoedit = (String) iprms.getSelectedValue();
209 toktoedit = toktoedit.substring(0, toktoedit.indexOf(" "));
216 protected void iprmListSelection_doubleClicked()
218 String toktoedit = getSelectedInputToken();
219 if (toktoedit != null)
221 InputType toedit = currentservice.getInputParams().get(toktoedit);
222 String oldParam = toktoedit;
223 RestInputParamEditDialog dialog = new RestInputParamEditDialog(this,
224 currentservice, toedit);
225 if (dialog.wasUpdated())
227 currentservice.getInputParams().remove(oldParam);
228 currentservice.getInputParams().put(dialog.current.token,
230 initGuiWith(currentservice);
237 protected void iprmsAdd_actionPerformed(ActionEvent e)
239 RestInputParamEditDialog dialog = new RestInputParamEditDialog(this,
240 currentservice, "param"
241 + (1 + currentservice.getInputParams().size()));
242 if (dialog.wasUpdated())
244 currentservice.getInputParams().put(dialog.current.token,
246 initGuiWith(currentservice);
252 protected void iprmsRem_actionPerformed(ActionEvent e)
254 String toktoedit = getSelectedInputToken();
255 if (toktoedit != null)
257 currentservice.getInputParams().remove(toktoedit);
258 initGuiWith(currentservice);
264 protected void rdata_rightClicked(MouseEvent mouse)
266 final int rdatasel = rdata.getSelectedIndex();
269 JPopupMenu popup = new JPopupMenu("Select return type");
270 for (final JvDataType type : JvDataType.values())
272 popup.add(new JMenuItem(type.name())).addActionListener(
277 public void actionPerformed(ActionEvent e)
279 currentservice.getResultDataTypes().set(rdatasel, type);
280 initGuiWith(currentservice);
281 rdata.setSelectedIndex(rdatasel);
285 popup.show(rdata, mouse.getX(), mouse.getY());
290 protected void rdataAdd_actionPerformed(ActionEvent e)
293 if ((p = rdata.getSelectedIndex()) > -1)
295 currentservice.getResultDataTypes().add(p + 1, JvDataType.ANNOTATION);
299 currentservice.addResultDatatype(JvDataType.ANNOTATION);
301 initGuiWith(currentservice);
302 rdata.setSelectedIndex(p == -1 ? currentservice.getResultDataTypes()
303 .size() - 1 : p + 1);
307 protected void rdataNdown_actionPerformed(ActionEvent e)
310 if ((p = rdata.getSelectedIndex()) > -1 && p < _rparam.size() - 1)
312 List<JvDataType> rtypes = currentservice.getResultDataTypes();
313 JvDataType below = rtypes.get(p + 1);
314 rtypes.set(p + 1, rtypes.get(p));
315 rtypes.set(p, below);
316 initGuiWith(currentservice);
317 rdata.setSelectedIndex(p + 1);
322 protected void rdataNup_actionPerformed(ActionEvent e)
325 if ((p = rdata.getSelectedIndex()) > 0)
327 List<JvDataType> rtypes = currentservice.getResultDataTypes();
328 JvDataType above = rtypes.get(p - 1);
329 rtypes.set(p - 1, rtypes.get(p));
330 rtypes.set(p, above);
331 initGuiWith(currentservice);
332 rdata.setSelectedIndex(p - 1);
337 protected void rdataRem_actionPerformed(ActionEvent e)
339 if (rdata.getSelectedIndex() > -1)
341 currentservice.getResultDataTypes().remove(rdata.getSelectedIndex());
342 initGuiWith(currentservice);
346 private boolean updateServiceFromGui()
348 Map<String, InputType> inputTypes = new HashMap<String, InputType>();
349 StringBuffer warnings = new StringBuffer();
350 for (String its : _iparam)
352 Matcher mtch = Pattern.compile("(\\S+)\\s(\\S+):\\[(.+)]").matcher(
356 if (!RestServiceDescription.parseTypeString(mtch.group(2) + ":"
357 + mtch.group(3), mtch.group(1), mtch.group(2),
358 mtch.group(3), inputTypes, warnings))
361 .println("IMPLEMENTATION PROBLEM: Cannot parse RestService input parameter string '"
362 + its + "'" + "\n" + warnings);
366 char gc = gapChar.getSelectedItem() == null ? ' ' : ((String) gapChar
367 .getSelectedItem()).charAt(0);
368 RestServiceDescription newService = new RestServiceDescription(
369 (String) action.getSelectedItem(), descr.getText().trim(), name
370 .getText().trim(), url.getText().trim(), urlsuff
371 .getText().trim(), inputTypes, hSeparable.isSelected(),
372 vSeparable.isSelected(), gc);
374 if (newService.isValid())
376 for (String its : _rparam)
381 dtype = JvDataType.valueOf(its);
382 newService.addResultDatatype(dtype);
383 } catch (Throwable x)
387 .println("IMPLEMENTATION PROBLEM: Cannot parse RestService output parameter string '"
388 + its + "'" + "\n" + warnings);
391 currentservice = newService;
397 .println("IMPLEMENTATION PROBLEM: Restservice generated from GUI is invalid\n"
404 protected void refreshCutnPaste(boolean reparse)
406 if (!reparse && currentservice.isValid())
408 urldesc.setText(currentservice.toString());
409 parseWarnings.setVisible(false);
415 String txt = urldesc.getText().trim();
416 if (txt.length() > 0)
418 RestServiceDescription rsd = null;
421 rsd = new RestServiceDescription(txt);
424 parseWarnings.setVisible(false);
425 parseRes.setText("");
426 initGuiWith(currentservice = rsd);
430 parseRes.setText("Parsing failed. Syntax errors shown below\n"
431 + rsd.getInvalidMessage());
432 parseWarnings.setVisible(true);
434 } catch (Throwable e)
437 parseRes.setText("\nParsing failed. An unrecoverable exception was thrown:\n"
439 parseWarnings.setVisible(true);
448 public static void main(String[] args)
450 if (args.length == 0)
452 new Thread(new Runnable()
454 boolean visible = true;
458 boolean nulserv = true;
461 final Thread runner = Thread.currentThread();
462 JFrame df = new JFrame();
463 df.getContentPane().setLayout(new BorderLayout());
464 df.getContentPane().add(
465 (nulserv = !nulserv) ? new RestServiceEditorPane(
466 jalview.ws.rest.RestClient
467 .makeShmmrRestClient()
468 .getRestDescription())
469 : new RestServiceEditorPane(),
470 BorderLayout.CENTER);
471 df.setBounds(100, 100, 600, 400);
472 df.addComponentListener(new ComponentListener()
476 public void componentShown(ComponentEvent e)
482 public void componentResized(ComponentEvent e)
488 public void componentMoved(ComponentEvent e)
494 public void componentHidden(ComponentEvent e)
507 } catch (Exception x)
520 String finalService = null;
522 public void showDialog(String title)
524 if (oldservice != null)
526 finalService = oldservice.toString();
528 JalviewDialog jvd = new JalviewDialog()
532 protected void raiseClosed()
534 // TODO Auto-generated method stub
539 protected void okPressed()
541 updateServiceFromGui();
542 finalService = currentservice.toString();
546 protected void cancelPressed()
551 JPanel pane = new JPanel(new BorderLayout()), okcancel = new JPanel(
553 pane.add(this, BorderLayout.CENTER);
554 okcancel.add(jvd.ok);
555 okcancel.add(jvd.cancel);
556 pane.add(okcancel, BorderLayout.SOUTH);
557 jvd.initDialogFrame(pane, true, true, title, 600, 350);
561 public String getEditedRestService()