001package jmri.jmrit.display.palette; 002 003import java.awt.Color; 004import java.awt.Dimension; 005import jmri.util.swing.JmriColorChooser; 006 007import javax.swing.BoxLayout; 008import javax.swing.JButton; 009import javax.swing.JColorChooser; 010import javax.swing.JLabel; 011import javax.swing.JPanel; 012import javax.swing.event.ChangeEvent; 013 014import jmri.jmrit.display.DisplayFrame; 015import jmri.jmrit.display.Editor; 016import jmri.util.swing.DrawSquares; 017import jmri.util.swing.ImagePanel; 018import jmri.util.swing.JmriJOptionPane; 019 020/** 021 * JPanels for the Panel Backgrounds. 022 */ 023public class BackgroundItemPanel extends IconItemPanel { 024 025 JColorChooser _chooser; 026 JButton _colorButton; 027 JPanel _colorPanel; 028 Color _color; 029 030 /** 031 * Constructor for background of icons or panel color. 032 * @param parentFrame ItemPalette instance 033 * @param type identifier of the ItemPanel type, should be "Background" 034 */ 035 public BackgroundItemPanel(DisplayFrame parentFrame, String type) { 036 super(parentFrame, type); 037 _level = Editor.BKG; 038 } 039 040 @Override 041 public void init() { 042 if (!_initialized) { 043 super.init(); 044 _colorPanel = makeColorPanel(); 045 add(_colorPanel); 046 } 047 } 048 049 @Override 050 protected JPanel instructions() { 051 JPanel blurb = new JPanel(); 052 blurb.setLayout(new BoxLayout(blurb, BoxLayout.Y_AXIS)); 053 blurb.add(new JLabel(Bundle.getMessage("DragIconPanel"))); 054 blurb.add(new JLabel(Bundle.getMessage("DragIconCatalog"))); 055 blurb.add(javax.swing.Box.createVerticalStrut(ItemPalette.STRUT_SIZE)); 056 JLabel label = new JLabel(Bundle.getMessage("BackgroundIcons")); 057 label.setForeground(Color.RED); 058 blurb.add(label); 059 JPanel panel = new JPanel(); 060 panel.add(blurb); 061 return panel; 062 } 063 064 @Override 065 protected JPanel makeItemButtonPanel() { 066 JPanel panel = super.makeItemButtonPanel(); 067 panel.add(makeColorButton()); 068 return panel; 069 } 070 071 @Override 072 protected JPanel makeSpecialBottomPanel(boolean update) { 073 JPanel _bottom2Panel = super.makeSpecialBottomPanel(update); 074 _bottom2Panel.add(makeColorButton(), 1); 075 return _bottom2Panel; 076 } 077 078 @Override 079 protected void initLinkPanel() { 080 } 081 082 @Override 083 protected void showCatalog() { 084 _colorPanel.setVisible(false); 085 _colorPanel.invalidate(); 086 super.showCatalog(); 087 088 } 089 private void showColorPanel() { 090 deselectIcon(); 091 _chooser.setColor(_frame.getCurrentColor()); 092 Dimension totalDim = _frame.getSize(); 093 Dimension oldDim = getSize(); 094 boolean isPalette = (_frame instanceof ItemPalette); 095 _catalog.setVisible(false); 096 _catalog.invalidate(); 097 _colorPanel.setVisible(true); 098 _colorPanel.invalidate(); 099 _bottomPanel.setVisible(false); 100 _bottomPanel.invalidate(); 101 reSizeDisplay(isPalette, oldDim, totalDim); 102 _colorButton.setText(Bundle.getMessage("HideColorPanel")); 103 } 104 105 private void hideColorPanel() { 106 _chooser.setColor(_frame.getCurrentColor()); 107 Dimension totalDim = _frame.getSize(); 108 Dimension oldDim = getSize(); 109 boolean isPalette = (_frame instanceof ItemPalette); 110 _colorPanel.setVisible(false); 111 _colorPanel.invalidate(); 112 _bottomPanel.setVisible(true); 113 _bottomPanel.invalidate(); 114 reSizeDisplay(isPalette, oldDim, totalDim); 115 _colorButton.setText(Bundle.getMessage("ShowColorPanel")); 116 } 117 118 private JButton makeColorButton() { 119 _colorButton = new JButton(Bundle.getMessage("ButtonShowColorPanel")); 120 _colorButton.addActionListener(a -> { 121 if (_colorPanel.isVisible()) { 122 hideColorPanel(); 123 } else { 124 showColorPanel(); 125 } 126 }); 127 _colorButton.setToolTipText(Bundle.getMessage("ToolTipColor")); 128 return _colorButton; 129 } 130 131 protected JPanel makeColorButtonPanel() { 132 JPanel panel = new JPanel(); 133 JButton button = new JButton(Bundle.getMessage("ButtonBackgroundColor")); 134 button.addActionListener(a -> setColor()); 135 button.setToolTipText(Bundle.getMessage("ToColorBackground", Bundle.getMessage("ButtonBackgroundColor"))); 136 panel.add(button); 137 138 button = new JButton(Bundle.getMessage("ButtonCancel")); 139 button.addActionListener(a -> hideColorPanel()); 140 button.setToolTipText(Bundle.getMessage("ToColorBackground", Bundle.getMessage("ButtonBackgroundColor"))); 141 panel.add(button); 142 panel.setToolTipText(Bundle.getMessage("ToColorBackground", Bundle.getMessage("ButtonBackgroundColor"))); 143 return panel; 144 } 145 146 private JPanel makeColorPanel() { 147 JPanel panel =new JPanel(); 148 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 149 _chooser = new JColorChooser(_frame.getCurrentColor()); 150 _chooser.setPreviewPanel(new JPanel()); 151 _chooser.getSelectionModel().addChangeListener((ChangeEvent e) -> colorChange()); 152 _chooser.setColor(_frame.getCurrentColor()); 153 JmriColorChooser.extendColorChooser(_chooser); 154 panel.add(_chooser); 155 156 panel.add(makeColorButtonPanel()); 157 panel.setToolTipText(Bundle.getMessage("ToColorBackground", Bundle.getMessage("ButtonBackgroundColor"))); 158 panel.setVisible(false); 159 return panel; 160 } 161 162 private void colorChange() { 163 _color = _chooser.getColor(); 164 _iconPanel.setImage(DrawSquares.getImage(500, 400, 10, _color, _color)); 165 _iconPanel.invalidate(); 166 } 167 168 private void setColor() { 169 if (_color == null) { 170 JmriJOptionPane.showMessageDialog(_frame, 171 Bundle.getMessage("ToColorBackground", Bundle.getMessage("ButtonBackgroundColor")), 172 Bundle.getMessage("ErrorTitle"), JmriJOptionPane.ERROR_MESSAGE); 173 return; 174 } 175 _frame.getEditor().setBackgroundColor(_color); 176 _frame.updateBackground(_frame.getEditor()); 177 hideColorPanel(); 178 } 179 180 @Override 181 protected void previewColorChange() { 182 if (_initialized) { 183 ImagePanel iconPanel = _catalog.getPreviewPanel(); 184 if (iconPanel != null) { 185 iconPanel.setImage(_frame.getPreviewBackground()); 186 } 187 _iconPanel.setImage(_frame.getPreviewBackground()); 188 _iconPanel.setImage(_frame.getBackground(0)); 189 } 190 } 191 192}