View Javadoc

1   /*
2    * Copyright 2006 Markku Saarela 
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, 
11   * software distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. 
14   * See the License for the specific language governing permissions and limitations under the License. 
15   */
16  package net.sourceforge.jivalo.editor;
17  
18  import java.awt.Dimension;
19  import java.util.Iterator;
20  import java.util.Map;
21  
22  import javax.swing.InputMap;
23  import javax.swing.JTextPane;
24  import javax.swing.KeyStroke;
25  import javax.swing.text.AbstractDocument;
26  
27  import net.sourceforge.jivalo.editor.key.CustomKeyBinding;
28  import net.sourceforge.jivalo.editor.syntax.SyntaxHighlightDocumentFilter;
29  import net.sourceforge.jivalo.editor.syntax.SyntaxHighlightings;
30  
31  /**
32   * @author <a href="mailto:ivalo@iki.fi">Markku Saarela</a>
33   *
34   */
35  public class TextPane extends JTextPane
36  {
37  
38      private static final long serialVersionUID = 1L;
39      
40      private String fileSuffix;
41      
42      /**
43       * @param keyBindings 
44       * @param syntaxHighlightings 
45       * 
46       */
47      public TextPane(SyntaxHighlightings syntaxHighlightings
48              ,CustomKeyBinding keyBinding)
49      {
50          super();
51          
52          addBindings(keyBinding);
53          
54          ((AbstractDocument)this.getStyledDocument()).setDocumentFilter( 
55                  new SyntaxHighlightDocumentFilter( this, syntaxHighlightings ) );
56      }
57      
58      // override these methods to get horizontal scrollbar working
59      public boolean getScrollableTracksViewportWidth()
60      {
61          return false;
62      }
63  
64      public void setSize( Dimension d )
65      {
66          if ( d.width < getParent().getSize().width )
67          {
68              d.width = getParent().getSize().width;
69          }
70  
71          super.setSize( d );
72      }
73      
74      //Add a couple of emacs key bindings for navigation.
75      private void addBindings(CustomKeyBinding keyBinding) 
76      {
77          
78          if ( keyBinding != null )
79          {
80              InputMap inputMap = this.getInputMap();
81              
82              Map keyBindings = keyBinding.getKeyBindings();
83              
84              for ( Iterator iter = keyBindings.entrySet().iterator(); iter.hasNext(); )
85              {
86                  Map.Entry element = ( Map.Entry ) iter.next();
87                  
88                  inputMap.put(( KeyStroke ) element.getKey()
89                          , element.getValue() );
90              }      
91          }
92      }
93  
94      public String getFileSuffix()
95      {
96          return fileSuffix;
97      }
98  
99      public void setFileSuffix( String fileSuffix )
100     {
101         this.fileSuffix = fileSuffix;
102     }
103     
104 }