1 31 32 package org.antlr.works.debugger; 33 34 import com.jgoodies.forms.factories.Borders; 35 import com.jgoodies.forms.factories.FormFactory; 36 import com.jgoodies.forms.layout.*; 37 import org.antlr.works.prefs.AWPrefs; 38 import org.antlr.works.syntax.element.ElementRule; 39 import org.antlr.works.utils.TextUtils; 40 import org.antlr.works.utils.Utils; 41 import org.antlr.xjlib.appkit.frame.XJDialog; 42 import org.antlr.xjlib.appkit.utils.XJAlert; 43 import org.antlr.xjlib.appkit.utils.XJFileChooser; 44 import org.antlr.xjlib.foundation.XJSystem; 45 46 import javax.swing.*; 47 import java.awt.*; 48 import java.awt.event.ActionEvent ; 49 import java.awt.event.ActionListener ; 50 import java.util.prefs.Preferences ; 51 52 public class DebuggerInputDialog extends XJDialog { 53 54 public DebuggerInputDialog(Debugger debugger, Container parent) { 55 super(parent, true); 56 57 initComponents(); 58 setSize(600, 400); 59 60 if(XJSystem.isMacOS()) { 61 buttonBar.remove(okButton); 62 buttonBar.remove(cancelButton); 63 64 CellConstraints cc = new CellConstraints(); 65 buttonBar.add(cancelButton, cc.xy(2, 1)); 66 buttonBar.add(okButton, cc.xy(4, 1)); 67 } 68 69 setDefaultButton(okButton); 70 setOKButton(okButton); 71 setCancelButton(cancelButton); 72 73 TextUtils.createTabs(inputTextArea); 74 TextUtils.setDefaultTextPaneProperties(inputTextArea); 75 76 inputTextArea.setFont(new Font(AWPrefs.getEditorFont(), Font.PLAIN, AWPrefs.getEditorFontSize())); 77 inputTextArea.setFocusable(true); 78 inputTextArea.requestFocusInWindow(); 79 inputTextArea.setText(AWPrefs.getDebuggerInputText()); 80 81 rulesCombo.removeAllItems(); 82 for (ElementRule rule : debugger.getSortedRules()) { 83 rulesCombo.addItem(rule.name); 84 } 85 rulesCombo.setSelectedItem(AWPrefs.getStartSymbol()); 86 87 Utils.fillComboWithEOL(eolCombo); 88 eolCombo.setSelectedIndex(AWPrefs.getDebuggerEOL()); 89 90 if(AWPrefs.getDebuggerInputMode() == 0) { 91 textInputRadio.setSelected(true); 92 } else { 93 fileInputRadio.setSelected(true); 94 } 95 96 inputFileField.setText(AWPrefs.getDebuggerInputFile()); 97 98 browseInputFileButton.addActionListener(new ActionListener () { 99 public void actionPerformed(ActionEvent actionEvent) { 100 if(XJFileChooser.shared().displayOpenDialog(getContentPane(), false)) { 101 inputFileField.setText(XJFileChooser.shared().getSelectedFilePath()); 102 AWPrefs.setDebuggerInputFile(inputFileField.getText()); 103 } 104 } 105 }); 106 } 107 108 public void dialogWillCloseOK() { 109 String text = getRawInputText(); 110 if(text.length() > Preferences.MAX_VALUE_LENGTH) { 111 XJAlert.display(getJavaComponent(), "Error", "The input text is too large: "+text.length()+" bytes but preferences can only hold "+Preferences.MAX_VALUE_LENGTH+" bytes. It will be truncated."); 112 text = text.substring(0, Preferences.MAX_VALUE_LENGTH-1); 113 } 114 AWPrefs.setStartSymbol(getRule()); 115 AWPrefs.setDebuggerInputText(text); 116 AWPrefs.setDebuggerEOL(eolCombo.getSelectedIndex()); 117 AWPrefs.setDebuggerInputMode(textInputRadio.isSelected()?0:1); 118 AWPrefs.setDebuggerInputFile(inputFileField.getText()); 119 } 120 121 public int getInputMode() { 122 return textInputRadio.isSelected()?0:1; 123 } 124 125 public void setInputText(String text) { 126 if(text != null) { 127 inputTextArea.setText(text); 128 } 129 } 130 131 public String getRawInputText() { 132 return inputTextArea.getText(); 133 } 134 135 public String getInputText() { 136 return Utils.convertRawTextWithEOL(getRawInputText(), eolCombo); 137 } 138 139 public String getInputFile() { 140 return inputFileField.getText(); 141 } 142 143 public String getRule() { 144 return (String )rulesCombo.getSelectedItem(); 145 } 146 147 private void initComponents() { 148 dialogPane = new JPanel(); 151 contentPane = new JPanel(); 152 textInputRadio = new JRadioButton(); 153 scrollPane1 = new JScrollPane(); 154 inputTextArea = new JTextPane(); 155 fileInputRadio = new JRadioButton(); 156 inputFileField = new JTextField(); 157 browseInputFileButton = new JButton(); 158 label2 = new JLabel(); 159 rulesCombo = new JComboBox(); 160 label3 = new JLabel(); 161 eolCombo = new JComboBox(); 162 buttonBar = new JPanel(); 163 okButton = new JButton(); 164 cancelButton = new JButton(); 165 CellConstraints cc = new CellConstraints(); 166 167 setTitle("Input Text"); 169 Container contentPane2 = getContentPane(); 170 contentPane2.setLayout(new BorderLayout()); 171 172 { 174 dialogPane.setBorder(Borders.DIALOG_BORDER); 175 dialogPane.setLayout(new BorderLayout()); 176 177 { 179 contentPane.setLayout(new FormLayout( 180 new ColumnSpec[] { 181 new ColumnSpec(ColumnSpec.RIGHT, Sizes.DEFAULT, FormSpec.NO_GROW), 182 FormFactory.LABEL_COMPONENT_GAP_COLSPEC, 183 FormFactory.DEFAULT_COLSPEC, 184 FormFactory.LABEL_COMPONENT_GAP_COLSPEC, 185 new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW), 186 FormFactory.LABEL_COMPONENT_GAP_COLSPEC, 187 FormFactory.DEFAULT_COLSPEC 188 }, 189 new RowSpec[] { 190 FormFactory.DEFAULT_ROWSPEC, 191 FormFactory.LINE_GAP_ROWSPEC, 192 FormFactory.DEFAULT_ROWSPEC, 193 FormFactory.LINE_GAP_ROWSPEC, 194 new RowSpec(RowSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW), 195 FormFactory.LINE_GAP_ROWSPEC, 196 FormFactory.DEFAULT_ROWSPEC, 197 FormFactory.LINE_GAP_ROWSPEC, 198 FormFactory.DEFAULT_ROWSPEC, 199 FormFactory.LINE_GAP_ROWSPEC, 200 FormFactory.DEFAULT_ROWSPEC 201 })); 202 203 textInputRadio.setText("Text:"); 205 textInputRadio.setSelected(true); 206 contentPane.add(textInputRadio, cc.xy(1, 1)); 207 208 { 210 scrollPane1.setPreferredSize(new Dimension(300, 200)); 211 scrollPane1.setViewportView(inputTextArea); 212 } 213 contentPane.add(scrollPane1, cc.xywh(3, 1, 5, 5)); 214 215 fileInputRadio.setText("File:"); 217 contentPane.add(fileInputRadio, cc.xy(1, 7)); 218 contentPane.add(inputFileField, cc.xywh(3, 7, 3, 1)); 219 220 browseInputFileButton.setText("Browse"); 222 contentPane.add(browseInputFileButton, cc.xy(7, 7)); 223 224 label2.setText("Start Rule:"); 226 contentPane.add(label2, cc.xywh(1, 9, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT)); 227 contentPane.add(rulesCombo, cc.xywh(3, 9, 5, 1)); 228 229 label3.setText("Line Endings:"); 231 contentPane.add(label3, cc.xywh(1, 11, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT)); 232 233 eolCombo.setModel(new DefaultComboBoxModel(new String [] { 235 "Unix (LF)", 236 "Mac (CR)", 237 "Windows (CRLF)" 238 })); 239 contentPane.add(eolCombo, cc.xy(3, 11)); 240 } 241 dialogPane.add(contentPane, BorderLayout.CENTER); 242 243 { 245 buttonBar.setBorder(Borders.BUTTON_BAR_GAP_BORDER); 246 buttonBar.setLayout(new FormLayout( 247 new ColumnSpec[] { 248 FormFactory.GLUE_COLSPEC, 249 FormFactory.BUTTON_COLSPEC, 250 FormFactory.RELATED_GAP_COLSPEC, 251 FormFactory.BUTTON_COLSPEC 252 }, 253 RowSpec.decodeSpecs("pref"))); 254 255 okButton.setText("OK"); 257 buttonBar.add(okButton, cc.xy(2, 1)); 258 259 cancelButton.setText("Cancel"); 261 buttonBar.add(cancelButton, cc.xy(4, 1)); 262 } 263 dialogPane.add(buttonBar, BorderLayout.SOUTH); 264 } 265 contentPane2.add(dialogPane, BorderLayout.CENTER); 266 setSize(625, 395); 267 268 ButtonGroup buttonGroup1 = new ButtonGroup(); 270 buttonGroup1.add(textInputRadio); 271 buttonGroup1.add(fileInputRadio); 272 } 274 275 private JPanel dialogPane; 278 private JPanel contentPane; 279 private JRadioButton textInputRadio; 280 private JScrollPane scrollPane1; 281 private JTextPane inputTextArea; 282 private JRadioButton fileInputRadio; 283 private JTextField inputFileField; 284 private JButton browseInputFileButton; 285 private JLabel label2; 286 private JComboBox rulesCombo; 287 private JLabel label3; 288 private JComboBox eolCombo; 289 private JPanel buttonBar; 290 private JButton okButton; 291 private JButton cancelButton; 292 294 } 295 | Popular Tags |