JAL-715 - remove platform dependent includes
[jalview.git] / src / jalview / gui / RestInputParamEditDialog.java
1 /*******************************************************************************
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
4  *
5  * This file is part of Jalview.
6  *
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.
10  *
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.
15  *
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  *******************************************************************************/
18 package jalview.gui;
19
20 import java.util.ArrayList;
21 import java.util.Hashtable;
22
23 import javax.swing.JDialog;
24 import javax.swing.JInternalFrame;
25 import javax.swing.JOptionPane;
26 import javax.swing.JPanel;
27 import javax.swing.event.ListSelectionEvent;
28
29 import net.miginfocom.swing.MigLayout;
30
31 import jalview.jbgui.GRestInputParamEditDialog;
32 import jalview.ws.params.ArgumentI;
33 import jalview.ws.params.InvalidArgumentException;
34 import jalview.ws.params.OptionI;
35 import jalview.ws.params.ParameterI;
36 import jalview.ws.rest.InputType;
37 import jalview.ws.rest.RestServiceDescription;
38
39 public class RestInputParamEditDialog extends GRestInputParamEditDialog
40         implements OptsParametersContainerI
41 {
42   Hashtable<String, Class> typeclass = new Hashtable<String, Class>();
43
44   Hashtable<String, ArrayList<JPanel>> typeopts = new Hashtable<String, ArrayList<JPanel>>();
45
46   Hashtable<String,OptsAndParamsPage> opanps = new Hashtable<String, OptsAndParamsPage>();
47   
48   private InputType getTypeFor(String name)
49   {
50     try
51     {
52       return (InputType) (typeclass.get(name).getConstructor()
53               .newInstance(null));
54     } catch (Throwable x)
55     {
56       System.err
57               .println("Unexpected exception when instantiating rest input type.");
58       x.printStackTrace();
59     }
60     return null;
61   }
62
63   int reply;
64   JalviewDialog frame = new JalviewDialog()
65   {
66     
67     @Override
68     protected void raiseClosed()
69     {
70       
71     }
72     
73     @Override
74     protected void okPressed()
75     {
76       reply=JOptionPane.OK_OPTION; 
77     }
78     
79     @Override
80     protected void cancelPressed()
81     {
82       reply=JOptionPane.CANCEL_OPTION; 
83       
84     }
85   };
86   InputType old,current;
87   public RestInputParamEditDialog(
88           RestServiceEditorPane restServiceEditorPane,
89           RestServiceDescription currentservice, InputType toedit)
90   {
91     initFor(restServiceEditorPane, currentservice, toedit);
92     frame.waitForInput();
93     // TODO: warn user if they are about to overwrite an existing parameter because they have used the same name when editing a different parameter.
94     // TODO: make any press of the return key cause 'OK' to be pressed
95   }
96   private void initFor(RestServiceEditorPane restServiceEditorPane,
97           RestServiceDescription currentservice, InputType toedit)
98   {
99     okcancel.add(frame.cancel);
100     okcancel.add(frame.ok);
101     frame.initDialogFrame(dpane, true, true, "Edit parameter for service "+currentservice.getName(), 600,800);
102     
103     initTypeLists();
104     reply=JOptionPane.CANCEL_OPTION;
105     old = toedit;
106     current = null;
107     if (old!=null) {
108       setStateFor(old);
109     }
110     updated = updated && reply==JOptionPane.OK_OPTION;
111     frame.validate();
112   }
113
114   public RestInputParamEditDialog(
115           RestServiceEditorPane restServiceEditorPane,
116           RestServiceDescription currentservice, String string)
117   {
118     initFor(restServiceEditorPane, currentservice,null);
119     tok.setText(string);
120     frame.waitForInput();
121   }
122
123   private void setStateFor(InputType current)
124   {
125     tok.setText(current.token);
126     OptsAndParamsPage opanp=opanps.get(current.getURLtokenPrefix());
127     for (OptionI ops:current.getOptions())
128     {
129       if (ops instanceof ParameterI)
130       {
131         opanp.setParameter((ParameterI) ops);
132       } else {
133         if (ops.getValue()!=null && ops.getValue().length()>0) {
134           opanp.selectOption(ops, ops.getValue());
135         }
136       }
137     }
138     typeList.setSelectedValue(current.getURLtokenPrefix(), true);
139     type_SelectionChangedActionPerformed(null);
140   }
141   private void updateCurrentType()
142   {
143     if (typeList.getSelectedValue()!=null) {
144     InputType newType = getTypeFor((String)typeList.getSelectedValue());
145     if (newType!=null) {
146       newType.token = tok.getText().trim();
147       try {
148         newType.configureFromArgumentI(opanps.get(newType.getURLtokenPrefix()).getCurrentSettings());
149         current=newType;
150         updated=true;
151       } catch (InvalidArgumentException ex) {
152         System.err.println("IMPLEMENTATION ERROR: Invalid argument for type : "+typeList.getSelectedValue()+"\n");
153         ex.printStackTrace();
154       }
155     }
156     }
157     
158   }
159   private void initTypeLists()
160   {
161     ArrayList<String> types=new ArrayList<String>();
162     // populate type list
163     for (Class type : RestServiceDescription.getInputTypes())
164     {
165
166       InputType jtype = null;
167       try
168       {
169         JPanel inopts = new JPanel(new MigLayout());
170         ArrayList<JPanel> opts = new ArrayList<JPanel>(), prms = new ArrayList<JPanel>();
171         jtype = (InputType) (type.getConstructor().newInstance(null));
172         typeclass.put(jtype.getURLtokenPrefix(), type);
173         // and populate parameters from this type
174         OptsAndParamsPage opanp = new OptsAndParamsPage(this,true);
175         opanps.put(jtype.getURLtokenPrefix(),opanp);
176         for (OptionI opt : jtype.getOptions())
177         {
178
179           if (opt instanceof ParameterI)
180           {
181             prms.add(opanp.addParameter((ParameterI) opt));
182           }
183           else
184           {
185             opts.add(opanp.addOption(opt));
186           }
187         }
188         // then tag the params at the end of the options.
189         for (JPanel pnl : prms)
190         {
191           opts.add(pnl);
192         }
193         typeopts.put(jtype.getURLtokenPrefix(), opts);
194         types.add(jtype.getURLtokenPrefix());
195       } catch (Throwable x)
196       {
197         System.err
198                 .println("Unexpected exception when instantiating rest input type.");
199         x.printStackTrace();
200       }
201     }
202     typeList.setListData(types.toArray());
203     
204   }
205
206   @Override
207   protected void type_SelectionChangedActionPerformed(ListSelectionEvent e)
208   {
209     options.removeAll();
210     String typen = (String) typeList.getSelectedValue();
211     if (typeopts.get(typen) != null)
212     {
213       for (JPanel opt : typeopts.get(typen))
214       {
215         opt.setOpaque(true);
216         options.add(opt,"wrap");
217       }
218       options.invalidate();
219       optionsPanel.setVisible(true);
220     }
221     else
222     {
223       optionsPanel.setVisible(false);
224     }
225     dpane.revalidate();
226     updateCurrentType();
227   }
228
229   boolean updated=false;
230   public boolean wasUpdated()
231   {
232     return updated;
233   }
234
235   @Override
236   public void refreshParamLayout()
237   {
238     options.invalidate();
239     dpane.revalidate();
240   }
241
242   @Override
243   protected void tokChanged_actionPerformed()
244   {
245     if (tok.getText().trim().length()>0)
246     {
247       if (current!=null)
248       {
249         current.token = tok.getText().trim();
250         updated = true; 
251       } 
252     }
253   }
254   @Override
255   public void argSetModified(Object modifiedElement, boolean b)
256   {
257     updated = updated | b;
258     if (updated)
259     {
260       updateCurrentType();
261     }
262   }
263
264 }