001 /* 002 * Created by IntelliJ IDEA. 003 * User: Wei Wang 004 * Date: 2002-10-17 005 * Time: 17:11:15 006 * To change template for new class use 007 * Code Style | Class Templates options (Tools | IDE Options). 008 */ 009 package EVolve.util; 010 011 import java.awt.BorderLayout; 012 import java.awt.Color; 013 import java.awt.FlowLayout; 014 import java.awt.Rectangle; 015 import java.awt.event.ActionEvent; 016 import java.awt.event.ActionListener; 017 import java.util.ArrayList; 018 import java.util.HashMap; 019 import java.util.Iterator; 020 021 import javax.swing.Box; 022 import javax.swing.BoxLayout; 023 import javax.swing.DefaultListModel; 024 import javax.swing.JButton; 025 import javax.swing.JColorChooser; 026 import javax.swing.JComboBox; 027 import javax.swing.JDialog; 028 import javax.swing.JList; 029 import javax.swing.JPanel; 030 031 import EVolve.Scene; 032 import EVolve.visualization.AutoImage; 033 034 public class ThreadChooser{ 035 private ArrayList threadSet; 036 private HashMap imageMap,colorMap; 037 private JDialog dialog; 038 private JList threads; 039 private DefaultListModel threadListModel; 040 private AutoImage mergedImage; 041 private JComboBox comboThread; 042 private int [] indices; 043 private boolean multiChoice; 044 private String thread; 045 046 047 public ThreadChooser(HashMap imageMap,boolean multiChoice) { 048 this.imageMap = imageMap; 049 colorMap = new HashMap(); 050 this.multiChoice = multiChoice; 051 } 052 053 private String showMultiChoiceDialog() { 054 055 if (imageMap.size() == 0) return null; 056 dialog = new JDialog(Scene.getFrame(), "Choose thread & color to be visualized", true); 057 058 dialog.setBounds(new Rectangle(250,100)); 059 060 threadListModel = new DefaultListModel(); 061 threadSet = new ArrayList(); 062 threads = new JList(threadListModel); 063 Iterator it = imageMap.keySet().iterator(); 064 while (it.hasNext()) { 065 threadSet.add("Thread "+it.next()); 066 } 067 for (int i=0; i<threadSet.size(); i++) { 068 String item = (String)threadSet.get(i); 069 threadListModel.addElement(item); 070 } 071 dialog.getContentPane().add(threads,BorderLayout.CENTER); 072 073 JPanel panelButton = new JPanel(new FlowLayout()); 074 dialog.getContentPane().add(panelButton, BorderLayout.SOUTH); 075 076 JButton buttonColor = new JButton("Coloring"); 077 buttonColor.addActionListener(new ActionListener() { 078 public void actionPerformed(ActionEvent e) { 079 selectColor(); 080 } 081 }); 082 panelButton.add(buttonColor); 083 084 JButton buttonOK = new JButton("OK"); 085 buttonOK.addActionListener(new ActionListener() { 086 public void actionPerformed(ActionEvent e) { 087 if (threads.getSelectedIndices().length == 0) { 088 Scene.showErrorMessage("You must choose at least one thread!"); 089 return; 090 } 091 indices = threads.getSelectedIndices(); 092 dialog.setVisible(false); 093 dialog = null; 094 } 095 }); 096 panelButton.add(buttonOK); 097 098 /*JButton buttonCancel = new JButton("Cancel"); 099 buttonCancel.addActionListener(new ActionListener() { 100 public void actionPerformed(ActionEvent e) { 101 dialog.setVisible(false); 102 } 103 }); 104 panelButton.add(buttonCancel);*/ 105 dialog.pack(); 106 Scene.getUIManager().showDialog(dialog, dialog.getWidth(), dialog.getHeight()); 107 return null; 108 } 109 110 private String showSingleChoiceDialog() { 111 dialog = new JDialog(Scene.getFrame(), "Choose thread to be visualized", true); 112 113 Box boxMain = new Box(BoxLayout.Y_AXIS); 114 dialog.getContentPane().add(boxMain,BorderLayout.CENTER); 115 dialog.setBounds(new Rectangle(250,100)); 116 comboThread = new JComboBox(); 117 thread = null; 118 119 Iterator it = imageMap.keySet().iterator(); 120 while (it.hasNext()) { 121 comboThread.addItem("Thread "+it.next()); 122 } 123 boxMain.add(comboThread); 124 125 JPanel panelButton = new JPanel(new FlowLayout()); 126 dialog.getContentPane().add(panelButton, BorderLayout.SOUTH); 127 128 JButton buttonOK = new JButton("OK"); 129 buttonOK.addActionListener(new ActionListener() { 130 public void actionPerformed(ActionEvent e) { 131 if (!validateThread()) 132 Scene.showErrorMessage("No data is available for this thread."); 133 else { 134 thread = (String)comboThread.getSelectedItem(); 135 dialog.setVisible(false); 136 } 137 } 138 }); 139 panelButton.add(buttonOK); 140 141 JButton buttonCancel = new JButton("Cancel"); 142 buttonCancel.addActionListener(new ActionListener() { 143 public void actionPerformed(ActionEvent e) { 144 dialog.setVisible(false); 145 } 146 }); 147 panelButton.add(buttonCancel); 148 dialog.pack(); 149 Scene.getUIManager().showDialog(dialog, dialog.getWidth(), dialog.getHeight()); 150 151 dialog = null; 152 return thread; 153 } 154 155 public String showDialog() { 156 if (multiChoice) 157 return showMultiChoiceDialog(); 158 else 159 return showSingleChoiceDialog(); 160 } 161 162 private void selectColor() { 163 int index = threads.getSelectedIndex(); 164 165 if (index != -1) { 166 Color newColor = JColorChooser.showDialog(Scene.getFrame(), "Choose a color", Color.black); 167 if (newColor != null) { 168 colorMap.put(new Integer(index),newColor); 169 threadListModel.removeAllElements(); 170 171 for (int i=0; i<threadSet.size(); i++) { 172 if (colorMap.get(new Integer(i)) == null) 173 threadListModel.addElement(threadSet.get(i)); 174 else 175 threadListModel.addElement("<html><font color=#" + getColorHex((Color)colorMap.get(new Integer(i))) + ">" 176 + threadSet.get(i) +" </font></html>" ); 177 } 178 } 179 } 180 } 181 182 public AutoImage coloringImages(HashMap imageMap) { 183 mergedImage = new AutoImage(); 184 for (int i=0; i<indices.length; i++) { 185 Color newColor = (Color)colorMap.get(new Integer(indices[i])); 186 String threadId = (String)threadSet.get(indices[i]); 187 threadId = threadId.substring(7,threadId.length()); 188 AutoImage image = (AutoImage)imageMap.get(new Long(threadId)); 189 int w = image.getW(); 190 int h = image.getH(); 191 for (int j=0; j<w; j++) { 192 for (int k=0; k<h; k++) { 193 if (image.getColor(j,k) != null) 194 if (mergedImage.getColor(j,k) != null) 195 mergedImage.setColor(j,k,new Color(153,0,153)); 196 else 197 if (newColor != null) { 198 mergedImage.setColor(j,k,newColor); 199 } else mergedImage.setColor(j,k,image.getColor(j,k)); 200 } 201 } 202 203 } 204 return mergedImage; 205 } 206 207 private String getColorHex(Color color) { 208 String returnVal = Integer.toHexString(color.getBlue()); 209 if (returnVal.length() < 2) { 210 returnVal = "0" + returnVal; 211 } 212 returnVal = Integer.toHexString(color.getGreen()) + returnVal; 213 if (returnVal.length() < 4) { 214 returnVal = "0" + returnVal; 215 } 216 returnVal = Integer.toHexString(color.getRed()) + returnVal; 217 if (returnVal.length() < 6) { 218 returnVal = "0" + returnVal; 219 } 220 221 return returnVal; 222 } 223 224 public AutoImage getMergedImage() { 225 return mergedImage; 226 } 227 228 private boolean validateThread() { 229 AutoImage img; 230 231 String selected = (String)comboThread.getSelectedItem(); 232 img = (AutoImage)imageMap.get(new Long(selected.substring(7,selected.length()))); 233 if (img.getW()*img.getH() <= 0) return false; 234 235 return true; 236 } 237 238 239 }