PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/SideKick/sidekick/ModeOptionsDialog.java

#
Java | 206 lines | 133 code | 27 blank | 46 comment | 8 complexity | e441ebb1fdab38616fb79882a2388ba9 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. * ModeOptionsDialog.java
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2006 Alan Ezust
  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 sidekick;
  23. // {{{ imports
  24. import java.awt.BorderLayout;
  25. import java.awt.GridLayout;
  26. import java.awt.event.ActionEvent;
  27. import java.awt.event.ActionListener;
  28. import java.util.Collections;
  29. import java.util.Enumeration;
  30. import java.util.Vector;
  31. import javax.swing.JCheckBox;
  32. import javax.swing.JComboBox;
  33. import javax.swing.JLabel;
  34. import javax.swing.JPanel;
  35. import javax.swing.event.TreeSelectionEvent;
  36. import org.gjt.sp.jedit.MiscUtilities;
  37. import org.gjt.sp.jedit.OptionGroup;
  38. import org.gjt.sp.jedit.ServiceManager;
  39. import org.gjt.sp.jedit.View;
  40. import org.gjt.sp.jedit.jEdit;
  41. import org.gjt.sp.jedit.gui.OptionsDialog;
  42. import org.gjt.sp.util.StandardUtilities;
  43. import org.gjt.sp.util.StringList;
  44. // }}}
  45. /** {{{ ModeOptionsDialog class
  46. * A customized OptionDialog for
  47. * SideKick, which includes a shared ComboBox for the current edit
  48. * mode.
  49. *
  50. * It creates an OptionPane for each plugin that defines the proper
  51. * service, and is currently loaded.
  52. *
  53. * @see AbstractModeOptionPane
  54. * @author Alan Ezust
  55. *
  56. */
  57. public class ModeOptionsDialog extends OptionsDialog
  58. {
  59. // {{{ data members
  60. public static final String SERVICECLASS="org.gjt.sp.jedit.options.ModeOptionPane";
  61. public static final String DEFAULT=jEdit.getProperty("options.editing.global");
  62. public static final String ALL="ALL";
  63. Vector<ModeOptionPane> panes;
  64. OptionTreeModel paneTreeModel;
  65. StringList modes;
  66. JComboBox modeCombo;
  67. JCheckBox useDefaultsCheck;
  68. // }}}
  69. // {{{ ModeOptionsDialog ctor
  70. public ModeOptionsDialog(View v) {
  71. super(v, "options.mode.settings", "sidekick.mode");
  72. } // }}}
  73. // {{{ getMode()
  74. public String getMode() {
  75. return modeCombo.getSelectedItem().toString();
  76. } // }}}
  77. // {{{ createOptionTreeModel method
  78. protected OptionTreeModel createOptionTreeModel()
  79. {
  80. modes = new StringList(jEdit.getModes());
  81. Collections.sort(modes,new StandardUtilities.StringCompare(true));
  82. modes.add(0, DEFAULT);
  83. modeCombo = new JComboBox(modes.toArray());
  84. useDefaultsCheck = new JCheckBox(jEdit.getProperty("options.editing.useDefaults"));
  85. JLabel editModeLabel = new JLabel(jEdit.getProperty("buffer-options.mode"));
  86. GridLayout gl = new GridLayout(1, 3);
  87. JPanel editModePanel = new JPanel(gl);
  88. // JLabel spacer = new JLabel(" ");
  89. //for (int i=0; i<3; ++i) editModePanel.add(spacer);
  90. editModePanel.add(useDefaultsCheck);
  91. editModePanel.add(editModeLabel);
  92. editModePanel.add(modeCombo);
  93. JPanel content = (JPanel) getContentPane();
  94. content.add(editModePanel, BorderLayout.NORTH);
  95. paneTreeModel = new OptionTreeModel();
  96. OptionGroup root = (OptionGroup) (paneTreeModel.getRoot());
  97. panes = new Vector<ModeOptionPane>();
  98. // iterate through all parsers and get their name, attempt to get an option pane.
  99. for (String service: ServiceManager.getServiceNames(SERVICECLASS))
  100. {
  101. AbstractModeOptionPane mop = (AbstractModeOptionPane) ServiceManager.getService(SERVICECLASS, service);
  102. root.addOptionPane(mop);
  103. panes.add(mop);
  104. }
  105. modeCombo.addActionListener(new ActionListener() {
  106. public void actionPerformed(ActionEvent event) {
  107. modeSelected();
  108. }
  109. });
  110. useDefaultsCheck.addActionListener(new ActionListener() {
  111. public void actionPerformed(ActionEvent e) {
  112. useDefaultsChanged();
  113. }
  114. });
  115. String currentMode = jEdit.getActiveView().getBuffer().getMode().getName();
  116. modeCombo.setSelectedItem(currentMode);
  117. return paneTreeModel;
  118. } // }}}
  119. private void useDefaultsChanged() {
  120. if (currentPane instanceof AbstractModeOptionPane)
  121. ((AbstractModeOptionPane)currentPane).setUseDefaults(
  122. useDefaultsCheck.isSelected());
  123. }
  124. private void modeSelected()
  125. {
  126. int index = modeCombo.getSelectedIndex();
  127. String mode;
  128. if (index == 0) {
  129. mode = null;
  130. useDefaultsCheck.setEnabled(false);
  131. } else {
  132. mode = (String) modeCombo.getItemAt(index);
  133. useDefaultsCheck.setEnabled(true);
  134. }
  135. if (currentPane instanceof AbstractModeOptionPane)
  136. {
  137. AbstractModeOptionPane current = (AbstractModeOptionPane)currentPane;
  138. current.modeSelected(mode);
  139. useDefaultsCheck.setSelected(current.getUseDefaults(mode));
  140. }
  141. else
  142. {
  143. useDefaultsCheck.setSelected(false);
  144. }
  145. }
  146. @Override
  147. public void valueChanged(TreeSelectionEvent evt) {
  148. super.valueChanged(evt);
  149. modeSelected(); // Update the current pane with the dialog state
  150. }
  151. // {{{ load() methods
  152. private void load(Object obj)
  153. {
  154. if(obj instanceof OptionGroup)
  155. {
  156. OptionGroup grp = (OptionGroup)obj;
  157. Enumeration members = grp.getMembers();
  158. while(members.hasMoreElements())
  159. {
  160. load(members.nextElement());
  161. }
  162. }
  163. }
  164. protected void load() {
  165. load(getDefaultGroup());
  166. } // }}}
  167. // {{{ getDefaultGroup() method
  168. protected OptionGroup getDefaultGroup()
  169. {
  170. return (OptionGroup) paneTreeModel.getRoot();
  171. } // }}}
  172. @Override
  173. public void cancel() {
  174. // Clear the temporary mode data in the panes
  175. for (int i = 0; i < panes.size(); i++)
  176. panes.get(i).cancel();
  177. super.cancel();
  178. }
  179. } // }}}