Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / swing / optionsEditionByMousePopupMenu / JOptionsEditionByMousePopupMenu.java @ 13136

History | View | Annotate | Download (13.1 KB)

1
package org.gvsig.gui.beans.swing.optionsEditionByMousePopupMenu;
2

    
3
import java.awt.event.ActionEvent;
4
import java.awt.event.ActionListener;
5
import java.beans.PropertyChangeListener;
6
import java.beans.PropertyChangeSupport;
7
import java.io.Serializable;
8
import java.util.HashMap;
9

    
10

    
11
import javax.swing.ImageIcon;
12
import javax.swing.JMenuItem;
13
import javax.swing.JPopupMenu;
14
import javax.swing.border.BevelBorder;
15

    
16
import org.gvsig.gui.beans.Messages;
17

    
18
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
19
*
20
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
21
*
22
* This program is free software; you can redistribute it and/or
23
* modify it under the terms of the GNU General Public License
24
* as published by the Free Software Foundation; either version 2
25
* of the License, or (at your option) any later version.
26
*
27
* This program is distributed in the hope that it will be useful,
28
* but WITHOUT ANY WARRANTY; without even the implied warranty of
29
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30
* GNU General Public License for more details.
31
*
32
* You should have received a copy of the GNU General Public License
33
* along with this program; if not, write to the Free Software
34
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
35
*
36
* For more information, contact:
37
*
38
*  Generalitat Valenciana
39
*   Conselleria d'Infraestructures i Transport
40
*   Av. Blasco Ib??ez, 50
41
*   46010 VALENCIA
42
*   SPAIN
43
*
44
*      +34 963862235
45
*   gvsig@gva.es
46
*      www.gvsig.gva.es
47
*
48
*    or
49
*
50
*   IVER T.I. S.A
51
*   Salamanca 50
52
*   46005 Valencia
53
*   Spain
54
*
55
*   +34 963163400
56
*   dac@iver.es
57
*/
58

    
59

    
60
/**
61
 * This class is a JPopupMenu that can be used with another component
62
 * The items showed in this menu are for editing: UNDO, REDO, CUT, COPY, PASTE, DELETE and SELECT ALL.
63
 * When the user clickes on a item, this Component fires a property change event that its value allows identificate the item clicked
64
 * (The icons used are from the opensource 'Tango Icon Library' project: (http://tango.freedesktop.org/Tango_Icon_Library))
65
 *  
66
 * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
67
 *
68
 */
69
public class JOptionsEditionByMousePopupMenu extends JPopupMenu implements Serializable{
70
        private static final long serialVersionUID = 584506146143698165L;
71

    
72
        // Items of this JPopupMenu
73
        private JMenuItem jMenuItemUndo = null;
74
        private JMenuItem jMenuItemRedo = null;
75
        private JMenuItem jMenuItemCut = null;
76
        private JMenuItem jMenuItemCopy = null;
77
        private JMenuItem jMenuItemPaste = null;
78
        private JMenuItem jMenuItemDelete = null;
79
        private JMenuItem jMenuItemSelectAll = null;
80
        
81
        // Values of the events fired when has been clicked an item
82
        public static final int DEFAULT = 0;
83
        public static final int UNDO = 1;
84
        public static final int REDO = 2;
85
        public static final int CUT = 3;
86
        public static final int COPY = 4;
87
        public static final int PASTE = 5;
88
        public static final int DELETE = 6;
89
        public static final int SELECT_ALL = 7;
90
        
91
        // Values of the type of event fired
92
        public static final int VISIBILITY = 8;
93
        public static final String SELECTEDOPTION = "SelectedOption";
94
        public static final String VISIBILITYCHANGED = "Visibility";
95
        
96
        // Hash map for the items
97
        private HashMap map;
98

    
99
        // Action listener for notify (fire) some events that had happened to this component
100
        private ActionListener menuListener = null;
101

    
102
        // Listener for property change support
103
        private PropertyChangeSupport changes = new PropertyChangeSupport(this);
104

    
105
         /**
106
         * Default constructor 
107
         */
108
        public JOptionsEditionByMousePopupMenu() {
109
                super();
110
                initialize();
111
        }
112

    
113
        /**
114
         * This method initializes this component 
115
         */
116
        private void initialize() {
117
        map = new HashMap();
118
        this.add(getJMenuItemUndo());
119
        this.add(getJMenuItemRedo());
120
        this.addSeparator();
121
        this.add(getJMenuItemCut());
122
        this.add(getJMenuItemCopy());
123
        this.add(getJMenuItemPaste());
124
        this.add(getJMenuItemDelete());
125
        this.addSeparator();
126
        this.add(getJMenuItemSelectAll());
127
        this.setLabel("Edition");
128
        this.setBorder(new BevelBorder(BevelBorder.RAISED));
129
        }
130
        
131
        /**
132
         * This method initializes jMenuItemUndo        
133
         *         
134
         * @return javax.swing.JMenuItem
135
         */
136
        private JMenuItem getJMenuItemUndo() {
137
                if (jMenuItemUndo == null) {
138
                        ImageIcon undoIcon = new ImageIcon(JOptionsEditionByMousePopupMenu.class.getResource("images/edit-undo.png"), Messages.getText("edit_undo"));
139
                        jMenuItemUndo = new JMenuItem(Messages.getText("edit_undo"), undoIcon);
140
                        jMenuItemUndo.setHorizontalTextPosition(JMenuItem.RIGHT);
141
                        jMenuItemUndo.addActionListener(this.getMenuListener());
142
                        map.put(Messages.getText("edit_undo"), new Integer(JOptionsEditionByMousePopupMenu.UNDO));
143
                }
144
                return jMenuItemUndo;
145
        }
146

    
147
        /**
148
         * This method initializes jMenuItemRedo        
149
         *         
150
         * @return javax.swing.JMenuItem        
151
         */
152
        private JMenuItem getJMenuItemRedo() {
153
                if (jMenuItemRedo == null) {
154
                        ImageIcon redoIcon = new ImageIcon(JOptionsEditionByMousePopupMenu.class.getResource("images/edit-redo.png"), Messages.getText("edit_redo"));
155
                        jMenuItemRedo = new JMenuItem(Messages.getText("edit_redo"), redoIcon);
156
                        jMenuItemRedo.setHorizontalTextPosition(JMenuItem.RIGHT);
157
                        jMenuItemRedo.addActionListener(this.getMenuListener());
158
                        map.put(Messages.getText("edit_redo"), new Integer(JOptionsEditionByMousePopupMenu.REDO));
159
                }
160
                return jMenuItemRedo;
161
        }
162

    
163
        /**
164
         * This method initializes jMenuItemCut        
165
         *         
166
         * @return javax.swing.JMenuItem        
167
         */
168
        private JMenuItem getJMenuItemCut() {
169
                if (jMenuItemCut == null) {
170
                        ImageIcon cutIcon = new ImageIcon(JOptionsEditionByMousePopupMenu.class.getResource("images/edit-cut.png"), Messages.getText("edit_cut"));
171
                        jMenuItemCut = new JMenuItem(Messages.getText("edit_cut"), cutIcon);
172
                        jMenuItemCut.setHorizontalTextPosition(JMenuItem.RIGHT);
173
                        jMenuItemCut.addActionListener(this.getMenuListener());
174
                        map.put(Messages.getText("edit_cut"), new Integer(JOptionsEditionByMousePopupMenu.CUT));
175
                }
176
                return jMenuItemCut;
177
        }
178
        
179
        /**
180
         * This method initializes jMenuItemCopy        
181
         *         
182
         * @return javax.swing.JMenuItem        
183
         */
184
        private JMenuItem getJMenuItemCopy() {
185
                if (jMenuItemCopy == null) {
186
                        ImageIcon copyIcon = new ImageIcon(JOptionsEditionByMousePopupMenu.class.getResource("images/edit-copy.png"), Messages.getText("edit_copy"));
187
                        jMenuItemCopy = new JMenuItem(Messages.getText("edit_copy"), copyIcon);
188
                        jMenuItemCopy.setHorizontalTextPosition(JMenuItem.RIGHT);
189
                        jMenuItemCopy.addActionListener(this.getMenuListener());
190
                        map.put(Messages.getText("edit_copy"), new Integer(JOptionsEditionByMousePopupMenu.COPY));
191
                }
192
                return jMenuItemCopy;
193
        }
194

    
195
        /**
196
         * This method initializes jMenuItemPaste        
197
         *         
198
         * @return javax.swing.JMenuItem        
199
         */
200
        private JMenuItem getJMenuItemPaste() {
201
                if (jMenuItemPaste == null) {
202
                        ImageIcon pasteIcon = new ImageIcon(JOptionsEditionByMousePopupMenu.class.getResource("images/edit-paste.png"), Messages.getText("edit_paste"));
203
                        jMenuItemPaste = new JMenuItem(Messages.getText("edit_paste"), pasteIcon);
204
                        jMenuItemPaste.setHorizontalTextPosition(JMenuItem.RIGHT);
205
                        jMenuItemPaste.addActionListener(this.getMenuListener());
206
                        map.put(Messages.getText("edit_paste"), new Integer(JOptionsEditionByMousePopupMenu.PASTE));
207
                }
208
                return jMenuItemPaste;
209
        }
210

    
211
        /**
212
         * This method initializes jMenuItemDelete        
213
         *         
214
         * @return javax.swing.JMenuItem        
215
         */
216
        private JMenuItem getJMenuItemDelete() {
217
                if (jMenuItemDelete == null) {
218
                        ImageIcon deleteIcon = new ImageIcon(JOptionsEditionByMousePopupMenu.class.getResource("images/edit-delete.png"), Messages.getText("edit_delete"));
219
                        jMenuItemDelete = new JMenuItem(Messages.getText("edit_delete"), deleteIcon);
220
                        jMenuItemDelete.setHorizontalTextPosition(JMenuItem.RIGHT);
221
                        jMenuItemDelete.addActionListener(this.getMenuListener());
222
                        map.put(Messages.getText("edit_delete"), new Integer(JOptionsEditionByMousePopupMenu.DELETE));
223
                }
224
                return jMenuItemDelete;
225
        }        
226

    
227
        /**
228
         * This method initializes jMenuItemSelectAll        
229
         *         
230
         * @return javax.swing.JMenuItem        
231
         */
232
        private JMenuItem getJMenuItemSelectAll() {
233
                if (jMenuItemSelectAll == null) {
234
                        ImageIcon selectAllIcon = new ImageIcon(JOptionsEditionByMousePopupMenu.class.getResource("images/edit-select-all.png"), Messages.getText("edit_select_all"));
235
                        jMenuItemSelectAll = new JMenuItem(Messages.getText("edit_select_all"), selectAllIcon);
236
                        jMenuItemSelectAll.setHorizontalTextPosition(JMenuItem.RIGHT);
237
                        jMenuItemSelectAll.addActionListener(this.getMenuListener());
238
                        map.put(Messages.getText("edit_select_all"), new Integer(JOptionsEditionByMousePopupMenu.SELECT_ALL));
239
                }
240
                return jMenuItemSelectAll;
241
        }
242
        
243
        /**
244
         * This method initializes the "menuListener" ActionListener
245
         * 
246
         * @return ActionListener
247
         */
248
        private ActionListener getMenuListener() {
249
                if (menuListener == null) {
250
                    menuListener = new ActionListener() {
251
                            /*
252
                             * (non-Javadoc)
253
                             * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
254
                             */
255
                        public void actionPerformed(ActionEvent event) {
256
                                // Notifies that the Visibility of this component has changed
257
                                if (event.getActionCommand().equals(JOptionsEditionByMousePopupMenu.VISIBILITYCHANGED))
258
                                           changes.firePropertyChange(JOptionsEditionByMousePopupMenu.VISIBILITYCHANGED, JOptionsEditionByMousePopupMenu.DEFAULT, JOptionsEditionByMousePopupMenu.VISIBILITY);
259
                                 else // Notifies that has been clicked on an item
260
                                           changes.firePropertyChange(JOptionsEditionByMousePopupMenu.SELECTEDOPTION, JOptionsEditionByMousePopupMenu.DEFAULT, ((Integer)map.get(event.getActionCommand())).intValue());
261
                        }
262
                    };
263
                }
264
                return menuListener;
265
        } 
266

    
267
        /**
268
         * Enables or disables the undo option item
269
         * 
270
         * @param b Value true or false
271
         */
272
        public void setEnabledUndoOption(boolean b) {
273
                jMenuItemUndo.setEnabled(b);
274
        }
275
        
276
        /**
277
         * Returns if the undo option item is enabled or not
278
         * 
279
         * @return Value true or false
280
         */
281
        public boolean isEnabledUndoOption() {
282
                return jMenuItemUndo.isEnabled();
283
        }
284
        
285
        /**
286
         * Enables or disables the redo option item
287
         * 
288
         * @param b Value true or false
289
         */
290
        public void setEnabledRedoOption(boolean b) {
291
                jMenuItemRedo.setEnabled(b);
292
        }
293
        
294
        /**
295
         * Returns if the redo option item is enabled or not
296
         * 
297
         * @return Value true or false
298
         */
299
        public boolean isEnabledRedoOption() {
300
                return jMenuItemRedo.isEnabled();
301
        }
302
        
303
        /**
304
         * Enables or disables the cut option item
305
         * 
306
         * @param b Value true or false
307
         */
308
        public void setEnabledCutOption(boolean b) {
309
                jMenuItemCut.setEnabled(b);
310
        }
311
        
312
        /**
313
         * Returns if the cut option item is enabled or not
314
         * 
315
         * @return Value true or false
316
         */
317
        public boolean isEnabledCutOption() {
318
                return jMenuItemCut.isEnabled();
319
        }
320
        
321
        /**
322
         * Enables or disables the copy option item
323
         * 
324
         * @param b Value true or false
325
         */
326
        public void setEnabledCopyOption(boolean b) {
327
                jMenuItemCopy.setEnabled(b);
328
        }
329
        
330
        /**
331
         * Returns if the copy option item is enabled or not
332
         * 
333
         * @return Value true or false
334
         */
335
        public boolean isEnabledCopyOption() {
336
                return jMenuItemCopy.isEnabled();
337
        }
338
        
339
        /**
340
         * Enables or disables the paste option item
341
         * 
342
         * @param b Value true or false
343
         */
344
        public void setEnabledPasteOption(boolean b) {
345
                jMenuItemPaste.setEnabled(b);
346
        }
347
        
348
        /**
349
         * Returns if the paste option item is enabled or not
350
         * 
351
         * @return Value true or false
352
         */
353
        public boolean isEnabledPasteOption() {
354
                return jMenuItemPaste.isEnabled();
355
        }
356
        
357
        /**
358
         * Enables or disables the delete option item
359
         * 
360
         * @param b Value true or false
361
         */
362
        public void setEnabledDeleteOption(boolean b) {
363
                jMenuItemDelete.setEnabled(b);
364
        }
365
        
366
        /**
367
         * Returns if the delete option item is enabled or not
368
         * 
369
         * @return Value true or false
370
         */
371
        public boolean isEnabledDeleteOption() {
372
                return jMenuItemDelete.isEnabled();
373
        }
374
        
375
        /**
376
         * Enables or disables the select all option item
377
         * 
378
         * @param b Value true or false
379
         */
380
        public void setEnabledSelectAllOption(boolean b) {
381
                jMenuItemSelectAll.setEnabled(b);
382
        }
383
        
384
        /**
385
         * Returns if the select all option item is enabled or not
386
         * 
387
         * @return Value true or false
388
         */
389
        public boolean isEnabledelectAllOption() {
390
                return jMenuItemSelectAll.isEnabled();
391
        }        
392

    
393
        /**
394
         * Enables or disables all option items
395
         * 
396
         * @param b Value true or false
397
         */
398
        public void setEnabledAllOptions(boolean b) {
399
                this.setEnabledUndoOption(b);
400
                this.setEnabledRedoOption(b);
401
                this.setEnabledCutOption(b);
402
                this.setEnabledCopyOption(b);
403
                this.setEnabledPasteOption(b);
404
                this.setEnabledDeleteOption(b);
405
                this.setEnabledSelectAllOption(b);
406
        }        
407
        
408
        /* 
409
         * (non-Javadoc)
410
         * @see javax.swing.JPopupMenu#setVisible(boolean)
411
         */
412
        public void setVisible(boolean b) {
413
                // Notifies that has changed the visibility of this component
414
                super.setVisible(b);
415
                this.getMenuListener().actionPerformed(new ActionEvent(this, JOptionsEditionByMousePopupMenu.VISIBILITY, JOptionsEditionByMousePopupMenu.VISIBILITYCHANGED));
416
        }
417
           
418
    /**
419
     * Adds a "Property Change Listener"
420
     */
421
    public void addPropertyChangeListener(PropertyChangeListener l) {
422
            changes.addPropertyChangeListener(l);
423
    }
424

    
425
    /**
426
     * Removes a "Property Change Listener"
427
     */
428
    public void removePropertyChangeListener(PropertyChangeListener l) {
429
            changes.removePropertyChangeListener(l);
430
    }    
431
}