001    /*
002     * Created by IntelliJ IDEA.
003     * User: Wei Wang
004     * Date: 2002-9-6
005     * Time: 15:53:23
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.visualization.*;
012    import EVolve.visualization.Dimension;
013    import EVolve.Scene;
014    import javax.swing.*;
015    import javax.swing.event.*;
016    import java.awt.*;
017    import java.awt.event.*;
018    import java.util.ArrayList;
019    
020    public class SynchronousOverlapper extends OverlapVisualization{
021        private final String name = "SynchronousOverlapper";
022        private Visualization overlappedVisualization;
023        private JList availableList,overlappableList;
024        private DefaultListModel modelList;
025        private ArrayList candidates;
026        private boolean shift_ctrl_pressed;
027    
028        public SynchronousOverlapper() {
029            super();
030            colorList = new ArrayList();
031            dialog = null;
032            overlappedVisualization = null;
033            candidates = new ArrayList();
034            modelList = new DefaultListModel();
035            xMax = 0;
036        }
037    
038        public String getName() {
039            return name;
040        }
041    
042        public void createDialog() {
043            shift_ctrl_pressed = false;
044            colorList.clear();
045            visualizationList.clear();
046            modelList.removeAllElements();
047            dialog = new JDialog(Scene.getFrame(), "Overlap Visualizations...", true);
048            dialog.setBounds(new Rectangle(500,400));
049    
050            Box boxMain = new Box(BoxLayout.Y_AXIS);
051            boxMain.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
052                    "Choose Visualizations to be overlapped"));
053            dialog.getContentPane().add(boxMain,BorderLayout.CENTER);
054    
055            Box boxLabels = new Box(BoxLayout.X_AXIS);
056            boxLabels.add(new JLabel("Available visualizations:"));
057            boxLabels.add(Box.createHorizontalStrut(130));
058            boxLabels.add(new JLabel("Overlappable visualizations:"));
059    
060            JPanel boxLists = new JPanel(new GridLayout(1,2,25,2));
061            DefaultListModel modelAvail = new DefaultListModel();
062            availableList = new JList(modelAvail);
063            ArrayList arr = Scene.getVisualizationManager().getVisualizationList();
064            for (int i=0; i<arr.size(); i++) {
065                modelAvail.addElement((((Visualization)arr.get(i)).getName()));
066            }
067            availableList.addListSelectionListener(new ListSelectionListener() {
068                public void valueChanged(ListSelectionEvent e){
069                    updateOverlappableList();
070                }
071            });
072    
073            overlappableList = new JList(modelList);
074            overlappableList.addKeyListener(new KeyListener(){
075                public void keyPressed(KeyEvent e) {
076                    if ((e.getKeyCode() == KeyEvent.VK_SHIFT) || (e.getKeyCode()==KeyEvent.VK_CONTROL)) {
077                        shift_ctrl_pressed = true;
078                    }
079                }
080                public void keyReleased(KeyEvent e) {
081                    if ((e.getKeyCode() == KeyEvent.VK_SHIFT) || (e.getKeyCode()==KeyEvent.VK_CONTROL)) {
082                        shift_ctrl_pressed = false;
083                    }
084                }
085                public void keyTyped(KeyEvent e) { }
086            });
087            overlappableList.addListSelectionListener(new ListSelectionListener() {
088                public void valueChanged(ListSelectionEvent e){
089                    if ((!shift_ctrl_pressed)&&(!e.getValueIsAdjusting()) && overlappableList.isFocusOwner())
090                        selectColor();
091                }
092            });
093            JScrollPane scrollPane1 = new JScrollPane(availableList);
094            JScrollPane scrollPane2 = new JScrollPane(overlappableList);
095            boxLists.add(scrollPane1);
096            boxLists.add(scrollPane2);
097    
098            boxMain.add(Box.createVerticalStrut(-65));
099            boxMain.add(boxLabels);
100            boxMain.add(Box.createVerticalStrut(-65));
101            boxMain.add(boxLists);
102            boxMain.add(Box.createVerticalStrut(10));
103    
104            Box boxOkCancel = new Box(BoxLayout.X_AXIS);
105            JButton buttonOK = new JButton("OK");
106            JButton buttonCancel = new JButton("Cancel");
107    
108            buttonOK.addActionListener(new ActionListener(){
109                public void actionPerformed(ActionEvent e){
110                    onOK();
111                }
112            });
113            buttonCancel.addActionListener(new ActionListener(){
114                public void actionPerformed(ActionEvent e){
115                    onCancel();
116                }
117            });
118    
119            boxOkCancel.add(Box.createHorizontalStrut(180));
120            boxOkCancel.add(buttonOK);
121            boxOkCancel.add(Box.createHorizontalStrut(20));
122            boxOkCancel.add(buttonCancel);
123    
124            dialog.getContentPane().add(boxOkCancel,BorderLayout.SOUTH);
125            //dialog.setResizable(false);
126        }
127    
128        private void selectColor() {
129            int index = overlappableList.getSelectedIndex();
130    
131            if (index != -1) {
132                Color newColor = JColorChooser.showDialog(Scene.getFrame(), "Choose a color", Color.black);
133                if (newColor != null) {
134                    colorList.add(index+1,newColor);
135                    colorList.remove(index+2);
136                    modelList.removeAllElements();
137                    for (int i=0; i<candidates.size(); i++) {
138                        if (colorList.get(i+1) == null)
139                            modelList.addElement(((Visualization)candidates.get(i)).getName());
140                        else
141                            modelList.addElement("<html><font color=#" + getColorHex((Color)colorList.get(i+1)) + ">"
142                                                 +((Visualization)candidates.get(i)).getName() +" </font></html>" );
143                    }
144                    overlappableList.setSelectedIndex(index);
145                } else {
146                    newColor = Color.black;
147                }
148            }
149        }
150    
151        private void updateOverlappableList() {
152            ArrayList arr = Scene.getVisualizationManager().getVisualizationList();
153            overlappedVisualization = (Visualization)arr.get(availableList.getSelectedIndex());
154    
155            candidates.clear();
156            colorList.clear();
157            overlappableList.removeAll();
158            modelList.removeAllElements();
159            colorList.add(null);
160            for (int i=0; i<arr.size(); i++) {
161                Visualization visual =(Visualization)arr.get(i);
162                if (visual.getVisualizationID() == overlappedVisualization.getVisualizationID()) continue;
163                if (isOverlapable(visual)) {
164                    modelList.addElement(visual.getName());
165                    colorList.add(null);
166                    candidates.add(visual);
167                }
168            }
169        }
170    
171        private void onOK() {
172            int[] indexes = overlappableList.getSelectedIndices();
173            visualizationList.clear();
174    
175            if (indexes.length == 0) {
176                Scene.showErrorMessage("Please select visualizations to be linked");
177                return;
178            }
179    
180            visualizationList.add(overlappedVisualization);
181            for (int i=0; i<indexes.length; i++) {
182                visualizationList.add(candidates.get(indexes[i]));
183            }
184    
185            ArrayList newColorList = new ArrayList();
186            newColorList.add(null);
187            for (int i=0; i<modelList.size(); i++) {
188                for (int j=0; j<indexes.length;j++) {
189                    if (i==indexes[j]) {
190                        newColorList.add(colorList.get(i+1));
191                        break;
192                    }
193                }
194            }
195            colorList = newColorList;
196            dialog.setVisible(false);
197            noEntityAvailable = false;
198            overlappedVisualize();
199        }
200    
201        private void onCancel() {
202            dialog.setVisible(false);
203        }
204    
205        public boolean isOverlapable(Visualization visualToBeOverlapped) {
206            for (int i=0; i<2; i++) {
207                if ((overlappedVisualization.getLinkableDimension(i) == null)||
208                   (visualToBeOverlapped.getLinkableDimension(i) == null))
209                {
210                    if ((overlappedVisualization.getDimension()[i] instanceof ReferenceDimension) ||
211                        visualToBeOverlapped.getDimension()[i] instanceof ReferenceDimension)
212                        return false;
213    
214                    if (!overlappedVisualization.getDimension()[i].getName().equals(visualToBeOverlapped.getDimension()[i].getName()))
215                        return false;
216    
217                } else {
218                    if (overlappedVisualization.getDimension()[i].getDataFilter().getTargetType() !=
219                        visualToBeOverlapped.getDimension()[i].getDataFilter().getTargetType())
220                        return false;
221                }
222            }
223    
224            if ((visualToBeOverlapped.getSubjectDefinition().getName().equals(overlappedVisualization.getSubjectDefinition().getName())) &&
225                (visualToBeOverlapped.getFactory().getName().equals(overlappedVisualization.getFactory().getName())))
226                return false;
227    
228            return true;
229        }
230    
231        public void overlappedVisualize() {
232            maxEntity[0] = null;
233            maxEntity[1] = null;
234            xMax = 0;
235            if (((Visualization)visualizationList.get(0)).getDimension()[0] instanceof ReferenceDimension)
236                maxEntity[0] = Scene.getDataManager().getEntity()[((Visualization)visualizationList.get(0)).getDimension()[0].getDataFilter().getTargetType()];
237            if (((Visualization)visualizationList.get(0)).getDimension()[1] instanceof ReferenceDimension)
238                maxEntity[1] = Scene.getDataManager().getEntity()[((Visualization)visualizationList.get(0)).getDimension()[1].getDataFilter().getTargetType()];
239    
240            if (window instanceof EVolve.Window) {
241                Scene.getUIManager().removeWindow(window);
242                window = null;
243            }
244    
245            Scene.getVisualizationManager().prepareForLinkedViz(visualizationList,0);
246            Scene.visualize();
247    
248            unifyVisualizations();
249    
250            newOverlappedVisualization(this);
251    
252            sort();
253            enableSortMenu();
254        }
255    
256        private void unifyVisualizations() {
257            for (int i=0; i<visualizationList.size(); i++) {
258                Visualization visual = (Visualization)visualizationList.get(i);
259                for (int j=0; j<maxEntity.length; j++) {
260                    Dimension dim = visual.getDimension()[j];
261                    if (dim instanceof ReferenceDimension) {
262                        ((ReferenceDimension)dim).linkEntities(maxEntity[j]);
263                        ((ReferenceDimension)dim).visualize();
264                    }
265                }
266                // recoloring with the selected color scheme
267                AutoImage image = visual.getImage();
268                Color newColor = (Color)colorList.get(i);
269                int w = image.getW();
270                int h = image.getH();
271    
272                if (newColor == null) continue;
273                for (int j=0; j<w; j++) {
274                    for (int k=0; k<h; k++) {
275                        if (image.getColor(j,k) != null)
276                            image.setColor(j,k,newColor);
277                    }
278                }
279    
280            }
281        }
282    }