View Javadoc

1   /*
2    *******************************************************************************
3    * Copyright (c) 2005 Chris Rose and AIMedia
4    * All rights reserved. SummaryPanel 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   
13  package ca.spaz.cron.summary;
14  
15  import java.awt.BorderLayout;
16  import java.util.*;
17  
18  import javax.swing.*;
19  
20  import com.aimedia.ui.IValueListUser;
21  
22  /***
23   * An abstract UI class for display of a set of field values.  Supports
24   * arbitrary numbers of rows in 3-column displays.
25   * 
26   * @deprecated No longer needed for UI.
27   * @author Chris Rose
28   */
29  public abstract class SummaryPanel extends JPanel {
30  
31      private Map valueHash;
32  
33  //    private Map valueLabels;
34  
35      private List values;
36  
37      private Map valueSuffixes;
38  
39  //    private int columns;
40  
41      private Comparator fieldOrdering;
42  
43      private Map valueLabelText;
44  
45      private SimpleSummaryContent content;
46  
47      /***
48       * Construct a summary panel with the default ordering of components.
49       */
50      protected SummaryPanel() {
51          this(null);
52      }
53      
54      /***
55       * Construct a summary panel with the specified number of columns and field ordering.
56       * 
57       * @param fieldOrdering The <code>Comparator</code> to sort the fields with.  If null,
58       * the field default ordering is the order they are added in.
59       */
60      protected SummaryPanel(Comparator fieldOrdering) {
61          super();
62  //        this.columns = columns;
63          valueHash = new HashMap();
64          values = new ArrayList();
65  //        valueLabels = new HashMap();
66          valueSuffixes = new HashMap();
67          valueLabelText = new HashMap();
68          this.fieldOrdering = fieldOrdering;
69          this.setBorder(BorderFactory.createEmptyBorder(4,4,4,4));
70          this.setLayout(new BorderLayout());
71      }
72  
73      /***
74       * Add a new value field to the component.  Sets a default value of "" (empty string)
75       * for the value.
76       * 
77       * @param valueName The name to use for both the label and as the key.  Must be unique.
78       * @param valueUnit The unit of measure (will be appended to the output value by default).
79       */
80      protected final void addValueField(String valueName, String valueUnit) {
81          addValueField(valueName, valueName, valueUnit);
82      }
83  
84      /***
85       * Add a new value field to the component.  Sets a default value of "" (empty string)
86       * for the value.
87       * 
88       * @param valueName The name to use for both the label and as the key.  Must be unique.
89       * @param labelText The text to be displayed on the label.
90       * @param valueUnit The unit of measure (will be appended to the output value by default).
91       */
92      protected final void addValueField(String valueName, String labelText, String valueUnit) {
93          values.add(valueName);
94          valueLabelText.put(valueName, labelText);
95          valueSuffixes.put(valueName, valueUnit);
96          setValue(valueName, "");
97      }
98  
99      /***
100      * Format a <code>Boolean</code> for display.  Defaults to toString(), but sublcasses
101      * can override this behaviour.
102      * @param valueName The name of the value to be formatted.
103      * @param value The value to format.
104      * @return a formatted String for value.
105      */
106     protected String doBooleanString(String valueName, Boolean value) {
107         return value.toString() + " " + getUnit(valueName);
108     }
109 
110     /***
111      * Format a <code>Double</code> for display.  Defaults to toString(), but sublcasses
112      * can override this behaviour.
113      * @param valueName The name of the value to be formatted.
114      * @param value The value to format.
115      * @return a formatted String for value.
116      */
117     protected String doDoubleString(String valueName, Double value) {
118         return value.toString() + " " + getUnit(valueName);
119     }
120 
121     /***
122      * Get the label text for a value.
123      * 
124      * @param valueName The name of the value to retrieve.
125      * @param value The actual value to display.
126      * @return a String with the value name and the value itself formatted according to the
127      * policy of this component.
128      */
129     protected final String doGetTextForValue(String valueName, Object value) {
130         String ret = "";
131         if (value instanceof Integer) {
132             Integer ival = (Integer) value;
133             ret = doIntegerString(valueName, ival);
134         } else if (value instanceof Double) {
135             Double dval = (Double) value;
136             ret = doDoubleString(valueName, dval);
137         } else if (value instanceof Boolean) {
138             Boolean bval = (Boolean) value;
139             ret = doBooleanString(valueName, bval);
140         } else if (value instanceof String) {
141             String sval = (String) value;
142             ret = doString(valueName, sval);
143         } else {
144             ret = value.toString();
145         }
146         return ret;
147     }
148 
149     /***
150      * Format a <code>Integer</code> for display.  Defaults to toString(), but sublcasses
151      * can override this behaviour.
152      * @param valueName The name of the value to be formatted.
153      * @param value The value to format.
154      * @return a formatted String for value.
155      */
156     protected String doIntegerString(String valueName, Integer value) {
157         return value.toString() + " " + getUnit(valueName);
158     }
159 
160     private void doSetValue(String valueName, Object value) {
161         valueHash.put(valueName, value);
162 //        relabelValue(valueName);
163         if (content != null) {
164             ((IValueListUser)content).setValueList(getValueList());
165         }
166         doValueUpdate(valueName);
167         revalidate();
168     }
169 
170     /***
171      * Format a <code>String</code> for display.  Defaults to unmodified, but sublcasses
172      * can override this behaviour.
173      * @param valueName The name of the value to be formatted.
174      * @param value The value to format.
175      * @return a formatted String for value.
176      */
177    protected String doString(String valueName, String value) {
178         return value + " " + getUnit(valueName);
179     }
180 
181     /***
182      * get the unit string for the value, with all the fixings.
183      * @param valueName the value to get the unit for.
184      * @return the unit stored in valueSuffixes, or an empty string if there was none.
185      */
186     protected final String getUnit(String valueName) {
187         String ret = (String) valueSuffixes.get(valueName);
188         return (ret == null) ? "" : ret;
189     }
190     
191     protected final String getText(String valueName) {
192         String ret = (String) valueLabelText.get(valueName);
193         return (ret == null) ? "" : ret;
194     }
195     
196     public final String getValueText(String valueName) {
197         return doGetTextForValue(valueName, valueHash.get(valueName));
198     }
199     
200     public final String getLabelText(String valueName) {
201         String ret = (String) valueLabelText.get(valueName);
202         if (ret == null) {
203             return valueName;
204         } else {
205             return ret;
206         }
207     }
208 
209     /***
210      * Subclass-specific behaviour on value settings occurs here.  By the time this
211      * is called, the value will already be set, but the UI will not have been updated
212      * yet.  Empty implementations of this are perfectly valid.
213      * 
214      * @param valueName the name of the value that was updated.
215      */
216     protected abstract void doValueUpdate(String valueName);
217 
218     /***
219      * Retrieve a <code>boolean</code> value. 
220      * @param valueName the name of the value to return.
221      * @return the value stored under <code>valueName</code>.  False if no boolean stored.
222      */
223     public final boolean getBooleanValue(String valueName) {
224         boolean ret = false;
225         Object o = valueHash.get(valueName);
226         if (o != null && o instanceof Boolean) {
227             ret = ((Boolean) o).booleanValue();
228         }
229         return ret;
230     }
231 
232     /***
233      * Retrieve a <code>double</code> value. 
234      * @param valueName the name of the value to return.
235      * @return the value stored under <code>valueName</code>.  
236      * <code>Double.NaN</code> if no double stored.
237      */
238     public final double getDoubleValue(String valueName) {
239         double ret = Double.NaN;
240         Object o = valueHash.get(valueName);
241         if (o != null && o instanceof Double) {
242             ret = ((Double) o).doubleValue();
243         }
244         return ret;
245     }
246 
247     /***
248      * Retrieve an <code>int</code> value. 
249      * @param valueName the name of the value to return.
250      * @return the value stored under <code>valueName</code>.  
251      * 0 if no int stored.
252      */
253     public final int getIntValue(String valueName) {
254         int ret = 0;
255         Object o = valueHash.get(valueName);
256         if (o != null && o instanceof Integer) {
257             ret = ((Integer) o).intValue();
258         }
259         return ret;
260     }
261 
262     /***
263      * Retrieve a <code>String</code> value. 
264      * @param valueName the name of the value to return.
265      * @return the value stored under <code>valueName</code>.  
266      * Empty string if no object stored.
267      */
268     public final String getStringValue(String valueName) {
269         String ret = "";
270         Object o = valueHash.get(valueName);
271         if (o != null) {
272             ret = o.toString();
273         }
274         return ret;
275     }
276 
277     /***
278      * @param valueName
279      */
280 //    private void relabelValue(String valueName) {
281 //        Object value = valueHash.get(valueName);
282 //        if (null == value) {
283 //            return;
284 //        }
285 //        JLabel lbl = (JLabel) valueLabels.get(valueName);
286 //        if (lbl == null) {
287 //            lbl = new JLabel();
288 //            lbl.setAlignmentX(JLabel.LEFT_ALIGNMENT);
289 //            valueLabels.put(valueName, lbl);
290 //        }
291 //        lbl.setText(doGetTextForValue(valueName, value));
292 //    }
293 
294     /***
295      * Regenerate the UI components.  Call this whenever the return
296      * value of getContentPane() would change.
297      */
298     protected final void generateContentPane() {
299         removeAll();
300         List intersect = getValueList();
301         add(getContentPane(intersect), BorderLayout.CENTER);
302     }
303     
304     /***
305      * Get a list of the values in this summary panel.
306      * @return a <code>List</code> containing all the values in this summary panel.
307      */
308     protected final List getValueList() {
309         List intersect = new ArrayList(values);
310         intersect.retainAll(valueHash.keySet());
311         if (null != fieldOrdering) {
312             Collections.sort(intersect, fieldOrdering);
313         }
314         return intersect;
315     }
316 
317     /***
318      * Overridable method to select the actual content panel of the display 
319      * component.
320      * @param contentValues The list of value tags that will be displayed in 
321      * this component.
322      * 
323      */
324     protected JComponent getContentPane(List contentValues) {
325         content = new SimpleSummaryContent(this);
326         content.setValueList(contentValues);
327 /*        int numlbls = contentValues.size();
328         int nrows = numlbls / columns;
329         if ((numlbls % columns) != 0) {
330             nrows++;
331         }
332 //        content.removeAll();
333         content.setLayout(new GridBagLayout());
334         GridBagConstraints gclbl = new GridBagConstraints();
335         GridBagConstraints gcval = new GridBagConstraints();
336         gclbl.ipadx=4;
337 //        gclbl.ipady=4;
338         gcval.ipadx=4;
339 //        gcval.ipady=4;
340         gclbl.insets=new Insets(2,2,2,6);
341         gcval.insets=new Insets(2,2,6,2);
342         gclbl.anchor=GridBagConstraints.WEST;
343         gcval.anchor=GridBagConstraints.EAST;
344         gclbl.weightx=1;
345         gcval.weightx=0.9;
346         gclbl.gridy = 0;
347         gcval.gridy = 0;
348         int col = 0;
349         for (Iterator iter = contentValues.iterator(); iter.hasNext();) {
350             String valueName = (String) iter.next();
351             JLabel vlbl = (JLabel) valueLabels.get(valueName);
352             content.add(makeRLabel(valueName), gclbl);
353             content.add(vlbl, gcval);
354             col+=2;
355             if (col % (2 * columns) == 0) {
356                 gcval.gridy++;
357                 gclbl.gridy++;
358             }
359         }*/
360         return content;
361     }
362     
363     /***
364      * Purge a value from the component.  Removes all versions of it.
365      * @param valueName
366      */
367     protected final void removeValueField(String valueName) {
368         values.remove(valueName);
369         valueSuffixes.remove(valueName);
370 //        valueLabels.remove(valueName);
371         valueHash.remove(valueName);
372 //        generateContentPane();
373     }
374 
375     /***
376      * Set a <code>boolean</code> value in the value set.
377      * @param valueName the name of the value to set.
378      * @param value the value to set.
379      */
380     public final void setValue(String valueName, boolean value) {
381         doSetValue(valueName, Boolean.valueOf(value));
382     }
383 
384     /***
385      * Set a <code>double</code> value in the value set.
386      * @param valueName the name of the value to set.
387      * @param value the value to set.
388      */
389     public final void setValue(String valueName, double value) {
390         doSetValue(valueName, new Double(value));
391     }
392 
393     /***
394      * Set an <code>int</code> value in the value set.
395      * @param valueName the name of the value to set.
396      * @param value the value to set.
397      */
398 
399     public final void setValue(String valueName, int value) {
400         doSetValue(valueName, new Integer(value));
401     }
402 
403     /***
404      * Set a <code>String</code> value in the value set.
405      * @param valueName the name of the value to set.
406      * @param value the value to set.
407      */
408     public final void setValue(String valueName, String value) {
409         doSetValue(valueName, value);
410     }
411 
412 }