/plugins/ant/src/com/intellij/lang/ant/config/explorer/SaveMetaTargetDialog.java

https://bitbucket.org/nbargnesi/idea · Java · 153 lines · 122 code · 16 blank · 15 comment · 5 complexity · fcf23d5a4dd102e284bba5d8b7793360 MD5 · raw file

  1. /*
  2. * Copyright 2000-2009 JetBrains s.r.o.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.intellij.lang.ant.config.explorer;
  17. import com.intellij.lang.ant.AntBundle;
  18. import com.intellij.lang.ant.config.AntBuildFile;
  19. import com.intellij.lang.ant.config.AntConfigurationBase;
  20. import com.intellij.lang.ant.config.impl.ExecuteCompositeTargetEvent;
  21. import com.intellij.openapi.ui.DialogWrapper;
  22. import com.intellij.openapi.ui.Messages;
  23. import com.intellij.ui.ListUtil;
  24. import com.intellij.ui.ScrollPaneFactory;
  25. import com.intellij.ui.components.JBList;
  26. import com.intellij.util.ArrayUtil;
  27. import javax.swing.*;
  28. import javax.swing.event.ListSelectionEvent;
  29. import javax.swing.event.ListSelectionListener;
  30. import java.awt.*;
  31. import java.awt.event.ActionEvent;
  32. import java.awt.event.ActionListener;
  33. public class SaveMetaTargetDialog extends DialogWrapper {
  34. private JList myTargetList;
  35. private JTextField myTfName;
  36. private final ExecuteCompositeTargetEvent myInitialEvent;
  37. private final AntConfigurationBase myAntConfiguration;
  38. private final AntBuildFile myBuildFile;
  39. public SaveMetaTargetDialog(final Component parent,
  40. final ExecuteCompositeTargetEvent event,
  41. final AntConfigurationBase antConfiguration,
  42. final AntBuildFile buildFile) {
  43. super(parent, true);
  44. myInitialEvent = event;
  45. myAntConfiguration = antConfiguration;
  46. myBuildFile = buildFile;
  47. setModal(true);
  48. init();
  49. }
  50. protected String getDimensionServiceKey() {
  51. return "#com.intellij.ant.explorer.SaveMetaTargetDialog";
  52. }
  53. protected Action[] createActions() {
  54. return new Action[]{getOKAction(), getCancelAction()};
  55. }
  56. protected void doOKAction() {
  57. final ExecuteCompositeTargetEvent eventObject = createEventObject();
  58. if (myAntConfiguration.getTargetForEvent(eventObject) == null) {
  59. myAntConfiguration.setTargetForEvent(myBuildFile, eventObject.getMetaTargetName(), eventObject);
  60. super.doOKAction();
  61. }
  62. else {
  63. Messages.showInfoMessage(getContentPane(), AntBundle.message("save.meta.data.such.sequence.of.targets.already.exists.error.message"),
  64. getTitle());
  65. }
  66. }
  67. public JComponent getPreferredFocusedComponent() {
  68. return myTfName;
  69. }
  70. protected JComponent createCenterPanel() {
  71. final JPanel panel = new JPanel(new GridBagLayout());
  72. final JLabel nameLabel = new JLabel(AntBundle.message("save.meta.data.name.label"));
  73. panel.add(nameLabel, new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST,
  74. GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
  75. myTfName = new JTextField(myInitialEvent.getPresentableName());
  76. nameLabel.setLabelFor(myTfName);
  77. myTfName.selectAll();
  78. panel.add(myTfName, new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST,
  79. GridBagConstraints.HORIZONTAL, new Insets(4, 0, 0, 0), 0, 0));
  80. final DefaultListModel dataModel = new DefaultListModel();
  81. myTargetList = new JBList(dataModel);
  82. final String[] targetNames = myInitialEvent.getTargetNames();
  83. for (String name : targetNames) {
  84. dataModel.addElement(name);
  85. }
  86. panel.add(new JLabel(AntBundle.message("save.meta.data.targets.label")), new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1,
  87. 0.0, 0.0, GridBagConstraints.NORTHWEST,
  88. GridBagConstraints.NONE,
  89. new Insets(6, 0, 0, 0), 0, 0));
  90. panel.add(ScrollPaneFactory.createScrollPane(myTargetList), new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 2, 1.0, 1.0,
  91. GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH,
  92. new Insets(4, 0, 0, 0), 0, 0));
  93. final JButton upButton = new JButton(AntBundle.message("button.move.up"));
  94. panel.add(upButton, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL,
  95. new Insets(6, 6, 0, 0), 0, 0));
  96. final JButton downButton = new JButton(AntBundle.message("button.move.down"));
  97. panel.add(downButton, new GridBagConstraints(1, 4, 1, GridBagConstraints.REMAINDER, 0.0, 1.0, GridBagConstraints.NORTHWEST,
  98. GridBagConstraints.HORIZONTAL, new Insets(0, 6, 0, 0), 0, 0));
  99. class UpdateAction implements ActionListener {
  100. public void actionPerformed(ActionEvent e) {
  101. upButton.setEnabled(ListUtil.canMoveSelectedItemsUp(myTargetList));
  102. downButton.setEnabled(ListUtil.canMoveSelectedItemsDown(myTargetList));
  103. }
  104. }
  105. upButton.addActionListener(new UpdateAction() {
  106. public void actionPerformed(ActionEvent e) {
  107. ListUtil.moveSelectedItemsUp(myTargetList);
  108. super.actionPerformed(e);
  109. }
  110. });
  111. downButton.addActionListener(new UpdateAction() {
  112. public void actionPerformed(ActionEvent e) {
  113. ListUtil.moveSelectedItemsDown(myTargetList);
  114. super.actionPerformed(e);
  115. }
  116. });
  117. myTargetList.addListSelectionListener(new ListSelectionListener() {
  118. public void valueChanged(ListSelectionEvent e) {
  119. upButton.setEnabled(ListUtil.canMoveSelectedItemsUp(myTargetList));
  120. downButton.setEnabled(ListUtil.canMoveSelectedItemsDown(myTargetList));
  121. }
  122. });
  123. myTargetList.setSelectedIndex(0);
  124. return panel;
  125. }
  126. private ExecuteCompositeTargetEvent createEventObject() {
  127. final ListModel model = myTargetList.getModel();
  128. final int size = model.getSize();
  129. final String[] names = ArrayUtil.newStringArray(size);
  130. for (int idx = 0; idx < size; idx++) {
  131. names[idx] = (String)model.getElementAt(idx);
  132. }
  133. final ExecuteCompositeTargetEvent event = new ExecuteCompositeTargetEvent(names);
  134. event.setPresentableName(myTfName.getText().trim());
  135. return event;
  136. }
  137. }