1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sourceforge.jivalo.editor;
17
18 import java.beans.PropertyChangeEvent;
19 import java.beans.PropertyChangeListener;
20 import java.io.UnsupportedEncodingException;
21
22 import javax.swing.JPanel;
23 import javax.swing.JScrollPane;
24 import javax.swing.SpringLayout;
25
26 import net.sourceforge.jivalo.editor.key.CustomKeyBinding;
27 import net.sourceforge.jivalo.editor.key.KeyBindings;
28 import net.sourceforge.jivalo.editor.syntax.SyntaxHighlightings;
29
30 /**
31 * Container class for holding editor TextPane.
32 * @author <a href="mailto:ivalo@iki.fi">Markku Saarela</a>
33 *
34 */
35 public class JIvaloEditor extends JPanel implements PropertyChangeListener
36 {
37
38 private static final long serialVersionUID = 1L;
39
40 /**
41 * Editor TextPane.
42 */
43 private TextPane textPane;
44
45 /**
46 * jIvalo editor preferences.
47 */
48 private JIvaloEditorProperties editorProperties;
49
50 /**
51 * Constructor for JIvaloEditor.
52 *
53 */
54 public JIvaloEditor()
55 {
56 super();
57 initialize();
58 }
59
60 /**
61 * Initializes editor preferences and layout for editor and add's this
62 * as property change listener for TextPane.
63 *
64 */
65 private void initialize()
66 {
67
68 KeyBindings keyBindings = null;
69
70 SyntaxHighlightings syntaxHighlightnings = null;
71
72 CustomKeyBinding keyBinding = null;
73
74 try
75 {
76 this. editorProperties =
77 PropertiesUtils.getInstance().getEditorProperties();
78 keyBindings = this.editorProperties.getKeyBindings();
79 syntaxHighlightnings =
80 this.editorProperties.getSyntaxHighlightings();
81
82 String currentKeyBinding =
83 this.editorProperties.getCurrenKeyBindingName();
84
85 if ( currentKeyBinding != null && keyBindings != null )
86 {
87 keyBinding = keyBindings.getKeyBinding( currentKeyBinding );
88 }
89 } catch ( Exception e )
90 {
91
92 e.printStackTrace();
93 }
94
95 SpringLayout layout = new SpringLayout();
96
97 this.setLayout(layout);
98
99 this.textPane = new TextPane(syntaxHighlightnings, keyBinding);
100
101 JScrollPane paneScrollPane = new JScrollPane(this.textPane);
102
103 this.add( paneScrollPane );
104
105 this.textPane.addPropertyChangeListener( "text", this );
106
107 layout.putConstraint(SpringLayout.WEST, paneScrollPane,
108 5, SpringLayout.WEST, this);
109 layout.putConstraint(SpringLayout.NORTH, paneScrollPane,
110 5, SpringLayout.NORTH, this);
111 layout.putConstraint(SpringLayout.EAST, this,
112 5, SpringLayout.EAST, paneScrollPane);
113 layout.putConstraint(SpringLayout.SOUTH, this, 5,
114 SpringLayout.SOUTH, paneScrollPane);
115
116 }
117
118 /**
119 * Get's text from editor encoded to configurated character set.
120 * @return
121 */
122 public String getText()
123 {
124
125 if ( this.editorProperties != null
126 && this.editorProperties.getEncodingCharset() != null )
127 {
128 try
129 {
130 byte[] bytes = this.textPane.getText().getBytes(
131 this.editorProperties.getEncodingCharset().name() );
132
133 return new String( bytes,
134 this.editorProperties.getEncodingCharset().name());
135 } catch ( UnsupportedEncodingException e )
136 {
137 return this.textPane.getText();
138 }
139 }
140 else
141 {
142 return this.textPane.getText();
143 }
144
145 }
146
147 /**
148 * Set's new text to editor.
149 * @param text new editor text.
150 * @param fileSuffix file suffix if text is from file.
151 */
152 public void setText(String text, String fileSuffix)
153 {
154 this.textPane.setFileSuffix( fileSuffix );
155 this.textPane.setText( text );
156 }
157
158 /**
159 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
160 */
161 public void propertyChange( PropertyChangeEvent evt )
162 {
163 if ( "text".equals( evt.getPropertyName() ) )
164 {
165 firePropertyChange( "text", false, true );
166 }
167 }
168
169 }