PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/soa.ui/src/org/netbeans/modules/soa/ui/form/valid/AbstractDialogDescriptor.java

https://bitbucket.org/rsaqc/netbeans-soa
Java | 176 lines | 120 code | 17 blank | 39 comment | 24 complexity | 059b91cab61107b3381b2e9670bd8e9a MD5 | raw file
  1. /*
  2. * The contents of this file are subject to the terms of the Common Development
  3. * and Distribution License (the License). You may not use this file except in
  4. * compliance with the License.
  5. *
  6. * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
  7. * or http://www.netbeans.org/cddl.txt.
  8. *
  9. * When distributing Covered Code, include this CDDL Header Notice in each file
  10. * and include the License file at http://www.netbeans.org/cddl.txt.
  11. * If applicable, add the following below the CDDL Header, with the fields
  12. * enclosed by brackets [] replaced by your own identifying information:
  13. * "Portions Copyrighted [year] [name of copyright owner]"
  14. *
  15. * The Original Software is NetBeans. The Initial Developer of the Original
  16. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
  17. * Microsystems, Inc. All Rights Reserved.
  18. */
  19. package org.netbeans.modules.soa.ui.form.valid;
  20. import java.awt.Container;
  21. import java.awt.event.ActionEvent;
  22. import java.awt.event.ActionListener;
  23. import java.util.ArrayList;
  24. import java.util.Arrays;
  25. import java.util.List;
  26. import javax.swing.JButton;
  27. import javax.swing.JOptionPane;
  28. import org.netbeans.modules.soa.ui.form.CommonUiBundle;
  29. import org.netbeans.modules.soa.ui.form.FormLifeCycle;
  30. import org.netbeans.modules.soa.ui.form.valid.Validator.Reason;
  31. import org.netbeans.modules.soa.ui.form.valid.Validator.Severity;
  32. import org.openide.DialogDescriptor;
  33. import org.openide.util.NbBundle;
  34. /**
  35. * Base class for DialogDescriptors which support the dialog's validation and life cycle
  36. *
  37. * @author nk160297
  38. */
  39. public abstract class AbstractDialogDescriptor extends DialogDescriptor
  40. implements ValidStateManager.ValidStateListener {
  41. protected JButton btnOk;
  42. protected JButton btnCancel;
  43. private ValidStateManager.Provider myVsmProvider;
  44. public AbstractDialogDescriptor(Object innerPane, String title) {
  45. super(innerPane, title, true, JOptionPane.DEFAULT_OPTION,
  46. null, DEFAULT_ALIGN, null, null);
  47. //
  48. assert innerPane != null;
  49. assert innerPane instanceof Container;
  50. assert innerPane instanceof FormLifeCycle;
  51. //
  52. btnOk = new JButton(NbBundle.getMessage(CommonUiBundle.class, "BTN_Ok")); // NOI18N
  53. btnOk.getAccessibleContext().setAccessibleName(NbBundle.getMessage(CommonUiBundle.class,"ACSN_BTN_Ok")); // NOI18N
  54. btnOk.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CommonUiBundle.class,"ACSD_BTN_Ok")); // NOI18N
  55. btnCancel = new JButton(NbBundle.getMessage(CommonUiBundle.class, "BTN_Cancel")); // NOI18N
  56. btnCancel.getAccessibleContext().setAccessibleName(NbBundle.getMessage(CommonUiBundle.class,"ACSN_BTN_Cancel")); // NOI18N
  57. btnCancel.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(CommonUiBundle.class,"ACSD_BTN_Cancel")); // NOI18N
  58. setOptions(new Object[] {btnOk, btnCancel});
  59. //
  60. setClosingOptions(new Object[] {btnCancel});
  61. //
  62. ActionListener buttonListener = new ActionListener() {
  63. public void actionPerformed(ActionEvent event) {
  64. if (btnOk.equals(event.getSource())) {
  65. processOkButton();
  66. }
  67. }
  68. };
  69. setButtonListener(buttonListener);
  70. //
  71. ValidStateManager validStateManager = getValidStateManager(true);
  72. if (validStateManager != null) {
  73. validStateManager.addValidStateListener(this);
  74. // update to initial value
  75. updateOkButton(validStateManager);
  76. }
  77. }
  78. public synchronized ValidStateManager getValidStateManager(boolean isFast) {
  79. if (myVsmProvider == null) {
  80. final Object innerPane = getMessage();
  81. if (innerPane instanceof ValidStateManager.Provider) {
  82. myVsmProvider = (ValidStateManager.Provider)innerPane;
  83. } else if (innerPane instanceof ValidStateManager) {
  84. myVsmProvider = new ValidStateManager.Provider() {
  85. public ValidStateManager getValidStateManager(boolean isFast) {
  86. return (ValidStateManager)innerPane;
  87. }
  88. };
  89. } else {
  90. myVsmProvider = new ValidStateManager.Provider.Default();
  91. }
  92. }
  93. return myVsmProvider.getValidStateManager(isFast);
  94. }
  95. @Override
  96. public Object getDefaultValue() {
  97. return btnOk;
  98. }
  99. /**
  100. * Indicates if the Ok button has pressed.
  101. * This method is intended to be called after dialog has closed to
  102. * check if the Ok button was pressed.
  103. */
  104. public boolean isOkHasPressed() {
  105. return getValue() == btnOk;
  106. }
  107. public abstract void processOkButton();
  108. public abstract void processWindowClose();
  109. @Override
  110. public void setMessage(Object innerPane) {
  111. assert innerPane != null;
  112. assert innerPane instanceof Container;
  113. assert innerPane instanceof FormLifeCycle;
  114. //
  115. ValidStateManager validStateManager = getValidStateManager(true);
  116. if (validStateManager != null) {
  117. validStateManager.removeValidStateListener(this);
  118. }
  119. //
  120. myVsmProvider = null;
  121. super.setMessage(innerPane);
  122. //
  123. validStateManager = getValidStateManager(true);
  124. if (validStateManager != null) {
  125. validStateManager.addValidStateListener(this);
  126. }
  127. }
  128. public void stateChanged(ValidStateManager source, boolean isValid) {
  129. updateOkButton(source);
  130. }
  131. protected void setOptionClosable(Object option, boolean flag) {
  132. List<Object> cOptions =
  133. new ArrayList<Object>(Arrays.asList(getClosingOptions()));
  134. //
  135. if (flag) {
  136. if (!cOptions.contains(option)) {
  137. cOptions.add(option);
  138. }
  139. } else {
  140. if (cOptions.contains(option)) {
  141. cOptions.remove(option);
  142. }
  143. }
  144. //
  145. setClosingOptions(cOptions.toArray());
  146. }
  147. protected void updateOkButton(ValidStateManager validStateManager) {
  148. if (validStateManager.isValid()) {
  149. btnOk.setEnabled(true);
  150. btnOk.setToolTipText(null);
  151. } else {
  152. btnOk.setEnabled(false);
  153. Reason reason = validStateManager.getFistReason(Severity.ERROR);
  154. if (reason != null) {
  155. btnOk.setToolTipText(reason.getText());
  156. }
  157. }
  158. }
  159. }