1 22 23 package org.gjt.sp.jedit.options; 24 25 import javax.swing.*; 27 import java.awt.event.*; 28 import java.util.Arrays ; 29 30 import org.gjt.sp.jedit.*; 31 import org.gjt.sp.jedit.buffer.FoldHandler; 32 import org.gjt.sp.util.StandardUtilities; 33 35 38 public class EditingOptionPane extends AbstractOptionPane 39 { 40 public EditingOptionPane() 42 { 43 super("editing"); 44 } 46 protected void _init() 48 { 49 Mode[] modes = jEdit.getModes(); 50 Arrays.sort(modes,new MiscUtilities.StringICaseCompare()); 51 52 global = new ModeProperties(); 53 modeProps = new ModeProperties[modes.length]; 54 55 String [] modeNames = new String [modes.length + 1]; 56 modeNames[0] = jEdit.getProperty("options.editing.global"); 57 58 for(int i = 0; i < modes.length; i++) 59 { 60 modeProps[i] = new ModeProperties(modes[i]); 61 modeNames[i + 1] = modes[i].getName(); 62 } 63 64 mode = new JComboBox(modeNames); 65 mode.addActionListener(new ActionHandler()); 66 67 captionBox = new Box(BoxLayout.X_AXIS); 68 addComponent(captionBox); 69 70 addComponent(jEdit.getProperty("options.editing.mode"),mode); 71 72 useDefaults = new JCheckBox(jEdit.getProperty("options.editing.useDefaults")); 73 useDefaults.addActionListener(new ActionHandler()); 74 addComponent(useDefaults); 75 76 addComponent(jEdit.getProperty("options.editing.noWordSep"), 77 noWordSep = new JTextField()); 78 79 String [] foldModes = FoldHandler.getFoldModes(); 80 addComponent(jEdit.getProperty("options.editing.folding"), 81 folding = new JComboBox(foldModes)); 82 83 addComponent(jEdit.getProperty("options.editing.collapseFolds"), 84 collapseFolds = new JTextField()); 85 86 String [] wrapModes = { 87 "none", 88 "soft", 89 "hard" 90 }; 91 addComponent(jEdit.getProperty("options.editing.wrap"), 92 wrap = new JComboBox(wrapModes)); 93 94 String [] lineLens = { "0", "72", "76", "80" }; 95 maxLineLen = new JComboBox(lineLens); 96 maxLineLen.setToolTipText(jEdit.getProperty("options.editing.maxLineLen.tooltip")); 97 addComponent(jEdit.getProperty("options.editing.maxLineLen"), maxLineLen); 98 maxLineLen.setEditable(true); 99 100 String [] tabSizes = { "2", "4", "8" }; 101 addComponent(jEdit.getProperty("options.editing.tabSize"), 102 tabSize = new JComboBox(tabSizes)); 103 tabSize.setEditable(true); 104 105 addComponent(jEdit.getProperty("options.editing.indentSize"), 106 indentSize = new JComboBox(tabSizes)); 107 indentSize.setEditable(true); 108 109 addComponent(noTabs = new JCheckBox(jEdit.getProperty( 110 "options.editing.noTabs"))); 111 112 addComponent(deepIndent = new JCheckBox(jEdit.getProperty( 113 "options.editing.deepIndent"))); 114 115 addComponent(jEdit.getProperty("options.editing.filenameGlob"), 116 filenameGlob = new JTextField()); 117 118 addComponent(jEdit.getProperty("options.editing.firstlineGlob"), 119 firstlineGlob = new JTextField()); 120 121 selectMode(); 122 123 addSeparator(); 124 125 defaultMode = new JComboBox(modes); 126 defaultMode.setSelectedItem(jEdit.getMode( 127 jEdit.getProperty("buffer.defaultMode"))); 128 addComponent(jEdit.getProperty("options.editing.defaultMode"), 129 defaultMode); 130 131 undoCount = new JTextField(jEdit.getProperty("buffer.undoCount")); 132 addComponent(jEdit.getProperty("options.editing.undoCount"),undoCount); 133 } 135 protected void _save() 137 { 138 jEdit.setProperty("buffer.defaultMode", 139 ((Mode)defaultMode.getSelectedItem()).getName()); 140 jEdit.setProperty("buffer.undoCount",undoCount.getText()); 141 142 saveMode(); 143 144 global.save(); 145 146 for(int i = 0; i < modeProps.length; i++) 147 { 148 modeProps[i].save(); 149 } 150 } 152 154 private JComboBox defaultMode; 156 private JTextField undoCount; 157 private ModeProperties global; 158 private ModeProperties[] modeProps; 159 private ModeProperties current; 160 private Box captionBox; 161 private JComboBox mode; 162 private JCheckBox useDefaults; 163 private JTextField filenameGlob; 164 private JTextField firstlineGlob; 165 private JTextField noWordSep; 166 private JComboBox folding; 167 private JTextField collapseFolds; 168 private JComboBox wrap; 169 private JComboBox maxLineLen; 170 private JComboBox tabSize; 171 private JComboBox indentSize; 172 private JCheckBox noTabs; 173 private JCheckBox deepIndent; 174 176 private void saveMode() 178 { 179 current.useDefaults = useDefaults.isSelected(); 180 current.filenameGlob = filenameGlob.getText(); 181 current.firstlineGlob = firstlineGlob.getText(); 182 current.noWordSep = noWordSep.getText(); 183 current.folding = (String )folding.getSelectedItem(); 184 current.collapseFolds = collapseFolds.getText(); 185 current.wrap = (String )wrap.getSelectedItem(); 186 current.maxLineLen = (String )maxLineLen.getSelectedItem(); 187 current.tabSize = (String )tabSize.getSelectedItem(); 188 current.indentSize = (String )indentSize.getSelectedItem(); 189 current.noTabs = noTabs.isSelected(); 190 current.deepIndent = deepIndent.isSelected(); 191 } 193 private void selectMode() 195 { 196 int index = mode.getSelectedIndex(); 197 current = index == 0 ? global : modeProps[index - 1]; 198 current.edited = true; 199 current.load(); 200 201 captionBox.removeAll(); 202 captionBox.add(GUIUtilities.createMultilineLabel( 203 jEdit.getProperty("options.editing.caption-" 204 + (index == 0 ? "0" : "1")))); 205 206 useDefaults.setSelected(current.useDefaults); 207 filenameGlob.setText(current.filenameGlob); 208 firstlineGlob.setText(current.firstlineGlob); 209 noWordSep.setText(current.noWordSep); 210 folding.setSelectedItem(current.folding); 211 collapseFolds.setText(current.collapseFolds); 212 wrap.setSelectedItem(current.wrap); 213 maxLineLen.setSelectedItem(current.maxLineLen); 214 tabSize.setSelectedItem(current.tabSize); 215 indentSize.setSelectedItem(current.indentSize); 216 noTabs.setSelected(current.noTabs); 217 deepIndent.setSelected(current.deepIndent); 218 219 updateEnabled(); 220 revalidate(); 221 } 223 private void updateEnabled() 225 { 226 boolean enabled; 227 if(current == global) 228 { 229 enabled = true; 230 useDefaults.setEnabled(false); 231 filenameGlob.setEnabled(false); 232 firstlineGlob.setEnabled(false); 233 } 234 else 235 { 236 enabled = !modeProps[mode.getSelectedIndex() - 1] 237 .useDefaults; 238 useDefaults.setEnabled(true); 239 filenameGlob.setEnabled(enabled); 240 firstlineGlob.setEnabled(enabled); 241 } 242 243 noWordSep.setEnabled(enabled); 244 folding.setEnabled(enabled); 245 collapseFolds.setEnabled(enabled); 246 wrap.setEnabled(enabled); 247 maxLineLen.setEnabled(enabled); 248 tabSize.setEnabled(enabled); 249 indentSize.setEnabled(enabled); 250 noTabs.setEnabled(enabled); 251 deepIndent.setEnabled(enabled); 252 } 254 256 class ActionHandler implements ActionListener 258 { 259 public void actionPerformed(ActionEvent evt) 260 { 261 Object source = evt.getSource(); 262 if(source == mode) 263 { 264 saveMode(); 265 selectMode(); 266 } 267 else if(source == useDefaults) 268 { 269 modeProps[mode.getSelectedIndex() - 1].useDefaults = 270 useDefaults.isSelected(); 271 updateEnabled(); 272 } 273 } 274 } 276 static class ModeProperties 278 { 279 Mode mode; 281 boolean edited; 282 boolean loaded; 283 284 boolean useDefaults; 285 String filenameGlob; 286 String firstlineGlob; 287 String noWordSep; 288 String folding; 289 String collapseFolds; 290 String wrap; 291 String maxLineLen; 292 String tabSize; 293 String indentSize; 294 boolean noTabs; 295 boolean deepIndent; 296 298 ModeProperties() 300 { 301 } 303 ModeProperties(Mode mode) 305 { 306 this.mode = mode; 307 } 309 void load() 311 { 312 if(loaded) 313 return; 314 315 loaded = true; 316 317 if(mode != null) 318 { 319 mode.loadIfNecessary(); 320 321 useDefaults = !jEdit.getBooleanProperty("mode." 322 + mode.getName() + ".customSettings"); 323 filenameGlob = (String )mode.getProperty("filenameGlob"); 324 firstlineGlob = (String )mode.getProperty("firstlineGlob"); 325 noWordSep = (String )mode.getProperty("noWordSep"); 326 folding = mode.getProperty("folding").toString(); 327 collapseFolds = mode.getProperty("collapseFolds").toString(); 328 wrap = mode.getProperty("wrap").toString(); 329 maxLineLen = mode.getProperty("maxLineLen").toString(); 330 tabSize = mode.getProperty("tabSize").toString(); 331 indentSize = mode.getProperty("indentSize").toString(); 332 noTabs = mode.getBooleanProperty("noTabs"); 333 deepIndent = mode.getBooleanProperty("deepIndent"); 334 } 335 else 336 { 337 noWordSep = jEdit.getProperty("buffer.noWordSep"); 338 folding = jEdit.getProperty("buffer.folding"); 339 collapseFolds = jEdit.getProperty("buffer.collapseFolds"); 340 wrap = jEdit.getProperty("buffer.wrap"); 341 maxLineLen = jEdit.getProperty("buffer.maxLineLen"); 342 tabSize = jEdit.getProperty("buffer.tabSize"); 343 indentSize = jEdit.getProperty("buffer.indentSize"); 344 noTabs = jEdit.getBooleanProperty("buffer.noTabs"); 345 deepIndent = jEdit.getBooleanProperty("buffer.deepIndent"); 346 } 347 } 349 void save() 351 { 352 if(!edited) 355 return; 356 357 String prefix; 358 if(mode != null) 359 { 360 prefix = "mode." + mode.getName() + '.'; 361 jEdit.setBooleanProperty(prefix + "customSettings",!useDefaults); 362 363 String oldFilenameGlob = (String )mode.getProperty("filenameGlob"); 366 String oldFirstlineGlob = (String )mode.getProperty("firstlineGlob"); 367 if(useDefaults) 368 { 369 jEdit.resetProperty(prefix + "filenameGlob"); 370 jEdit.resetProperty(prefix + "firstlineGlob"); 371 jEdit.resetProperty(prefix + "noWordSep"); 372 jEdit.resetProperty(prefix + "folding"); 373 jEdit.resetProperty(prefix + "collapseFolds"); 374 jEdit.resetProperty(prefix + "wrap"); 375 jEdit.resetProperty(prefix + "maxLineLen"); 376 jEdit.resetProperty(prefix + "tabSize"); 377 jEdit.resetProperty(prefix + "indentSize"); 378 jEdit.resetProperty(prefix + "noTabs"); 379 jEdit.resetProperty(prefix + "deepIndent"); 380 381 if(!(StandardUtilities.objectsEqual(oldFilenameGlob, 382 mode.getProperty("filenameGlob")) 383 && StandardUtilities.objectsEqual(oldFirstlineGlob, 384 mode.getProperty("firstlineGlob")))) 385 { 386 mode.init(); 387 } 388 389 return; 390 } 391 else 392 { 393 jEdit.setProperty(prefix + "filenameGlob",filenameGlob); 394 jEdit.setProperty(prefix + "firstlineGlob",firstlineGlob); 395 396 if(!(StandardUtilities.objectsEqual(oldFilenameGlob, 397 filenameGlob) 398 && StandardUtilities.objectsEqual(oldFirstlineGlob, 399 firstlineGlob))) 400 { 401 mode.init(); 402 } 403 } 404 } 405 else 406 { 407 prefix = "buffer."; 408 } 409 410 jEdit.setProperty(prefix + "noWordSep",noWordSep); 411 jEdit.setProperty(prefix + "folding",folding); 412 jEdit.setProperty(prefix + "collapseFolds",collapseFolds); 413 jEdit.setProperty(prefix + "wrap",wrap); 414 jEdit.setProperty(prefix + "maxLineLen",maxLineLen); 415 jEdit.setProperty(prefix + "tabSize",tabSize); 416 jEdit.setProperty(prefix + "indentSize",indentSize); 417 jEdit.setBooleanProperty(prefix + "noTabs",noTabs); 418 jEdit.setBooleanProperty(prefix + "deepIndent",deepIndent); 419 } } } 422 | Popular Tags |