001: //Copyright (c) Janna Khegai 2004, Hans-Joachim Daniels 2005
002: //
003: //This program is free software; you can redistribute it and/or modify
004: //it under the terms of the GNU General Public License as published by
005: //the Free Software Foundation; either version 2 of the License, or
006: //(at your option) any later version.
007: //
008: //This program is distributed in the hope that it will be useful,
009: //but WITHOUT ANY WARRANTY; without even the implied warranty of
010: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: //GNU General Public License for more details.
012: //
013: //You can either finde the file LICENSE or LICENSE.TXT in the source
014: //distribution or in the .jar file of this application
015:
016: package de.uka.ilkd.key.ocl.gf;
017:
018: import java.awt.BorderLayout;
019: import java.awt.Dimension;
020: import java.awt.Font;
021: import java.awt.GridLayout;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ActionListener;
024: import java.io.File;
025: import java.util.logging.Level;
026: import java.util.logging.Logger;
027:
028: import javax.swing.*;
029:
030: /**
031: * Takes care of reading in Strings that are to be parsed and terms.
032: * @author daniels
033: *
034: */
035: class ReadDialog implements ActionListener {
036: /** XML parsing debug messages */
037: private static Logger xmlLogger = Logger.getLogger(GFEditor2.class
038: .getName()
039: + "_XML");
040: /** The window to which this class belongs */
041: private final GFEditor2 owner;
042: /** is the main thing of this class */
043: private final JDialog readDialog;
044: /** main area of the Read dialog (content pane)*/
045: private final JPanel inputPanel = new JPanel();
046: /** OK, Cancel, Browse in the Read dialog */
047: private final JPanel inputPanel2 = new JPanel();
048: /** in the Read dialog the OK button */
049: private final JButton ok = new JButton("OK");
050: /** in the Read dialog the Cancel button */
051: private final JButton cancel = new JButton("Cancel");
052: /** in the Read dialog the Browse button */
053: private final JButton browse = new JButton("Browse...");
054: /** groups inputField and inputLabel */
055: private final JPanel inputPanel3 = new JPanel();
056: /** for 'Read' to get the input */
057: private final JTextField inputField = new JTextField();
058: /** "Read: " */
059: private final JLabel inputLabel = new JLabel("Read: ");
060: /** the radio group in the Read dialog to select Term or String */
061: private final ButtonGroup readGroup = new ButtonGroup();
062: /** to select to input a Term in the Read dialog */
063: private final JRadioButton termReadButton = new JRadioButton("Term");
064: /** to select to input a String in the Read dialog */
065: private final JRadioButton stringReadButton = new JRadioButton(
066: "String");
067: /** used for new Topic, Import and Browse (readDialog) */
068: private final JFileChooser fc = new JFileChooser("./");
069: /**
070: * if a user sends a custom command to GF, he might want to do this
071: * again with the same command.
072: * Therefore it is saved.
073: */
074: private String parseInput = "";
075: /**
076: * if the user enters a term, he perhaps wants to input the same text again.
077: * Therefore it is saved.
078: */
079: private String termInput = "";
080:
081: /**
082: * creates a modal dialog
083: * @param owner The parent for which this dialog shall be modal.
084: */
085: protected ReadDialog(GFEditor2 owner) {
086: this .owner = owner;
087: readDialog = new JDialog(owner, "Input", true);
088: readDialog.setLocationRelativeTo(owner);
089: readDialog.getContentPane().add(inputPanel);
090: readDialog.setSize(480, 135);
091:
092: termReadButton.setActionCommand("term");
093: stringReadButton.setSelected(true);
094: stringReadButton.setActionCommand("lin");
095: // Group the radio buttons.
096: readGroup.add(stringReadButton);
097: readGroup.add(termReadButton);
098: JPanel readButtonPanel = new JPanel();
099: readButtonPanel.setLayout(new GridLayout(3, 1));
100: readButtonPanel.setPreferredSize(new Dimension(70, 70));
101: readButtonPanel.add(new JLabel("Format:"));
102: readButtonPanel.add(stringReadButton);
103: readButtonPanel.add(termReadButton);
104: inputPanel.setLayout(new BorderLayout(10, 10));
105: inputPanel3.setLayout(new GridLayout(2, 1, 5, 5));
106: inputPanel3.add(inputLabel);
107: inputPanel3.add(inputField);
108: ok.addActionListener(this );
109: browse.addActionListener(this );
110: cancel.addActionListener(this );
111: inputField.setPreferredSize(new Dimension(300, 23));
112: inputPanel.add(inputPanel3, BorderLayout.CENTER);
113: inputPanel.add(new JLabel(" "), BorderLayout.WEST);
114: inputPanel.add(readButtonPanel, BorderLayout.EAST);
115: inputPanel.add(inputPanel2, BorderLayout.SOUTH);
116: inputPanel2.add(ok);
117: inputPanel2.add(cancel);
118: inputPanel2.add(browse);
119: }
120:
121: /**
122: * Shows this modal dialog.
123: * The previous input text will be there again.
124: *
125: */
126: protected void show() {
127: if (stringReadButton.isSelected()) {
128: inputField.setText(this .parseInput);
129: } else {
130: inputField.setText(this .termInput);
131: }
132: this .readDialog.setVisible(true);
133: }
134:
135: /**
136: * Sets the font of all GUI elements to font
137: * @param font
138: */
139: protected void setFont(Font font) {
140: ok.setFont(font);
141: cancel.setFont(font);
142: inputLabel.setFont(font);
143: browse.setFont(font);
144: termReadButton.setFont(font);
145: stringReadButton.setFont(font);
146: }
147:
148: /**
149: * the ActionListener method that does the user interaction
150: */
151: public void actionPerformed(ActionEvent ae) {
152: Object obj = ae.getSource();
153:
154: if (obj == cancel) {
155: readDialog.setVisible(false);
156: }
157:
158: if (obj == browse) {
159: if (fc.getChoosableFileFilters().length < 2)
160: fc.addChoosableFileFilter(new GrammarFilter());
161: int returnVal = fc.showOpenDialog(owner);
162: if (returnVal == JFileChooser.APPROVE_OPTION) {
163: File file = fc.getSelectedFile();
164: inputField.setText(file.getPath().replace('\\', '/'));
165: }
166: }
167:
168: if (obj == ok) {
169: if (termReadButton.isSelected()) {
170: termInput = inputField.getText();
171: if (termInput.indexOf(File.separatorChar) == -1) {
172: owner.send("[t] g " + termInput);
173: if (xmlLogger.isLoggable(Level.FINER))
174: xmlLogger.finer("sending term string");
175: } else {
176: owner.send("[t] tfile " + termInput);
177: if (xmlLogger.isLoggable(Level.FINER)) {
178: xmlLogger.finer("sending file term: "
179: + termInput);
180: }
181: }
182: } else { //String selected
183: parseInput = inputField.getText();
184: if (parseInput.indexOf(File.separatorChar) == -1) {
185: owner.send("[t] p " + parseInput);
186: if (xmlLogger.isLoggable(Level.FINER))
187: xmlLogger.finer("sending parse string: "
188: + parseInput);
189: } else {
190: owner.send("[t] pfile " + parseInput);
191: if (xmlLogger.isLoggable(Level.FINER))
192: xmlLogger.finer("sending file parse string: "
193: + parseInput);
194: }
195: }
196: readDialog.setVisible(false);
197: }
198:
199: }
200:
201: }
|