b0700cc5c1cc97c1f7e0552002c99574f97747bf
[jalview.git] / srcjar / fr / orsay / lri / varna / controlers / ControleurBaseSpecialColorEditor.java
1 /*
2  VARNA is a tool for the automated drawing, visualization and annotation of the secondary structure of RNA, designed as a companion software for web servers and databases.
3  Copyright (C) 2008  Kevin Darty, Alain Denise and Yann Ponty.
4  electronic mail : Yann.Ponty@lri.fr
5  paper mail : LRI, bat 490 Université Paris-Sud 91405 Orsay Cedex France
6
7  This file is part of VARNA version 3.1.
8  VARNA version 3.1 is free software: you can redistribute it and/or 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  VARNA version 3.1 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  See the GNU General Public License for more details.
14
15  You should have received a copy of the GNU General Public License along with VARNA version 3.1.
16  If not, see http://www.gnu.org/licenses.
17  */
18 package fr.orsay.lri.varna.controlers;
19
20 import java.awt.Color;
21 import java.awt.event.ActionEvent;
22 import java.awt.event.ActionListener;
23 import java.awt.event.ComponentEvent;
24 import java.awt.event.ComponentListener;
25
26 import fr.orsay.lri.varna.components.BaseSpecialColorEditor;
27 import fr.orsay.lri.varna.models.BaseList;
28 import fr.orsay.lri.varna.models.rna.ModeleBase;
29
30 /**
31  * BH SwingJS - additions here to allow asynchronous JColorChooser  
32  */
33 public class ControleurBaseSpecialColorEditor implements ActionListener, ComponentListener {  // BH SwingJS
34
35         private BaseSpecialColorEditor _specialColorEditor;
36
37         private int _selectedRow;
38         private int _selectedCol;
39         private Color _selectedColor;
40         private String _selectedColTitle;
41
42         public ControleurBaseSpecialColorEditor(BaseSpecialColorEditor specialColorEditor) {
43                 _specialColorEditor = specialColorEditor;
44         }
45
46         /**
47          * Handles events from the editor button and from the dialog's OK button.
48          */
49         public void actionPerformed(ActionEvent e) {
50                 if (BaseSpecialColorEditor.getEDIT().equals(e.getActionCommand())) {
51                         // The user has clicked the cell, so
52                         // bring up the dialog.
53                         _specialColorEditor.getButton().setBackground(
54                                         _specialColorEditor.getCurrentColor());
55                         _specialColorEditor.getColorChooser().setColor(
56                                         _specialColorEditor.getCurrentColor());
57                         
58                         // BH SwingJS in JavaScript, this is not modal. 
59                         // We have to set a callback to stop the editing.
60                         
61                         _specialColorEditor.getDialog().removeComponentListener(this);
62                         _specialColorEditor.getDialog().addComponentListener(this);
63                         _specialColorEditor.getDialog().setVisible(true);
64                         
65                         
66                         // Make the renderer reappear.
67                         // BH SwingJS not so fast...
68                         //_specialColorEditor.callFireEditingStopped();
69
70                 } else { // User pressed dialog's "OK" button.
71                         _specialColorEditor.setCurrentColor(_specialColorEditor
72                                         .getColorChooser().getColor());
73
74                         _selectedRow = _specialColorEditor.get_vueBases().getTable()
75                                         .getSelectedRow();
76
77                         _selectedCol = _specialColorEditor.get_vueBases().getTable()
78                                         .getSelectedColumn();
79
80                         _selectedColor = _specialColorEditor.getCurrentColor();
81
82                         _selectedColTitle = _specialColorEditor.get_vueBases()
83                                         .getSpecialTableModel().getColumnName(_selectedCol);
84                         BaseList lb = _specialColorEditor.get_vueBases().getDataAt(_selectedRow);
85                         for(ModeleBase mb: lb.getBases())
86                         {
87                                 applyColor(_selectedColTitle, _selectedColor,mb);
88                         }
89                         _specialColorEditor.get_vueBases().get_vp().repaint();
90                 }
91         }
92
93
94         private void applyColor(String titreCol, Color couleur, ModeleBase mb) {
95                 if (titreCol.equals("Inner Color")) {
96                         mb.getStyleBase()
97                                         .setBaseInnerColor(couleur);
98                 } else if (titreCol.equals("Outline Color")) {
99                         mb.getStyleBase()
100                                         .setBaseOutlineColor(couleur);
101                 } else if (titreCol.equals("Name Color")) {
102                         mb.getStyleBase()
103                                         .setBaseNameColor(couleur);
104                 } else if (titreCol.equals("Number Color")) {
105                         mb.getStyleBase()
106                                         .setBaseNumberColor(couleur);
107                 }
108
109         }
110
111
112         @Override
113         public void componentResized(ComponentEvent e) {
114         }
115
116         @Override
117         public void componentMoved(ComponentEvent e) {
118         }
119
120         @Override
121         public void componentShown(ComponentEvent e) {
122         }
123
124         @Override
125         public void componentHidden(ComponentEvent e) {
126                 
127                 _specialColorEditor.callFireEditingStopped(); // BH SwingJS -- need to catch this
128                 
129         }
130
131 }