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