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.util.MessageManager;
24 import jalview.ws.rest.InputType;
25 import jalview.ws.rest.RestServiceDescription;
27 import java.awt.BorderLayout;
28 import java.awt.FlowLayout;
29 import java.awt.event.ActionEvent;
30 import java.awt.event.ActionListener;
31 import java.awt.event.ComponentEvent;
32 import java.awt.event.ComponentListener;
33 import java.awt.event.KeyEvent;
34 import java.awt.event.KeyListener;
35 import java.awt.event.MouseEvent;
36 import java.util.HashMap;
37 import java.util.List;
39 import java.util.Vector;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
43 import javax.swing.JFrame;
44 import javax.swing.JMenuItem;
45 import javax.swing.JPanel;
46 import javax.swing.JPopupMenu;
47 import javax.swing.event.ChangeEvent;
48 import javax.swing.event.ChangeListener;
50 public class RestServiceEditorPane extends GRestServiceEditorPane
53 * the latest version of the service definition.
55 jalview.ws.rest.RestServiceDescription currentservice = null;
58 * original service passed to editor if we are modifying an existing service
61 jalview.ws.rest.RestServiceDescription oldservice = null;
63 public RestServiceEditorPane()
66 // begin with initial text description box enabled.
67 urldesc.addKeyListener(new KeyListener()
70 public void keyTyped(KeyEvent e)
75 public void keyReleased(KeyEvent e)
77 refreshCutnPaste(true);
81 public void keyPressed(KeyEvent e)
86 panels.addChangeListener(new ChangeListener()
90 * last panel selected - used to decide whether the service or the GUI has
96 public void stateChanged(ChangeEvent e)
98 if (lastComp != paste)
100 updateServiceFromGui();
101 refreshCutnPaste(false);
105 refreshCutnPaste(true);
107 lastComp = panels.getSelectedComponent();
111 currentservice = new RestServiceDescription("Analysis",
112 "service description", "service name", "http://localhost/", "",
113 null, false, false, '-');
114 initGuiWith(currentservice);
115 refreshCutnPaste(false);
119 public RestServiceEditorPane(RestServiceDescription toedit)
123 if (oldservice != null)
125 currentservice = new RestServiceDescription(toedit);
129 currentservice = new RestServiceDescription("Analysis",
130 "service description", "service name", "http://localhost/",
131 "", null, false, false, '-');
133 initGuiWith(currentservice);
134 refreshCutnPaste(false);
139 * refresh the buttons based on model state
141 public void updateButtons()
143 cancelButton.setEnabled(true);
144 okButton.setEnabled(currentservice != null && currentservice.isValid());
148 Vector<String> _iparam = new Vector<String>();
150 Vector<String> _rparam = new Vector<String>();
153 * generate an editable URL service string and parameter list using the
156 * @param currentservice2
158 private void initGuiWith(RestServiceDescription currentservice)
162 action.removeAllItems();
163 action.addItem("Alignment");
164 action.addItem("Analysis");
165 gapChar.removeAllItems();
166 gapChar.addItem(".");
167 gapChar.addItem(" ");
168 gapChar.addItem("-");
169 if (currentservice == null)
175 action.setSelectedItem("Analysis");
176 gapChar.setSelectedItem("-");
180 name.setText(currentservice.getName());
181 descr.setText(currentservice.getDescription());
182 url.setText(currentservice.getPostUrl());
183 urlsuff.setText(currentservice.getUrlSuffix());
184 for (Map.Entry<String, InputType> inparam : currentservice
185 .getInputParams().entrySet())
187 _iparam.add(inparam.getKey() + " "
188 + inparam.getValue().getURLtokenPrefix() + ":"
189 + inparam.getValue().getURLEncodedParameter().toString());
192 for (JvDataType oparam : currentservice.getResultDataTypes())
194 _rparam.add(oparam.name());
196 iprms.setListData(_iparam);
197 rdata.setListData(_rparam);
199 action.setSelectedItem(currentservice.getAction());
201 gapChar.setSelectedItem("" + currentservice.getGapCharacter());
206 private String getSelectedInputToken()
208 if (iprms.getSelectedIndex() > -1)
210 String toktoedit = (String) iprms.getSelectedValue();
211 toktoedit = toktoedit.substring(0, toktoedit.indexOf(" "));
218 protected void iprmListSelection_doubleClicked()
220 String toktoedit = getSelectedInputToken();
221 if (toktoedit != null)
223 InputType toedit = currentservice.getInputParams().get(toktoedit);
224 String oldParam = toktoedit;
225 RestInputParamEditDialog dialog = new RestInputParamEditDialog(this,
226 currentservice, toedit);
227 if (dialog.wasUpdated())
229 currentservice.getInputParams().remove(oldParam);
230 currentservice.getInputParams().put(dialog.current.token,
232 initGuiWith(currentservice);
239 protected void iprmsAdd_actionPerformed(ActionEvent e)
241 RestInputParamEditDialog dialog = new RestInputParamEditDialog(this,
242 currentservice, "param"
243 + (1 + currentservice.getInputParams().size()));
244 if (dialog.wasUpdated())
246 currentservice.getInputParams().put(dialog.current.token,
248 initGuiWith(currentservice);
254 protected void iprmsRem_actionPerformed(ActionEvent e)
256 String toktoedit = getSelectedInputToken();
257 if (toktoedit != null)
259 currentservice.getInputParams().remove(toktoedit);
260 initGuiWith(currentservice);
266 protected void rdata_rightClicked(MouseEvent mouse)
268 final int rdatasel = rdata.getSelectedIndex();
271 JPopupMenu popup = new JPopupMenu(MessageManager.getString("label.select_return_type"));
272 for (final JvDataType type : JvDataType.values())
274 popup.add(new JMenuItem(type.name())).addActionListener(
279 public void actionPerformed(ActionEvent e)
281 currentservice.getResultDataTypes().set(rdatasel, type);
282 initGuiWith(currentservice);
283 rdata.setSelectedIndex(rdatasel);
287 popup.show(rdata, mouse.getX(), mouse.getY());
292 protected void rdataAdd_actionPerformed(ActionEvent e)
295 if ((p = rdata.getSelectedIndex()) > -1)
297 currentservice.getResultDataTypes().add(p + 1, JvDataType.ANNOTATION);
301 currentservice.addResultDatatype(JvDataType.ANNOTATION);
303 initGuiWith(currentservice);
304 rdata.setSelectedIndex(p == -1 ? currentservice.getResultDataTypes()
305 .size() - 1 : p + 1);
309 protected void rdataNdown_actionPerformed(ActionEvent e)
312 if ((p = rdata.getSelectedIndex()) > -1 && p < _rparam.size() - 1)
314 List<JvDataType> rtypes = currentservice.getResultDataTypes();
315 JvDataType below = rtypes.get(p + 1);
316 rtypes.set(p + 1, rtypes.get(p));
317 rtypes.set(p, below);
318 initGuiWith(currentservice);
319 rdata.setSelectedIndex(p + 1);
324 protected void rdataNup_actionPerformed(ActionEvent e)
327 if ((p = rdata.getSelectedIndex()) > 0)
329 List<JvDataType> rtypes = currentservice.getResultDataTypes();
330 JvDataType above = rtypes.get(p - 1);
331 rtypes.set(p - 1, rtypes.get(p));
332 rtypes.set(p, above);
333 initGuiWith(currentservice);
334 rdata.setSelectedIndex(p - 1);
339 protected void rdataRem_actionPerformed(ActionEvent e)
341 if (rdata.getSelectedIndex() > -1)
343 currentservice.getResultDataTypes().remove(rdata.getSelectedIndex());
344 initGuiWith(currentservice);
348 private boolean updateServiceFromGui()
350 Map<String, InputType> inputTypes = new HashMap<String, InputType>();
351 StringBuffer warnings = new StringBuffer();
352 for (String its : _iparam)
354 Matcher mtch = Pattern.compile("(\\S+)\\s(\\S+):\\[(.+)]").matcher(
358 if (!RestServiceDescription.parseTypeString(mtch.group(2) + ":"
359 + mtch.group(3), mtch.group(1), mtch.group(2),
360 mtch.group(3), inputTypes, warnings))
363 .println("IMPLEMENTATION PROBLEM: Cannot parse RestService input parameter string '"
364 + its + "'" + "\n" + warnings);
368 char gc = gapChar.getSelectedItem() == null ? ' ' : ((String) gapChar
369 .getSelectedItem()).charAt(0);
370 RestServiceDescription newService = new RestServiceDescription(
371 (String) action.getSelectedItem(), descr.getText().trim(), name
372 .getText().trim(), url.getText().trim(), urlsuff
373 .getText().trim(), inputTypes, hSeparable.isSelected(),
374 vSeparable.isSelected(), gc);
376 if (newService.isValid())
378 for (String its : _rparam)
383 dtype = JvDataType.valueOf(its);
384 newService.addResultDatatype(dtype);
385 } catch (Throwable x)
389 .println("IMPLEMENTATION PROBLEM: Cannot parse RestService output parameter string '"
390 + its + "'" + "\n" + warnings);
393 currentservice = newService;
399 .println("IMPLEMENTATION PROBLEM: Restservice generated from GUI is invalid\n"
406 protected void refreshCutnPaste(boolean reparse)
408 if (!reparse && currentservice.isValid())
410 urldesc.setText(currentservice.toString());
411 parseWarnings.setVisible(false);
417 String txt = urldesc.getText().trim();
418 if (txt.length() > 0)
420 RestServiceDescription rsd = null;
423 rsd = new RestServiceDescription(txt);
426 parseWarnings.setVisible(false);
427 parseRes.setText("");
428 initGuiWith(currentservice = rsd);
432 parseRes.setText(MessageManager.formatMessage("label.parsing_failed_syntax_errors_shown_below_param", new String[]{rsd.getInvalidMessage()}));
433 parseWarnings.setVisible(true);
435 } catch (Throwable e)
438 parseRes.setText(MessageManager.formatMessage("label.parsing_failed_unrecoverable_exception_thrown_param", new String[]{e.toString()}));
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()