/* * Copyright 2008 Ayman Al-Sairafi ayman.alsairafi@gmail.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License * at http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jsyntaxpane; import java.awt.event.ItemEvent; import java.io.IOException; import java.io.StringReader; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.DefaultComboBoxModel; import jsyntaxpane.actions.ActionUtils; import jsyntaxpane.actions.CaretMonitor; public class SyntaxTester extends javax.swing.JFrame { /** Creates new form Tester */ public SyntaxTester() { // this is a test for adding regex lexer. DefaultSyntaxKit.registerContentType("text/aa_regex", "jsyntaxpane.JavaRegexKit"); initComponents(); jCmbLangs.setModel(new DefaultComboBoxModel(DefaultSyntaxKit.getContentTypes())); // jEdtTest.setContentType(jCmbLangs.getItemAt(0).toString()); jCmbLangs.setSelectedItem("text/java"); new CaretMonitor(jEdtTest, lblCaretPos); } /** * This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { lblCaretPos = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); jEdtTest = new javax.swing.JEditorPane(); lblToken = new javax.swing.JLabel(); jCmbLangs = new javax.swing.JComboBox(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("JSyntaxPane Tester"); lblCaretPos.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING); lblCaretPos.setText("Caret Position"); jEdtTest.setContentType(""); jEdtTest.setFont(new java.awt.Font("Monospaced", 0, 13)); jEdtTest.setCaretColor(new java.awt.Color(153, 204, 255)); jEdtTest.addCaretListener(new javax.swing.event.CaretListener() { public void caretUpdate(javax.swing.event.CaretEvent evt) { jEdtTestCaretUpdate(evt); } }); jScrollPane1.setViewportView(jEdtTest); lblToken.setFont(new java.awt.Font("Courier New", 0, 12)); lblToken.setText("Token under cursor"); jCmbLangs.setMaximumRowCount(20); jCmbLangs.setFocusable(false); jCmbLangs.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent evt) { jCmbLangsItemStateChanged(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(lblToken, javax.swing.GroupLayout.DEFAULT_SIZE, 487, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(lblCaretPos, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 612, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(jCmbLangs, 0, 272, Short.MAX_VALUE) .addGap(340, 340, 340))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 397, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup() .addGap(2, 2, 2) .addComponent(lblToken, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(lblCaretPos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jCmbLangs, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); pack(); }// </editor-fold>//GEN-END:initComponents private void jEdtTestCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jEdtTestCaretUpdate SyntaxDocument sDoc = ActionUtils.getSyntaxDocument(jEdtTest); if (sDoc != null) { Token t = sDoc.getTokenAt(evt.getDot()); if (t != null) { CharSequence tData = t.getText(sDoc); if (t.length > 40) { tData = tData.subSequence(0, 40); } lblToken.setText(t.toString() + ": " + tData); } else { // null token, remove the status lblToken.setText("NO Token at cursor"); } } }//GEN-LAST:event_jEdtTestCaretUpdate private void jCmbLangsItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCmbLangsItemStateChanged if (evt.getStateChange() == ItemEvent.SELECTED) { String lang = jCmbLangs.getSelectedItem().toString(); // save the state of the current JEditorPane, as it's Document is about // to be replaced. String oldText = jEdtTest.getText(); // install a new DefaultSyntaxKit on the JEditorPane for the requested language. jEdtTest.setContentType(lang); try { // setText should not be called (read the JavaDocs). Better use the read // method and create a new document. jEdtTest.read(new StringReader(oldText), lang); } catch (IOException ex) { Logger.getLogger(SyntaxTester.class.getName()).log(Level.SEVERE, null, ex); } } }//GEN-LAST:event_jCmbLangsItemStateChanged /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { try { DefaultSyntaxKit.initKit(); new SyntaxTester().setVisible(true); } catch (Exception e) { e.printStackTrace(); System.exit(2); } } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JComboBox jCmbLangs; private javax.swing.JEditorPane jEdtTest; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JLabel lblCaretPos; private javax.swing.JLabel lblToken; // End of variables declaration//GEN-END:variables }