Changeset 7221 for extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/ui/mainframe/CategoriesTree.java
- Timestamp:
- Oct 16, 2010, 1:08:28 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/ui/mainframe/CategoriesTree.java
r6980 r7221 8 8 import java.awt.event.MouseListener; 9 9 import java.util.List; 10 11 10 import javax.swing.JMenuItem; 12 11 import javax.swing.JOptionPane; … … 21 20 import javax.swing.tree.TreePath; 22 21 import javax.swing.tree.TreeSelectionModel; 23 24 22 import fr.mael.jiwigo.om.Category; 25 23 import fr.mael.jiwigo.service.CategoryService; 26 24 import fr.mael.jiwigo.transverse.util.Messages; 27 25 import fr.mael.jiwigo.transverse.util.Outil; 26 28 27 29 28 /** … … 58 57 public class CategoriesTree extends JPanel implements TreeSelectionListener, MouseListener, ActionListener { 59 58 60 /** 61 * Logger 62 */ 63 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory 64 .getLog(CategoriesTree.class); 65 /** 66 * the tree 67 */ 68 private JTree tree; 69 /** 70 * the menu to add a category 71 */ 72 private JMenuItem menuAjouter; 73 /** 74 * the menu to refresh the categories 75 */ 76 private JMenuItem menuActualiser; 77 /** 78 * the root of the tree which is not displayed 79 */ 80 private DefaultMutableTreeNode root = new DefaultMutableTreeNode(""); 81 /** 82 * the selected category 83 */ 84 private Category selectedCategory; 85 86 /** 87 * Constructor 88 */ 89 public CategoriesTree() { 90 super(new BorderLayout()); 91 //creation of the tree 92 createNodes(root); 93 tree = new JTree(root); 94 tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); 95 tree.addTreeSelectionListener(this); 96 tree.setRootVisible(false); 97 tree.addMouseListener(this); 98 JScrollPane treeView = new JScrollPane(tree); 99 Dimension minimumSize = new Dimension(100, 50); 100 treeView.setMinimumSize(minimumSize); 101 add(treeView, BorderLayout.CENTER); 102 } 103 104 /** 105 * Method that allows to update the tree 106 */ 107 public void setUpUi() { 108 //begin bug:0001830 109 root.removeAllChildren(); 110 createNodes(root); 111 ((DefaultTreeModel) tree.getModel()).reload(); 112 //end 113 114 } 115 116 /* (non-Javadoc) 117 * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent) 118 */ 119 public void valueChanged(TreeSelectionEvent e) { 120 DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); 121 // 122 if (node == null) 123 return; 124 Object catSelectionnee = node.getUserObject(); 125 Category category = (Category) catSelectionnee; 126 //MainFrame.imagesPanel.rafraichir(category.getIdentifiant(), false); 127 MainFrame.getInstance().addTabb(new ThumbnailCategoryPanel(category)); 128 } 129 130 /** 131 * creation of the nodes 132 * @param root the root 133 */ 134 private void createNodes(DefaultMutableTreeNode root) { 135 DefaultMutableTreeNode category = null; 136 try { 137 List<Category> list = CategoryService.getInstance().construireArbre(); 138 for (Category cat : list) { 139 if (cat.getCategoriesMeres().size() == 0) { 140 category = new DefaultMutableTreeNode(cat); 141 root.add(category); 142 recursiveNodeCreation(category, cat); 143 } 144 145 } 146 } catch (Exception e) { 147 LOG.error(Outil.getStackTrace(e)); 148 JOptionPane.showMessageDialog(null, Messages.getMessage("treeCreationError"), Messages.getMessage("error"), 149 JOptionPane.ERROR_MESSAGE); 150 } 151 152 } 153 154 /** 155 * recursive method for the creation of the nodes 156 * @param categoryNode 157 * @param category 158 */ 159 private void recursiveNodeCreation(DefaultMutableTreeNode categoryNode, Category category) { 160 for (Category cat : category.getCategoriesFilles()) { 161 DefaultMutableTreeNode node = new DefaultMutableTreeNode(cat); 162 categoryNode.add(node); 163 recursiveNodeCreation(node, cat); 164 } 165 } 166 167 @Override 168 public void mouseClicked(MouseEvent arg0) { 169 170 } 171 172 @Override 173 public void mouseEntered(MouseEvent arg0) { 174 // TODO Auto-generated method stub 175 176 } 177 178 @Override 179 public void mouseExited(MouseEvent arg0) { 180 // TODO Auto-generated method stub 181 182 } 183 184 @Override 185 public void mousePressed(MouseEvent e) { 186 int selRow = tree.getRowForLocation(e.getX(), e.getY()); 187 TreePath selPath = tree.getPathForLocation(e.getX(), e.getY()); 188 //begin bug:0001832 189 tree.setSelectionRow(selRow); 190 //end 191 192 if (e.getButton() == 3) { 193 JPopupMenu popup = new JPopupMenu(); 194 menuAjouter = new JMenuItem(Messages.getMessage("categories_add")); 195 menuAjouter.addActionListener(this); 196 popup.add(menuAjouter); 197 menuActualiser = new JMenuItem(Messages.getMessage("categories_update")); 198 menuActualiser.addActionListener(this); 199 popup.add(menuActualiser); 200 popup.show(tree, e.getX(), e.getY()); 201 if (selRow != -1) { 202 DefaultMutableTreeNode node = (DefaultMutableTreeNode) (selPath.getLastPathComponent()); 203 this.selectedCategory = (Category) node.getUserObject(); 204 } else { 205 this.selectedCategory = null; 206 } 207 } 208 209 } 210 211 @Override 212 public void mouseReleased(MouseEvent arg0) { 213 // TODO Auto-generated method stub 214 215 } 216 217 @Override 218 public void actionPerformed(ActionEvent arg0) { 219 if (arg0.getSource().equals(menuAjouter)) { 220 String nomcategorie = JOptionPane.showInputDialog(null, Messages.getMessage("categories_enterName"), 221 Messages.getMessage("categories_add"), JOptionPane.INFORMATION_MESSAGE); 222 //if the name of the category is not empty 223 if (!(nomcategorie == null) && !nomcategorie.equals("")) { 224 //if there is a parent category 225 if (selectedCategory != null) { 226 //try to create a category 227 if (CategoryService.getInstance().creer(nomcategorie, selectedCategory.getIdentifiant())) { 59 /** 60 * Logger 61 */ 62 public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog(CategoriesTree.class); 63 /** 64 * the tree 65 */ 66 private JTree tree; 67 /** 68 * the menu to add a category 69 */ 70 private JMenuItem menuAjouter; 71 /** 72 * the menu to refresh the categories 73 */ 74 private JMenuItem menuActualiser; 75 /** 76 * the root of the tree which is not displayed 77 */ 78 private DefaultMutableTreeNode root = new DefaultMutableTreeNode(""); 79 /** 80 * the selected category 81 */ 82 private Category selectedCategory; 83 84 85 /** 86 * Constructor 87 */ 88 public CategoriesTree() { 89 super(new BorderLayout()); 90 //creation of the tree 91 createNodes(root); 92 tree = new JTree(root); 93 tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); 94 tree.addTreeSelectionListener(this); 95 tree.setRootVisible(false); 96 tree.addMouseListener(this); 97 JScrollPane treeView = new JScrollPane(tree); 98 Dimension minimumSize = new Dimension(100, 50); 99 treeView.setMinimumSize(minimumSize); 100 add(treeView, BorderLayout.CENTER); 101 } 102 103 104 /** 105 * Method that allows to update the tree 106 */ 107 public void setUpUi() { 108 //begin bug:0001830 109 root.removeAllChildren(); 110 createNodes(root); 111 ((DefaultTreeModel) tree.getModel()).reload(); 112 //end 113 114 } 115 116 117 /* (non-Javadoc) 118 * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent) 119 */ 120 public void valueChanged(TreeSelectionEvent e) { 121 DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); 122 // 123 if (node == null) 124 return; 125 Object catSelectionnee = node.getUserObject(); 126 Category category = (Category) catSelectionnee; 127 //MainFrame.imagesPanel.rafraichir(category.getIdentifiant(), false); 128 MainFrame.getInstance().addTabb(new ThumbnailCategoryPanel(category)); 129 } 130 131 132 /** 133 * creation of the nodes 134 * @param root the root 135 */ 136 private void createNodes(DefaultMutableTreeNode root) { 137 DefaultMutableTreeNode category = null; 138 try { 139 List<Category> list = CategoryService.getInstance().construireArbre(); 140 for (Category cat : list) { 141 if (cat.getCategoriesMeres().size() == 0) { 142 category = new DefaultMutableTreeNode(cat); 143 root.add(category); 144 recursiveNodeCreation(category, cat); 145 } 146 147 } 148 } catch (Exception e) { 149 LOG.error(Outil.getStackTrace(e)); 150 JOptionPane.showMessageDialog(null, Messages.getMessage("treeCreationError"), Messages.getMessage("error"), 151 JOptionPane.ERROR_MESSAGE); 152 } 153 154 } 155 156 157 /** 158 * recursive method for the creation of the nodes 159 * @param categoryNode 160 * @param category 161 */ 162 private void recursiveNodeCreation(DefaultMutableTreeNode categoryNode, Category category) { 163 for (Category cat : category.getCategoriesFilles()) { 164 DefaultMutableTreeNode node = new DefaultMutableTreeNode(cat); 165 categoryNode.add(node); 166 recursiveNodeCreation(node, cat); 167 } 168 } 169 170 171 @Override 172 public void mouseClicked(MouseEvent arg0) { 173 174 } 175 176 177 @Override 178 public void mouseEntered(MouseEvent arg0) { 179 // TODO Auto-generated method stub 180 181 } 182 183 184 @Override 185 public void mouseExited(MouseEvent arg0) { 186 // TODO Auto-generated method stub 187 188 } 189 190 191 @Override 192 public void mousePressed(MouseEvent e) { 193 int selRow = tree.getRowForLocation(e.getX(), e.getY()); 194 TreePath selPath = tree.getPathForLocation(e.getX(), e.getY()); 195 //begin bug:0001832 196 tree.setSelectionRow(selRow); 197 //end 198 199 if (e.getButton() == 3) { 200 JPopupMenu popup = new JPopupMenu(); 201 menuAjouter = new JMenuItem(Messages.getMessage("categories_add")); 202 menuAjouter.addActionListener(this); 203 popup.add(menuAjouter); 204 menuActualiser = new JMenuItem(Messages.getMessage("categories_update")); 205 menuActualiser.addActionListener(this); 206 popup.add(menuActualiser); 207 popup.show(tree, e.getX(), e.getY()); 208 if (selRow != -1) { 209 DefaultMutableTreeNode node = (DefaultMutableTreeNode) (selPath.getLastPathComponent()); 210 this.selectedCategory = (Category) node.getUserObject(); 211 } else { 212 this.selectedCategory = null; 213 } 214 } 215 216 } 217 218 219 @Override 220 public void mouseReleased(MouseEvent arg0) { 221 // TODO Auto-generated method stub 222 223 } 224 225 226 @Override 227 public void actionPerformed(ActionEvent arg0) { 228 if (arg0.getSource().equals(menuAjouter)) { 229 String nomcategorie = JOptionPane.showInputDialog(null, Messages.getMessage("categories_enterName"), 230 Messages.getMessage("categories_add"), JOptionPane.INFORMATION_MESSAGE); 231 //if the name of the category is not empty 232 if (!(nomcategorie == null) && !nomcategorie.equals("")) { 233 //if there is a parent category 234 if (selectedCategory != null) { 235 //try to create a category 236 if (CategoryService.getInstance().creer(nomcategorie, selectedCategory.getIdentifiant())) { 237 setUpUi(); 238 } else { 239 JOptionPane.showMessageDialog(null, Messages.getMessage("categoryCreationError"), 240 Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); 241 242 } 243 } else { 244 if (CategoryService.getInstance().creer(nomcategorie)) { 245 setUpUi(); 246 } else { 247 JOptionPane.showMessageDialog(null, Messages.getMessage("categoryCreationError"), 248 Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE); 249 250 } 251 } 252 } 253 } else if (arg0.getSource().equals(menuActualiser)) { 228 254 setUpUi(); 229 } else { 230 JOptionPane.showMessageDialog(null, Messages.getMessage("categoryCreationError"), Messages 231 .getMessage("error"), JOptionPane.ERROR_MESSAGE); 232 233 } 234 } else { 235 if (CategoryService.getInstance().creer(nomcategorie)) { 236 setUpUi(); 237 } else { 238 JOptionPane.showMessageDialog(null, Messages.getMessage("categoryCreationError"), Messages 239 .getMessage("error"), JOptionPane.ERROR_MESSAGE); 240 241 } 242 } 243 } 244 } else if (arg0.getSource().equals(menuActualiser)) { 245 setUpUi(); 246 } 247 } 255 } 256 } 248 257 }
Note: See TracChangeset
for help on using the changeset viewer.