Anonymous avatar Anonymous committed 6c29633

Changed default tree, added related icons

Comments (0)

Files changed (8)

Added
New image
Added
New image
Added
New image
Added
New image

src/nyagua/IconNode.java

+/*
+    Gui-bits - library of resuable gui components
+    Copyright (C) 2010  Thomas C.A. Judge
+
+    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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+*/
+package nyagua;
+ 
+import java.awt.Component;
+import javax.swing.Icon;
+import javax.swing.tree.DefaultMutableTreeNode;
+
+/**
+ * A Tree Node with an Icon.
+ * 
+ * A comprehensive example is avaliable in com.tomjudge.gui.tree.Example
+ * Example:
+ * <pre>
+ * {@code
+ * IconNode rootNode = new IconNode("Root", new ImageIcon(getClass().getResource("/icons/root.png")), "Root") {
+ *     
+ *     public void showPopupMenu(int x, int y) {
+ *     }
+ *
+ *     
+ *     public void doubleClicked() {
+ *     }
+ * };
+ * final JTree tree = new JTree();
+ * tree.setCellRenderer(new IconNodeRenderer());
+ * tree.setModel(new DefaultTreeModel(rootNode));
+ *
+ * // Add double click listener.
+ * tree.addMouseListener(new DoubleClickListener(tree));
+ *
+ * // Add popup menu listener
+ * tree.addMouseListener(new PopupMenuListener(tree));
+ *
+ * final JPopupMenu nodeMenu = new JPopupMenu();
+ * JMenuItem menuItem = new JMenuItem();
+ * menuItem.setMnemonic('1');
+ * menuItem.setText("Menu item 1");
+ * menuItem.addActionListener(new ActionListener() {
+ *     public void actionPerformed(ActionEvent evt) {
+ *         System.out.println("Node menu item 1 action");
+ *     }
+ * });
+ * popupMenu.add(newContactMenuItem);
+ *
+ * rootNode.add(new IconNode("Node 1", null, "No Icon") {
+ *     
+ *     public void showPopupMenu(int x, int y) {
+ *         popupMenu.show(tree, x,y);
+ *     }
+ *
+ *     
+ *     public void doubleClicked() {
+ *         System.out.println("I was double clicked");
+ *     }
+ * });
+ * }
+ * </pre>
+ *
+ * 
+ * @author Tom Judger
+ */
+public abstract class IconNode extends DefaultMutableTreeNode {
+    /**
+     * The current icon to be displayed
+     */
+    protected Icon icon;
+    /**
+     * The tool tip for the node.
+     */
+    protected String toolTip;
+
+    /**
+     * Create a new IconNode
+     * @param userObject The object to be rendered as the items value (Usually a String)
+     * @param i The icon for the node.
+     * @param t The tool tip for the node.
+     */
+    public IconNode(Object userObject, Icon i, String t) {
+        super(userObject, true);
+        icon = i;
+        toolTip = t;
+        
+    }
+    
+    /**
+     * Set new tool tip.
+     * You should call JTree.updateUI() for the tree that this node is in
+     * after calling this function.
+     * @param t New tool tip text
+     */
+    public void setToolTip(String t) {
+        toolTip = t;
+    }
+    
+    /**
+     * Get the tool tip text.
+     * @return tool tip text
+     */
+    public String getToolTip() {
+        return toolTip;
+    }
+
+    /**
+     * Set the nodes icon.
+     * You should call JTree.updateUI() for the tree that this node is in
+     * after calling this function.
+     * @param icon
+     */
+    public void setIcon(Icon icon) {
+        this.icon = icon;
+    }
+
+    /**
+     * The current icon.
+     * @return the icon.
+     */
+    public Icon getIcon() {
+        return icon;
+    }
+
+    /**
+     * Show the PopUp menu for this node.
+     * @param x Co-ordinate
+     * @param y Co-ordinate
+     */
+    abstract public void showPopupMenu(Component comp, int x, int y);
+
+    /**
+     * Action to perform when this node is double clicked.
+     */
+    abstract public void doubleClicked();
+}
+

src/nyagua/IconNodeRenderer.java

+/*
+    Gui-bits - library of resuable gui components
+    Copyright (C) 2010  Thomas C.A. Judge
+
+    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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+*/
+package nyagua;
+
+import java.awt.Component;
+import javax.swing.JLabel;
+import javax.swing.JTree;
+import javax.swing.ToolTipManager;
+import javax.swing.tree.DefaultTreeCellRenderer;
+
+/**
+ * Node renderer for JTrees containing IconNode's
+ * @author tj
+ */
+public class IconNodeRenderer extends DefaultTreeCellRenderer {
+    /**
+     * Create new renderer.
+     */
+    public IconNodeRenderer() {
+    }
+    
+    @Override
+    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
+        JLabel t = (JLabel) super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
+        if (value instanceof IconNode) {
+            IconNode tmp = (IconNode)value;
+            t.setIcon(tmp.getIcon());
+        
+            if (tmp.getToolTip() != null) {
+                t.setToolTipText(tmp.getToolTip());
+            } else {
+                t.setToolTipText("");
+            }
+            t.setFont(new java.awt.Font("Dialog", 1, 12));
+            
+        } 
+        ToolTipManager toolTipManager = ToolTipManager.sharedInstance();
+        toolTipManager.registerComponent(t);    
+        return t;
+    }
+}

src/nyagua/Ny.form

                       <Group type="102" attributes="0">
                           <EmptySpace min="-2" pref="23" max="-2" attributes="0"/>
                           <Component id="phKhCo2RelationPanel" min="-2" max="-2" attributes="0"/>
-                          <EmptySpace pref="52" max="32767" attributes="0"/>
+                          <EmptySpace pref="47" max="32767" attributes="0"/>
                           <Component id="heaterCalculatorPanel" min="-2" max="-2" attributes="0"/>
-                          <EmptySpace min="0" pref="174" max="32767" attributes="0"/>
+                          <EmptySpace min="0" pref="169" max="32767" attributes="0"/>
                       </Group>
                   </Group>
                 </DimensionLayout>

src/nyagua/Ny.java

 package nyagua;
 
 import java.awt.Color;
+import java.awt.Component;
 import java.awt.Frame;
 import java.awt.Rectangle;
 import java.awt.Window;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import java.util.prefs.InvalidPreferencesFormatException;
+import javax.swing.Icon;
 import javax.swing.tree.DefaultMutableTreeNode;
 import javax.swing.DefaultListModel;
 import javax.swing.ImageIcon;
 import javax.swing.JOptionPane;
 import javax.swing.JTextField;
+import javax.swing.tree.DefaultTreeModel;
 import javax.xml.parsers.ParserConfigurationException;
 import nyagua.data.*;
 
     @SuppressWarnings("static-access")
 private void jTree1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTree1MouseClicked
     /**Load selected aquarium data from db */
-    try {
-        //get all Aquarium data:
-        String qry = "SELECT * FROM Aquarium;";// NOI18N
-        DB.openConn();
-        ResultSet rs = DB.getQuery(qry);
-        while (rs.next()) {
-            String aquariumName = rs.getString("Name");// NOI18N
-            //compare with selected element
-            if (aquariumName.matches(
-                    jTree1.getLastSelectedPathComponent().toString())) {
-                Global.AqID = rs.getInt("id");// NOI18N
-                Ny.PopulateTable();
-
-                Aquarium tank=Aquarium.getById(Global.AqID);
-                aquariumNameTextField.setText(tank.getName());// NOI18N
-                aquariumDeepTextField.setText(tank.getDepth());// NOI18N
-                aquariumWidthTextField.setText(tank.getWidth());// NOI18N
-                aquariumHeightTextField.setText(tank.getHeight());// NOI18N
-                aquariumWaterVolTextField.setText(tank.getVolume());// NOI18N
-                aquariumTypeTextField.setText(tank.getType());// NOI18N
-                aquariumStartDateTextField.setDate(tank.getStartDate());// NOI18N
-                aquariumEndDateTextField.setDate(tank.getEndDate());// NOI18N
-                aquariumOCCheckBox.setSelected(tank.getOpen());
-                aquariumTankVolTextField.setText(tank.getTankVolume());
-                aquariumBottomTextField.setText(tank.getBottom());
-                //try to load image
-                BufferedImage img = null;
-                aquariumImageLabelImg.setText(null);
-                if (tank.hasImage()){
-                    aquariumImageLabelImg.setIcon((new javax.swing.ImageIcon(tank.getImage())));
-                } else {
-                    aquariumImageLabelImg.setText(java.util.ResourceBundle.getBundle("nyagua/Bundle").getString("NO_IMAGE!"));
-                    aquariumImageLabelImg.setIcon(null);
+    int node=jTree1.getLeadSelectionRow();    
+    if (node==1) {//fishbase
+        this.jTabbedPane1.setSelectedIndex(5);
+    } else if (node==2){//plantsbase
+            
+        this.jTabbedPane1.setSelectedIndex(7);
+    } else {
+        this.jTabbedPane1.setSelectedIndex(0);
+        try {
+            //get all Aquarium data:
+            String qry = "SELECT * FROM Aquarium;";// NOI18N
+            DB.openConn();
+            ResultSet rs = DB.getQuery(qry);
+            while (rs.next()) {
+                String aquariumName = rs.getString("Name");// NOI18N
+                //compare with selected element
+                if (aquariumName.matches(
+                        jTree1.getLastSelectedPathComponent().toString())) {
+                    Global.AqID = rs.getInt("id");// NOI18N
+                    Ny.PopulateTable();
+
+                    Aquarium tank=Aquarium.getById(Global.AqID);
+                    aquariumNameTextField.setText(tank.getName());// NOI18N
+                    aquariumDeepTextField.setText(tank.getDepth());// NOI18N
+                    aquariumWidthTextField.setText(tank.getWidth());// NOI18N
+                    aquariumHeightTextField.setText(tank.getHeight());// NOI18N
+                    aquariumWaterVolTextField.setText(tank.getVolume());// NOI18N
+                    aquariumTypeTextField.setText(tank.getType());// NOI18N
+                    aquariumStartDateTextField.setDate(tank.getStartDate());// NOI18N
+                    aquariumEndDateTextField.setDate(tank.getEndDate());// NOI18N
+                    aquariumOCCheckBox.setSelected(tank.getOpen());
+                    aquariumTankVolTextField.setText(tank.getTankVolume());
+                    aquariumBottomTextField.setText(tank.getBottom());
+                    //try to load image
+                    BufferedImage img = null;
+                    aquariumImageLabelImg.setText(null);
+                    if (tank.hasImage()){
+                        aquariumImageLabelImg.setIcon((new javax.swing.ImageIcon(tank.getImage())));
+                    } else {
+                        aquariumImageLabelImg.setText(java.util.ResourceBundle.getBundle("nyagua/Bundle").getString("NO_IMAGE!"));
+                        aquariumImageLabelImg.setIcon(null);
+                    }
+                    Maintenance.populateList(maintList);
+                    FishBase.populateList(fishList);
+                    PlantBase.populateList(PlantsList);
                 }
-                Maintenance.populateList(maintList);
-                FishBase.populateList(fishList);
-                PlantBase.populateList(PlantsList);
             }
+            DB.closeConn();
+        } catch (SQLException ex) {
+            Logger.getLogger(Ny.class.getName()).log(Level.SEVERE, null, ex);
         }
-        DB.closeConn();
-    } catch (SQLException ex) {
-        Logger.getLogger(Ny.class.getName()).log(Level.SEVERE, null, ex);
-    }
+    }    
     if (jTree1.isRowSelected(0)) {
         Global.AqID = 0;
         Ny.PopulateTable();
      */
     public void PopulateTree() {
         /**Populate tree from db */
-        jTree1.setModel(null);
-        javax.swing.tree.DefaultMutableTreeNode treeNode1 =
-                new javax.swing.tree.DefaultMutableTreeNode(java.util.ResourceBundle.getBundle("nyagua/Bundle").getString("AQUARIUMS"));
-        jTree1.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
+        String treename="/";
+        if (Application.DATA_FILENAME!=null){
+            treename=Application.DATA_FILENAME;
+        }
+        jTree1.setModel(null);        
+        //javax.swing.tree.DefaultMutableTreeNode treeNode1 =new javax.swing.tree.DefaultMutableTreeNode(treename);
+        //root node
+        IconNode treeNode1=new IconNodeImpl(treename, new javax.swing.ImageIcon(getClass().getResource("/icons/database.png")), "");//NOI18N            
+        //jTree1.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
+        jTree1.setCellRenderer(new IconNodeRenderer());
+        jTree1.setModel(new DefaultTreeModel(treeNode1));//Apply custom gui-bits model structure
+        
+        
+        IconNode leaf1 = new IconNodeImpl(java.util.ResourceBundle.getBundle("nyagua/Bundle").getString("FISHBASE"), new javax.swing.ImageIcon(getClass().getResource("/icons/fish.png")),"");// NOI18N
+        treeNode1.add(leaf1);
+        //Fishbase leaf
+        IconNode leaf2 = new IconNodeImpl(java.util.ResourceBundle.getBundle("nyagua/Bundle").getString("PLANTSBASE"), new javax.swing.ImageIcon(getClass().getResource("/icons/plants.png")),"");// NOI18N
+        treeNode1.add(leaf2);
+        //PlantsBase leaf
+        IconNode leaf3 = new IconNodeImpl(java.util.ResourceBundle.getBundle("nyagua/Bundle").getString("AQUARIUMS"), new javax.swing.ImageIcon(getClass().getResource("/icons/aquarium.png")),"");// NOI18N
+        treeNode1.add(leaf3);
         String qry = "SELECT Name FROM Aquarium;";// NOI18N
         try {
             DB.openConn();
             ResultSet rs = DB.getQuery(qry);
             while (rs.next()) {
                 DefaultMutableTreeNode leaf = new DefaultMutableTreeNode(rs.getString("Name"));// NOI18N
-                treeNode1.add(leaf);
+                //treeNode1.add(leaf);
+                leaf3.add(leaf);
             }
             DB.closeConn();
         } catch (SQLException ex) {
             Logger.getLogger(Ny.class.getName()).log(Level.SEVERE, null, ex);
         }
         jTree1.expandRow(0);
+        jTree1.expandRow(3);
     }
 
     /**
     private javax.swing.JTextField utilityPhTextField1;
     private javax.swing.JRadioButton utilityPpmRadioButton;
     // End of variables declaration//GEN-END:variables
+
+    private static class IconNodeImpl extends IconNode {
+
+        public IconNodeImpl(Object userObject, Icon i, String t) {
+            super(userObject, i, t);
+        }
+
+        @Override
+        public void showPopupMenu(Component comp, int x, int y) {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        @Override
+        public void doubleClicked() {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+    }
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.