/jEdit/branches/lp_clone_test/org/gjt/sp/jedit/gui/StyleEditor.java

# · Java · 235 lines · 169 code · 29 blank · 37 comment · 25 complexity · 27af5228d89a6945d13bdf1112dfb348 MD5 · raw file

  1. /*
  2. * StyleEditor.java - Style editor dialog
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1999, 2000, 2001 Slava Pestov
  7. * Portions copyright (C) 1999 mike dillon
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. package org.gjt.sp.jedit.gui;
  24. import java.awt.BorderLayout;
  25. import java.awt.Color;
  26. import java.awt.Component;
  27. import java.awt.Font;
  28. import java.awt.GridLayout;
  29. import java.awt.event.ActionEvent;
  30. import java.awt.event.ActionListener;
  31. import javax.swing.Box;
  32. import javax.swing.BoxLayout;
  33. import javax.swing.JButton;
  34. import javax.swing.JCheckBox;
  35. import javax.swing.JDialog;
  36. import javax.swing.JFrame;
  37. import javax.swing.JLabel;
  38. import javax.swing.JPanel;
  39. import javax.swing.border.EmptyBorder;
  40. import javax.swing.JOptionPane;
  41. import org.gjt.sp.jedit.jEdit;
  42. import org.gjt.sp.jedit.GUIUtilities;
  43. import org.gjt.sp.jedit.syntax.SyntaxStyle;
  44. import org.gjt.sp.jedit.syntax.DefaultTokenHandler;
  45. import org.gjt.sp.jedit.syntax.Token;
  46. import org.gjt.sp.jedit.textarea.JEditTextArea;
  47. import org.gjt.sp.jedit.buffer.JEditBuffer;
  48. //{{{ StyleEditor class
  49. public class StyleEditor extends EnhancedDialog implements ActionListener
  50. {
  51. //{{{ invokeForCaret() method
  52. /**
  53. * Edit the syntax style of the token under the caret.
  54. *
  55. * @param textArea the textarea where your caret is
  56. * @since jEdit 4.4pre1
  57. */
  58. public static void invokeForCaret(JEditTextArea textArea)
  59. {
  60. JEditBuffer buffer = textArea.getBuffer();
  61. int lineNum = textArea.getCaretLine();
  62. int start = buffer.getLineStartOffset(lineNum);
  63. int position = textArea.getCaretPosition();
  64. DefaultTokenHandler tokenHandler = new DefaultTokenHandler();
  65. buffer.markTokens(lineNum,tokenHandler);
  66. Token token = tokenHandler.getTokens();
  67. while(token.id != Token.END)
  68. {
  69. int next = start + token.length;
  70. if (start <= position && next > position)
  71. break;
  72. start = next;
  73. token = token.next;
  74. }
  75. if (token.id == Token.END || token.id == Token.NULL)
  76. {
  77. JOptionPane.showMessageDialog(textArea.getView(),
  78. jEdit.getProperty("syntax-style-no-token.message"),
  79. jEdit.getProperty("syntax-style-no-token.title"),
  80. JOptionPane.PLAIN_MESSAGE);
  81. return;
  82. }
  83. String typeName = Token.tokenToString(token.id);
  84. String property = "view.style." + typeName.toLowerCase();
  85. SyntaxStyle currentStyle = GUIUtilities.parseStyle(
  86. jEdit.getProperty(property), "Dialog",12);
  87. SyntaxStyle style = new StyleEditor(textArea.getView(),
  88. currentStyle, typeName).getStyle();
  89. if(style != null)
  90. {
  91. jEdit.setProperty(property, GUIUtilities.getStyleString(style));
  92. jEdit.propertiesChanged();
  93. }
  94. } //}}}
  95. //{{{ StyleEditor constructor
  96. public StyleEditor(JDialog parent, SyntaxStyle style, String styleName)
  97. {
  98. super(parent, jEdit.getProperty("style-editor.title"),true);
  99. initialize(parent, style, styleName);
  100. }
  101. public StyleEditor(JFrame parent, SyntaxStyle style, String styleName)
  102. {
  103. super(parent, jEdit.getProperty("style-editor.title"),true);
  104. initialize(parent, style, styleName);
  105. }
  106. private void initialize(Component comp, SyntaxStyle style, String styleName)
  107. {
  108. JPanel content = new JPanel(new BorderLayout(12,12));
  109. content.setBorder(new EmptyBorder(12,12,12,12));
  110. setContentPane(content);
  111. JPanel panel = new JPanel(new GridLayout(5,2,12,12));
  112. panel.add(new JLabel(jEdit.getProperty("style-editor.tokenType")));
  113. panel.add(new JLabel(styleName));
  114. italics = new JCheckBox(jEdit.getProperty("style-editor.italics"));
  115. italics.setSelected(style.getFont().isItalic());
  116. panel.add(italics);
  117. panel.add(new JLabel());
  118. bold = new JCheckBox(jEdit.getProperty("style-editor.bold"));
  119. bold.setSelected(style.getFont().isBold());
  120. panel.add(bold);
  121. panel.add(new JLabel());
  122. Color fg = style.getForegroundColor();
  123. fgColorCheckBox = new JCheckBox(jEdit.getProperty("style-editor.fgColor"));
  124. fgColorCheckBox.setSelected(fg != null);
  125. fgColorCheckBox.addActionListener(this);
  126. panel.add(fgColorCheckBox);
  127. fgColor = new ColorWellButton(fg);
  128. fgColor.setEnabled(fg != null);
  129. panel.add(fgColor);
  130. Color bg = style.getBackgroundColor();
  131. bgColorCheckBox = new JCheckBox(jEdit.getProperty("style-editor.bgColor"));
  132. bgColorCheckBox.setSelected(bg != null);
  133. bgColorCheckBox.addActionListener(this);
  134. panel.add(bgColorCheckBox);
  135. bgColor = new ColorWellButton(bg);
  136. bgColor.setEnabled(bg != null);
  137. panel.add(bgColor);
  138. content.add(BorderLayout.CENTER,panel);
  139. Box box = new Box(BoxLayout.X_AXIS);
  140. box.add(Box.createGlue());
  141. box.add(ok = new JButton(jEdit.getProperty("common.ok")));
  142. getRootPane().setDefaultButton(ok);
  143. ok.addActionListener(this);
  144. box.add(Box.createHorizontalStrut(6));
  145. box.add(cancel = new JButton(jEdit.getProperty("common.cancel")));
  146. cancel.addActionListener(this);
  147. box.add(Box.createGlue());
  148. content.add(BorderLayout.SOUTH,box);
  149. pack();
  150. setLocationRelativeTo(comp);
  151. setResizable(false);
  152. setVisible(true);
  153. } //}}}
  154. //{{{ actionPerformed() method
  155. public void actionPerformed(ActionEvent evt)
  156. {
  157. Object source = evt.getSource();
  158. if(source == ok)
  159. ok();
  160. else if(source == cancel)
  161. cancel();
  162. else if(source == fgColorCheckBox)
  163. fgColor.setEnabled(fgColorCheckBox.isSelected());
  164. else if(source == bgColorCheckBox)
  165. bgColor.setEnabled(bgColorCheckBox.isSelected());
  166. } //}}}
  167. //{{{ ok() method
  168. public void ok()
  169. {
  170. okClicked = true;
  171. dispose();
  172. } //}}}
  173. //{{{ cancel() method
  174. public void cancel()
  175. {
  176. dispose();
  177. } //}}}
  178. //{{{ getStyle() method
  179. public SyntaxStyle getStyle()
  180. {
  181. if(!okClicked)
  182. return null;
  183. Color foreground = (fgColorCheckBox.isSelected()
  184. ? fgColor.getSelectedColor()
  185. : null);
  186. Color background = (bgColorCheckBox.isSelected()
  187. ? bgColor.getSelectedColor()
  188. : null);
  189. return new SyntaxStyle(foreground,background,
  190. new Font("Dialog",
  191. (italics.isSelected() ? Font.ITALIC : 0)
  192. | (bold.isSelected() ? Font.BOLD : 0),
  193. 12));
  194. } //}}}
  195. //{{{ Private members
  196. private JCheckBox italics;
  197. private JCheckBox bold;
  198. private JCheckBox fgColorCheckBox;
  199. private ColorWellButton fgColor;
  200. private JCheckBox bgColorCheckBox;
  201. private ColorWellButton bgColor;
  202. private JButton ok;
  203. private JButton cancel;
  204. private boolean okClicked;
  205. //}}}
  206. } //}}}