JAL-4186 attempts to work in jalviewjs, still not working
[jalview.git] / src / jalview / gui / EditNameDialog.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 jalview.gui;
22
23 import java.awt.FlowLayout;
24 import java.awt.Font;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.concurrent.Callable;
28
29 import javax.swing.BoxLayout;
30 import javax.swing.JButton;
31 import javax.swing.JComponent;
32 import javax.swing.JLabel;
33 import javax.swing.JPanel;
34 import javax.swing.JTextField;
35
36 import jalview.util.MessageManager;
37
38 /**
39  * A dialog where a name and description may be edited
40  */
41 public class EditNameDialog
42 {
43   private static final Font COURIER_12 = new Font("Courier", Font.PLAIN,
44           12);
45
46   JTextField id;
47
48   JTextField description;
49
50   JButton ok = new JButton(MessageManager.getString("action.accept"));
51
52   JButton cancel = new JButton(MessageManager.getString("action.cancel"));
53
54   private JPanel panel;
55
56   public String getName()
57   {
58     return id.getText();
59   }
60
61   public String getDescription()
62   {
63     if (description.getText().length() < 1)
64     {
65       return null;
66     }
67     else
68     {
69       return description.getText();
70     }
71   }
72
73   /**
74    * Constructor
75    * 
76    * @param name
77    * @param desc
78    * @param label1
79    * @param label2
80    */
81   public EditNameDialog(String name, String desc, String label1,
82           String label2)
83   {
84     panel = new JPanel();
85     panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
86     JPanel namePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
87     JPanel descriptionPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
88     panel.add(namePanel);
89     panel.add(descriptionPanel);
90
91     JLabel nameLabel = new JLabel(label1);
92     // nameLabel.setFont(COURIER_12);
93     namePanel.add(nameLabel);
94
95     id = new JTextField(name, 40);
96     namePanel.add(id);
97
98     description = new JTextField(desc, 40);
99
100     /*
101      * optionally add field for description
102      */
103     if (desc != null || label2 != null)
104     {
105       JLabel descLabel = new JLabel(label2);
106       // descLabel.setFont(COURIER_12);
107       descriptionPanel.add(descLabel);
108       descriptionPanel.add(description);
109     }
110   }
111
112   /**
113    * Shows the dialog, and runs the response action if OK is selected
114    * 
115    * @param action
116    */
117   public void showDialog(JComponent parent, String title,
118           Callable<Void> action)
119   {
120     String ok = MessageManager.getString("action.ok");
121     String cancel = MessageManager.getString("action.cancel");
122     String[] options = new String[] { ok, cancel };
123
124     /*
125     JvOptionPane.newOptionDialog(parent)
126             .setResponseHandler(JvOptionPane.OK_OPTION, action)
127             .showInternalDialog(panel, title, JvOptionPane.OK_CANCEL_OPTION,
128                     JvOptionPane.PLAIN_MESSAGE, null, options, ok);
129             */
130
131     List<Callable<Void>> actions = new ArrayList<>();
132     actions.add(action);
133     actions.add(JvOptionPane.NULLCALLABLE);
134
135     JvOptionPane.frameDialog(panel, title, JvOptionPane.PLAIN_MESSAGE,
136             options, ok, actions, false);
137   }
138 }