OSPE_SelectPanel.java 14.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
package osp.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultTreeSelectionModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

import osp.FieldOfView;
import osp.Image;
import osp.MaskInterface;
import osp.utils.CustomJDialog;

/**
 * Represents the left panel of the application, containing the project tree
 * (with the list of images and masks) and the mask properties panel.
 */
public class OSPE_SelectPanel extends JPanel implements Observer{
	private static final long serialVersionUID = 1L;

	/** Main frame of the application */
	private StatusInterface mainFrame;

	private FieldOfViewSelectionInterface fov;


	/** Tree panel */
	private JPanel treesPanel;
	private JScrollPane scrollTreePanel;
    
	/** Tree */
	private JTree fovTree;
	private DefaultTreeModel dtm;
	/** Using tree */
	/** List of images */
	private List<DefaultMutableTreeNode> listImages;
	/** Mask properties panel */
	private OSPE_MaskPropPanel maskPropertiesPanel;
	
	/** Flag to prevent the select panel to update itself when not wanted */
	private boolean isMySelfFlag = false;
	
	private boolean isMask = false;

	private OSPE_VisuPanelInterface visuPanel;

	private AccessSlitMenuInterface accessMenuInterface;
	
	private OspeControl control;


	/**
	 * Constructs a new select panel and initializes the tree for displaying and
	 * selecting images and masks as well as the mask properties panel.
	 * 
	 * @param p
	 *            Application main panel.
	 * @param accessMenuInterface 
	 */
	public OSPE_SelectPanel(StatusInterface p, AccessSlitMenuInterface accessMenuInterface, 
			FieldOfViewSelectionInterface fov, OSPE_VisuPanelInterface visuPanel) {
		super(new BorderLayout());
		this.mainFrame = p;
		this.accessMenuInterface = accessMenuInterface;
		this.fov = fov;
		this.visuPanel = visuPanel;
		
		((FieldOfView) fov).addObserver(this);
		setPreferredSize(new Dimension(200, 600));

		treesPanel = new JPanel(new GridLayout(1, 1));
		Border border = BorderFactory.createTitledBorder(
				BorderFactory.createLineBorder(Color.black), treesPanel.getName(),
				TitledBorder.LEFT, TitledBorder.DEFAULT_POSITION);
		treesPanel.setBorder(border);
		scrollTreePanel = new JScrollPane(treesPanel);
        scrollTreePanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        
        listImages = new ArrayList<DefaultMutableTreeNode>();

		treesPanel.add(getFovTree());
		add(scrollTreePanel, BorderLayout.CENTER);
		// add(treesPanel, BorderLayout.CENTER);

		maskPropertiesPanel = new OSPE_MaskPropPanel(fov, mainFrame);
		add(maskPropertiesPanel, BorderLayout.SOUTH);
	}

	public void setControl(OspeControl ospeControl){
		this.control = ospeControl;
	}
	
	/**
	 * Updates the tree and the mask properties panel when there are changes.
	 */
	private void updateByHand() {
	
		// Test when adding an new image, if there is no mask defined, put the flag down.
	    if ((fov.getCurrentImage() == null) || (fov.getCurrentImage().getListMasks().size()) == 0 )
	    	isMask=false;
	    
	    	maskPropertiesPanel.updateMaskProp(isMask);

		if (isMySelfFlag) 
		{
			isMySelfFlag = false;
		}
		else 
		{
			fov.computeWithTheBrain();
			int nbChild = getNbItemOfTree();
			if (nbChild != fov.getNbItems())
			{ // more or less item
				displayFovTree((DefaultMutableTreeNode) dtm.getRoot());
		    }
			else if (getNbAllItemTree() != fov.getNbItems())
			{
				displayFovTree((DefaultMutableTreeNode) dtm.getRoot());
			}
		}
		
	}

	/**
	 * return the number of Image on tree
	 * @return
	 */
	public int getNbItemOfTree() {
		int res = 0;
		if (!((DefaultMutableTreeNode)dtm.getRoot()).isLeaf())
			res=  ((DefaultMutableTreeNode)dtm.getRoot()).getLeafCount(); 
		return res;
	}

	/**
	 * Return the all numbers of element on tree 
	 * @return
	 */
	public int getNbAllItemTree() {
		int res = 0;
		if (!((DefaultMutableTreeNode)dtm.getRoot()).isLeaf())
			for (int i = 0; i < ((DefaultMutableTreeNode)dtm.getRoot()).getChildCount(); i++)
			{
				res = res + 1;
				res = res + ((DefaultMutableTreeNode)dtm.getRoot()).getChildAt(i).getChildCount();
			}
		return res;
	}
	/**
	 * Called by changed observers.
	 */
	@Override
	public void update(Observable changed, Object arg) {
		updateByHand();
	}

	/**
	 * Displays the tree with the list of images and masks of the project (expanded)
	 * 
	 * @param t
	 *            The root node.
	 */
	private void displayFovTree(DefaultMutableTreeNode t) {
		
		t.removeAllChildren();
		DefaultMutableTreeNode  nodeMask = null;
		for (int j = 0; j < fov.getNbrImages(); j++) {
			DefaultMutableTreeNode imName = new DefaultMutableTreeNode(fov.getNameOfImage(j));

			for (int i = 0; i < fov.getNbrMasksOfImage(j); i++) {
				DefaultMutableTreeNode mskName = new DefaultMutableTreeNode(
						fov.getNameOfMaskOfImage(i, j));
				imName.add(mskName);
				if (i == fov.getSelectedMaskOfCurrentImage() && 
				    j == fov.getSelectedImage())
					nodeMask = mskName;
			}
			listImages.add(imName);
			t.add(imName);
		}

		if (fov.getSelectedImage() >= 0) {
			if (nodeMask != null){
				final TreePath path = new TreePath(nodeMask.getPath());
				fovTree.expandPath(path);
				fovTree.getSelectionModel().setSelectionPath(path);
			} else {
				final TreePath path = new TreePath(((DefaultMutableTreeNode) t.getChildAt(fov
						.getSelectedImage())).getPath());
				fovTree.expandPath(path);
				fovTree.getSelectionModel().setSelectionPath(path);
			}
			
		}
		fovTree.updateUI();

		for (int i = 0; i < fovTree.getRowCount(); i++) {
			fovTree.expandRow(i);
		}
	}

	/**
	 * Called when an element on the tree panel is selected. It gets the
	 * selected image and/or mask and displays it/them.
	 */
	public void onFovTreeSelection(TreeSelectionEvent e) {
		mainFrame.setStatusBarText(" ");
		if ((fov.getSelectedImage() >= 0) && (fovTree.getSelectionCount()==1)){
			boolean displayImage = displayNewImageIfNecessary((DefaultMutableTreeNode) fovTree.getLastSelectedPathComponent());
			if (! displayImage)
				displayNewMaskIfNecessary((DefaultMutableTreeNode) fovTree.getLastSelectedPathComponent());
		}
	}

	/**
	 * @param defaultMutableTreeNode
	 */
	private void displayNewMaskIfNecessary(DefaultMutableTreeNode defaultMutableTreeNode) {
		{
			int[] indices = getIndicesImageAndMask(defaultMutableTreeNode);
			
			if (indices[1] != -1){

				visuPanel.updateCutLevels(indices[0]);
				fov.setSelectedImage(indices[0]);
				isMySelfFlag = true;
				isMask=true;
				fov.setSelectedMaskOfImage(indices[1], indices[0]);

				visuPanel.enableValidateAndGenerateButton(fov.isMaskOfImageValidated(indices[1], indices[0]));

				if (!accessMenuInterface.isjMenuSlitEnabled())
					accessMenuInterface.setjMenuSlitEnabled(true);

				visuPanel.updateByHand();
				visuPanel.updateSlides();
				mainFrame.updateButtons();
				updateByHand();
			}
		}
	}

	/**
	 * @param defaultMutableTreeNode
	 * @return indices of image and mask 
	 * 		indices[0] = indice Image
	 * 		indices[1] = indice mask
	 */
	private int[] getIndicesImageAndMask(DefaultMutableTreeNode defaultMutableTreeNode) {
		

		int indices[] = new int []{-1, -1};
		for (int i = 0; i < fov.getNbrImages(); i++) {
			for (int j = 0; j < fov.getNbrMasksOfImage(i); j++) {
				if (fov.getNameOfMaskOfImage(j, i).equals(defaultMutableTreeNode.getUserObject().toString())) {
					indices[0] = i;
					indices[1] = j;
				}
			}
		}
		return indices;
	}

	/**
	 * display new image  if the chosen element is an image
	 * @param defaultMutableTreeNode
	 */
	private boolean  displayNewImageIfNecessary(DefaultMutableTreeNode defaultMutableTreeNode) {
		boolean isImage = false;
		int val = getIndiceImage(defaultMutableTreeNode);
		
		if (val != -1){
			isImage = true;
			visuPanel.getNavigatorImageDisplay().getImageProcessor().setLowCut(fov.getImage(val).getLowCut());
			visuPanel.getNavigatorImageDisplay().getImageProcessor().setHighCut(fov.getImage(val).getHighCut());
			control.updateCutColorsAndSetImage(val);	
			isMySelfFlag = true;
			isMask=false;
			// test // 
			fov.getImage(val).setSelectedMask(-1);
			mainFrame.updateButtons();
			updateByHand();
		}
		return isImage;
	}

	

	/**
	 * @param defaultMutableTreeNode
	 * @return
	 */
	private int getIndiceImage(DefaultMutableTreeNode defaultMutableTreeNode) {
		int val = -1;
		boolean find = false;
		
		for (int i = 0; ! find && i < fov.getNbrImages() ; i++) {
			if (fov.getNameOfImage(i).equals(defaultMutableTreeNode.getUserObject().toString())) {
				val = i;	
			}
		}
		return val;
	}

	public JTree getFovTree() {
		if (fovTree == null){
			dtm =new DefaultTreeModel(new DefaultMutableTreeNode("Field of View:"));
	        fovTree = new JTree(dtm);
			fovTree.setName("jTreeProjects");
			fovTree.getSelectionModel().setSelectionMode(
					TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
			fovTree.addTreeSelectionListener(new TreeSelectionListener() {
				@Override
				public void valueChanged(TreeSelectionEvent e) {
					onFovTreeSelection(e);
				}
			});
			fovTree.addKeyListener(new KeyListener() {
				@Override
				public void keyTyped(KeyEvent e) {}
				@Override
				public void keyPressed(KeyEvent e) {}
				@Override
				public void keyReleased(KeyEvent e) {
					onFovTreeKeyReleased(e);
				}
			});
		}
		return fovTree;
	}
	

	public void onFovTreeKeyReleased(KeyEvent event) {
		int keyCode = event.getKeyCode();
		if (keyCode == KeyEvent.VK_DELETE) 
		{
			int nbSelect = fovTree.getSelectionCount();
			if (nbSelect == 1) 
			{
				String mess = "Are you sure you want to delete this selection ? ";
				CustomJDialog dialog = new CustomJDialog(mess, CustomJDialog._WARNING_,
						CustomJDialog._YESNO_BUTTON_, this);
	
				if (dialog.getValue() == JOptionPane.YES_OPTION) {			
					removeSelected((DefaultMutableTreeNode) fovTree.getLastSelectedPathComponent());
				}
			} 
			else 
			{
				TreePath[] tPaths = fovTree.getSelectionPaths();				
				if (tPaths != null) 
				{
					
					String mess = "Are you sure you want to delete all the selection ? ";
					CustomJDialog dialog = new CustomJDialog(mess, CustomJDialog._WARNING_,
							CustomJDialog._YESNO_BUTTON_, this);
	
					if (dialog.getValue() == JOptionPane.YES_OPTION) 
					{
						DefaultMutableTreeNode nodes[] = new DefaultMutableTreeNode[tPaths.length];
						for (int i = 0; i < nodes.length; i++) 
						{
							nodes[i] = (DefaultMutableTreeNode) tPaths[i].getLastPathComponent();
							removeSelected(nodes[i]);
						}
					}
				}
			}
		}
	}
	

	/**
	 * Remove the selected object(s) (Image or mask)
	 * @param b 
	 * 
	 * @param defaultMutableTreeNode
	 */
	private void removeSelected(DefaultMutableTreeNode node) {
		
		if (!node.isRoot())
		{
			dtm.removeNodeFromParent(node);
			int indiceImage = getIndiceImage(node);
			if (indiceImage > -1) {
				fov.setSelectedImage(indiceImage);
				control.removeImage();
			} 
			else {
				int[] indices = getIndicesImageAndMask(node);
				if (indices[0] > -1){
					fov.setSelectedMaskOfImage(indices[1], indices[0]);
					control.removeMask();
				}
			}
		}
	}
	
	/**
	 * For tests 
	 * @param args
	 */
	public static void main(String[] args) {
		OSPE_SelectPanel ospe_SelectPanel = new OSPE_SelectPanel(new StatusInterface() {
			@Override
			public OspeControl getOspeControl() {
				return null;
			}
			@Override
			public OSPE_VisuPanel getVisuPanel() {return null;}
			
			@Override
			public void setStatusBarText(String string) {
				System.out.println(string);
			}
			
			@Override
			public void setStatusBarText(Color color, String string) {
				System.out.println("Color :" + color + string);
			}
			
			@Override
			public void updateButtons(){}
			
		}, new AccessSlitMenuInterface() {
			
			@Override
			public void setjMenuSlitEnabled(boolean b) {}
			
			@Override
			public boolean isjMenuSlitEnabled() {
				return false;
			}
		}, new FieldOfViewSelectionInterface() {
			
			@Override
			public int getSelectedMaskOfCurrentImage() {return 0;}
			@Override
			public int getSelectedImage() {return 0;}
			@Override
			public MaskInterface getCurrentMaskOfCurrentImage() {return null;}
			@Override
			public void setSelectedMaskOfImage(int j, int i) {}
			@Override
			public void setSelectedImage(int i) {}
			@Override
			public Image getImage(int i) {return null;}
			@Override
			public Image getCurrentImage() {return null;}
			@Override
			public int getNbrMasksOfImage(int j) {return 0;}
			@Override
			public int getNbrImages() {return 0;}
			@Override
			public String getNameOfMaskOfImage(int i, int j) {return null;}
			@Override
			public String getNameOfImage(int j) {return null;}
			@Override
			public void computeWithTheBrain() {}
			@Override
			public void setOmega(float a){}
			@Override
			public boolean isMaskOfImageValidated(int j, int i) {return false;}
			@Override
			public int getNbItems() {
				// TODO Auto-generated method stub
				return 0;
			}
		}, new OSPE_VisuPanelInterface() {
			@Override
			public void updateSlides() {}
			@Override
			public void updateOfImageNavigatorDisplay() {}
			@Override
			public void updateCutLevels(int i) {}
			@Override
			public void updateColors(int i) {}
			@Override
			public void updateByHand() {}
			@Override
			public void enableValidateAndGenerateButton(boolean maskValidated) {}
			@Override
			public OSPE_NavigatorImageDisplay getNavigatorImageDisplay() {
				return null;
			}
		});
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(ospe_SelectPanel);
		frame.setVisible(true);
		frame.pack();
	}
	


}