2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 The Jalview Authors
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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 import jalview.io.packed.DataProvider.JvDataType;
22 import jalview.jbgui.GRestServiceEditorPane;
23 import jalview.ws.rest.InputType;
24 import jalview.ws.rest.RestServiceDescription;
26 import java.awt.BorderLayout;
27 import java.awt.FlowLayout;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.awt.event.ComponentEvent;
31 import java.awt.event.ComponentListener;
32 import java.awt.event.KeyEvent;
33 import java.awt.event.KeyListener;
34 import java.awt.event.MouseEvent;
35 import java.util.HashMap;
36 import java.util.List;
38 import java.util.Vector;
39 import java.util.regex.Matcher;
40 import java.util.regex.Pattern;
42 import javax.swing.JFrame;
43 import javax.swing.JMenuItem;
44 import javax.swing.JPanel;
45 import javax.swing.JPopupMenu;
46 import javax.swing.event.ChangeEvent;
47 import javax.swing.event.ChangeListener;
49 public class RestServiceEditorPane extends GRestServiceEditorPane
52 * the latest version of the service definition.
54 jalview.ws.rest.RestServiceDescription currentservice = null;
57 * original service passed to editor if we are modifying an existing service
60 jalview.ws.rest.RestServiceDescription oldservice = null;
62 public RestServiceEditorPane()
65 // begin with initial text description box enabled.
66 urldesc.addKeyListener(new KeyListener()
69 public void keyTyped(KeyEvent e)
74 public void keyReleased(KeyEvent e)
76 refreshCutnPaste(true);
80 public void keyPressed(KeyEvent e)
85 panels.addChangeListener(new ChangeListener()
89 * last panel selected - used to decide whether the service or the GUI has
95 public void stateChanged(ChangeEvent e)
97 if (lastComp != paste)
99 updateServiceFromGui();
100 refreshCutnPaste(false);
104 refreshCutnPaste(true);
106 lastComp = panels.getSelectedComponent();
110 currentservice = new RestServiceDescription("Analysis",
111 "service description", "service name", "http://localhost/", "",
112 null, false, false, '-');
113 initGuiWith(currentservice);
114 refreshCutnPaste(false);
118 public RestServiceEditorPane(RestServiceDescription toedit)
122 if (oldservice != null)
124 currentservice = new RestServiceDescription(toedit);
128 currentservice = new RestServiceDescription("Analysis",
129 "service description", "service name", "http://localhost/",
130 "", null, false, false, '-');
132 initGuiWith(currentservice);
133 refreshCutnPaste(false);
138 * refresh the buttons based on model state
140 public void updateButtons()
142 cancelButton.setEnabled(true);
143 okButton.setEnabled(currentservice != null && currentservice.isValid());
147 Vector<String> _iparam = new Vector<String>();
149 Vector<String> _rparam = new Vector<String>();
152 * generate an editable URL service string and parameter list using the
155 * @param currentservice2
157 private void initGuiWith(RestServiceDescription currentservice)
161 action.removeAllItems();
162 action.addItem("Alignment");
163 action.addItem("Analysis");
164 gapChar.removeAllItems();
165 gapChar.addItem(".");
166 gapChar.addItem(" ");
167 gapChar.addItem("-");
168 if (currentservice == null)
174 action.setSelectedItem("Analysis");
175 gapChar.setSelectedItem("-");
179 name.setText(currentservice.getName());
180 descr.setText(currentservice.getDescription());
181 url.setText(currentservice.getPostUrl());
182 urlsuff.setText(currentservice.getUrlSuffix());
183 for (Map.Entry<String, InputType> inparam : currentservice
184 .getInputParams().entrySet())
186 _iparam.add(inparam.getKey() + " "
187 + inparam.getValue().getURLtokenPrefix() + ":"
188 + inparam.getValue().getURLEncodedParameter().toString());
191 for (JvDataType oparam : currentservice.getResultDataTypes())
193 _rparam.add(oparam.name());
195 iprms.setListData(_iparam);
196 rdata.setListData(_rparam);
198 action.setSelectedItem(currentservice.getAction());
200 gapChar.setSelectedItem("" + currentservice.getGapCharacter());
205 private String getSelectedInputToken()
207 if (iprms.getSelectedIndex() > -1)
209 String toktoedit = (String) iprms.getSelectedValue();
210 toktoedit = toktoedit.substring(0, toktoedit.indexOf(" "));
217 protected void iprmListSelection_doubleClicked()
219 String toktoedit = getSelectedInputToken();
220 if (toktoedit != null)
222 InputType toedit = currentservice.getInputParams().get(toktoedit);
223 String oldParam = toktoedit;
224 RestInputParamEditDialog dialog = new RestInputParamEditDialog(this,
225 currentservice, toedit);
226 if (dialog.wasUpdated())
228 currentservice.getInputParams().remove(oldParam);
229 currentservice.getInputParams().put(dialog.current.token,
231 initGuiWith(currentservice);
238 protected void iprmsAdd_actionPerformed(ActionEvent e)
240 RestInputParamEditDialog dialog = new RestInputParamEditDialog(this,
241 currentservice, "param"
242 + (1 + currentservice.getInputParams().size()));
243 if (dialog.wasUpdated())
245 currentservice.getInputParams().put(dialog.current.token,
247 initGuiWith(currentservice);
253 protected void iprmsRem_actionPerformed(ActionEvent e)
255 String toktoedit = getSelectedInputToken();
256 if (toktoedit != null)
258 currentservice.getInputParams().remove(toktoedit);
259 initGuiWith(currentservice);
265 protected void rdata_rightClicked(MouseEvent mouse)
267 final int rdatasel = rdata.getSelectedIndex();
270 JPopupMenu popup = new JPopupMenu("Select return type");
271 for (final JvDataType type : JvDataType.values())
273 popup.add(new JMenuItem(type.name())).addActionListener(
278 public void actionPerformed(ActionEvent e)
280 currentservice.getResultDataTypes().set(rdatasel, type);
281 initGuiWith(currentservice);
282 rdata.setSelectedIndex(rdatasel);
286 popup.show(rdata, mouse.getX(), mouse.getY());
291 protected void rdataAdd_actionPerformed(ActionEvent e)
294 if ((p = rdata.getSelectedIndex()) > -1)
296 currentservice.getResultDataTypes().add(p + 1, JvDataType.ANNOTATION);
300 currentservice.addResultDatatype(JvDataType.ANNOTATION);
302 initGuiWith(currentservice);
303 rdata.setSelectedIndex(p == -1 ? currentservice.getResultDataTypes()
304 .size() - 1 : p + 1);
308 protected void rdataNdown_actionPerformed(ActionEvent e)
311 if ((p = rdata.getSelectedIndex()) > -1 && p < _rparam.size() - 1)
313 List<JvDataType> rtypes = currentservice.getResultDataTypes();
314 JvDataType below = rtypes.get(p + 1);
315 rtypes.set(p + 1, rtypes.get(p));
316 rtypes.set(p, below);
317 initGuiWith(currentservice);
318 rdata.setSelectedIndex(p + 1);
323 protected void rdataNup_actionPerformed(ActionEvent e)
326 if ((p = rdata.getSelectedIndex()) > 0)
328 List<JvDataType> rtypes = currentservice.getResultDataTypes();
329 JvDataType above = rtypes.get(p - 1);
330 rtypes.set(p - 1, rtypes.get(p));
331 rtypes.set(p, above);
332 initGuiWith(currentservice);
333 rdata.setSelectedIndex(p - 1);
338 protected void rdataRem_actionPerformed(ActionEvent e)
340 if (rdata.getSelectedIndex() > -1)
342 currentservice.getResultDataTypes().remove(rdata.getSelectedIndex());
343 initGuiWith(currentservice);
347 private boolean updateServiceFromGui()
349 Map<String, InputType> inputTypes = new HashMap<String, InputType>();
350 StringBuffer warnings = new StringBuffer();
351 for (String its : _iparam)
353 Matcher mtch = Pattern.compile("(\\S+)\\s(\\S+):\\[(.+)]").matcher(
357 if (!RestServiceDescription.parseTypeString(mtch.group(2) + ":"
358 + mtch.group(3), mtch.group(1), mtch.group(2),
359 mtch.group(3), inputTypes, warnings))
362 .println("IMPLEMENTATION PROBLEM: Cannot parse RestService input parameter string '"
363 + its + "'" + "\n" + warnings);
367 char gc = gapChar.getSelectedItem() == null ? ' ' : ((String) gapChar
368 .getSelectedItem()).charAt(0);
369 RestServiceDescription newService = new RestServiceDescription(
370 (String) action.getSelectedItem(), descr.getText().trim(), name
371 .getText().trim(), url.getText().trim(), urlsuff
372 .getText().trim(), inputTypes, hSeparable.isSelected(),
373 vSeparable.isSelected(), gc);
375 if (newService.isValid())
377 for (String its : _rparam)
382 dtype = JvDataType.valueOf(its);
383 newService.addResultDatatype(dtype);
384 } catch (Throwable x)
388 .println("IMPLEMENTATION PROBLEM: Cannot parse RestService output parameter string '"
389 + its + "'" + "\n" + warnings);
392 currentservice = newService;
398 .println("IMPLEMENTATION PROBLEM: Restservice generated from GUI is invalid\n"
405 protected void refreshCutnPaste(boolean reparse)
407 if (!reparse && currentservice.isValid())
409 urldesc.setText(currentservice.toString());
410 parseWarnings.setVisible(false);
416 String txt = urldesc.getText().trim();
417 if (txt.length() > 0)
419 RestServiceDescription rsd = null;
422 rsd = new RestServiceDescription(txt);
425 parseWarnings.setVisible(false);
426 parseRes.setText("");
427 initGuiWith(currentservice = rsd);
431 parseRes.setText("Parsing failed. Syntax errors shown below\n"
432 + rsd.getInvalidMessage());
433 parseWarnings.setVisible(true);
435 } catch (Throwable e)
438 parseRes.setText("\nParsing failed. An unrecoverable exception was thrown:\n"
440 parseWarnings.setVisible(true);
449 public static void main(String[] args)
451 if (args.length == 0)
453 new Thread(new Runnable()
455 boolean visible = true;
459 boolean nulserv = true;
462 final Thread runner = Thread.currentThread();
463 JFrame df = new JFrame();
464 df.getContentPane().setLayout(new BorderLayout());
465 df.getContentPane().add(
466 (nulserv = !nulserv) ? new RestServiceEditorPane(
467 jalview.ws.rest.RestClient
468 .makeShmmrRestClient()
469 .getRestDescription())
470 : new RestServiceEditorPane(),
471 BorderLayout.CENTER);
472 df.setBounds(100, 100, 600, 400);
473 df.addComponentListener(new ComponentListener()
477 public void componentShown(ComponentEvent e)
483 public void componentResized(ComponentEvent e)
489 public void componentMoved(ComponentEvent e)
495 public void componentHidden(ComponentEvent e)
508 } catch (Exception x)
521 String finalService = null;
523 public void showDialog(String title)
525 if (oldservice != null)
527 finalService = oldservice.toString();
529 JalviewDialog jvd = new JalviewDialog()
533 protected void raiseClosed()
535 // TODO Auto-generated method stub
540 protected void okPressed()
542 updateServiceFromGui();
543 finalService = currentservice.toString();
547 protected void cancelPressed()
552 JPanel pane = new JPanel(new BorderLayout()), okcancel = new JPanel(
554 pane.add(this, BorderLayout.CENTER);
555 okcancel.add(jvd.ok);
556 okcancel.add(jvd.cancel);
557 pane.add(okcancel, BorderLayout.SOUTH);
558 jvd.initDialogFrame(pane, true, true, title, 600, 350);
562 public String getEditedRestService()