PageRenderTime 37ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/code/MainPane.java

http://silkin.googlecode.com/
Java | 1192 lines | 1014 code | 35 blank | 143 comment | 190 complexity | c64a0fc2ad48ff173ec190db6287d020 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. import java.io.*;
  2. import java.util.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6. import javax.swing.text.*;
  7. import javax.swing.filechooser.*;
  8. import java.beans.*;
  9. /**
  10. The MainPane class is the top-level window for this application, holding all other windows in its contentPane
  11. (which is a JDesktopPane). There should be only one instance of MainPane. The static fields <code>activity</code> and
  12. <code>openBrowsers</code> hold, respectively, the system-wide Error Log and a list of all open LibBrowser windows
  13. (for good housekeeping purposes).
  14. <p>
  15. This class creates the MenuBar and is the ActionListener which implements menu choices. Menu choices which lead to
  16. exceptions trigger the instantiation of the Error Log and posting to it. The Error Log is intended to record all
  17. problems encountered and recommended solutions. Therefore, all methods that post to it must provide enough information
  18. about the problem for the User to know what specific action to take much later when the log is reviewed.
  19. @author Gary Morris, Northern Virginia Community College garymorris2245@verizon.net
  20. */
  21. public class MainPane extends JFrame implements ActionListener {
  22. public static KSJInternalFrame activity; // single activity log for all windows
  23. static ArrayList<Object> openBrowsers = new ArrayList<Object>(), openPersonEditors = new ArrayList<Object>(),
  24. openFamilyEditors = new ArrayList<Object>();
  25. public static ContextEditor curr_CUC_Editor;
  26. static JMenu menuView;
  27. public JMenu menuFileAdd2Library;
  28. public JMenuItem miFileOpenSILK, miFileDelete, miFileDeleteContext, miFileSaveContext;
  29. public static JDesktopPane desktop;
  30. public static MainPane topPane;
  31. public static int testSerialNmbr;
  32. public static int buildNmbr = 11;
  33. public static int NUMBER_OF_EGOS = 4;
  34. public static boolean fill_In_Flag = false;
  35. private JMenuBar menuBar = new JMenuBar();
  36. JMenu menuAdmin;
  37. KSJInternalFrame currentFrame;
  38. Runtime topDog = Runtime.getRuntime();
  39. JFileChooser fc = null;
  40. File currDomTheoryDir = null, currGEDCOMDir = null;
  41. int numWindows = 0;
  42. public static TreeMap treeMapDeepCopy(TreeMap model) {
  43. // this utility method returns a "deep copy" with new elements replacing any
  44. // subordinate TreeMaps or ALists as VALUES in the model.
  45. // KEYS are not deep-copied.
  46. TreeMap newTM = new TreeMap();
  47. Iterator topIter = model.entrySet().iterator();
  48. while (topIter.hasNext()) {
  49. Map.Entry entry = (Map.Entry) topIter.next();
  50. Object key = entry.getKey(),
  51. value = entry.getValue(),
  52. newValue;
  53. if (value instanceof TreeMap) {
  54. newValue = treeMapDeepCopy((TreeMap) value);
  55. } else if (value instanceof ArrayList) {
  56. newValue = new ArrayList<Object>((ArrayList<Object>) value);
  57. } else {
  58. newValue = value;
  59. }
  60. newTM.put(key, newValue);
  61. } // end of loop thru top level of model TMap
  62. return newTM;
  63. } // end of method treeMapDeepCopy
  64. public MainPane(String title) {
  65. super(title);
  66. createMenu();
  67. setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  68. desktop = new JDesktopPane();
  69. topPane = this;
  70. setContentPane(desktop);
  71. Toolkit tKit = getToolkit();
  72. createActivityLog(desktop, menuView);
  73. Dimension wndSize = tKit.getScreenSize();
  74. int maxHi = 768, maxWide = 1024;
  75. setBounds(40, 0,
  76. Math.min(maxWide, (int) (wndSize.width / 1.1)),
  77. Math.min(maxHi, (int) (wndSize.height / 1.1)));
  78. setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
  79. // Listen for window closing, so Library.stubs file can be saved before exit.
  80. topDog.addShutdownHook(new CleanUpThread());
  81. setVisible(false);
  82. } // end of constructor
  83. public static void displayError(String msg, String title, int typ) {
  84. if (activity == null) {
  85. createActivityLog(desktop, menuView);
  86. }
  87. JFrame topWindow = topPane;
  88. if (topWindow == null || !topWindow.isVisible()) {
  89. topWindow = SIL_Edit.editWindow;
  90. }
  91. JOptionPane.showMessageDialog(topWindow, msg, title, typ);
  92. MainPane.activity.log.append(msg + "\n\n");
  93. }
  94. /** Add a window to the desktop, and make it the current focus.
  95. @param wind A window to be added to the system's desktop & displayed. */
  96. public void addWindow(KSJInternalFrame wind) {
  97. currentFrame = wind;
  98. numWindows++;
  99. wind.desktop = desktop;
  100. wind.miViewMe = menuView.add(wind.windowNum);
  101. wind.miViewMe.addActionListener(wind);
  102. wind.menuView = menuView;
  103. desktop.add(wind);
  104. wind.show();
  105. }
  106. public void createMenu() {
  107. // menu bar
  108. setJMenuBar(menuBar);
  109. JMenu menuFile = new JMenu("File");
  110. menuFile.setMnemonic(KeyEvent.VK_F);
  111. JMenu menuEdit = new JMenu("Edit");
  112. menuEdit.setMnemonic(KeyEvent.VK_E);
  113. menuView = new JMenu("View");
  114. menuView.setMnemonic(KeyEvent.VK_V);
  115. menuAdmin = new JMenu("Admin");
  116. JMenu menuHelp = new HelpFrame.HMenu();
  117. menuHelp.setText("Help");
  118. menuHelp.setMnemonic(KeyEvent.VK_H);
  119. HelpFrame.help = new HelpFrame();
  120. // JMenu menuFileNew = new JMenu("New");
  121. JMenuItem miFileNewBrowser = menuFile.add("New Library Browser");
  122. miFileNewBrowser.setActionCommand("new browser");
  123. miFileNewBrowser.addActionListener(this);
  124. // JMenuItem miFileNewContext = menuFileNew.add("Context (Language)");
  125. // miFileNewContext.setActionCommand("new context");
  126. // miFileNewContext.addActionListener(this);
  127. // miFileNewSuggest = menuFileNew.add("Suggestions");
  128. // miFileNewSuggest.setEnabled(false);
  129. // menuFile.add(menuFileNew);
  130. JMenuItem miFileClose = menuFile.add("Close Window");
  131. miFileClose.setActionCommand("close window");
  132. miFileClose.addActionListener(this);
  133. miFileOpenSILK = menuFile.add("Open SILK File");
  134. miFileOpenSILK.setActionCommand("open silk");
  135. miFileOpenSILK.addActionListener(this);
  136. JMenuItem miFileOpenContext = menuFile.add("Open Context");
  137. miFileOpenContext.setActionCommand("open context");
  138. miFileOpenContext.addActionListener(this);
  139. menuFileAdd2Library = new JMenu("Add to Library...");
  140. JMenuItem miFileAdd2LibraryDT = menuFileAdd2Library.add("Domain Theory");
  141. miFileAdd2LibraryDT.setActionCommand("add domain theory");
  142. miFileAdd2LibraryDT.addActionListener(this);
  143. JMenuItem miFileAdd2LibraryBatch = menuFileAdd2Library.add("Batch Add Dom Thys");
  144. miFileAdd2LibraryBatch.setActionCommand("add batch of domain theories");
  145. miFileAdd2LibraryBatch.addActionListener(this);
  146. JMenuItem miFileAdd2LibraryCen = menuFileAdd2Library.add("People Data (.cen)");
  147. miFileAdd2LibraryCen.setEnabled(false);
  148. JMenuItem miFileAdd2LibraryGEDCOM = menuFileAdd2Library.add("GEDCOM File (.ged)");
  149. miFileAdd2LibraryGEDCOM.setEnabled(false);
  150. menuFile.add(menuFileAdd2Library);
  151. miFileDelete = menuFile.add("Delete From Library...");
  152. miFileDelete.setActionCommand("delete from library");
  153. miFileDelete.addActionListener(this);
  154. miFileDeleteContext = menuFile.add("Delete Context");
  155. miFileDeleteContext.setActionCommand("delete context");
  156. miFileDeleteContext.addActionListener(this);
  157. menuFile.addSeparator();
  158. miFileSaveContext = menuFile.add("Save Context");
  159. miFileSaveContext.setActionCommand("save context");
  160. miFileSaveContext.addActionListener(this);
  161. JMenuItem miFileSaveContextAs = menuFile.add("Save Context As...");
  162. miFileSaveContextAs.setActionCommand("save context as");
  163. miFileSaveContextAs.addActionListener(this);
  164. JMenu menuFileExport = new JMenu("Export File ...");
  165. JMenuItem miFileExportDT = menuFileExport.add("Domain Theory (.thy)");
  166. miFileExportDT.setActionCommand("export domain theory");
  167. miFileExportDT.addActionListener(this);
  168. JMenuItem miFileExportCen = menuFileExport.add("People Data (.cen)");
  169. miFileExportCen.setEnabled(false);
  170. JMenuItem miFileExportGEDCOM = menuFileExport.add("GEDCOM File (.ged)");
  171. miFileExportGEDCOM.setActionCommand("export GEDCOM");
  172. miFileExportGEDCOM.addActionListener(this);
  173. menuFile.add(menuFileExport);
  174. menuFile.addSeparator();
  175. JMenuItem miFileExit = menuFile.add("Exit SILKin");
  176. miFileExit.setActionCommand("exit");
  177. miFileExit.addActionListener(this);
  178. JMenuItem miEditUnDo = menuEdit.add("Un-Do");
  179. miEditUnDo.setEnabled(false);
  180. JMenuItem miEditReDo = menuEdit.add("Re-Do");
  181. miEditReDo.setEnabled(false);
  182. menuEdit.addSeparator();
  183. JMenuItem miEditCut = new JMenuItem(new DefaultEditorKit.CutAction());
  184. miEditCut.setText("Cut");
  185. miEditCut.setMnemonic(KeyEvent.VK_T);
  186. menuEdit.add(miEditCut);
  187. JMenuItem miEditCopy = new JMenuItem(new DefaultEditorKit.CopyAction());
  188. miEditCopy.setText("Copy");
  189. miEditCopy.setMnemonic(KeyEvent.VK_C);
  190. menuEdit.add(miEditCopy);
  191. JMenuItem miEditPaste = new JMenuItem(new DefaultEditorKit.PasteAction());
  192. miEditPaste.setText("Paste");
  193. miEditPaste.setMnemonic(KeyEvent.VK_P);
  194. menuEdit.add(miEditPaste);
  195. // JMenuItem miEditDelete = menuEdit.add("Delete");
  196. // JMenuItem miEditSelectAll= menuEdit.add("Select All");
  197. menuEdit.addSeparator();
  198. JMenuItem miEditPrefs = menuEdit.add("Edit Prefs");
  199. miEditPrefs.setActionCommand("edit prefs");
  200. miEditPrefs.addActionListener(this);
  201. JMenuItem miEditUserContext = menuEdit.add("Edit User Context");
  202. miEditUserContext.setActionCommand("edit user context");
  203. miEditUserContext.addActionListener(this);
  204. JMenuItem miTest1 = menuAdmin.add("Compute Similarity Matrix");
  205. miTest1.setActionCommand("Compute Similarity Matrix");
  206. miTest1.addActionListener(this);
  207. JMenuItem miTest2 = menuAdmin.add("Make Curr Lang dyadsUndefined");
  208. miTest2.setActionCommand("Make Curr Lang dyadsUndefined");
  209. miTest2.addActionListener(this);
  210. JMenuItem miTest3 = menuAdmin.add("Match dyadsUndefined");
  211. miTest3.setActionCommand("Match dyadsUndefined");
  212. miTest3.addActionListener(this);
  213. JMenuItem miTest4 = menuAdmin.add("Cluster the Feature Vectors");
  214. miTest4.setActionCommand("Cluster the Feature Vectors");
  215. miTest4.addActionListener(this);
  216. JMenuItem miTest5 = menuAdmin.add("Gen CUC Population");
  217. miTest5.setActionCommand("Gen CUC Population");
  218. miTest5.addActionListener(this);
  219. JMenuItem miTest6 = menuAdmin.add("Gen Indexes");
  220. miTest6.setActionCommand("Gen Indexes");
  221. miTest6.addActionListener(this);
  222. JMenuItem miTest7 = menuAdmin.add("Merge Indexes");
  223. miTest7.setActionCommand("Merge Indexes");
  224. miTest7.addActionListener(this);
  225. JMenuItem miTest10 = menuAdmin.add("Simulate User Data");
  226. miTest10.setActionCommand("Simulate User Data");
  227. miTest10.addActionListener(this);
  228. JMenuItem miTest11 = menuAdmin.add("Leave One Out");
  229. miTest11.setActionCommand("Leave One Out");
  230. miTest11.addActionListener(this);
  231. JMenuItem miTest12 = menuAdmin.add("Leave 1 Instant Replay");
  232. miTest12.setActionCommand("Leave 1 Instant Replay");
  233. miTest12.addActionListener(this);
  234. menuBar.add(menuFile);
  235. menuBar.add(menuEdit);
  236. menuBar.add(menuView);
  237. menuBar.add(menuAdmin);
  238. menuBar.add(menuHelp);
  239. if (Library.currentActivity != Library.ADMIN) {
  240. menuAdmin.setEnabled(false);
  241. }
  242. menuBar.validate();
  243. } // end of method createMenu
  244. public void enableAdvancedMenuItems(boolean bool) {
  245. menuFileAdd2Library.setEnabled(bool);
  246. miFileDelete.setEnabled(bool);
  247. miFileDeleteContext.setEnabled(bool);
  248. }
  249. // Method required to implement ActionListener interface
  250. public void actionPerformed(ActionEvent e) {
  251. if (e.getActionCommand().equals("new browser")) {
  252. browseLibrary();
  253. } // end of action-is-new-browser
  254. else if (e.getActionCommand().equals("new context")) {
  255. // FIX ME !!! open a DG window, and pass sit instead. Activity -> DG
  256. try {
  257. Library.createUserContext(currentFrame);
  258. } catch (Exception ex) {
  259. activity.log.append("While creating a new context, " + ex + "\n\n");
  260. }
  261. changeActivity(Library.DATA_GATHERING);
  262. } // end of action-is-new-context
  263. else if (e.getActionCommand().equals("Match dyadsUndefined")) {
  264. try {
  265. DomainTheory.current.matchDyads();
  266. } catch (Exception ex) {
  267. activity.log.append("While matching dyadsUndefined, " + ex + "\n\n");
  268. }
  269. } // end of action-is-Match-dyadsUndefined
  270. else if (e.getActionCommand().equals("open context")) {
  271. // FIX ME !!! open a DG window, and pass it instead. Activity -> DG
  272. try {
  273. Library.chooseUserContext(currentFrame);
  274. } catch (Exception ex) {
  275. activity.log.append("While opening a new context, " + ex + "\n\n");
  276. }
  277. changeActivity(Library.DATA_GATHERING);
  278. } // end of action-is-new-context
  279. else if (e.getActionCommand().equals("open silk")) {
  280. if (fc == null) {
  281. fc = new JFileChooser();
  282. }
  283. fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
  284. fc.setDialogTitle("File from KinshipEditor (.silk)");
  285. // Display File-Open dialog and get User's selection
  286. int returnVal = fc.showOpenDialog(desktop);
  287. if (returnVal == JFileChooser.APPROVE_OPTION) { // User Finds File
  288. changeActivity(Library.DATA_GATHERING);
  289. File file = fc.getSelectedFile();
  290. try {
  291. Library.loadSILKFile(file);
  292. } catch (Exception ex) {
  293. activity.log.append("While opening a KinshipEditor (.silk) file:\n" + ex + "\n\n");
  294. }
  295. } // end of User Finds File
  296. } // end of action-is-open-kinedit
  297. else if (e.getActionCommand().equals("close window")) {
  298. KSJInternalFrame frame = (KSJInternalFrame) desktop.getSelectedFrame();
  299. // IF INSTANCE_OF DATA_GATHERING, SAVE THE CONTEXT TO DISK
  300. frame.doDefaultCloseAction();
  301. } // end of action-is-close-window
  302. else if (e.getActionCommand().equals("exit")) {
  303. if (SIL_Edit.editWindow != null
  304. && SIL_Edit.editWindow.chart.dirty) {
  305. SIL_Edit.editWindow.chart.doWantToSave();
  306. }
  307. System.exit(0);
  308. } else if (e.getActionCommand().equals("delete from library")) {
  309. // First, let User choose what to language to delete.
  310. String[] langs = Library.genCtxtMenu("All");
  311. String victim = (String) JOptionPane.showInputDialog(desktop,
  312. "1) For what language would you like to make a deletion?",
  313. "What To Delete",
  314. JOptionPane.PLAIN_MESSAGE,
  315. null,
  316. langs,
  317. langs[0]);
  318. //Check for cancellation.
  319. if (victim == null) {
  320. return;
  321. }
  322. if (victim.equals("All")) {
  323. String vict;
  324. Library.ContextStub cs1;
  325. File aboutToDi;
  326. try {
  327. for (int k = 1; k < langs.length; k++) {
  328. vict = langs[k];
  329. cs1 = Library.retrieveOrCreateStub(vict);
  330. Library.removeContextStub(cs1);
  331. aboutToDi = new File(Library.libraryCtxtDirectory + vict + ".ctxt");
  332. aboutToDi.delete();
  333. }
  334. Library.cbIndex = null;
  335. Library.writeStubFile();
  336. } catch (Exception exc) {
  337. activity.log.append("While deleting Library files:\n" + exc + "\n\n");
  338. } // end of catch block
  339. } // end of delete-All
  340. else {
  341. Library.ContextStub cs = Library.retrieveOrCreateStub(victim);
  342. Context actxt = (Context) Library.activeContexts.get(victim);
  343. String[] choices;
  344. if (cs.adrThyExists && cs.refThyExists) { // a context with 2 domain theories
  345. choices = new String[4];
  346. choices[3] = "Both domain theories";
  347. choices[1] = victim + " domain theory";
  348. choices[2] = victim + " (Adr) domain theory";
  349. choices[0] = "The entire " + victim + " context & its 2 domain theories";
  350. } else if (cs.refThyExists) {
  351. choices = new String[2];
  352. choices[1] = victim + " domain theory";
  353. choices[0] = "The entire " + victim + " context and its domain theory";
  354. } else if (cs.adrThyExists) {
  355. choices = new String[2];
  356. choices[1] = victim + " (Adr) domain theory";
  357. choices[0] = "The entire " + victim + " context and its domain theory";
  358. } else {
  359. choices = new String[1];
  360. choices[0] = "The entire " + victim + " context. (It has no domain theory.)";
  361. }
  362. String choice = (String) JOptionPane.showInputDialog(desktop,
  363. "2) Which elements of the context for that language \nwould you like to delete?",
  364. "What To Delete",
  365. JOptionPane.PLAIN_MESSAGE,
  366. null,
  367. choices,
  368. choices[0]);
  369. if (choice == null) {
  370. return;
  371. }
  372. String[] onlyAlso = {"Card File ONLY", "Disk ALSO"};
  373. String eraser = (String) JOptionPane.showInputDialog(desktop,
  374. "3) Delete from the Library's 'card file' only?\n Or delete the file from your computer also?",
  375. "What To Delete",
  376. JOptionPane.PLAIN_MESSAGE,
  377. null,
  378. onlyAlso,
  379. "Disk ALSO");
  380. if (eraser == null) {
  381. return;
  382. }
  383. String filePath = Library.libraryCtxtDirectory + victim;
  384. File aboutToDie;
  385. try {
  386. if (choice.substring(0, 10).equals("The entire")) { // kill the whole context
  387. Library.removeContextStub(cs);
  388. Library.writeStubFile();
  389. if (eraser.substring(0, 4).equals("Disk")) {
  390. aboutToDie = new File(filePath + ".ctxt");
  391. aboutToDie.delete();
  392. filePath = Library.thyDirectory + victim;
  393. aboutToDie = new File(filePath + ".thy");
  394. if (aboutToDie.exists()) {
  395. aboutToDie.delete();
  396. }
  397. aboutToDie = new File(filePath + "(Adr).thy");
  398. if (aboutToDie.exists()) {
  399. aboutToDie.delete();
  400. }
  401. } // end of erase-disk-also
  402. } else if (choice.substring(0, 4).equals("Both")) { // choice-equals-both-theories
  403. cs.refThyExists = false;
  404. cs.adrThyExists = false;
  405. if (actxt == null) {
  406. actxt = Library.readContextFromDisk(filePath + ".ctxt");
  407. } // end of this-context-was-not-active
  408. actxt.domTheoryAdrNullify();
  409. actxt.domTheoryRefNullify();
  410. if (eraser.substring(0, 4).equals("Disk")) { // erase disk = over-write .ctxt file
  411. Library.saveContextToDisk(actxt);
  412. filePath = Library.thyDirectory + victim;
  413. aboutToDie = new File(filePath + ".thy");
  414. if (aboutToDie.exists()) {
  415. aboutToDie.delete();
  416. }
  417. aboutToDie = new File(filePath + "(Adr).thy");
  418. if (aboutToDie.exists()) {
  419. aboutToDie.delete();
  420. }
  421. }
  422. } else if (choice.indexOf("(Adr)") >= 0) {
  423. cs.adrThyExists = false;
  424. if (actxt == null) {
  425. actxt = Library.readContextFromDisk(filePath + ".ctxt");
  426. } // end of this-context-was-not-active
  427. actxt.domTheoryAdrNullify();
  428. if (eraser.substring(0, 4).equals("Disk")) { // erase disk = over-write .ctxt file
  429. Library.saveContextToDisk(actxt);
  430. filePath = Library.thyDirectory + victim;
  431. aboutToDie = new File(filePath + "(Adr).thy");
  432. if (aboutToDie.exists()) {
  433. aboutToDie.delete();
  434. }
  435. }
  436. } else {
  437. cs.refThyExists = false;
  438. if (actxt == null) {
  439. actxt = Library.readContextFromDisk(filePath + ".ctxt");
  440. } // end of this-context-was-not-active
  441. actxt.domTheoryRefNullify();
  442. if (eraser.substring(0, 4).equals("Disk")) { // erase disk = over-write .ctxt file
  443. Library.saveContextToDisk(actxt);
  444. filePath = Library.thyDirectory + victim;
  445. aboutToDie = new File(filePath + ".thy");
  446. if (aboutToDie.exists()) {
  447. aboutToDie.delete();
  448. }
  449. }
  450. }
  451. } catch (Exception exc) {
  452. activity.log.append("While deleting Library files:\n" + exc + "\n\n");
  453. }
  454. } // end of don't-delete-all
  455. for (int i = 0; i < MainPane.openBrowsers.size(); i++) {
  456. LibBrowser lb = (LibBrowser) MainPane.openBrowsers.get(i);
  457. lb.picker.refreshLangMenu();
  458. } // end of loop thru MainPane.openBrowsers
  459. JOptionPane.showMessageDialog(desktop,
  460. "Requested deletions are completed.",
  461. "Task Completed",
  462. JOptionPane.PLAIN_MESSAGE);
  463. } // end of action-command-is-delete
  464. else if (e.getActionCommand().equals("add domain theory")) {
  465. if (fc == null) {
  466. fc = new JFileChooser();
  467. }
  468. fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
  469. fc.setDialogTitle("File holding Domain Theory (Horn Clauses)");
  470. if (currDomTheoryDir != null) {
  471. fc.setCurrentDirectory(currDomTheoryDir);
  472. }
  473. // Display File-Open dialog and get User's selection
  474. int returnVal = fc.showOpenDialog(desktop);
  475. if (returnVal == JFileChooser.APPROVE_OPTION) {
  476. File file = fc.getSelectedFile();
  477. try {
  478. Library.loadNewDomTh(file);
  479. DomainTheory dt = DomainTheory.current;
  480. Context.simulation = false;
  481. dt.ctxt.simDataGen = false;
  482. String lang = dt.languageName, filePath;
  483. for (int i = 0; i < MainPane.openBrowsers.size(); i++) {
  484. LibBrowser lb = (LibBrowser) MainPane.openBrowsers.get(i);
  485. lb.picker.refreshLangMenu();
  486. } // end of loop thru MainPane.openBrowsers
  487. // Create a FeatureVector, postDistributions & write to disk
  488. ArrayList<Object> egoList = new ArrayList<Object>();
  489. if (FeatureVectorObj.el_1_distn == null) {
  490. Library.readDistributionsFromFile();
  491. }
  492. FeatureVectorObj fv1 = dt.computeFeatureVector(egoList);
  493. Library.postDistributions(fv1);
  494. filePath = Library.libraryDirectory + "Feature Vectors/" + fv1.languageName + ".fvect";
  495. PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(filePath)));
  496. fv1.toDisk(out);
  497. dt.findOverlappingTerms(egoList);
  498. dt.findHiddenNeuterEgos();
  499. currDomTheoryDir = fc.getCurrentDirectory();
  500. filePath = Library.libraryDirectory + "Domain Theory Files/" + lang + ".thy";
  501. out = new PrintWriter(new BufferedWriter(new FileWriter(filePath)));
  502. dt.toThyFile(out);
  503. Library.saveContextToDisk(dt.ctxt);
  504. JOptionPane.showMessageDialog(desktop,
  505. lang + " Successfully Added to Library.",
  506. "Task Completed",
  507. JOptionPane.PLAIN_MESSAGE);
  508. } catch (KSConstraintInconsistency exc) {
  509. int where = exc.toString().indexOf(":");
  510. String msg = "PROBLEM: " + exc.toString().substring(where + 1)
  511. + "\nRECOMMENDATION: Correct or remove this clause from domain theory.\n "
  512. + "Then delete this theory from the Library & re-add it.";
  513. displayError(msg, "Horn Clause Error - Constraints", JOptionPane.ERROR_MESSAGE);
  514. } catch (KSBadHornClauseException exc) {
  515. int where = exc.toString().indexOf(":");
  516. String msg = "PROBLEM: " + exc.toString().substring(where + 1)
  517. + "\nRECOMMENDATION: Correct or remove this clause from domain theory.\n "
  518. + "Then delete this theory from the Library & re-add it.";
  519. displayError(msg, "Horn Clause Error", JOptionPane.ERROR_MESSAGE);
  520. } catch (KSParsingErrorException exc) {
  521. int where = exc.toString().indexOf(":");
  522. String msg = "PROBLEM: " + exc.toString().substring(where + 1)
  523. + "\nRECOMMENDATION: Correct this line number & try again.";
  524. JOptionPane.showMessageDialog(desktop, msg, "Parsing Error",
  525. JOptionPane.ERROR_MESSAGE);
  526. activity.log.append(msg + "\n\n");
  527. } catch (ClassNotFoundException exc) {
  528. int where = exc.toString().indexOf(":");
  529. String msg = "PROBLEM: " + exc.toString().substring(where + 1)
  530. + "\nRECOMMENDATION: Submit Bug Report.";
  531. JOptionPane.showMessageDialog(desktop, msg, "Missing Class encountered",
  532. JOptionPane.ERROR_MESSAGE);
  533. activity.log.append(msg + "\n\n");
  534. } catch (KSInternalErrorException exc) {
  535. int where = exc.toString().indexOf(":");
  536. String msg = "PROBLEM: " + exc.toString().substring(where + 1)
  537. + "\nRECOMMENDATION: To replace an existing theory, delete the old"
  538. + " version, then add the new version.";
  539. JOptionPane.showMessageDialog(desktop, msg, "Duplicate Domain Theory",
  540. JOptionPane.ERROR_MESSAGE);
  541. activity.log.append(msg + "\n\n");
  542. } catch (NotSerializableException exc) {
  543. int where = exc.toString().indexOf(":");
  544. String msg = "PROBLEM: Internal Error: " + exc
  545. + "\nRECOMMENDATION: Submit Bug Report.";
  546. JOptionPane.showMessageDialog(desktop, msg, "Internal Error",
  547. JOptionPane.ERROR_MESSAGE);
  548. activity.log.append(msg + "\n\n");
  549. } catch (IOException exc) {
  550. int where = exc.toString().indexOf(":");
  551. String msg = "PROBLEM: Writing out to Library, " + exc.toString().substring(where + 1)
  552. + "\nRECOMMENDATION: Check for disk, directory, or permissions problems.";
  553. JOptionPane.showMessageDialog(desktop, msg, "File System Error",
  554. JOptionPane.ERROR_MESSAGE);
  555. activity.log.append(msg + "\n\n");
  556. } catch (JavaSystemException exc) {
  557. int where = exc.toString().indexOf(":");
  558. String msg = "PROBLEM: Writing out to Library, " + exc.toString().substring(where + 1)
  559. + "\nRECOMMENDATION: Check for disk, directory, or permissions problems.";
  560. JOptionPane.showMessageDialog(desktop, msg, "File System Error",
  561. JOptionPane.ERROR_MESSAGE);
  562. activity.log.append(msg + "\n\n");
  563. } // end of catch blocks
  564. } // end of if-returnVal=Approve
  565. } // end of action-is-add-dom-thy
  566. else if (e.getActionCommand().equals("add batch of domain theories")) {
  567. if (fc == null) {
  568. fc = new JFileChooser();
  569. }
  570. fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  571. fc.setDialogTitle("Directory holding Domain Theories");
  572. if (currDomTheoryDir != null) {
  573. fc.setCurrentDirectory(currDomTheoryDir.getParentFile());
  574. }
  575. // Display File-Open dialog and get User's selection
  576. String lang = "";
  577. DomainTheory dt = null;
  578. int returnVal = fc.showOpenDialog(desktop);
  579. if (returnVal == JFileChooser.APPROVE_OPTION) {
  580. try {
  581. File dir = fc.getSelectedFile();
  582. if (!dir.isDirectory()) {
  583. throw new JavaSystemException("Selected file is not a directory.");
  584. }
  585. int doFVs = JOptionPane.showConfirmDialog(this, "Compute Feature Vectors?");
  586. int keep = JOptionPane.showConfirmDialog(this, "Keep Contexts Active?");
  587. int overWrite = JOptionPane.showConfirmDialog(this, "Allow Over-Write of Existing DTs?");
  588. Library.allowOverWrites = (overWrite == JOptionPane.YES_OPTION);
  589. currDomTheoryDir = dir;
  590. if (doFVs == JOptionPane.YES_OPTION && FeatureVectorObj.el_1_distn == null) {
  591. Library.resetDistributions();
  592. }
  593. File[] batch = dir.listFiles();
  594. for (int i = 0; i < batch.length; i++) {
  595. File file = batch[i];
  596. String fName = file.getName(), filePath, mod;
  597. if (fName.indexOf(".") != 0) {
  598. Library.loadNewDomTh(file);
  599. dt = DomainTheory.current;
  600. Context.current = dt.ctxt;
  601. Context.simulation = false;
  602. dt.ctxt.simDataGen = false;
  603. if (keep == JOptionPane.NO_OPTION) {
  604. lang = dt.languageName;
  605. int end = lang.indexOf("(");
  606. if (end > -1) {
  607. lang = lang.substring(0, end);
  608. }
  609. Library.activeContexts.remove(lang);
  610. }
  611. PrintWriter out;
  612. ArrayList<Object> egoList = new ArrayList<Object>();
  613. if (doFVs == JOptionPane.YES_OPTION) {
  614. // Create a FeatureVector & write to disk
  615. FeatureVectorObj fv1 = dt.computeFeatureVector(egoList);
  616. filePath = Library.libraryDirectory + "Feature Vectors/" + dt.languageName + ".fvect";
  617. out = new PrintWriter(new BufferedWriter(new FileWriter(filePath)));
  618. fv1.toDisk(out);
  619. } else {
  620. // Just asssure examples generated
  621. ClauseBody.priorPred = ""; // Needed for Clause Counts
  622. ClauseBody.seqTotal = 0;
  623. ClauseBody.dupTotal = 0;
  624. Iterator ktdIter = dt.theory.values().iterator();
  625. egoList = dt.maleAndFemaleCreatedHeThem();
  626. while (ktdIter.hasNext()) {
  627. KinTermDef ktd = (KinTermDef) ktdIter.next();
  628. if (dt.printableTerm(ktd)) {
  629. ktd.assureExamplesGenerated(egoList);
  630. if (ktd.eqcSigExact == null) {
  631. ktd.makeSigStrings();
  632. }
  633. }
  634. }
  635. // Print out the Clause Counts, etc.
  636. String pad = "";
  637. if (++ClauseBody.seq < 10) {
  638. pad = " ";
  639. } else if (ClauseBody.seq < 100) {
  640. pad = " ";
  641. }
  642. System.out.println(ClauseBody.priorPred + ": " + pad + ClauseBody.seq + " - " + ClauseBody.dups
  643. + " = " + (ClauseBody.seq - ClauseBody.dups));
  644. ClauseBody.seqTotal += ClauseBody.seq;
  645. System.out.println("\nTotal clauses for " + dt.languageName + " is " + ClauseBody.seqTotal
  646. + " - " + ClauseBody.dupTotal + " = " + (ClauseBody.seqTotal - ClauseBody.dupTotal) + "\n\n");
  647. }
  648. dt.findOverlappingTerms(egoList);
  649. dt.findHiddenNeuterEgos();
  650. filePath = Library.libraryDirectory + "Domain Theory Files/" + dt.languageName + ".thy";
  651. out = new PrintWriter(new BufferedWriter(new FileWriter(filePath)));
  652. dt.toThyFile(out);
  653. System.out.println("Wrote: " + filePath);
  654. Library.saveContextToDisk(Context.current);
  655. } // end of it's not an invisible file (e.g. .DSxxxx)
  656. } // end of loop thru DT files
  657. for (int i = 0; i < MainPane.openBrowsers.size(); i++) {
  658. LibBrowser lb = (LibBrowser) MainPane.openBrowsers.get(i);
  659. lb.picker.refreshLangMenu();
  660. } // end of loop thru MainPane.openBrowsers
  661. int num = batch.length - 1;
  662. JOptionPane.showMessageDialog(desktop,
  663. num + " DomTheories Added to Library.",
  664. "Task Completed",
  665. JOptionPane.PLAIN_MESSAGE);
  666. } catch (KSConstraintInconsistency exc) {
  667. int where = exc.toString().indexOf(":");
  668. String msg = "PROBLEM with " + dt.languageName + ": " + exc.toString().substring(where + 1)
  669. + "\nRECOMMENDATION: Correct or remove this clause from domain theory.\n "
  670. + "Then delete this theory from the Library & re-add it.";
  671. JOptionPane.showMessageDialog(desktop, msg, "Horn Clause Error - Constraints",
  672. JOptionPane.ERROR_MESSAGE);
  673. activity.log.append(msg + "\n\n");
  674. } catch (KSBadHornClauseException exc) {
  675. int where = exc.toString().indexOf(":");
  676. String msg = "PROBLEM with " + dt.languageName + ": " + exc.toString().substring(where + 1)
  677. + "\nRECOMMENDATION: Correct or remove this clause from domain theory.\n "
  678. + "Then delete this theory from the Library & re-add it.";
  679. JOptionPane.showMessageDialog(desktop, msg, "Horn Clause Error",
  680. JOptionPane.ERROR_MESSAGE);
  681. activity.log.append(msg + "\n\n");
  682. } catch (KSParsingErrorException exc) {
  683. int where = exc.toString().indexOf(":");
  684. String msg = "PROBLEM in Parsing: " + exc.toString().substring(where + 1)
  685. + "\nRECOMMENDATION: Correct this line number & try again.";
  686. JOptionPane.showMessageDialog(desktop, msg, "Parsing Error",
  687. JOptionPane.ERROR_MESSAGE);
  688. activity.log.append(msg + "\n\n");
  689. } catch (ClassNotFoundException exc) {
  690. int where = exc.toString().indexOf(":");
  691. String msg = "PROBLEM with " + dt.languageName + ": " + exc.toString().substring(where + 1)
  692. + "\nRECOMMENDATION: Submit Bug Report.";
  693. JOptionPane.showMessageDialog(desktop, msg, "Missing Class encountered",
  694. JOptionPane.ERROR_MESSAGE);
  695. activity.log.append(msg + "\n\n");
  696. } catch (KSInternalErrorException exc) {
  697. int where = exc.toString().indexOf(":");
  698. String msg = "PROBLEM with " + DomainTheory.current.languageName + ": " + exc.toString().substring(where + 1)
  699. + "\nRECOMMENDATION: To replace an existing theory, delete the old"
  700. + " version, then add the new version.";
  701. JOptionPane.showMessageDialog(desktop, msg, "Duplicate Domain Theory",
  702. JOptionPane.ERROR_MESSAGE);
  703. activity.log.append(msg + "\n\n");
  704. } catch (NotSerializableException exc) {
  705. int where = exc.toString().indexOf(":");
  706. String msg = "PROBLEM: Internal Error: " + exc.toString().substring(where + 1)
  707. + "\nRECOMMENDATION: Submit Bug Report.";
  708. JOptionPane.showMessageDialog(desktop, msg, "Internal Error",
  709. JOptionPane.ERROR_MESSAGE);
  710. activity.log.append(msg + "\n\n");
  711. } catch (IOException exc) {
  712. int where = exc.toString().indexOf(":");
  713. String msg = "PROBLEM: Writing out to Library, " + exc.toString().substring(where + 1)
  714. + "\nRECOMMENDATION: Check for disk, directory, or permissions problems.";
  715. JOptionPane.showMessageDialog(desktop, msg, "File System Error",
  716. JOptionPane.ERROR_MESSAGE);
  717. activity.log.append(msg + "\n\n");
  718. } catch (JavaSystemException exc) {
  719. int where = exc.toString().indexOf(":");
  720. String msg = "PROBLEM: Writing out to Library, " + exc.toString().substring(where + 1)
  721. + "\nRECOMMENDATION: Check for disk, directory, or permissions problems.";
  722. JOptionPane.showMessageDialog(desktop, msg, "File System Error",
  723. JOptionPane.ERROR_MESSAGE);
  724. activity.log.append(msg + "\n\n");
  725. } // end of catch blocks
  726. } // end of if-returnVal=Approve
  727. } // end of action-is-add-batch-of-dom-thys
  728. else if (e.getActionCommand().equals("delete context")) {
  729. // FINISH ME !!!
  730. try {
  731. Library.deleteContext(currentFrame);
  732. } catch (Exception ex) {
  733. activity.log.append("While deleting a context, " + ex + "\n\n");
  734. }
  735. changeActivity(Library.DATA_GATHERING);
  736. } // end of action-is-delete-context
  737. else if (e.getActionCommand().equals("save context")) {
  738. if (SIL_Edit.editWindow != null) {
  739. SIL_Edit.editWindow.chart.saveSILKinFile();
  740. }else try {
  741. Library.saveUserContext(currentFrame, false);
  742. } catch (Exception ex) {
  743. activity.log.append("While saving a context, " + ex + "\n\n");
  744. }
  745. changeActivity(Library.DATA_GATHERING);
  746. } // end of action-is-save-context
  747. else if (e.getActionCommand().equals("save context as")) {
  748. if (SIL_Edit.editWindow != null) {
  749. SIL_Edit.editWindow.chart.saveAsFile();
  750. }else try {
  751. Library.saveUserContext(currentFrame, true);
  752. } catch (Exception ex) {
  753. activity.log.append("While saving a context, " + ex + "\n\n");
  754. }
  755. changeActivity(Library.DATA_GATHERING);
  756. } // end of action-is-save-context-as
  757. else if (e.getActionCommand().equals("export GEDCOM")) {
  758. int population = Context.current.indSerNumGen + Context.current.famSerNumGen;
  759. if (population < 2) {
  760. JOptionPane.showMessageDialog(desktop,
  761. "You are requesting export of a total of " + population + " people and families.\n"
  762. + "That makes no sense. Make sure you have chosen the correct\n"
  763. + "context and generated a population before exporting.",
  764. "Nothing To Export",
  765. JOptionPane.ERROR_MESSAGE);
  766. } // end of too-small-population
  767. else { // normal-population-numberOfKinTerms
  768. if (fc == null) {
  769. fc = new JFileChooser();
  770. }
  771. fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
  772. fc.setDialogTitle("Location for GEDCOM file -- .ged extension recommended");
  773. if (currGEDCOMDir != null) {
  774. fc.setCurrentDirectory(currGEDCOMDir);
  775. }
  776. // Display File-Open dialog and get User's selection
  777. int returnVal = fc.showSaveDialog(desktop);
  778. if (returnVal == JFileChooser.APPROVE_OPTION) {
  779. File file = fc.getSelectedFile();
  780. currGEDCOMDir = fc.getCurrentDirectory();
  781. String fName = file.getName(), fPath = file.getPath();
  782. Object[] options2 = {"Include", "Don't Include"};
  783. int choice1 = 1;
  784. choice1 = JOptionPane.showOptionDialog(desktop,
  785. "Some names have 'flags' like <aux> embedded in them. Include those?",
  786. "Include Names With Tags?",
  787. JOptionPane.YES_NO_OPTION,
  788. JOptionPane.QUESTION_MESSAGE,
  789. null, //don't use a custom Icon
  790. options2, //the titles of buttons
  791. options2[1]); //default is "Don't Include"
  792. try {
  793. PrintWriter outFile = new PrintWriter(new BufferedWriter(new FileWriter(fPath)));
  794. Context.current.exportGEDCOM(outFile, fName, (String) options2[choice1]);
  795. outFile.flush();
  796. outFile.close();
  797. } catch (IOException exc) {
  798. String msg = "PROBLEM: While exporting a GEDCOM file, " + prettify(exc.toString())
  799. + "\nRECOMMENDATION: Check for disk, directory, or permissions problems.";
  800. JOptionPane.showMessageDialog(desktop, msg, "File System Error",
  801. JOptionPane.ERROR_MESSAGE);
  802. activity.log.append(msg + "\n\n");
  803. } catch (Exception exc) {
  804. String msg = "PROBLEM: While exporting a GEDCOM file, " + prettify(exc.toString())
  805. + "\nRECOMMENDATION: Submit a Bug Report!";
  806. JOptionPane.showMessageDialog(desktop, msg, "General Error",
  807. JOptionPane.ERROR_MESSAGE);
  808. activity.log.append(msg + "\n\n");
  809. } // end of catch bodies
  810. } // end of if-APPROVE_OPTION
  811. } // end of normal-population-numberOfKinTerms
  812. } // end of action-is-export-GEDCOM
  813. else if (e.getActionCommand().equals("export domain theory")) {
  814. // Add Here the code to write out a .thy file to location of User's choice (EXCEPT LIBRARY).
  815. // REMEMBER TO RE-USE FILE CHOOSER
  816. // DomTh.writeThyFile() is all set to go.
  817. } // end of action-is-export-domain-theory
  818. else if (e.getActionCommand().equals("edit prefs")) {
  819. if (SIL_Edit.editWindow != null) {
  820. SIL_Edit.editWindow.editPrefsItemActionPerformed(null);
  821. }else {
  822. String msg = "You may only edit Prefs for a Context Under Construction."
  823. + "\nThere currently is none.";
  824. JOptionPane.showMessageDialog(desktop, msg, "Invalid Command",
  825. JOptionPane.ERROR_MESSAGE);
  826. activity.log.append(msg + "\n\n");
  827. }
  828. } // end of action-is-edit-prefs
  829. else if (e.getActionCommand().equals("edit user context")) {
  830. editCUC();
  831. } // end of action-is-edit-context
  832. else if (e.getActionCommand().equals("Compute Similarity Matrix")) {
  833. try {
  834. Library.generateSimMatrix();
  835. } catch (Exception exc) {
  836. String msg = "PROBLEM: During FV generation, " + prettify(exc.toString())
  837. + "\nRECOMMENDATION: DeBug!";
  838. JOptionPane.showMessageDialog(desktop, msg, "Testing Error",
  839. JOptionPane.ERROR_MESSAGE);
  840. activity.log.append(msg + "\n\n");
  841. }
  842. } // end of action-is-Compute-Similarity-Matrix
  843. else if (e.getActionCommand().equals("Cluster the Feature Vectors")) {
  844. int minK, maxK;
  845. float penalty;
  846. try {
  847. String minKStr = JOptionPane.showInputDialog(this, "Enter Smallest Number of Clusters"),
  848. maxKStr = JOptionPane.showInputDialog(this, "Enter Largest Number of Clusters"),
  849. penaltyStr = JOptionPane.showInputDialog(this, "Enter Penalty Percentage (a whole number)");
  850. minK = Integer.parseInt(minKStr);
  851. maxK = Integer.parseInt(maxKStr);
  852. penalty = Float.parseFloat(penaltyStr) / 100f;
  853. Library.clusterFVs(minK, maxK, penalty);
  854. } catch (Exception exc) {
  855. String msg = "PROBLEM: During FV clustering, " + prettify(exc.toString())
  856. + "\nRECOMMENDATION: DeBug!";
  857. JOptionPane.showMessageDialog(desktop, msg, "Testing Error",
  858. JOptionPane.ERROR_MESSAGE);
  859. activity.log.append(msg + "\n\n");
  860. }
  861. } // end of action-is-Cluster-the-Feature-Vectors
  862. else if (e.getActionCommand().equals("Make Curr Lang dyadsUndefined")) {
  863. try {
  864. if (Context.current != null) {
  865. System.out.println("Generating dyadsUndefined for " + Context.current.languageName);
  866. DomainTheory.current.dyadsUndefined = new DyadTMap();
  867. // currentContext.cleanNodeFields();
  868. Context.current.addDyads((Individual) Context.current.individualCensus.get(0));
  869. }
  870. // ((JTextArea)diagramArea).setText(censusString() + dtm.summaryString());
  871. } catch (Exception exc) {
  872. String msg = "PROBLEM: During Dyad generation, " + prettify(exc.toString())
  873. + "\nRECOMMENDATION: DeBug!";
  874. JOptionPane.showMessageDialog(desktop, msg, "Testing Error",
  875. JOptionPane.ERROR_MESSAGE);
  876. activity.log.append(msg + "\n\n");
  877. }
  878. } // end of action-is-Make-dyadsUndefined
  879. else

Large files files are truncated, but you can click here to view the full file