CycMappingTab.java
Go to the documentation of this file.
00001 package instruction.gui.tab;
00002 
00003 import instruction.configuration.ConfigurationManager;
00004 
00005 import java.awt.GridBagLayout;
00006 import javax.swing.JPanel;
00007 import javax.swing.JLabel;
00008 import java.awt.GridBagConstraints;
00009 import java.awt.Insets;
00010 import java.util.ArrayList;
00011 import java.util.HashMap;
00012 import java.util.Iterator;
00013 import java.util.List;
00014 import java.util.Map;
00015 import java.util.Set;
00016 import javax.swing.JScrollPane;
00017 import javax.swing.JTable;
00018 import javax.swing.JButton;
00019 import javax.swing.table.AbstractTableModel;
00020 import javax.swing.table.TableModel;
00021 
00022 public class CycMappingTab extends JPanel implements DialogTab {
00023 
00024         private static final long serialVersionUID = 1L;
00025         private JLabel jLabel = null;
00026         private JPanel jPanel = null;
00027         private JScrollPane jScrollPane = null;
00028         private JTable jTable = null;
00029         private JButton jButton = null;
00030         private JButton jButton1 = null;
00031         private JPanel jPanel1 = null;
00032 
00036         public CycMappingTab () {
00037 
00038                 super();
00039                 initialize();
00040         }
00041 
00047         private void initialize() {
00048 
00049                 GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
00050                 gridBagConstraints6.gridx = 0;
00051                 gridBagConstraints6.insets = new Insets( 5, 5, 5, 5 );
00052                 gridBagConstraints6.fill = GridBagConstraints.HORIZONTAL;
00053                 gridBagConstraints6.gridy = 3;
00054                 GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
00055                 gridBagConstraints2.fill = GridBagConstraints.BOTH;
00056                 gridBagConstraints2.gridy = 2;
00057                 gridBagConstraints2.weightx = 1.0;
00058                 gridBagConstraints2.weighty = 1.0;
00059                 gridBagConstraints2.gridx = 0;
00060                 gridBagConstraints2.insets = new Insets( 5, 5, 5, 5 );
00061                 GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
00062                 gridBagConstraints1.gridx = 0;
00063                 gridBagConstraints1.gridy = 1;
00064                 GridBagConstraints gridBagConstraints = new GridBagConstraints();
00065                 gridBagConstraints.gridx = 0;
00066                 gridBagConstraints.insets = new Insets( 5, 5, 5, 5 );
00067                 gridBagConstraints.anchor = GridBagConstraints.WEST;
00068                 gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
00069                 gridBagConstraints.gridy = 0;
00070                 jLabel = new JLabel();
00071                 jLabel.setText( "The follwing mappings from WordNet Synset-IDs to Cyc concepts are added." );
00072                 this.setSize( 510, 349 );
00073                 this.setLayout( new GridBagLayout() );
00074                 this.add( jLabel, gridBagConstraints );
00075                 this.add( getJPanel(), gridBagConstraints1 );
00076                 this.add( getJScrollPane(), gridBagConstraints2 );
00077                 this.add( getJPanel1(), gridBagConstraints6 );
00078         }
00079 
00080         public void onCancel() {
00081 
00082         }
00083 
00084         public void onOK() {
00085 
00086                 Map<String, List<String>> newMappings = new HashMap<String, List<String>>();
00087                 
00088                 for ( int i = 0; i < getJTable().getRowCount(); i++ ) {
00089                         TableModel model = getJTable().getModel();
00090                         String synset = (String) model.getValueAt( i, 0 );
00091                         String concept = (String) model.getValueAt( i, 1 );
00092                         List<String> concepts = newMappings.get( synset );
00093                         if (concepts == null) {
00094                                 concepts = new ArrayList<String>();
00095                                 newMappings.put( synset, concepts );
00096                         }
00097                         concepts.add( concept );
00098                         
00099                         ConfigurationManager.setMappings( newMappings );
00100                 }
00101 
00102         }
00103 
00109         private JPanel getJPanel() {
00110 
00111                 if ( jPanel == null ) {
00112                         jPanel = new JPanel();
00113                         jPanel.setLayout( new GridBagLayout() );
00114 
00115                 }
00116                 return jPanel;
00117         }
00118 
00124         private JScrollPane getJScrollPane() {
00125 
00126                 if ( jScrollPane == null ) {
00127                         jScrollPane = new JScrollPane();
00128                         jScrollPane.setViewportView( getJTable() );
00129                 }
00130                 return jScrollPane;
00131         }
00132 
00138         private JTable getJTable() {
00139 
00140                 if ( jTable == null ) {
00141 
00142                         jTable = new JTable( new AbstractTableModel() {
00143                                 private static final long serialVersionUID = -7702568498509726688L;
00144 
00145                                 String[] columns = {
00146                                                 "Synset-ID", "Cyc concept"
00147                                 };
00148 
00149                                 List<Object[]> data = new ArrayList<Object[]>();
00150 
00151                                 @Override
00152                                 public String getColumnName( int column ) {
00153 
00154                                         return columns[column];
00155                                 }
00156 
00157                                 public int getColumnCount() {
00158 
00159                                         return columns.length;
00160                                 }
00161 
00162                                 public int getRowCount() {
00163 
00164                                         return data.size();
00165                                 }
00166 
00167                                 public Object getValueAt( int row, int col ) {
00168 
00169                                         return data.get( row )[col];
00170                                 }
00171 
00172                                 @Override
00173                                 public void setValueAt( Object value, int rowIndex, int columnIndex ) {
00174 
00175                                         if ( rowIndex > getRowCount() - 1 ) {
00176                                                 Object[] newData = new Object[columns.length];
00177                                                 newData[columnIndex] = value;
00178                                                 data.add( newData );
00179                                         }
00180                                         else {
00181                                                 if ( value == null )
00182                                                         data.remove( rowIndex );
00183                                                 else
00184                                                         data.get( rowIndex )[columnIndex] = value;
00185                                         }
00186                                         fireTableDataChanged();
00187                                 }
00188 
00189                                 @Override
00190                                 public boolean isCellEditable( int rowIndex, int columnIndex ) {
00191 
00192                                         return true;
00193                                 }
00194 
00195                         } );
00196 
00197                         Map<String, List<String>> mappings = ConfigurationManager.getMappings();
00198                         Set<String> synsets = mappings.keySet();
00199                         for ( Iterator<String> i = synsets.iterator(); i.hasNext(); ) {
00200                                 String synset = i.next();
00201                                 List<String> concepts = mappings.get( synset );
00202                                 for ( Iterator<String> j = concepts.iterator(); j.hasNext(); ) {
00203                                         int row = jTable.getRowCount();
00204                                         jTable.setValueAt( synset, row, 0 );
00205                                         jTable.setValueAt( j.next(), row, 1 );
00206                                 }
00207                         }
00208                 }
00209                 return jTable;
00210         }
00211 
00217         private JButton getJButton() {
00218 
00219                 if ( jButton == null ) {
00220                         jButton = new JButton();
00221                         jButton.setText( "Add Mapping" );
00222                         jButton.addActionListener( new java.awt.event.ActionListener() {
00223                                 public void actionPerformed( java.awt.event.ActionEvent e ) {
00224 
00225                                         int row = getJTable().getModel().getRowCount();
00226                                         getJTable().getModel().setValueAt( "Synset-ID", row, 0 );
00227                                         getJTable().getModel().setValueAt( "Concept", row, 1 );
00228                                 }
00229                         } );
00230                 }
00231                 return jButton;
00232         }
00233 
00239         private JButton getJButton1() {
00240 
00241                 if ( jButton1 == null ) {
00242                         jButton1 = new JButton();
00243                         jButton1.setText( "Remove" );
00244                         jButton1.addActionListener( new java.awt.event.ActionListener() {
00245                                 public void actionPerformed( java.awt.event.ActionEvent e ) {
00246 
00247                                         int selIndex = getJTable().getSelectedRow();
00248                                         if ( selIndex >= 0 )
00249                                                 getJTable().getModel().setValueAt( null, selIndex, 0 );
00250                                 }
00251                         } );
00252 
00253                 }
00254                 return jButton1;
00255         }
00256 
00262         private JPanel getJPanel1() {
00263 
00264                 if ( jPanel1 == null ) {
00265                         GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
00266                         gridBagConstraints4.gridx = 1;
00267                         gridBagConstraints4.insets = new Insets( 5, 5, 5, 5 );
00268                         gridBagConstraints4.gridy = 0;
00269                         GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
00270                         gridBagConstraints3.gridx = 0;
00271                         gridBagConstraints3.insets = new Insets( 5, 5, 5, 5 );
00272                         gridBagConstraints3.gridy = 0;
00273                         jPanel1 = new JPanel();
00274                         jPanel1.setLayout( new GridBagLayout() );
00275                         jPanel1.add( getJButton(), gridBagConstraints3 );
00276                         jPanel1.add( getJButton1(), gridBagConstraints4 );
00277                 }
00278                 return jPanel1;
00279         }
00280 
00281 } // @jve:decl-index=0:visual-constraint="10,10"
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


comp_ehow
Author(s): Moritz Tenorth, Daniel Nyga
autogenerated on Tue Apr 16 2013 00:18:02