JAL-1645 JAL-1355 UI/i18n: added missing messages, moved ‘…’ into action messages...
[jalview.git] / src / MCview / PDBViewer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10  * of the License, or (at your option) any later version.
11  *  
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.
16  * 
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.
20  */
21 package MCview;
22
23 import jalview.datamodel.PDBEntry;
24 import jalview.datamodel.SequenceI;
25 import jalview.gui.AlignmentPanel;
26 import jalview.gui.Desktop;
27 import jalview.gui.OOMWarning;
28 import jalview.gui.UserDefinedColours;
29 import jalview.io.JalviewFileChooser;
30 import jalview.io.JalviewFileView;
31 import jalview.schemes.BuriedColourScheme;
32 import jalview.schemes.HelixColourScheme;
33 import jalview.schemes.HydrophobicColourScheme;
34 import jalview.schemes.StrandColourScheme;
35 import jalview.schemes.TaylorColourScheme;
36 import jalview.schemes.TurnColourScheme;
37 import jalview.schemes.UserColourScheme;
38 import jalview.schemes.ZappoColourScheme;
39 import jalview.util.MessageManager;
40 import jalview.ws.ebi.EBIFetchClient;
41
42 import java.awt.event.ActionEvent;
43 import java.awt.event.ActionListener;
44 import java.awt.event.ItemEvent;
45 import java.awt.event.ItemListener;
46 import java.awt.event.KeyAdapter;
47 import java.awt.event.KeyEvent;
48 import java.awt.event.MouseAdapter;
49 import java.awt.event.MouseEvent;
50 import java.io.BufferedReader;
51 import java.io.File;
52 import java.io.FileOutputStream;
53 import java.io.FileReader;
54 import java.io.PrintWriter;
55
56 import javax.swing.ButtonGroup;
57 import javax.swing.JCheckBoxMenuItem;
58 import javax.swing.JColorChooser;
59 import javax.swing.JInternalFrame;
60 import javax.swing.JMenu;
61 import javax.swing.JMenuBar;
62 import javax.swing.JMenuItem;
63 import javax.swing.JOptionPane;
64 import javax.swing.JRadioButtonMenuItem;
65 import javax.swing.SwingUtilities;
66
67 public class PDBViewer extends JInternalFrame implements Runnable
68 {
69
70   /**
71    * The associated sequence in an alignment
72    */
73   PDBCanvas pdbcanvas;
74
75   PDBEntry pdbentry;
76
77   SequenceI[] seq;
78
79   String[] chains;
80
81   AlignmentPanel ap;
82
83   String protocol;
84
85   String tmpPDBFile;
86
87   public PDBViewer(PDBEntry pdbentry, SequenceI[] seq, String[] chains,
88           AlignmentPanel ap, String protocol)
89   {
90     this.pdbentry = pdbentry;
91     this.seq = seq;
92     this.chains = chains;
93     this.ap = ap;
94     this.protocol = protocol;
95
96     try
97     {
98       jbInit();
99     } catch (Exception ex)
100     {
101       ex.printStackTrace();
102     }
103
104     StringBuffer title = new StringBuffer(seq[0].getName() + ":"
105             + pdbentry.getFile());
106
107     pdbcanvas = new PDBCanvas();
108
109     setContentPane(pdbcanvas);
110
111     if (pdbentry.getFile() != null)
112     {
113       try
114       {
115         tmpPDBFile = pdbentry.getFile();
116         PDBfile pdbfile = new PDBfile(false, false, false, tmpPDBFile,
117                 jalview.io.AppletFormatAdapter.FILE);
118
119         pdbcanvas.init(pdbentry, seq, chains, ap, protocol);
120
121       } catch (java.io.IOException ex)
122       {
123         ex.printStackTrace();
124       }
125     }
126     else
127     {
128       Thread worker = new Thread(this);
129       worker.start();
130     }
131
132     if (pdbentry.getProperty() != null)
133     {
134       if (pdbentry.getProperty().get("method") != null)
135       {
136         title.append(" Method: ");
137         title.append(pdbentry.getProperty().get("method"));
138       }
139       if (pdbentry.getProperty().get("chains") != null)
140       {
141         title.append(" Chain:");
142         title.append(pdbentry.getProperty().get("chains"));
143       }
144     }
145     Desktop.addInternalFrame(this, title.toString(), 400, 400);
146   }
147
148   public void run()
149   {
150     try
151     {
152       EBIFetchClient ebi = new EBIFetchClient();
153       String query = "pdb:" + pdbentry.getId();
154       pdbentry.setFile(ebi.fetchDataAsFile(query, "default", "raw")
155               .getAbsolutePath());
156
157       if (pdbentry.getFile() != null)
158       {
159         pdbcanvas.init(pdbentry, seq, chains, ap, protocol);
160       }
161     } catch (Exception ex)
162     {
163       pdbcanvas.errorMessage = "Error retrieving file: " + pdbentry.getId();
164       ex.printStackTrace();
165     }
166   }
167
168   private void jbInit() throws Exception
169   {
170     this.addKeyListener(new KeyAdapter()
171     {
172       public void keyPressed(KeyEvent evt)
173       {
174         pdbcanvas.keyPressed(evt);
175       }
176     });
177
178     this.setJMenuBar(jMenuBar1);
179     fileMenu.setText(MessageManager.getString("action.file"));
180     coloursMenu.setText(MessageManager.getString("label.colours"));
181     saveMenu.setActionCommand(MessageManager.getString("action.save_image"));
182     saveMenu.setText(MessageManager.getString("action.save_as"));
183     png.setText("PNG");
184     png.addActionListener(new ActionListener()
185     {
186       public void actionPerformed(ActionEvent e)
187       {
188         png_actionPerformed(e);
189       }
190     });
191     eps.setText("EPS");
192     eps.addActionListener(new ActionListener()
193     {
194       public void actionPerformed(ActionEvent e)
195       {
196         eps_actionPerformed(e);
197       }
198     });
199     mapping.setText(MessageManager.getString("label.view_mapping"));
200     mapping.addActionListener(new ActionListener()
201     {
202       public void actionPerformed(ActionEvent e)
203       {
204         mapping_actionPerformed(e);
205       }
206     });
207     wire.setText(MessageManager.getString("label.wireframe"));
208     wire.addActionListener(new ActionListener()
209     {
210       public void actionPerformed(ActionEvent e)
211       {
212         wire_actionPerformed(e);
213       }
214     });
215     depth.setSelected(true);
216     depth.setText(MessageManager.getString("label.depthcue"));
217     depth.addActionListener(new ActionListener()
218     {
219       public void actionPerformed(ActionEvent e)
220       {
221         depth_actionPerformed(e);
222       }
223     });
224     zbuffer.setSelected(true);
225     zbuffer.setText(MessageManager.getString("label.z_buffering"));
226     zbuffer.addActionListener(new ActionListener()
227     {
228       public void actionPerformed(ActionEvent e)
229       {
230         zbuffer_actionPerformed(e);
231       }
232     });
233     charge.setText(MessageManager.getString("label.charge_cysteine"));
234     charge.addActionListener(new ActionListener()
235     {
236       public void actionPerformed(ActionEvent e)
237       {
238         charge_actionPerformed(e);
239       }
240     });
241     chain.setText(MessageManager.getString("action.by_chain"));
242     chain.addActionListener(new ActionListener()
243     {
244       public void actionPerformed(ActionEvent e)
245       {
246         chain_actionPerformed(e);
247       }
248     });
249     seqButton.setSelected(true);
250     seqButton.setText(MessageManager.getString("action.by_sequence"));
251     seqButton.addActionListener(new ActionListener()
252     {
253       public void actionPerformed(ActionEvent e)
254       {
255         seqButton_actionPerformed(e);
256       }
257     });
258     allchains.setSelected(true);
259     allchains.setText(MessageManager.getString("label.show_all_chains"));
260     allchains.addItemListener(new ItemListener()
261     {
262       public void itemStateChanged(ItemEvent e)
263       {
264         allchains_itemStateChanged(e);
265       }
266     });
267     zappo.setText(MessageManager.getString("label.zappo"));
268     zappo.addActionListener(new ActionListener()
269     {
270       public void actionPerformed(ActionEvent e)
271       {
272         zappo_actionPerformed(e);
273       }
274     });
275     taylor.setText(MessageManager.getString("label.taylor"));
276     taylor.addActionListener(new ActionListener()
277     {
278       public void actionPerformed(ActionEvent e)
279       {
280         taylor_actionPerformed(e);
281       }
282     });
283     hydro.setText(MessageManager.getString("label.hydrophobicity"));
284     hydro.addActionListener(new ActionListener()
285     {
286       public void actionPerformed(ActionEvent e)
287       {
288         hydro_actionPerformed(e);
289       }
290     });
291     helix.setText(MessageManager.getString("label.helix_propensity"));
292     helix.addActionListener(new ActionListener()
293     {
294       public void actionPerformed(ActionEvent e)
295       {
296         helix_actionPerformed(e);
297       }
298     });
299     strand.setText(MessageManager.getString("label.strand_propensity"));
300     strand.addActionListener(new ActionListener()
301     {
302       public void actionPerformed(ActionEvent e)
303       {
304         strand_actionPerformed(e);
305       }
306     });
307     turn.setText(MessageManager.getString("label.turn_propensity"));
308     turn.addActionListener(new ActionListener()
309     {
310       public void actionPerformed(ActionEvent e)
311       {
312         turn_actionPerformed(e);
313       }
314     });
315     buried.setText(MessageManager.getString("label.buried_index"));
316     buried.addActionListener(new ActionListener()
317     {
318       public void actionPerformed(ActionEvent e)
319       {
320         buried_actionPerformed(e);
321       }
322     });
323     user.setText(MessageManager.getString("action.user_defined"));
324     user.addActionListener(new ActionListener()
325     {
326       public void actionPerformed(ActionEvent e)
327       {
328         user_actionPerformed(e);
329       }
330     });
331     viewMenu.setText(MessageManager.getString("action.view"));
332     background.setText(MessageManager.getString("action.background_colour"));
333     background.addActionListener(new ActionListener()
334     {
335       public void actionPerformed(ActionEvent e)
336       {
337         background_actionPerformed(e);
338       }
339     });
340     savePDB.setText(MessageManager.getString("label.pdb_file"));
341     savePDB.addActionListener(new ActionListener()
342     {
343       public void actionPerformed(ActionEvent e)
344       {
345         savePDB_actionPerformed(e);
346       }
347     });
348     jMenuBar1.add(fileMenu);
349     jMenuBar1.add(coloursMenu);
350     jMenuBar1.add(viewMenu);
351     fileMenu.add(saveMenu);
352     fileMenu.add(mapping);
353     saveMenu.add(savePDB);
354     saveMenu.add(png);
355     saveMenu.add(eps);
356     coloursMenu.add(seqButton);
357     coloursMenu.add(chain);
358     coloursMenu.add(charge);
359     coloursMenu.add(zappo);
360     coloursMenu.add(taylor);
361     coloursMenu.add(hydro);
362     coloursMenu.add(helix);
363     coloursMenu.add(strand);
364     coloursMenu.add(turn);
365     coloursMenu.add(buried);
366     coloursMenu.add(user);
367     coloursMenu.add(background);
368     ButtonGroup bg = new ButtonGroup();
369     bg.add(seqButton);
370     bg.add(chain);
371     bg.add(charge);
372     bg.add(zappo);
373     bg.add(taylor);
374     bg.add(hydro);
375     bg.add(helix);
376     bg.add(strand);
377     bg.add(turn);
378     bg.add(buried);
379     bg.add(user);
380
381     if (jalview.gui.UserDefinedColours.getUserColourSchemes() != null)
382     {
383       java.util.Enumeration userColours = jalview.gui.UserDefinedColours
384               .getUserColourSchemes().keys();
385
386       while (userColours.hasMoreElements())
387       {
388         final JRadioButtonMenuItem radioItem = new JRadioButtonMenuItem(
389                 userColours.nextElement().toString());
390         radioItem.setName("USER_DEFINED");
391         radioItem.addMouseListener(new MouseAdapter()
392         {
393           public void mousePressed(MouseEvent evt)
394           {
395             if (evt.isControlDown()
396                     || SwingUtilities.isRightMouseButton(evt))
397             {
398               radioItem.removeActionListener(radioItem.getActionListeners()[0]);
399
400               int option = JOptionPane.showInternalConfirmDialog(
401                       jalview.gui.Desktop.desktop,
402                       MessageManager
403                               .getString("label.remove_from_default_list"),
404                       MessageManager
405                               .getString("label.remove_user_defined_colour"),
406                       JOptionPane.YES_NO_OPTION);
407               if (option == JOptionPane.YES_OPTION)
408               {
409                 jalview.gui.UserDefinedColours
410                         .removeColourFromDefaults(radioItem.getText());
411                 coloursMenu.remove(radioItem);
412               }
413               else
414               {
415                 radioItem.addActionListener(new ActionListener()
416                 {
417                   public void actionPerformed(ActionEvent evt)
418                   {
419                     user_actionPerformed(evt);
420                   }
421                 });
422               }
423             }
424           }
425         });
426         radioItem.addActionListener(new ActionListener()
427         {
428           public void actionPerformed(ActionEvent evt)
429           {
430             user_actionPerformed(evt);
431           }
432         });
433         coloursMenu.add(radioItem);
434         bg.add(radioItem);
435       }
436     }
437
438     viewMenu.add(wire);
439     viewMenu.add(depth);
440     viewMenu.add(zbuffer);
441     viewMenu.add(allchains);
442   }
443
444   JMenuBar jMenuBar1 = new JMenuBar();
445
446   JMenu fileMenu = new JMenu();
447
448   JMenu coloursMenu = new JMenu();
449
450   JMenu saveMenu = new JMenu();
451
452   JMenuItem png = new JMenuItem();
453
454   JMenuItem eps = new JMenuItem();
455
456   JMenuItem mapping = new JMenuItem();
457
458   JCheckBoxMenuItem wire = new JCheckBoxMenuItem();
459
460   JCheckBoxMenuItem depth = new JCheckBoxMenuItem();
461
462   JCheckBoxMenuItem zbuffer = new JCheckBoxMenuItem();
463
464   JCheckBoxMenuItem allchains = new JCheckBoxMenuItem();
465
466   JRadioButtonMenuItem charge = new JRadioButtonMenuItem();
467
468   JRadioButtonMenuItem chain = new JRadioButtonMenuItem();
469
470   JRadioButtonMenuItem seqButton = new JRadioButtonMenuItem();
471
472   JRadioButtonMenuItem hydro = new JRadioButtonMenuItem();
473
474   JRadioButtonMenuItem taylor = new JRadioButtonMenuItem();
475
476   JRadioButtonMenuItem zappo = new JRadioButtonMenuItem();
477
478   JRadioButtonMenuItem user = new JRadioButtonMenuItem();
479
480   JRadioButtonMenuItem buried = new JRadioButtonMenuItem();
481
482   JRadioButtonMenuItem turn = new JRadioButtonMenuItem();
483
484   JRadioButtonMenuItem strand = new JRadioButtonMenuItem();
485
486   JRadioButtonMenuItem helix = new JRadioButtonMenuItem();
487
488   JMenu viewMenu = new JMenu();
489
490   JMenuItem background = new JMenuItem();
491
492   JMenuItem savePDB = new JMenuItem();
493
494   /**
495    * DOCUMENT ME!
496    * 
497    * @param e
498    *          DOCUMENT ME!
499    */
500   public void eps_actionPerformed(ActionEvent e)
501   {
502     makePDBImage(jalview.util.ImageMaker.TYPE.EPS);
503   }
504
505   /**
506    * DOCUMENT ME!
507    * 
508    * @param e
509    *          DOCUMENT ME!
510    */
511   public void png_actionPerformed(ActionEvent e)
512   {
513     makePDBImage(jalview.util.ImageMaker.TYPE.PNG);
514   }
515
516   void makePDBImage(jalview.util.ImageMaker.TYPE type)
517   {
518     int width = pdbcanvas.getWidth();
519     int height = pdbcanvas.getHeight();
520
521     jalview.util.ImageMaker im;
522
523     if (type == jalview.util.ImageMaker.TYPE.PNG)
524     {
525       im = new jalview.util.ImageMaker(this,
526               jalview.util.ImageMaker.TYPE.PNG,
527               "Make PNG image from view", width, height, null, null);
528     }
529     else if (type == jalview.util.ImageMaker.TYPE.EPS)
530     {
531       im = new jalview.util.ImageMaker(this,
532               jalview.util.ImageMaker.TYPE.EPS,
533               "Make EPS file from view", width, height, null,
534               this.getTitle());
535     }
536     else
537     {
538
539       im = new jalview.util.ImageMaker(this,
540               jalview.util.ImageMaker.TYPE.SVG, "Make SVG file from PCA",
541               width, height, null, this.getTitle());
542     }
543
544     if (im.getGraphics() != null)
545     {
546       pdbcanvas.drawAll(im.getGraphics(), width, height);
547       im.writeImage();
548     }
549   }
550
551   public void charge_actionPerformed(ActionEvent e)
552   {
553     pdbcanvas.bysequence = false;
554     pdbcanvas.pdb.setChargeColours();
555     pdbcanvas.redrawneeded = true;
556     pdbcanvas.repaint();
557   }
558
559   public void hydro_actionPerformed(ActionEvent e)
560   {
561     pdbcanvas.bysequence = false;
562     pdbcanvas.pdb.setColours(new HydrophobicColourScheme());
563     pdbcanvas.redrawneeded = true;
564     pdbcanvas.repaint();
565   }
566
567   public void chain_actionPerformed(ActionEvent e)
568   {
569     pdbcanvas.bysequence = false;
570     pdbcanvas.pdb.setChainColours();
571     pdbcanvas.redrawneeded = true;
572     pdbcanvas.repaint();
573   }
574
575   public void zbuffer_actionPerformed(ActionEvent e)
576   {
577     pdbcanvas.zbuffer = !pdbcanvas.zbuffer;
578     pdbcanvas.redrawneeded = true;
579     pdbcanvas.repaint();
580   }
581
582   public void molecule_actionPerformed(ActionEvent e)
583   {
584     pdbcanvas.bymolecule = !pdbcanvas.bymolecule;
585     pdbcanvas.redrawneeded = true;
586     pdbcanvas.repaint();
587   }
588
589   public void depth_actionPerformed(ActionEvent e)
590   {
591     pdbcanvas.depthcue = !pdbcanvas.depthcue;
592     pdbcanvas.redrawneeded = true;
593     pdbcanvas.repaint();
594   }
595
596   public void wire_actionPerformed(ActionEvent e)
597   {
598     pdbcanvas.wire = !pdbcanvas.wire;
599     pdbcanvas.redrawneeded = true;
600     pdbcanvas.repaint();
601   }
602
603   public void seqButton_actionPerformed(ActionEvent e)
604   {
605     pdbcanvas.bysequence = true;
606     pdbcanvas.updateSeqColours();
607   }
608
609   public void mapping_actionPerformed(ActionEvent e)
610   {
611     jalview.gui.CutAndPasteTransfer cap = new jalview.gui.CutAndPasteTransfer();
612     try
613     {
614       cap.setText(pdbcanvas.mappingDetails.toString());
615       Desktop.addInternalFrame(cap,
616               MessageManager.getString("label.pdb_sequence_mapping"), 550,
617               600);
618     } catch (OutOfMemoryError oom)
619     {
620       new OOMWarning("Opening sequence to structure mapping report", oom);
621       cap.dispose();
622     }
623   }
624
625   public void allchains_itemStateChanged(ItemEvent e)
626   {
627     pdbcanvas.setAllchainsVisible(allchains.getState());
628   }
629
630   public void zappo_actionPerformed(ActionEvent e)
631   {
632     pdbcanvas.bysequence = false;
633     pdbcanvas.pdb.setColours(new ZappoColourScheme());
634     pdbcanvas.redrawneeded = true;
635     pdbcanvas.repaint();
636   }
637
638   public void taylor_actionPerformed(ActionEvent e)
639   {
640     pdbcanvas.bysequence = false;
641     pdbcanvas.pdb.setColours(new TaylorColourScheme());
642     pdbcanvas.redrawneeded = true;
643     pdbcanvas.repaint();
644   }
645
646   public void helix_actionPerformed(ActionEvent e)
647   {
648     pdbcanvas.bysequence = false;
649     pdbcanvas.pdb.setColours(new HelixColourScheme());
650     pdbcanvas.redrawneeded = true;
651     pdbcanvas.repaint();
652   }
653
654   public void strand_actionPerformed(ActionEvent e)
655   {
656     pdbcanvas.bysequence = false;
657     pdbcanvas.pdb.setColours(new StrandColourScheme());
658     pdbcanvas.redrawneeded = true;
659     pdbcanvas.repaint();
660   }
661
662   public void turn_actionPerformed(ActionEvent e)
663   {
664     pdbcanvas.bysequence = false;
665     pdbcanvas.pdb.setColours(new TurnColourScheme());
666     pdbcanvas.redrawneeded = true;
667     pdbcanvas.repaint();
668   }
669
670   public void buried_actionPerformed(ActionEvent e)
671   {
672     pdbcanvas.bysequence = false;
673     pdbcanvas.pdb.setColours(new BuriedColourScheme());
674     pdbcanvas.redrawneeded = true;
675     pdbcanvas.repaint();
676   }
677
678   public void user_actionPerformed(ActionEvent e)
679   {
680     if (e.getActionCommand().equals(
681             MessageManager.getString("action.user_defined")))
682     {
683       // new UserDefinedColours(pdbcanvas, null);
684     }
685     else
686     {
687       UserColourScheme udc = (UserColourScheme) UserDefinedColours
688               .getUserColourSchemes().get(e.getActionCommand());
689
690       pdbcanvas.pdb.setColours(udc);
691       pdbcanvas.redrawneeded = true;
692       pdbcanvas.repaint();
693     }
694   }
695
696   public void background_actionPerformed(ActionEvent e)
697   {
698     java.awt.Color col = JColorChooser.showDialog(this,
699             MessageManager.getString("label.select_backgroud_colour"),
700             pdbcanvas.backgroundColour);
701
702     if (col != null)
703     {
704       pdbcanvas.backgroundColour = col;
705       pdbcanvas.redrawneeded = true;
706       pdbcanvas.repaint();
707     }
708   }
709
710   public void savePDB_actionPerformed(ActionEvent e)
711   {
712     JalviewFileChooser chooser = new JalviewFileChooser(
713             jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
714
715     chooser.setFileView(new JalviewFileView());
716     chooser.setDialogTitle(MessageManager.getString("label.save_pdb_file"));
717     chooser.setToolTipText(MessageManager.getString("action.save"));
718
719     int value = chooser.showSaveDialog(this);
720
721     if (value == JalviewFileChooser.APPROVE_OPTION)
722     {
723       try
724       {
725         BufferedReader in = new BufferedReader(new FileReader(tmpPDBFile));
726         File outFile = chooser.getSelectedFile();
727
728         PrintWriter out = new PrintWriter(new FileOutputStream(outFile));
729         String data;
730         while ((data = in.readLine()) != null)
731         {
732           if (!(data.indexOf("<PRE>") > -1 || data.indexOf("</PRE>") > -1))
733           {
734             out.println(data);
735           }
736         }
737         out.close();
738         in.close();
739       } catch (Exception ex)
740       {
741         ex.printStackTrace();
742       }
743     }
744   }
745 }