Menu

[01c4ec]: / VisibleWorld.java  Maximize  Restore  History

Download this file

697 lines (656 with data), 23.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/* Copyright (C) 2006-2010 Joan Queralt Molina
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
package biogenesis;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.*;
import org.xml.sax.SAXException;
import java.awt.image.*;
/**
* This class is associated with a {@link World} and represents its visible
* part: it's the place where organisms are drawn and is in charge of
* the context menus management.
*/
public class VisibleWorld extends JPanel {
/**
* The version of this class
*/
private static final long serialVersionUID = Utils.FILE_VERSION;
/**
* A reference to the {@link MainWindow} where the VisibleWorld is.
*/
protected MainWindow _mainWindow;
/**
* The context menu showed when right clicking on an alive organism.
*/
protected JPopupMenu popupAlive;
/**
* Menu option in {@link popupAlive} used to keep an organism on the center of the view.
*/
protected TrackAction trackAction;
protected StdAction feedAction;
protected StdAction weakenAction;
protected StdAction killAction;
protected StdAction copyAction;
protected StdAction saveImageAction;
protected StdAction reviveAction;
protected StdAction disperseAction;
/**
* Menu option in {@link popupAlive} used to give extra energy to an organism.
*/
/**
* Menu option in {@link popupAlive} used to force the reproduction of the organism,
* even if it dies.
*/
protected StdAction reproduceAction;
/**
* Menu option in {@link popupAlive} used to kill an organism.
*/
/**
* Menu option in {@link popupAlive} used to set the age counter of the organism back to 0.
*/
protected StdAction rejuvenateAction;
/**
* Menu option in {@link popupAlive} used to remove energy from an organism.
*/
/**
* Menu option in {@link popupAlive} used to copy an organism genetic code
* to use it to create new organisms or to edit it in the genetic lab.
*/
/**
* Menu option in {@link popupAlive} used to save the genetic code of
* an organism to a file.
*/
protected StdAction exportAction;
/**
* Menu option in {@link popupAlive} used to save an image of an organism
* to a jpg file.
*/
/**
* The context menu showed when right clicking on a dead organism.
*/
protected JPopupMenu popupDead;
/**
* Menu option in {@link popupDead} used to give live to a dead organism.
*/
/**
* Menu option in {@link popupDead} used to return all its carbony back to
* the atmosphere, in form of CO2, and make the corpse disappear.
*/
/**
* The context menu showed when right clicking on a void plave in the world.
*/
protected JPopupMenu popupVoid;
/**
* Menu option in {@link popupVoid} used to create a new organism with the
* genetic code previously copies with the Copy option.
*/
protected StdAction pasteAction;
/**
* Menu option in {@link popupVoid} used to create a new organism randomly.
*/
protected StdAction randomCreateAction;
/**
* Menu option in {@link popupVoid} used to create a new organism with a
* genetic code obtained from a file.
*/
protected StdAction importAction;
/**
* This is the selected organism. It is drawn with an orange bounding rectangle
* and, if there is an {@link InfoWindow}, it shows information about this organism.
*/
protected Organism _selectedOrganism = null;
/**
* This is the last genetic code obtained using a Copy option. It is used when
* pasting new organisms or in the genetic lab.
*/
protected GeneticCode clippedGeneticCode = null;
/**
* X coordinate of the mouse pointer when the user clicks or right clicks on the
* visible world.
*/
protected int mouseX;
/**
* Y coordinate of the mouse pointer when the user clicks or right clicks on the
* visible world.
*/
protected int mouseY;
/**
* A reference to the {@link InfoWindow}, that is created from this class.
*/
//transient protected InfoWindow _infoWindow = null;
class TrackAction extends StdAction {
private static final long serialVersionUID = 1L;
protected String name_key2;
protected String desc_key2;
public TrackAction(String text_key, String text_key2, String icon_path, String desc, String desc2) {
super(text_key, icon_path, desc);
name_key2 = text_key2;
desc_key2 = desc2;
}
public void actionPerformed(ActionEvent e) {
Organism b = getSelectedOrganism();
if (b != null && b.isAlive()) {
if (_mainWindow._trackedOrganism == b)
_mainWindow.setTrackedOrganism(null);
else
_mainWindow.setTrackedOrganism(b);
}
}
public void setTracking(boolean isTracking) {
if (isTracking) {
putValue(NAME, Messages.getString(name_key2));
putValue(SHORT_DESCRIPTION, Messages.getString(desc_key2));
putValue(MNEMONIC_KEY, Messages.getMnemonic(name_key2));
} else {
putValue(NAME, Messages.getString(name_key));
putValue(SHORT_DESCRIPTION, Messages.getString(desc_key));
putValue(MNEMONIC_KEY, Messages.getMnemonic(name_key));
}
}
}
class FeedAction extends StdAction {
private static final long serialVersionUID = 1L;
public FeedAction(String text_key, String icon_path, String desc) {
super(text_key, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
Organism b = getSelectedOrganism();
if (b != null && b.isAlive()) {
double q = Math.min(10, _mainWindow.getWorld().getCO2());
_mainWindow.getWorld().addCO2(q);
b._energy += q;
_mainWindow.getWorld().addO2(q);
}
}
}
class WeakenAction extends StdAction {
private static final long serialVersionUID = 1L;
public WeakenAction(String text_key, String icon_path, String desc) {
super(text_key, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
Organism b = getSelectedOrganism();
if (b != null && b.isAlive()) {
b.useEnergy(b.getEnergy()/2);
}
}
}
class KillAction extends StdAction {
private static final long serialVersionUID = 1L;
public KillAction(String text_key, String icon_path, String desc) {
super(text_key, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
Organism b = getSelectedOrganism();
if (b != null && b.isAlive()) {
b.die(null);
}
}
}
class CopyAction extends StdAction {
private static final long serialVersionUID = 1L;
public CopyAction(String text_key, String icon_path, String desc) {
super(text_key, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
Organism b = getSelectedOrganism();
if (b != null && b.isAlive()) {
clippedGeneticCode = b.getGeneticCode();
}
}
}
class SaveImageAction extends StdAction {
private static final long serialVersionUID = 1L;
public SaveImageAction(String text_key, String icon_path, String desc) {
super(text_key, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
Organism b = getSelectedOrganism();
if (b != null && b.isAlive()) {
boolean processState = _mainWindow._isProcessActive;
// Stop time while asking for a file name
_mainWindow._isProcessActive = false;
// Get the image to save
BufferedImage image = b.getImage();
try {
// Ask for file name
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new BioFileFilter("png")); //$NON-NLS-1$
int returnVal = chooser.showSaveDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
int canWrite = JOptionPane.YES_OPTION;
File f = chooser.getSelectedFile();
// Check if file already exists and ask for confirmation
if (f.exists()) {
canWrite = JOptionPane.showConfirmDialog(null,Messages.getString("T_CONFIRM_FILE_OVERRIDE"), //$NON-NLS-1$
Messages.getString("T_FILE_EXISTS"),JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$
}
if (canWrite == JOptionPane.YES_OPTION) {
// Write image to file
try {
ImageIO.write(image,"PNG",f); //$NON-NLS-1$
} catch (FileNotFoundException ex) {
System.err.println(ex.getMessage());
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
}
}
} catch (SecurityException ex) {
System.err.println(ex.getMessage());
JOptionPane.showMessageDialog(null,Messages.getString("T_PERMISSION_DENIED"),Messages.getString("T_PERMISSION_DENIED"),JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$
}
_mainWindow._isProcessActive = processState;
}
}
}
class ReviveAction extends StdAction {
private static final long serialVersionUID = 1L;
public ReviveAction(String text_key, String icon_path, String desc) {
super(text_key, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
Organism b = getSelectedOrganism();
if (b != null && !b.isAlive()) {
for (int i = 0; i < b._segments; i++)
b._segColor[i] = b.getGeneticCode().getGene(i%b.getGeneticCode().getNGenes()).getColor();
b.alive = true;
b.hasMoved = true;
b._age = 0;
_mainWindow.getWorld().increasePopulation();
showAliveToolbar();
}
}
}
class DisperseAction extends StdAction {
private static final long serialVersionUID = 1L;
public DisperseAction(String text_key, String icon_path, String desc) {
super(text_key, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
Organism b = getSelectedOrganism();
if (b != null && !b.isAlive()) {
b.useEnergy(b.getEnergy());
}
}
}
class ReproduceAction extends StdAction {
private static final long serialVersionUID = 1L;
public ReproduceAction(String text_key, String icon_path, String desc) {
super(text_key, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
Organism b = getSelectedOrganism();
if (b != null && b.isAlive()) {
b.reproduce();
}
}
}
class RejuvenateAction extends StdAction {
private static final long serialVersionUID = 1L;
public RejuvenateAction(String text_key, String icon_path, String desc) {
super(text_key, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
Organism b = getSelectedOrganism();
if (b != null && b.isAlive()) {
b._age = 0;
}
}
}
class ExportAction extends StdAction {
private static final long serialVersionUID = 1L;
public ExportAction(String text_key, String icon_path, String desc) {
super(text_key, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
Organism b = getSelectedOrganism();
if (b != null && b.isAlive()) {
_mainWindow.saveObjectAs(b.getGeneticCode());
}
}
}
class PasteAction extends StdAction {
private static final long serialVersionUID = 1L;
public PasteAction(String text_key, String icon_path, String desc) {
super(text_key, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
if (clippedGeneticCode != null) {
pasteGeneticCode(clippedGeneticCode, mouseX, mouseY);
}
}
}
class RandomCreateAction extends StdAction {
private static final long serialVersionUID = 1L;
public RandomCreateAction(String text_key, String icon_path, String desc) {
super(text_key, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
GeneticCode g = new GeneticCode();
Organism newBiot = new Organism(_mainWindow.getWorld(), g);
if (newBiot.pasteOrganism(mouseX, mouseY))
_mainWindow.getWorld().addOrganism(newBiot, null);
}
}
class ImportAction extends StdAction {
private static final long serialVersionUID = 1L;
public ImportAction(String text_key, String icon_path, String desc) {
super(text_key, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
GeneticCode g;
Organism newBiot;
boolean processState = _mainWindow._isProcessActive;
// Stop time
_mainWindow._isProcessActive = false;
try {
JFileChooser chooser = _mainWindow.getGeneticCodeChooser();
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
// Read XML code from file
BioXMLParser parser = new BioXMLParser();
g = parser.parseGeneticCode(chooser.getSelectedFile());
// Create organism
newBiot = new Organism(_mainWindow.getWorld(), g);
if (newBiot.pasteOrganism(mouseX, mouseY))
_mainWindow.getWorld().addOrganism(newBiot, null);
} catch (SAXException ex) {
System.err.println(ex.getMessage());
JOptionPane.showMessageDialog(null,Messages.getString("T_WRONG_FILE_VERSION"),Messages.getString("T_READ_ERROR"),JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$
} catch (IOException ex) {
System.err.println(ex.getMessage());
JOptionPane.showMessageDialog(null,Messages.getString("T_CANT_READ_FILE"),Messages.getString("T_READ_ERROR"),JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$
}
}
} catch (SecurityException ex) {
System.err.println(ex.getMessage());
JOptionPane.showMessageDialog(null,Messages.getString("T_PERMISSION_DENIED"),Messages.getString("T_PERMISSION_DENIED"),JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$
}
_mainWindow._isProcessActive = processState;
}
}
/**
* Sets an organism as the selected organism. If required, it creates an
* {@link InfoWindow} with information of this organism.
*
* @param b The new selected organism
* @param showInfo true if an InfoWindow should be created
*/
public void setSelectedOrganism(Organism b) {
Organism lastSelectedOrganism = _selectedOrganism;
_selectedOrganism = b;
if (lastSelectedOrganism != null)
repaint(lastSelectedOrganism);
_mainWindow.getInfoPanel().setSelectedOrganism(b);
if (_selectedOrganism != null)
repaint(_selectedOrganism);
// Make sure to don't create the tool bar twice when starting the program
// because this causes spurious exceptions.
if (_selectedOrganism != lastSelectedOrganism) {
if (_selectedOrganism != null) {
if (_selectedOrganism.isAlive())
showAliveToolbar();
else
showDeadToolbar();
}
else
_mainWindow.createToolBar();
}
}
/**
* Return the selected organism.
*
* @return The selected organism, if any.
*/
public Organism getSelectedOrganism() {
return _selectedOrganism;
}
/**
* Set a genetic code as the clipped genetic code, that will be used when
* pasting a new organism or in the genetic lab as the staring genetic code.
*
* @param gc The clipped genetic code
*/
public void setClippedGeneticCode(GeneticCode gc) {
if (gc != null)
clippedGeneticCode = gc;
}
/**
* Creates a new VisibleWorld associated with a {@link MainWindow}.
* Creates the menus and the MouseAdapter.
*
* @param mainWindow The MainWindow associated with this VisibleWorld.
*/
public VisibleWorld(MainWindow mainWindow) {
_mainWindow = mainWindow;
setPreferredSize(new Dimension(Utils.WORLD_WIDTH,Utils.WORLD_HEIGHT));
setBackground(Color.BLACK);
createActions();
createPopupMenu();
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
setSelectedOrganism(findOrganismFromPosition(e.getX(),e.getY()));
}
}
@Override
public void mousePressed(MouseEvent e) {
maybeShowPopupMenu(e);
}
@Override
public void mouseReleased(MouseEvent e) {
maybeShowPopupMenu(e);
}
});
}
/**
* Finds an organism that has the given coordinates inside its bounding box and
* returns a reference to it. If more than on organism satisfies this condition,
* if possible, an alive organism is returned. If non organism satisfies this
* condition, this method returns null.
*
* @param x X coordinate
* @param y Y coordinate
* @return An organism with the point (x,y) inside its bounding box, or null
* if such organism doesn't exist.
*/
Organism findOrganismFromPosition(int x, int y) {
return _mainWindow.getWorld().findOrganismFromPosition(x, y);
}
/**
* Calls World.draw to draw all world elements and paints the bounding rectangle
* of the selected organism.
*
* @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
*/
@Override
public void paintComponent (Graphics g) {
super.paintComponent(g);
_mainWindow.getWorld().draw(g);
if (getSelectedOrganism() != null) {
g.setColor(Color.ORANGE);
g.drawRect(_selectedOrganism.x, _selectedOrganism.y,
_selectedOrganism.width-1, _selectedOrganism.height-1);
}
}
private void createActions() {
trackAction = new TrackAction("T_TRACK", "T_ABORT_TRACKING", "images/menu_track.png", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
"T_TRACK_ORGANISM", "T_ABORT_TRACKING_ORGANISM"); //$NON-NLS-1$ //$NON-NLS-2$
feedAction = new FeedAction("T_FEED", "images/menu_feed.png", //$NON-NLS-1$//$NON-NLS-2$
"T_FEED_ORGANISM"); //$NON-NLS-1$
weakenAction = new WeakenAction("T_WEAKEN", "images/menu_weaken.png", //$NON-NLS-1$//$NON-NLS-2$
"T_WEAKEN_ORGANISM"); //$NON-NLS-1$
killAction = new KillAction("T_KILL", "images/menu_kill.png", //$NON-NLS-1$//$NON-NLS-2$
"T_KILL_ORGANISM"); //$NON-NLS-1$
copyAction = new CopyAction("T_COPY", "images/menu_copy.png", //$NON-NLS-1$//$NON-NLS-2$
"T_COPY_GENETIC_CODE"); //$NON-NLS-1$
saveImageAction = new SaveImageAction("T_SAVE_IMAGE", "images/menu_save_image.png", //$NON-NLS-1$//$NON-NLS-2$
"T_SAVE_IMAGE"); //$NON-NLS-1$
reviveAction = new ReviveAction("T_REVIVE", "images/menu_revive.png", //$NON-NLS-1$ //$NON-NLS-2$
"T_REVIVE_ORGANISM"); //$NON-NLS-1$
disperseAction = new DisperseAction("T_DISPERSE", "images/menu_disperse.png", //$NON-NLS-1$ //$NON-NLS-2$
"T_DISPERSE_ORGANISM"); //$NON-NLS-1$
reproduceAction = new ReproduceAction("T_FORCE_REPRODUCTION", null, "T_FORCE_REPRODUCTION"); //$NON-NLS-1$ //$NON-NLS-2$
rejuvenateAction = new RejuvenateAction("T_REJUVENATE", null, "T_REJUVENATE"); //$NON-NLS-1$ //$NON-NLS-2$
exportAction = new ExportAction("T_EXPORT", null, "T_EXPORT_GENETIC_CODE"); //$NON-NLS-1$ //$NON-NLS-2$
pasteAction = new PasteAction("T_PASTE", null, "T_PASTE_GENETIC_CODE"); //$NON-NLS-1$ //$NON-NLS-2$
randomCreateAction = new RandomCreateAction("T_CREATE_RANDOMLY", null, "T_CREATE_RANDOMLY"); //$NON-NLS-1$ //$NON-NLS-2$
importAction = new ImportAction("T_IMPORT", null, "T_IMPORT_GENETIC_CODE"); //$NON-NLS-1$ //$NON-NLS-2$
}
public void showAliveToolbar() {
JToolBar toolBar = _mainWindow.toolBar;
toolBar.removeAll();
toolBar.add(feedAction);
toolBar.add(weakenAction);
toolBar.add(killAction);
toolBar.add(copyAction);
toolBar.add(saveImageAction);
toolBar.add(trackAction);
toolBar.add(_mainWindow.abortTrackingAction);
toolBar.invalidate();
toolBar.repaint();
}
public void showDeadToolbar() {
JToolBar toolBar = _mainWindow.toolBar;
toolBar.removeAll();
toolBar.add(reviveAction);
toolBar.add(disperseAction);
toolBar.invalidate();
toolBar.repaint();
}
/**
* Creates all popup menus.
*/
private void createPopupMenu() {
JMenuItem menuItem;
popupAlive = new JPopupMenu();
menuItem = new JMenuItem(trackAction);
menuItem.setIcon(null);
popupAlive.add(menuItem);
menuItem = new JMenuItem(feedAction);
menuItem.setIcon(null);
popupAlive.add(menuItem);
menuItem = new JMenuItem(weakenAction);
menuItem.setIcon(null);
popupAlive.add(menuItem);
popupAlive.add(new JMenuItem(reproduceAction));
popupAlive.add(new JMenuItem(rejuvenateAction));
menuItem = new JMenuItem(killAction);
menuItem.setIcon(null);
popupAlive.add(menuItem);
menuItem = new JMenuItem(copyAction);
menuItem.setIcon(null);
popupAlive.add(menuItem);
popupAlive.add(new JMenuItem(exportAction));
menuItem = new JMenuItem(saveImageAction);
menuItem.setIcon(null);
popupAlive.add(menuItem);
popupDead = new JPopupMenu();
menuItem = new JMenuItem(reviveAction);
menuItem.setIcon(null);
popupDead.add(menuItem);
menuItem = new JMenuItem(disperseAction);
menuItem.setIcon(null);
popupDead.add(menuItem);
popupVoid = new JPopupMenu();
popupVoid.add(new JMenuItem(pasteAction));
popupVoid.add(new JMenuItem(randomCreateAction));
popupVoid.add(new JMenuItem(importAction));
// Only enable file management menu options if at least there is
//permission to read user's home directory
SecurityManager sec = System.getSecurityManager();
try {
if (sec != null)
sec.checkPropertyAccess("user.home"); //$NON-NLS-1$
} catch (SecurityException ex) {
exportAction.setEnabled(false);
importAction.setEnabled(false);
saveImageAction.setEnabled(false);
}
}
/**
* Called from MainWindow when the locale is changed in order to update the menu
* entries to the new language.
*/
public void changeLocale() {
trackAction.changeLocale();
feedAction.changeLocale();
weakenAction.changeLocale();
reproduceAction.changeLocale();
rejuvenateAction.changeLocale();
killAction.changeLocale();
copyAction.changeLocale();
exportAction.changeLocale();
saveImageAction.changeLocale();
reviveAction.changeLocale();
disperseAction.changeLocale();
pasteAction.changeLocale();
randomCreateAction.changeLocale();
importAction.changeLocale();
createPopupMenu();
}
/**
* Creates a new organism with the given genetic code and puts it in the world,
* at the specified position.
*
* @param gc The genetic code for the new organism
* @param x X coordinate
* @param y Y coordinate
* @return true if the organism has been created (if there is space for it), false otherwise
*/
public boolean pasteGeneticCode(GeneticCode gc, int x, int y) {
Organism newOrganism = new Organism(_mainWindow.getWorld(), gc);
if (newOrganism.pasteOrganism(x, y)) {
_mainWindow.getWorld().addOrganism(newOrganism, null);
return true;
}
return false;
}
/**
* This method is called when a mouse event occurs. If the mouse event is
* a popup trigger, this method decide which popup menu is shown, based on
* the position of the mouse.
*
* @param e
*/
void maybeShowPopupMenu(MouseEvent e) {
if (e.isPopupTrigger()) {
mouseX = e.getX();
mouseY = e.getY();
Organism b = findOrganismFromPosition(mouseX,mouseY);
if (b != null) {
setSelectedOrganism(b);
if (b.isAlive()) {
trackAction.setTracking(_mainWindow._trackedOrganism == b);
popupAlive.show(e.getComponent(), mouseX, mouseY);
}
else
popupDead.show(e.getComponent(), mouseX, mouseY);
} else
popupVoid.show(e.getComponent(), mouseX, mouseY);
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.