001 /* EVolve - an Extensible Software Visualization Framework 002 * Copyright (C) 2001-2002 Qin Wang 003 * 004 * This library is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU Library General Public 006 * License as published by the Free Software Foundation; either 007 * version 2 of the License, or (at your option) any later version. 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Library General Public License for more details. 013 * 014 * You should have received a copy of the GNU Library General Public 015 * License along with this library; if not, write to the 016 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 017 * Boston, MA 02111-1307, USA. 018 */ 019 020 /* 021 * EVolve is distributed at http://www.sable.mcgill.ca/EVolve/ 022 */ 023 024 package EVolve.data; 025 026 import java.awt.Color; 027 import java.util.ArrayList; 028 import java.util.HashSet; 029 import java.util.StringTokenizer; 030 import java.util.regex.Matcher; 031 import java.util.regex.Pattern; 032 033 import EVolve.Scene; 034 035 public class Selection implements Cloneable{ 036 public static int total = 0; 037 private String name; 038 private int sourceType, entityType; 039 private String entityCategory; 040 private Entity[] selected; 041 private long start, end; 042 private ReferenceLink[] link; 043 private Color color; 044 private long startTime = -1, endTime = -1; 045 private ArrayList timeMap; 046 047 public Selection(String name, int sourceType, int entityType, String entityCategory, long[] selected, long start, long end, ArrayList timemap) { 048 this.entityType = entityType; 049 this.entityCategory = entityCategory; 050 this.sourceType = sourceType; 051 this.selected = new Entity[selected.length]; 052 for (int i = 0; i < selected.length; i++) { 053 try { 054 this.selected[i] = (Entity)Scene.getDataManager().getEntity()[entityType].get(new Long(selected[i])); 055 } catch (Exception e) { 056 int qq = 0; 057 qq++; 058 } 059 } 060 this.start = start; 061 this.end = end; 062 this.color = null; 063 this.link = new ReferenceLink[0]; 064 065 this.name = name; 066 total++; 067 timeMap = new ArrayList(); 068 if (timemap != null) { 069 for (int i=0; i<timemap.size(); i++) { 070 long[] newValue = new long[2]; 071 long[] oldValue = (long[])timemap.get(i); 072 newValue[0] = oldValue[0]; 073 newValue[1] = oldValue[1]; 074 timeMap.add(newValue); 075 } 076 findTimeInterval(start, end); 077 } 078 } 079 080 public void setName(String name) { 081 this.name = name; 082 } 083 084 public String getName() { 085 return name; 086 } 087 088 public void setColor(Color color) { 089 this.color = color; 090 } 091 092 public Color getColor() { 093 return color; 094 } 095 096 public void setLink(ReferenceLink[] link) { 097 this.link = link; 098 } 099 100 public ReferenceLink[] getLink() { 101 return link; 102 } 103 104 public int getEntityType() { 105 return entityType; 106 } 107 108 public Entity[] getSelected() { 109 return selected; 110 } 111 112 public void setSelected(Entity[] selected) { 113 this.selected = selected; 114 } 115 116 public long getStart() { 117 return start; 118 } 119 120 public long getEnd() { 121 return end; 122 } 123 124 public long getStartTime() { 125 return startTime; 126 } 127 128 public void setStartTime(long startTime) { 129 this.startTime = startTime; 130 } 131 132 public void setEndTime(long endTime) { 133 this.endTime = endTime; 134 } 135 136 public long getEndTime() { 137 return endTime; 138 } 139 140 public void setStartEvent(int start) { 141 startTime = start; 142 } 143 144 public void setEndEvent(int end) { 145 endTime = end; 146 } 147 148 public void setTimeInterval(long startTime, long endTime) { 149 this.startTime = startTime; 150 this.endTime = endTime; 151 findEventInterval(); 152 } 153 154 public Selection specialClone() { 155 long ids[] = new long[selected.length]; 156 157 for (int i=0; i<ids.length; i++) { 158 ids[i] = selected[i].getId(); 159 } 160 161 //Selection returnVal = new Selection(entityType,ids,0,Integer.MAX_VALUE,-1,-1,null); 162 Selection returnVal = new Selection(name, sourceType,entityType,entityCategory,ids,0,Long.MAX_VALUE,null); 163 returnVal.setColor(color); 164 returnVal.setName(name); 165 returnVal.setLink(link); 166 167 return returnVal; 168 } 169 170 public ArrayList getTimeMap() { 171 return timeMap; 172 } 173 174 public Object clone() { 175 Selection o = null; 176 177 try { 178 o = (Selection)super.clone(); 179 } catch (CloneNotSupportedException e) { 180 System.out.println("Clone not supported in selection"); 181 return o; 182 } 183 184 o.name = name; 185 o.selected = new Entity[selected.length]; 186 for (int i=0; i<selected.length; i++) { 187 o.selected[i] = selected[i]; 188 } 189 o.link = new ReferenceLink[link.length]; 190 for (int i=0; i<link.length; i++) { 191 o.link[i] = (ReferenceLink)link[i].clone(); 192 } 193 o.color = color == null ? null : new Color(color.getRGB()); 194 return o; 195 } 196 197 private void findTimeInterval(long start, long end) { 198 int found = 0; 199 if (timeMap != null) { 200 for (int i = 0; i< timeMap.size(); i++) { 201 long[] map = (long[])timeMap.get(i); 202 if ((startTime == -1) && (map[1] == start)) { 203 startTime = map[0]; 204 found++; 205 } 206 if (map[1] == end) { 207 endTime = map[0]; 208 found++; 209 } 210 if (found == 2) break; 211 } 212 if (endTime == -1) endTime = Long.MAX_VALUE; 213 } 214 } 215 216 private void findEventInterval() { 217 long time[] = new long[2], event[] = new long[2]; 218 time[0] = startTime; 219 time[1] = endTime; 220 event[0] = 0; 221 event[1] = Long.MAX_VALUE; 222 223 if (timeMap != null) { 224 for (int i=0; i<time.length; i++) { 225 long lastTarget = -1; 226 for (int j = 0; j< timeMap.size(); j++) { 227 long[] map = (long[])timeMap.get(j); 228 if ((lastTarget<= time[i])&&(map[0]>=time[i])) { 229 event[i] = map[1]; 230 break; 231 } 232 } 233 } 234 start = event[0]; 235 end = event[1]; 236 } 237 } 238 239 public void changeTimeFrame(long startTime, long endTime) { 240 setTimeInterval(startTime, endTime); 241 findEventInterval(); 242 } 243 244 public int getSourceType() { 245 return sourceType; 246 } 247 248 public String getEntityCategory() { 249 return entityCategory; 250 } 251 252 public void filterEntities(String regExps, boolean keepMatched) { 253 HashSet result = new HashSet(); 254 255 for (int i=0; i<selected.length; i++) { 256 String entityName = selected[i].getName().replace('.','/'); 257 StringTokenizer token = new StringTokenizer(regExps,";"); 258 boolean found = false; 259 while (token.hasMoreTokens()) { 260 String regExp = token.nextToken(); 261 Pattern pattern = Pattern.compile(regExp); 262 Matcher matcher = pattern.matcher(""); 263 matcher.reset(entityName); 264 265 found = matcher.lookingAt(); 266 if (found) break; 267 268 } 269 270 if ((found==keepMatched)&&(!result.contains(new Integer(i)))) 271 result.add(new Integer(i)); 272 } 273 274 Entity[] newSelected = new Entity[result.size()]; 275 int i = 0; 276 for (int j=0; j<selected.length; j++) { 277 if (result.contains(new Integer(j))) 278 newSelected[i++] = selected[j]; 279 } 280 selected = null; 281 selected = newSelected; 282 } 283 }