001/** 002 * 003 */ 004package icy.action; 005 006import icy.canvas.IcyCanvas; 007import icy.canvas.Layer; 008import icy.common.listener.weak.WeakActiveViewerListener; 009import icy.gui.inspector.LayersPanel; 010import icy.gui.main.ActiveViewerListener; 011import icy.gui.viewer.Viewer; 012import icy.gui.viewer.ViewerEvent; 013import icy.main.Icy; 014import icy.resource.ResourceUtil; 015import icy.resource.icon.IcyIcon; 016import icy.sequence.Sequence; 017import icy.util.ClassUtil; 018 019import java.awt.event.ActionEvent; 020import java.awt.event.InputEvent; 021import java.awt.event.KeyEvent; 022import java.lang.reflect.Field; 023import java.util.ArrayList; 024import java.util.Arrays; 025import java.util.List; 026 027/** 028 * Canvas associated actions (disable/enable layers, fit, remove layer...) 029 * 030 * @author Stephane 031 */ 032public class CanvasActions 033{ 034 public static class ToggleLayersAction extends IcyAbstractAction implements ActiveViewerListener 035 { 036 public ToggleLayersAction(boolean selected) 037 { 038 super("Layers", new IcyIcon(ResourceUtil.ICON_LAYER_H2), "Show/Hide layers", KeyEvent.VK_L); 039 040 setSelected(selected); 041 if (selected) 042 setDescription("Hide layers"); 043 else 044 setDescription("Show layers"); 045 046 Icy.getMainInterface().addActiveViewerListener(new WeakActiveViewerListener(this)); 047 } 048 049 public ToggleLayersAction() 050 { 051 this(false); 052 } 053 054 /** 055 * 056 */ 057 private static final long serialVersionUID = 923175461167344847L; 058 059 @Override 060 public boolean doAction(ActionEvent e) 061 { 062 final Viewer viewer = Icy.getMainInterface().getActiveViewer(); 063 final IcyCanvas canvas = (viewer != null) ? viewer.getCanvas() : null; 064 065 if (canvas != null) 066 { 067 final boolean visible = !canvas.isLayersVisible(); 068 069 canvas.setLayersVisible(visible); 070 071 if (visible) 072 setDescription("Hide layers"); 073 else 074 setDescription("Show layers"); 075 076 return true; 077 } 078 079 return false; 080 } 081 082 @Override 083 public boolean isEnabled() 084 { 085 return super.isEnabled() && (Icy.getMainInterface().getActiveViewer() != null); 086 } 087 088 @Override 089 public void viewerActivated(Viewer viewer) 090 { 091 // notify enabled change 092 enabledChanged(); 093 } 094 095 @Override 096 public void viewerDeactivated(Viewer viewer) 097 { 098 } 099 100 @Override 101 public void activeViewerChanged(ViewerEvent event) 102 { 103 } 104 }; 105 106 public static IcyAbstractAction screenShotAction = new IcyAbstractAction("Screeshot (view)", new IcyIcon( 107 ResourceUtil.ICON_PHOTO), "Take a screenshot of current view", true, "Rendering...") 108 { 109 /** 110 * 111 */ 112 private static final long serialVersionUID = -8320047127782258236L; 113 114 @Override 115 public boolean doAction(ActionEvent e) 116 { 117 // so it won't change during process 118 final Viewer viewer = Icy.getMainInterface().getActiveViewer(); 119 final IcyCanvas canvas = (viewer != null) ? viewer.getCanvas() : null; 120 final Sequence sequence = (viewer != null) ? viewer.getSequence() : null; 121 122 if ((sequence != null) && (canvas != null)) 123 { 124 final Sequence seqOut = canvas.getRenderedSequence(true, progressFrame); 125 126 if (seqOut != null) 127 { 128 // set sequence name 129 seqOut.setName("Screen shot of '" + sequence.getName() + "' view"); 130 // add sequence 131 Icy.getMainInterface().addSequence(seqOut); 132 133 return true; 134 } 135 } 136 137 return false; 138 } 139 140 @Override 141 public boolean isEnabled() 142 { 143 return super.isEnabled() && (Icy.getMainInterface().getActiveViewer() != null); 144 } 145 }; 146 147 public static IcyAbstractAction screenShotAlternateAction = new IcyAbstractAction("Screenshot (global)", 148 new IcyIcon(ResourceUtil.ICON_PHOTO_SMALL), 149 "Take a screenshot of current view with original sequence dimension", true, "Rendering...") 150 { 151 152 /** 153 * 154 */ 155 private static final long serialVersionUID = -6434663157861847013L; 156 157 @Override 158 public boolean doAction(ActionEvent e) 159 { 160 // so it won't change during process 161 final Viewer viewer = Icy.getMainInterface().getActiveViewer(); 162 final IcyCanvas canvas = (viewer != null) ? viewer.getCanvas() : null; 163 final Sequence sequence = (viewer != null) ? viewer.getSequence() : null; 164 165 if ((sequence != null) && (canvas != null)) 166 { 167 final Sequence seqOut = canvas.getRenderedSequence(false, progressFrame); 168 169 if (seqOut != null) 170 { 171 // set sequence name 172 seqOut.setName("Rendering of '" + sequence.getName() + "' view"); 173 // add sequence 174 Icy.getMainInterface().addSequence(seqOut); 175 176 return true; 177 } 178 } 179 180 return false; 181 } 182 183 @Override 184 public boolean isEnabled() 185 { 186 return super.isEnabled() && (Icy.getMainInterface().getActiveViewer() != null); 187 } 188 }; 189 190 public static IcyAbstractAction unselectAction = new IcyAbstractAction("Unselect", (IcyIcon) null, 191 "Unselect layer(s)", KeyEvent.VK_ESCAPE) 192 { 193 /** 194 * 195 */ 196 private static final long serialVersionUID = -6136680076368815566L; 197 198 @Override 199 public boolean doAction(ActionEvent e) 200 { 201 final LayersPanel layersPanel = Icy.getMainInterface().getLayersPanel(); 202 203 if (layersPanel != null) 204 { 205 layersPanel.clearSelected(); 206 207 return true; 208 } 209 210 return false; 211 } 212 213 @Override 214 public boolean isEnabled() 215 { 216 return super.isEnabled() && (Icy.getMainInterface().getActiveSequence() != null); 217 } 218 }; 219 220 public static IcyAbstractAction deleteLayersAction = new IcyAbstractAction("Delete", new IcyIcon( 221 ResourceUtil.ICON_DELETE), "Delete selected layer(s)", KeyEvent.VK_DELETE) 222 { 223 /** 224 * 225 */ 226 private static final long serialVersionUID = 929998190473791930L; 227 228 @Override 229 public boolean doAction(ActionEvent e) 230 { 231 final Viewer viewer = Icy.getMainInterface().getActiveViewer(); 232 233 if (viewer != null) 234 { 235 final Sequence sequence = viewer.getSequence(); 236 final LayersPanel layersPanel = Icy.getMainInterface().getLayersPanel(); 237 238 if ((sequence != null) && (layersPanel != null)) 239 { 240 final List<Layer> layers = layersPanel.getSelectedLayers(); 241 242 if (layers.size() > 0) 243 { 244 sequence.beginUpdate(); 245 try 246 { 247 // delete selected layer 248 for (Layer layer : layers) 249 if (layer.getCanBeRemoved()) 250 // we try first to remove the overlay from Sequence 251 if (!sequence.removeOverlay(layer.getOverlay())) 252 // the overlay is just present on the canvas so we remove from it only 253 viewer.getCanvas().removeLayer(layer); 254 } 255 finally 256 { 257 sequence.endUpdate(); 258 } 259 260 return true; 261 262 } 263 } 264 } 265 266 return false; 267 } 268 269 @Override 270 public boolean isEnabled() 271 { 272 final Sequence sequence = Icy.getMainInterface().getActiveSequence(); 273 final LayersPanel layersPanel = Icy.getMainInterface().getLayersPanel(); 274 return super.isEnabled() && (sequence != null) && (layersPanel != null) 275 && (layersPanel.getSelectedLayers().size() > 0); 276 } 277 }; 278 279 public static IcyAbstractAction toggleLayersAction = new ToggleLayersAction(); 280 281 public static IcyAbstractAction globalDisableSyncAction = new IcyAbstractAction("Disabled (all)", new IcyIcon( 282 ResourceUtil.ICON_LOCK_OPEN), "Synchronization disabled on all viewers", KeyEvent.VK_0, 283 InputEvent.SHIFT_MASK) 284 { 285 /** 286 * 287 */ 288 private static final long serialVersionUID = -8167090991290743018L; 289 290 @Override 291 public boolean doAction(ActionEvent e) 292 { 293 Icy.getMainInterface().setGlobalViewSyncId(0); 294 295 return true; 296 } 297 }; 298 299 public static IcyAbstractAction globalSyncGroup1Action = new IcyAbstractAction("Group 1 (all)", new IcyIcon( 300 ResourceUtil.getLockedImage(1)), "All viewers set to full synchronization group 1 (view and Z/T position)", 301 KeyEvent.VK_1, InputEvent.SHIFT_MASK) 302 { 303 /** 304 * 305 */ 306 private static final long serialVersionUID = -2303919386920010513L; 307 308 @Override 309 public boolean doAction(ActionEvent e) 310 { 311 Icy.getMainInterface().setGlobalViewSyncId(1); 312 313 return true; 314 } 315 316 @Override 317 public boolean isEnabled() 318 { 319 final Viewer viewer = Icy.getMainInterface().getActiveViewer(); 320 return super.isEnabled() && (viewer != null); 321 } 322 }; 323 324 public static IcyAbstractAction globalSyncGroup2Action = new IcyAbstractAction("Group 2 (all)", new IcyIcon( 325 ResourceUtil.getLockedImage(2)), "All viewers set to full synchronization group 2 (view and Z/T position)", 326 KeyEvent.VK_2, InputEvent.SHIFT_MASK) 327 { 328 /** 329 * 330 */ 331 private static final long serialVersionUID = 3238069599592469829L; 332 333 @Override 334 public boolean doAction(ActionEvent e) 335 { 336 Icy.getMainInterface().setGlobalViewSyncId(2); 337 338 return true; 339 } 340 341 @Override 342 public boolean isEnabled() 343 { 344 final Viewer viewer = Icy.getMainInterface().getActiveViewer(); 345 return super.isEnabled() && (viewer != null); 346 } 347 }; 348 349 public static IcyAbstractAction globalSyncGroup3Action = new IcyAbstractAction("Group 3 (all)", new IcyIcon( 350 ResourceUtil.getLockedImage(3)), 351 "All viewers set to view synchronization group (view synched but not Z/T position)", KeyEvent.VK_3, 352 InputEvent.SHIFT_MASK) 353 { 354 /** 355 * 356 */ 357 private static final long serialVersionUID = -6943970700811154609L; 358 359 @Override 360 public boolean doAction(ActionEvent e) 361 { 362 Icy.getMainInterface().setGlobalViewSyncId(3); 363 364 return true; 365 } 366 367 @Override 368 public boolean isEnabled() 369 { 370 final Viewer viewer = Icy.getMainInterface().getActiveViewer(); 371 return super.isEnabled() && (viewer != null); 372 } 373 }; 374 375 public static IcyAbstractAction globalSyncGroup4Action = new IcyAbstractAction("Group 4 (all)", new IcyIcon( 376 ResourceUtil.getLockedImage(4)), 377 "All viewers set to navigation synchronization group (Z/T position synched but not view)", KeyEvent.VK_4, 378 InputEvent.SHIFT_MASK) 379 { 380 /** 381 * 382 */ 383 private static final long serialVersionUID = 4861151153688280102L; 384 385 @Override 386 public boolean doAction(ActionEvent e) 387 { 388 Icy.getMainInterface().setGlobalViewSyncId(4); 389 390 return true; 391 } 392 393 @Override 394 public boolean isEnabled() 395 { 396 final Viewer viewer = Icy.getMainInterface().getActiveViewer(); 397 return super.isEnabled() && (viewer != null); 398 } 399 }; 400 401 public static IcyAbstractAction disableSyncAction = new IcyAbstractAction("disabled", new IcyIcon( 402 ResourceUtil.ICON_LOCK_OPEN), "Synchronization disabled (global)", KeyEvent.VK_0) 403 { 404 /** 405 * 406 */ 407 private static final long serialVersionUID = -5275762712812447215L; 408 409 @Override 410 public boolean doAction(ActionEvent e) 411 { 412 final Viewer viewer = Icy.getMainInterface().getActiveViewer(); 413 final IcyCanvas canvas = (viewer != null) ? viewer.getCanvas() : null; 414 415 if (canvas != null) 416 { 417 canvas.setSyncId(0); 418 return true; 419 } 420 421 return false; 422 } 423 424 @Override 425 public boolean isEnabled() 426 { 427 final Viewer viewer = Icy.getMainInterface().getActiveViewer(); 428 return super.isEnabled() && (viewer != null); 429 } 430 }; 431 432 public static IcyAbstractAction syncGroup1Action = new IcyAbstractAction("Group 1", new IcyIcon( 433 ResourceUtil.getLockedImage(1)), "Full synchronization group 1 (view and Z/T position)", KeyEvent.VK_1) 434 { 435 /** 436 * 437 */ 438 private static final long serialVersionUID = 5469991474868966986L; 439 440 @Override 441 public boolean doAction(ActionEvent e) 442 { 443 final Viewer viewer = Icy.getMainInterface().getActiveViewer(); 444 final IcyCanvas canvas = (viewer != null) ? viewer.getCanvas() : null; 445 446 if (canvas != null) 447 { 448 // already set --> remove it 449 if (canvas.getSyncId() == 1) 450 canvas.setSyncId(0); 451 else 452 canvas.setSyncId(1); 453 return true; 454 } 455 456 return false; 457 } 458 459 @Override 460 public boolean isEnabled() 461 { 462 final Viewer viewer = Icy.getMainInterface().getActiveViewer(); 463 return super.isEnabled() && (viewer != null); 464 } 465 }; 466 467 public static IcyAbstractAction syncGroup2Action = new IcyAbstractAction("Group 2", new IcyIcon( 468 ResourceUtil.getLockedImage(2)), "Full synchronization group 2 (view and Z/T position)", KeyEvent.VK_2) 469 { 470 /** 471 * 472 */ 473 private static final long serialVersionUID = -8000162851973321503L; 474 475 @Override 476 public boolean doAction(ActionEvent e) 477 { 478 final Viewer viewer = Icy.getMainInterface().getActiveViewer(); 479 final IcyCanvas canvas = (viewer != null) ? viewer.getCanvas() : null; 480 481 if (canvas != null) 482 { 483 // already set --> remove it 484 if (canvas.getSyncId() == 2) 485 canvas.setSyncId(0); 486 else 487 canvas.setSyncId(2); 488 return true; 489 } 490 491 return false; 492 } 493 494 @Override 495 public boolean isEnabled() 496 { 497 final Viewer viewer = Icy.getMainInterface().getActiveViewer(); 498 return super.isEnabled() && (viewer != null); 499 } 500 }; 501 502 public static IcyAbstractAction syncGroup3Action = new IcyAbstractAction("Group 3", new IcyIcon( 503 ResourceUtil.getLockedImage(3)), "View synchronization group (view synched but not Z/T position)", 504 KeyEvent.VK_3) 505 { 506 /** 507 * 508 */ 509 private static final long serialVersionUID = 2131076522855333994L; 510 511 @Override 512 public boolean doAction(ActionEvent e) 513 { 514 final Viewer viewer = Icy.getMainInterface().getActiveViewer(); 515 final IcyCanvas canvas = (viewer != null) ? viewer.getCanvas() : null; 516 517 if (canvas != null) 518 { 519 // already set --> remove it 520 if (canvas.getSyncId() == 3) 521 canvas.setSyncId(0); 522 else 523 canvas.setSyncId(3); 524 return true; 525 } 526 527 return false; 528 } 529 530 @Override 531 public boolean isEnabled() 532 { 533 final Viewer viewer = Icy.getMainInterface().getActiveViewer(); 534 return super.isEnabled() && (viewer != null); 535 } 536 }; 537 538 public static IcyAbstractAction syncGroup4Action = new IcyAbstractAction("Group 4", new IcyIcon( 539 ResourceUtil.getLockedImage(4)), "Navigation synchronization group (Z/T position synched but not view)", 540 KeyEvent.VK_4) 541 { 542 /** 543 * 544 */ 545 private static final long serialVersionUID = -7921163331144086906L; 546 547 @Override 548 public boolean doAction(ActionEvent e) 549 { 550 final Viewer viewer = Icy.getMainInterface().getActiveViewer(); 551 final IcyCanvas canvas = (viewer != null) ? viewer.getCanvas() : null; 552 553 if (canvas != null) 554 { 555 // already set --> remove it 556 if (canvas.getSyncId() == 4) 557 canvas.setSyncId(0); 558 else 559 canvas.setSyncId(4); 560 return true; 561 } 562 563 return false; 564 } 565 566 @Override 567 public boolean isEnabled() 568 { 569 final Viewer viewer = Icy.getMainInterface().getActiveViewer(); 570 return super.isEnabled() && (viewer != null); 571 } 572 }; 573 574 /** 575 * Return all actions of this class 576 */ 577 public static List<IcyAbstractAction> getAllActions() 578 { 579 final List<IcyAbstractAction> result = new ArrayList<IcyAbstractAction>(); 580 581 for (Field field : CanvasActions.class.getFields()) 582 { 583 final Class<?> type = field.getType(); 584 585 try 586 { 587 if (ClassUtil.isSubClass(type, IcyAbstractAction[].class)) 588 result.addAll(Arrays.asList(((IcyAbstractAction[]) field.get(null)))); 589 if (ClassUtil.isSubClass(type, IcyAbstractAction.class)) 590 result.add((IcyAbstractAction) field.get(null)); 591 } 592 catch (Exception e) 593 { 594 // ignore 595 } 596 } 597 598 return result; 599 } 600}