001    /**
002     * Created by IntelliJ IDEA.
003     * User: Wei Wang
004     * Date: Nov 27, 2002
005     * Time: 3:50:38 PM
006     */
007    
008    package EVolve.visualization.XYViz;
009    
010    import EVolve.visualization.*;
011    import EVolve.util.Painters.*;
012    import EVolve.util.Magnifier;
013    import EVolve.util.SourceBrowser.SourceBrowser;
014    import EVolve.Scene;
015    import javax.swing.*;
016    import java.util.HashMap;
017    import java.awt.event.*;
018    import java.awt.*;
019    
020    public abstract class XYVisualization extends Visualization{
021        protected int autoInterval;
022        protected String autoPredictorname;
023        protected int interval;
024        protected boolean shift_pressed;
025        protected AutoImage image; // the image
026        protected Painter painter;
027        protected int xMax; // maximum value on X-axis
028        protected Magnifier magnifier;
029        private JMenuItem itemChangeOrientation;
030        protected boolean normalOrientation;
031        protected ReferenceDimension threadFilter;
032        protected int currentThread;
033        protected HashMap imageMap;
034        protected HashMap threadDataSet;
035        protected int xOffset;
036        protected SourceBrowser sourceBrowser;
037        private JMenuItem itemBrowseSource;
038    
039        protected abstract void mouseMove(int x, int y);
040    
041        public XYVisualization() {
042            super();
043            shift_pressed = false;
044            autoInterval = -1;
045            autoPredictorname = null;
046            interval = -1;
047            itemChangeOrientation = null;
048            normalOrientation = true;
049            threadFilter = new ReferenceDimension();
050            currentThread = -1;
051            imageMap = new HashMap();
052            threadDataSet = new HashMap();
053            sourceBrowser = new SourceBrowser();
054        }
055    
056        protected JPanel createPanel() {
057            AxesPanel returnVal = new AxesPanel();
058    
059            magnifier = new Magnifier();
060            returnVal.addMouseMotionListener(new MouseMotionAdapter() {
061                public void mouseMoved(MouseEvent e) {
062                    if (e.isShiftDown()) shift_pressed = true;
063                    else shift_pressed = false;
064    
065                    if (e.isControlDown()) {
066                        magnifier.showWindow(image,null,((AxesPanel)panel).getImageX(e.getX()),
067                                            ((AxesPanel)panel).getImageY(e.getY()));
068                        ((AxesPanel)panel).drawZoomingArea(e.getX(),e.getY());
069                    }
070                    mouseMove(e.getX(), e.getY());
071                }
072            });
073    
074            returnVal.addMouseMotionListener(new MouseMotionAdapter() {
075                public void mouseDragged(MouseEvent e) {
076                    if (e.isShiftDown()) shift_pressed = true;
077                    else shift_pressed = false;
078                    mouseMove(e.getX(), e.getY());
079                }
080            });
081    
082            return returnVal;
083        }
084    
085        protected void updateConfiguration() {
086            initialThreadFilter();
087            ((AxesPanel)panel).setImage(null);
088            panel.repaint();
089            normalOrientation = true;
090        }
091    
092        protected void createMenu() {
093            super.createMenu();
094    
095            itemBrowseSource = new JMenuItem("Browse source code...");
096            itemBrowseSource.setMnemonic(KeyEvent.VK_B);
097            itemBrowseSource.addActionListener(new ActionListener() {
098                public void actionPerformed(ActionEvent e) {
099                    sourceBrowser.showSourceFile(getEntityUnderMouse());
100                }
101            });
102            popup.add(itemBrowseSource);
103            itemBrowseSource.setEnabled(false);
104        }
105    
106        public VisualizationDefinition getDefinition() {
107            return definition;
108        }
109    
110        public void autoUpdateConfiguration(HashMap config) {
111            autoInterval = ((Integer)config.get("Interval")).intValue();
112            PredictorFactory predictor = (PredictorFactory)config.get("Predictor");
113            autoPredictorname = (predictor == null) ? null : predictor.getName();
114            interval = autoInterval;
115            super.autoUpdateConfiguration(config);
116        }
117    
118        public HashMap getCurrentConfigure() {
119            HashMap configure = super.getCurrentConfigure();
120    
121            configure.put("Interval", new Integer(interval));
122    
123            return configure;
124        }
125    
126        public int getxMax() {
127            return xMax;
128        }
129    
130        public AutoImage getImage() {
131            return image;
132        }
133    
134        public void setImage(AutoImage image) {
135            this.image = image;
136        }
137    
138        public void updateMenu() {
139            super.updateMenu();
140            if (itemChangeOrientation == null) {
141                itemChangeOrientation = new JMenuItem("Change Orientation");
142                itemChangeOrientation.setMnemonic(KeyEvent.VK_C);
143                itemChangeOrientation.addActionListener(new ActionListener() {
144                    public void actionPerformed(ActionEvent e) {
145                        changeOrientation();
146                    }
147                });
148                popup.add(itemChangeOrientation);
149            }
150        }
151    
152        public void disablePopupMenu() {
153            MenuElement[] menus = popup.getSubElements();
154            for (int i=0; i<menus.length; i++) {
155                if (((JMenuItem)menus[i]).getText().equals("Sort") || ((JMenuItem)menus[i]).getText().equals("Save..."))
156                    continue;
157                ((JMenuItem)menus[i]).setEnabled(false);
158            }
159            Scene.getUIManager().disableVisualizationMenu();
160        }
161    
162        protected void changeOrientation() {
163            AutoImage newImage = new AutoImage();
164            int wMax = image.getW();
165            int hMax = image.getH();
166            Color color;
167    
168            for (int i=0; i<wMax; i++) {
169                for (int j=0; j<hMax; j++) {
170                    color = image.getColor(i,j);
171                    if (color != null) {
172                        newImage.setColor(j,i,color);
173                    }
174                }
175            }
176            image = newImage;
177    
178            normalOrientation = ! normalOrientation;
179        }
180    
181        protected void initialThreadFilter() {
182            int subjectIndex = 0;
183            for (int i=0; i<elementDefinition.length; i++)
184                if (elementDefinition[i].getName().equals(subjectDefinition.getName())) {subjectIndex = i;}
185    
186            for (int i=0; i<dataFilter[subjectIndex][1].length; i++) {
187                if (dataFilter[subjectIndex][1][i].getName().equals("Thread"))
188                    threadFilter.setDataFilter(dataFilter[subjectIndex][1][i]);
189            }
190        }
191    
192        protected void switchThread(int threadId) {
193            if (currentThread != threadId) {
194                currentThread = threadId;
195                image = (AutoImage)imageMap.get(new Integer(threadId));
196                if (image == null) {
197                    image = new AutoImage();
198                    imageMap.put(new Integer(threadId),image);
199                }
200            }
201        }
202    
203        protected void installPainter() {
204            painter = new DefaultPainter();
205        }
206    
207        protected void enableBrowseSourceMenu() {
208            itemBrowseSource.setEnabled(true);
209        }
210    
211        public void cleanup() {
212            super.cleanup();
213            magnifier.cleanup();
214        }
215    
216        protected abstract String getEntityUnderMouse();
217    }