Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libUIComponent / src / org / gvsig / gui / beans / editionpopupmenu / JOptionsEditionByMousePopupMenu.java @ 34247

History | View | Annotate | Download (12.6 KB)

1
package org.gvsig.gui.beans.editionpopupmenu;
2

    
3
import java.awt.event.ActionEvent;
4
import java.awt.event.ActionListener;
5
import java.io.Serializable;
6
import java.util.HashMap;
7

    
8
import javax.swing.ImageIcon;
9
import javax.swing.JMenuItem;
10
import javax.swing.JPopupMenu;
11
import javax.swing.border.BevelBorder;
12

    
13
import org.gvsig.gui.beans.Messages;
14

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

    
56

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

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

    
95
        // Action listener for notify (fire) some events that had happened to this component
96
        private ActionListener menuListener = null;
97

    
98
         /**
99
         * Default constructor 
100
         */
101
        public JOptionsEditionByMousePopupMenu() {
102
                super();
103
                initialize();
104
        }
105

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

    
140
        /**
141
         * This method initializes jMenuItemRedo        
142
         *         
143
         * @return javax.swing.JMenuItem        
144
         */
145
        private JMenuItem getJMenuItemRedo() {
146
                if (jMenuItemRedo == null) {
147
                        ImageIcon redoIcon = new ImageIcon(JOptionsEditionByMousePopupMenu.class.getResource("images/edit-redo.png"), Messages.getText("edit_redo"));
148
                        jMenuItemRedo = new JMenuItem(Messages.getText("edit_redo"), redoIcon);
149
                        jMenuItemRedo.setHorizontalTextPosition(JMenuItem.RIGHT);
150
                        jMenuItemRedo.addActionListener(this.getMenuListener());
151
                        map.put(Messages.getText("edit_redo"), Integer.toString(JOptionsEditionByMousePopupMenu.REDO));
152
                }
153
                return jMenuItemRedo;
154
        }
155

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

    
188
        /**
189
         * This method initializes jMenuItemPaste        
190
         *         
191
         * @return javax.swing.JMenuItem        
192
         */
193
        private JMenuItem getJMenuItemPaste() {
194
                if (jMenuItemPaste == null) {
195
                        ImageIcon pasteIcon = new ImageIcon(JOptionsEditionByMousePopupMenu.class.getResource("images/edit-paste.png"), Messages.getText("edit_paste"));
196
                        jMenuItemPaste = new JMenuItem(Messages.getText("edit_paste"), pasteIcon);
197
                        jMenuItemPaste.setHorizontalTextPosition(JMenuItem.RIGHT);
198
                        jMenuItemPaste.addActionListener(this.getMenuListener());
199
                        map.put(Messages.getText("edit_paste"), Integer.toString(JOptionsEditionByMousePopupMenu.PASTE));
200
                }
201
                return jMenuItemPaste;
202
        }
203

    
204
        /**
205
         * This method initializes jMenuItemDelete        
206
         *         
207
         * @return javax.swing.JMenuItem        
208
         */
209
        private JMenuItem getJMenuItemDelete() {
210
                if (jMenuItemDelete == null) {
211
                        ImageIcon deleteIcon = new ImageIcon(JOptionsEditionByMousePopupMenu.class.getResource("images/edit-delete.png"), Messages.getText("edit_delete"));
212
                        jMenuItemDelete = new JMenuItem(Messages.getText("edit_delete"), deleteIcon);
213
                        jMenuItemDelete.setHorizontalTextPosition(JMenuItem.RIGHT);
214
                        jMenuItemDelete.addActionListener(this.getMenuListener());
215
                        map.put(Messages.getText("edit_delete"), Integer.toString(JOptionsEditionByMousePopupMenu.DELETE));
216
                }
217
                return jMenuItemDelete;
218
        }        
219

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

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

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