home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 June / PCpro_2005_06.ISO / files / opensource / tvbrowser / TvBrowser_1.0.exe / plugins / PrintPlugin.jar / printplugin / ProgramIcon.class (.txt) < prev    next >
Encoding:
Java Class File  |  2004-09-19  |  7.7 KB  |  228 lines

  1. package printplugin;
  2.  
  3. import devplugin.Plugin;
  4. import devplugin.Program;
  5. import devplugin.ProgramFieldType;
  6. import java.awt.AlphaComposite;
  7. import java.awt.Color;
  8. import java.awt.Component;
  9. import java.awt.Composite;
  10. import java.awt.Graphics;
  11. import java.awt.Graphics2D;
  12. import java.io.IOException;
  13. import java.io.Reader;
  14. import java.util.ArrayList;
  15. import java.util.logging.Level;
  16. import java.util.logging.Logger;
  17. import javax.swing.Icon;
  18. import util.io.IOUtilities;
  19. import util.ui.MultipleFieldReader;
  20. import util.ui.TextAreaIcon;
  21.  
  22. public class ProgramIcon implements Icon {
  23.    private static Logger mLog;
  24.    private static final Composite NORMAL_COMPOSITE;
  25.    private static final Composite PALE_COMPOSITE;
  26.    private ProgramIconSettings mSettings;
  27.    private int mHeight;
  28.    private int mWidth;
  29.    private int mPreferredHeight;
  30.    private String mProgramTimeAsString;
  31.    private TextAreaIcon mTitleIcon;
  32.    private TextAreaIcon mDescriptionIcon;
  33.    private Icon[] mIconArr;
  34.    private Program mProgram;
  35.    private static final ProgramIconSettings DEFAULT_PROGRAM_ICON_SETTINGS;
  36.    // $FF: synthetic field
  37.    static Class class$printplugin$ProgramIcon;
  38.  
  39.    public ProgramIcon(Program prog) {
  40.       this(prog, (ProgramIconSettings)null, 100);
  41.    }
  42.  
  43.    public ProgramIcon(Program prog, ProgramIconSettings settings, int width) {
  44.       this.mHeight = 0;
  45.       this.mPreferredHeight = 0;
  46.       if (settings == null) {
  47.          this.mSettings = DEFAULT_PROGRAM_ICON_SETTINGS;
  48.       } else {
  49.          this.mSettings = settings;
  50.       }
  51.  
  52.       this.mWidth = width;
  53.       this.mTitleIcon = new TextAreaIcon((String)null, this.mSettings.getTitleFont(), width - this.mSettings.getTimeFieldWidth() - 5);
  54.       this.mDescriptionIcon = new TextAreaIcon((String)null, this.mSettings.getTextFont(), width - this.mSettings.getTimeFieldWidth() - 5);
  55.       this.mDescriptionIcon.setMaximumLineCount(3);
  56.       this.setProgram(prog, -1);
  57.    }
  58.  
  59.    public void setMaximumHeight(int height) {
  60.       this.setProgram(this.mProgram, height);
  61.    }
  62.  
  63.    private Icon[] getPluginIcons(Program program) {
  64.       ArrayList list = new ArrayList();
  65.       String[] iconPluginArr = this.mSettings.getProgramTableIconPlugins();
  66.       Plugin[] pluginArr = Plugin.getPluginManager().getInstalledPlugins();
  67.  
  68.       for(int i = 0; i < iconPluginArr.length; ++i) {
  69.          for(int j = 0; j < pluginArr.length; ++j) {
  70.             String className = pluginArr[j].getClass().getName();
  71.             if (iconPluginArr[i].equals(className)) {
  72.                Icon[] iconArr = pluginArr[j].getProgramTableIcons(program);
  73.                if (iconArr != null) {
  74.                   for(int k = 0; k < iconArr.length; ++k) {
  75.                      list.add(iconArr[k]);
  76.                   }
  77.                }
  78.             }
  79.          }
  80.       }
  81.  
  82.       Icon[] asArr = new Icon[list.size()];
  83.       list.toArray(asArr);
  84.       return asArr;
  85.    }
  86.  
  87.    private void setProgram(Program program, int maxHeight) {
  88.       Program oldProgram = this.mProgram;
  89.       this.mProgram = program;
  90.       boolean programChanged = oldProgram != program;
  91.       if (programChanged) {
  92.          this.mProgramTimeAsString = program.getTimeString();
  93.          this.mIconArr = this.getPluginIcons(program);
  94.          this.mTitleIcon.setText(program.getTitle());
  95.       }
  96.  
  97.       int titleHeight = this.mTitleIcon.getIconHeight();
  98.       int maxDescLines = 0;
  99.       if (maxHeight != -1) {
  100.          maxDescLines = (maxHeight - titleHeight) / this.mSettings.getTextFont().getSize();
  101.       }
  102.  
  103.       if (programChanged || maxDescLines != this.mDescriptionIcon.getMaximumLineCount()) {
  104.          this.mDescriptionIcon.setMaximumLineCount(maxDescLines);
  105.          ProgramFieldType[] infoFieldArr = this.mSettings.getProgramInfoFields();
  106.          Reader infoReader = new MultipleFieldReader(program, infoFieldArr);
  107.  
  108.          try {
  109.             this.mDescriptionIcon.setText(infoReader);
  110.          } catch (IOException exc) {
  111.             mLog.log(Level.WARNING, "Reading program info failed for " + program, exc);
  112.          }
  113.  
  114.          this.mHeight = this.mTitleIcon.getIconHeight() + this.mDescriptionIcon.getIconHeight();
  115.          this.mPreferredHeight = titleHeight + 3 * this.mSettings.getTextFont().getSize() + 10;
  116.          if (this.mHeight < this.mPreferredHeight) {
  117.             this.mPreferredHeight = this.mHeight;
  118.          }
  119.       }
  120.  
  121.    }
  122.  
  123.    public int getIconHeight() {
  124.       return this.mHeight;
  125.    }
  126.  
  127.    public int getIconWidth() {
  128.       return this.mWidth;
  129.    }
  130.  
  131.    public void paintIcon(Component component, Graphics g, int posX, int posY) {
  132.       g.translate(posX, posY);
  133.       int width = this.getIconWidth();
  134.       int height = this.mHeight;
  135.       Graphics2D grp = (Graphics2D)g;
  136.       if (this.mSettings.getPaintProgramOnAir() && this.mProgram.isOnAir()) {
  137.          int minutesAfterMidnight = IOUtilities.getMinutesAfterMidnight();
  138.          int progLength = this.mProgram.getLength();
  139.          int startTime = this.mProgram.getHours() * 60 + this.mProgram.getMinutes();
  140.          int elapsedMinutes;
  141.          if (minutesAfterMidnight < startTime) {
  142.             elapsedMinutes = 1440 + minutesAfterMidnight - startTime;
  143.          } else {
  144.             elapsedMinutes = minutesAfterMidnight - startTime;
  145.          }
  146.  
  147.          int progressY = 0;
  148.          if (progLength > 0) {
  149.             progressY = elapsedMinutes * height / progLength;
  150.          }
  151.  
  152.          grp.setColor(this.mSettings.getColorOnAir_dark());
  153.          grp.fillRect(1, 1, width - 2, progressY - 1);
  154.          grp.setColor(this.mSettings.getColorOnAir_light());
  155.          grp.fillRect(1, progressY, width - 2, height - progressY - 1);
  156.          grp.draw3DRect(0, 0, width - 1, height - 1, true);
  157.       }
  158.  
  159.       Plugin[] markedByPluginArr = this.mProgram.getMarkedByPlugins();
  160.       if (this.mSettings.getPaintPluginMarks() && markedByPluginArr.length != 0) {
  161.          grp.setColor(this.mSettings.getColorMarked());
  162.          grp.fill3DRect(0, 0, width, height, true);
  163.       }
  164.  
  165.       if (this.mSettings.getPaintExpiredProgramsPale() && this.mProgram.isExpired()) {
  166.          grp.setColor(Color.gray);
  167.       } else {
  168.          grp.setColor(Color.black);
  169.       }
  170.  
  171.       grp.setFont(this.mSettings.getTimeFont());
  172.       grp.drawString(this.mProgramTimeAsString, 1, this.mSettings.getTimeFont().getSize());
  173.       this.mTitleIcon.paintIcon(component, grp, this.mSettings.getTimeFieldWidth(), 0);
  174.       this.mDescriptionIcon.paintIcon(component, grp, this.mSettings.getTimeFieldWidth(), this.mTitleIcon.getIconHeight());
  175.       if (this.mSettings.getPaintExpiredProgramsPale() && this.mProgram.isExpired()) {
  176.          grp.setComposite(PALE_COMPOSITE);
  177.       }
  178.  
  179.       int x = width - 1;
  180.       int y = this.mTitleIcon.getIconHeight() + this.mDescriptionIcon.getIconHeight() + 18;
  181.       y = Math.min(y, height - 1);
  182.       if (this.mSettings.getPaintPluginMarks()) {
  183.          for(int i = 0; i < markedByPluginArr.length; ++i) {
  184.             Icon icon = markedByPluginArr[i].getMarkIcon();
  185.             if (icon != null) {
  186.                x -= icon.getIconWidth();
  187.                icon.paintIcon(component, grp, x, y - icon.getIconHeight());
  188.             }
  189.          }
  190.       }
  191.  
  192.       if (this.mIconArr != null) {
  193.          x = 2;
  194.          y = this.mSettings.getTimeFont().getSize() + 3;
  195.  
  196.          for(int i = 0; i < this.mIconArr.length; ++i) {
  197.             int iconHeight = this.mIconArr[i].getIconHeight();
  198.             if (y + iconHeight < this.mHeight) {
  199.                this.mIconArr[i].paintIcon(component, grp, x, y);
  200.                y += iconHeight + 2;
  201.             }
  202.          }
  203.       }
  204.  
  205.       if (this.mSettings.getPaintExpiredProgramsPale() && this.mProgram.isExpired()) {
  206.          grp.setComposite(NORMAL_COMPOSITE);
  207.       }
  208.  
  209.       g.translate(-posX, -posY);
  210.    }
  211.  
  212.    // $FF: synthetic method
  213.    static Class class$(String x0) {
  214.       try {
  215.          return Class.forName(x0);
  216.       } catch (ClassNotFoundException x1) {
  217.          throw new NoClassDefFoundError(x1.getMessage());
  218.       }
  219.    }
  220.  
  221.    static {
  222.       mLog = Logger.getLogger((class$printplugin$ProgramIcon == null ? (class$printplugin$ProgramIcon = class$("printplugin.ProgramIcon")) : class$printplugin$ProgramIcon).getName());
  223.       NORMAL_COMPOSITE = AlphaComposite.SrcOver;
  224.       PALE_COMPOSITE = AlphaComposite.getInstance(3, 0.5F);
  225.       DEFAULT_PROGRAM_ICON_SETTINGS = PrinterProgramIconSettings.create();
  226.    }
  227. }
  228.