~ubuntu-branches/debian/sid/latexdraw/sid

« back to all changes in this revision

Viewing changes to latexDraw/ui/components/XScale.java

  • Committer: Bazaar Package Importer
  • Author(s): Stuart Prescott
  • Date: 2009-05-03 23:49:35 UTC
  • Revision ID: james.westby@ubuntu.com-20090503234935-cls7n48x018g0vk2
Tags: upstream-2.0.2+1
ImportĀ upstreamĀ versionĀ 2.0.2+1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package latexDraw.ui.components;
 
2
 
 
3
import java.awt.BasicStroke;
 
4
import java.awt.Color;
 
5
import java.awt.Dimension;
 
6
import java.awt.Graphics;
 
7
import java.awt.Graphics2D;
 
8
import java.awt.RenderingHints;
 
9
import java.awt.event.ItemEvent;
 
10
import java.awt.event.ItemListener;
 
11
import java.awt.event.MouseEvent;
 
12
import java.awt.event.MouseListener;
 
13
import java.awt.geom.Line2D;
 
14
 
 
15
import javax.swing.ButtonGroup;
 
16
import javax.swing.JCheckBoxMenuItem;
 
17
import javax.swing.JComboBox;
 
18
import javax.swing.JComponent;
 
19
import javax.swing.JPopupMenu;
 
20
 
 
21
import latexDraw.lang.LaTeXDrawLang;
 
22
import latexDraw.psTricks.PSTricksConstants;
 
23
import latexDraw.ui.DrawPanel;
 
24
import latexDraw.ui.dialog.ExceptionFrameDialog;
 
25
 
 
26
 
 
27
/** 
 
28
 * This class defines the X-scale.<br>
 
29
 *<br>
 
30
 * This file is part of LaTeXDraw<br>
 
31
 * Copyright (c) 2005-2008 Arnaud BLOUIN<br>
 
32
 *<br>
 
33
 *  LaTeXDraw is free software; you can redistribute it and/or modify
 
34
 *  it under the terms of the GNU General Public License as published by
 
35
 *  the Free Software Foundation; either version 2 of the License, or
 
36
 *  (at your option) any later version.<br>
 
37
 *<br>
 
38
 *  LaTeXDraw is distributed without any warranty; without even the 
 
39
 *  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
 
40
 *  PURPOSE. See the GNU General Public License for more details.<br>
 
41
 *<br>
 
42
 * 01/20/06<br>
 
43
 * @author Arnaud BLOUIN<br>
 
44
 * @version 2.0.0<br>
 
45
 */
 
46
public class XScale extends JComponent implements MouseListener,ItemListener
 
47
{
 
48
        private static final long serialVersionUID = 1L;
 
49
 
 
50
        /** The drawPanel which contains the pixels per centimetre */
 
51
        private DrawPanel drawPanel;
 
52
        
 
53
        /** The other scale : the Y-scale (Useful for a good display) */
 
54
        private YScale yScale;
 
55
        
 
56
        /** The size of the lines in axes */
 
57
        public static final int SIZE = 10;
 
58
        
 
59
        /** The popup menu which allows to set the type of measure (inch, cm, ...) */
 
60
        private JPopupMenu popupMenu;
 
61
        
 
62
        /** The label of the centimetre menu */
 
63
        public static final String LABEL_CM = LaTeXDrawLang.getOthersString("XScale.cm"); //$NON-NLS-1$
 
64
        
 
65
        /** The label of the inch menu */
 
66
        public static final String LABEL_INCH = LaTeXDrawLang.getOthersString("XScale.inch"); //$NON-NLS-1$
 
67
        
 
68
        /** The unit of length by default */
 
69
        public static final String DEFAULT_UNIT = LABEL_CM;
 
70
        
 
71
        /** The menu for centimetres */ 
 
72
        protected JCheckBoxMenuItem cmMenu;
 
73
        
 
74
        /** The menu for inches */
 
75
        protected JCheckBoxMenuItem inchMenu;
 
76
        
 
77
        
 
78
        
 
79
        /**
 
80
         * The constructor.
 
81
         * @param drawP The drawPanel which contains the pixels per centimetre.
 
82
         * @param y The YScale bounds to the XScale.
 
83
         * @throws IllegalArgumentException If drawP or y is null.
 
84
         */
 
85
        public XScale(DrawPanel drawP, YScale y)
 
86
        {
 
87
                super();
 
88
                
 
89
                if(drawP==null || y==null)
 
90
                        throw new IllegalArgumentException();
 
91
                
 
92
                setPreferredSize(new Dimension(500, SIZE));
 
93
                drawPanel = drawP;
 
94
                yScale = y;
 
95
                
 
96
                /* Creation of the popup menu */
 
97
                ButtonGroup buttonGroup = new ButtonGroup();
 
98
                popupMenu = new JPopupMenu();
 
99
                cmMenu = new JCheckBoxMenuItem(LABEL_CM);
 
100
                cmMenu.setActionCommand(LABEL_CM);
 
101
                cmMenu.addItemListener(this);
 
102
                inchMenu = new JCheckBoxMenuItem(LABEL_INCH);
 
103
                inchMenu.addItemListener(this);
 
104
                inchMenu.setActionCommand(LABEL_INCH);
 
105
                cmMenu.setSelected(true);
 
106
                popupMenu.add(cmMenu);
 
107
                popupMenu.add(inchMenu);
 
108
                buttonGroup.add(cmMenu);
 
109
                buttonGroup.add(inchMenu);
 
110
                addMouseListener(this);
 
111
                setDoubleBuffered(true);
 
112
        }
 
113
        
 
114
 
 
115
        
 
116
    
 
117
        /**
 
118
         * Sets the Y-scale.
 
119
         * @param y The new Y-scale.
 
120
         * @throws IllegalArgumentException If y is null.
 
121
         */
 
122
        public void setYScale(YScale y)
 
123
        {
 
124
                if(y==null)
 
125
                        throw new IllegalArgumentException();
 
126
                
 
127
                yScale = y;
 
128
        }
 
129
        
 
130
        
 
131
        
 
132
        
 
133
        /**
 
134
         * Allows to access to the popup menu.
 
135
         * @return The popup menu.
 
136
         */
 
137
        public JPopupMenu getPopupMenu()
 
138
        {
 
139
                return popupMenu;
 
140
        }
 
141
        
 
142
        
 
143
        
 
144
        
 
145
        /**
 
146
         * Allows to know the X-coordinates of the start of the X-Scale.
 
147
         * @return The X-coordinates of the start of the X-Scale.
 
148
         */
 
149
        public float getStart()
 
150
        {
 
151
                float start;
 
152
                
 
153
                if(yScale==null)
 
154
                start = 0;
 
155
        else
 
156
                if(yScale.isVisible())
 
157
                          start = SIZE;
 
158
                else  start = 0;
 
159
                
 
160
                return start;
 
161
        }
 
162
        
 
163
        
 
164
        
 
165
        
 
166
        @Override
 
167
    public void paintComponent(Graphics g)
 
168
    {           
 
169
                try
 
170
                {
 
171
                Dimension d = getSize();
 
172
                double zoom = drawPanel.getZoom();
 
173
                double width = d.getWidth()*(1/zoom), i, j, start, cpt;
 
174
                double pixPerCm = drawPanel.getPixelsPerCm();
 
175
                Graphics2D g2 = (Graphics2D)g;
 
176
                
 
177
                g2.scale(zoom, zoom);
 
178
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
 
179
                        g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
 
180
                        g2.setStroke(new BasicStroke(0, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER));
 
181
                        
 
182
                        Color formerCol = g2.getColor();
 
183
                g2.setColor(Color.BLACK);
 
184
                        
 
185
                if(yScale==null)
 
186
                        start = 0.;
 
187
                else
 
188
                        if(yScale.isVisible())
 
189
                                  start = SIZE;
 
190
                        else  start = 0.;
 
191
          
 
192
                if(inchMenu.isSelected())
 
193
                        pixPerCm*=PSTricksConstants.INCH_VAL_CM;
 
194
                
 
195
                if(pixPerCm/10.!=(int)(pixPerCm/10.))
 
196
                        {
 
197
                        double tmp = pixPerCm/10.;
 
198
                        tmp = tmp-(int)tmp>0.5 ? ((int)tmp)+1 : (int)tmp;
 
199
                        pixPerCm = tmp*10;
 
200
                        }
 
201
 
 
202
                if(pixPerCm>20/zoom)
 
203
                {                                               
 
204
                        double pixPerCm10 = pixPerCm/10.;
 
205
                        
 
206
                        for(i=start/zoom+pixPerCm10; i<width; i+=pixPerCm)
 
207
                                for(j=i, cpt=1; cpt<10; j+=pixPerCm10, cpt++)
 
208
                                        g2.draw(new Line2D.Double(j,(SIZE/2.)/zoom,j,SIZE/zoom));
 
209
                }
 
210
                
 
211
                for(i=start/zoom; i<width;i+=pixPerCm)
 
212
                        g2.draw(new Line2D.Double(i, 0., i, SIZE/zoom));
 
213
                
 
214
                g2.setColor(formerCol);
 
215
                }
 
216
                catch(Exception ex)
 
217
                {
 
218
                        ex.printStackTrace(); 
 
219
                        ExceptionFrameDialog.showExceptionDialog(ex);
 
220
                }
 
221
    }
 
222
 
 
223
 
 
224
        
 
225
        
 
226
    /** 
 
227
     * Defines the actions that it must be realised by a right click of the mouse.
 
228
     * @param e Corresponds to the click of the mouse.
 
229
     */
 
230
        public void mouseClickedRightButton(MouseEvent e)
 
231
    {
 
232
                if(popupMenu!=null)
 
233
                        popupMenu.show(e.getComponent(), e.getX(), e.getY());
 
234
    }
 
235
        
 
236
        
 
237
 
 
238
 
 
239
         /** 
 
240
     * Defines the actions that it must be realised by a click of the mouse.
 
241
     * @param e The event
 
242
     */
 
243
        public void mouseClicked(MouseEvent e) 
 
244
        {
 
245
        switch(e.getButton())
 
246
            {
 
247
                    case MouseEvent.BUTTON1 :           
 
248
                                        break;
 
249
                        
 
250
                    case MouseEvent.BUTTON2 : 
 
251
                                        break;
 
252
        
 
253
                    case MouseEvent.BUTTON3 : 
 
254
                                                mouseClickedRightButton(e);    
 
255
                                                break;
 
256
                                
 
257
                    default : break;
 
258
            } // switch
 
259
 
 
260
        e.consume();
 
261
        }
 
262
 
 
263
        
 
264
 
 
265
        public void mousePressed(MouseEvent e) 
 
266
        {
 
267
                /* No action to do. */
 
268
        }
 
269
 
 
270
 
 
271
        public void mouseReleased(MouseEvent e) 
 
272
        {
 
273
                /* No action to do. */
 
274
        }
 
275
 
 
276
 
 
277
        public void mouseEntered(MouseEvent e) 
 
278
        {
 
279
                /* No action to do. */
 
280
        }
 
281
 
 
282
 
 
283
 
 
284
        public void mouseExited(MouseEvent e) 
 
285
        {
 
286
                /* No action to do. */
 
287
        }
 
288
 
 
289
 
 
290
 
 
291
        public void itemStateChanged(ItemEvent e) 
 
292
        {
 
293
                Object o = e.getSource();
 
294
                
 
295
                if(o instanceof JCheckBoxMenuItem || o instanceof JComboBox)
 
296
                {
 
297
                        boolean isInCm = true;
 
298
 
 
299
                        if(o instanceof JCheckBoxMenuItem)
 
300
                                drawPanel.getDraw().setUnitForPixelsPerCm(
 
301
                                                ((JCheckBoxMenuItem)o).getActionCommand());
 
302
                        else
 
303
                        if(o instanceof JComboBox)
 
304
                        {
 
305
                                if(!(((String)((JComboBox)o).getSelectedItem())).equals(LABEL_CM))
 
306
                                         isInCm = false;
 
307
                                cmMenu.setSelected(isInCm);
 
308
                                inchMenu.setSelected(!isInCm);
 
309
                                
 
310
                                drawPanel.getDraw().setUnitForPixelsPerCm(
 
311
                                                                                ((String)((JComboBox)o).getSelectedItem()));
 
312
                        }
 
313
                        
 
314
                        repaint();
 
315
                        yScale.repaint();
 
316
                }
 
317
        }
 
318
        
 
319
        
 
320
        
 
321
        
 
322
        /**
 
323
         * Sets the unit of length (and to change the scale of the XScale and to check the good menu).
 
324
         * @param unitForPixelsPerCm The unitForPixelsPerCm to set.
 
325
         * @throws IllegalArgumentException If unitForPixelsPerCm is not valid.
 
326
         */
 
327
        public void setUnitForPixelsPerCm(String unitForPixelsPerCm)
 
328
        {
 
329
                if(unitForPixelsPerCm==null || (!unitForPixelsPerCm.equals(LABEL_CM) && !unitForPixelsPerCm.equals(LABEL_INCH)))
 
330
                        throw new IllegalArgumentException();
 
331
                
 
332
                boolean inCm = unitForPixelsPerCm.equals(LABEL_CM);
 
333
                
 
334
                cmMenu.setSelected(inCm);
 
335
                inchMenu.setSelected(!inCm);
 
336
                repaint();
 
337
                yScale.repaint();
 
338
        }
 
339
}