1 22 23 package org.gjt.sp.jedit.gui; 24 25 import javax.swing.border.*; 27 import javax.swing.*; 28 import java.awt.*; 29 import org.gjt.sp.jedit.*; 30 32 public class AbbrevEditor extends JPanel 33 { 34 public AbbrevEditor() 36 { 37 GridBagLayout layout = new GridBagLayout(); 38 setLayout(layout); 39 40 GridBagConstraints cons = new GridBagConstraints(); 41 cons.anchor = cons.WEST; 42 cons.fill = cons.BOTH; 43 cons.weightx = 0.0f; 44 cons.gridx = 1; 45 cons.gridy = 1; 46 47 JLabel label = new JLabel(jEdit.getProperty("abbrev-editor.abbrev"), 48 SwingConstants.RIGHT); 49 label.setBorder(new EmptyBorder(0,0,0,12)); 50 layout.setConstraints(label,cons); 51 add(label); 52 cons.gridx++; 53 cons.weightx = 1.0f; 54 abbrev = new JTextField(); 55 layout.setConstraints(abbrev,cons); 56 add(abbrev); 57 58 cons.gridx = 1; 59 cons.weightx = 0.0f; 60 cons.gridwidth = 2; 61 62 cons.gridy++; 63 label = new JLabel(jEdit.getProperty("abbrev-editor.before")); 64 label.setBorder(new EmptyBorder(6,0,3,0)); 65 layout.setConstraints(label,cons); 66 add(label); 67 68 cons.gridy++; 69 cons.weighty = 1.0f; 70 beforeCaret = new JTextArea(4,40); 71 JScrollPane scroller = new JScrollPane(beforeCaret); 72 layout.setConstraints(scroller,cons); 73 add(scroller); 74 75 cons.gridy++; 76 cons.weighty = 0.0f; 77 label = new JLabel(jEdit.getProperty("abbrev-editor.after")); 78 label.setBorder(new EmptyBorder(6,0,3,0)); 79 layout.setConstraints(label,cons); 80 add(label); 81 82 cons.gridy++; 83 cons.weighty = 1.0f; 84 afterCaret = new JTextArea(4,40); 85 scroller = new JScrollPane(afterCaret); 86 layout.setConstraints(scroller,cons); 87 add(scroller); 88 } 90 public String getAbbrev() 92 { 93 return abbrev.getText(); 94 } 96 public void setAbbrev(String abbrev) 98 { 99 this.abbrev.setText(abbrev); 100 } 102 public String getExpansion() 104 { 105 StringBuffer buf = new StringBuffer (); 106 107 String beforeCaretText = beforeCaret.getText(); 108 String afterCaretText = afterCaret.getText(); 109 110 for(int i = 0; i < beforeCaretText.length(); i++) 111 { 112 char ch = beforeCaretText.charAt(i); 113 switch(ch) 114 { 115 case '\n': 116 buf.append("\\n"); 117 break; 118 case '\t': 119 buf.append("\\t"); 120 break; 121 case '\\': 122 buf.append("\\\\"); 123 break; 124 default: 125 buf.append(ch); 126 break; 127 } 128 } 129 130 if(afterCaretText.length() != 0) 131 { 132 buf.append("\\|"); 133 134 for(int i = 0; i < afterCaretText.length(); i++) 135 { 136 char ch = afterCaretText.charAt(i); 137 switch(ch) 138 { 139 case '\n': 140 buf.append("\\n"); 141 break; 142 case '\t': 143 buf.append("\\t"); 144 break; 145 case '\\': 146 buf.append("\\\\"); 147 break; 148 default: 149 buf.append(ch); 150 break; 151 } 152 } 153 } 154 155 return buf.toString(); 156 } 158 public void setExpansion(String expansion) 160 { 161 if(expansion == null) 162 { 163 beforeCaret.setText(null); 164 afterCaret.setText(null); 165 return; 166 } 167 168 String beforeCaretText = null; 169 String afterCaretText = null; 170 StringBuffer buf = new StringBuffer (); 171 172 for(int i = 0; i < expansion.length(); i++) 173 { 174 char ch = expansion.charAt(i); 175 176 if(ch == '\\' && i != expansion.length() - 1) 177 { 178 ch = expansion.charAt(++i); 179 switch(ch) 180 { 181 case 't': 182 buf.append('\t'); 183 break; 184 case 'n': 185 buf.append('\n'); 186 break; 187 case '|': 188 beforeCaretText = buf.toString(); 189 buf.setLength(0); 190 break; 191 default: 192 buf.append(ch); 193 break; 194 } 195 } 196 else 197 buf.append(ch); 198 } 199 200 if(beforeCaretText == null) 201 beforeCaretText = buf.toString(); 202 else 203 afterCaretText = buf.toString(); 204 205 beforeCaret.setText(beforeCaretText); 206 afterCaret.setText(afterCaretText); 207 } 209 public JTextField getAbbrevField() 211 { 212 return abbrev; 213 } 215 public JTextArea getBeforeCaretTextArea() 217 { 218 return beforeCaret; 219 } 221 public JTextArea getAfterCaretTextArea() 223 { 224 return afterCaret; 225 } 227 private JTextField abbrev; 229 private JTextArea beforeCaret, afterCaret; 230 } 232 | Popular Tags |