001: /* Copyright 2006 David N. Welton
002:
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014: */
015:
016: package heclbuilder;
017:
018: import java.io.File;
019:
020: import javax.swing.JOptionPane;
021:
022: import org.hecl.HeclException;
023: import org.hecl.files.HeclFile;
024:
025: /**
026: * <code>HeclEditor</code> -- This class provides a very, very crude
027: * editor/viewer for Hecl scripts.
028: *
029: * @author <a href="mailto:davidw@dedasys.com">David N. Welton</a>
030: * @version 1.0
031: */
032: public class HeclEditor extends javax.swing.JFrame {
033:
034: private javax.swing.JMenu editMenu;
035: private javax.swing.JMenuBar editMenuBar1;
036: private javax.swing.JEditorPane editPanel;
037: private javax.swing.JScrollPane editScrollPane;
038: private javax.swing.JMenuItem menuClose;
039: private javax.swing.JMenuItem menuSave;
040:
041: private StringBuffer script;
042: private File scriptfile;
043:
044: public HeclEditor() {
045: initComponents();
046: }
047:
048: public HeclEditor(String filename) {
049: initComponents();
050: scriptfile = new File(filename);
051: if (scriptfile.exists()) {
052: try {
053: script = HeclFile.readFile(filename);
054: } catch (HeclException e) {
055: JOptionPane.showMessageDialog(null, "File Error",
056: "Error reading file: " + filename + "\n"
057: + e.toString(),
058: JOptionPane.ERROR_MESSAGE);
059: }
060: } else {
061: script = new StringBuffer("");
062: }
063: editPanel.setText(script.toString());
064: }
065:
066: private void initComponents() {
067: editScrollPane = new javax.swing.JScrollPane();
068: editPanel = new javax.swing.JEditorPane();
069: editMenuBar1 = new javax.swing.JMenuBar();
070: editMenu = new javax.swing.JMenu();
071: menuSave = new javax.swing.JMenuItem();
072: menuClose = new javax.swing.JMenuItem();
073:
074: setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
075: editScrollPane.setAutoscrolls(true);
076: editScrollPane.setPreferredSize(null);
077: editPanel.setDragEnabled(true);
078: editScrollPane.setViewportView(editPanel);
079:
080: editMenu.setText("Menu");
081: menuSave.setText("Save");
082: menuSave.addActionListener(new java.awt.event.ActionListener() {
083: public void actionPerformed(java.awt.event.ActionEvent evt) {
084: menuSaveActionPerformed(evt);
085: }
086: });
087:
088: editMenu.add(menuSave);
089:
090: menuClose.setText("Close");
091: menuClose
092: .addActionListener(new java.awt.event.ActionListener() {
093: public void actionPerformed(
094: java.awt.event.ActionEvent evt) {
095: menuCloseActionPerformed(evt);
096: }
097: });
098:
099: editMenu.add(menuClose);
100:
101: editMenuBar1.add(editMenu);
102:
103: setJMenuBar(editMenuBar1);
104:
105: org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
106: getContentPane());
107: getContentPane().setLayout(layout);
108: layout.setHorizontalGroup(layout.createParallelGroup(
109: org.jdesktop.layout.GroupLayout.LEADING).add(
110: org.jdesktop.layout.GroupLayout.LEADING,
111: layout.createSequentialGroup().add(editScrollPane,
112: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
113: 640, Short.MAX_VALUE).addContainerGap()));
114: layout.setVerticalGroup(layout.createParallelGroup(
115: org.jdesktop.layout.GroupLayout.LEADING).add(
116: org.jdesktop.layout.GroupLayout.LEADING,
117: layout.createSequentialGroup().add(editScrollPane,
118: org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
119: 333, Short.MAX_VALUE).addContainerGap()));
120: pack();
121: }
122:
123: private void menuCloseActionPerformed(java.awt.event.ActionEvent evt) {
124: script = null;
125: this .dispose();
126: }
127:
128: private void menuSaveActionPerformed(java.awt.event.ActionEvent evt) {
129: try {
130: HeclFile.writeFile(scriptfile.toString(), editPanel
131: .getText());
132: } catch (HeclException e) {
133: JOptionPane.showMessageDialog(null, "File Error",
134: "Could not write to: " + scriptfile + "\n"
135: + e.toString(), JOptionPane.ERROR_MESSAGE);
136: }
137: }
138: }
|