View Javadoc
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  public class ProfileSelectorPanel extends JPanel {
54  	private static final long serialVersionUID = 1L;
55  	private	JDialog	owningDialog = null;
56  	private JComboBox<String> jComboBoxProfileName = null;
57  	private JButton jButtonNewProfile = null;
58  	private	BFProperties properties;
59  	private	ProfileSelectorNotifier profileSelectorNotifier;
60  	private	boolean initializing = true;	// don't let events fire prematurely
61  
62  	/**
63  	 * This method initializes 
64  	 * 
65  	 */
66  	public ProfileSelectorPanel() {
67  		super();
68  		initialize();
69  		initializing = false;
70  	}
71  
72  	public ProfileSelectorPanel(JDialog owner, ProfileSelectorNotifier profileSelectorNotifier, BFProperties properties) {
73  		super();
74  		this.owningDialog = owner;
75  		this.profileSelectorNotifier = profileSelectorNotifier;
76  		this.properties = properties;
77  		initialize();
78  		setupProfiles();
79  		initializing = false;
80  	}
81  
82  	public int getSelectedProfile() {
83  		if (!this.isVisible())
84  			return(0);
85  		if (this.jComboBoxProfileName.getSelectedIndex() == -1)
86  			return(0);
87  		return(this.jComboBoxProfileName.getSelectedIndex());
88  	}
89  
90  	private void setupProfiles() {
91  		int profileCount = this.properties.getProfileCount();
92  		if (profileCount == 0) {
93  			String s = "Profile 0";
94  			this.jComboBoxProfileName.addItem(s);
95  			this.properties.setProfileName(0, s);
96  			this.properties.setSelectedProfile(0);
97  			return;
98  		}
99  		for (int i=0; i<profileCount; i++) {
100 			String s = this.properties.getProfileName(i);
101 			if (s != null && s.length() > 0) {
102 				this.jComboBoxProfileName.addItem(s);
103 			} else {
104 				break;
105 			}
106 		}
107 		this.jComboBoxProfileName.setSelectedIndex(this.properties.getSelectedProfile());
108 	}
109 
110 	private void profileChanged(java.awt.event.ItemEvent e) {
111 		if (initializing)
112 			return;
113 		if (e.getStateChange() == java.awt.event.ItemEvent.SELECTED) {
114 			this.properties.setSelectedProfile(this.jComboBoxProfileName.getSelectedIndex());
115 			profileSelectorNotifier.profileChanged();
116 		}
117 	}
118 
119 	/**
120 	 * This method initializes this
121 	 * 
122 	 */
123 	private void initialize() {
124         this.add(getJComboBoxProfileName(), null);
125         this.add(getJButtonNewProfile(), null);
126 	}
127 
128 	/**
129 	 * This method initializes jComboBoxProfileName	
130 	 * 	
131 	 * @return javax.swing.JComboBox	
132 	 */
133 	private JComboBox<String> getJComboBoxProfileName() {
134 		if (jComboBoxProfileName == null) {
135 			jComboBoxProfileName = new JComboBox<String>();
136 			jComboBoxProfileName.addItemListener(new java.awt.event.ItemListener() {
137 				public void itemStateChanged(java.awt.event.ItemEvent e) {
138 					profileChanged(e);
139 				}
140 			});
141 		}
142 		return jComboBoxProfileName;
143 	}
144 
145 	/**
146 	 * This method initializes jButtonNewProfile	
147 	 * 	
148 	 * @return javax.swing.JButton	
149 	 */
150 	private JButton getJButtonNewProfile() {
151 		if (jButtonNewProfile == null) {
152 			jButtonNewProfile = new JButton();
153 			jButtonNewProfile.setText("New");
154 			jButtonNewProfile.setFont(new Font("Dialog", Font.PLAIN, 10));
155 			jButtonNewProfile.addActionListener(new java.awt.event.ActionListener() {
156 				public void actionPerformed(java.awt.event.ActionEvent e) {
157 					showNewProfileDialog();
158 				}
159 			});
160 		}
161 		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 		int profileId = this.properties.getProfileCount();
170 		String defaultName = "Profile " + profileId;
171 		String s = (String)JOptionPane.showInputDialog(
172 				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 		if (s == null || s.length() == 0)
180 			return;
181 		this.properties.setProfileName(profileId, s);
182 		this.properties.setUserName(profileId, "");
183 		this.properties.setPassword(profileId, "");
184 		this.jComboBoxProfileName.addItem(s);
185 		this.jComboBoxProfileName.setSelectedIndex(profileId);
186 	}
187 }