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