2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.io.packed.DataProvider.JvDataType;
24 import jalview.jbgui.GRestServiceEditorPane;
25 import jalview.util.MessageManager;
26 import jalview.ws.rest.InputType;
27 import jalview.ws.rest.RestServiceDescription;
29 import java.awt.BorderLayout;
30 import java.awt.FlowLayout;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ActionListener;
33 import java.awt.event.ComponentEvent;
34 import java.awt.event.ComponentListener;
35 import java.awt.event.KeyEvent;
36 import java.awt.event.KeyListener;
37 import java.awt.event.MouseEvent;
38 import java.util.HashMap;
39 import java.util.List;
41 import java.util.Vector;
42 import java.util.regex.Matcher;
43 import java.util.regex.Pattern;
45 import javax.swing.JFrame;
46 import javax.swing.JMenuItem;
47 import javax.swing.JPanel;
48 import javax.swing.JPopupMenu;
49 import javax.swing.event.ChangeEvent;
50 import javax.swing.event.ChangeListener;
52 public class RestServiceEditorPane extends GRestServiceEditorPane
55 * the latest version of the service definition.
57 jalview.ws.rest.RestServiceDescription currentservice = null;
60 * original service passed to editor if we are modifying an existing service
63 jalview.ws.rest.RestServiceDescription oldservice = null;
65 public RestServiceEditorPane()
68 // begin with initial text description box enabled.
69 urldesc.addKeyListener(new KeyListener()
72 public void keyTyped(KeyEvent e)
77 public void keyReleased(KeyEvent e)
79 refreshCutnPaste(true);
83 public void keyPressed(KeyEvent e)
88 panels.addChangeListener(new ChangeListener()
92 * last panel selected - used to decide whether the service or the GUI has
98 public void stateChanged(ChangeEvent e)
100 if (lastComp != paste)
102 updateServiceFromGui();
103 refreshCutnPaste(false);
107 refreshCutnPaste(true);
109 lastComp = panels.getSelectedComponent();
113 currentservice = new RestServiceDescription("Analysis",
114 "service description", "service name", "http://localhost/", "",
115 null, false, false, '-');
116 initGuiWith(currentservice);
117 refreshCutnPaste(false);
121 public RestServiceEditorPane(RestServiceDescription toedit)
125 if (oldservice != null)
127 currentservice = new RestServiceDescription(toedit);
131 currentservice = new RestServiceDescription("Analysis",
132 "service description", "service name", "http://localhost/",
133 "", null, false, false, '-');
135 initGuiWith(currentservice);
136 refreshCutnPaste(false);
141 * refresh the buttons based on model state
143 public void updateButtons()
145 cancelButton.setEnabled(true);
146 okButton.setEnabled(currentservice != null && currentservice.isValid());
150 Vector<String> _iparam = new Vector<String>();
152 Vector<String> _rparam = new Vector<String>();
155 * generate an editable URL service string and parameter list using the
158 * @param currentservice2
160 private void initGuiWith(RestServiceDescription currentservice)
164 action.removeAllItems();
165 action.addItem("Alignment");
166 action.addItem("Analysis");
167 gapChar.removeAllItems();
168 gapChar.addItem(".");
169 gapChar.addItem(" ");
170 gapChar.addItem("-");
171 if (currentservice == null)
177 action.setSelectedItem("Analysis");
178 gapChar.setSelectedItem("-");
182 name.setText(currentservice.getName());
183 descr.setText(currentservice.getDescription());
184 url.setText(currentservice.getPostUrl());
185 urlsuff.setText(currentservice.getUrlSuffix());
186 for (Map.Entry<String, InputType> inparam : currentservice
187 .getInputParams().entrySet())
189 _iparam.add(inparam.getKey() + " "
190 + inparam.getValue().getURLtokenPrefix() + ":"
191 + inparam.getValue().getURLEncodedParameter().toString());
194 for (JvDataType oparam : currentservice.getResultDataTypes())
196 _rparam.add(oparam.name());
198 iprms.setListData(_iparam);
199 rdata.setListData(_rparam);
201 action.setSelectedItem(currentservice.getAction());
203 gapChar.setSelectedItem("" + currentservice.getGapCharacter());
208 private String getSelectedInputToken()
210 if (iprms.getSelectedIndex() > -1)
212 String toktoedit = (String) iprms.getSelectedValue();
213 toktoedit = toktoedit.substring(0, toktoedit.indexOf(" "));
220 protected void iprmListSelection_doubleClicked()
222 String toktoedit = getSelectedInputToken();
223 if (toktoedit != null)
225 InputType toedit = currentservice.getInputParams().get(toktoedit);
226 String oldParam = toktoedit;
227 RestInputParamEditDialog dialog = new RestInputParamEditDialog(this,
228 currentservice, toedit);
229 if (dialog.wasUpdated())
231 currentservice.getInputParams().remove(oldParam);
232 currentservice.getInputParams().put(dialog.current.token,
234 initGuiWith(currentservice);
241 protected void iprmsAdd_actionPerformed(ActionEvent e)
243 RestInputParamEditDialog dialog = new RestInputParamEditDialog(this,
244 currentservice, "param"
245 + (1 + currentservice.getInputParams().size()));
246 if (dialog.wasUpdated())
248 currentservice.getInputParams().put(dialog.current.token,
250 initGuiWith(currentservice);
256 protected void iprmsRem_actionPerformed(ActionEvent e)
258 String toktoedit = getSelectedInputToken();
259 if (toktoedit != null)
261 currentservice.getInputParams().remove(toktoedit);
262 initGuiWith(currentservice);
268 protected void rdata_rightClicked(MouseEvent mouse)
270 final int rdatasel = rdata.getSelectedIndex();
273 JPopupMenu popup = new JPopupMenu(MessageManager.getString("label.select_return_type"));
274 for (final JvDataType type : JvDataType.values())
276 popup.add(new JMenuItem(type.name())).addActionListener(
281 public void actionPerformed(ActionEvent e)
283 currentservice.getResultDataTypes().set(rdatasel, type);
284 initGuiWith(currentservice);
285 rdata.setSelectedIndex(rdatasel);
289 popup.show(rdata, mouse.getX(), mouse.getY());
294 protected void rdataAdd_actionPerformed(ActionEvent e)
297 if ((p = rdata.getSelectedIndex()) > -1)
299 currentservice.getResultDataTypes().add(p + 1, JvDataType.ANNOTATION);
303 currentservice.addResultDatatype(JvDataType.ANNOTATION);
305 initGuiWith(currentservice);
306 rdata.setSelectedIndex(p == -1 ? currentservice.getResultDataTypes()
307 .size() - 1 : p + 1);
311 protected void rdataNdown_actionPerformed(ActionEvent e)
314 if ((p = rdata.getSelectedIndex()) > -1 && p < _rparam.size() - 1)
316 List<JvDataType> rtypes = currentservice.getResultDataTypes();
317 JvDataType below = rtypes.get(p + 1);
318 rtypes.set(p + 1, rtypes.get(p));
319 rtypes.set(p, below);
320 initGuiWith(currentservice);
321 rdata.setSelectedIndex(p + 1);
326 protected void rdataNup_actionPerformed(ActionEvent e)
329 if ((p = rdata.getSelectedIndex()) > 0)
331 List<JvDataType> rtypes = currentservice.getResultDataTypes();
332 JvDataType above = rtypes.get(p - 1);
333 rtypes.set(p - 1, rtypes.get(p));
334 rtypes.set(p, above);
335 initGuiWith(currentservice);
336 rdata.setSelectedIndex(p - 1);
341 protected void rdataRem_actionPerformed(ActionEvent e)
343 if (rdata.getSelectedIndex() > -1)
345 currentservice.getResultDataTypes().remove(rdata.getSelectedIndex());
346 initGuiWith(currentservice);
350 private boolean updateServiceFromGui()
352 Map<String, InputType> inputTypes = new HashMap<String, InputType>();
353 StringBuffer warnings = new StringBuffer();
354 for (String its : _iparam)
356 Matcher mtch = Pattern.compile("(\\S+)\\s(\\S+):\\[(.+)]").matcher(
360 if (!RestServiceDescription.parseTypeString(mtch.group(2) + ":"
361 + mtch.group(3), mtch.group(1), mtch.group(2),
362 mtch.group(3), inputTypes, warnings))
365 .println("IMPLEMENTATION PROBLEM: Cannot parse RestService input parameter string '"
366 + its + "'" + "\n" + warnings);
370 char gc = gapChar.getSelectedItem() == null ? ' ' : ((String) gapChar
371 .getSelectedItem()).charAt(0);
372 RestServiceDescription newService = new RestServiceDescription(
373 (String) action.getSelectedItem(), descr.getText().trim(), name
374 .getText().trim(), url.getText().trim(), urlsuff
375 .getText().trim(), inputTypes, hSeparable.isSelected(),
376 vSeparable.isSelected(), gc);
378 if (newService.isValid())
380 for (String its : _rparam)
385 dtype = JvDataType.valueOf(its);
386 newService.addResultDatatype(dtype);
387 } catch (Throwable x)
391 .println("IMPLEMENTATION PROBLEM: Cannot parse RestService output parameter string '"
392 + its + "'" + "\n" + warnings);
395 currentservice = newService;
401 .println("IMPLEMENTATION PROBLEM: Restservice generated from GUI is invalid\n"
408 protected void refreshCutnPaste(boolean reparse)
410 if (!reparse && currentservice.isValid())
412 urldesc.setText(currentservice.toString());
413 parseWarnings.setVisible(false);
419 String txt = urldesc.getText().trim();
420 if (txt.length() > 0)
422 RestServiceDescription rsd = null;
425 rsd = new RestServiceDescription(txt);
428 parseWarnings.setVisible(false);
429 parseRes.setText("");
430 initGuiWith(currentservice = rsd);
434 parseRes.setText(MessageManager.formatMessage("label.parsing_failed_syntax_errors_shown_below_param", new String[]{rsd.getInvalidMessage()}));
435 parseWarnings.setVisible(true);
437 } catch (Throwable e)
440 parseRes.setText(MessageManager.formatMessage("label.parsing_failed_unrecoverable_exception_thrown_param", new String[]{e.toString()}));
441 parseWarnings.setVisible(true);
450 public static void main(String[] args)
452 if (args.length == 0)
454 new Thread(new Runnable()
456 boolean visible = true;
460 boolean nulserv = true;
463 final Thread runner = Thread.currentThread();
464 JFrame df = new JFrame();
465 df.getContentPane().setLayout(new BorderLayout());
466 df.getContentPane().add(
467 (nulserv = !nulserv) ? new RestServiceEditorPane(
468 jalview.ws.rest.RestClient
469 .makeShmmrRestClient()
470 .getRestDescription())
471 : new RestServiceEditorPane(),
472 BorderLayout.CENTER);
473 df.setBounds(100, 100, 600, 400);
474 df.addComponentListener(new ComponentListener()
478 public void componentShown(ComponentEvent e)
484 public void componentResized(ComponentEvent e)
490 public void componentMoved(ComponentEvent e)
496 public void componentHidden(ComponentEvent e)
509 } catch (Exception x)
522 String finalService = null;
524 public void showDialog(String title)
526 if (oldservice != null)
528 finalService = oldservice.toString();
530 JalviewDialog jvd = new JalviewDialog()
534 protected void raiseClosed()
536 // TODO Auto-generated method stub
541 protected void okPressed()
543 updateServiceFromGui();
544 finalService = currentservice.toString();
548 protected void cancelPressed()
553 JPanel pane = new JPanel(new BorderLayout()), okcancel = new JPanel(
555 pane.add(this, BorderLayout.CENTER);
556 okcancel.add(jvd.ok);
557 okcancel.add(jvd.cancel);
558 pane.add(okcancel, BorderLayout.SOUTH);
559 jvd.initDialogFrame(pane, true, true, title, 600, 350);
563 public String getEditedRestService()