source: ether_statistics/service/implementation/gov/noaa/pmel/sgt/beans/PropertyPanel.java @ 569

Last change on this file since 569 was 569, checked in by vmipsl, 12 years ago

Nouveau projet

File size: 14.6 KB
Line 
1/*
2 * $Id: PropertyPanel.java,v 1.4 2003/09/17 22:30:02 dwd Exp $
3 *
4 * This software is provided by NOAA for full, free and open release.  It is
5 * understood by the recipient/user that NOAA assumes no liability for any
6 * errors contained in the code.  Although this software is released without
7 * conditions or restrictions in its use, it is expected that appropriate
8 * credit be given to its author and to the National Oceanic and Atmospheric
9 * Administration should the software be included by the recipient as an
10 * element in other product development.
11 */
12
13package gov.noaa.pmel.sgt.beans;
14
15import java.awt.*;
16import javax.swing.*;
17import java.awt.event.ActionListener;
18import java.awt.event.FocusListener;
19import java.util.EventListener;
20import java.util.Vector;
21import java.text.DecimalFormat;
22import java.util.StringTokenizer;
23
24import gov.noaa.pmel.sgt.SGLabel;
25import gov.noaa.pmel.util.Rectangle2D;
26import gov.noaa.pmel.util.Range2D;
27import gov.noaa.pmel.util.Point2D;
28import gov.noaa.pmel.util.SoTRange;
29import gov.noaa.pmel.util.SoTPoint;
30import gov.noaa.pmel.util.IllegalTimeValue;
31import gov.noaa.pmel.util.GeoDate;
32import gov.noaa.pmel.sgt.swing.ColorSwatchIcon;
33
34/**
35 * @author Donald Denbo
36 * @version $Revision: 1.4 $, $Date: 2003/09/17 22:30:02 $
37 * @since 3.0
38 **/
39abstract class PropertyPanel extends JComponent implements DesignListener {
40  private PanelHolder pHolder_ = null;
41  private JLabel jLabel1 = new JLabel();
42  private JLabel jLabel3 = new JLabel();
43  private GridBagLayout gridBagLayout1 = new GridBagLayout();
44  private Font hdrFont_ = new Font("Dialog", 1, 9);
45  private Font textFont_ = new Font("Dialog", 0, 9);
46  private Insets lInset = new Insets(2, 1, 1, 3);
47  private Insets rInset = new Insets(2, 3, 1, 1);
48  private static DecimalFormat numberFormat_ = new DecimalFormat("#.##");
49  private String dateFormat_ = "yyyy-MM-dd hh:mm";
50
51  public PropertyPanel() {
52    try {
53      jbInit();
54    }
55    catch(Exception e) {
56      e.printStackTrace();
57    }
58  }
59
60  private void jbInit() throws Exception {
61    jLabel1.setText("Property");
62    jLabel1.setFont(hdrFont_);
63    jLabel1.setForeground(Color.black);
64    jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
65    this.setLayout(gridBagLayout1);
66    jLabel3.setText("Value");
67    jLabel3.setFont(hdrFont_);
68    jLabel3.setForeground(Color.black);
69    this.add(jLabel1,          new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
70            ,GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, lInset, 5, 5));
71    this.add(jLabel3,       new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0
72            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, rInset, 10, 5));
73  }
74
75  abstract void resetFields();
76
77  public void reset() {
78    this.removeAll();
79    resetFields();
80    try {
81      jbInit();
82    } catch (Exception e) {
83      e.printStackTrace();
84    }
85    create();
86    revalidate();
87  }
88
89  protected void addProperty(int row, String propertyName, JComponent component, boolean last) {
90    double weighty;
91    int left, right;
92    JLabel label = new JLabel(propertyName);
93    label.setHorizontalAlignment(SwingConstants.RIGHT);
94    label.setFont(textFont_);
95    component.setFont(textFont_);
96    if(last) {
97      weighty = 1.0;
98      left = GridBagConstraints.NORTHEAST;
99      right = GridBagConstraints.NORTHWEST;
100    } else {
101      weighty = 0.0;
102      left = GridBagConstraints.EAST;
103      right = GridBagConstraints.WEST;
104    }
105    this.add(label, new GridBagConstraints(0, row, 1, 1, 0.0, weighty,
106        left, GridBagConstraints.BOTH, lInset, 5, 5));
107    this.add(component, new GridBagConstraints(1, row, 1, 1, 1.0, weighty,
108        right, GridBagConstraints.HORIZONTAL, rInset, 5, 5));
109  }
110
111  abstract void update();
112  abstract void create();
113
114  protected JTextField createTextField(String text,
115                                       String action, EventListener listen,
116                                       boolean edit) {
117    JTextField tf = new JTextField(text);
118    tf.setActionCommand(action);
119    tf.setName(action);
120    tf.setEditable(edit);
121    if(listen != null) {
122      if(listen instanceof ActionListener) tf.addActionListener((ActionListener)listen);
123      if(listen instanceof FocusListener) tf.addFocusListener((FocusListener)listen);
124    }
125    return tf;
126  }
127
128  protected JLabel createLabel(String text) {
129    JLabel jl = new JLabel(text);
130    return jl;
131  }
132
133  protected JLabel createLabel(int value) {
134    return new JLabel(Integer.toString(value));
135  }
136
137  protected JCheckBox createCheckBox(boolean value,
138                                     String action, ActionListener listen) {
139    JCheckBox cb = new JCheckBox("", value);
140    cb.setHorizontalTextPosition(SwingConstants.LEFT);
141    cb.setActionCommand(action);
142    if(listen != null) cb.addActionListener(listen);
143    return cb;
144  }
145
146  protected JComboBox createComboBox(Vector list, int item,
147                                     String action, ActionListener listen,
148                                     boolean edit) {
149    return createComboBox(list.toArray(), item, action, listen, edit);
150  }
151
152  protected JComboBox createComboBox(Object[] list, int item,
153                                     String action, ActionListener listen,
154                                     boolean edit) {
155    JComboBox cb = new JComboBox(list);
156    cb.setSelectedIndex(item);
157    cb.setActionCommand(action);
158    cb.setEditable(edit);
159    cb.setEnabled(edit);
160    if(listen != null) cb.addActionListener(listen);
161    return cb;
162  }
163
164  protected JButton createSGLabel(SGLabel value,
165                                String action, ActionListener listen) {
166    if(value == null) return new JButton("null");
167    JButton jb = new JButton(value.getText());
168    jb.setFont(value.getFont());
169    jb.setForeground(value.getColor());
170//  jb.setHorizontalTextPosition(SwingConstants.LEFT);
171    jb.setActionCommand(action);
172    if(listen != null) jb.addActionListener(listen);
173    return jb;
174  }
175
176  protected Point2D.Double parsePoint2D(String value) {
177    StringTokenizer tok = new StringTokenizer(value, ",\t\n\r\f");
178    if(tok.countTokens() != 2) {
179      JOptionPane.showMessageDialog(this, "Four values required", "Illegal Response", JOptionPane.ERROR_MESSAGE);
180      return null;
181    }
182    double x = Double.parseDouble(tok.nextToken().trim());
183    double y = Double.parseDouble(tok.nextToken().trim());
184    return new Point2D.Double(x, y);
185  }
186
187  protected Point parsePoint(String value) {
188    StringTokenizer tok = new StringTokenizer(value, ",\t\n\r\f");
189    if(tok.countTokens() != 2) {
190      JOptionPane.showMessageDialog(this, "Two values required", "Illegal Response", JOptionPane.ERROR_MESSAGE);
191      return null;
192    }
193    int x = Integer.parseInt(tok.nextToken().trim());
194    int y = Integer.parseInt(tok.nextToken().trim());
195    return new Point(x, y);
196  }
197
198  protected Dimension parseDimension(String value) {
199    StringTokenizer tok = new StringTokenizer(value, ",\t\n\r\f");
200    if(tok.countTokens() != 2) {
201      JOptionPane.showMessageDialog(this, "Two values required", "Illegal Response", JOptionPane.ERROR_MESSAGE);
202      return null;
203    }
204    int width = Integer.parseInt(tok.nextToken().trim());
205    int height = Integer.parseInt(tok.nextToken().trim());
206    return new Dimension(width, height);
207  }
208
209  protected Rectangle2D parseBounds(String value) {
210    StringTokenizer tok = new StringTokenizer(value, ",\t\n\r\f");
211    if(tok.countTokens() != 4) {
212      JOptionPane.showMessageDialog(this, "Four values required", "Illegal Response", JOptionPane.ERROR_MESSAGE);
213      return null;
214    }
215    double x = Double.parseDouble(tok.nextToken().trim());
216    double y = Double.parseDouble(tok.nextToken().trim());
217    double width = Double.parseDouble(tok.nextToken().trim());
218    double height = Double.parseDouble(tok.nextToken().trim());
219    return new Rectangle2D.Double(x, y, width, height);
220  }
221
222  protected SoTRange parseRange(String value, boolean isTime) {
223    StringTokenizer tok = new StringTokenizer(value, ",\t\n\r\f");
224    if(tok.countTokens() != 3) {
225      JOptionPane.showMessageDialog(this, "Three values required", "Illegal Response", JOptionPane.ERROR_MESSAGE);
226      return null;
227    }
228    SoTRange range = null;
229    if(isTime) {
230//      String format = "yyyy-MM-dd hh:mm";
231      try {
232        GeoDate start = new GeoDate(tok.nextToken().trim(), dateFormat_);
233        GeoDate end = new GeoDate(tok.nextToken().trim(), dateFormat_);
234        long dlta = Long.parseLong(tok.nextToken().trim())*86400000;
235        GeoDate delta = new GeoDate(dlta);
236        range = new SoTRange.Time(start, end, delta);
237      } catch (IllegalTimeValue itv) {
238        JOptionPane.showMessageDialog(this, "Illegal Time Value", "Illegal Response", JOptionPane.ERROR_MESSAGE);
239        return null;
240      }
241    } else {
242      double start = Double.parseDouble(tok.nextToken().trim());
243      double end = Double.parseDouble(tok.nextToken().trim());
244      double delta  = Double.parseDouble(tok.nextToken().trim());
245      range = new SoTRange.Double(start, end, delta);
246    }
247    return range;
248  }
249
250  protected String colorString(Color value) {
251    StringBuffer sbuf = new StringBuffer("[");
252    sbuf.append(value.getRed()).append(", ");
253    sbuf.append(value.getGreen()).append(", ");
254    sbuf.append(value.getRed());
255    if(value.getAlpha() == 255) {
256      sbuf.append("]");
257    } else {
258      sbuf.append(", ").append(value.getAlpha()).append("]");
259    }
260    return sbuf.toString();
261  }
262
263  protected JButton createColor(Color value,
264                                String action, ActionListener listen) {
265    Icon icon = new ColorSwatchIcon(value, 32, 20);
266    JButton jb = new JButton(colorString(value), icon);
267    jb.setActionCommand(action);
268    if(listen != null) jb.addActionListener(listen);
269    return jb;
270  }
271
272  protected void updateColor(JButton comp, Color value) {
273    comp.setText(colorString(value));
274    Icon icon = new ColorSwatchIcon(value, 32, 20);
275    comp.setIcon(icon);
276  }
277
278  protected JButton createFont(Font value,
279                                String action, ActionListener listen) {
280    JButton jb = new JButton(value.getName());
281    jb.setFont(value);
282//  jb.setHorizontalTextPosition(SwingConstants.LEFT);
283    jb.setActionCommand(action);
284    if(listen != null) jb.addActionListener(listen);
285    return jb;
286  }
287
288  protected void updateFont(JButton comp, Font value) {
289    comp.setText(value.getName());
290    comp.setFont(value);
291  }
292  protected void updateSGLabel(JButton comp, SGLabel value) {
293    comp.setFont(value.getFont());
294    comp.setText(value.getText());
295    comp.setForeground(value.getColor());
296  }
297
298  protected String format(Point val, boolean brackets) {
299    StringBuffer buf = new StringBuffer();
300    if(brackets) buf.append("[");
301    buf.append(format(val.x)).append(", ");
302    buf.append(format(val.y));
303    if(brackets) buf.append("]");
304    return buf.toString();
305  }
306
307  protected String format(Dimension val, boolean brackets) {
308    StringBuffer buf = new StringBuffer();
309    if(brackets) buf.append("[");
310    buf.append(format(val.width)).append(", ");
311    buf.append(format(val.height));
312    if(brackets) buf.append("]");
313    return buf.toString();
314  }
315
316  protected String format(Rectangle2D.Double val, boolean brackets) {
317    StringBuffer buf = new StringBuffer();
318    if(brackets) buf.append("[");
319    buf.append(format(val.x)).append(", ");
320    buf.append(format(val.y)).append(", ");
321    buf.append(format(val.width)).append(", ");
322    buf.append(format(val.height));
323    if(brackets) buf.append("]");
324    return buf.toString();
325  }
326
327  protected String format(Point2D.Double val, boolean brackets) {
328    StringBuffer buf = new StringBuffer();
329    if(brackets) buf.append("[");
330    buf.append(format(val.x)).append(", ");
331    buf.append(format(val.y));
332    if(brackets) buf.append("]");
333    return buf.toString();
334  }
335
336  protected String format(Range2D val, boolean brackets) {
337    StringBuffer buf = new StringBuffer();
338    if(brackets) buf.append("[");
339    buf.append(format(val.start)).append(", ");
340    buf.append(format(val.end)).append(", ");
341    buf.append(format(val.delta));
342    if(brackets) buf.append("]");
343    return buf.toString();
344  }
345
346  protected String format(SoTRange.Double val, boolean brackets) {
347    StringBuffer buf = new StringBuffer();
348    if(brackets) buf.append("[");
349    buf.append(format(val.start)).append(", ");
350    buf.append(format(val.end)).append(", ");
351    buf.append(format(val.delta));
352    if(brackets) buf.append("]");
353    return buf.toString();
354  }
355
356  protected String format(SoTRange.Time val, boolean brackets) {
357    StringBuffer buf = new StringBuffer();
358    if(brackets) buf.append("[");
359    buf.append(val.getStart().toString()).append(", ");
360    buf.append(val.getEnd().toString()).append(", ");
361    buf.append(Long.toString(val.getDelta().getLongTime()/86400000)); // days
362    if(brackets) buf.append("]");
363    return buf.toString();
364  }
365
366  protected String format(SoTRange val, boolean brackets) {
367    if(val instanceof SoTRange.Float) {
368      return format((SoTRange.Float)val, brackets);
369    } else if(val instanceof SoTRange.Double) {
370      return format((SoTRange.Double)val, brackets);
371    } else if(val instanceof SoTRange.Time) {
372      return format((SoTRange.Time)val, brackets);
373    }
374    return "";
375  }
376
377  protected String format(SoTPoint val, boolean brackets) {
378    if(val == null) return "null";
379    return val.toString();
380  }
381
382  protected String format(float value) {
383    return numberFormat_.format(value);
384  }
385
386  protected String format(float value, DecimalFormat format) {
387    return format.format(value);
388  }
389
390  protected String format(double value, DecimalFormat format) {
391    return format.format(value);
392  }
393
394  protected String format(double value) {
395    DecimalFormat format = new DecimalFormat("#.##");
396    return format.format(value);
397  }
398
399  protected void paintBorder(Graphics g) {
400    int x0=0, x1=0, x2=0;
401    int y;
402    int y1=0;
403    int y0=0;
404
405    Component[] comps = getComponents();
406
407    for(int i=0;  i < comps.length;i += 2) {
408      if(i == 0) {
409        x0 = comps[0].getBounds().x - lInset.left;
410        x1 = comps[0].getBounds().x + comps[0].getBounds().width + lInset.right;
411        x2 = comps[1].getBounds().x + comps[1].getBounds().width;
412        y0 = comps[0].getBounds().y - lInset.top;
413      }
414      y = comps[i].getBounds().y - lInset.top;
415      if(i >= comps.length - 2) {
416        y1 = y;
417      }
418      g.drawLine(0, y, x2, y);
419    }
420    g.drawLine(x0, y0, x0, y1);
421    g.drawLine(x1, y0, x1, y1);
422    g.drawLine(x2, y0, x2, y1);
423  }
424
425  public String toString() {
426    return getClass().getName() + '@' + Integer.toHexString(hashCode());
427  }
428
429  Frame getFrame() {
430    Window fr = javax.swing.SwingUtilities.getWindowAncestor(this);
431    if(fr != null && fr instanceof Frame) {
432      return (Frame)fr;
433    }
434    return null;
435  }
436
437  abstract public void setExpert(boolean expert);
438  abstract public boolean isExpert();
439}
Note: See TracBrowser for help on using the repository browser.