/plugins/GdbPlugin/tags/release-0-5/gdb/options/LaunchConfigOptionPane.java

# · Java · 238 lines · 179 code · 26 blank · 33 comment · 15 complexity · 512be78c641c980699b27992d592843f MD5 · raw file

  1. /*
  2. Copyright (C) 2007 Shlomy Reinstein
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. */
  15. package gdb.options;
  16. import gdb.launch.LaunchConfiguration;
  17. import gdb.launch.LaunchConfigurationManager;
  18. import java.awt.Component;
  19. import java.awt.GridBagConstraints;
  20. import java.awt.event.ActionEvent;
  21. import java.awt.event.ActionListener;
  22. import java.util.Vector;
  23. import javax.swing.DefaultListCellRenderer;
  24. import javax.swing.DefaultListModel;
  25. import javax.swing.JButton;
  26. import javax.swing.JLabel;
  27. import javax.swing.JList;
  28. import javax.swing.JPanel;
  29. import javax.swing.JScrollPane;
  30. import javax.swing.ListSelectionModel;
  31. import javax.swing.border.EmptyBorder;
  32. import org.gjt.sp.jedit.AbstractOptionPane;
  33. import org.gjt.sp.jedit.GUIUtilities;
  34. import org.gjt.sp.jedit.jEdit;
  35. import debugger.jedit.Plugin;
  36. /** ************************************************************************** */
  37. public class LaunchConfigOptionPane extends AbstractOptionPane {
  38. /**
  39. *
  40. */
  41. private static final long serialVersionUID = 1L;
  42. /***************************************************************************
  43. * Vars
  44. **************************************************************************/
  45. private LaunchConfigurationManager configs;
  46. private JList configurationsList;
  47. private DefaultListModel configurationsModel;
  48. static final String PREFIX = Plugin.OPTION_PREFIX;
  49. static final String CONFIGURATIONS_LABEL = PREFIX + "configurations_label";
  50. static final String CONFIGURATION_LABEL = PREFIX + "configuration_label";
  51. static final String PROGRAM_LABEL = PREFIX + "program_label";
  52. static final String ARGUMENTS_LABEL = PREFIX + "arguments_label";
  53. static final String DIRECTORY_LABEL = PREFIX + "directory_label";
  54. static final String ENVIRONMENT_LABEL = PREFIX + "environment_label";
  55. private LaunchConfiguration currentConfig = null;
  56. private JButton delete;
  57. private JButton add;
  58. private JButton makeDefault;
  59. private JButton edit;
  60. private JButton copy;
  61. /***************************************************************************
  62. * Factory methods
  63. **************************************************************************/
  64. @SuppressWarnings("serial")
  65. public LaunchConfigOptionPane()
  66. {
  67. super("debugger.programs");
  68. configs = LaunchConfigurationManager.getInstance();
  69. setBorder(new EmptyBorder(5, 5, 5, 5));
  70. GridBagConstraints gbc = new GridBagConstraints();
  71. gbc.fill = GridBagConstraints.NONE;
  72. gbc.gridx = gbc.gridy = 0;
  73. gbc.weighty = 0.0;
  74. gbc.anchor = GridBagConstraints.NORTHWEST;
  75. add(new JLabel(jEdit.getProperty(CONFIGURATIONS_LABEL)), gbc);
  76. configurationsModel = new DefaultListModel();
  77. configurationsList = new JList(configurationsModel);
  78. configurationsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  79. configurationsList.setCellRenderer(new DefaultListCellRenderer() {
  80. public Component getListCellRendererComponent(JList list,
  81. Object value, int index, boolean isSelected, boolean cellHasFocus) {
  82. JLabel label = (JLabel) super.getListCellRendererComponent(list,
  83. value, index, isSelected, cellHasFocus);
  84. String configuration = configs.getName(index);
  85. String append = (index == configs.getDefaultIndex()) ?
  86. " *" : "";
  87. label.setText(configuration + append);
  88. return label;
  89. }
  90. });
  91. gbc.gridy++;
  92. gbc.weightx = gbc.weighty = 1.0;
  93. gbc.fill = GridBagConstraints.BOTH;
  94. add(new JScrollPane(configurationsList), gbc);
  95. JPanel buttons = new JPanel();
  96. add = new JButton("New");
  97. add.addActionListener(new ActionListener() {
  98. public void actionPerformed(ActionEvent e) {
  99. createNewConfiguration();
  100. }
  101. });
  102. buttons.add(add);
  103. copy = new JButton("Duplicate");
  104. copy.addActionListener(new ActionListener() {
  105. public void actionPerformed(ActionEvent e) {
  106. duplicateSelectedConfiguration();
  107. }
  108. });
  109. buttons.add(copy);
  110. delete = new JButton("Delete");
  111. delete.addActionListener(new ActionListener() {
  112. public void actionPerformed(ActionEvent e) {
  113. deleteSelectedConfiguration();
  114. }
  115. });
  116. buttons.add(delete);
  117. edit = new JButton("Edit");
  118. edit.addActionListener(new ActionListener() {
  119. public void actionPerformed(ActionEvent e) {
  120. editSelectedConfiguration();
  121. }
  122. });
  123. buttons.add(edit);
  124. makeDefault = new JButton("Make default");
  125. makeDefault.addActionListener(new ActionListener() {
  126. public void actionPerformed(ActionEvent e) {
  127. makeSelectedConfigurationDefault();
  128. }
  129. });
  130. buttons.add(makeDefault);
  131. gbc.gridy++;
  132. gbc.weightx = gbc.weighty = 0.0;
  133. add(buttons, gbc);
  134. // Finally, initialize the list of configurations
  135. for (int i = 0; i < configs.size(); i++)
  136. configurationsModel.addElement(configs.getName(i));
  137. if (configs.size() > 0) {
  138. int index = configs.getDefaultIndex();
  139. if (index >= 0 && index < configs.size())
  140. configurationsList.setSelectedIndex(index);
  141. }
  142. }
  143. private void makeSelectedConfigurationDefault()
  144. {
  145. int prevDefault = configs.getDefaultIndex();
  146. int defaultIndex = configurationsList.getSelectedIndex();
  147. configs.setDefaultIndex(defaultIndex);
  148. // These are required for updating the display
  149. if (prevDefault >= 0 && prevDefault < configs.size())
  150. configurationsModel.set(prevDefault, configs.getName(prevDefault));
  151. if (defaultIndex >= 0 && defaultIndex < configs.size())
  152. configurationsModel.set(defaultIndex, configs.getName(defaultIndex));
  153. }
  154. private void deleteSelectedConfiguration()
  155. {
  156. int index = configurationsList.getSelectedIndex();
  157. configs.remove(index);
  158. Vector<String> configNames = configs.getNames();
  159. configurationsList.setListData(configNames);
  160. if (! configNames.isEmpty()) {
  161. if (index == configNames.size())
  162. index--;
  163. configurationsList.setSelectedIndex(index);
  164. }
  165. }
  166. private void editSelectedConfiguration()
  167. {
  168. int index = configurationsList.getSelectedIndex();
  169. if (index > -1) {
  170. currentConfig = configs.getByIndex(index);
  171. LaunchConfigEditor d = new LaunchConfigEditor(GUIUtilities.getParentDialog(this), currentConfig);
  172. d.setVisible(true);
  173. }
  174. }
  175. private void createConfiguration(LaunchConfiguration config)
  176. {
  177. LaunchConfigEditor d = new LaunchConfigEditor(GUIUtilities.getParentDialog(this), config);
  178. d.setVisible(true);
  179. if (! d.accepted())
  180. return;
  181. configs.add(config);
  182. configurationsModel.addElement(config);
  183. configurationsList.setSelectedIndex(configs.size() - 1);
  184. }
  185. private void duplicateSelectedConfiguration()
  186. {
  187. int index = configurationsList.getSelectedIndex();
  188. if (index > -1) {
  189. currentConfig = configs.getByIndex(index);
  190. LaunchConfiguration newConfig = currentConfig.createDuplicate();
  191. newConfig.setName(configs.getNewName(currentConfig.getName()));
  192. createConfiguration(newConfig);
  193. }
  194. }
  195. private void createNewConfiguration()
  196. {
  197. LaunchConfiguration newConfig =
  198. new LaunchConfiguration("Unnamed", "", "", "", "");
  199. createConfiguration(newConfig);
  200. }
  201. /***************************************************************************
  202. * Implementation
  203. **************************************************************************/
  204. public void save()
  205. {
  206. configs.save();
  207. }
  208. }
  209. /** ***********************************************************************EOF */