OSDN Git Service

Remove Date tag of Subversion
[stigmata/stigmata.git] / src / main / java / jp / sourceforge / stigmata / ui / swing / graph / SimilarityDistributionGraphPane.java
1 package jp.sourceforge.stigmata.ui.swing.graph;
2
3 /*
4  * $Id$
5  */
6
7 import java.awt.BorderLayout;
8 import java.awt.Color;
9 import java.awt.Dimension;
10 import java.awt.FlowLayout;
11 import java.awt.Graphics2D;
12 import java.awt.RenderingHints;
13 import java.awt.event.ActionEvent;
14 import java.awt.event.ActionListener;
15 import java.awt.geom.Line2D;
16 import java.awt.image.BufferedImage;
17 import java.io.File;
18 import java.io.IOException;
19 import java.util.HashSet;
20 import java.util.Map;
21 import java.util.Set;
22
23 import javax.imageio.ImageIO;
24 import javax.swing.Box;
25 import javax.swing.ImageIcon;
26 import javax.swing.JButton;
27 import javax.swing.JLabel;
28 import javax.swing.JOptionPane;
29 import javax.swing.JPanel;
30
31 import jp.sourceforge.stigmata.ui.swing.GUIUtility;
32 import jp.sourceforge.stigmata.ui.swing.StigmataFrame;
33 import jp.sourceforge.stigmata.ui.swing.actions.ChangeColorAction;
34
35 /**
36  * 
37  * @author Haruaki TAMADA
38  * @version $Revision$ 
39  */
40 public class SimilarityDistributionGraphPane extends JPanel{
41     private static final long serialVersionUID = 2314463453465L;
42
43     private StigmataFrame stigmata;
44     private Map<Integer, Integer> distributions;
45     private int totalCount = 0;
46     private int maxFrequency = 0;
47     private JLabel iconLabel;
48     private BufferedImage image;
49
50     public SimilarityDistributionGraphPane(StigmataFrame stigmata, Map<Integer, Integer> distributions){
51         this.stigmata = stigmata;
52
53         this.distributions = distributions;
54         initializeLayouts();
55         initializeData();
56
57         drawGraph(Color.RED);
58     }
59
60     public String[] getSupportedFormats(){
61         String[] formats = ImageIO.getWriterFormatNames();
62         Set<String> set = new HashSet<String>();
63         for(String f: formats){
64             if(f != null){
65                 set.add(f.toLowerCase());
66             }
67         }
68         return set.toArray(new String[set.size()]);
69     }
70
71     private void drawGraph(Color color){
72         Graphics2D g = image.createGraphics();
73         g.setColor(getBackground());
74         g.fillRect(0, 0, image.getWidth(), image.getHeight());
75
76         int width = image.getWidth();
77         int height = image.getHeight();
78         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
79
80         drawBorder(g, width, height);
81
82         g.setColor(color);
83         Dimension d = new Dimension(width - 20, height - 20);
84         double w = (d.width / 100d);
85
86         Integer v1 = distributions.get(0);
87         if(v1 == null) v1 = new Integer(0);
88         double x = 20;
89         for(int i = 0; i <= 100; i++){
90             Integer v2 = distributions.get(i);
91             if(v2 == null) new Integer(0);
92
93             double hh1 = v1 * ((double)height / totalCount);
94             double hh2 = v2 * ((double)height / totalCount);
95
96             g.draw(new Line2D.Double(x, d.height - hh1, x + w, d.height - hh2));
97             x += w;
98             v1 = v2;
99         }
100         iconLabel.setIcon(new ImageIcon(image));
101     }
102
103     private void drawBorder(Graphics2D g, int width, int height){
104         g.setColor(Color.BLACK);
105         g.draw(new Line2D.Double(0, height - 20, width, height - 20));
106         g.draw(new Line2D.Double(20, 0, 20, height));
107
108         g.setColor(Color.GRAY);
109         // x axis
110         double h = (height - 20) / 2d;
111         g.draw(new Line2D.Double(20, h, width, h));
112         h = h / 2;
113         g.draw(new Line2D.Double(20, h, width, h));
114         g.draw(new Line2D.Double(20, h * 3, width, h * 3));
115
116         // y axis
117         double w = (width - 20d) / 2d;
118         g.draw(new Line2D.Double(w + 20, 0, w + 20, height - 20));
119         w = w / 2;
120         g.draw(new Line2D.Double(w + 20, 0, w + 20, height - 20));
121         g.draw(new Line2D.Double(w * 3 + 20, 0, w * 3 + 20, height - 20));
122
123         g.drawString("0", 10, height - 5);
124         g.drawString("50%", (width - 20) / 2 + 10, height - 5);
125         g.drawString(stigmata.getMessages().get("similarity.label"), width - 60, height - 5);
126         g.drawString("50%", 0, (height - 20) / 2);
127     }
128
129     private void initializeData(){
130         maxFrequency = 0;
131         for(int i = 0; i <= 100; i++){
132             Integer frequency = distributions.get(new Integer(i));
133             if(frequency == null){
134                 frequency = new Integer(0);
135                 distributions.put(new Integer(i), frequency);
136             }
137             if(maxFrequency < frequency.intValue()){
138                 maxFrequency = frequency.intValue();
139             }
140             totalCount += frequency.intValue();
141         }
142     }
143
144     private void initializeLayouts(){
145         image = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB);
146
147         JPanel center = new JPanel(new FlowLayout(FlowLayout.CENTER));
148         iconLabel = new JLabel();
149         Box south = Box.createHorizontalBox();
150         JButton storeImageButton = GUIUtility.createButton(stigmata.getMessages(), "savegraph");
151         JButton switchColorButton = new JButton(new ChangeColorAction(stigmata, new ActionListener(){
152             public void actionPerformed(ActionEvent e){
153                 ChangeColorAction action = (ChangeColorAction)e.getSource();
154                 if(action.isColorSelected()){
155                     drawGraph(action.getColor());
156                 }
157             }
158         }));
159         storeImageButton.addActionListener(new ActionListener(){
160             public void actionPerformed(ActionEvent e){
161                 storeGraphImage();
162             }
163         });
164
165         setLayout(new BorderLayout());
166         center.add(iconLabel = new JLabel());
167         south.add(Box.createHorizontalGlue());
168         south.add(switchColorButton);
169         south.add(Box.createHorizontalGlue());
170         south.add(storeImageButton);
171         south.add(Box.createHorizontalGlue());
172
173         add(center, BorderLayout.CENTER);
174         add(south, BorderLayout.SOUTH);
175     }
176
177     private void storeGraphImage(){
178         String[] exts = getSupportedFormats();
179         File file = stigmata.getSaveFile(
180             exts, stigmata.getMessages().get("savegraph.description")
181         );
182         try{
183             if(file != null){
184                 String format = file.getName();
185                 format = format.substring(format.lastIndexOf('.') + 1);
186
187                 ImageIO.write(image, format, file);
188             }
189         } catch(IOException e){
190             JOptionPane.showMessageDialog(
191                 this,
192                 stigmata.getMessages().get("error.io", e.getMessage()),
193                 stigmata.getMessages().get("error.dialog.title"),
194                 JOptionPane.ERROR_MESSAGE
195             );
196         }
197     }
198 }