PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/gui/BufferOptions.java

#
Java | 639 lines | 223 code | 333 blank | 83 comment | 29 complexity | eedbc8158c9db8a5a1a6bb3715f70bd8 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. * BufferOptions.java - Buffer-specific options dialog
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1999, 2004 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.gui;
  23. //{{{ Imports
  24. import javax.swing.border.EmptyBorder;
  25. import javax.swing.*;
  26. import java.awt.*;
  27. import java.awt.event.*;
  28. import java.util.Arrays;
  29. import org.gjt.sp.jedit.buffer.FoldHandler;
  30. import org.gjt.sp.jedit.buffer.JEditBuffer;
  31. import org.gjt.sp.jedit.*;
  32. //}}}
  33. /**
  34. * Buffer-specific options dialog.
  35. * @author Slava Pestov
  36. * @version $Id: BufferOptions.java 5487 2006-06-23 22:58:12Z kpouer $
  37. *
  38. */
  39. public class BufferOptions extends EnhancedDialog
  40. {
  41. //{{{ BufferOptions constructor
  42. public BufferOptions(View view, Buffer buffer)
  43. {
  44. super(view,jEdit.getProperty("buffer-options.title"),true);
  45. this.view = view;
  46. this.buffer = buffer;
  47. JPanel content = new JPanel(new BorderLayout());
  48. content.setBorder(new EmptyBorder(12,12,12,12));
  49. setContentPane(content);
  50. ActionHandler actionListener = new ActionHandler();
  51. AbstractOptionPane panel = new AbstractOptionPane(null);
  52. panel.addComponent(GUIUtilities.createMultilineLabel(
  53. jEdit.getProperty("buffer-options.caption")));
  54. panel.addSeparator("buffer-options.loading-saving");
  55. //{{{ Line separator
  56. String[] lineSeps = { jEdit.getProperty("lineSep.unix"),
  57. jEdit.getProperty("lineSep.windows"),
  58. jEdit.getProperty("lineSep.mac") };
  59. lineSeparator = new JComboBox(lineSeps);
  60. String lineSep = buffer.getStringProperty(JEditBuffer.LINESEP);
  61. if(lineSep == null)
  62. lineSep = System.getProperty("line.separator");
  63. if("\n".equals(lineSep))
  64. lineSeparator.setSelectedIndex(0);
  65. else if("\r\n".equals(lineSep))
  66. lineSeparator.setSelectedIndex(1);
  67. else if("\r".equals(lineSep))
  68. lineSeparator.setSelectedIndex(2);
  69. panel.addComponent(jEdit.getProperty("buffer-options.lineSeparator"),
  70. lineSeparator);
  71. //}}}
  72. //{{{ Encoding
  73. String[] encodings = MiscUtilities.getEncodings();
  74. Arrays.sort(encodings,new MiscUtilities.StringICaseCompare());
  75. encoding = new JComboBox(encodings);
  76. encoding.setEditable(true);
  77. encoding.setSelectedItem(buffer.getStringProperty(JEditBuffer.ENCODING));
  78. panel.addComponent(jEdit.getProperty("buffer-options.encoding"),
  79. encoding);
  80. //}}}
  81. //{{{ GZipped setting
  82. gzipped = new JCheckBox(jEdit.getProperty(
  83. "buffer-options.gzipped"));
  84. gzipped.setSelected(buffer.getBooleanProperty(Buffer.GZIPPED));
  85. panel.addComponent(gzipped);
  86. //}}}
  87. panel.addSeparator("buffer-options.editing");
  88. //{{{ Edit mode
  89. modes = jEdit.getModes();
  90. Arrays.sort(modes,new MiscUtilities.StringICaseCompare());
  91. mode = new JComboBox(modes);
  92. mode.setSelectedItem(buffer.getMode());
  93. mode.addActionListener(actionListener);
  94. panel.addComponent(jEdit.getProperty("buffer-options.mode"),mode);
  95. //}}}
  96. //{{{ Fold mode
  97. String[] foldModes = FoldHandler.getFoldModes();
  98. folding = new JComboBox(foldModes);
  99. folding.setSelectedItem(buffer.getStringProperty("folding"));
  100. panel.addComponent(jEdit.getProperty("options.editing.folding"),
  101. folding);
  102. //}}}
  103. //{{{ Wrap mode
  104. String[] wrapModes = {
  105. "none",
  106. "soft",
  107. "hard"
  108. };
  109. wrap = new JComboBox(wrapModes);
  110. wrap.setSelectedItem(buffer.getStringProperty("wrap"));
  111. panel.addComponent(jEdit.getProperty("options.editing.wrap"),
  112. wrap);
  113. //}}}
  114. //{{{ Max line length
  115. String[] lineLengths = { "0", "72", "76", "80" };
  116. maxLineLen = new JComboBox(lineLengths);
  117. maxLineLen.setEditable(true);
  118. maxLineLen.setSelectedItem(buffer.getStringProperty("maxLineLen"));
  119. panel.addComponent(jEdit.getProperty("options.editing.maxLineLen"),
  120. maxLineLen);
  121. //}}}
  122. //{{{ Tab size
  123. String[] tabSizes = { "2", "4", "8" };
  124. tabSize = new JComboBox(tabSizes);
  125. tabSize.setEditable(true);
  126. tabSize.setSelectedItem(buffer.getStringProperty("tabSize"));
  127. panel.addComponent(jEdit.getProperty("options.editing.tabSize"),tabSize);
  128. //}}}
  129. //{{{ Indent size
  130. indentSize = new JComboBox(tabSizes);
  131. indentSize.setEditable(true);
  132. indentSize.setSelectedItem(buffer.getStringProperty("indentSize"));
  133. panel.addComponent(jEdit.getProperty("options.editing.indentSize"),
  134. indentSize);
  135. //}}}
  136. //{{{ Soft tabs
  137. noTabs = new JCheckBox(jEdit.getProperty(
  138. "options.editing.noTabs"));
  139. noTabs.setSelected(buffer.getBooleanProperty("noTabs"));
  140. panel.addComponent(noTabs);
  141. //}}}
  142. content.add(BorderLayout.NORTH,panel);
  143. //{{{ Buttons
  144. JPanel buttons = new JPanel();
  145. buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
  146. buttons.setBorder(new EmptyBorder(12,0,0,0));
  147. buttons.add(Box.createGlue());
  148. ok = new JButton(jEdit.getProperty("common.ok"));
  149. ok.addActionListener(actionListener);
  150. getRootPane().setDefaultButton(ok);
  151. buttons.add(ok);
  152. buttons.add(Box.createHorizontalStrut(6));
  153. cancel = new JButton(jEdit.getProperty("common.cancel"));
  154. cancel.addActionListener(actionListener);
  155. buttons.add(cancel);
  156. buttons.add(Box.createGlue());
  157. content.add(BorderLayout.SOUTH,buttons);
  158. //}}}
  159. pack();
  160. setLocationRelativeTo(view);
  161. setVisible(true);
  162. } //}}}
  163. //{{{ ok() method
  164. public void ok()
  165. {
  166. int index = lineSeparator.getSelectedIndex();
  167. String lineSep;
  168. if(index == 0)
  169. lineSep = "\n";
  170. else if(index == 1)
  171. lineSep = "\r\n";
  172. else if(index == 2)
  173. lineSep = "\r";
  174. else
  175. throw new InternalError();
  176. String oldLineSep = buffer.getStringProperty(JEditBuffer.LINESEP);
  177. if(oldLineSep == null)
  178. oldLineSep = System.getProperty("line.separator");
  179. if(!oldLineSep.equals(lineSep))
  180. {
  181. buffer.setStringProperty("lineSeparator",lineSep);
  182. buffer.setDirty(true);
  183. }
  184. String encoding = (String)this.encoding.getSelectedItem();
  185. String oldEncoding = buffer.getStringProperty(JEditBuffer.ENCODING);
  186. if(!oldEncoding.equals(encoding))
  187. {
  188. buffer.setStringProperty(JEditBuffer.ENCODING,encoding);
  189. buffer.setDirty(true);
  190. }
  191. boolean gzippedValue = gzipped.isSelected();
  192. boolean oldGzipped = buffer.getBooleanProperty(
  193. Buffer.GZIPPED);
  194. if(gzippedValue != oldGzipped)
  195. {
  196. buffer.setBooleanProperty(Buffer.GZIPPED,gzippedValue);
  197. buffer.setDirty(true);
  198. }
  199. buffer.setStringProperty("folding",(String)folding.getSelectedItem());
  200. buffer.setStringProperty("wrap",(String)wrap.getSelectedItem());
  201. try
  202. {
  203. buffer.setProperty("maxLineLen",new Integer(
  204. maxLineLen.getSelectedItem().toString()));
  205. }
  206. catch(NumberFormatException nf)
  207. {
  208. }
  209. try
  210. {
  211. buffer.setProperty("tabSize",new Integer(
  212. tabSize.getSelectedItem().toString()));
  213. }
  214. catch(NumberFormatException nf)
  215. {
  216. }
  217. try
  218. {
  219. buffer.setProperty("indentSize",new Integer(
  220. indentSize.getSelectedItem().toString()));
  221. }
  222. catch(NumberFormatException nf)
  223. {
  224. }
  225. buffer.setBooleanProperty("noTabs",noTabs.isSelected());
  226. index = mode.getSelectedIndex();
  227. buffer.setMode(modes[index]);
  228. dispose();
  229. } //}}}
  230. //{{{ cancel() method
  231. public void cancel()
  232. {
  233. dispose();
  234. } //}}}
  235. //{{{ Private members
  236. private View view;
  237. private Buffer buffer;
  238. private Mode[] modes;
  239. private JComboBox mode;
  240. private JComboBox lineSeparator;
  241. private JComboBox encoding;
  242. private JCheckBox gzipped;
  243. private JComboBox folding;
  244. private JComboBox wrap;
  245. private JComboBox maxLineLen;
  246. private JComboBox tabSize;
  247. private JComboBox indentSize;
  248. private JCheckBox noTabs;
  249. private JButton ok;
  250. private JButton cancel;
  251. //}}}
  252. //{{{ ActionHandler class
  253. class ActionHandler implements ActionListener
  254. {
  255. //{{{ actionPerformed() method
  256. public void actionPerformed(ActionEvent evt)
  257. {
  258. Object source = evt.getSource();
  259. if(source == ok)
  260. ok();
  261. else if(source == cancel)
  262. cancel();
  263. else if(source == mode)
  264. {
  265. Mode _mode = (Mode)mode.getSelectedItem();
  266. folding.setSelectedItem(_mode.getProperty(
  267. "folding"));
  268. wrap.setSelectedItem(_mode.getProperty(
  269. "wrap"));
  270. maxLineLen.setSelectedItem(_mode.getProperty(
  271. "maxLineLen"));
  272. tabSize.setSelectedItem(_mode.getProperty(
  273. "tabSize"));
  274. indentSize.setSelectedItem(_mode.getProperty(
  275. "indentSize"));
  276. noTabs.setSelected(_mode.getBooleanProperty(
  277. "noTabs"));
  278. }
  279. } //}}}
  280. } //}}}
  281. }