PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/code/PersonPicker.java

http://silkin.googlecode.com/
Java | 393 lines | 328 code | 31 blank | 34 comment | 66 complexity | be48d53e2d13f400897b7375f29d1247 MD5 | raw file
  1. import java.util.* ;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import javax.swing.event.*;
  6. import java.beans.*;
  7. /**
  8. The PersonPicker is a window that displays a menu of {@link Individual}s. When one person
  9. is selected, their fields are displayed to confirm it is the right one. When the User clicks
  10. the "Choose" button, the Individual selected is added to the appropriate field(s) of the <code>editor</code>
  11. which launched this picker.
  12. <p>
  13. It is an extension of KSJInternalFrame so it will appear in the View menu.
  14. @author Gary Morris, Northern Virginia Community College garymorris2245@verizon.net
  15. */
  16. public class PersonPicker extends KSJInternalFrame {
  17. public KSJInternalFrame genericEditor;
  18. public PPListener listener;
  19. public Individual ind;
  20. public ArrayList<Object> source, selections = new ArrayList<Object>();
  21. public boolean oneValue;
  22. public int udpNmbr = 0;
  23. public String fieldName;
  24. public JPanel picker;
  25. public JComboBox personPick;
  26. public JLabel fullName, surName, birthday, deathday, gender, hName, wName;
  27. public JTextArea spouseList, pickList;
  28. public JButton unDo, choose;
  29. public Rectangle bnds = new Rectangle();
  30. public PersonPicker(ArrayList<Object> srce, String title, String fieldNam,
  31. boolean singleValue, KSJInternalFrame editor) {
  32. super(title);
  33. windowNum = "Person Picker: " + fieldNam;
  34. oneValue = singleValue;
  35. source = srce;
  36. genericEditor = editor;
  37. fieldName = fieldNam;
  38. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  39. addInternalFrameListener(this); // allows View Menu to work
  40. listener = new PPListener(this);
  41. buildEditor();
  42. } // end of PersonPicker constructor with 5 args
  43. public PersonPicker(ArrayList<Object> srce, String title, String fieldNam, int udpNum,
  44. boolean singleValue, KSJInternalFrame editor) {
  45. super(title);
  46. windowNum = "Person Picker: " + fieldNam;
  47. oneValue = singleValue;
  48. source = srce;
  49. genericEditor = editor;
  50. fieldName = fieldNam;
  51. udpNmbr = udpNum;
  52. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  53. addInternalFrameListener(this); // allows View Menu to work
  54. listener = new PPListener(this);
  55. buildEditor();
  56. } // end of PersonPicker constructor with 6 args
  57. public void buildEditor() {
  58. // MENU OF PERSONS AVAILABLE
  59. picker = new JPanel();
  60. picker.setLayout(new BoxLayout(picker, BoxLayout.PAGE_AXIS));
  61. JPanel line1 = new JPanel();
  62. line1.setLayout(new BoxLayout(line1, BoxLayout.LINE_AXIS));
  63. JLabel avaiLable = new JLabel("Persons Available: ");
  64. Dimension sizer = new Dimension(250, 50);
  65. String[] personMenu = ContextEditor.genIndMenu(source);
  66. personPick = new JComboBox(personMenu);
  67. personPick.setSelectedIndex(0);
  68. ind = (Individual)source.get(0);
  69. personPick.addActionListener(listener);
  70. personPick.setActionCommand("pick person");
  71. personPick.setMinimumSize(sizer);
  72. personPick.setMaximumSize(sizer);
  73. line1.add(avaiLable);
  74. line1.add(personPick);
  75. picker.add(line1);
  76. picker.add(Box.createRigidArea(new Dimension(0,8)));
  77. // NAME ROW
  78. JPanel nameBox = new JPanel();
  79. nameBox.setLayout(new BoxLayout(nameBox, BoxLayout.LINE_AXIS));
  80. JLabel fullNameLabel = new JLabel("Full Name: ");
  81. fullName = new JLabel(ind.name);
  82. JLabel surnameLabel = new JLabel("Surname: ");
  83. surName = new JLabel(ind.surname);
  84. nameBox.add(fullNameLabel);
  85. nameBox.add(fullName);
  86. nameBox.add(Box.createRigidArea(new Dimension(80,0)));
  87. nameBox.add(surnameLabel);
  88. nameBox.add(surName);
  89. picker.add(nameBox);
  90. picker.add(Box.createRigidArea(new Dimension(0,8)));
  91. // BORN ROW
  92. JPanel bornRow = new JPanel();
  93. bornRow.setLayout(new BoxLayout(bornRow, BoxLayout.LINE_AXIS));
  94. birthday = new JLabel("Born: " + ind.getDateOfBirth());
  95. deathday = new JLabel("Died: " + ind.getDateOfDeath());
  96. gender = new JLabel("Gender: " + ind.gender);
  97. bornRow.add(birthday);
  98. bornRow.add(Box.createRigidArea(new Dimension(20,0)));
  99. bornRow.add(deathday);
  100. bornRow.add(Box.createRigidArea(new Dimension(20,0)));
  101. bornRow.add(gender);
  102. picker.add(bornRow);
  103. picker.add(Box.createRigidArea(new Dimension(0,8)));
  104. // BIRTH FAMILY ROW
  105. JPanel famRow = new JPanel();
  106. famRow.setLayout(new BoxLayout(famRow, BoxLayout.LINE_AXIS));
  107. JLabel famLeft = new JLabel("Birth Family: ");
  108. JPanel famRt = new JPanel();
  109. famRt.setLayout(new BoxLayout(famRt, BoxLayout.PAGE_AXIS));
  110. String husbName = "Birth Family Not Recorded", wifeName = "";
  111. if (ind.birthFamily != null) {
  112. if (ind.birthFamily.husband != null) husbName = ind.birthFamily.husband.name;
  113. else husbName = "Father's Name Not Recorded";
  114. if (ind.birthFamily.wife != null) wifeName = ind.birthFamily.wife.name;
  115. else wifeName = "Mother's Name Not Recorded";
  116. }
  117. hName = new JLabel("H: " + husbName);
  118. wName = new JLabel("W: " + wifeName);
  119. famRt.add(hName);
  120. famRt.add(wName);
  121. famRow.add(famLeft);
  122. famRow.add(famRt);
  123. picker.add(famRow);
  124. picker.add(Box.createRigidArea(new Dimension(0,8)));
  125. // MARRIAGES
  126. JPanel marriageBox = new JPanel();
  127. marriageBox.setLayout(new BoxLayout(marriageBox, BoxLayout.LINE_AXIS));
  128. JLabel marriageLabel = new JLabel("Marriages: ");
  129. marriageBox.add(marriageLabel);
  130. spouseList = new JTextArea();
  131. JScrollPane spicePane = new JScrollPane(spouseList);
  132. spicePane.setMaximumSize(new Dimension(350, 60));
  133. spicePane.setMinimumSize(new Dimension(350, 30));
  134. String multiLine = "Not Married";
  135. if (ind.marriages.size() > 0) {
  136. multiLine = "";
  137. String[] spice = PersonEditor.genSpouseMenu(ind, ind.marriages);
  138. for (int i=0; i < spice.length; i++) {
  139. if (i > 0) multiLine += "\n";
  140. multiLine += spice[i];
  141. }
  142. }
  143. spouseList.setText(multiLine);
  144. spouseList.setEditable(false);
  145. marriageBox.add(spicePane);
  146. picker.add(Box.createRigidArea(new Dimension(0,8)));
  147. picker.add(marriageBox);
  148. picker.add(Box.createRigidArea(new Dimension(0,8)));
  149. // OPTIONAL LIST OF SELECTIONS
  150. if (! oneValue) {
  151. JPanel pickBox = new JPanel();
  152. pickBox.setLayout(new BoxLayout(pickBox, BoxLayout.LINE_AXIS));
  153. JLabel picks = new JLabel("Chosen So Far: ");
  154. pickList = new JTextArea();
  155. JScrollPane pickPane = new JScrollPane(pickList);
  156. pickList.setEditable(false);
  157. pickPane.setMinimumSize(new Dimension(350, 50));
  158. pickPane.setMaximumSize(new Dimension(350, 100));
  159. pickBox.add(picks);
  160. pickBox.add(pickPane);
  161. picker.add(pickBox);
  162. picker.add(Box.createRigidArea(new Dimension(0,12)));
  163. } // end of multi-Valued selections list
  164. // BUTTONS AT THE BOTTOM
  165. JPanel bottomBtns = new JPanel();
  166. bottomBtns.setLayout(new BoxLayout(bottomBtns, BoxLayout.LINE_AXIS));
  167. JButton done = new JButton("Done");
  168. done.setActionCommand("done");
  169. done.addActionListener(listener);
  170. done.setEnabled(true);
  171. bottomBtns.add(done);
  172. unDo = new JButton("Un-Do");
  173. unDo.setActionCommand("un-do");
  174. unDo.addActionListener(listener);
  175. unDo.setEnabled(false);
  176. bottomBtns.add(unDo);
  177. choose = new JButton("Choose");
  178. choose.setActionCommand("choose");
  179. choose.addActionListener(listener);
  180. choose.setEnabled(true);
  181. bottomBtns.add(choose);
  182. picker.add(bottomBtns);
  183. getContentPane().add(picker);
  184. addInternalFrameListener(this);
  185. setSize(450, 400);
  186. setVisible(true);
  187. } // end of method buildEditor
  188. public class PPListener implements ActionListener {
  189. KSJInternalFrame p_picker;
  190. public PPListener(KSJInternalFrame ped) {
  191. p_picker = ped;
  192. } // end of constructor
  193. public void actionPerformed(ActionEvent e) {
  194. if (e.getActionCommand().equals("pick person")) {
  195. int where = personPick.getSelectedIndex();
  196. ind = (Individual)source.get(where);
  197. fullName.setText(ind.name);
  198. surName.setText(ind.surname);
  199. String bday = "", dday = "";
  200. if (ind.hasDoB()) bday = ind.getDateOfBirth();
  201. if (ind.hasDoD()) dday = ind.getDateOfDeath();
  202. birthday.setText("Born: " + bday);
  203. deathday.setText("Died: " + dday);
  204. gender.setText("Gender: " + ind.gender);
  205. String husbName = "Birth Family Not Recorded", wifeName = "";
  206. if (ind.birthFamily != null) {
  207. if (ind.birthFamily.husband != null) husbName = ind.birthFamily.husband.name;
  208. else husbName = "Father's Name Not Recorded";
  209. if (ind.birthFamily.wife != null) wifeName = ind.birthFamily.wife.name;
  210. else wifeName = "Mother's Name Not Recorded";
  211. }
  212. hName.setText("H: " + husbName);
  213. wName.setText("W: " + wifeName);
  214. String[] spice = PersonEditor.genSpouseMenu(ind, ind.marriages);
  215. String multiLine = "";
  216. for (int i=0; i < spice.length; i++) {
  217. if (i > 0) multiLine += "\n";
  218. multiLine += spice[i];
  219. }
  220. spouseList.setText(multiLine);
  221. picker.getBounds(bnds);
  222. picker.repaint(bnds);
  223. } // end of picked-a-person
  224. else if (e.getActionCommand().equals("done")) {
  225. if (selections.isEmpty()) {
  226. int choice = JOptionPane.showConfirmDialog(p_picker,
  227. "You have not made any selections.\n" +
  228. "Do you want to close without choosing?",
  229. "Confirm", JOptionPane.YES_NO_OPTION);
  230. if (choice == JOptionPane.YES_OPTION) fieldName = "no-field"; // just close
  231. else return;
  232. } // end of no-selections
  233. if (fieldName.equals("husband")) { // called by a FamEditor to pick a husband
  234. FamilyEditor editor = (FamilyEditor)genericEditor;
  235. editor.fam.husband = (Individual)selections.get(0); // families have only 1 husband
  236. editor.fam.husband.marriages.add(editor.fam);
  237. editor.buildSpouseRow(editor.hRow, "husband");
  238. editor.getBounds(bnds);
  239. editor.repaint(bnds);
  240. // end of hubby-pickin'
  241. } else if (fieldName.equals("wife")) {
  242. FamilyEditor editor = (FamilyEditor)genericEditor;
  243. editor.fam.wife = (Individual)selections.get(0); // families have only 1 wife
  244. editor.fam.wife.marriages.add(editor.fam);
  245. editor.buildSpouseRow(editor.wRow, "wife");
  246. editor.getBounds(bnds);
  247. editor.repaint(bnds);
  248. // end of wife-pickin'
  249. } else if (fieldName.equals("children")) {
  250. FamilyEditor editor = (FamilyEditor)genericEditor;
  251. editor.fam.children.addAll(selections);
  252. for (int k=0; k < selections.size(); k++) // fill in the kids' birth families
  253. ((Individual)selections.get(k)).birthFamily = editor.fam;
  254. // update display in the calling FamilyEditor's Children ComboBox
  255. editor.buildKidBox();
  256. editor.getBounds(bnds);
  257. editor.repaint(bnds);
  258. // end of kid-pickin' -- without labor pains
  259. } else if (fieldName.equals("delete children")) {
  260. FamilyEditor editor = (FamilyEditor)genericEditor;
  261. Individual deletee;
  262. for (int k=0; k < selections.size(); k++) { // delete kids - nullify birth families
  263. deletee = (Individual)selections.get(k);
  264. deletee.birthFamily = null;
  265. editor.fam.children.remove(deletee);
  266. }
  267. // update display in the calling FamilyEditor's Children ComboBox
  268. editor.buildKidBox();
  269. editor.getBounds(bnds);
  270. editor.repaint(bnds);
  271. // end of kid-deletion -- without killing the kid
  272. } else if (fieldName.equals("Add UDP")) {
  273. PersonEditor editor = (PersonEditor)genericEditor;
  274. for (int j=0; j < selections.size(); j++)
  275. editor.udCopy.value.add(selections.get(j));
  276. // update display in the calling PersonEditor
  277. editor.buildUDPArea(udpNmbr);
  278. editor.getBounds(bnds);
  279. editor.repaint(bnds);
  280. // end of adding-people-to-UDP-value'
  281. } else if (fieldName.equals("Delete UDP")) {
  282. PersonEditor editor = (PersonEditor)genericEditor;
  283. int victim;
  284. for (int j=0; j < selections.size(); j++) {
  285. victim = editor.udCopy.value.indexOf(selections.get(j));
  286. editor.udCopy.value.remove(victim);
  287. }
  288. // update display in the calling PersonEditor
  289. editor.buildUDPArea(udpNmbr);
  290. editor.getBounds(bnds);
  291. editor.repaint(bnds);
  292. // end of deleting-people-from-UDP-value'
  293. } else if (fieldName.equals("marriages")) {
  294. PersonEditor editor = (PersonEditor)genericEditor;
  295. Individual mate = (Individual)selections.get(0);
  296. Family bliss = new Family(editor.ctxt, editor.ind, mate);
  297. // update display in the calling PersonEditor
  298. if (editor.spousePicker != null)
  299. editor.spousePicker.addItem(mate.name + " (" + mate.serialNmbr + ")");
  300. else { // spousePicker-was-null
  301. editor.marriageBox.removeAll();
  302. editor.buildMarriageBox();
  303. } // end of spousePicker-was-null
  304. editor.getBounds(bnds);
  305. editor.repaint(bnds);
  306. // launch a FamilyEditor to complete input of family info
  307. FamilyEditor fEd = new FamilyEditor(editor.ctxt, editor, "View/Edit a Marriage",
  308. bliss, "marriage");
  309. fEd.desktop = desktop;
  310. fEd.setLocation(350, 100);
  311. desktop.add(fEd);
  312. fEd.miViewMe = menuView.add(fEd.windowNum);
  313. fEd.miViewMe.addActionListener(fEd);
  314. fEd.menuView = menuView;
  315. fEd.show();
  316. fEd.moveToFront();
  317. try { fEd.setSelected(true);
  318. }catch(PropertyVetoException pv) {}
  319. // end of spouse-pickin' -- it should be so easy in the real world!!
  320. }
  321. try { p_picker.setClosed(true); // close the panel
  322. }catch(PropertyVetoException pve) {
  323. if (MainPane.activity == null) MainPane.createActivityLog(desktop, menuView);
  324. MainPane.activity.log.append("In PersonPicker:\n" + pve);
  325. }
  326. }
  327. else if (e.getActionCommand().equals("un-do")) {
  328. selections.remove(selections.size() - 1); // kill the last element
  329. if (selections.isEmpty()) unDo.setEnabled(false);
  330. if (oneValue) choose.setEnabled(true);
  331. else { // multi-selections-allowed
  332. String[] pickArray = ContextEditor.genIndMenu(selections); // make list of names
  333. String multiLine = "";
  334. for (int i=0; i < pickArray.length; i++) {
  335. if (i > 0) multiLine += "\n";
  336. multiLine += pickArray[i];
  337. }
  338. pickList.setText(multiLine);
  339. // picker.getBounds(bnds);
  340. // picker.repaint(bnds);
  341. } // end of if-multi-Value
  342. }
  343. else if (e.getActionCommand().equals("choose")) {
  344. selections.add(ind);
  345. unDo.setEnabled(true);
  346. if (oneValue) choose.setEnabled(false);
  347. else { // multi-selections-allowed
  348. String[] pickArray = ContextEditor.genIndMenu(selections);
  349. String multiLine = "";
  350. for (int i=0; i < pickArray.length; i++) {
  351. if (i > 0) multiLine += "\n";
  352. multiLine += pickArray[i];
  353. }
  354. pickList.setText(multiLine);
  355. // picker.getBounds(bnds);
  356. // picker.repaint(bnds);
  357. } // end of multi-selections-allowed
  358. } // end of action = choose
  359. } // end of ActionListerner method actionPerformed
  360. } // end of inner class PPListener
  361. } // end of class PersonPicker