001/* 002 * Copyright (c) 2010-2012 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: crawl $' 006 * '$Date: 2012-05-09 11:05:40 -0700 (Wed, 09 May 2012) $' 007 * '$Revision: 29823 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.kepler.plotting; 031 032import java.awt.Color; 033import java.util.ArrayList; 034import java.util.Collections; 035import java.util.HashMap; 036import java.util.List; 037import java.util.Map; 038import java.util.Set; 039 040import org.apache.commons.logging.Log; 041import org.apache.commons.logging.LogFactory; 042 043import ptolemy.actor.TypedIOPort; 044 045/** 046 * Created by IntelliJ IDEA. 047 * User: sean 048 * Date: Jul 7, 2010 049 * Time: 12:42:55 PM 050 */ 051 052public class Row { 053 public Row(String name, String x, String y, PointType pointType, Color color, PlotEditor plotEditor) { 054 this.setAttribute("Name", name); 055 this.setAttribute("X", x); 056 this.setAttribute("Y", y); 057 this.setAttribute("Point Type", pointType); 058 this.setAttribute("Color", color); 059 setPlotEditor(plotEditor); 060 } 061 062 private void setAttribute(String name, Object object) { 063 attributes.put(name, object); 064 } 065 066 public void setValueByIndex(int index, Object value) { 067// System.out.println("Putting '" + value + "' as column: " + COLUMN_NAMES[index]); 068 attributes.put(COLUMN_NAMES[index], value); 069 } 070 071 public void setCandidateValue(int columnIndex, Object aValue) { 072 this.candidateIndex = columnIndex; 073 this.candidateValue = aValue; 074 } 075 076 public void rollback() { 077 this.candidateIndex = -1; 078 } 079 080 public void commit() { 081 if (candidateIndex >= 0) { 082// System.out.println("Committing! Index = " + candidateIndex + ", value = " + candidateValue); 083 this.setValueByIndex(candidateIndex, candidateValue); 084 } 085 } 086 087 public int getCandidateIndex() { 088 return candidateIndex; 089 } 090 091 public Object getCandidateValue() { 092 return candidateValue; 093 } 094 095 public void setPlotEditor(PlotEditor plotEditor) { 096 this.plotEditor = plotEditor; 097 } 098 099 public PlotEditor getPlotEditor() { 100 return plotEditor; 101 } 102 103 public Color getColor() { 104 return (Color) attributes.get("Color"); 105 } 106 107 public String getSensorName() { 108 return (String) attributes.get("Name"); 109 } 110 111 public PointType getPointType() { 112 return (PointType) attributes.get("Point Type"); 113 } 114 115 public void addDeletionListener(DeletionListener listener, int priority) { 116 listeners.put(listener, priority); 117 if (!listenersByPriority.containsKey(priority)) { 118 listenersByPriority.put(priority, new ArrayList<DeletionListener>()); 119 } 120 listenersByPriority.get(priority).add(listener); 121 } 122 123 public void addDeletionListener(DeletionListener listener) { 124 addDeletionListener(listener, DEFAULT_PRIORITY); 125 } 126 127 public void removeDeletionListener(DeletionListener listener) { 128 Integer priority = listeners.get(listener); 129 listenersByPriority.get(priority).remove(listener); 130 listeners.remove(listener); 131 132 } 133 134 public void notifyDeletionListeners() { 135 // Evaluating in priority order 136 Set<Integer> prioritiesSet = listenersByPriority.keySet(); 137 List<Integer> priorities = new ArrayList<Integer>(prioritiesSet); 138 Collections.sort(priorities); 139 for (Integer priority : priorities) { 140 log.debug("Evaluating priority: " + priority); 141 for (DeletionListener listener : listenersByPriority.get(priority)) { 142 listener.delete(this.getSensorName()); 143 } 144 } 145 } 146 147 public boolean containsListener(DeletionListener listener) { 148 return listeners.containsKey(listener); 149 } 150 151 public void setDataPort(TypedIOPort dataPort) { 152 this.dataPort = dataPort; 153 } 154 155 public TypedIOPort getDataPort() { 156 return dataPort; 157 } 158 159 public static boolean isEditableColumns(int i) { 160 String columnName = COLUMN_NAMES[i]; 161 return !"Y".equals(columnName) && !"X".equals(columnName); 162 } 163 164 private Map<String, Object> attributes = new HashMap<String, Object>(); 165 166 public Object getValueByIndex(int index) { 167 return attributes.get(COLUMN_NAMES[index]); 168 } 169 170 public static int getColumnCount() { 171 return COLUMN_NAMES.length; 172 } 173 174 public static String getColumnName(int i) { 175 try { 176 return COLUMN_NAMES[i]; 177 } 178 catch(ArrayIndexOutOfBoundsException ex) { 179 throw new IllegalArgumentException("This column does not exist"); 180 } 181 } 182 183 public static Class getColumnClass(int i) { 184 try { 185 return COLUMN_CLASSES[i]; 186 } 187 catch(ArrayIndexOutOfBoundsException ex) { 188 throw new IllegalArgumentException("This column does not exist"); 189 } 190 } 191 192 private static final Log log = LogFactory.getLog(Row.class.getName()); 193 194 private static String[] COLUMN_NAMES = new String[] {"Name", "X", "Y", "Point Type", "Color"}; 195 private static Class[] COLUMN_CLASSES = new Class[] {String.class, String.class, String.class, PointType.class, Color.class}; 196 public static int[] COLUMN_WIDTHS = new int[] {120, 10, 120, 10, 10}; 197 198 private int candidateIndex; 199 private Object candidateValue; 200 private PlotEditor plotEditor; 201 private Map<DeletionListener, Integer> listeners = new HashMap<DeletionListener, Integer>(); 202 private Map<Integer, List<DeletionListener>> listenersByPriority = new HashMap<Integer, List<DeletionListener>>(); 203 private static final int DEFAULT_PRIORITY = 0; 204 private boolean adHocMode = false; 205 private TypedIOPort dataPort; 206}