00001
00002
00003
00004
00005
00006
00007 package com.dapissarenko.hierarchy;
00008
00009 import java.awt.Cursor;
00010 import java.awt.event.ActionEvent;
00011 import java.util.Collection;
00012
00013 import javax.swing.AbstractAction;
00014 import javax.swing.JDialog;
00015 import javax.swing.JOptionPane;
00016 import javax.swing.table.DefaultTableModel;
00017
00018 import org.apache.log4j.BasicConfigurator;
00019 import org.apache.log4j.Logger;
00020 import org.opencyc.api.CycAccess;
00021 import org.opencyc.cycobject.CycConstant;
00022 import org.swixml.SwingEngine;
00023
00024 import com.dapissarenko.hierarchy.civilServants.CivilServantData;
00025 import com.dapissarenko.hierarchy.civilServants.CivilServantListTableModel;
00026 import com.dapissarenko.hierarchy.countries.CountryListTableModel;
00027 import com.dapissarenko.hierarchy.departments.DepartmentData;
00028 import com.dapissarenko.hierarchy.departments.DepartmentDataWithHeadTableModel;
00029 import com.dapissarenko.hierarchy.departments.DepartmentListTableModel;
00030
00031
00032
00033
00034
00035 public class UI {
00036 private Logger logger = Logger.getLogger(getClass());
00037
00038 private CycAccess cycAccess;
00039
00040 private CycConstant mt;
00041
00042 private JDialog dialog;
00043
00044 private DataAccessLayer dataAccess;
00045
00046 public AbstractAction connectButtonAction = new AbstractAction() {
00047 public void actionPerformed(ActionEvent event) {
00048 connectButtonAction();
00049 }
00050 };
00051
00052 public AbstractAction enterDataIntoOpenCycButtonAction = new AbstractAction() {
00053 public void actionPerformed(ActionEvent event) {
00054 enterDataIntoOpenCycButtonAction();
00055 }
00056 };
00057
00058 public AbstractAction countriesButtonAction = new AbstractAction() {
00059 public void actionPerformed(ActionEvent event) {
00060 countriesButtonAction();
00061 }
00062 };
00063
00064 public AbstractAction departmentsButtonAction = new AbstractAction() {
00065 public void actionPerformed(ActionEvent event) {
00066 departmentsButtonAction();
00067 }
00068 };
00069
00070 public AbstractAction civilServantsButtonAction = new AbstractAction() {
00071 public void actionPerformed(ActionEvent event) {
00072 civilServantsButtonAction();
00073 }
00074 };
00075
00076 public AbstractAction departmentsAndTheirHeadsButtonAction = new AbstractAction() {
00077 public void actionPerformed(ActionEvent event) {
00078 departmentsAndTheirHeadsButtonAction();
00079 }
00080 };
00081
00082 public AbstractAction rabinovichButtonAction = new AbstractAction() {
00083 public void actionPerformed(ActionEvent event) {
00084 rabinovichButtonAction();
00085 }
00086 };
00087
00088 public AbstractAction civilServantsWithSubordinatesButtonAction = new AbstractAction() {
00089 public void actionPerformed(ActionEvent event) {
00090 civilServantsWithSubordinatesButtonAction();
00091 }
00092 };
00093
00094 public AbstractAction bossOfBagaturLoboButtonAction = new AbstractAction() {
00095 public void actionPerformed(ActionEvent event) {
00096 bossOfBagaturLoboButtonAction();
00097 }
00098 };
00099
00100 public AbstractAction exitButtonAction = new AbstractAction() {
00101 public void actionPerformed(ActionEvent event) {
00102 exitButtonAction();
00103 }
00104 };
00105
00106 public final static String UI_DEF_FILE_NAME = "com/dapissarenko/hierarchy/UI.xml";
00107
00108 public UI() {
00109 try {
00110 this.dialog = (JDialog) (new SwingEngine(this)).render(ClassLoader
00111 .getSystemClassLoader().getResource(UI_DEF_FILE_NAME));
00112 this.dialog.pack();
00113 } catch (Exception exception) {
00114 this.logger.error("", exception);
00115 }
00116 this.dialog.pack();
00117 }
00118
00119 public static void main(String[] args) {
00120 UI ui = null;
00121
00122 BasicConfigurator.configure();
00123
00124 ui = new UI();
00125 ui.getDialog().setVisible(true);
00126 }
00127
00128
00129
00130
00131
00132 public JDialog getDialog() {
00133 return dialog;
00134 }
00135
00136 private void enterDataIntoOpenCycButtonAction() {
00137 OrdussianPoliticalSystemMtWriter writer = null;
00138
00139 try {
00140 this.dialog.setCursor(Cursor
00141 .getPredefinedCursor(Cursor.WAIT_CURSOR));
00142
00143 writer = new OrdussianPoliticalSystemMtWriter();
00144 this.mt = writer.writeMicroTheoryToCyc(cycAccess);
00145
00146 this.dialog.setCursor(Cursor
00147 .getPredefinedCursor(Cursor.DEFAULT_CURSOR));
00148 JOptionPane
00149 .showMessageDialog(this.dialog,
00150 "Data has been successfully entered into the OpenCyc database.");
00151 } catch (Exception exception) {
00152 this.dialog.setCursor(Cursor
00153 .getPredefinedCursor(Cursor.DEFAULT_CURSOR));
00154 this.logger.error("", exception);
00155 JOptionPane
00156 .showMessageDialog(
00157 this.dialog,
00158 "An error occured while trying to write data into OpenCyc database. Perhaps OpenCyc server is not running.");
00159 }
00160 }
00161
00162 private void countriesButtonAction() {
00163 TableDialog tableDialog = null;
00164 CountryListTableModel tableModel = null;
00165 Collection countryData = null;
00166
00167 try {
00168 countryData = this.dataAccess.getCountryList(this.mt);
00169
00170 tableModel = new CountryListTableModel(countryData);
00171
00172 tableDialog = new TableDialog(tableModel);
00173 tableDialog.getDialog().setTitle("What countries are there?");
00174 tableDialog.getDialog().setVisible(true);
00175 } catch (Exception exception) {
00176 this.logger.error("", exception);
00177 JOptionPane
00178 .showMessageDialog(
00179 this.dialog,
00180 "An error occured while trying to execute this action. Perhaps OpenCyc server is not running.");
00181 }
00182 }
00183
00184 private void departmentsButtonAction() {
00185 TableDialog tableDialog = null;
00186 DepartmentListTableModel tableModel = null;
00187 Collection departmentData = null;
00188
00189 try {
00190 departmentData = this.dataAccess.getDepartmentList(mt);
00191
00192 tableModel = new DepartmentListTableModel(
00193 (DepartmentData[]) departmentData
00194 .toArray(new DepartmentData[departmentData.size()]));
00195
00196 tableDialog = new TableDialog(tableModel);
00197 tableDialog.getDialog().setTitle("What departments are there?");
00198 tableDialog.getDialog().setVisible(true);
00199 } catch (Exception exception) {
00200 this.logger.error("", exception);
00201 JOptionPane
00202 .showMessageDialog(
00203 this.dialog,
00204 "An error occured while trying to execute this action. Perhaps OpenCyc server is not running.");
00205 }
00206 }
00207
00208 private void civilServantsButtonAction() {
00209 TableDialog tableDialog = null;
00210 CivilServantListTableModel tableModel = null;
00211 Collection civilServantData = null;
00212
00213 try {
00214 civilServantData = this.dataAccess.getCivilServantsList(mt);
00215
00216 tableModel = new CivilServantListTableModel(
00217 (CivilServantData[]) civilServantData
00218 .toArray(new CivilServantData[civilServantData
00219 .size()]));
00220
00221 tableDialog = new TableDialog(tableModel);
00222 tableDialog.getDialog().setTitle("What civil servants are there?");
00223 tableDialog.getDialog().setVisible(true);
00224 } catch (Exception exception) {
00225 this.logger.error("", exception);
00226 JOptionPane
00227 .showMessageDialog(
00228 this.dialog,
00229 "An error occured while trying to execute this action. Perhaps OpenCyc server is not running.");
00230 }
00231 }
00232
00233 private void departmentsAndTheirHeadsButtonAction() {
00234 TableDialog tableDialog = null;
00235 DepartmentDataWithHeadTableModel tableModel = null;
00236 Collection departmentData = null;
00237
00238 try {
00239 departmentData = this.dataAccess.getHeadOfDepartmentList(mt);
00240
00241 tableModel = new DepartmentDataWithHeadTableModel(
00242 (DepartmentData[]) departmentData
00243 .toArray(new DepartmentData[departmentData.size()]));
00244
00245 tableDialog = new TableDialog(tableModel);
00246 tableDialog.getDialog().setTitle(
00247 "Tell me all departments and their heads.");
00248 tableDialog.getDialog().setVisible(true);
00249 } catch (Exception exception) {
00250 this.logger.error("", exception);
00251 JOptionPane
00252 .showMessageDialog(
00253 this.dialog,
00254 "An error occured while trying to execute this action. Perhaps OpenCyc server is not running.");
00255 }
00256
00257 }
00258
00259 private void rabinovichButtonAction() {
00260 TableDialog tableDialog = null;
00261 DefaultTableModel tableModel = null;
00262 String[] rawRabinovichRoles = null;
00263 String[][] data = null;
00264 String[] columnNames = null;
00265
00266 try {
00267 rawRabinovichRoles = this.dataAccess.getRabinovichRoles(mt);
00268
00269 data = new String[rawRabinovichRoles.length][1];
00270
00271 for (int i = 0; i < data.length; i++) {
00272 data[i][0] = rawRabinovichRoles[i];
00273 }
00274
00275 columnNames = new String[1];
00276 columnNames[0] = "Role";
00277
00278 tableModel = new DefaultTableModel(data, columnNames);
00279
00280 tableDialog = new TableDialog(tableModel);
00281 tableDialog.getDialog().setTitle(
00282 "Who is Mokii Nilovich Rabinovich?");
00283 tableDialog.getDialog().setVisible(true);
00284 } catch (Exception exception) {
00285 this.logger.error("", exception);
00286 JOptionPane
00287 .showMessageDialog(
00288 this.dialog,
00289 "An error occured while trying to execute this action. Perhaps OpenCyc server is not running.");
00290 }
00291 }
00292
00293 private void connectButtonAction() {
00294 try {
00295 this.dialog.setCursor(Cursor
00296 .getPredefinedCursor(Cursor.WAIT_CURSOR));
00297
00298 this.cycAccess = new CycAccess("localhost");
00299 this.cycAccess.traceOn();
00300 this.dataAccess = new DataAccessLayer(this.cycAccess);
00301 try {
00302 this.mt = this.cycAccess
00303 .getKnownConstantByName("OrdussianPoliticalSystemMt");
00304 } catch (Exception exception) {
00305 }
00306 this.dialog.setCursor(Cursor
00307 .getPredefinedCursor(Cursor.DEFAULT_CURSOR));
00308 JOptionPane
00309 .showMessageDialog(this.dialog,
00310 "Connection to OpenCyc database has been successfully established.");
00311 } catch (Exception exception) {
00312 this.dialog.setCursor(Cursor
00313 .getPredefinedCursor(Cursor.DEFAULT_CURSOR));
00314 this.logger.error("", exception);
00315 JOptionPane
00316 .showMessageDialog(
00317 this.dialog,
00318 "An error occured while trying to connect to OpenCyc database. Perhaps OpenCyc server is not running.");
00319 }
00320
00321 }
00322
00323 private void civilServantsWithSubordinatesButtonAction() {
00324 TableDialog tableDialog = null;
00325 CivilServantListTableModel tableModel = null;
00326 CivilServantData[] civilServantData = null;
00327
00328 try {
00329 civilServantData = this.dataAccess
00330 .getCivilServantsWithSubordinates(mt);
00331
00332 tableModel = new CivilServantListTableModel(civilServantData);
00333
00334 tableDialog = new TableDialog(tableModel);
00335 tableDialog
00336 .getDialog()
00337 .setTitle(
00338 "Tell me the names of all civil servants with subordinates.");
00339 tableDialog.getDialog().setVisible(true);
00340 } catch (Exception exception) {
00341 this.logger.error("", exception);
00342 JOptionPane
00343 .showMessageDialog(
00344 this.dialog,
00345 "An error occured while trying to execute this action. Perhaps OpenCyc server is not running.");
00346 }
00347
00348 }
00349
00350 private void bossOfBagaturLoboButtonAction() {
00351 TableDialog tableDialog = null;
00352 CivilServantListTableModel tableModel = null;
00353 CivilServantData[] civilServantData = null;
00354
00355 try {
00356 civilServantData = this.dataAccess.getBossOfBagaturLobo(mt);
00357
00358 tableModel = new CivilServantListTableModel(civilServantData);
00359
00360 tableDialog = new TableDialog(tableModel);
00361 tableDialog.getDialog()
00362 .setTitle("Who is the boss of Bagatur Lobo?");
00363 tableDialog.getDialog().setVisible(true);
00364 } catch (Exception exception) {
00365 this.logger.error("", exception);
00366 JOptionPane
00367 .showMessageDialog(
00368 this.dialog,
00369 "An error occured while trying to execute this action. Perhaps OpenCyc server is not running.");
00370 }
00371
00372 }
00373
00374 private void exitButtonAction() {
00375 if (this.cycAccess != null) {
00376 this.cycAccess.close();
00377 }
00378 System.exit(0);
00379 }
00380 }