Skip to content

Commit

Permalink
use parent pane setBg field
Browse files Browse the repository at this point in the history
pass tab index in onInit msg
  • Loading branch information
silverailscolo committed Dec 16, 2017
1 parent f39ec8b commit c3aec49
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 69 deletions.
4 changes: 2 additions & 2 deletions java/src/jmri/jmrit/display/palette/BackgroundItemPanel.java
Expand Up @@ -121,8 +121,8 @@ protected JPanel makeDoneButtonPanel() {
public void actionPerformed(ActionEvent a) {
_editor.setBackgroundColor(_chooser.getColor());
_currentBackground = _chooser.getColor();
_backgrounds[0] = DrawSquares.getImage(500, 400, 10, _currentBackground, _currentBackground);
_iconPanel.setImage(_backgrounds[0]);
_backgrounds[0] = DrawSquares.getImage(500, 400, 10, _currentBackground, _currentBackground); // replace panel color preview image in array
_iconPanel.setImage(_backgrounds[0]); // show new value in preview pane
dialog.dispose();
}

Expand Down
2 changes: 1 addition & 1 deletion java/src/jmri/jmrit/display/palette/DecoratorPanel.java
Expand Up @@ -557,7 +557,7 @@ private JPanel makeBgButtonPanel(@Nonnull ImagePanel preview1, ImagePanel previe
bgColorBox.addItem(Bundle.getMessage("LightGray"));
bgColorBox.addItem(Bundle.getMessage("DarkGray"));
bgColorBox.addItem(Bundle.getMessage("Checkers"));
bgColorBox.setSelectedIndex(previewBgSet); // starts as 0 = panel bg color, cannot read shared choice
bgColorBox.setSelectedIndex(previewBgSet); // starts as 0 = panel bg color, DecoratorPanel cannot read shared ItemPanel choice
bgColorBox.addActionListener((ActionEvent e) -> {
if (imgArray != null) {
if (previewBgSet != bgColorBox.getSelectedIndex()) {
Expand Down
28 changes: 15 additions & 13 deletions java/src/jmri/jmrit/display/palette/FamilyItemPanel.java
Expand Up @@ -55,7 +55,7 @@ public abstract class FamilyItemPanel extends ItemPanel {
protected HashMap<String, NamedIcon> _currentIconMap;
IconDialog _dialog;
ButtonGroup _familyButtonGroup;
protected JPanel bgBoxPanel; // panel with a combo box to set a contrasting background behind the icon preview
protected JPanel bgBoxPanel; // panel with a combo box to manually pick a contrasting background behind the icon preview

static boolean _suppressNamePrompts = false;

Expand Down Expand Up @@ -244,10 +244,10 @@ private void checkCurrentMap(HashMap<String, NamedIcon> iconMap) {
return;
} else { // no match with Palette families
if (ItemPalette.getIconMap(_itemType, _family) != null) {
// JOptionPane.showMessageDialog(_paletteFrame,
// Bundle.getMessage("DuplicateFamilyName", _family, _itemType),
// Bundle.getMessage("WarningTitle"), JOptionPane.WARNING_MESSAGE);
// make sure name does not duplicate a known name
// JOptionPane.showMessageDialog(_paletteFrame,
// Bundle.getMessage("DuplicateFamilyName", _family, _itemType),
// Bundle.getMessage("WarningTitle"), JOptionPane.WARNING_MESSAGE);
// make sure name does not duplicate a known name
_family = null;
}
}
Expand Down Expand Up @@ -477,14 +477,6 @@ protected void makeDragIconPanel(int position) {
if (_dragIconPanel == null) {
_dragIconPanel = new ImagePanel();
_dragIconPanel.setOpaque(true); // to show background color/squares
if (_backgrounds != null) {
_dragIconPanel.setImage(_backgrounds[previewBgSet]); // pick up shared setting
if (_iconPanel != null) {
_iconPanel.setImage(_backgrounds[previewBgSet]); // pick up shared setting
}
} else {
log.debug("FamilyItemPanel - no value for previewBgSet");
}
_dragIconPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black, 1),
Bundle.getMessage("PreviewBorderTitle")));
_dragIconPanel.setLayout(new FlowLayout());
Expand All @@ -493,6 +485,16 @@ protected void makeDragIconPanel(int position) {
} else {
_dragIconPanel.removeAll();
}
if (_backgrounds != null) {
int previewBgSet = _paletteFrame.getPreviewBg();
log.debug("set backgrounds to {}", previewBgSet);
_dragIconPanel.setImage(_backgrounds[previewBgSet]); // pick up shared setting
if (_iconPanel != null) {
_iconPanel.setImage(_backgrounds[previewBgSet]); // pick up shared setting
}
} else {
log.debug("FamilyItemPanel - no value for previewBgSet");
}
_dragIconPanel.setVisible(true);

// add a SetBackground combo
Expand Down
2 changes: 1 addition & 1 deletion java/src/jmri/jmrit/display/palette/IconItemPanel.java
Expand Up @@ -152,7 +152,7 @@ protected void addIconsToPanel(HashMap<String, NamedIcon> iconMap) {
} else { // clear existing to keep connection to combo
_iconPanel.removeAll();
}
_iconPanel.setImage(_backgrounds[previewBgSet]); // pick up shared setting
_iconPanel.setImage(_backgrounds[_paletteFrame.getPreviewBg()]); // pick up shared setting

JPanel iPanel = new JPanel();
iPanel.setOpaque(false);
Expand Down
2 changes: 1 addition & 1 deletion java/src/jmri/jmrit/display/palette/InitEventListener.java
Expand Up @@ -4,5 +4,5 @@
* Custom listener to respond to redisplay of a tabbed pane.
*/
public interface InitEventListener {
void onInitEvent(int choice);
void onInitEvent(int choice, int selectedPane);
}
6 changes: 3 additions & 3 deletions java/src/jmri/jmrit/display/palette/ItemPalette.java
Expand Up @@ -588,13 +588,13 @@ public void stateChanged(ChangeEvent e) {
JTabbedPane tp = (JTabbedPane) e.getSource();
JScrollPane sp = (JScrollPane) tp.getSelectedComponent();
ItemPanel p = (ItemPanel) sp.getViewport().getView();
p.init();
p.init(); // (re)initialize tab pane
log.debug("different tab displayed");
if (_currentItemPanel != null) {
_currentItemPanel.closeDialogs();
}
if (listener != null) listener.onInitEvent(super.getPreviewBg());
log.debug("tab redisplayed.");
if (listener != null) listener.onInitEvent(super.getPreviewBg(), _tabPane.getSelectedIndex()); // signal tab
log.debug("tab redisplayed, previewBgSet updated to {}", super.getPreviewBg());
_currentItemPanel = p;
pack();
}
Expand Down
93 changes: 48 additions & 45 deletions java/src/jmri/jmrit/display/palette/ItemPanel.java
Expand Up @@ -55,27 +55,23 @@ public abstract class ItemPanel extends JPanel {
JTextField _linkName = new JTextField(30);
static Color _grayColor = new Color(235, 235, 235);
static Color _darkGrayColor = new Color(150, 150, 150);
protected Color[] colorChoice = new Color[] {Color.white, _grayColor, _darkGrayColor}; // panel bg color picked up directly
protected Color[] colorChoice = new Color[]{Color.white, _grayColor, _darkGrayColor}; // panel bg color picked up directly
/**
* Active base color for Preview background, read from active Panel where available.
*/
protected Color _currentBackground = _grayColor;
/**
* Array of BufferedImage backgrounds loaded as background image in Preview, shared to save on RAM
* Array of BufferedImage backgrounds loaded as background image in Preview (not shared across tabs)
*/
protected BufferedImage[] _backgrounds;
/**
* Shared setting for preview pane background color combo choice. Starts as 0 = use current Panel bg color.
*/
protected int previewBgSet = 0;
private InitEventListener listener;

/**
* Constructor for all item types.
*
* @param parentFrame ItemPalette instance
* @param type identifier of the ItemPanel type
* @param editor Editor that called this ItemPalette
* @param parentFrame ItemPalette instance
* @param type identifier of the ItemPanel type
* @param editor Editor that called this ItemPalette
*/
public ItemPanel(DisplayFrame parentFrame, String type, Editor editor) {
_paletteFrame = (DisplayFrame) parentFrame;
Expand Down Expand Up @@ -110,13 +106,13 @@ protected void initLinkPanel() {

/**
* Create panel element containing [Set background:] drop down list.
*
* @see jmri.jmrit.catalog.PreviewDialog#setupPanel()
* @see DecoratorPanel
*
* @param preview1 preview pane1 to set background image on
* @param preview2 (optional) second preview pane1 to set background image on
* @param imgArray the image array to choose from
* @return JPanel with label and drop down with actions
* @param preview1 preview pane1 to set background image on
* @param preview2 (optional) second preview pane1 to set background image on
* @param imgArray the image array to choose from
* @return JPanel with label and drop down with actions
*/
protected JPanel makeBgButtonPanel(@Nonnull ImagePanel preview1, ImagePanel preview2, BufferedImage[] imgArray, DisplayFrame parent) {
BgComboBox<String> bgColorBox = new BgComboBox<>();
Expand All @@ -125,29 +121,24 @@ protected JPanel makeBgButtonPanel(@Nonnull ImagePanel preview1, ImagePanel prev
bgColorBox.addItem(Bundle.getMessage("LightGray"));
bgColorBox.addItem(Bundle.getMessage("DarkGray"));
bgColorBox.addItem(Bundle.getMessage("Checkers"));
bgColorBox.setSelectedIndex(previewBgSet); // Global field, starts as 0 = panel bg color
bgColorBox.setSelectedIndex(parent.getPreviewBg()); // Global field, starts as 0 = panel bg color
bgColorBox.addActionListener((ActionEvent e) -> {
if (imgArray != null) {
//previewBgSet = parent.getPreviewBg();
//if (previewBgSet != bgColorBox.getSelectedIndex()) {
previewBgSet = bgColorBox.getSelectedIndex();
parent.setPreviewBg(previewBgSet); // store user choice in field
// load background image
log.debug("ItemPalette setImage called {}", previewBgSet);
preview1.setImage(imgArray[previewBgSet]);
//preview1.setOpaque(false); // needed?
preview1.revalidate(); // force redraw
if (preview2 != null) {
preview2.setImage(imgArray[previewBgSet]);
//preview2.setOpaque(false); // needed?
preview2.revalidate(); // force redraw
}
//}
int previewBgSet = bgColorBox.getSelectedIndex();
parent.setPreviewBg(previewBgSet); // store user choice in field on parent
// load background image
log.debug("ItemPalette setImage called {}", previewBgSet);
preview1.setImage(imgArray[previewBgSet]);
preview1.revalidate(); // force redraw
if (preview2 != null) {
preview2.setImage(imgArray[previewBgSet]);
preview2.revalidate(); // force redraw
}
} else {
log.debug("imgArray is empty");
}
});
if (parent instanceof ItemPalette) { // if Panel is part of a ItemPalette JTabbedFrame
if (parent instanceof ItemPalette) { // if Panel is part of an ItemPalette JTabbedFrame
parent.setInitEventListener(bgColorBox); // register custom init (show) event
}
JPanel backgroundPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
Expand Down Expand Up @@ -178,7 +169,7 @@ protected void closeDialogs() {
}

protected void reset() {
// _paletteFrame.repaint();
// _paletteFrame.repaint();
}

protected final boolean isUpdate() {
Expand All @@ -192,26 +183,26 @@ protected final boolean isUpdate() {
* element names defined in xml/defaultPanelIcons.xml
*/
static final String[] TURNOUT = {"TurnoutStateClosed", "TurnoutStateThrown",
"BeanStateInconsistent", "BeanStateUnknown"};
"BeanStateInconsistent", "BeanStateUnknown"};
static final String[] SENSOR = {"SensorStateActive", "SensorStateInactive",
"BeanStateInconsistent", "BeanStateUnknown"};
"BeanStateInconsistent", "BeanStateUnknown"};
static final String[] SIGNALHEAD = {"SignalHeadStateRed", "SignalHeadStateYellow",
"SignalHeadStateGreen", "SignalHeadStateDark",
"SignalHeadStateHeld", "SignalHeadStateLunar",
"SignalHeadStateFlashingRed", "SignalHeadStateFlashingYellow",
"SignalHeadStateFlashingGreen", "SignalHeadStateFlashingLunar"};
"SignalHeadStateGreen", "SignalHeadStateDark",
"SignalHeadStateHeld", "SignalHeadStateLunar",
"SignalHeadStateFlashingRed", "SignalHeadStateFlashingYellow",
"SignalHeadStateFlashingGreen", "SignalHeadStateFlashingLunar"};
static final String[] LIGHT = {"StateOff", "StateOn",
"BeanStateInconsistent", "BeanStateUnknown"};
"BeanStateInconsistent", "BeanStateUnknown"};
static final String[] MULTISENSOR = {"SensorStateInactive", "BeanStateInconsistent",
"BeanStateUnknown", "first", "second", "third"};
"BeanStateUnknown", "first", "second", "third"};
// SIGNALMAST family is empty for now
static final String[] RPSREPORTER = {"active", "error"};
static final String[] ICON = {"Icon"};
static final String[] BACKGROUND = {"Background"};
static final String[] INDICATOR_TRACK = {"ClearTrack", "OccupiedTrack", "AllocatedTrack",
"PositionTrack", "DontUseTrack", "ErrorTrack"};
"PositionTrack", "DontUseTrack", "ErrorTrack"};
static final String[] PORTAL = {PortalIcon.HIDDEN, PortalIcon.VISIBLE, PortalIcon.PATH,
PortalIcon.TO_ARROW, PortalIcon.FROM_ARROW};
PortalIcon.TO_ARROW, PortalIcon.FROM_ARROW};

static private String[] getNames(String type) {
if (type.equals("Turnout")) {
Expand Down Expand Up @@ -241,6 +232,7 @@ static private String[] getNames(String type) {
return null;
}
}

static String redX = "resources/icons/misc/X-red.gif";

static protected HashMap<String, NamedIcon> makeNewIconMap(String type) {
Expand All @@ -264,16 +256,27 @@ static protected void checkIconMap(String type, HashMap<String, NamedIcon> map)
}
}

protected DisplayFrame getParentFrame() {
return _paletteFrame;
}

/**
* Class for bgColorCombo.
*
* @param <T> the type of combobox
*/
public class BgComboBox<T> extends JComboBox<T> implements InitEventListener {

public void onInitEvent(int choice) {
log.debug("InitEvent seen by comboBox, set to {}", choice);
setSelectedIndex(choice); // update bgColorBox when InitEvent happens.
/**
* Store value from parent when changed on another tab.
*
* @param choice index of colorBox, to be applied to preview backgrounds
*/
public void onInitEvent(int choice, int selectedPane) {
setVisible(false);
log.debug("InitEvent seen by tab#{} comboBox, was {}, set to {}", selectedPane, this.getSelectedIndex(), choice);
setSelectedIndex(choice); // update bgColorBox when tab shows (doesn't always work)
setVisible(true); // force redraw of updated selection
}
}

Expand Down
3 changes: 1 addition & 2 deletions java/src/jmri/jmrit/display/palette/ReporterItemPanel.java
Expand Up @@ -82,7 +82,7 @@ protected void initIconFamiliesPanel() {
//_iconFamilyPanel.add(_iconPanel); // On Reporter, no icon family to choose
}
if (_backgrounds != null) {
_dragIconPanel.setImage(_backgrounds[previewBgSet]); // pick up shared setting
_dragIconPanel.setImage(_backgrounds[_paletteFrame.getPreviewBg()]); // pick up shared setting
} else {
log.debug("ReporterItemPanel - no value for previewBgSet");
}
Expand All @@ -98,7 +98,6 @@ protected void makeBottomPanel(ActionListener doneAction) {
initIconFamiliesPanel();
add(_iconFamilyPanel);
// add a SetBackground combo
// TODO add indirect updating of panel upon display via previewBgSet
if (bgBoxPanel == null) {
bgBoxPanel = makeBgButtonPanel(_dragIconPanel, null, _backgrounds, _paletteFrame);
add(bgBoxPanel);
Expand Down
Expand Up @@ -98,7 +98,7 @@ protected void initIconFamiliesPanel() {
Bundle.getMessage("PreviewBorderTitle")));
}
if (_backgrounds != null) {
_iconPanel.setImage(_backgrounds[previewBgSet]); // pick up shared setting
_iconPanel.setImage(_backgrounds[_paletteFrame.getPreviewBg()]); // pick up shared setting
} else {
log.debug("SignalMastItemPanel - no value for previewBgSet");
}
Expand Down

0 comments on commit c3aec49

Please sign in to comment.