View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package p3j.gui.panels.dboverview;
17  
18  import java.awt.BorderLayout;
19  import java.util.List;
20  import java.util.logging.Level;
21  
22  import javax.swing.JPanel;
23  import javax.swing.JScrollPane;
24  import javax.swing.JTree;
25  import javax.swing.event.TreeSelectionEvent;
26  import javax.swing.event.TreeSelectionListener;
27  import javax.swing.tree.DefaultTreeModel;
28  import javax.swing.tree.TreePath;
29  
30  import org.jamesii.SimSystem;
31  
32  import p3j.database.DatabaseFactory;
33  import p3j.database.IP3MDatabase;
34  import p3j.gui.panels.AbstractNavigationPanel;
35  import p3j.gui.panels.projections.ProjectionTreeNode;
36  import p3j.pppm.ProjectionModel;
37  
38  /**
39   * Panel to display an overview of the database, e.g. to facilitate loading.
40   * 
41   * @author Christina Bohk
42   * @author Roland Ewald
43   * 
44   */
45  public class DatabaseOverviewPanel extends AbstractNavigationPanel {
46  
47    /** Serialization ID. */
48    private static final long serialVersionUID = 7431882091363418297L;
49  
50    /** Root of the database overview tree. */
51    private DatabaseNode root;
52  
53    /** Reference to the database. */
54    private IP3MDatabase db;
55  
56    /**
57     * Instantiates a new database overview panel.
58     * 
59     * @param contentP
60     *          the content panel
61     */
62    public DatabaseOverviewPanel(JPanel contentP) {
63      super(null, contentP);
64      db = DatabaseFactory.getDatabaseSingleton();
65      initTree();
66      setLayout(new BorderLayout());
67      add(getScrollPane(), BorderLayout.CENTER);
68    }
69  
70    @Override
71    protected final void initTree() {
72  
73      // We do not need to be re-initialized
74      if (root != null) {
75        return;
76      }
77  
78      root = new DatabaseNode(DatabaseFactory.getDbConnData(), DatabaseFactory
79          .getDbConnData().getURL());
80      setTreeModel(new DefaultTreeModel(root));
81      setTree(new JTree(getTreeModel()));
82      getTree().setRootVisible(true);
83      setScrollPane(new JScrollPane(getTree()));
84  
85      totalRefresh();
86  
87      getTree().addTreeSelectionListener(new TreeSelectionListener() {
88        @Override
89        public void valueChanged(TreeSelectionEvent e) {
90          TreePath oldPath = e.getOldLeadSelectionPath();
91          if (oldPath != null) {
92            ((ProjectionTreeNode<?>) oldPath.getLastPathComponent()).deselected();
93          }
94          getContentPanel().removeAll();
95          TreePath newPath = e.getNewLeadSelectionPath();
96          if (newPath != null) {
97            ((ProjectionTreeNode<?>) newPath.getLastPathComponent()).selected(
98                newPath, getContentPanel(), null);
99          }
100         getContentPanel().repaint();
101         getContentPanel().updateUI();
102       }
103     });
104   }
105 
106   /**
107    * Refreshes database information.
108    */
109   public final void totalRefresh() {
110     for (ProjectionTreeNode<?> child : root.getChilds()) {
111       getTreeModel().removeNodeFromParent(child);
112     }
113 
114     List<ProjectionModel> projections = null;
115 
116     try {
117       projections = db.getAllProjections();
118     } catch (Exception ex) {
119       SimSystem.report(Level.SEVERE,
120           "Could not read projections list from data base.", ex);
121       projections = null;
122     }
123 
124     if (projections == null) {
125       return;
126     }
127 
128     for (ProjectionModel projection : projections) {
129       root.add(new OverviewProjectionNode(projection, projection.getName()));
130     }
131 
132     getTreeModel().nodeStructureChanged(root);
133     getTree().repaint();
134   }
135 
136 }