root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / editionpopupmenu / JOptionsEditionByMousePopupMenu.java @ 17812
History | View | Annotate | Download (13.1 KB)
1 |
package org.gvsig.gui.beans.editionpopupmenu; |
---|---|
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 |
import javax.swing.ImageIcon; |
11 |
import javax.swing.JMenuItem; |
12 |
import javax.swing.JPopupMenu; |
13 |
import javax.swing.border.BevelBorder; |
14 |
|
15 |
import org.gvsig.gui.beans.Messages; |
16 |
|
17 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
18 |
*
|
19 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
20 |
*
|
21 |
* This program is free software; you can redistribute it and/or
|
22 |
* modify it under the terms of the GNU General Public License
|
23 |
* as published by the Free Software Foundation; either version 2
|
24 |
* of the License, or (at your option) any later version.
|
25 |
*
|
26 |
* This program is distributed in the hope that it will be useful,
|
27 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
28 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
29 |
* GNU General Public License for more details.
|
30 |
*
|
31 |
* You should have received a copy of the GNU General Public License
|
32 |
* along with this program; if not, write to the Free Software
|
33 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
34 |
*
|
35 |
* For more information, contact:
|
36 |
*
|
37 |
* Generalitat Valenciana
|
38 |
* Conselleria d'Infraestructures i Transport
|
39 |
* Av. Blasco Ib??ez, 50
|
40 |
* 46010 VALENCIA
|
41 |
* SPAIN
|
42 |
*
|
43 |
* +34 963862235
|
44 |
* gvsig@gva.es
|
45 |
* www.gvsig.gva.es
|
46 |
*
|
47 |
* or
|
48 |
*
|
49 |
* IVER T.I. S.A
|
50 |
* Salamanca 50
|
51 |
* 46005 Valencia
|
52 |
* Spain
|
53 |
*
|
54 |
* +34 963163400
|
55 |
* dac@iver.es
|
56 |
*/
|
57 |
|
58 |
|
59 |
/**
|
60 |
* <p>This class is a JPopupMenu that can be used with another component.</p>
|
61 |
* <p>The items showed in this menu are for editing: UNDO, REDO, CUT, COPY, PASTE, DELETE and SELECT ALL.</p>
|
62 |
* <p>When the user clicks on a item, this Component fires a property change event that its value allows identify the item clicked.</p>
|
63 |
* <p>(The icons used are from the open-source 'Tango Icon Library' project: (http://tango.freedesktop.org/Tango_Icon_Library)).</p>
|
64 |
*
|
65 |
* @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
66 |
*/
|
67 |
public class JOptionsEditionByMousePopupMenu extends JPopupMenu implements Serializable{ |
68 |
private static final long serialVersionUID = -142360699557710223L; |
69 |
|
70 |
// Items of this JPopupMenu
|
71 |
private JMenuItem jMenuItemUndo = null; |
72 |
private JMenuItem jMenuItemRedo = null; |
73 |
private JMenuItem jMenuItemCut = null; |
74 |
private JMenuItem jMenuItemCopy = null; |
75 |
private JMenuItem jMenuItemPaste = null; |
76 |
private JMenuItem jMenuItemDelete = null; |
77 |
private JMenuItem jMenuItemSelectAll = null; |
78 |
|
79 |
// Values of the events fired when has been clicked an item
|
80 |
public static final int DEFAULT = 0; |
81 |
public static final int UNDO = 1; |
82 |
public static final int REDO = 2; |
83 |
public static final int CUT = 3; |
84 |
public static final int COPY = 4; |
85 |
public static final int PASTE = 5; |
86 |
public static final int DELETE = 6; |
87 |
public static final int SELECT_ALL = 7; |
88 |
|
89 |
// Values of the type of event fired
|
90 |
public static final int VISIBILITY = 8; |
91 |
public static final String SELECTEDOPTION = "SelectedOption"; |
92 |
public static final String VISIBILITYCHANGED = "Visibility"; |
93 |
|
94 |
// Hash map for the items
|
95 |
private HashMap<String, String> map; |
96 |
|
97 |
// Action listener for notify (fire) some events that had happened to this component
|
98 |
private ActionListener menuListener = null; |
99 |
|
100 |
// Listener for property change support
|
101 |
private PropertyChangeSupport changes = new PropertyChangeSupport(this); |
102 |
|
103 |
/**
|
104 |
* Default constructor
|
105 |
*/
|
106 |
public JOptionsEditionByMousePopupMenu() {
|
107 |
super();
|
108 |
initialize(); |
109 |
} |
110 |
|
111 |
/**
|
112 |
* This method initializes this component
|
113 |
*/
|
114 |
private void initialize() { |
115 |
map = new HashMap<String, String>(); |
116 |
this.add(getJMenuItemUndo());
|
117 |
this.add(getJMenuItemRedo());
|
118 |
this.addSeparator();
|
119 |
this.add(getJMenuItemCut());
|
120 |
this.add(getJMenuItemCopy());
|
121 |
this.add(getJMenuItemPaste());
|
122 |
this.add(getJMenuItemDelete());
|
123 |
this.addSeparator();
|
124 |
this.add(getJMenuItemSelectAll());
|
125 |
this.setLabel("Edition"); |
126 |
this.setBorder(new BevelBorder(BevelBorder.RAISED)); |
127 |
} |
128 |
|
129 |
/**
|
130 |
* This method initializes jMenuItemUndo
|
131 |
*
|
132 |
* @return javax.swing.JMenuItem
|
133 |
*/
|
134 |
private JMenuItem getJMenuItemUndo() { |
135 |
if (jMenuItemUndo == null) { |
136 |
ImageIcon undoIcon = new ImageIcon(JOptionsEditionByMousePopupMenu.class.getResource("images/edit-undo.png"), Messages.getText("edit_undo")); |
137 |
jMenuItemUndo = new JMenuItem(Messages.getText("edit_undo"), undoIcon); |
138 |
jMenuItemUndo.setHorizontalTextPosition(JMenuItem.RIGHT);
|
139 |
jMenuItemUndo.addActionListener(this.getMenuListener());
|
140 |
map.put(Messages.getText("edit_undo"), Integer.toString(JOptionsEditionByMousePopupMenu.UNDO)); |
141 |
} |
142 |
return jMenuItemUndo;
|
143 |
} |
144 |
|
145 |
/**
|
146 |
* This method initializes jMenuItemRedo
|
147 |
*
|
148 |
* @return javax.swing.JMenuItem
|
149 |
*/
|
150 |
private JMenuItem getJMenuItemRedo() { |
151 |
if (jMenuItemRedo == null) { |
152 |
ImageIcon redoIcon = new ImageIcon(JOptionsEditionByMousePopupMenu.class.getResource("images/edit-redo.png"), Messages.getText("edit_redo")); |
153 |
jMenuItemRedo = new JMenuItem(Messages.getText("edit_redo"), redoIcon); |
154 |
jMenuItemRedo.setHorizontalTextPosition(JMenuItem.RIGHT);
|
155 |
jMenuItemRedo.addActionListener(this.getMenuListener());
|
156 |
map.put(Messages.getText("edit_redo"), Integer.toString(JOptionsEditionByMousePopupMenu.REDO)); |
157 |
} |
158 |
return jMenuItemRedo;
|
159 |
} |
160 |
|
161 |
/**
|
162 |
* This method initializes jMenuItemCut
|
163 |
*
|
164 |
* @return javax.swing.JMenuItem
|
165 |
*/
|
166 |
private JMenuItem getJMenuItemCut() { |
167 |
if (jMenuItemCut == null) { |
168 |
ImageIcon cutIcon = new ImageIcon(JOptionsEditionByMousePopupMenu.class.getResource("images/edit-cut.png"), Messages.getText("edit_cut")); |
169 |
jMenuItemCut = new JMenuItem(Messages.getText("edit_cut"), cutIcon); |
170 |
jMenuItemCut.setHorizontalTextPosition(JMenuItem.RIGHT);
|
171 |
jMenuItemCut.addActionListener(this.getMenuListener());
|
172 |
map.put(Messages.getText("edit_cut"), Integer.toString(JOptionsEditionByMousePopupMenu.CUT)); |
173 |
} |
174 |
return jMenuItemCut;
|
175 |
} |
176 |
|
177 |
/**
|
178 |
* This method initializes jMenuItemCopy
|
179 |
*
|
180 |
* @return javax.swing.JMenuItem
|
181 |
*/
|
182 |
private JMenuItem getJMenuItemCopy() { |
183 |
if (jMenuItemCopy == null) { |
184 |
ImageIcon copyIcon = new ImageIcon(JOptionsEditionByMousePopupMenu.class.getResource("images/edit-copy.png"), Messages.getText("edit_copy")); |
185 |
jMenuItemCopy = new JMenuItem(Messages.getText("edit_copy"), copyIcon); |
186 |
jMenuItemCopy.setHorizontalTextPosition(JMenuItem.RIGHT);
|
187 |
jMenuItemCopy.addActionListener(this.getMenuListener());
|
188 |
map.put(Messages.getText("edit_copy"), Integer.toString(JOptionsEditionByMousePopupMenu.COPY)); |
189 |
} |
190 |
return jMenuItemCopy;
|
191 |
} |
192 |
|
193 |
/**
|
194 |
* This method initializes jMenuItemPaste
|
195 |
*
|
196 |
* @return javax.swing.JMenuItem
|
197 |
*/
|
198 |
private JMenuItem getJMenuItemPaste() { |
199 |
if (jMenuItemPaste == null) { |
200 |
ImageIcon pasteIcon = new ImageIcon(JOptionsEditionByMousePopupMenu.class.getResource("images/edit-paste.png"), Messages.getText("edit_paste")); |
201 |
jMenuItemPaste = new JMenuItem(Messages.getText("edit_paste"), pasteIcon); |
202 |
jMenuItemPaste.setHorizontalTextPosition(JMenuItem.RIGHT);
|
203 |
jMenuItemPaste.addActionListener(this.getMenuListener());
|
204 |
map.put(Messages.getText("edit_paste"), Integer.toString(JOptionsEditionByMousePopupMenu.PASTE)); |
205 |
} |
206 |
return jMenuItemPaste;
|
207 |
} |
208 |
|
209 |
/**
|
210 |
* This method initializes jMenuItemDelete
|
211 |
*
|
212 |
* @return javax.swing.JMenuItem
|
213 |
*/
|
214 |
private JMenuItem getJMenuItemDelete() { |
215 |
if (jMenuItemDelete == null) { |
216 |
ImageIcon deleteIcon = new ImageIcon(JOptionsEditionByMousePopupMenu.class.getResource("images/edit-delete.png"), Messages.getText("edit_delete")); |
217 |
jMenuItemDelete = new JMenuItem(Messages.getText("edit_delete"), deleteIcon); |
218 |
jMenuItemDelete.setHorizontalTextPosition(JMenuItem.RIGHT);
|
219 |
jMenuItemDelete.addActionListener(this.getMenuListener());
|
220 |
map.put(Messages.getText("edit_delete"), Integer.toString(JOptionsEditionByMousePopupMenu.DELETE)); |
221 |
} |
222 |
return jMenuItemDelete;
|
223 |
} |
224 |
|
225 |
/**
|
226 |
* This method initializes jMenuItemSelectAll
|
227 |
*
|
228 |
* @return javax.swing.JMenuItem
|
229 |
*/
|
230 |
private JMenuItem getJMenuItemSelectAll() { |
231 |
if (jMenuItemSelectAll == null) { |
232 |
ImageIcon selectAllIcon = new ImageIcon(JOptionsEditionByMousePopupMenu.class.getResource("images/edit-select-all.png"), Messages.getText("edit_select_all")); |
233 |
jMenuItemSelectAll = new JMenuItem(Messages.getText("edit_select_all"), selectAllIcon); |
234 |
jMenuItemSelectAll.setHorizontalTextPosition(JMenuItem.RIGHT);
|
235 |
jMenuItemSelectAll.addActionListener(this.getMenuListener());
|
236 |
map.put(Messages.getText("edit_select_all"), Integer.toString(JOptionsEditionByMousePopupMenu.SELECT_ALL)); |
237 |
} |
238 |
return jMenuItemSelectAll;
|
239 |
} |
240 |
|
241 |
/**
|
242 |
* This method initializes the "menuListener" ActionListener
|
243 |
*
|
244 |
* @return ActionListener
|
245 |
*/
|
246 |
private ActionListener getMenuListener() { |
247 |
if (menuListener == null) { |
248 |
menuListener = new ActionListener() { |
249 |
/*
|
250 |
* (non-Javadoc)
|
251 |
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
|
252 |
*/
|
253 |
public void actionPerformed(ActionEvent event) { |
254 |
// Notifies that the Visibility of this component has changed
|
255 |
if (event.getActionCommand().equals(JOptionsEditionByMousePopupMenu.VISIBILITYCHANGED))
|
256 |
changes.firePropertyChange(JOptionsEditionByMousePopupMenu.VISIBILITYCHANGED, JOptionsEditionByMousePopupMenu.DEFAULT, JOptionsEditionByMousePopupMenu.VISIBILITY); |
257 |
else // Notifies that has been clicked on an item |
258 |
changes.firePropertyChange(JOptionsEditionByMousePopupMenu.SELECTEDOPTION, JOptionsEditionByMousePopupMenu.DEFAULT, map.get(event.getActionCommand())); |
259 |
} |
260 |
}; |
261 |
} |
262 |
return menuListener;
|
263 |
} |
264 |
|
265 |
/**
|
266 |
* Enables or disables the undo option item
|
267 |
*
|
268 |
* @param b Value true or false
|
269 |
*/
|
270 |
public void setEnabledUndoOption(boolean b) { |
271 |
jMenuItemUndo.setEnabled(b); |
272 |
} |
273 |
|
274 |
/**
|
275 |
* Returns if the undo option item is enabled or not
|
276 |
*
|
277 |
* @return Value true or false
|
278 |
*/
|
279 |
public boolean isEnabledUndoOption() { |
280 |
return jMenuItemUndo.isEnabled();
|
281 |
} |
282 |
|
283 |
/**
|
284 |
* Enables or disables the redo option item
|
285 |
*
|
286 |
* @param b Value true or false
|
287 |
*/
|
288 |
public void setEnabledRedoOption(boolean b) { |
289 |
jMenuItemRedo.setEnabled(b); |
290 |
} |
291 |
|
292 |
/**
|
293 |
* Returns if the redo option item is enabled or not
|
294 |
*
|
295 |
* @return Value true or false
|
296 |
*/
|
297 |
public boolean isEnabledRedoOption() { |
298 |
return jMenuItemRedo.isEnabled();
|
299 |
} |
300 |
|
301 |
/**
|
302 |
* Enables or disables the cut option item
|
303 |
*
|
304 |
* @param b Value true or false
|
305 |
*/
|
306 |
public void setEnabledCutOption(boolean b) { |
307 |
jMenuItemCut.setEnabled(b); |
308 |
} |
309 |
|
310 |
/**
|
311 |
* Returns if the cut option item is enabled or not
|
312 |
*
|
313 |
* @return Value true or false
|
314 |
*/
|
315 |
public boolean isEnabledCutOption() { |
316 |
return jMenuItemCut.isEnabled();
|
317 |
} |
318 |
|
319 |
/**
|
320 |
* Enables or disables the copy option item
|
321 |
*
|
322 |
* @param b Value true or false
|
323 |
*/
|
324 |
public void setEnabledCopyOption(boolean b) { |
325 |
jMenuItemCopy.setEnabled(b); |
326 |
} |
327 |
|
328 |
/**
|
329 |
* Returns if the copy option item is enabled or not
|
330 |
*
|
331 |
* @return Value true or false
|
332 |
*/
|
333 |
public boolean isEnabledCopyOption() { |
334 |
return jMenuItemCopy.isEnabled();
|
335 |
} |
336 |
|
337 |
/**
|
338 |
* Enables or disables the paste option item
|
339 |
*
|
340 |
* @param b Value true or false
|
341 |
*/
|
342 |
public void setEnabledPasteOption(boolean b) { |
343 |
jMenuItemPaste.setEnabled(b); |
344 |
} |
345 |
|
346 |
/**
|
347 |
* Returns if the paste option item is enabled or not
|
348 |
*
|
349 |
* @return Value true or false
|
350 |
*/
|
351 |
public boolean isEnabledPasteOption() { |
352 |
return jMenuItemPaste.isEnabled();
|
353 |
} |
354 |
|
355 |
/**
|
356 |
* Enables or disables the delete option item
|
357 |
*
|
358 |
* @param b Value true or false
|
359 |
*/
|
360 |
public void setEnabledDeleteOption(boolean b) { |
361 |
jMenuItemDelete.setEnabled(b); |
362 |
} |
363 |
|
364 |
/**
|
365 |
* Returns if the delete option item is enabled or not
|
366 |
*
|
367 |
* @return Value true or false
|
368 |
*/
|
369 |
public boolean isEnabledDeleteOption() { |
370 |
return jMenuItemDelete.isEnabled();
|
371 |
} |
372 |
|
373 |
/**
|
374 |
* Enables or disables the select all option item
|
375 |
*
|
376 |
* @param b Value true or false
|
377 |
*/
|
378 |
public void setEnabledSelectAllOption(boolean b) { |
379 |
jMenuItemSelectAll.setEnabled(b); |
380 |
} |
381 |
|
382 |
/**
|
383 |
* Returns if the select all option item is enabled or not
|
384 |
*
|
385 |
* @return Value true or false
|
386 |
*/
|
387 |
public boolean isEnabledelectAllOption() { |
388 |
return jMenuItemSelectAll.isEnabled();
|
389 |
} |
390 |
|
391 |
/**
|
392 |
* Enables or disables all option items
|
393 |
*
|
394 |
* @param b Value true or false
|
395 |
*/
|
396 |
public void setEnabledAllOptions(boolean b) { |
397 |
this.setEnabledUndoOption(b);
|
398 |
this.setEnabledRedoOption(b);
|
399 |
this.setEnabledCutOption(b);
|
400 |
this.setEnabledCopyOption(b);
|
401 |
this.setEnabledPasteOption(b);
|
402 |
this.setEnabledDeleteOption(b);
|
403 |
this.setEnabledSelectAllOption(b);
|
404 |
} |
405 |
|
406 |
/*
|
407 |
* (non-Javadoc)
|
408 |
* @see javax.swing.JPopupMenu#setVisible(boolean)
|
409 |
*/
|
410 |
public void setVisible(boolean b) { |
411 |
// Notifies that has changed the visibility of this component
|
412 |
super.setVisible(b);
|
413 |
this.getMenuListener().actionPerformed(new ActionEvent(this, JOptionsEditionByMousePopupMenu.VISIBILITY, JOptionsEditionByMousePopupMenu.VISIBILITYCHANGED)); |
414 |
} |
415 |
|
416 |
/**
|
417 |
* Adds a "Property Change Listener"
|
418 |
*/
|
419 |
public void addPropertyChangeListener(PropertyChangeListener l) { |
420 |
changes.addPropertyChangeListener(l); |
421 |
} |
422 |
|
423 |
/**
|
424 |
* Removes a "Property Change Listener"
|
425 |
*/
|
426 |
public void removePropertyChangeListener(PropertyChangeListener l) { |
427 |
changes.removePropertyChangeListener(l); |
428 |
} |
429 |
} |