PageRenderTime 130ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/options/EditingOptionPane.java

#
Java | 416 lines | 314 code | 57 blank | 45 comment | 22 complexity | 80779e1351b86c7b349aa46d78f2d73e MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * EditingOptionPane.java - Mode-specific options panel
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1998, 2002 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.options;
  23. //{{{ Imports
  24. import javax.swing.*;
  25. import java.awt.event.*;
  26. import java.util.Arrays;
  27. import org.gjt.sp.jedit.*;
  28. import org.gjt.sp.jedit.buffer.FoldHandler;
  29. //}}}
  30. public class EditingOptionPane extends AbstractOptionPane
  31. {
  32. //{{{ EditingOptionPane constructor
  33. public EditingOptionPane()
  34. {
  35. super("editing");
  36. } //}}}
  37. //{{{ _init() method
  38. protected void _init()
  39. {
  40. Mode[] modes = jEdit.getModes();
  41. Arrays.sort(modes,new MiscUtilities.StringICaseCompare());
  42. global = new ModeProperties();
  43. modeProps = new ModeProperties[modes.length];
  44. String[] modeNames = new String[modes.length + 1];
  45. modeNames[0] = jEdit.getProperty("options.editing.global");
  46. for(int i = 0; i < modes.length; i++)
  47. {
  48. modeProps[i] = new ModeProperties(modes[i]);
  49. modeNames[i + 1] = modes[i].getName();
  50. }
  51. mode = new JComboBox(modeNames);
  52. mode.addActionListener(new ActionHandler());
  53. captionBox = new Box(BoxLayout.X_AXIS);
  54. addComponent(captionBox);
  55. addComponent(jEdit.getProperty("options.editing.mode"),mode);
  56. useDefaults = new JCheckBox(jEdit.getProperty("options.editing.useDefaults"));
  57. useDefaults.addActionListener(new ActionHandler());
  58. addComponent(useDefaults);
  59. addComponent(jEdit.getProperty("options.editing.noWordSep"),
  60. noWordSep = new JTextField());
  61. String[] foldModes = FoldHandler.getFoldModes();
  62. addComponent(jEdit.getProperty("options.editing.folding"),
  63. folding = new JComboBox(foldModes));
  64. addComponent(jEdit.getProperty("options.editing.collapseFolds"),
  65. collapseFolds = new JTextField());
  66. String[] wrapModes = {
  67. "none",
  68. "soft",
  69. "hard"
  70. };
  71. addComponent(jEdit.getProperty("options.editing.wrap"),
  72. wrap = new JComboBox(wrapModes));
  73. String[] lineLens = { "0", "72", "76", "80" };
  74. addComponent(jEdit.getProperty("options.editing.maxLineLen"),
  75. maxLineLen = new JComboBox(lineLens));
  76. maxLineLen.setEditable(true);
  77. String[] tabSizes = { "2", "4", "8" };
  78. addComponent(jEdit.getProperty("options.editing.tabSize"),
  79. tabSize = new JComboBox(tabSizes));
  80. tabSize.setEditable(true);
  81. addComponent(jEdit.getProperty("options.editing.indentSize"),
  82. indentSize = new JComboBox(tabSizes));
  83. indentSize.setEditable(true);
  84. addComponent(noTabs = new JCheckBox(jEdit.getProperty(
  85. "options.editing.noTabs")));
  86. addComponent(deepIndent = new JCheckBox(jEdit.getProperty(
  87. "options.editing.deepIndent")));
  88. addComponent(jEdit.getProperty("options.editing.filenameGlob"),
  89. filenameGlob = new JTextField());
  90. addComponent(jEdit.getProperty("options.editing.firstlineGlob"),
  91. firstlineGlob = new JTextField());
  92. selectMode();
  93. addSeparator();
  94. defaultMode = new JComboBox(modes);
  95. defaultMode.setSelectedItem(jEdit.getMode(
  96. jEdit.getProperty("buffer.defaultMode")));
  97. addComponent(jEdit.getProperty("options.editing.defaultMode"),
  98. defaultMode);
  99. undoCount = new JTextField(jEdit.getProperty("buffer.undoCount"));
  100. addComponent(jEdit.getProperty("options.editing.undoCount"),undoCount);
  101. } //}}}
  102. //{{{ _save() method
  103. protected void _save()
  104. {
  105. jEdit.setProperty("buffer.defaultMode",
  106. ((Mode)defaultMode.getSelectedItem()).getName());
  107. jEdit.setProperty("buffer.undoCount",undoCount.getText());
  108. saveMode();
  109. global.save();
  110. for(int i = 0; i < modeProps.length; i++)
  111. {
  112. modeProps[i].save();
  113. }
  114. } //}}}
  115. //{{{ Private members
  116. //{{{ Instance variables
  117. private JComboBox defaultMode;
  118. private JTextField undoCount;
  119. private ModeProperties global;
  120. private ModeProperties[] modeProps;
  121. private ModeProperties current;
  122. private Box captionBox;
  123. private JComboBox mode;
  124. private JCheckBox useDefaults;
  125. private JTextField filenameGlob;
  126. private JTextField firstlineGlob;
  127. private JTextField noWordSep;
  128. private JComboBox folding;
  129. private JTextField collapseFolds;
  130. private JComboBox wrap;
  131. private JComboBox maxLineLen;
  132. private JComboBox tabSize;
  133. private JComboBox indentSize;
  134. private JCheckBox noTabs;
  135. private JCheckBox deepIndent;
  136. //}}}
  137. //{{{ saveMode() method
  138. private void saveMode()
  139. {
  140. current.useDefaults = useDefaults.isSelected();
  141. current.filenameGlob = filenameGlob.getText();
  142. current.firstlineGlob = firstlineGlob.getText();
  143. current.noWordSep = noWordSep.getText();
  144. current.folding = (String)folding.getSelectedItem();
  145. current.collapseFolds = collapseFolds.getText();
  146. current.wrap = (String)wrap.getSelectedItem();
  147. current.maxLineLen = (String)maxLineLen.getSelectedItem();
  148. current.tabSize = (String)tabSize.getSelectedItem();
  149. current.indentSize = (String)indentSize.getSelectedItem();
  150. current.noTabs = noTabs.isSelected();
  151. current.deepIndent = deepIndent.isSelected();
  152. } //}}}
  153. //{{{ selectMode() method
  154. private void selectMode()
  155. {
  156. int index = mode.getSelectedIndex();
  157. current = (index == 0 ? global : modeProps[index - 1]);
  158. current.edited = true;
  159. current.load();
  160. captionBox.removeAll();
  161. captionBox.add(GUIUtilities.createMultilineLabel(
  162. jEdit.getProperty("options.editing.caption-"
  163. + (index == 0 ? "0" : "1"))));
  164. useDefaults.setSelected(current.useDefaults);
  165. filenameGlob.setText(current.filenameGlob);
  166. firstlineGlob.setText(current.firstlineGlob);
  167. noWordSep.setText(current.noWordSep);
  168. folding.setSelectedItem(current.folding);
  169. collapseFolds.setText(current.collapseFolds);
  170. wrap.setSelectedItem(current.wrap);
  171. maxLineLen.setSelectedItem(current.maxLineLen);
  172. tabSize.setSelectedItem(current.tabSize);
  173. indentSize.setSelectedItem(current.indentSize);
  174. noTabs.setSelected(current.noTabs);
  175. deepIndent.setSelected(current.deepIndent);
  176. updateEnabled();
  177. revalidate();
  178. } //}}}
  179. //{{{ updateEnabled() method
  180. private void updateEnabled()
  181. {
  182. boolean enabled;
  183. if(current == global)
  184. {
  185. enabled = true;
  186. useDefaults.setEnabled(false);
  187. filenameGlob.setEnabled(false);
  188. firstlineGlob.setEnabled(false);
  189. }
  190. else
  191. {
  192. enabled = !modeProps[mode.getSelectedIndex() - 1]
  193. .useDefaults;
  194. useDefaults.setEnabled(true);
  195. filenameGlob.setEnabled(enabled);
  196. firstlineGlob.setEnabled(enabled);
  197. }
  198. noWordSep.setEnabled(enabled);
  199. folding.setEnabled(enabled);
  200. collapseFolds.setEnabled(enabled);
  201. wrap.setEnabled(enabled);
  202. maxLineLen.setEnabled(enabled);
  203. tabSize.setEnabled(enabled);
  204. indentSize.setEnabled(enabled);
  205. noTabs.setEnabled(enabled);
  206. deepIndent.setEnabled(enabled);
  207. } //}}}
  208. //}}}
  209. //{{{ ActionHandler class
  210. class ActionHandler implements ActionListener
  211. {
  212. public void actionPerformed(ActionEvent evt)
  213. {
  214. Object source = evt.getSource();
  215. if(source == mode)
  216. {
  217. saveMode();
  218. selectMode();
  219. }
  220. else if(source == useDefaults)
  221. {
  222. modeProps[mode.getSelectedIndex() - 1].useDefaults =
  223. useDefaults.isSelected();
  224. updateEnabled();
  225. }
  226. }
  227. } //}}}
  228. //{{{ ModeProperties class
  229. static class ModeProperties
  230. {
  231. //{{{ Instance variables
  232. Mode mode;
  233. boolean edited;
  234. boolean loaded;
  235. boolean useDefaults;
  236. String filenameGlob;
  237. String firstlineGlob;
  238. String noWordSep;
  239. String folding;
  240. String collapseFolds;
  241. String wrap;
  242. String maxLineLen;
  243. String tabSize;
  244. String indentSize;
  245. boolean noTabs;
  246. boolean deepIndent;
  247. //}}}
  248. //{{{ ModeProperties constructor
  249. ModeProperties()
  250. {
  251. } //}}}
  252. //{{{ ModeProperties constructor
  253. ModeProperties(Mode mode)
  254. {
  255. this.mode = mode;
  256. } //}}}
  257. //{{{ load() method
  258. void load()
  259. {
  260. if(loaded)
  261. return;
  262. loaded = true;
  263. if(mode != null)
  264. {
  265. mode.loadIfNecessary();
  266. useDefaults = !jEdit.getBooleanProperty("mode."
  267. + mode.getName() + ".customSettings");
  268. filenameGlob = (String)mode.getProperty("filenameGlob");
  269. firstlineGlob = (String)mode.getProperty("firstlineGlob");
  270. noWordSep = (String)mode.getProperty("noWordSep");
  271. folding = mode.getProperty("folding").toString();
  272. collapseFolds = mode.getProperty("collapseFolds").toString();
  273. wrap = mode.getProperty("wrap").toString();
  274. maxLineLen = mode.getProperty("maxLineLen").toString();
  275. tabSize = mode.getProperty("tabSize").toString();
  276. indentSize = mode.getProperty("indentSize").toString();
  277. noTabs = mode.getBooleanProperty("noTabs");
  278. deepIndent = mode.getBooleanProperty("deepIndent");
  279. }
  280. else
  281. {
  282. noWordSep = jEdit.getProperty("buffer.noWordSep");
  283. folding = jEdit.getProperty("buffer.folding");
  284. collapseFolds = jEdit.getProperty("buffer.collapseFolds");
  285. wrap = jEdit.getProperty("buffer.wrap");
  286. maxLineLen = jEdit.getProperty("buffer.maxLineLen");
  287. tabSize = jEdit.getProperty("buffer.tabSize");
  288. indentSize = jEdit.getProperty("buffer.indentSize");
  289. noTabs = jEdit.getBooleanProperty("buffer.noTabs");
  290. deepIndent = jEdit.getBooleanProperty("buffer.deepIndent");
  291. }
  292. } //}}}
  293. //{{{ save() method
  294. void save()
  295. {
  296. // don't do anything if the user didn't change
  297. // any settings
  298. if(!edited)
  299. return;
  300. String prefix;
  301. if(mode != null)
  302. {
  303. prefix = "mode." + mode.getName() + '.';
  304. jEdit.setBooleanProperty(prefix + "customSettings",!useDefaults);
  305. // need to call Mode.init() if the file name or first line
  306. // globs change
  307. String oldFilenameGlob = (String)mode.getProperty("filenameGlob");
  308. String oldFirstlineGlob = (String)mode.getProperty("firstlineGlob");
  309. if(useDefaults)
  310. {
  311. jEdit.resetProperty(prefix + "filenameGlob");
  312. jEdit.resetProperty(prefix + "firstlineGlob");
  313. jEdit.resetProperty(prefix + "noWordSep");
  314. jEdit.resetProperty(prefix + "folding");
  315. jEdit.resetProperty(prefix + "collapseFolds");
  316. jEdit.resetProperty(prefix + "wrap");
  317. jEdit.resetProperty(prefix + "maxLineLen");
  318. jEdit.resetProperty(prefix + "tabSize");
  319. jEdit.resetProperty(prefix + "indentSize");
  320. jEdit.resetProperty(prefix + "noTabs");
  321. jEdit.resetProperty(prefix + "deepIndent");
  322. if(!(MiscUtilities.objectsEqual(oldFilenameGlob,
  323. mode.getProperty("filenameGlob"))
  324. && MiscUtilities.objectsEqual(oldFirstlineGlob,
  325. mode.getProperty("firstlineGlob"))))
  326. {
  327. mode.init();
  328. }
  329. return;
  330. }
  331. else
  332. {
  333. jEdit.setProperty(prefix + "filenameGlob",filenameGlob);
  334. jEdit.setProperty(prefix + "firstlineGlob",firstlineGlob);
  335. if(!(MiscUtilities.objectsEqual(oldFilenameGlob,
  336. filenameGlob)
  337. && MiscUtilities.objectsEqual(oldFirstlineGlob,
  338. firstlineGlob)))
  339. {
  340. mode.init();
  341. }
  342. }
  343. }
  344. else
  345. {
  346. prefix = "buffer.";
  347. }
  348. jEdit.setProperty(prefix + "noWordSep",noWordSep);
  349. jEdit.setProperty(prefix + "folding",folding);
  350. jEdit.setProperty(prefix + "collapseFolds",collapseFolds);
  351. jEdit.setProperty(prefix + "wrap",wrap);
  352. jEdit.setProperty(prefix + "maxLineLen",maxLineLen);
  353. jEdit.setProperty(prefix + "tabSize",tabSize);
  354. jEdit.setProperty(prefix + "indentSize",indentSize);
  355. jEdit.setBooleanProperty(prefix + "noTabs",noTabs);
  356. jEdit.setBooleanProperty(prefix + "deepIndent",deepIndent);
  357. } //}}}
  358. } //}}}
  359. }