View Javadoc

1   /*
2    *******************************************************************************
3    * Copyright (c) 2005 Chris Rose and AIMedia
4    * All rights reserved. SwitchableSummaryPanel and the accompanying materials
5    * are made available under the terms of the Common Public License v1.0
6    * which accompanies this distribution, and is available at
7    * http://www.eclipse.org/legal/cpl-v10.html
8    * 
9    * Contributors:
10   *     Chris Rose
11   *******************************************************************************/
12  package ca.spaz.cron.summary;
13  
14  import java.util.*;
15  
16  import javax.swing.*;
17  
18  import com.aimedia.ui.IValueListUser;
19  
20  /***
21   * This is a generic summaryPanel subclass that allows for multiple subpanels
22   * for viewing nutrient summary information.  Subclasses of it can add and remove
23   * subpanels that will be displayed in a tabbed layout.
24   *  
25   * @deprecated No longer needed for UI
26   * @author Chris Rose
27   */
28  public abstract class SwitchableSummaryPanel extends SummaryPanel {
29  
30      private Map panelMap;
31      private Map iconMap;
32      private JComponent currentContent;
33      private JTabbedPane tabbedPane;
34  
35      /***
36       * Create a new instance.
37       */
38      protected SwitchableSummaryPanel() {
39          this(null);
40      }
41  
42      /***
43       * @param fieldOrdering
44       */
45      protected SwitchableSummaryPanel(Comparator fieldOrdering) {
46          super(fieldOrdering);
47          this.iconMap = new HashMap();
48          this.panelMap = new HashMap();
49      }
50  
51      /***
52       * @return Returns the tabbedPane.
53       */
54      private JTabbedPane getTabbedPane() {
55          if (null == tabbedPane) {
56              tabbedPane = new JTabbedPane(JTabbedPane.LEFT, JTabbedPane.SCROLL_TAB_LAYOUT);
57          }
58          return tabbedPane;
59      }
60  
61      /* (non-Javadoc)
62       * @see ca.spaz.cron.SummaryPanel#doValueUpdate(java.lang.String)
63       */
64      protected void doValueUpdate(String valueName) {
65          List vl = getValueList();
66          for (Iterator iter = panelMap.values().iterator(); iter.hasNext();) {
67              IValueListUser ivl = (IValueListUser) iter.next();
68              ivl.setValueList(vl);
69          }
70      }
71  
72      /***
73       * Add a new content panel to the summary panel.
74       * @param cpName the name (Human-usable) of the panel.  Will be used for the label
75       * of the panel.
76       * @param content the panel to add.  This <em>must</em> be a 
77       * <code>JComponent</code> subclass.
78       * @throws IllegalArgumentException if the content argument does not descend from
79       * <code>JComponent</code>.
80       */
81      protected final void addContentPane(String cpName, IValueListUser content) {
82          addContentPane(cpName, null, content);
83      }
84      
85      /***
86       * Add a new content panel to the summary panel.
87       * @param cpName the name (Human-usable) of the panel.  Will be used for the label
88       * of the panel.
89       * @param cpIcon an icon for display on the label of the component.  If null, no icon
90       * will appear.
91       * @param content the panel to add.  This <em>must</em> be a 
92       * <code>JComponent</code> subclass.
93       * @throws IllegalArgumentException if the content argument does not descend from
94       * <code>JComponent</code>.
95       */
96      protected final void addContentPane(String cpName, Icon cpIcon, IValueListUser content) {
97          if (!(content instanceof JComponent)) {
98              throw new IllegalArgumentException("Content must also be a Swing component");
99          }
100         panelMap.put(cpName, content);
101         iconMap.put(cpName, cpIcon);
102         getTabbedPane().addTab(cpName, cpIcon, (JComponent) content);
103     }
104     
105     /***
106      * Remove the named content pane from the summary panel.
107      * @param cpName the content pane to remove.
108      */
109     protected final void removeContentPane(String cpName) {
110         getTabbedPane().remove((JComponent) panelMap.remove(cpName));
111         iconMap.remove(cpName);
112     }
113     
114     /***
115      * Get a list of available content panes.
116      * @return a <code>List</code> of content pane names (<code>String</code>s)
117      */
118     protected final List getAvailableContentPanes() {
119         List ret = Collections.unmodifiableList(new ArrayList(panelMap.keySet()));
120         return ret;
121     }
122     
123     /***
124      * Get the icon for a given content pane.
125      * @param cpName the pane to retrieve the icon for.
126      * @return an <code>Icon</code> for the content pane, or <code>null</code> if
127      * there is none.
128      */
129     protected final Icon getIcon(String cpName) {
130         Icon ico = (Icon) iconMap.get(cpName);
131         return ico;
132     }
133     
134     /***
135      * Bring the selected content pane to the front.
136      * @param cpName the pane to select.
137      */
138     public void selectContentPane(String cpName) {
139         if (panelMap.size() > 1) {
140             JComponent cmp = (JComponent) panelMap.get(cpName);
141             if (cmp != null) {
142                 getTabbedPane().setSelectedComponent(cmp);
143             }
144         }
145     }
146     
147     /* (non-Javadoc)
148      * @see ca.spaz.cron.SummaryPanel#getContentPane(java.util.List)
149      */
150     protected JComponent getContentPane(List contentValues) {
151         if (panelMap.size() == 0) {
152             return super.getContentPane(contentValues);
153         } else if (panelMap.size() == 1) {
154             JComponent c = (JComponent) getTabbedPane().getComponentAt(0);
155             ((IValueListUser)c).setValueList(contentValues);
156             return c;
157         }
158         for (Iterator iter = panelMap.values().iterator(); iter.hasNext();) {
159             IValueListUser vl = (IValueListUser) iter.next();
160             vl.setValueList(contentValues);
161         }
162         return getTabbedPane();
163     }
164     
165 }