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