Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ProfileSelectorPanel |
|
| 2.5454545454545454;2.545 | ||||
ProfileSelectorPanel$1 |
|
| 2.5454545454545454;2.545 | ||||
ProfileSelectorPanel$2 |
|
| 2.5454545454545454;2.545 |
1 | /****************************************************************************** | |
2 | * ProfileSelectorPanel.java - A little panel the selects a profile, or creates a new one. | |
3 | * $Id$ | |
4 | * | |
5 | * BuckoFIBS - Backgammon by BuckoSoft | |
6 | * Copyright© 2009,2010 - Dick Balaska - BuckoSoft, Corp. | |
7 | * | |
8 | * $Log$ | |
9 | * Revision 1.3 2010/03/03 13:12:21 inim | |
10 | * Replaced (c) sign in comment mangled by CVS default encoding back to UTF-8 | |
11 | * | |
12 | * Revision 1.2 2010/03/03 12:19:49 inim | |
13 | * Moved source to UTF8 encoding from CP1252 encoding. To this end all source files' (c) message was updated to "Copyright© 2009,2010 - Dick Balaska - BuckoSoft, Corp.". This replaces the (c) sign to UTF8, and adds the new year 2010. | |
14 | * | |
15 | * Revision 1.1 2010/02/04 05:57:53 inim | |
16 | * Mavenized project folder layout | |
17 | * | |
18 | * Revision 1.5 2009/01/28 18:15:10 dick | |
19 | * Prettier cvs link in the javadoc. | |
20 | * | |
21 | * Revision 1.4 2009/01/09 07:18:55 dick | |
22 | * Don't fire events during construction. (Causes us to 'select' profile 0 before ever reading the data). | |
23 | * | |
24 | * Revision 1.3 2009/01/07 08:36:34 dick | |
25 | * Make it functional. | |
26 | * | |
27 | * Revision 1.2 2009/01/06 08:11:24 dick | |
28 | * Pass the owning dialog in the constructor. | |
29 | * Select 0 if really -1. | |
30 | * | |
31 | * Revision 1.1 2009/01/05 07:16:35 dick | |
32 | * A little panel the selects a profile, or creates a new one. | |
33 | * | |
34 | */ | |
35 | package com.buckosoft.fibs.BuckoFIBS.gui.account; | |
36 | ||
37 | import java.awt.Font; | |
38 | ||
39 | import javax.swing.JButton; | |
40 | import javax.swing.JComboBox; | |
41 | import javax.swing.JDialog; | |
42 | import javax.swing.JOptionPane; | |
43 | import javax.swing.JPanel; | |
44 | ||
45 | import com.buckosoft.fibs.BuckoFIBS.BFProperties; | |
46 | ||
47 | /** A panel to manage which profile is selected. | |
48 | * @author Dick Balaska | |
49 | * @since 2009/01/04 | |
50 | * @version $Revision$ <br> $Date$ | |
51 | * @see <a href="http://cvs.buckosoft.com/Projects/BuckoFIBS/BuckoFIBS/src/com/buckosoft/fibs/BuckoFIBS/gui/account/ServerPortSelectorPanel.java">cvs ServerPortSelectorPanel.java</a> | |
52 | */ | |
53 | 0 | public class ProfileSelectorPanel extends JPanel { |
54 | private static final long serialVersionUID = 1L; | |
55 | 0 | private JDialog owningDialog = null; |
56 | 0 | private JComboBox<String> jComboBoxProfileName = null; |
57 | 0 | private JButton jButtonNewProfile = null; |
58 | private BFProperties properties; | |
59 | private ProfileSelectorNotifier profileSelectorNotifier; | |
60 | 0 | private boolean initializing = true; // don't let events fire prematurely |
61 | ||
62 | /** | |
63 | * This method initializes | |
64 | * | |
65 | */ | |
66 | public ProfileSelectorPanel() { | |
67 | 0 | super(); |
68 | 0 | initialize(); |
69 | 0 | initializing = false; |
70 | 0 | } |
71 | ||
72 | public ProfileSelectorPanel(JDialog owner, ProfileSelectorNotifier profileSelectorNotifier, BFProperties properties) { | |
73 | 0 | super(); |
74 | 0 | this.owningDialog = owner; |
75 | 0 | this.profileSelectorNotifier = profileSelectorNotifier; |
76 | 0 | this.properties = properties; |
77 | 0 | initialize(); |
78 | 0 | setupProfiles(); |
79 | 0 | initializing = false; |
80 | 0 | } |
81 | ||
82 | public int getSelectedProfile() { | |
83 | 0 | if (!this.isVisible()) |
84 | 0 | return(0); |
85 | 0 | if (this.jComboBoxProfileName.getSelectedIndex() == -1) |
86 | 0 | return(0); |
87 | 0 | return(this.jComboBoxProfileName.getSelectedIndex()); |
88 | } | |
89 | ||
90 | private void setupProfiles() { | |
91 | 0 | int profileCount = this.properties.getProfileCount(); |
92 | 0 | if (profileCount == 0) { |
93 | 0 | String s = "Profile 0"; |
94 | 0 | this.jComboBoxProfileName.addItem(s); |
95 | 0 | this.properties.setProfileName(0, s); |
96 | 0 | this.properties.setSelectedProfile(0); |
97 | 0 | return; |
98 | } | |
99 | 0 | for (int i=0; i<profileCount; i++) { |
100 | 0 | String s = this.properties.getProfileName(i); |
101 | 0 | if (s != null && s.length() > 0) { |
102 | 0 | this.jComboBoxProfileName.addItem(s); |
103 | } else { | |
104 | break; | |
105 | } | |
106 | } | |
107 | 0 | this.jComboBoxProfileName.setSelectedIndex(this.properties.getSelectedProfile()); |
108 | 0 | } |
109 | ||
110 | private void profileChanged(java.awt.event.ItemEvent e) { | |
111 | 0 | if (initializing) |
112 | 0 | return; |
113 | 0 | if (e.getStateChange() == java.awt.event.ItemEvent.SELECTED) { |
114 | 0 | this.properties.setSelectedProfile(this.jComboBoxProfileName.getSelectedIndex()); |
115 | 0 | profileSelectorNotifier.profileChanged(); |
116 | } | |
117 | 0 | } |
118 | ||
119 | /** | |
120 | * This method initializes this | |
121 | * | |
122 | */ | |
123 | private void initialize() { | |
124 | 0 | this.add(getJComboBoxProfileName(), null); |
125 | 0 | this.add(getJButtonNewProfile(), null); |
126 | 0 | } |
127 | ||
128 | /** | |
129 | * This method initializes jComboBoxProfileName | |
130 | * | |
131 | * @return javax.swing.JComboBox | |
132 | */ | |
133 | private JComboBox<String> getJComboBoxProfileName() { | |
134 | 0 | if (jComboBoxProfileName == null) { |
135 | 0 | jComboBoxProfileName = new JComboBox<String>(); |
136 | 0 | jComboBoxProfileName.addItemListener(new java.awt.event.ItemListener() { |
137 | public void itemStateChanged(java.awt.event.ItemEvent e) { | |
138 | 0 | profileChanged(e); |
139 | 0 | } |
140 | }); | |
141 | } | |
142 | 0 | return jComboBoxProfileName; |
143 | } | |
144 | ||
145 | /** | |
146 | * This method initializes jButtonNewProfile | |
147 | * | |
148 | * @return javax.swing.JButton | |
149 | */ | |
150 | private JButton getJButtonNewProfile() { | |
151 | 0 | if (jButtonNewProfile == null) { |
152 | 0 | jButtonNewProfile = new JButton(); |
153 | 0 | jButtonNewProfile.setText("New"); |
154 | 0 | jButtonNewProfile.setFont(new Font("Dialog", Font.PLAIN, 10)); |
155 | 0 | jButtonNewProfile.addActionListener(new java.awt.event.ActionListener() { |
156 | public void actionPerformed(java.awt.event.ActionEvent e) { | |
157 | 0 | showNewProfileDialog(); |
158 | 0 | } |
159 | }); | |
160 | } | |
161 | 0 | return jButtonNewProfile; |
162 | } | |
163 | ||
164 | /** Show the dialog and return the name of the new Profile | |
165 | * @return The name of the new Profile, or null | |
166 | */ | |
167 | private void showNewProfileDialog() { | |
168 | //newProfileNameDialog = new NewProfileNameDialog(owningDialog); | |
169 | 0 | int profileId = this.properties.getProfileCount(); |
170 | 0 | String defaultName = "Profile " + profileId; |
171 | 0 | String s = (String)JOptionPane.showInputDialog( |
172 | 0 | this.owningDialog.getComponent(0), |
173 | "Type a name for the new Profile", | |
174 | "New Profile Name", | |
175 | JOptionPane.PLAIN_MESSAGE, | |
176 | null, | |
177 | null, | |
178 | defaultName); | |
179 | 0 | if (s == null || s.length() == 0) |
180 | 0 | return; |
181 | 0 | this.properties.setProfileName(profileId, s); |
182 | 0 | this.properties.setUserName(profileId, ""); |
183 | 0 | this.properties.setPassword(profileId, ""); |
184 | 0 | this.jComboBoxProfileName.addItem(s); |
185 | 0 | this.jComboBoxProfileName.setSelectedIndex(profileId); |
186 | 0 | } |
187 | } |