1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
48
49
50
51
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;
61
62
63
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
121
122
123 private void initialize() {
124 this.add(getJComboBoxProfileName(), null);
125 this.add(getJButtonNewProfile(), null);
126 }
127
128
129
130
131
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
147
148
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
165
166
167 private void showNewProfileDialog() {
168
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 }