Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

MeasurementSequenceTableModel.java

Go to the documentation of this file.
00001 /*
00002  * MeasurementSequenceTableModel.java
00003  *
00004  * Copyright (C) 2005 Project SQUID, http://www.cs.helsinki.fi/group/squid/
00005  *
00006  * This file is part of Ikayaki.
00007  *
00008  * Ikayaki is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * Ikayaki is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with Ikayaki; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00021  */
00022 
00023 package ikayaki.gui;
00024 
00025 import ikayaki.*;
00026 
00027 import javax.swing.table.AbstractTableModel;
00028 import java.util.ArrayList;
00029 import java.util.List;
00030 
00037 public class MeasurementSequenceTableModel extends AbstractTableModel implements ProjectListener, MeasurementListener {
00038 
00039     private static final String VISIBLE_COLUMNS_PROPERTY = "visibleColumns";
00040 
00041     private Project project = null;
00042 
00043     private List<SequenceColumn> visibleColumns = new ArrayList<SequenceColumn>();
00044     private List<SequenceColumn> possibleColumns = new ArrayList<SequenceColumn>();
00045 
00049     public MeasurementSequenceTableModel() {
00050         // initialize with no project
00051         setProject(null);
00052     }
00053 
00057     public Project getProject() {
00058         return project;
00059     }
00060 
00067     public void setProject(Project project) {
00068         if (this.project != null) {
00069             this.project.removeProjectListener(this);
00070             this.project.removeMeasurementListener(this);
00071         }
00072         if (project != null) {
00073             project.addProjectListener(this);
00074             project.addMeasurementListener(this);
00075         }
00076         this.project = project;
00077 
00078         // set all possible columns, in order of appearance
00079         possibleColumns.clear();
00080         SequenceColumn[] allColumns = SequenceColumn.getAllColumns();
00081         for (SequenceColumn column : allColumns) {
00082             possibleColumns.add(column);
00083         }
00084 
00085         // NOTE: here it would be possible to disable some columns depending on the project's type
00086 
00087         // show default columns
00088         visibleColumns.clear();
00089         List<SequenceColumn> defaultColumns = Settings.getDefaultColumns();
00090         for (SequenceColumn column : defaultColumns) {
00091             showColumn(column, false);
00092         }
00093 
00094         if (project != null) {
00095 
00096             // show customized columns
00097             String property = project.getProperty(VISIBLE_COLUMNS_PROPERTY);
00098             if (property != null) {
00099                 String[] cols = property.split(",");
00100 
00101                 // set each of the explicitly specified columns visible or hidden
00102                 for (String s : cols) {
00103                     if (!s.endsWith("+") && !s.endsWith("-")) {
00104                         System.err.println("Invalid " + VISIBLE_COLUMNS_PROPERTY + " property: " + s);
00105                         continue;
00106                     }
00107 
00108                     // split the property string to NAME and +/-
00109                     String column = s.substring(0, s.length() - 1);
00110                     boolean visible = s.substring(s.length() - 1).equals("+");
00111                     try {
00112                         if (visible) {
00113                             showColumn(SequenceColumn.valueOf(column), false);
00114                         } else {
00115                             hideColumn(SequenceColumn.valueOf(column), false);
00116                         }
00117                     } catch (IllegalArgumentException e) {
00118                         e.printStackTrace();
00119                     }
00120                 }
00121             }
00122         }
00123     }
00124 
00132     public void projectUpdated(ProjectEvent event) {
00133 //        if (event.getType() == ProjectEvent.Type.DATA_CHANGED) {
00134 //            fireTableDataChanged();
00135 //        }
00136     }
00137 
00141     public void measurementUpdated(MeasurementEvent event) {
00142         if (event.getType() == MeasurementEvent.Type.VALUE_MEASURED
00143                 || event.getType() == MeasurementEvent.Type.STEP_START
00144                 || event.getType() == MeasurementEvent.Type.STEP_END
00145                 || event.getType() == MeasurementEvent.Type.STEP_ABORTED) {
00146             MeasurementStep step = event.getStep();
00147             for (int i = 0; i < project.getSteps(); i++) {
00148                 if (project.getStep(i) == step) {
00149                     fireTableRowsUpdated(i, i);
00150                     return;
00151                 }
00152             }
00153         }
00154     }
00155 
00159     public SequenceColumn[] getPossibleColumns() {
00160         return possibleColumns.toArray(new SequenceColumn[possibleColumns.size()]);
00161     }
00162 
00169     private void showColumn(SequenceColumn column, boolean save) {
00170         if (visibleColumns.indexOf(column) < 0) {
00171             int i, j;
00172             for (i = 0, j = 0; i < visibleColumns.size() && j < possibleColumns.size();) {
00173                 if (visibleColumns.get(i) == possibleColumns.get(j)) {
00174                     // all in good sync
00175                     i++;
00176                     j++;
00177                 } else if (column == possibleColumns.get(j)) {
00178                     // this is the place where to add column
00179                     break;
00180                 } else {
00181                     // this possibleColumn item is not visible, but it is not the one we want to show either
00182                     j++;
00183                 }
00184             }
00185             visibleColumns.add(i, column);
00186             fireTableStructureChanged();
00187             if (save) {
00188                 saveColumn(column, true);
00189             }
00190         }
00191     }
00192 
00199     private void hideColumn(SequenceColumn column, boolean save) {
00200         if (visibleColumns.remove(column)) {
00201             fireTableStructureChanged();
00202             if (save) {
00203                 saveColumn(column, false);
00204             }
00205         }
00206     }
00207 
00215     public boolean isColumnVisible(SequenceColumn column) {
00216         if (column == null) {
00217             throw new NullPointerException();
00218         }
00219         return visibleColumns.contains(column);
00220     }
00221 
00230     public void setColumnVisible(SequenceColumn column, boolean visible) {
00231         if (column == null) {
00232             throw new NullPointerException();
00233         }
00234         if (isColumnVisible(column) == visible) {
00235             return;
00236         }
00237         if (visible) {
00238             showColumn(column, true);
00239         } else {
00240             hideColumn(column, true);
00241         }
00242     }
00243 
00251     private void saveColumn(SequenceColumn column, boolean visible) {
00252         if (project != null) {
00253 
00254             // get the project's old properties
00255             String[] cols = project.getProperty(VISIBLE_COLUMNS_PROPERTY, "").split(",");
00256             StringBuffer result = new StringBuffer();
00257 
00258             // keep the unaffected columns safe
00259             for (String s : cols) {
00260                 if (!(s.startsWith(column.name()) && s.length() == (column.name().length() + 1))) {
00261                     if (result.length() != 0) {
00262                         result.append(',');
00263                     }
00264                     result.append(s);
00265                 }
00266             }
00267 
00268             // explicitly hide/show this column
00269             if (result.length() != 0) {
00270                 result.append(',');
00271             }
00272             result.append(column.name());
00273             result.append(visible ? '+' : '-');
00274 
00275             project.setProperty(VISIBLE_COLUMNS_PROPERTY, result.toString());
00276         }
00277     }
00278 
00286     public int getRowCount() {
00287         if (project != null && visibleColumns.size() > 0) {
00288             if (project.isSequenceEditEnabled()) {
00289                 return project.getSteps() + 1;
00290             } else {
00291                 return project.getSteps();
00292             }
00293         } else {
00294             return 0;
00295         }
00296     }
00297 
00305     public int getColumnCount() {
00306         return Math.max(1, visibleColumns.size());
00307     }
00308 
00316     public Object getValueAt(int rowIndex, int columnIndex) {
00317         return visibleColumns.get(columnIndex).getValue(rowIndex, project);
00318     }
00319 
00327     public void setValueAt(Object data, int rowIndex, int columnIndex) {
00328         visibleColumns.get(columnIndex).setValue(data, rowIndex, project);
00329     }
00330 
00338     @Override public boolean isCellEditable(int rowIndex, int columnIndex) {
00339         return visibleColumns.get(columnIndex).isCellEditable(rowIndex, project);
00340     }
00341 
00348     @Override public String getColumnName(int column) {
00349         if (visibleColumns.size() <= column) {
00350             return "< right-click to select columns >";
00351         }
00352         return visibleColumns.get(column).getColumnName(project);
00353     }
00354 
00358     public String getColumnToolTip(int column) {
00359         if (column < visibleColumns.size()) {
00360             return visibleColumns.get(column).getToolTipText(project);
00361         }
00362         return null;
00363     }
00364 
00371     @Override public Class<?> getColumnClass(int columnIndex) {
00372         if (visibleColumns.size() <= columnIndex) {
00373             return Object.class;
00374         }
00375         return visibleColumns.get(columnIndex).getColumnClass();
00376     }
00377 }

Generated on Fri May 6 16:00:32 2005 for Squid by  doxygen 1.4.1