001 /** 002 * Created by IntelliJ IDEA. 003 * User: Wei Wang 004 * Date: Nov 28, 2002 005 * Time: 10:23:35 AM 006 */ 007 008 package EVolve.visualization.XYViz.ValRefViz; 009 010 import java.awt.event.ActionEvent; 011 import java.awt.event.ActionListener; 012 import java.awt.*; 013 import java.util.ArrayList; 014 import javax.swing.*; 015 import EVolve.Scene; 016 import EVolve.data.Element; 017 import EVolve.data.ValueComparator; 018 import EVolve.data.Entity; 019 import EVolve.util.painters.BarChartPainter; 020 import EVolve.util.painters.shapes.Box; 021 import EVolve.util.xmlutils.datastructures.SerializedVisualization; 022 import EVolve.visualization.AutoShapeImage; 023 import EVolve.visualization.Dimension; 024 025 public class BarChartViz extends ValueReferenceVisualization{ 026 private static JCheckBox selectionOptions[] = null; 027 private JCheckBox chkSelectOccurredEntities,chkSelectAllEntities; 028 protected static int SELECT_OPTION = 0x100; 029 private int barHeight = 5; 030 private boolean useRandomColor; 031 private JComboBox comboPainter; 032 private final int randomPainter = 1; 033 034 035 public BarChartViz() { 036 super(); 037 useRandomColor = false; 038 } 039 040 public Dimension[] createDimension() { 041 Dimension [] returnDimension = super.createDimension(); 042 043 yAxis.addSortSchemes("Value"); 044 return returnDimension; 045 } 046 047 protected JPanel createConfigurationPanel() { 048 JPanel panelBottom = new JPanel(new GridLayout(1,2)); 049 050 comboPainter = new JComboBox(); 051 comboPainter.addItem("Default Painter"); 052 comboPainter.addItem("Random Color Painter"); 053 panelBottom.add(new JLabel("Painter used: ")); 054 panelBottom.add(comboPainter); 055 056 057 JPanel configurationPanel = new JPanel(new FlowLayout()); 058 configurationPanel.add(panelBottom); 059 060 return configurationPanel; 061 } 062 063 public void restoreConfiguration(SerializedVisualization config) { 064 useRandomColor = config.PainterName.equals("Random Color Painter"); 065 comboPainter.setSelectedIndex(useRandomColor ? 1 : 0); 066 super.restoreConfiguration(config); 067 } 068 069 public SerializedVisualization getCurrentConfiguration() { 070 SerializedVisualization data = super.getCurrentConfiguration(); 071 072 data.PainterName = useRandomColor ? "Random Color Painter" : "Default Painter"; 073 return data; 074 } 075 076 public void makeSelection() { 077 preMakeSelection(); 078 if (selectionName==null) return; 079 080 int y1 = canvas.getEndY(); 081 int y2 = canvas.getStartY(); 082 int x1 = canvas.getStartX(); 083 084 if (dataSourceId != Scene.getDataSourceManager().getCurrentDataSourceId()) { 085 Scene.showErrorMessage("The active data source used currently is different from \n" + 086 "this visualization, please choose \"" + 087 Scene.getDataSourceManager().getUsedDataSourceName(dataSourceId)+"\"."); 088 return; 089 } 090 091 if ((y1<0)&&(y2<0)) 092 return; 093 094 if (y1 < 0) { 095 y1 = 0; 096 } 097 098 int selection[] = null; 099 switch (SELECT_OPTION & 0x0f0f) { 100 case 0x0001: // select entities in the draw box 101 selection = new int[y2/barHeight - y1/barHeight + 1]; 102 for (int i = y1/barHeight; i <= y2/barHeight; i++) { 103 selection[i - y1/barHeight] = i; 104 } 105 break; 106 case 0x0100: // select occurred entities 107 int w = image.getW(), h = image.getH(); 108 ArrayList idList = new ArrayList(); 109 for (int i=0; i<w; i++) { 110 for (int j=0; j<h; j++) { 111 ArrayList value = (ArrayList)image.getColor(i,j); 112 if (value == null) continue; 113 Box box = (Box)value.get(0); 114 if ((box.getWidth()>x1) && 115 (((box.y<y2)&&(box.y>y1)) || ((box.y+box.getHeight()<y2)&&(box.y+box.getHeight()>y1))) && 116 !idList.contains(new Long(box.getEntityID()))) 117 idList.add(new Long(box.getEntityID())); 118 } 119 } 120 121 selection = new int[idList.size()]; 122 for (int i=0; i<idList.size(); i++) { 123 selection[i] = yAxis.getSortedIndex(((Long)idList.get(i)) .intValue()); 124 } 125 break; 126 } 127 128 yAxis.makeSelection(selectionName,subjectDefinition.getType(),selection); 129 } 130 131 public void preVisualize() { 132 image = new AutoShapeImage(); 133 adjustTimeAxis(); 134 installPainter(); 135 timeMap.clear(); 136 canvas.reset(); 137 } 138 139 public void receiveElement(Element element) { 140 if (element.isOptional()) return; 141 142 long y = yAxis.getField(element); 143 long v = xAxis.getField(element); 144 painter.paint(image,v,y,0); 145 146 } 147 148 public void visualize() { 149 xMax = ((BarChartPainter)painter).getxMax(); 150 151 yAxis.addComparator(new ValueComparator("Value", false, ((BarChartPainter)painter).getValue(), yAxis.getEntityName2IntMap())); 152 yAxis.selectComparator(comboSortSchemes[0].getSelectedItem().toString()); 153 sort(); 154 } 155 156 public String mouseMove(int x, int y) { 157 int X = canvas.getImageX(x); 158 int Y = canvas.getImageY(y); 159 160 if (image != null) { 161 EVolve.util.painters.shapes.Shape entityShape = ((AutoShapeImage)image).getEntityShapes(X,Y); 162 if (entityShape != null) { 163 Entity entity = yAxis.getEntityFromInt((int)entityShape.getEntityID()); 164 Scene.setStatus(entity.getName()+", "+((Box)entityShape).getRealWidth()); 165 } else 166 Scene.setStatus(" "); 167 } else { 168 Scene.setStatus(" "); 169 } 170 return null; 171 } 172 173 public void makeSelection2() { 174 int x1 = canvas.getStartX(); 175 int x2 = canvas.getEndX(); 176 int y1 = canvas.getEndY(); 177 int y2 = canvas.getStartY(); 178 179 if (((x1<0)&&(x2<0)) || ((x1>=timeMap.size()))&&(x2>=timeMap.size())) 180 return; 181 182 if (x1 < 0) { 183 x1 = 0; 184 } 185 186 if (x1 > (timeMap.size() - 1)) { 187 x1 = timeMap.size() - 1; 188 } 189 190 if (x2 > (timeMap.size() - 1)) { 191 x2 = timeMap.size() - 1; 192 } 193 194 if (y1 < 0) { 195 y1 = 0; 196 } 197 198 preMakeSelection(); 199 long start = ((long[])timeMap.get(x1))[1]; 200 long end = ((long[])timeMap.get(x2))[1]; 201 202 if (((SELECT_OPTION & 0x000f) != SELECT_TIME_FRAME)||((x2==x1)&&(x1==0))) { // do not select time frame 203 start = 0; 204 end = Long.MAX_VALUE; 205 } else { 206 if (x1==x2) { 207 if (x2+1<timeMap.size()) { 208 end = ((long[])timeMap.get(x2+1))[1]; 209 } else 210 end = Long.MAX_VALUE; 211 } 212 } 213 214 int[] selection = null; 215 switch (SELECT_OPTION & 0x0ff0) { 216 case 0x0100: // select all entities in the draw box 217 selection = new int[y2/barHeight - y1/barHeight + 1]; 218 for (int i = y1/barHeight; i <= y2/barHeight; i++) { 219 selection[i - y1/barHeight] = i; 220 } 221 break; 222 case 0x0010: // select occurred entities 223 int w = image.getW(), h = image.getH(); 224 ArrayList idList = new ArrayList(); 225 for (int i=0; i<w; i++) { 226 for (int j=0; j<h; j++) { 227 ArrayList value = (ArrayList)image.getColor(i,j); 228 if (value == null) continue; 229 Box box = (Box)value.get(0); 230 if (((x2-box.x < x2-x1+box.getWidth())||(box.x-x1 < x2-x1)) && 231 ((box.y-y2 <0) || (y1-box.y>box.getHeight())) && 232 !idList.contains(new Long(box.getEntityID())) ) 233 idList.add(new Long(box.getEntityID())); 234 } 235 } 236 237 selection = new int[idList.size()]; 238 for (int i=0; i<idList.size(); i++) { 239 selection[i] = ((Long)idList.get(i)).intValue(); 240 } 241 break; 242 case 0x0000: // no selection on entities 243 y1 = 0; 244 y2 = yAxis.getEntityNumber() - 1; 245 selection = new int[y2 - y1 + 1]; 246 for (int i = y1; i <= y2; i++) { 247 selection[i - y1] = i; 248 } 249 break; 250 } 251 yAxis.makeSelection(selectionName, subjectDefinition.getType(),selection,start,end,timeMap); 252 } 253 254 protected void installPainter() { 255 useRandomColor = (comboPainter.getSelectedIndex() == randomPainter); 256 painter = new BarChartPainter(canvas, barHeight,useRandomColor); 257 } 258 259 public JCheckBox[] createSelectionOptions() { 260 if (selectionOptions != null) return selectionOptions; 261 262 chkSelectOccurredEntities = new JCheckBox("Occurred Entities in the box"); 263 chkSelectOccurredEntities.setSelected(true); 264 chkSelectOccurredEntities.addActionListener(new ActionListener() { 265 public void actionPerformed(ActionEvent e) { 266 boolean selected = chkSelectOccurredEntities.isSelected(); 267 SELECT_OPTION = switchOption(selected, SELECT_OPTION, SELECT_Y_AXIS); 268 269 if (chkSelectAllEntities.isSelected() && selected) { 270 chkSelectAllEntities.setSelected(false); 271 SELECT_OPTION = switchOption(false,SELECT_OPTION,SELECT_ALL_Y_AXIS); 272 } 273 } 274 }); 275 276 chkSelectAllEntities = new JCheckBox("All Entities in the Y-Axis range"); 277 chkSelectAllEntities.addActionListener(new ActionListener() { 278 public void actionPerformed(ActionEvent e) { 279 boolean selected = chkSelectAllEntities.isSelected(); 280 SELECT_OPTION = switchOption(selected, SELECT_OPTION, SELECT_ALL_Y_AXIS); 281 282 if (chkSelectOccurredEntities.isSelected() && selected) { 283 chkSelectOccurredEntities.setSelected(false); 284 SELECT_OPTION = switchOption(false,SELECT_OPTION,SELECT_Y_AXIS); 285 } 286 } 287 }); 288 chkSelectAllEntities.setSelected(false); 289 290 selectionOptions = new JCheckBox[2]; 291 selectionOptions[0] = chkSelectOccurredEntities; 292 selectionOptions[1] = chkSelectAllEntities; 293 294 return selectionOptions; 295 } 296 297 public Object clone() { 298 BarChartViz o = (BarChartViz) super.clone(); 299 o.panelConfiguration = o.createConfigurationPanel(); 300 o.createDialog(); 301 return o; 302 } 303 }