Back to index

wims  3.65+svn20090927
DialogEditColumns.java
Go to the documentation of this file.
00001 /*
00002     WIMSchem Elements: Chemistry molecular diagram drawing tool.
00003     
00004     (c) 2008 Dr. Alex M. Clark
00005     
00006     Released as GNUware, under the Gnu Public License (GPL)
00007     
00008     See www.gnu.org for details.
00009 */
00010 
00011 package WIMSchem.ds;
00012 
00013 import WIMSchem.*;
00014 
00015 import java.io.*;
00016 import java.awt.*;
00017 import java.util.*;
00018 import java.awt.event.*;
00019 import javax.swing.*;
00020 import javax.swing.event.*;
00021 import javax.swing.table.*;
00022 
00023 /*
00024     A dialog box which allows the columns of a table to be edited, and then calculates the effect of such edits.
00025 */
00026 
00027 public class DialogEditColumns extends JDialog implements ActionListener
00028 {
00029     JButton bappend,bdelete,bmoveup,bmovedn,baccept,breject;
00030     JTable table;
00031     SchemaDataModel model;
00032     DataSheet ds;
00033 
00034     ArrayList<Integer> idx=new ArrayList<Integer>();
00035     ArrayList<String> name=new ArrayList<String>();
00036     ArrayList<Integer> type=new ArrayList<Integer>();
00037     ArrayList<String> descr=new ArrayList<String>();
00038     
00039     int[] resultOldPos=null,resultNewPos=null,resultType=null;
00040     String[] resultName=null,resultDescr=null;
00041     
00042     public DialogEditColumns(Frame Parent,DataSheet DS)
00043     {
00044        super(Parent,"Edit Column Layout",true);
00045        ds=DS;
00046        
00047        setLayout(new BorderLayout());
00048        
00049        for (int n=0;n<ds.NumCols();n++)
00050        {
00051            idx.add(new Integer(n+1));
00052            name.add(ds.ColName(n));
00053            type.add(new Integer(ds.ColType(n)));
00054            descr.add(ds.ColDescr(n));
00055        }
00056        model=new SchemaDataModel(idx,name,type,descr);
00057        table=new JTable(model);
00058            
00059        SetColumnModel();
00060        
00061        JPanel buttons=new JPanel();
00062        buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
00063        bappend=new JButton("Append"); bappend.addActionListener(this); bappend.setMnemonic(KeyEvent.VK_P);
00064        bdelete=new JButton("Delete"); bdelete.addActionListener(this); bdelete.setMnemonic(KeyEvent.VK_D);
00065        bmoveup=new JButton("Move Up"); bmoveup.addActionListener(this); bmoveup.setMnemonic(KeyEvent.VK_U);
00066        bmovedn=new JButton("Move Down"); bmovedn.addActionListener(this); bmovedn.setMnemonic(KeyEvent.VK_N);
00067        baccept=new JButton("Accept"); baccept.addActionListener(this); baccept.setMnemonic(KeyEvent.VK_A);
00068        breject=new JButton("Reject"); breject.addActionListener(this); breject.setMnemonic(KeyEvent.VK_R);
00069 
00070        buttons.add(bappend);
00071        buttons.add(bdelete);
00072        buttons.add(bmoveup);
00073        buttons.add(bmovedn);
00074        buttons.add(baccept);
00075        buttons.add(breject);
00076 
00077        add(new JScrollPane(table),BorderLayout.CENTER);
00078        add(buttons,BorderLayout.SOUTH);
00079        
00080        pack();
00081     }
00082 
00083     private void SetColumnModel()
00084     {
00085        JComboBox colTypes=new JComboBox();
00086        colTypes.addItem(DataSheet.TypeName(DataSheet.COLTYPE_MOLECULE));
00087        colTypes.addItem(DataSheet.TypeName(DataSheet.COLTYPE_STRING));
00088        colTypes.addItem(DataSheet.TypeName(DataSheet.COLTYPE_REAL));
00089        colTypes.addItem(DataSheet.TypeName(DataSheet.COLTYPE_INTEGER));
00090        colTypes.addItem(DataSheet.TypeName(DataSheet.COLTYPE_BOOLEAN));
00091        table.getColumnModel().getColumn(2).setCellEditor(new DefaultCellEditor(colTypes));
00092        table.setPreferredScrollableViewportSize(new Dimension(350,200));
00093        table.getColumnModel().getColumn(0).setMaxWidth(40);
00094        table.getColumnModel().getColumn(1).setMaxWidth(150);
00095        table.getColumnModel().getColumn(2).setMaxWidth(100);
00096     }
00097 
00098     private void ActionAppend()
00099     {
00100        idx.add(0);
00101        name.add("");
00102        type.add(DataSheet.COLTYPE_STRING);
00103        descr.add("");
00104        model.fireTableStructureChanged();
00105        SetColumnModel();
00106     }
00107 
00108     private void ActionDelete()
00109     {
00110        int i=table.getSelectedRow();
00111        if (i<0) return;
00112        idx.remove(i);
00113        name.remove(i);
00114        type.remove(i);
00115        descr.remove(i);
00116        model.fireTableStructureChanged();
00117        SetColumnModel();
00118     }
00119     
00120     private void ActionMove(int Dir)
00121     {
00122        int i=table.getSelectedRow();
00123        if (i<0 || i+Dir<0 || i+Dir>=idx.size()) return;
00124 
00125        idx.add(i+Dir,idx.remove(i));
00126        name.add(i+Dir,name.remove(i));
00127        type.add(i+Dir,type.remove(i));
00128        descr.add(i+Dir,descr.remove(i));
00129 
00130        model.fireTableStructureChanged();
00131        SetColumnModel();
00132        table.changeSelection(i+Dir,table.getSelectedColumn(),false,false);
00133     }
00134 
00135     private void ActionAccept()
00136     {
00137        for (int n=0;n<idx.size();n++) if (name.get(n).length()==0)
00138        {
00139            JOptionPane.showMessageDialog(null,"All columns must be named.","Invalid",JOptionPane.ERROR_MESSAGE);
00140            return;
00141        }
00142 
00143        // count retained+new+deleted
00144        int nsz=ds.NumCols();
00145        boolean[] retained=new boolean[ds.NumCols()];
00146        for (int n=0;n<idx.size();n++) 
00147        {
00148            int i=idx.get(n).intValue();
00149            if (i==0) nsz++; else retained[i-1]=true;
00150        }
00151        
00152        // build up the indices: add the retained and new columns first, then the deleted columns
00153        int[] oldIdx=new int[nsz],newIdx=new int[nsz],newType=new int[nsz];
00154        String[] newName=new String[nsz],newDescr=new String[nsz];
00155        
00156        int pos=0;
00157        for (int n=0;n<idx.size();n++,pos++)
00158        {
00159            oldIdx[n]=idx.get(n).intValue()-1;
00160            newIdx[n]=pos;
00161            newName[n]=name.get(n);
00162            newType[n]=type.get(n).intValue();
00163            newDescr[n]=descr.get(n);
00164        }
00165        for (int n=0;n<retained.length;n++) if (!retained[n]) 
00166        {
00167            oldIdx[pos]=n;
00168            newIdx[pos]=-1;
00169            // (other fields left blank)
00170            pos++;
00171        }
00172        
00173        // make a string version of the to-do list
00174        String explan="";
00175        for (int n=0;n<oldIdx.length;n++)
00176        {
00177            if (oldIdx[n]<0 && newIdx[n]>=0)
00178            {
00179               explan+="New column: ["+newName[n]+"], type: "+DataSheet.TypeName(newType[n])+"\n";
00180            }
00181            else if (oldIdx[n]>=0 && newIdx[n]<0)
00182            {
00183               explan+="Delete column: ["+ds.ColName(oldIdx[n])+"]\n";
00184            }
00185            else if (oldIdx[n]>=0 && newIdx[n]>=0)
00186            {
00187               if (newName[n].compareTo(ds.ColName(oldIdx[n]))!=0)
00188                   explan+="Rename ["+ds.ColName(oldIdx[n])+"] to ["+newName[n]+"]\n";
00189               if (newDescr[n].compareTo(ds.ColDescr(oldIdx[n]))!=0)
00190                   explan+="Change description for ["+newName[n]+"]\n";
00191               if (ds.ColType(oldIdx[n])!=newType[n])
00192                   explan+="Change type of ["+newName[n]+"] to: "+DataSheet.TypeName(newType[n])+"\n";
00193               if (newIdx[n]!=oldIdx[n])
00194                   explan+="Reorder ["+newName[n]+"] from #"+(oldIdx[n]+1)+" to #"+(newIdx[n]+1)+"\n";
00195            }
00196        }
00197        if (explan.length()==0) {setVisible(false); return;} // nothing to do, equivalent to reject
00198        
00199        String opt[]={"Yes","No"};
00200        if (JOptionPane.showOptionDialog(null,explan,"Apply Changes?",
00201                   JOptionPane.YES_NO_OPTION,JOptionPane.YES_NO_OPTION,null,opt,opt[0])!=JOptionPane.YES_OPTION) return;
00202 
00203        resultOldPos=oldIdx;
00204        resultNewPos=newIdx;
00205        resultName=newName;
00206        resultType=newType;
00207        resultDescr=newDescr;
00208        setVisible(false);
00209     }
00210     
00211     private void ActionReject()
00212     {
00213        setVisible(false);
00214     }
00215 
00216     // returns true if the datasheet has changed
00217     public boolean Execute()
00218     {
00219        setVisible(true);
00220        return resultOldPos!=null;
00221     }
00222 
00223     public int[] ResultOldPos() {return resultOldPos;}
00224     public int[] ResultNewPos() {return resultNewPos;}
00225     public String[] ResultName() {return resultName;}
00226     public int[] ResultType() {return resultType;}
00227     public String[] ResultDescr() {return resultDescr;}
00228 
00229     public void actionPerformed(ActionEvent e)
00230     {
00231        if (e.getSource()==bappend) ActionAppend();
00232        else if (e.getSource()==bdelete) ActionDelete();
00233        else if (e.getSource()==bmoveup) ActionMove(-1);
00234        else if (e.getSource()==bmovedn) ActionMove(1);
00235        else if (e.getSource()==baccept) ActionAccept();
00236        else if (e.getSource()==breject) ActionReject();
00237     }
00238     
00239     // internal class for the underlying data
00240     
00241     class SchemaDataModel extends AbstractTableModel
00242     {
00243        ArrayList<Integer> idx;
00244        ArrayList<String> name;
00245        ArrayList<Integer> type;
00246        ArrayList<String> descr;
00247        
00248        public SchemaDataModel(ArrayList<Integer> Idx,ArrayList<String> Name,ArrayList<Integer> Type,ArrayList<String> Descr)
00249        {
00250            idx=Idx;
00251            name=Name;
00252            type=Type;
00253            descr=Descr;
00254        }
00255        
00256        public String getColumnName(int col) 
00257        {
00258            final String[] COLNAMES={"#","Name","Type","Description"};
00259             return COLNAMES[col];
00260        }
00261        public int getRowCount() {return idx.size();}
00262        public int getColumnCount() {return 4;}
00263        public Object getValueAt(int row,int col) 
00264        {
00265            if (col==0)
00266            {
00267               int i=idx.get(row).intValue();
00268               return i==0 ? "" : ""+i;
00269            }
00270            else if (col==1) return name.get(row);
00271            else if (col==2) return DataSheet.TypeName(type.get(row).intValue());
00272            else if (col==3) return descr.get(row);
00273            return null;
00274        }
00275        public boolean isCellEditable(int row,int col) {return col>0;}
00276        public void setValueAt(Object value,int row,int col) 
00277        {
00278            if (col==1) name.set(row,(String)value);
00279            else if (col==2)
00280            {
00281               String t=(String)value;
00282               if (t.compareTo(DataSheet.TypeName(DataSheet.COLTYPE_MOLECULE))==0) type.set(row,DataSheet.COLTYPE_MOLECULE);
00283               else if (t.compareTo(DataSheet.TypeName(DataSheet.COLTYPE_STRING))==0) type.set(row,DataSheet.COLTYPE_STRING);
00284               else if (t.compareTo(DataSheet.TypeName(DataSheet.COLTYPE_REAL))==0) type.set(row,DataSheet.COLTYPE_REAL);
00285               else if (t.compareTo(DataSheet.TypeName(DataSheet.COLTYPE_INTEGER))==0) type.set(row,DataSheet.COLTYPE_INTEGER);
00286               else if (t.compareTo(DataSheet.TypeName(DataSheet.COLTYPE_BOOLEAN))==0) type.set(row,DataSheet.COLTYPE_BOOLEAN);
00287            }
00288            else if (col==3) descr.set(row,(String)value);
00289             fireTableCellUpdated(row,col);
00290        }
00291     }
00292     
00293 }