001package jmri.jmrit.display.palette; 002 003import java.awt.Color; 004import java.awt.datatransfer.DataFlavor; 005import java.awt.datatransfer.UnsupportedFlavorException; 006import java.io.IOException; 007import java.util.HashMap; 008 009import javax.swing.Box; 010import javax.swing.BoxLayout; 011import javax.swing.JLabel; 012import javax.swing.JPanel; 013import jmri.jmrit.catalog.DragJLabel; 014import jmri.jmrit.catalog.NamedIcon; 015import jmri.jmrit.display.AnalogClock2Display; 016import jmri.jmrit.display.DisplayFrame; 017import jmri.jmrit.display.Editor; 018import org.slf4j.Logger; 019import org.slf4j.LoggerFactory; 020 021/** 022 * ItemPanel for Clocks. 023 */ 024public class ClockItemPanel extends IconItemPanel { 025 026 public ClockItemPanel(DisplayFrame parentFrame, String type) { 027 super(parentFrame, type); 028 setToolTipText(Bundle.getMessage("ToolTipDragIcon")); 029 } 030 031 @Override 032 protected JPanel instructions() { 033 JPanel blurb = new JPanel(); 034 blurb.setLayout(new BoxLayout(blurb, BoxLayout.Y_AXIS)); 035 blurb.add(Box.createVerticalStrut(ItemPalette.STRUT_SIZE)); 036 blurb.add(new JLabel(Bundle.getMessage("AddClockToPanel", Bundle.getMessage("FastClock")))); 037 blurb.add(Box.createVerticalStrut(ItemPalette.STRUT_SIZE)); 038 JPanel panel = new JPanel(); 039 panel.add(blurb); 040 return panel; 041 } 042 043 @Override 044 public void init() { 045 if (!_initialized) { 046 initIconFamiliesPanel(); 047 initLinkPanel(); 048 add(Box.createVerticalGlue()); 049 } 050 _previewPanel.invalidate(); 051 hideIcons(); 052 } 053 054 @Override 055 protected JPanel makeIconDisplayPanel(String key, HashMap<String, NamedIcon> iconMap, boolean dropIcon) { 056 NamedIcon icon = iconMap.get(key); 057 JPanel panel = new JPanel(); 058 panel.setOpaque(false); 059 JLabel label; 060 if (dropIcon) { 061 try { 062 label = new ClockDragJLabel(new DataFlavor(Editor.POSITIONABLE_FLAVOR), icon); 063 } catch (ClassNotFoundException cnfe) { 064 label = new JLabel(cnfe.toString()); 065 log.error("Unable to find class supporting {}", Editor.POSITIONABLE_FLAVOR, cnfe); 066 } 067 } else { 068 label = new JLabel(icon); 069 } 070 if (icon.getIconWidth() < 1 || icon.getIconHeight() < 1) { 071 label.setText(Bundle.getMessage("invisibleIcon")); 072 label.setForeground(Color.lightGray); 073 } 074 wrapIconImage(icon, label, panel, key); 075 return panel; 076 } 077 078 public class ClockDragJLabel extends DragJLabel { 079 080 public ClockDragJLabel(DataFlavor flavor, NamedIcon icon) { 081 super(flavor, icon); 082 } 083 084 @Override 085 public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { 086 if (!isDataFlavorSupported(flavor)) { 087 return null; 088 } 089 String url = ((NamedIcon) getIcon()).getURL(); 090 log.debug("DragJLabel.getTransferData url= {}", url); 091 if (flavor.isMimeTypeEqual(Editor.POSITIONABLE_FLAVOR)) { 092 AnalogClock2Display c; 093 String link = _linkName.getText().trim(); 094 if (link.length() == 0) { 095 c = new AnalogClock2Display(_frame.getEditor()); 096 } else { 097 c = new AnalogClock2Display(_frame.getEditor(), link); 098 } 099 c.setOpaque(false); 100 c.update(); 101 c.setLevel(Editor.CLOCK); 102 return c; 103 } else if (DataFlavor.stringFlavor.equals(flavor)) { 104 return _itemType + " icon \"" + url + "\""; 105 } 106 return null; 107 } 108 } 109 110 private final static Logger log = LoggerFactory.getLogger(ClockItemPanel.class); 111 112}