001    /* 
002     * Copyright (c) Holger Pfaff - http://pfaff.ws
003     *
004     * This software maybe used for any purpose provided the
005     * above copyright notice is retained. It is supplied as is.
006     * No warranty expressed or implied - Use at your own risk.
007     */
008    
009    import java.awt.*;
010    import java.awt.event.*;
011    import java.util.*;
012    
013    /**
014      * (#)Triage.java
015      * @author   Holger Pfaff
016      * @version  3.2 19-Mar-2004<br><br> 
017      *
018      * My version of the awt choice widget
019      */
020    
021    public class Triage extends MenuKnob {
022    
023      /**
024       * String to prefix currently displayed choice with e.g. to
025       * change font, color etc.
026       */
027      protected String prefix = "";
028    
029      /**
030       * Construct an empty triage
031       */
032      public Triage() {
033        super();
034      }
035      
036      /**
037       * Construct an empty triage
038       *
039       * @param   pf   string to use as prefix
040       */
041      public Triage(String pf) {
042        super();
043        prefix = pf;
044      }
045    
046      /**
047       * Construct triage using a string array as initializer
048       *
049       * @param   menuItems   Items for this triage
050       */
051      public Triage(String[] menuItems) {
052        super(menuItems);
053      }
054      
055      /**
056       * Construct triage using a string array as initializer
057       *
058       * @param   pf   string to use as prefix
059       * @param   menuItems   Items for this triage
060       */
061      public Triage(String pf, String[] menuItems) {
062        super(menuItems);
063        prefix = pf;
064      }
065      
066      /**
067       * Change the prefix
068       *
069       * @param   pf   string to use as prefix
070       */
071      public void setPrefix(String pf) {
072        pf = prefix;
073        repaint();
074      }
075      
076      /**
077       * Get the prefix
078       *
079       * @return   current prefix
080       */
081      public String getPrefix() {
082        return prefix;
083      }
084    
085      /**
086       * overwrite MenuKnob.paint()
087       *
088       * @param   g       Graphics object to use
089       */
090      public void paint(Graphics g) {
091        caption = prefix + getSelectedItem();
092        super.paint(g);
093      }
094    
095      /**
096       * overwrite MenuKnob.measureCaption()
097       *
098       * @return   size requested by current caption
099       */
100      public Dimension measureCaption() {
101        Dimension d = new Dimension();
102        for (Enumeration enum = menuItems.elements() ; enum.hasMoreElements() ;) {
103          Dimension sd = measureString(prefix + (String) enum.nextElement(), getFont());
104          d.width  = Math.max(d.width, sd.width);
105          d.height = Math.max(d.height, sd.height);
106        }
107        return d;
108      }
109    
110      /**
111       * overwrite MenuKnob.paintIcon() since we use a rectangle as handle
112       *
113       * @param   g       Graphics object to use
114       * @param   x       x-position of describing rect
115       * @param   y       y-position of describing rect
116       * @param   w       width of describing rect
117       * @param   h       height of describing rect
118       */
119      public void paintIcon(Graphics g, int x, int y, int w, int h) {
120        drawRectangle(g, getBackground(), x, y + w/4, w - 1, w/2 - 1, 1, RAISED);
121      }
122    }