/plugins/ProjectViewer/tags/pv_2_9_1/projectviewer/config/ExtensionConfigPane.java

# · Java · 241 lines · 166 code · 44 blank · 31 comment · 18 complexity · 873a75102d5c23da841646da3c91df9b MD5 · raw file

  1. /*
  2. * :tabSize=4:indentSize=4:noTabs=false:
  3. * :folding=explicit:collapseFolds=1:
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package projectviewer.config;
  20. import java.awt.BorderLayout;
  21. import java.awt.Component;
  22. import java.awt.event.ItemEvent;
  23. import java.awt.event.ItemListener;
  24. import java.awt.event.MouseAdapter;
  25. import java.awt.event.MouseEvent;
  26. import java.util.Arrays;
  27. import java.util.ArrayList;
  28. import java.util.Collections;
  29. import java.util.Comparator;
  30. import java.util.List;
  31. import javax.swing.DefaultListCellRenderer;
  32. import javax.swing.JCheckBox;
  33. import javax.swing.JComboBox;
  34. import javax.swing.JLabel;
  35. import javax.swing.JList;
  36. import org.gjt.sp.jedit.ServiceManager;
  37. import projectviewer.gui.OptionPaneBase;
  38. import static projectviewer.config.ExtensionManager.ManagedService;
  39. /**
  40. * Config pane for managing available extensions. This allows the user
  41. * to effectivelly disable extensions to ProjectViewer.
  42. *
  43. * The extension is still instantiated since jEdit's "service"
  44. * mechanism makes it pretty hard to correlate names to the service's
  45. * implementation class, so as part of doing that PV will cause the
  46. * service to be instantiated. So this only controls whether the
  47. * extension is show by the PV UI, not whether it's loaded at all.
  48. *
  49. * @author Marcelo Vanzin
  50. * @since PV 3.0.0
  51. */
  52. public class ExtensionConfigPane
  53. extends OptionPaneBase
  54. implements ItemListener
  55. {
  56. private List<ServiceData> svcs;
  57. private JComboBox types;
  58. private ExtensionManager mgr = ExtensionManager.getInstance();
  59. private ProjectViewerConfig config = ProjectViewerConfig.getInstance();
  60. public ExtensionConfigPane()
  61. {
  62. super("projectviewer.optiongroup.extensions",
  63. "projectviewer.options.ext_cfg");
  64. }
  65. public void itemStateChanged(ItemEvent e)
  66. {
  67. ServiceData sd = (ServiceData) e.getItem();
  68. if (e.getStateChange() == ItemEvent.SELECTED) {
  69. add(BorderLayout.CENTER, sd.list);
  70. revalidate();
  71. } else if (e.getStateChange() == ItemEvent.DESELECTED) {
  72. remove(sd.list);
  73. revalidate();
  74. }
  75. }
  76. protected void _init()
  77. {
  78. JLabel help;
  79. List<ManagedService> services = mgr.getServices();
  80. Collections.sort(services, new ServiceComparator());
  81. setLayout(new BorderLayout());
  82. types = new JComboBox();
  83. types.setRenderer(new ServiceRenderer());
  84. add(BorderLayout.NORTH, types);
  85. svcs = new ArrayList<ServiceData>();
  86. for (ManagedService ms : services) {
  87. ServiceData sd = new ServiceData(ms);
  88. types.addItem(sd);
  89. svcs.add(sd);
  90. }
  91. types.setSelectedItem(0);
  92. types.addItemListener(this);
  93. add(BorderLayout.CENTER, svcs.get(0).list);
  94. help = new JLabel(prop("help"));
  95. add(BorderLayout.SOUTH, help);
  96. }
  97. protected void _save()
  98. {
  99. for (ServiceData sd : svcs) {
  100. String type = sd.service.getServiceClass().getName();
  101. if (sd.cbs != null) {
  102. for (JCheckBox cb : sd.cbs) {
  103. Class clazz = ServiceManager.getService(type, cb.getText())
  104. .getClass();
  105. if (cb.isSelected()) {
  106. config.enableExtension(type, clazz.getName());
  107. } else {
  108. config.disableExtension(type, clazz.getName());
  109. }
  110. }
  111. }
  112. }
  113. mgr.reloadExtensions();
  114. config.save();
  115. }
  116. private class ServiceComparator implements Comparator<ManagedService>
  117. {
  118. public int compare(ManagedService o1,
  119. ManagedService o2)
  120. {
  121. return o1.getServiceName().compareTo(o2.getServiceName());
  122. }
  123. }
  124. private class ServiceData
  125. {
  126. ServiceData(ManagedService svc)
  127. {
  128. String type = svc.getServiceClass().getName();
  129. String[] names = ServiceManager.getServiceNames(type);
  130. Arrays.sort(names);
  131. this.service = svc;
  132. if (names == null) {
  133. cbs = null;
  134. list = new JList();
  135. } else {
  136. cbs = new JCheckBox[names.length];
  137. for (int i = 0; i < cbs.length; i++) {
  138. Class clazz = ServiceManager.getService(type, names[i])
  139. .getClass();
  140. cbs[i] = new JCheckBox(names[i]);
  141. cbs[i].setSelected(config.isExtensionEnabled(type,
  142. clazz.getName()));
  143. }
  144. list = new JList(cbs);
  145. list.setCellRenderer(new ServiceListRenderer());
  146. list.addMouseListener(new ServiceListMouseHandler());
  147. }
  148. }
  149. JList list;
  150. JCheckBox cbs[];
  151. ManagedService service;
  152. }
  153. private class ServiceRenderer extends DefaultListCellRenderer
  154. {
  155. public Component getListCellRendererComponent(JList list,
  156. Object value,
  157. int index,
  158. boolean isSelected,
  159. boolean cellHasFocus)
  160. {
  161. super.getListCellRendererComponent(list, value, index,
  162. isSelected, cellHasFocus);
  163. setText(((ServiceData)value).service.getServiceName());
  164. return this;
  165. }
  166. }
  167. private class ServiceListRenderer extends DefaultListCellRenderer
  168. {
  169. public Component getListCellRendererComponent(JList list,
  170. Object value,
  171. int index,
  172. boolean isSelected,
  173. boolean cellHasFocus)
  174. {
  175. JCheckBox item = (JCheckBox) value;
  176. item.setBackground(getBackground());
  177. item.setForeground(getForeground());
  178. item.setFont(getFont());
  179. item.setFocusPainted(false);
  180. return item;
  181. }
  182. }
  183. private class ServiceListMouseHandler extends MouseAdapter
  184. {
  185. public void mousePressed(MouseEvent e)
  186. {
  187. JList list = (JList) e.getSource();
  188. int index = list.locationToIndex(e.getPoint());
  189. if (index != -1) {
  190. JCheckBox checkbox = (JCheckBox) list.getModel()
  191. .getElementAt(index);
  192. checkbox.setSelected(!checkbox.isSelected());
  193. repaint();
  194. }
  195. }
  196. }
  197. }