001package jmri.jmrit.display.controlPanelEditor; 002 003import java.awt.FlowLayout; 004 005import javax.swing.BorderFactory; 006import javax.swing.BoxLayout; 007import javax.swing.JButton; 008import javax.swing.JPanel; 009import javax.swing.JScrollPane; 010 011import jmri.InstanceManager; 012import jmri.jmrit.logix.OBlock; 013import jmri.jmrit.logix.OBlockManager; 014import jmri.util.swing.JmriJOptionPane; 015 016/** 017 * Abstract class for the editing frames of CircuitBulder 018 * 019 * @author Pete Cressman Copyright: Copyright (c) 2019 020 */ 021public abstract class EditFrame extends jmri.util.JmriJFrame { 022 023 protected OBlock _homeBlock; 024 protected final CircuitBuilder _parent; 025 protected boolean _canEdit = true; 026 protected boolean _suppressWarnings = false; 027 028 static int STRUT_SIZE = 10; 029 030 public EditFrame(String title, CircuitBuilder parent, OBlock block) { 031 super(false, false); 032 _parent = parent; 033 if (block != null) { 034 setTitle(java.text.MessageFormat.format(title, block.getDisplayName())); 035 _homeBlock = block; 036 } else { 037 setTitle(Bundle.getMessage("newCircuitItem")); 038 _homeBlock = new OBlock(InstanceManager.getDefault(OBlockManager.class).getAutoSystemName(), null); 039 } 040 041 addWindowListener(new java.awt.event.WindowAdapter() { 042 @Override 043 public void windowClosing(java.awt.event.WindowEvent e) { 044 closingEvent(true); 045 } 046 }); 047 addHelpMenu("package.jmri.jmrit.display.CircuitBuilder", true); 048 049 JPanel contentPane = new JPanel(); 050 contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); 051 javax.swing.border.Border padding = BorderFactory.createEmptyBorder(10, 5, 4, 5); 052 contentPane.setBorder(padding); 053 054 contentPane.add(new JScrollPane(makeContentPanel())); 055 setContentPane(contentPane); 056 057 pack(); 058 InstanceManager.getDefault(jmri.util.PlaceWindow.class).nextTo(_parent._editor, null, this); 059 setVisible(true); 060 } 061 062 protected abstract JPanel makeContentPanel(); 063 064 protected JPanel makeDoneButtonPanel() { 065 JPanel buttonPanel = new JPanel(); 066 buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS)); 067 JPanel panel = new JPanel(); 068 panel.setLayout(new FlowLayout()); 069 070 JButton doneButton = new JButton(Bundle.getMessage("ButtonDone")); 071 doneButton.addActionListener(a -> closingEvent(false)); 072 panel.add(doneButton); 073 buttonPanel.add(panel); 074 075 panel = new JPanel(); 076 panel.setLayout(new FlowLayout()); 077 panel.add(buttonPanel); 078 079 return panel; 080 } 081 082 protected void checkCircuitIcons(String editType) { 083 StringBuilder sb = new StringBuilder(); 084 String msg = _parent.checkForTrackIcons(_homeBlock, editType); 085 if (msg.length() > 0) { 086 _canEdit = false; 087 sb.append(msg); 088 sb.append("\n"); 089 } 090 msg = _parent.checkForPortals(_homeBlock, editType); 091 if (msg.length() > 0) { 092 _canEdit = false; 093 sb.append(msg); 094 sb.append("\n"); 095 } 096 msg = _parent.checkForPortalIcons(_homeBlock, editType); 097 if (msg.length() > 0) { 098 _canEdit = false; 099 sb.append(msg); 100 } 101 if (!_canEdit) { 102 JmriJOptionPane.showMessageDialog(this, sb.toString(), 103 Bundle.getMessage("incompleteCircuit"), JmriJOptionPane.INFORMATION_MESSAGE); 104 } 105 } 106 107 protected void clearListSelection() { 108 } 109 110 /** 111 * Close frame if editing cannot be done 112 * @return whether editing can be done 113 */ 114 protected boolean canEdit() { 115 if (!_canEdit) { 116 closingEvent(true, null); 117 } 118 return _canEdit; 119 } 120 121 protected abstract void closingEvent(boolean close); 122 123 protected boolean closingEvent(boolean close, String msg) { 124 if (msg != null && msg.length() > 0) { 125 if (close) { 126 JmriJOptionPane.showMessageDialog(this, msg, Bundle.getMessage("editCiruit"), JmriJOptionPane.INFORMATION_MESSAGE); 127 } else { 128 StringBuilder sb = new StringBuilder(msg); 129 sb.append(Bundle.getMessage("exitQuestion")); 130 int answer = JmriJOptionPane.showConfirmDialog(this, sb.toString(), Bundle.getMessage("continue"), 131 JmriJOptionPane.YES_NO_OPTION, JmriJOptionPane.QUESTION_MESSAGE); 132 if (answer != JmriJOptionPane.YES_OPTION ) { 133 return false; 134 } 135 } 136 } 137 _parent.closeCircuitBuilder(_homeBlock); 138 dispose(); 139 return true; 140 } 141}