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 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(); 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;} @Override public void setImageAtObjectPosition(int idObj) { // TODO Auto-generated method stub } }); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(ospe_SelectPanel); frame.setVisible(true); frame.pack(); } }