KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > debugger > DebuggerInputDialog


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

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 JavaDoc;
49 import java.awt.event.ActionListener JavaDoc;
50 import java.util.prefs.Preferences JavaDoc;
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 JavaDoc() {
99             public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc 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 JavaDoc text) {
126         if(text != null) {
127             inputTextArea.setText(text);
128         }
129     }
130
131     public String JavaDoc getRawInputText() {
132         return inputTextArea.getText();
133     }
134
135     public String JavaDoc getInputText() {
136         return Utils.convertRawTextWithEOL(getRawInputText(), eolCombo);
137     }
138
139     public String JavaDoc getInputFile() {
140         return inputFileField.getText();
141     }
142
143     public String JavaDoc getRule() {
144         return (String JavaDoc)rulesCombo.getSelectedItem();
145     }
146
147     private void initComponents() {
148         // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
149
// Generated using JFormDesigner Open Source Project license - ANTLR (www.antlr.org)
150
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         //======== this ========
168
setTitle("Input Text");
169         Container contentPane2 = getContentPane();
170         contentPane2.setLayout(new BorderLayout());
171
172         //======== dialogPane ========
173
{
174             dialogPane.setBorder(Borders.DIALOG_BORDER);
175             dialogPane.setLayout(new BorderLayout());
176
177             //======== contentPane ========
178
{
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 ----
204
textInputRadio.setText("Text:");
205                 textInputRadio.setSelected(true);
206                 contentPane.add(textInputRadio, cc.xy(1, 1));
207
208                 //======== scrollPane1 ========
209
{
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 ----
216
fileInputRadio.setText("File:");
217                 contentPane.add(fileInputRadio, cc.xy(1, 7));
218                 contentPane.add(inputFileField, cc.xywh(3, 7, 3, 1));
219
220                 //---- browseInputFileButton ----
221
browseInputFileButton.setText("Browse");
222                 contentPane.add(browseInputFileButton, cc.xy(7, 7));
223
224                 //---- label2 ----
225
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 ----
230
label3.setText("Line Endings:");
231                 contentPane.add(label3, cc.xywh(1, 11, 1, 1, CellConstraints.RIGHT, CellConstraints.DEFAULT));
232
233                 //---- eolCombo ----
234
eolCombo.setModel(new DefaultComboBoxModel(new String JavaDoc[] {
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             //======== buttonBar ========
244
{
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 ----
256
okButton.setText("OK");
257                 buttonBar.add(okButton, cc.xy(2, 1));
258
259                 //---- cancelButton ----
260
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         //---- buttonGroup1 ----
269
ButtonGroup buttonGroup1 = new ButtonGroup();
270         buttonGroup1.add(textInputRadio);
271         buttonGroup1.add(fileInputRadio);
272         // JFormDesigner - End of component initialization //GEN-END:initComponents
273
}
274
275     // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
276
// Generated using JFormDesigner Open Source Project license - ANTLR (www.antlr.org)
277
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     // JFormDesigner - End of variables declaration //GEN-END:variables
293

294 }
295
Popular Tags